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
33 set m_repo $w.mbar.repository
35 -label [mc Repository] \
40 $w.mbar add cascade -label [mc Apple] -menu .mbar.apple
42 $w.mbar.apple add command \
43 -label [mc "About %s" [appname]] \
46 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
48 $w.mbar.help add command \
49 -label [mc "About %s" [appname]] \
53 wm protocol $top WM_DELETE_WINDOW exit
54 bind $top <$M1B-q> exit
55 bind $top <$M1B-Q> exit
56 bind $top <Key-Escape> exit
58 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
59 bind $top <Key-Escape> [list destroy $top]
63 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
66 set opts $w_body.options
69 -cursor $::cursor_ptr \
71 -background [$w_body cget -background] \
76 pack $opts -anchor w -fill x
78 $opts tag conf link_new -foreground blue -underline 1
79 $opts tag bind link_new <1> [cb _next new]
80 $opts insert end [mc "Create New Repository"] link_new
84 -command [cb _next new] \
89 $opts tag conf link_clone -foreground blue -underline 1
90 $opts tag bind link_clone <1> [cb _next clone]
91 $opts insert end [mc "Clone Existing Repository"] link_clone
95 -command [cb _next clone] \
97 -label [mc "Clone..."]
100 $opts tag conf link_open -foreground blue -underline 1
101 $opts tag bind link_open <1> [cb _next open]
102 $opts insert end [mc "Open Existing Repository"] link_open
103 $opts insert end "\n"
105 $m_repo add command \
106 -command [cb _next open] \
107 -accelerator $M1T-O \
108 -label [mc "Open..."]
111 set sorted_recent [_get_recentrepos]
112 if {[llength $sorted_recent] > 0} {
114 $m_repo add separator
115 $m_repo add command \
117 -label [mc "Recent Repositories"]
121 label $w_body.recentlabel \
123 -text [mc "Open Recent Repository:"]
124 set w_recentlist $w_body.recentlist
126 -cursor $::cursor_ptr \
128 -background [$w_body.recentlabel cget -background] \
132 $w_recentlist tag conf link \
135 set home "[file normalize $::env(HOME)][file separator]"
136 set hlen [string length $home]
137 foreach p $sorted_recent {
139 if {[string equal -length $hlen $home $p]} {
140 set p "~[file separator][string range $p $hlen end]"
142 regsub -all "\n" $p "\\n" p
143 $w_recentlist insert end $p link
144 $w_recentlist insert end "\n"
147 $m_repo add command \
148 -command [cb _open_recent_path $path] \
152 $w_recentlist conf -state disabled
153 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
154 pack $w_body.space -anchor w -fill x
155 pack $w_body.recentlabel -anchor w -fill x
156 pack $w_recentlist -anchor w -fill x
158 pack $w_body -fill x -padx 10 -pady 10
161 set w_next $w.buttons.next
162 set w_quit $w.buttons.quit
166 pack $w_quit -side right -padx 5
167 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
170 $m_repo add separator
171 $m_repo add command \
177 bind $top <Return> [cb _invoke_next]
178 bind $top <Visibility> "
182 bind $top <Visibility> {}
185 tkwait variable @done
188 eval destroy [winfo children $top]
193 if {[catch {set h $::env(HOME)}]
194 || ![file isdirectory $h]} {
201 set nx [winfo reqwidth $top]
202 set ny [winfo reqheight $top]
203 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
204 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
205 wm geometry $top [format {+%d+%d} $rx $ry]
208 method _invoke_next {} {
209 if {[winfo exists $w_next]} {
210 uplevel #0 [$w_next cget -command]
214 proc _get_recentrepos {} {
216 foreach p [get_config gui.recentrepo] {
217 if {[_is_git [file join $p .git]]} {
221 return [lsort $recent]
224 proc _unset_recentrepo {p} {
225 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
226 git config --global --unset gui.recentrepo "^$p\$"
229 proc _append_recentrepos {path} {
230 set path [file normalize $path]
231 set recent [get_config gui.recentrepo]
233 if {[lindex $recent end] eq $path} {
237 set i [lsearch $recent $path]
239 _unset_recentrepo $path
240 set recent [lreplace $recent $i $i]
244 git config --global --add gui.recentrepo $path
246 while {[llength $recent] > 10} {
247 _unset_recentrepo [lindex $recent 0]
248 set recent [lrange $recent 1 end]
252 method _open_recent {xy} {
253 set id [lindex [split [$w_recentlist index @$xy] .] 0]
254 set local_path [lindex $sorted_recent [expr {$id - 1}]]
258 method _open_recent_path {p} {
263 method _next {action} {
265 if {![winfo exists $w_next]} {
266 button $w_next -default active
267 pack $w_next -side right -padx 5 -before $w_quit
272 method _write_local_path {args} {
273 if {$local_path eq {}} {
274 $w_next conf -state disabled
276 $w_next conf -state normal
280 method _git_init {} {
281 if {[file exists $local_path]} {
282 error_popup [mc "Location %s already exists." $local_path]
286 if {[catch {file mkdir $local_path} err]} {
287 error_popup [strcat \
288 [mc "Failed to create repository %s:" $local_path] \
293 if {[catch {cd $local_path} err]} {
294 error_popup [strcat \
295 [mc "Failed to create repository %s:" $local_path] \
300 if {[catch {git init} err]} {
301 error_popup [strcat \
302 [mc "Failed to create repository %s:" $local_path] \
307 _append_recentrepos [pwd]
313 proc _is_git {path} {
314 if {[file exists [file join $path HEAD]]
315 && [file exists [file join $path objects]]
316 && [file exists [file join $path config]]} {
322 ######################################################################
324 ## Create New Repository
329 -command [cb _do_new2] \
335 -text [mc "Create New Repository"]
336 pack $w_body.h -side top -fill x -pady 10
337 pack $w_body -fill x -padx 10
340 label $w_body.where.l -text [mc "Directory:"]
341 entry $w_body.where.t \
342 -textvariable @local_path \
345 button $w_body.where.b \
346 -text [mc "Browse"] \
347 -command [cb _new_local_path]
349 pack $w_body.where.b -side right
350 pack $w_body.where.l -side left
351 pack $w_body.where.t -fill x
352 pack $w_body.where -fill x
354 trace add variable @local_path write [cb _write_local_path]
355 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
357 focus $w_body.where.t
360 method _new_local_path {} {
361 if {$local_path ne {}} {
362 set p [file dirname $local_path]
367 set p [tk_chooseDirectory \
370 -title [mc "Git Repository"] \
374 set p [file normalize $p]
375 if {[file isdirectory $p]} {
385 error_popup [mc "Directory %s already exists." $p]
390 if {[catch {file delete $p} err]} {
391 error_popup [strcat \
392 [mc "Directory %s already exists." $p] \
396 } elseif {[file exists $p]} {
397 error_popup [mc "File %s already exists." $p]
404 if {![_git_init $this]} {
410 ######################################################################
412 ## Clone Existing Repository
414 method _do_clone {} {
417 -command [cb _do_clone2] \
423 -text [mc "Clone Existing Repository"]
424 pack $w_body.h -side top -fill x -pady 10
425 pack $w_body -fill x -padx 10
427 set args $w_body.args
429 pack $args -fill both
431 label $args.origin_l -text [mc "URL:"]
432 entry $args.origin_t \
433 -textvariable @origin_url \
436 button $args.origin_b \
437 -text [mc "Browse"] \
438 -command [cb _open_origin]
439 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
441 label $args.where_l -text [mc "Directory:"]
442 entry $args.where_t \
443 -textvariable @local_path \
446 button $args.where_b \
447 -text [mc "Browse"] \
448 -command [cb _new_local_path]
449 grid $args.where_l $args.where_t $args.where_b -sticky ew
451 label $args.type_l -text [mc "Clone Type:"]
454 lappend w_types [radiobutton $args.type_f.hardlink \
457 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
458 -variable @clone_type \
460 lappend w_types [radiobutton $args.type_f.full \
463 -text [mc "Full Copy (Slower, Redundant Backup)"] \
464 -variable @clone_type \
466 lappend w_types [radiobutton $args.type_f.shared \
469 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
470 -variable @clone_type \
475 grid $args.type_l $args.type_f -sticky new
477 grid columnconfigure $args 1 -weight 1
479 trace add variable @local_path write [cb _update_clone]
480 trace add variable @origin_url write [cb _update_clone]
481 bind $w_body.h <Destroy> "
482 [list trace remove variable @local_path write [cb _update_clone]]
483 [list trace remove variable @origin_url write [cb _update_clone]]
489 method _open_origin {} {
490 if {$origin_url ne {} && [file isdirectory $origin_url]} {
496 set p [tk_chooseDirectory \
499 -title [mc "Git Repository"] \
503 set p [file normalize $p]
504 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
505 error_popup [mc "Not a Git repository: %s" [file tail $p]]
511 method _update_clone {args} {
512 if {$local_path ne {} && $origin_url ne {}} {
513 $w_next conf -state normal
515 $w_next conf -state disabled
518 if {$origin_url ne {} &&
519 ( [_is_git [file join $origin_url .git]]
520 || [_is_git $origin_url])} {
522 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
523 set clone_type hardlink
535 method _do_clone2 {} {
536 if {[file isdirectory $origin_url]} {
537 set origin_url [file normalize $origin_url]
540 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
541 error_popup [mc "Standard only available for local repository."]
544 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
545 error_popup [mc "Shared only available for local repository."]
549 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
550 set objdir [file join $origin_url .git objects]
551 if {![file isdirectory $objdir]} {
552 set objdir [file join $origin_url objects]
553 if {![file isdirectory $objdir]} {
554 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
560 set giturl $origin_url
561 if {[is_Cygwin] && [file isdirectory $giturl]} {
562 set giturl [exec cygpath --unix --absolute $giturl]
563 if {$clone_type eq {shared}} {
564 set objdir [exec cygpath --unix --absolute $objdir]
568 if {![_git_init $this]} return
572 git config remote.$origin_name.url $giturl
573 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
575 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
579 destroy $w_body $w_next
581 switch -exact -- $clone_type {
583 set o_cons [status_bar::two_line $w_body]
584 pack $w_body -fill x -padx 10 -pady 10
587 [mc "Counting objects"] \
591 if {[file exists [file join $objdir info alternates]]} {
594 file mkdir [gitdir objects info]
595 set f_in [open [file join $objdir info alternates] r]
596 set f_cp [open [gitdir objects info alternates] w]
597 fconfigure $f_in -translation binary -encoding binary
598 fconfigure $f_cp -translation binary -encoding binary
600 while {[gets $f_in line] >= 0} {
602 puts $f_cp [exec cygpath --unix --absolute $line]
604 puts $f_cp [file normalize $line]
612 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
621 -directory [file join $objdir] ??]
622 set bcnt [expr {[llength $buckets] + 2}]
624 $o_cons update $bcur $bcnt
627 file mkdir [file join .git objects pack]
628 foreach i [glob -tails -nocomplain \
629 -directory [file join $objdir pack] *] {
630 lappend tolink [file join pack $i]
632 $o_cons update [incr bcur] $bcnt
636 file mkdir [file join .git objects $i]
637 foreach j [glob -tails -nocomplain \
638 -directory [file join $objdir $i] *] {
639 lappend tolink [file join $i $j]
641 $o_cons update [incr bcur] $bcnt
648 [mc "Nothing to clone from %s." $origin_url] \
650 [mc "The 'master' branch has not been initialized."] \
657 set i [lindex $tolink 0]
660 [file join .git objects $i] \
661 [file join $objdir $i]
663 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
664 set i [_copy_files $this $objdir $tolink]
666 set i [_link_files $this $objdir [lrange $tolink 1 end]]
673 set o_cons [console::embed \
675 [mc "Cloning from %s" $origin_url]]
676 pack $w_body -fill both -expand 1 -padx 10
678 [list git fetch --no-tags -k $origin_name] \
682 set fd [open [gitdir objects info alternates] w]
683 fconfigure $fd -translation binary
689 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
690 if {![_clone_refs $this]} return
694 set HEAD [git rev-parse --verify HEAD^0]
696 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
700 _do_clone_checkout $this $HEAD
704 method _copy_files {objdir tocopy} {
706 [mc "Copying objects"] \
711 incr tot [file size [file join $objdir $p]]
715 set f_in [open [file join $objdir $p] r]
716 set f_cp [open [file join .git objects $p] w]
717 fconfigure $f_in -translation binary -encoding binary
718 fconfigure $f_cp -translation binary -encoding binary
720 while {![eof $f_in]} {
721 incr cmp [fcopy $f_in $f_cp -size 16384]
723 [expr {$cmp / 1024}] \
731 _clone_failed $this [mc "Unable to copy object: %s" $err]
738 method _link_files {objdir tolink} {
739 set total [llength $tolink]
741 [mc "Linking objects"] \
743 for {set i 0} {$i < $total} {} {
744 set p [lindex $tolink $i]
747 [file join .git objects $p] \
748 [file join $objdir $p]
750 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
756 $o_cons update $i $total
763 method _clone_refs {} {
765 if {[catch {cd $origin_url} err]} {
766 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
769 set fd_in [git_read for-each-ref \
771 {--format=list %(refname) %(objectname) %(*objectname)}]
774 set fd [open [gitdir packed-refs] w]
775 fconfigure $fd -translation binary
776 puts $fd "# pack-refs with: peeled"
777 while {[gets $fd_in line] >= 0} {
778 set line [eval $line]
779 set refn [lindex $line 0]
780 set robj [lindex $line 1]
781 set tobj [lindex $line 2]
783 if {[regsub ^refs/heads/ $refn \
784 "refs/remotes/$origin_name/" refn]} {
785 puts $fd "$robj $refn"
786 } elseif {[string match refs/tags/* $refn]} {
787 puts $fd "$robj $refn"
798 method _do_clone_tags {ok} {
801 [list git fetch --tags -k $origin_name] \
805 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
809 method _do_clone_HEAD {ok} {
812 [list git fetch $origin_name HEAD] \
813 [cb _do_clone_full_end]
816 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
820 method _do_clone_full_end {ok} {
827 if {[file exists [gitdir FETCH_HEAD]]} {
828 set fd [open [gitdir FETCH_HEAD] r]
829 while {[gets $fd line] >= 0} {
830 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
837 catch {git pack-refs}
838 _do_clone_checkout $this $HEAD
840 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
844 method _clone_failed {{why {}}} {
845 if {[catch {file delete -force $local_path} err]} {
849 [mc "Unable to cleanup %s" $local_path] \
855 error_popup [strcat [mc "Clone failed."] "\n" $why]
859 method _do_clone_checkout {HEAD} {
862 [mc "No default branch obtained."] \
864 [mc "The 'master' branch has not been initialized."] \
870 git update-ref HEAD $HEAD^0
873 [mc "Cannot resolve %s as a commit." $HEAD^0] \
876 [mc "The 'master' branch has not been initialized."] \
882 set o_cons [status_bar::two_line $w_body]
883 pack $w_body -fill x -padx 10 -pady 10
885 [mc "Creating working directory"] \
889 set fd [git_read --stderr read-tree \
896 fconfigure $fd -blocking 0 -translation binary
897 fileevent $fd readable [cb _readtree_wait $fd]
900 method _readtree_wait {fd} {
902 $o_cons update_meter $buf
903 append readtree_err $buf
905 fconfigure $fd -blocking 1
907 fconfigure $fd -blocking 0
911 if {[catch {close $fd}]} {
912 set err $readtree_err
913 regsub {^fatal: } $err {} err
914 error_popup [strcat \
915 [mc "Initial file checkout failed."] \
923 ######################################################################
925 ## Open Existing Repository
930 -command [cb _do_open2] \
936 -text [mc "Open Existing Repository"]
937 pack $w_body.h -side top -fill x -pady 10
938 pack $w_body -fill x -padx 10
941 label $w_body.where.l -text [mc "Repository:"]
942 entry $w_body.where.t \
943 -textvariable @local_path \
946 button $w_body.where.b \
947 -text [mc "Browse"] \
948 -command [cb _open_local_path]
950 pack $w_body.where.b -side right
951 pack $w_body.where.l -side left
952 pack $w_body.where.t -fill x
953 pack $w_body.where -fill x
955 trace add variable @local_path write [cb _write_local_path]
956 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
958 focus $w_body.where.t
961 method _open_local_path {} {
962 if {$local_path ne {}} {
968 set p [tk_chooseDirectory \
971 -title [mc "Git Repository"] \
975 set p [file normalize $p]
976 if {![_is_git [file join $p .git]]} {
977 error_popup [mc "Not a Git repository: %s" [file tail $p]]
983 method _do_open2 {} {
984 if {![_is_git [file join $local_path .git]]} {
985 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
989 if {[catch {cd $local_path} err]} {
990 error_popup [strcat \
991 [mc "Failed to open repository %s:" $local_path] \
996 _append_recentrepos [pwd]