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 recursive true ; # Recursive cloning flag
22 field readtree_err ; # Error output from read-tree (if any)
23 field sorted_recent ; # recent repositories (sorted)
26 global M1T M1B use_ttk NS
28 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
33 wm title $top [mc "Git Gui"]
36 menu $w.mbar -tearoff 0
37 $top configure -menu $w.mbar
39 set m_repo $w.mbar.repository
41 -label [mc Repository] \
46 $w.mbar add cascade -label Apple -menu .mbar.apple
48 $w.mbar.apple add command \
49 -label [mc "About %s" [appname]] \
51 $w.mbar.apple add command \
52 -label [mc "Show SSH Key"] \
55 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
57 $w.mbar.help add command \
58 -label [mc "About %s" [appname]] \
60 $w.mbar.help add command \
61 -label [mc "Show SSH Key"] \
65 wm protocol $top WM_DELETE_WINDOW exit
66 bind $top <$M1B-q> exit
67 bind $top <$M1B-Q> exit
68 bind $top <Key-Escape> exit
70 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
71 bind $top <Key-Escape> [list destroy $top]
75 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
78 set opts $w_body.options
81 -cursor $::cursor_ptr \
83 -background [get_bg_color $w_body] \
88 pack $opts -anchor w -fill x
90 $opts tag conf link_new -foreground blue -underline 1
91 $opts tag bind link_new <1> [cb _next new]
92 $opts insert end [mc "Create New Repository"] link_new
96 -command [cb _next new] \
99 bind $top <$M1B-n> [cb _next new]
100 bind $top <$M1B-N> [cb _next new]
103 $opts tag conf link_clone -foreground blue -underline 1
104 $opts tag bind link_clone <1> [cb _next clone]
105 $opts insert end [mc "Clone Existing Repository"] link_clone
106 $opts insert end "\n"
108 if {[tk windowingsystem] eq "win32"} {
113 $m_repo add command \
114 -command [cb _next clone] \
115 -accelerator $M1T-$key \
116 -label [mc "Clone..."]
117 bind $top <$M1B-[string tolower $key]> [cb _next clone]
118 bind $top <$M1B-[string toupper $key]> [cb _next clone]
121 $opts tag conf link_open -foreground blue -underline 1
122 $opts tag bind link_open <1> [cb _next open]
123 $opts insert end [mc "Open Existing Repository"] link_open
124 $opts insert end "\n"
126 $m_repo add command \
127 -command [cb _next open] \
128 -accelerator $M1T-O \
129 -label [mc "Open..."]
130 bind $top <$M1B-o> [cb _next open]
131 bind $top <$M1B-O> [cb _next open]
134 $opts conf -state disabled
136 set sorted_recent [_get_recentrepos]
137 if {[llength $sorted_recent] > 0} {
139 $m_repo add separator
140 $m_repo add command \
142 -label [mc "Recent Repositories"]
145 if {[set lenrecent [llength $sorted_recent]] < $maxrecent} {
146 set lenrecent $maxrecent
149 ${NS}::label $w_body.space
150 ${NS}::label $w_body.recentlabel \
152 -text [mc "Open Recent Repository:"]
153 set w_recentlist $w_body.recentlist
155 -cursor $::cursor_ptr \
157 -background [get_bg_color $w_body.recentlabel] \
161 $w_recentlist tag conf link \
164 set home $::env(HOME)
166 set home [exec cygpath --windows --absolute $home]
168 set home "[file normalize $home]/"
169 set hlen [string length $home]
170 foreach p $sorted_recent {
172 if {[string equal -length $hlen $home $p]} {
173 set p "~/[string range $p $hlen end]"
175 regsub -all "\n" $p "\\n" p
176 $w_recentlist insert end $p link
177 $w_recentlist insert end "\n"
180 $m_repo add command \
181 -command [cb _open_recent_path $path] \
185 $w_recentlist conf -state disabled
186 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
187 pack $w_body.space -anchor w -fill x
188 pack $w_body.recentlabel -anchor w -fill x
189 pack $w_recentlist -anchor w -fill x
191 pack $w_body -fill x -padx 10 -pady 10
193 ${NS}::frame $w.buttons
194 set w_next $w.buttons.next
195 set w_quit $w.buttons.quit
196 ${NS}::button $w_quit \
199 pack $w_quit -side right -padx 5
200 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
203 $m_repo add separator
204 $m_repo add command \
210 bind $top <Return> [cb _invoke_next]
211 bind $top <Visibility> "
215 bind $top <Visibility> {}
218 tkwait variable @done
222 eval destroy [winfo children $top]
227 set nx [winfo reqwidth $top]
228 set ny [winfo reqheight $top]
229 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
230 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
231 wm geometry $top [format {+%d+%d} $rx $ry]
234 method _invoke_next {} {
235 if {[winfo exists $w_next]} {
236 uplevel #0 [$w_next cget -command]
240 proc _get_recentrepos {} {
242 foreach p [lsort -unique [get_config gui.recentrepo]] {
243 if {[_is_git [file join $p .git]]} {
252 proc _unset_recentrepo {p} {
253 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
254 catch {git config --global --unset-all gui.recentrepo "^$p\$"}
258 proc _append_recentrepos {path} {
259 set path [file normalize $path]
260 set recent [get_config gui.recentrepo]
262 if {[lindex $recent end] eq $path} {
266 set i [lsearch $recent $path]
268 _unset_recentrepo $path
271 git config --global --add gui.recentrepo $path
273 set recent [get_config gui.recentrepo]
275 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
279 while {[llength $recent] > $maxrecent} {
280 _unset_recentrepo [lindex $recent 0]
281 set recent [get_config gui.recentrepo]
285 method _open_recent {xy} {
286 set id [lindex [split [$w_recentlist index @$xy] .] 0]
287 set local_path [lindex $sorted_recent [expr {$id - 1}]]
291 method _open_recent_path {p} {
296 method _next {action} {
299 if {![winfo exists $w_next]} {
300 ${NS}::button $w_next -default active
302 if {[tk windowingsystem] eq "win32"} { set pos -after }
303 pack $w_next -side right -padx 5 $pos $w_quit
308 method _write_local_path {args} {
309 if {$local_path eq {}} {
310 $w_next conf -state disabled
312 $w_next conf -state normal
316 method _git_init {} {
317 if {[catch {file mkdir $local_path} err]} {
318 error_popup [strcat \
319 [mc "Failed to create repository %s:" $local_path] \
324 if {[catch {cd $local_path} err]} {
325 error_popup [strcat \
326 [mc "Failed to create repository %s:" $local_path] \
331 if {[catch {git init} err]} {
332 error_popup [strcat \
333 [mc "Failed to create repository %s:" $local_path] \
338 _append_recentrepos [pwd]
344 proc _is_git {path {outdir_var ""}} {
345 if {$outdir_var ne ""} {
346 upvar 1 $outdir_var outdir
348 if {[file isfile $path]} {
349 set fp [open $path r]
352 if {[regexp "^gitdir: (.+)$" $line line link_target]} {
353 set path [file join [file dirname $path] $link_target]
354 set path [file normalize $path]
358 if {[file exists [file join $path HEAD]]
359 && [file exists [file join $path objects]]
360 && [file exists [file join $path config]]} {
365 if {[file exists [file join $path HEAD]]
366 && [file exists [file join $path objects.lnk]]
367 && [file exists [file join $path config.lnk]]} {
375 proc _objdir {path} {
376 set objdir [file join $path .git objects]
377 if {[file isdirectory $objdir]} {
381 set objdir [file join $path objects]
382 if {[file isdirectory $objdir]} {
387 set objdir [file join $path .git objects.lnk]
388 if {[file isfile $objdir]} {
389 return [win32_read_lnk $objdir]
392 set objdir [file join $path objects.lnk]
393 if {[file isfile $objdir]} {
394 return [win32_read_lnk $objdir]
401 ######################################################################
403 ## Create New Repository
409 -command [cb _do_new2] \
413 ${NS}::label $w_body.h \
414 -font font_uibold -anchor center \
415 -text [mc "Create New Repository"]
416 pack $w_body.h -side top -fill x -pady 10
417 pack $w_body -fill x -padx 10
419 ${NS}::frame $w_body.where
420 ${NS}::label $w_body.where.l -text [mc "Directory:"]
421 ${NS}::entry $w_body.where.t \
422 -textvariable @local_path \
424 ${NS}::button $w_body.where.b \
425 -text [mc "Browse"] \
426 -command [cb _new_local_path]
427 set w_localpath $w_body.where.t
429 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
430 pack $w_body.where -fill x
432 grid columnconfigure $w_body.where 1 -weight 1
434 trace add variable @local_path write [cb _write_local_path]
435 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
437 focus $w_body.where.t
440 method _new_local_path {} {
441 if {$local_path ne {}} {
442 set p [file dirname $local_path]
447 set p [tk_chooseDirectory \
450 -title [mc "Git Repository"] \
454 set p [file normalize $p]
459 $w_localpath icursor end
463 if {![_new_ok $local_path]} {
466 if {![_git_init $this]} {
473 if {[file isdirectory $p]} {
474 if {[_is_git [file join $p .git]]} {
475 error_popup [mc "Directory %s already exists." $p]
478 } elseif {[file exists $p]} {
479 error_popup [mc "File %s already exists." $p]
485 ######################################################################
487 ## Clone Existing Repository
489 method _do_clone {} {
493 -command [cb _do_clone2] \
497 ${NS}::label $w_body.h \
498 -font font_uibold -anchor center \
499 -text [mc "Clone Existing Repository"]
500 pack $w_body.h -side top -fill x -pady 10
501 pack $w_body -fill x -padx 10
503 set args $w_body.args
504 ${NS}::frame $w_body.args
505 pack $args -fill both
507 ${NS}::label $args.origin_l -text [mc "Source Location:"]
508 ${NS}::entry $args.origin_t \
509 -textvariable @origin_url \
511 ${NS}::button $args.origin_b \
512 -text [mc "Browse"] \
513 -command [cb _open_origin]
514 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
516 ${NS}::label $args.where_l -text [mc "Target Directory:"]
517 ${NS}::entry $args.where_t \
518 -textvariable @local_path \
520 ${NS}::button $args.where_b \
521 -text [mc "Browse"] \
522 -command [cb _new_local_path]
523 grid $args.where_l $args.where_t $args.where_b -sticky ew
524 set w_localpath $args.where_t
526 ${NS}::label $args.type_l -text [mc "Clone Type:"]
527 ${NS}::frame $args.type_f
529 lappend w_types [${NS}::radiobutton $args.type_f.hardlink \
531 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
532 -variable @clone_type \
534 lappend w_types [${NS}::radiobutton $args.type_f.full \
536 -text [mc "Full Copy (Slower, Redundant Backup)"] \
537 -variable @clone_type \
539 lappend w_types [${NS}::radiobutton $args.type_f.shared \
541 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
542 -variable @clone_type \
547 ${NS}::checkbutton $args.type_f.recursive \
548 -text [mc "Recursively clone submodules too"] \
549 -variable @recursive \
550 -onvalue true -offvalue false
551 pack $args.type_f.recursive -anchor w
552 grid $args.type_l $args.type_f -sticky new
554 grid columnconfigure $args 1 -weight 1
556 trace add variable @local_path write [cb _update_clone]
557 trace add variable @origin_url write [cb _update_clone]
558 bind $w_body.h <Destroy> "
559 [list trace remove variable @local_path write [cb _update_clone]]
560 [list trace remove variable @origin_url write [cb _update_clone]]
566 method _open_origin {} {
567 if {$origin_url ne {} && [file isdirectory $origin_url]} {
573 set p [tk_chooseDirectory \
576 -title [mc "Git Repository"] \
580 set p [file normalize $p]
581 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
582 error_popup [mc "Not a Git repository: %s" [file tail $p]]
588 method _update_clone {args} {
589 if {$local_path ne {} && $origin_url ne {}} {
590 $w_next conf -state normal
592 $w_next conf -state disabled
595 if {$origin_url ne {} &&
596 ( [_is_git [file join $origin_url .git]]
597 || [_is_git $origin_url])} {
599 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
600 set clone_type hardlink
612 method _do_clone2 {} {
613 if {[file isdirectory $origin_url]} {
614 set origin_url [file normalize $origin_url]
617 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
618 error_popup [mc "Standard only available for local repository."]
621 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
622 error_popup [mc "Shared only available for local repository."]
626 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
627 set objdir [_objdir $origin_url]
629 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
634 set giturl $origin_url
635 if {[is_Cygwin] && [file isdirectory $giturl]} {
636 set giturl [exec cygpath --unix --absolute $giturl]
637 if {$clone_type eq {shared}} {
638 set objdir [exec cygpath --unix --absolute $objdir]
642 if {[file exists $local_path]} {
643 error_popup [mc "Location %s already exists." $local_path]
647 if {![_git_init $this]} return
651 git config remote.$origin_name.url $giturl
652 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
654 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
658 destroy $w_body $w_next
660 switch -exact -- $clone_type {
662 set o_cons [status_bar::two_line $w_body]
663 pack $w_body -fill x -padx 10 -pady 10
666 [mc "Counting objects"] \
670 if {[file exists [file join $objdir info alternates]]} {
673 file mkdir [gitdir objects info]
674 set f_in [open [file join $objdir info alternates] r]
675 set f_cp [open [gitdir objects info alternates] w]
676 fconfigure $f_in -translation binary -encoding binary
677 fconfigure $f_cp -translation binary -encoding binary
679 while {[gets $f_in line] >= 0} {
681 puts $f_cp [exec cygpath --unix --absolute $line]
683 puts $f_cp [file normalize $line]
691 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
700 -directory [file join $objdir] ??]
701 set bcnt [expr {[llength $buckets] + 2}]
703 $o_cons update $bcur $bcnt
706 file mkdir [file join .git objects pack]
707 foreach i [glob -tails -nocomplain \
708 -directory [file join $objdir pack] *] {
709 lappend tolink [file join pack $i]
711 $o_cons update [incr bcur] $bcnt
715 file mkdir [file join .git objects $i]
716 foreach j [glob -tails -nocomplain \
717 -directory [file join $objdir $i] *] {
718 lappend tolink [file join $i $j]
720 $o_cons update [incr bcur] $bcnt
727 [mc "Nothing to clone from %s." $origin_url] \
729 [mc "The 'master' branch has not been initialized."] \
736 set i [lindex $tolink 0]
739 [file join .git objects $i] \
740 [file join $objdir $i]
742 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
743 set i [_copy_files $this $objdir $tolink]
745 set i [_link_files $this $objdir [lrange $tolink 1 end]]
752 set o_cons [console::embed \
754 [mc "Cloning from %s" $origin_url]]
755 pack $w_body -fill both -expand 1 -padx 10
757 [list git fetch --no-tags -k $origin_name] \
761 set fd [open [gitdir objects info alternates] w]
762 fconfigure $fd -translation binary
768 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
769 if {![_clone_refs $this]} return
773 set HEAD [git rev-parse --verify HEAD^0]
775 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
779 _do_clone_checkout $this $HEAD
783 method _copy_files {objdir tocopy} {
785 [mc "Copying objects"] \
790 incr tot [file size [file join $objdir $p]]
794 set f_in [open [file join $objdir $p] r]
795 set f_cp [open [file join .git objects $p] w]
796 fconfigure $f_in -translation binary -encoding binary
797 fconfigure $f_cp -translation binary -encoding binary
799 while {![eof $f_in]} {
800 incr cmp [fcopy $f_in $f_cp -size 16384]
802 [expr {$cmp / 1024}] \
810 _clone_failed $this [mc "Unable to copy object: %s" $err]
817 method _link_files {objdir tolink} {
818 set total [llength $tolink]
820 [mc "Linking objects"] \
822 for {set i 0} {$i < $total} {} {
823 set p [lindex $tolink $i]
826 [file join .git objects $p] \
827 [file join $objdir $p]
829 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
835 $o_cons update $i $total
842 method _clone_refs {} {
844 if {[catch {cd $origin_url} err]} {
845 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
848 set fd_in [git_read for-each-ref \
850 {--format=list %(refname) %(objectname) %(*objectname)}]
853 set fd [open [gitdir packed-refs] w]
854 fconfigure $fd -translation binary
855 puts $fd "# pack-refs with: peeled"
856 while {[gets $fd_in line] >= 0} {
857 set line [eval $line]
858 set refn [lindex $line 0]
859 set robj [lindex $line 1]
860 set tobj [lindex $line 2]
862 if {[regsub ^refs/heads/ $refn \
863 "refs/remotes/$origin_name/" refn]} {
864 puts $fd "$robj $refn"
865 } elseif {[string match refs/tags/* $refn]} {
866 puts $fd "$robj $refn"
877 method _do_clone_tags {ok} {
880 [list git fetch --tags -k $origin_name] \
884 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
888 method _do_clone_HEAD {ok} {
891 [list git fetch $origin_name HEAD] \
892 [cb _do_clone_full_end]
895 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
899 method _do_clone_full_end {ok} {
906 if {[file exists [gitdir FETCH_HEAD]]} {
907 set fd [open [gitdir FETCH_HEAD] r]
908 while {[gets $fd line] >= 0} {
909 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
916 catch {git pack-refs}
917 _do_clone_checkout $this $HEAD
919 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
923 method _clone_failed {{why {}}} {
924 if {[catch {file delete -force $local_path} err]} {
928 [mc "Unable to cleanup %s" $local_path] \
934 error_popup [strcat [mc "Clone failed."] "\n" $why]
938 method _do_clone_checkout {HEAD} {
941 [mc "No default branch obtained."] \
943 [mc "The 'master' branch has not been initialized."] \
949 git update-ref HEAD $HEAD^0
952 [mc "Cannot resolve %s as a commit." $HEAD^0] \
955 [mc "The 'master' branch has not been initialized."] \
961 set o_cons [status_bar::two_line $w_body]
962 pack $w_body -fill x -padx 10 -pady 10
964 [mc "Creating working directory"] \
968 set fd [git_read --stderr read-tree \
975 fconfigure $fd -blocking 0 -translation binary
976 fileevent $fd readable [cb _readtree_wait $fd]
979 method _do_validate_submodule_cloning {ok} {
984 _clone_failed $this [mc "Cannot clone submodules."]
988 method _do_clone_submodules {} {
989 if {$recursive eq {true}} {
991 set o_cons [console::embed \
993 [mc "Cloning submodules"]]
994 pack $w_body -fill both -expand 1 -padx 10
996 [list git submodule update --init --recursive] \
997 [cb _do_validate_submodule_cloning]
1003 method _readtree_wait {fd} {
1005 $o_cons update_meter $buf
1006 append readtree_err $buf
1008 fconfigure $fd -blocking 1
1010 fconfigure $fd -blocking 0
1014 if {[catch {close $fd}]} {
1015 set err $readtree_err
1016 regsub {^fatal: } $err {} err
1017 error_popup [strcat \
1018 [mc "Initial file checkout failed."] \
1023 # -- Run the post-checkout hook.
1025 set fd_ph [githook_read post-checkout [string repeat 0 40] \
1026 [git rev-parse HEAD] 1]
1030 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
1031 fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
1033 _do_clone_submodules $this
1037 method _postcheckout_wait {fd_ph} {
1040 append pch_error [read $fd_ph]
1041 fconfigure $fd_ph -blocking 1
1043 if {[catch {close $fd_ph}]} {
1044 hook_failed_popup post-checkout $pch_error 0
1047 _do_clone_submodules $this
1050 fconfigure $fd_ph -blocking 0
1053 ######################################################################
1055 ## Open Existing Repository
1057 method _do_open {} {
1061 -command [cb _do_open2] \
1064 ${NS}::frame $w_body
1065 ${NS}::label $w_body.h \
1066 -font font_uibold -anchor center \
1067 -text [mc "Open Existing Repository"]
1068 pack $w_body.h -side top -fill x -pady 10
1069 pack $w_body -fill x -padx 10
1071 ${NS}::frame $w_body.where
1072 ${NS}::label $w_body.where.l -text [mc "Repository:"]
1073 ${NS}::entry $w_body.where.t \
1074 -textvariable @local_path \
1076 ${NS}::button $w_body.where.b \
1077 -text [mc "Browse"] \
1078 -command [cb _open_local_path]
1080 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
1081 pack $w_body.where -fill x
1083 grid columnconfigure $w_body.where 1 -weight 1
1085 trace add variable @local_path write [cb _write_local_path]
1086 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
1088 focus $w_body.where.t
1091 method _open_local_path {} {
1092 if {$local_path ne {}} {
1098 set p [tk_chooseDirectory \
1101 -title [mc "Git Repository"] \
1103 if {$p eq {}} return
1105 set p [file normalize $p]
1106 if {![_is_git [file join $p .git]]} {
1107 error_popup [mc "Not a Git repository: %s" [file tail $p]]
1113 method _do_open2 {} {
1114 if {![_is_git [file join $local_path .git] actualgit]} {
1115 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
1119 if {[catch {cd $local_path} err]} {
1120 error_popup [strcat \
1121 [mc "Failed to open repository %s:" $local_path] \
1126 _append_recentrepos [pwd]
1127 set ::_gitdir $actualgit