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]
356 focus $w_body.where.t
359 method _new_local_path {} {
360 if {$local_path ne {}} {
361 set p [file dirname $local_path]
366 set p [tk_chooseDirectory \
369 -title [mc "Git Repository"] \
373 set p [file normalize $p]
374 if {[file isdirectory $p]} {
384 error_popup [mc "Directory %s already exists." $p]
389 if {[catch {file delete $p} err]} {
390 error_popup [strcat \
391 [mc "Directory %s already exists." $p] \
395 } elseif {[file exists $p]} {
396 error_popup [mc "File %s already exists." $p]
403 if {![_git_init $this]} {
409 ######################################################################
411 ## Clone Existing Repository
413 method _do_clone {} {
416 -command [cb _do_clone2] \
422 -text [mc "Clone Existing Repository"]
423 pack $w_body.h -side top -fill x -pady 10
424 pack $w_body -fill x -padx 10
426 set args $w_body.args
428 pack $args -fill both
430 label $args.origin_l -text [mc "URL:"]
431 entry $args.origin_t \
432 -textvariable @origin_url \
435 button $args.origin_b \
436 -text [mc "Browse"] \
437 -command [cb _open_origin]
438 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
440 label $args.where_l -text [mc "Directory:"]
441 entry $args.where_t \
442 -textvariable @local_path \
445 button $args.where_b \
446 -text [mc "Browse"] \
447 -command [cb _new_local_path]
448 grid $args.where_l $args.where_t $args.where_b -sticky ew
450 label $args.type_l -text [mc "Clone Type:"]
453 lappend w_types [radiobutton $args.type_f.hardlink \
456 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
457 -variable @clone_type \
459 lappend w_types [radiobutton $args.type_f.full \
462 -text [mc "Full Copy (Slower, Redundant Backup)"] \
463 -variable @clone_type \
465 lappend w_types [radiobutton $args.type_f.shared \
468 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
469 -variable @clone_type \
474 grid $args.type_l $args.type_f -sticky new
476 grid columnconfigure $args 1 -weight 1
478 trace add variable @local_path write [cb _update_clone]
479 trace add variable @origin_url write [cb _update_clone]
484 method _open_origin {} {
485 if {$origin_url ne {} && [file isdirectory $origin_url]} {
491 set p [tk_chooseDirectory \
494 -title [mc "Git Repository"] \
498 set p [file normalize $p]
499 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
500 error_popup [mc "Not a Git repository: %s" [file tail $p]]
506 method _update_clone {args} {
507 if {$local_path ne {} && $origin_url ne {}} {
508 $w_next conf -state normal
510 $w_next conf -state disabled
513 if {$origin_url ne {} &&
514 ( [_is_git [file join $origin_url .git]]
515 || [_is_git $origin_url])} {
517 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
518 set clone_type hardlink
530 method _do_clone2 {} {
531 if {[file isdirectory $origin_url]} {
532 set origin_url [file normalize $origin_url]
535 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
536 error_popup [mc "Standard only available for local repository."]
539 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
540 error_popup [mc "Shared only available for local repository."]
544 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
545 set objdir [file join $origin_url .git objects]
546 if {![file isdirectory $objdir]} {
547 set objdir [file join $origin_url objects]
548 if {![file isdirectory $objdir]} {
549 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
555 set giturl $origin_url
556 if {[is_Cygwin] && [file isdirectory $giturl]} {
557 set giturl [exec cygpath --unix --absolute $giturl]
558 if {$clone_type eq {shared}} {
559 set objdir [exec cygpath --unix --absolute $objdir]
563 if {![_git_init $this]} return
567 git config remote.$origin_name.url $giturl
568 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
570 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
574 destroy $w_body $w_next
576 switch -exact -- $clone_type {
578 set o_cons [status_bar::two_line $w_body]
579 pack $w_body -fill x -padx 10 -pady 10
582 [mc "Counting objects"] \
586 if {[file exists [file join $objdir info alternates]]} {
589 file mkdir [gitdir objects info]
590 set f_in [open [file join $objdir info alternates] r]
591 set f_cp [open [gitdir objects info alternates] w]
592 fconfigure $f_in -translation binary -encoding binary
593 fconfigure $f_cp -translation binary -encoding binary
595 while {[gets $f_in line] >= 0} {
597 puts $f_cp [exec cygpath --unix --absolute $line]
599 puts $f_cp [file normalize $line]
607 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
616 -directory [file join $objdir] ??]
617 set bcnt [expr {[llength $buckets] + 2}]
619 $o_cons update $bcur $bcnt
622 file mkdir [file join .git objects pack]
623 foreach i [glob -tails -nocomplain \
624 -directory [file join $objdir pack] *] {
625 lappend tolink [file join pack $i]
627 $o_cons update [incr bcur] $bcnt
631 file mkdir [file join .git objects $i]
632 foreach j [glob -tails -nocomplain \
633 -directory [file join $objdir $i] *] {
634 lappend tolink [file join $i $j]
636 $o_cons update [incr bcur] $bcnt
643 [mc "Nothing to clone from %s." $origin_url] \
645 [mc "The 'master' branch has not been initialized."] \
652 set i [lindex $tolink 0]
655 [file join .git objects $i] \
656 [file join $objdir $i]
658 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
659 set i [_copy_files $this $objdir $tolink]
661 set i [_link_files $this $objdir [lrange $tolink 1 end]]
668 set o_cons [console::embed \
670 [mc "Cloning from %s" $origin_url]]
671 pack $w_body -fill both -expand 1 -padx 10
673 [list git fetch --no-tags -k $origin_name] \
677 set fd [open [gitdir objects info alternates] w]
678 fconfigure $fd -translation binary
684 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
685 if {![_clone_refs $this]} return
689 set HEAD [git rev-parse --verify HEAD^0]
691 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
695 _do_clone_checkout $this $HEAD
699 method _copy_files {objdir tocopy} {
701 [mc "Copying objects"] \
706 incr tot [file size [file join $objdir $p]]
710 set f_in [open [file join $objdir $p] r]
711 set f_cp [open [file join .git objects $p] w]
712 fconfigure $f_in -translation binary -encoding binary
713 fconfigure $f_cp -translation binary -encoding binary
715 while {![eof $f_in]} {
716 incr cmp [fcopy $f_in $f_cp -size 16384]
718 [expr {$cmp / 1024}] \
726 _clone_failed $this [mc "Unable to copy object: %s" $err]
733 method _link_files {objdir tolink} {
734 set total [llength $tolink]
736 [mc "Linking objects"] \
738 for {set i 0} {$i < $total} {} {
739 set p [lindex $tolink $i]
742 [file join .git objects $p] \
743 [file join $objdir $p]
745 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
751 $o_cons update $i $total
758 method _clone_refs {} {
760 if {[catch {cd $origin_url} err]} {
761 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
764 set fd_in [git_read for-each-ref \
766 {--format=list %(refname) %(objectname) %(*objectname)}]
769 set fd [open [gitdir packed-refs] w]
770 fconfigure $fd -translation binary
771 puts $fd "# pack-refs with: peeled"
772 while {[gets $fd_in line] >= 0} {
773 set line [eval $line]
774 set refn [lindex $line 0]
775 set robj [lindex $line 1]
776 set tobj [lindex $line 2]
778 if {[regsub ^refs/heads/ $refn \
779 "refs/remotes/$origin_name/" refn]} {
780 puts $fd "$robj $refn"
781 } elseif {[string match refs/tags/* $refn]} {
782 puts $fd "$robj $refn"
793 method _do_clone_tags {ok} {
796 [list git fetch --tags -k $origin_name] \
800 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
804 method _do_clone_HEAD {ok} {
807 [list git fetch $origin_name HEAD] \
808 [cb _do_clone_full_end]
811 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
815 method _do_clone_full_end {ok} {
822 if {[file exists [gitdir FETCH_HEAD]]} {
823 set fd [open [gitdir FETCH_HEAD] r]
824 while {[gets $fd line] >= 0} {
825 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
832 catch {git pack-refs}
833 _do_clone_checkout $this $HEAD
835 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
839 method _clone_failed {{why {}}} {
840 if {[catch {file delete -force $local_path} err]} {
844 [mc "Unable to cleanup %s" $local_path] \
850 error_popup [strcat [mc "Clone failed."] "\n" $why]
854 method _do_clone_checkout {HEAD} {
857 [mc "No default branch obtained."] \
859 [mc "The 'master' branch has not been initialized."] \
865 git update-ref HEAD $HEAD^0
868 [mc "Cannot resolve %s as a commit." $HEAD^0] \
871 [mc "The 'master' branch has not been initialized."] \
877 set o_cons [status_bar::two_line $w_body]
878 pack $w_body -fill x -padx 10 -pady 10
880 [mc "Creating working directory"] \
884 set fd [git_read --stderr read-tree \
891 fconfigure $fd -blocking 0 -translation binary
892 fileevent $fd readable [cb _readtree_wait $fd]
895 method _readtree_wait {fd} {
897 $o_cons update_meter $buf
898 append readtree_err $buf
900 fconfigure $fd -blocking 1
902 fconfigure $fd -blocking 0
906 if {[catch {close $fd}]} {
907 set err $readtree_err
908 regsub {^fatal: } $err {} err
909 error_popup [strcat \
910 [mc "Initial file checkout failed."] \
918 ######################################################################
920 ## Open Existing Repository
925 -command [cb _do_open2] \
931 -text [mc "Open Existing Repository"]
932 pack $w_body.h -side top -fill x -pady 10
933 pack $w_body -fill x -padx 10
936 label $w_body.where.l -text [mc "Repository:"]
937 entry $w_body.where.t \
938 -textvariable @local_path \
941 button $w_body.where.b \
942 -text [mc "Browse"] \
943 -command [cb _open_local_path]
945 pack $w_body.where.b -side right
946 pack $w_body.where.l -side left
947 pack $w_body.where.t -fill x
948 pack $w_body.where -fill x
950 trace add variable @local_path write [cb _write_local_path]
952 focus $w_body.where.t
955 method _open_local_path {} {
956 if {$local_path ne {}} {
962 set p [tk_chooseDirectory \
965 -title [mc "Git Repository"] \
969 set p [file normalize $p]
970 if {![_is_git [file join $p .git]]} {
971 error_popup [mc "Not a Git repository: %s" [file tail $p]]
977 method _do_open2 {} {
978 if {![_is_git [file join $local_path .git]]} {
979 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
983 if {[catch {cd $local_path} err]} {
984 error_popup [strcat \
985 [mc "Failed to open repository %s:" $local_path] \
990 _append_recentrepos [pwd]