1 # git-gui Git repository chooser
2 # Copyright (C) 2007 Shawn Pearce
4 class choose_repository {
8 field w_body ; # Widget holding the center content
9 field w_next ; # Next button
10 field w_quit ; # Quit button
11 field o_cons ; # Console object (if active)
12 field w_types ; # List of type buttons in clone
13 field w_recentlist ; # Listbox containing recent repositories
15 field done 0 ; # Finished picking the repository?
16 field local_path {} ; # Where this repository is locally
17 field origin_url {} ; # Where we are cloning from
18 field origin_name origin ; # What we shall call 'origin'
19 field clone_type hardlink ; # Type of clone to construct
20 field readtree_err ; # Error output from read-tree (if any)
21 field sorted_recent ; # recent repositories (sorted)
27 wm title $top [mc "Git Gui"]
30 menu $w.mbar -tearoff 0
31 $top configure -menu $w.mbar
34 -label [mc Repository] \
35 -menu $w.mbar.repository
36 menu $w.mbar.repository
37 $w.mbar.repository add command \
43 $w.mbar add cascade -label [mc Apple] -menu .mbar.apple
45 $w.mbar.apple add command \
46 -label [mc "About %s" [appname]] \
49 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
51 $w.mbar.help add command \
52 -label [mc "About %s" [appname]] \
56 wm protocol $top WM_DELETE_WINDOW exit
57 bind $top <$M1B-q> exit
58 bind $top <$M1B-Q> exit
59 bind $top <Key-Escape> exit
61 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
62 bind $top <Key-Escape> [list destroy $top]
65 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
68 set opts $w_body.options
71 -cursor $::cursor_ptr \
73 -background [$w_body cget -background] \
78 pack $opts -anchor w -fill x
80 $opts tag conf link_new -foreground blue -underline 1
81 $opts tag bind link_new <1> [cb _next new]
82 $opts insert end [mc "Create New Repository"] link_new
85 $opts tag conf link_clone -foreground blue -underline 1
86 $opts tag bind link_clone <1> [cb _next clone]
87 $opts insert end [mc "Clone Existing Repository"] link_clone
90 $opts tag conf link_open -foreground blue -underline 1
91 $opts tag bind link_open <1> [cb _next open]
92 $opts insert end [mc "Open Existing Repository"] link_open
95 set sorted_recent [_get_recentrepos]
96 if {[llength $sorted_recent] > 0} {
98 label $w_body.recentlabel \
100 -text [mc "Open Recent Repository:"]
101 set w_recentlist $w_body.recentlist
103 -cursor $::cursor_ptr \
105 -background [$w_body.recentlabel cget -background] \
109 $w_recentlist tag conf link \
112 set home "[file normalize $::env(HOME)][file separator]"
113 set hlen [string length $home]
114 foreach p $sorted_recent {
115 if {[string equal -length $hlen $home $p]} {
116 set p "~[file separator][string range $p $hlen end]"
118 regsub -all "\n" $p "\\n" p
119 $w_recentlist insert end $p link
120 $w_recentlist insert end "\n"
122 $w_recentlist conf -state disabled
123 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
124 pack $w_body.space -anchor w -fill x
125 pack $w_body.recentlabel -anchor w -fill x
126 pack $w_recentlist -anchor w -fill x
128 pack $w_body -fill x -padx 10 -pady 10
131 set w_next $w.buttons.next
132 set w_quit $w.buttons.quit
136 pack $w_quit -side right -padx 5
137 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
139 bind $top <Return> [cb _invoke_next]
140 bind $top <Visibility> "
144 bind $top <Visibility> {}
147 tkwait variable @done
150 eval destroy [winfo children $top]
155 if {[catch {set h $::env(HOME)}]
156 || ![file isdirectory $h]} {
163 set nx [winfo reqwidth $top]
164 set ny [winfo reqheight $top]
165 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
166 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
167 wm geometry $top [format {+%d+%d} $rx $ry]
170 method _invoke_next {} {
171 if {[winfo exists $w_next]} {
172 uplevel #0 [$w_next cget -command]
176 proc _get_recentrepos {} {
178 foreach p [get_config gui.recentrepo] {
179 if {[_is_git [file join $p .git]]} {
183 return [lsort $recent]
186 proc _unset_recentrepo {p} {
187 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
188 git config --global --unset gui.recentrepo "^$p\$"
191 proc _append_recentrepos {path} {
192 set path [file normalize $path]
193 set recent [get_config gui.recentrepo]
195 if {[lindex $recent end] eq $path} {
199 set i [lsearch $recent $path]
201 _unset_recentrepo $path
202 set recent [lreplace $recent $i $i]
206 git config --global --add gui.recentrepo $path
208 while {[llength $recent] > 10} {
209 _unset_recentrepo [lindex $recent 0]
210 set recent [lrange $recent 1 end]
214 method _open_recent {xy} {
215 set id [lindex [split [$w_recentlist index @$xy] .] 0]
216 set local_path [lindex $sorted_recent [expr {$id - 1}]]
220 method _next {action} {
222 if {![winfo exists $w_next]} {
223 button $w_next -default active
224 pack $w_next -side right -padx 5 -before $w_quit
229 method _write_local_path {args} {
230 if {$local_path eq {}} {
231 $w_next conf -state disabled
233 $w_next conf -state normal
237 method _git_init {} {
238 if {[file exists $local_path]} {
239 error_popup [mc "Location %s already exists." $local_path]
243 if {[catch {file mkdir $local_path} err]} {
244 error_popup [strcat \
245 [mc "Failed to create repository %s:" $local_path] \
250 if {[catch {cd $local_path} err]} {
251 error_popup [strcat \
252 [mc "Failed to create repository %s:" $local_path] \
257 if {[catch {git init} err]} {
258 error_popup [strcat \
259 [mc "Failed to create repository %s:" $local_path] \
264 _append_recentrepos [pwd]
270 proc _is_git {path} {
271 if {[file exists [file join $path HEAD]]
272 && [file exists [file join $path objects]]
273 && [file exists [file join $path config]]} {
279 ######################################################################
281 ## Create New Repository
286 -command [cb _do_new2] \
292 -text [mc "Create New Repository"]
293 pack $w_body.h -side top -fill x -pady 10
294 pack $w_body -fill x -padx 10
297 label $w_body.where.l -text [mc "Directory:"]
298 entry $w_body.where.t \
299 -textvariable @local_path \
302 button $w_body.where.b \
303 -text [mc "Browse"] \
304 -command [cb _new_local_path]
306 pack $w_body.where.b -side right
307 pack $w_body.where.l -side left
308 pack $w_body.where.t -fill x
309 pack $w_body.where -fill x
311 trace add variable @local_path write [cb _write_local_path]
313 focus $w_body.where.t
316 method _new_local_path {} {
317 if {$local_path ne {}} {
318 set p [file dirname $local_path]
323 set p [tk_chooseDirectory \
326 -title [mc "Git Repository"] \
330 set p [file normalize $p]
331 if {[file isdirectory $p]} {
341 error_popup [mc "Directory %s already exists." $p]
346 if {[catch {file delete $p} err]} {
347 error_popup [strcat \
348 [mc "Directory %s already exists." $p] \
352 } elseif {[file exists $p]} {
353 error_popup [mc "File %s already exists." $p]
360 if {![_git_init $this]} {
366 ######################################################################
368 ## Clone Existing Repository
370 method _do_clone {} {
373 -command [cb _do_clone2] \
379 -text [mc "Clone Existing Repository"]
380 pack $w_body.h -side top -fill x -pady 10
381 pack $w_body -fill x -padx 10
383 set args $w_body.args
385 pack $args -fill both
387 label $args.origin_l -text [mc "URL:"]
388 entry $args.origin_t \
389 -textvariable @origin_url \
392 button $args.origin_b \
393 -text [mc "Browse"] \
394 -command [cb _open_origin]
395 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
397 label $args.where_l -text [mc "Directory:"]
398 entry $args.where_t \
399 -textvariable @local_path \
402 button $args.where_b \
403 -text [mc "Browse"] \
404 -command [cb _new_local_path]
405 grid $args.where_l $args.where_t $args.where_b -sticky ew
407 label $args.type_l -text [mc "Clone Type:"]
410 lappend w_types [radiobutton $args.type_f.hardlink \
413 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
414 -variable @clone_type \
416 lappend w_types [radiobutton $args.type_f.full \
419 -text [mc "Full Copy (Slower, Redundant Backup)"] \
420 -variable @clone_type \
422 lappend w_types [radiobutton $args.type_f.shared \
425 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
426 -variable @clone_type \
431 grid $args.type_l $args.type_f -sticky new
433 grid columnconfigure $args 1 -weight 1
435 trace add variable @local_path write [cb _update_clone]
436 trace add variable @origin_url write [cb _update_clone]
441 method _open_origin {} {
442 if {$origin_url ne {} && [file isdirectory $origin_url]} {
448 set p [tk_chooseDirectory \
451 -title [mc "Git Repository"] \
455 set p [file normalize $p]
456 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
457 error_popup [mc "Not a Git repository: %s" [file tail $p]]
463 method _update_clone {args} {
464 if {$local_path ne {} && $origin_url ne {}} {
465 $w_next conf -state normal
467 $w_next conf -state disabled
470 if {$origin_url ne {} &&
471 ( [_is_git [file join $origin_url .git]]
472 || [_is_git $origin_url])} {
474 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
475 set clone_type hardlink
487 method _do_clone2 {} {
488 if {[file isdirectory $origin_url]} {
489 set origin_url [file normalize $origin_url]
492 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
493 error_popup [mc "Standard only available for local repository."]
496 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
497 error_popup [mc "Shared only available for local repository."]
501 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
502 set objdir [file join $origin_url .git objects]
503 if {![file isdirectory $objdir]} {
504 set objdir [file join $origin_url objects]
505 if {![file isdirectory $objdir]} {
506 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
512 set giturl $origin_url
513 if {[is_Cygwin] && [file isdirectory $giturl]} {
514 set giturl [exec cygpath --unix --absolute $giturl]
515 if {$clone_type eq {shared}} {
516 set objdir [exec cygpath --unix --absolute $objdir]
520 if {![_git_init $this]} return
524 git config remote.$origin_name.url $giturl
525 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
527 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
531 destroy $w_body $w_next
533 switch -exact -- $clone_type {
535 set o_cons [status_bar::two_line $w_body]
536 pack $w_body -fill x -padx 10 -pady 10
539 [mc "Counting objects"] \
543 if {[file exists [file join $objdir info alternates]]} {
546 file mkdir [gitdir objects info]
547 set f_in [open [file join $objdir info alternates] r]
548 set f_cp [open [gitdir objects info alternates] w]
549 fconfigure $f_in -translation binary -encoding binary
550 fconfigure $f_cp -translation binary -encoding binary
552 while {[gets $f_in line] >= 0} {
554 puts $f_cp [exec cygpath --unix --absolute $line]
556 puts $f_cp [file normalize $line]
564 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
573 -directory [file join $objdir] ??]
574 set bcnt [expr {[llength $buckets] + 2}]
576 $o_cons update $bcur $bcnt
579 file mkdir [file join .git objects pack]
580 foreach i [glob -tails -nocomplain \
581 -directory [file join $objdir pack] *] {
582 lappend tolink [file join pack $i]
584 $o_cons update [incr bcur] $bcnt
588 file mkdir [file join .git objects $i]
589 foreach j [glob -tails -nocomplain \
590 -directory [file join $objdir $i] *] {
591 lappend tolink [file join $i $j]
593 $o_cons update [incr bcur] $bcnt
600 [mc "Nothing to clone from %s." $origin_url] \
602 [mc "The 'master' branch has not been initialized."] \
609 set i [lindex $tolink 0]
612 [file join .git objects $i] \
613 [file join $objdir $i]
615 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
616 set i [_copy_files $this $objdir $tolink]
618 set i [_link_files $this $objdir [lrange $tolink 1 end]]
625 set o_cons [console::embed \
627 [mc "Cloning from %s" $origin_url]]
628 pack $w_body -fill both -expand 1 -padx 10
630 [list git fetch --no-tags -k $origin_name] \
634 set fd [open [gitdir objects info alternates] w]
635 fconfigure $fd -translation binary
641 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
642 if {![_clone_refs $this]} return
646 set HEAD [git rev-parse --verify HEAD^0]
648 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
652 _do_clone_checkout $this $HEAD
656 method _copy_files {objdir tocopy} {
658 [mc "Copying objects"] \
663 incr tot [file size [file join $objdir $p]]
667 set f_in [open [file join $objdir $p] r]
668 set f_cp [open [file join .git objects $p] w]
669 fconfigure $f_in -translation binary -encoding binary
670 fconfigure $f_cp -translation binary -encoding binary
672 while {![eof $f_in]} {
673 incr cmp [fcopy $f_in $f_cp -size 16384]
675 [expr {$cmp / 1024}] \
683 _clone_failed $this [mc "Unable to copy object: %s" $err]
690 method _link_files {objdir tolink} {
691 set total [llength $tolink]
693 [mc "Linking objects"] \
695 for {set i 0} {$i < $total} {} {
696 set p [lindex $tolink $i]
699 [file join .git objects $p] \
700 [file join $objdir $p]
702 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
708 $o_cons update $i $total
715 method _clone_refs {} {
717 if {[catch {cd $origin_url} err]} {
718 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
721 set fd_in [git_read for-each-ref \
723 {--format=list %(refname) %(objectname) %(*objectname)}]
726 set fd [open [gitdir packed-refs] w]
727 fconfigure $fd -translation binary
728 puts $fd "# pack-refs with: peeled"
729 while {[gets $fd_in line] >= 0} {
730 set line [eval $line]
731 set refn [lindex $line 0]
732 set robj [lindex $line 1]
733 set tobj [lindex $line 2]
735 if {[regsub ^refs/heads/ $refn \
736 "refs/remotes/$origin_name/" refn]} {
737 puts $fd "$robj $refn"
738 } elseif {[string match refs/tags/* $refn]} {
739 puts $fd "$robj $refn"
750 method _do_clone_tags {ok} {
753 [list git fetch --tags -k $origin_name] \
757 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
761 method _do_clone_HEAD {ok} {
764 [list git fetch $origin_name HEAD] \
765 [cb _do_clone_full_end]
768 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
772 method _do_clone_full_end {ok} {
779 if {[file exists [gitdir FETCH_HEAD]]} {
780 set fd [open [gitdir FETCH_HEAD] r]
781 while {[gets $fd line] >= 0} {
782 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
789 catch {git pack-refs}
790 _do_clone_checkout $this $HEAD
792 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
796 method _clone_failed {{why {}}} {
797 if {[catch {file delete -force $local_path} err]} {
801 [mc "Unable to cleanup %s" $local_path] \
807 error_popup [strcat [mc "Clone failed."] "\n" $why]
811 method _do_clone_checkout {HEAD} {
814 [mc "No default branch obtained."] \
816 [mc "The 'master' branch has not been initialized."] \
822 git update-ref HEAD $HEAD^0
825 [mc "Cannot resolve %s as a commit." $HEAD^0] \
828 [mc "The 'master' branch has not been initialized."] \
834 set o_cons [status_bar::two_line $w_body]
835 pack $w_body -fill x -padx 10 -pady 10
837 [mc "Creating working directory"] \
841 set fd [git_read --stderr read-tree \
848 fconfigure $fd -blocking 0 -translation binary
849 fileevent $fd readable [cb _readtree_wait $fd]
852 method _readtree_wait {fd} {
854 $o_cons update_meter $buf
855 append readtree_err $buf
857 fconfigure $fd -blocking 1
859 fconfigure $fd -blocking 0
863 if {[catch {close $fd}]} {
864 set err $readtree_err
865 regsub {^fatal: } $err {} err
866 error_popup [strcat \
867 [mc "Initial file checkout failed."] \
875 ######################################################################
877 ## Open Existing Repository
882 -command [cb _do_open2] \
888 -text [mc "Open Existing Repository"]
889 pack $w_body.h -side top -fill x -pady 10
890 pack $w_body -fill x -padx 10
893 label $w_body.where.l -text [mc "Repository:"]
894 entry $w_body.where.t \
895 -textvariable @local_path \
898 button $w_body.where.b \
899 -text [mc "Browse"] \
900 -command [cb _open_local_path]
902 pack $w_body.where.b -side right
903 pack $w_body.where.l -side left
904 pack $w_body.where.t -fill x
905 pack $w_body.where -fill x
907 trace add variable @local_path write [cb _write_local_path]
909 focus $w_body.where.t
912 method _open_local_path {} {
913 if {$local_path ne {}} {
919 set p [tk_chooseDirectory \
922 -title [mc "Git Repository"] \
926 set p [file normalize $p]
927 if {![_is_git [file join $p .git]]} {
928 error_popup [mc "Not a Git repository: %s" [file tail $p]]
934 method _do_open2 {} {
935 if {![_is_git [file join $local_path .git]]} {
936 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
940 if {[catch {cd $local_path} err]} {
941 error_popup [strcat \
942 [mc "Failed to open repository %s:" $local_path] \
947 _append_recentrepos [pwd]