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
14 field w_localpath ; # Entry widget bound to local_path
16 field done 0 ; # Finished picking the repository?
17 field local_path {} ; # Where this repository is locally
18 field origin_url {} ; # Where we are cloning from
19 field origin_name origin ; # What we shall call 'origin'
20 field clone_type hardlink ; # Type of clone to construct
21 field readtree_err ; # Error output from read-tree (if any)
22 field sorted_recent ; # recent repositories (sorted)
28 wm title $top [mc "Git Gui"]
31 menu $w.mbar -tearoff 0
32 $top configure -menu $w.mbar
34 set m_repo $w.mbar.repository
36 -label [mc Repository] \
41 $w.mbar add cascade -label Apple -menu .mbar.apple
43 $w.mbar.apple add command \
44 -label [mc "About %s" [appname]] \
46 $w.mbar.apple add command \
47 -label [mc "Show SSH Key"] \
50 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
52 $w.mbar.help add command \
53 -label [mc "About %s" [appname]] \
55 $w.mbar.help add command \
56 -label [mc "Show SSH Key"] \
60 wm protocol $top WM_DELETE_WINDOW exit
61 bind $top <$M1B-q> exit
62 bind $top <$M1B-Q> exit
63 bind $top <Key-Escape> exit
65 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
66 bind $top <Key-Escape> [list destroy $top]
70 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
73 set opts $w_body.options
76 -cursor $::cursor_ptr \
78 -background [$w_body cget -background] \
83 pack $opts -anchor w -fill x
85 $opts tag conf link_new -foreground blue -underline 1
86 $opts tag bind link_new <1> [cb _next new]
87 $opts insert end [mc "Create New Repository"] link_new
91 -command [cb _next new] \
94 bind $top <$M1B-n> [cb _next new]
95 bind $top <$M1B-N> [cb _next new]
98 $opts tag conf link_clone -foreground blue -underline 1
99 $opts tag bind link_clone <1> [cb _next clone]
100 $opts insert end [mc "Clone Existing Repository"] link_clone
101 $opts insert end "\n"
103 $m_repo add command \
104 -command [cb _next clone] \
105 -accelerator $M1T-C \
106 -label [mc "Clone..."]
107 bind $top <$M1B-c> [cb _next clone]
108 bind $top <$M1B-C> [cb _next clone]
111 $opts tag conf link_open -foreground blue -underline 1
112 $opts tag bind link_open <1> [cb _next open]
113 $opts insert end [mc "Open Existing Repository"] link_open
114 $opts insert end "\n"
116 $m_repo add command \
117 -command [cb _next open] \
118 -accelerator $M1T-O \
119 -label [mc "Open..."]
120 bind $top <$M1B-o> [cb _next open]
121 bind $top <$M1B-O> [cb _next open]
124 $opts conf -state disabled
126 set sorted_recent [_get_recentrepos]
127 if {[llength $sorted_recent] > 0} {
129 $m_repo add separator
130 $m_repo add command \
132 -label [mc "Recent Repositories"]
136 label $w_body.recentlabel \
138 -text [mc "Open Recent Repository:"]
139 set w_recentlist $w_body.recentlist
141 -cursor $::cursor_ptr \
143 -background [$w_body.recentlabel cget -background] \
147 $w_recentlist tag conf link \
150 set home $::env(HOME)
152 set home [exec cygpath --windows --absolute $home]
154 set home "[file normalize $home]/"
155 set hlen [string length $home]
156 foreach p $sorted_recent {
158 if {[string equal -length $hlen $home $p]} {
159 set p "~/[string range $p $hlen end]"
161 regsub -all "\n" $p "\\n" p
162 $w_recentlist insert end $p link
163 $w_recentlist insert end "\n"
166 $m_repo add command \
167 -command [cb _open_recent_path $path] \
171 $w_recentlist conf -state disabled
172 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
173 pack $w_body.space -anchor w -fill x
174 pack $w_body.recentlabel -anchor w -fill x
175 pack $w_recentlist -anchor w -fill x
177 pack $w_body -fill x -padx 10 -pady 10
180 set w_next $w.buttons.next
181 set w_quit $w.buttons.quit
185 pack $w_quit -side right -padx 5
186 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
189 $m_repo add separator
190 $m_repo add command \
196 bind $top <Return> [cb _invoke_next]
197 bind $top <Visibility> "
201 bind $top <Visibility> {}
204 tkwait variable @done
207 eval destroy [winfo children $top]
212 if {[catch {set h $::env(HOME)}]
213 || ![file isdirectory $h]} {
220 set nx [winfo reqwidth $top]
221 set ny [winfo reqheight $top]
222 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
223 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
224 wm geometry $top [format {+%d+%d} $rx $ry]
227 method _invoke_next {} {
228 if {[winfo exists $w_next]} {
229 uplevel #0 [$w_next cget -command]
233 proc _get_recentrepos {} {
235 foreach p [get_config gui.recentrepo] {
236 if {[_is_git [file join $p .git]]} {
242 return [lsort $recent]
245 proc _unset_recentrepo {p} {
246 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
247 git config --global --unset gui.recentrepo "^$p\$"
251 proc _append_recentrepos {path} {
252 set path [file normalize $path]
253 set recent [get_config gui.recentrepo]
255 if {[lindex $recent end] eq $path} {
259 set i [lsearch $recent $path]
261 _unset_recentrepo $path
262 set recent [lreplace $recent $i $i]
266 git config --global --add gui.recentrepo $path
269 while {[llength $recent] > 10} {
270 _unset_recentrepo [lindex $recent 0]
271 set recent [lrange $recent 1 end]
275 method _open_recent {xy} {
276 set id [lindex [split [$w_recentlist index @$xy] .] 0]
277 set local_path [lindex $sorted_recent [expr {$id - 1}]]
281 method _open_recent_path {p} {
286 method _next {action} {
288 if {![winfo exists $w_next]} {
289 button $w_next -default active
290 pack $w_next -side right -padx 5 -before $w_quit
295 method _write_local_path {args} {
296 if {$local_path eq {}} {
297 $w_next conf -state disabled
299 $w_next conf -state normal
303 method _git_init {} {
304 if {[catch {file mkdir $local_path} err]} {
305 error_popup [strcat \
306 [mc "Failed to create repository %s:" $local_path] \
311 if {[catch {cd $local_path} err]} {
312 error_popup [strcat \
313 [mc "Failed to create repository %s:" $local_path] \
318 if {[catch {git init} err]} {
319 error_popup [strcat \
320 [mc "Failed to create repository %s:" $local_path] \
325 _append_recentrepos [pwd]
331 proc _is_git {path} {
332 if {[file exists [file join $path HEAD]]
333 && [file exists [file join $path objects]]
334 && [file exists [file join $path config]]} {
338 if {[file exists [file join $path HEAD]]
339 && [file exists [file join $path objects.lnk]]
340 && [file exists [file join $path config.lnk]]} {
347 proc _objdir {path} {
348 set objdir [file join $path .git objects]
349 if {[file isdirectory $objdir]} {
353 set objdir [file join $path objects]
354 if {[file isdirectory $objdir]} {
359 set objdir [file join $path .git objects.lnk]
360 if {[file isfile $objdir]} {
361 return [win32_read_lnk $objdir]
364 set objdir [file join $path objects.lnk]
365 if {[file isfile $objdir]} {
366 return [win32_read_lnk $objdir]
373 ######################################################################
375 ## Create New Repository
380 -command [cb _do_new2] \
386 -text [mc "Create New Repository"]
387 pack $w_body.h -side top -fill x -pady 10
388 pack $w_body -fill x -padx 10
391 label $w_body.where.l -text [mc "Directory:"]
392 entry $w_body.where.t \
393 -textvariable @local_path \
397 button $w_body.where.b \
398 -text [mc "Browse"] \
399 -command [cb _new_local_path]
400 set w_localpath $w_body.where.t
402 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
403 pack $w_body.where -fill x
405 grid columnconfigure $w_body.where 1 -weight 1
407 trace add variable @local_path write [cb _write_local_path]
408 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
410 focus $w_body.where.t
413 method _new_local_path {} {
414 if {$local_path ne {}} {
415 set p [file dirname $local_path]
420 set p [tk_chooseDirectory \
423 -title [mc "Git Repository"] \
427 set p [file normalize $p]
432 $w_localpath icursor end
436 if {![_new_ok $local_path]} {
439 if {![_git_init $this]} {
446 if {[file isdirectory $p]} {
447 if {[_is_git [file join $p .git]]} {
448 error_popup [mc "Directory %s already exists." $p]
451 } elseif {[file exists $p]} {
452 error_popup [mc "File %s already exists." $p]
458 ######################################################################
460 ## Clone Existing Repository
462 method _do_clone {} {
465 -command [cb _do_clone2] \
471 -text [mc "Clone Existing Repository"]
472 pack $w_body.h -side top -fill x -pady 10
473 pack $w_body -fill x -padx 10
475 set args $w_body.args
477 pack $args -fill both
479 label $args.origin_l -text [mc "Source Location:"]
480 entry $args.origin_t \
481 -textvariable @origin_url \
485 button $args.origin_b \
486 -text [mc "Browse"] \
487 -command [cb _open_origin]
488 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
490 label $args.where_l -text [mc "Target Directory:"]
491 entry $args.where_t \
492 -textvariable @local_path \
496 button $args.where_b \
497 -text [mc "Browse"] \
498 -command [cb _new_local_path]
499 grid $args.where_l $args.where_t $args.where_b -sticky ew
500 set w_localpath $args.where_t
502 label $args.type_l -text [mc "Clone Type:"]
505 lappend w_types [radiobutton $args.type_f.hardlink \
508 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
509 -variable @clone_type \
511 lappend w_types [radiobutton $args.type_f.full \
514 -text [mc "Full Copy (Slower, Redundant Backup)"] \
515 -variable @clone_type \
517 lappend w_types [radiobutton $args.type_f.shared \
520 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
521 -variable @clone_type \
526 grid $args.type_l $args.type_f -sticky new
528 grid columnconfigure $args 1 -weight 1
530 trace add variable @local_path write [cb _update_clone]
531 trace add variable @origin_url write [cb _update_clone]
532 bind $w_body.h <Destroy> "
533 [list trace remove variable @local_path write [cb _update_clone]]
534 [list trace remove variable @origin_url write [cb _update_clone]]
540 method _open_origin {} {
541 if {$origin_url ne {} && [file isdirectory $origin_url]} {
547 set p [tk_chooseDirectory \
550 -title [mc "Git Repository"] \
554 set p [file normalize $p]
555 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
556 error_popup [mc "Not a Git repository: %s" [file tail $p]]
562 method _update_clone {args} {
563 if {$local_path ne {} && $origin_url ne {}} {
564 $w_next conf -state normal
566 $w_next conf -state disabled
569 if {$origin_url ne {} &&
570 ( [_is_git [file join $origin_url .git]]
571 || [_is_git $origin_url])} {
573 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
574 set clone_type hardlink
586 method _do_clone2 {} {
587 if {[file isdirectory $origin_url]} {
588 set origin_url [file normalize $origin_url]
591 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
592 error_popup [mc "Standard only available for local repository."]
595 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
596 error_popup [mc "Shared only available for local repository."]
600 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
601 set objdir [_objdir $origin_url]
603 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
608 set giturl $origin_url
609 if {[is_Cygwin] && [file isdirectory $giturl]} {
610 set giturl [exec cygpath --unix --absolute $giturl]
611 if {$clone_type eq {shared}} {
612 set objdir [exec cygpath --unix --absolute $objdir]
616 if {[file exists $local_path]} {
617 error_popup [mc "Location %s already exists." $local_path]
621 if {![_git_init $this]} return
625 git config remote.$origin_name.url $giturl
626 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
628 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
632 destroy $w_body $w_next
634 switch -exact -- $clone_type {
636 set o_cons [status_bar::two_line $w_body]
637 pack $w_body -fill x -padx 10 -pady 10
640 [mc "Counting objects"] \
644 if {[file exists [file join $objdir info alternates]]} {
647 file mkdir [gitdir objects info]
648 set f_in [open [file join $objdir info alternates] r]
649 set f_cp [open [gitdir objects info alternates] w]
650 fconfigure $f_in -translation binary -encoding binary
651 fconfigure $f_cp -translation binary -encoding binary
653 while {[gets $f_in line] >= 0} {
655 puts $f_cp [exec cygpath --unix --absolute $line]
657 puts $f_cp [file normalize $line]
665 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
674 -directory [file join $objdir] ??]
675 set bcnt [expr {[llength $buckets] + 2}]
677 $o_cons update $bcur $bcnt
680 file mkdir [file join .git objects pack]
681 foreach i [glob -tails -nocomplain \
682 -directory [file join $objdir pack] *] {
683 lappend tolink [file join pack $i]
685 $o_cons update [incr bcur] $bcnt
689 file mkdir [file join .git objects $i]
690 foreach j [glob -tails -nocomplain \
691 -directory [file join $objdir $i] *] {
692 lappend tolink [file join $i $j]
694 $o_cons update [incr bcur] $bcnt
701 [mc "Nothing to clone from %s." $origin_url] \
703 [mc "The 'master' branch has not been initialized."] \
710 set i [lindex $tolink 0]
713 [file join .git objects $i] \
714 [file join $objdir $i]
716 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
717 set i [_copy_files $this $objdir $tolink]
719 set i [_link_files $this $objdir [lrange $tolink 1 end]]
726 set o_cons [console::embed \
728 [mc "Cloning from %s" $origin_url]]
729 pack $w_body -fill both -expand 1 -padx 10
731 [list git fetch --no-tags -k $origin_name] \
735 set fd [open [gitdir objects info alternates] w]
736 fconfigure $fd -translation binary
742 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
743 if {![_clone_refs $this]} return
747 set HEAD [git rev-parse --verify HEAD^0]
749 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
753 _do_clone_checkout $this $HEAD
757 method _copy_files {objdir tocopy} {
759 [mc "Copying objects"] \
764 incr tot [file size [file join $objdir $p]]
768 set f_in [open [file join $objdir $p] r]
769 set f_cp [open [file join .git objects $p] w]
770 fconfigure $f_in -translation binary -encoding binary
771 fconfigure $f_cp -translation binary -encoding binary
773 while {![eof $f_in]} {
774 incr cmp [fcopy $f_in $f_cp -size 16384]
776 [expr {$cmp / 1024}] \
784 _clone_failed $this [mc "Unable to copy object: %s" $err]
791 method _link_files {objdir tolink} {
792 set total [llength $tolink]
794 [mc "Linking objects"] \
796 for {set i 0} {$i < $total} {} {
797 set p [lindex $tolink $i]
800 [file join .git objects $p] \
801 [file join $objdir $p]
803 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
809 $o_cons update $i $total
816 method _clone_refs {} {
818 if {[catch {cd $origin_url} err]} {
819 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
822 set fd_in [git_read for-each-ref \
824 {--format=list %(refname) %(objectname) %(*objectname)}]
827 set fd [open [gitdir packed-refs] w]
828 fconfigure $fd -translation binary
829 puts $fd "# pack-refs with: peeled"
830 while {[gets $fd_in line] >= 0} {
831 set line [eval $line]
832 set refn [lindex $line 0]
833 set robj [lindex $line 1]
834 set tobj [lindex $line 2]
836 if {[regsub ^refs/heads/ $refn \
837 "refs/remotes/$origin_name/" refn]} {
838 puts $fd "$robj $refn"
839 } elseif {[string match refs/tags/* $refn]} {
840 puts $fd "$robj $refn"
851 method _do_clone_tags {ok} {
854 [list git fetch --tags -k $origin_name] \
858 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
862 method _do_clone_HEAD {ok} {
865 [list git fetch $origin_name HEAD] \
866 [cb _do_clone_full_end]
869 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
873 method _do_clone_full_end {ok} {
880 if {[file exists [gitdir FETCH_HEAD]]} {
881 set fd [open [gitdir FETCH_HEAD] r]
882 while {[gets $fd line] >= 0} {
883 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
890 catch {git pack-refs}
891 _do_clone_checkout $this $HEAD
893 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
897 method _clone_failed {{why {}}} {
898 if {[catch {file delete -force $local_path} err]} {
902 [mc "Unable to cleanup %s" $local_path] \
908 error_popup [strcat [mc "Clone failed."] "\n" $why]
912 method _do_clone_checkout {HEAD} {
915 [mc "No default branch obtained."] \
917 [mc "The 'master' branch has not been initialized."] \
923 git update-ref HEAD $HEAD^0
926 [mc "Cannot resolve %s as a commit." $HEAD^0] \
929 [mc "The 'master' branch has not been initialized."] \
935 set o_cons [status_bar::two_line $w_body]
936 pack $w_body -fill x -padx 10 -pady 10
938 [mc "Creating working directory"] \
942 set fd [git_read --stderr read-tree \
949 fconfigure $fd -blocking 0 -translation binary
950 fileevent $fd readable [cb _readtree_wait $fd]
953 method _readtree_wait {fd} {
955 $o_cons update_meter $buf
956 append readtree_err $buf
958 fconfigure $fd -blocking 1
960 fconfigure $fd -blocking 0
964 if {[catch {close $fd}]} {
965 set err $readtree_err
966 regsub {^fatal: } $err {} err
967 error_popup [strcat \
968 [mc "Initial file checkout failed."] \
973 # -- Run the post-checkout hook.
975 set fd_ph [githook_read post-checkout [string repeat 0 40] \
976 [git rev-parse HEAD] 1]
980 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
981 fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
987 method _postcheckout_wait {fd_ph} {
990 append pch_error [read $fd_ph]
991 fconfigure $fd_ph -blocking 1
993 if {[catch {close $fd_ph}]} {
994 hook_failed_popup post-checkout $pch_error 0
1000 fconfigure $fd_ph -blocking 0
1003 ######################################################################
1005 ## Open Existing Repository
1007 method _do_open {} {
1010 -command [cb _do_open2] \
1016 -text [mc "Open Existing Repository"]
1017 pack $w_body.h -side top -fill x -pady 10
1018 pack $w_body -fill x -padx 10
1021 label $w_body.where.l -text [mc "Repository:"]
1022 entry $w_body.where.t \
1023 -textvariable @local_path \
1027 button $w_body.where.b \
1028 -text [mc "Browse"] \
1029 -command [cb _open_local_path]
1031 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
1032 pack $w_body.where -fill x
1034 grid columnconfigure $w_body.where 1 -weight 1
1036 trace add variable @local_path write [cb _write_local_path]
1037 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
1039 focus $w_body.where.t
1042 method _open_local_path {} {
1043 if {$local_path ne {}} {
1049 set p [tk_chooseDirectory \
1052 -title [mc "Git Repository"] \
1054 if {$p eq {}} return
1056 set p [file normalize $p]
1057 if {![_is_git [file join $p .git]]} {
1058 error_popup [mc "Not a Git repository: %s" [file tail $p]]
1064 method _do_open2 {} {
1065 if {![_is_git [file join $local_path .git]]} {
1066 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
1070 if {[catch {cd $local_path} err]} {
1071 error_popup [strcat \
1072 [mc "Failed to open repository %s:" $local_path] \
1077 _append_recentrepos [pwd]