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 $opts conf -state disabled
113 set sorted_recent [_get_recentrepos]
114 if {[llength $sorted_recent] > 0} {
116 $m_repo add separator
117 $m_repo add command \
119 -label [mc "Recent Repositories"]
123 label $w_body.recentlabel \
125 -text [mc "Open Recent Repository:"]
126 set w_recentlist $w_body.recentlist
128 -cursor $::cursor_ptr \
130 -background [$w_body.recentlabel cget -background] \
134 $w_recentlist tag conf link \
137 set home "[file normalize $::env(HOME)][file separator]"
138 set hlen [string length $home]
139 foreach p $sorted_recent {
141 if {[string equal -length $hlen $home $p]} {
142 set p "~[file separator][string range $p $hlen end]"
144 regsub -all "\n" $p "\\n" p
145 $w_recentlist insert end $p link
146 $w_recentlist insert end "\n"
149 $m_repo add command \
150 -command [cb _open_recent_path $path] \
154 $w_recentlist conf -state disabled
155 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
156 pack $w_body.space -anchor w -fill x
157 pack $w_body.recentlabel -anchor w -fill x
158 pack $w_recentlist -anchor w -fill x
160 pack $w_body -fill x -padx 10 -pady 10
163 set w_next $w.buttons.next
164 set w_quit $w.buttons.quit
168 pack $w_quit -side right -padx 5
169 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
172 $m_repo add separator
173 $m_repo add command \
179 bind $top <Return> [cb _invoke_next]
180 bind $top <Visibility> "
184 bind $top <Visibility> {}
187 tkwait variable @done
190 eval destroy [winfo children $top]
195 if {[catch {set h $::env(HOME)}]
196 || ![file isdirectory $h]} {
203 set nx [winfo reqwidth $top]
204 set ny [winfo reqheight $top]
205 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
206 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
207 wm geometry $top [format {+%d+%d} $rx $ry]
210 method _invoke_next {} {
211 if {[winfo exists $w_next]} {
212 uplevel #0 [$w_next cget -command]
216 proc _get_recentrepos {} {
218 foreach p [get_config gui.recentrepo] {
219 if {[_is_git [file join $p .git]]} {
223 return [lsort $recent]
226 proc _unset_recentrepo {p} {
227 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
228 git config --global --unset gui.recentrepo "^$p\$"
231 proc _append_recentrepos {path} {
232 set path [file normalize $path]
233 set recent [get_config gui.recentrepo]
235 if {[lindex $recent end] eq $path} {
239 set i [lsearch $recent $path]
241 _unset_recentrepo $path
242 set recent [lreplace $recent $i $i]
246 git config --global --add gui.recentrepo $path
248 while {[llength $recent] > 10} {
249 _unset_recentrepo [lindex $recent 0]
250 set recent [lrange $recent 1 end]
254 method _open_recent {xy} {
255 set id [lindex [split [$w_recentlist index @$xy] .] 0]
256 set local_path [lindex $sorted_recent [expr {$id - 1}]]
260 method _open_recent_path {p} {
265 method _next {action} {
267 if {![winfo exists $w_next]} {
268 button $w_next -default active
269 pack $w_next -side right -padx 5 -before $w_quit
274 method _write_local_path {args} {
275 if {$local_path eq {}} {
276 $w_next conf -state disabled
278 $w_next conf -state normal
282 method _git_init {} {
283 if {[file exists $local_path]} {
284 error_popup [mc "Location %s already exists." $local_path]
288 if {[catch {file mkdir $local_path} err]} {
289 error_popup [strcat \
290 [mc "Failed to create repository %s:" $local_path] \
295 if {[catch {cd $local_path} err]} {
296 error_popup [strcat \
297 [mc "Failed to create repository %s:" $local_path] \
302 if {[catch {git init} err]} {
303 error_popup [strcat \
304 [mc "Failed to create repository %s:" $local_path] \
309 _append_recentrepos [pwd]
315 proc _is_git {path} {
316 if {[file exists [file join $path HEAD]]
317 && [file exists [file join $path objects]]
318 && [file exists [file join $path config]]} {
324 ######################################################################
326 ## Create New Repository
331 -command [cb _do_new2] \
337 -text [mc "Create New Repository"]
338 pack $w_body.h -side top -fill x -pady 10
339 pack $w_body -fill x -padx 10
342 label $w_body.where.l -text [mc "Directory:"]
343 entry $w_body.where.t \
344 -textvariable @local_path \
347 button $w_body.where.b \
348 -text [mc "Browse"] \
349 -command [cb _new_local_path]
351 pack $w_body.where.b -side right
352 pack $w_body.where.l -side left
353 pack $w_body.where.t -fill x
354 pack $w_body.where -fill x
356 trace add variable @local_path write [cb _write_local_path]
357 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
359 focus $w_body.where.t
362 method _new_local_path {} {
363 if {$local_path ne {}} {
364 set p [file dirname $local_path]
369 set p [tk_chooseDirectory \
372 -title [mc "Git Repository"] \
376 set p [file normalize $p]
377 if {[file isdirectory $p]} {
387 error_popup [mc "Directory %s already exists." $p]
392 if {[catch {file delete $p} err]} {
393 error_popup [strcat \
394 [mc "Directory %s already exists." $p] \
398 } elseif {[file exists $p]} {
399 error_popup [mc "File %s already exists." $p]
406 if {![_git_init $this]} {
412 ######################################################################
414 ## Clone Existing Repository
416 method _do_clone {} {
419 -command [cb _do_clone2] \
425 -text [mc "Clone Existing Repository"]
426 pack $w_body.h -side top -fill x -pady 10
427 pack $w_body -fill x -padx 10
429 set args $w_body.args
431 pack $args -fill both
433 label $args.origin_l -text [mc "URL:"]
434 entry $args.origin_t \
435 -textvariable @origin_url \
438 button $args.origin_b \
439 -text [mc "Browse"] \
440 -command [cb _open_origin]
441 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
443 label $args.where_l -text [mc "Directory:"]
444 entry $args.where_t \
445 -textvariable @local_path \
448 button $args.where_b \
449 -text [mc "Browse"] \
450 -command [cb _new_local_path]
451 grid $args.where_l $args.where_t $args.where_b -sticky ew
453 label $args.type_l -text [mc "Clone Type:"]
456 lappend w_types [radiobutton $args.type_f.hardlink \
459 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
460 -variable @clone_type \
462 lappend w_types [radiobutton $args.type_f.full \
465 -text [mc "Full Copy (Slower, Redundant Backup)"] \
466 -variable @clone_type \
468 lappend w_types [radiobutton $args.type_f.shared \
471 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
472 -variable @clone_type \
477 grid $args.type_l $args.type_f -sticky new
479 grid columnconfigure $args 1 -weight 1
481 trace add variable @local_path write [cb _update_clone]
482 trace add variable @origin_url write [cb _update_clone]
483 bind $w_body.h <Destroy> "
484 [list trace remove variable @local_path write [cb _update_clone]]
485 [list trace remove variable @origin_url write [cb _update_clone]]
491 method _open_origin {} {
492 if {$origin_url ne {} && [file isdirectory $origin_url]} {
498 set p [tk_chooseDirectory \
501 -title [mc "Git Repository"] \
505 set p [file normalize $p]
506 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
507 error_popup [mc "Not a Git repository: %s" [file tail $p]]
513 method _update_clone {args} {
514 if {$local_path ne {} && $origin_url ne {}} {
515 $w_next conf -state normal
517 $w_next conf -state disabled
520 if {$origin_url ne {} &&
521 ( [_is_git [file join $origin_url .git]]
522 || [_is_git $origin_url])} {
524 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
525 set clone_type hardlink
537 method _do_clone2 {} {
538 if {[file isdirectory $origin_url]} {
539 set origin_url [file normalize $origin_url]
542 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
543 error_popup [mc "Standard only available for local repository."]
546 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
547 error_popup [mc "Shared only available for local repository."]
551 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
552 set objdir [file join $origin_url .git objects]
553 if {![file isdirectory $objdir]} {
554 set objdir [file join $origin_url objects]
555 if {![file isdirectory $objdir]} {
556 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
562 set giturl $origin_url
563 if {[is_Cygwin] && [file isdirectory $giturl]} {
564 set giturl [exec cygpath --unix --absolute $giturl]
565 if {$clone_type eq {shared}} {
566 set objdir [exec cygpath --unix --absolute $objdir]
570 if {![_git_init $this]} return
574 git config remote.$origin_name.url $giturl
575 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
577 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
581 destroy $w_body $w_next
583 switch -exact -- $clone_type {
585 set o_cons [status_bar::two_line $w_body]
586 pack $w_body -fill x -padx 10 -pady 10
589 [mc "Counting objects"] \
593 if {[file exists [file join $objdir info alternates]]} {
596 file mkdir [gitdir objects info]
597 set f_in [open [file join $objdir info alternates] r]
598 set f_cp [open [gitdir objects info alternates] w]
599 fconfigure $f_in -translation binary -encoding binary
600 fconfigure $f_cp -translation binary -encoding binary
602 while {[gets $f_in line] >= 0} {
604 puts $f_cp [exec cygpath --unix --absolute $line]
606 puts $f_cp [file normalize $line]
614 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
623 -directory [file join $objdir] ??]
624 set bcnt [expr {[llength $buckets] + 2}]
626 $o_cons update $bcur $bcnt
629 file mkdir [file join .git objects pack]
630 foreach i [glob -tails -nocomplain \
631 -directory [file join $objdir pack] *] {
632 lappend tolink [file join pack $i]
634 $o_cons update [incr bcur] $bcnt
638 file mkdir [file join .git objects $i]
639 foreach j [glob -tails -nocomplain \
640 -directory [file join $objdir $i] *] {
641 lappend tolink [file join $i $j]
643 $o_cons update [incr bcur] $bcnt
650 [mc "Nothing to clone from %s." $origin_url] \
652 [mc "The 'master' branch has not been initialized."] \
659 set i [lindex $tolink 0]
662 [file join .git objects $i] \
663 [file join $objdir $i]
665 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
666 set i [_copy_files $this $objdir $tolink]
668 set i [_link_files $this $objdir [lrange $tolink 1 end]]
675 set o_cons [console::embed \
677 [mc "Cloning from %s" $origin_url]]
678 pack $w_body -fill both -expand 1 -padx 10
680 [list git fetch --no-tags -k $origin_name] \
684 set fd [open [gitdir objects info alternates] w]
685 fconfigure $fd -translation binary
691 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
692 if {![_clone_refs $this]} return
696 set HEAD [git rev-parse --verify HEAD^0]
698 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
702 _do_clone_checkout $this $HEAD
706 method _copy_files {objdir tocopy} {
708 [mc "Copying objects"] \
713 incr tot [file size [file join $objdir $p]]
717 set f_in [open [file join $objdir $p] r]
718 set f_cp [open [file join .git objects $p] w]
719 fconfigure $f_in -translation binary -encoding binary
720 fconfigure $f_cp -translation binary -encoding binary
722 while {![eof $f_in]} {
723 incr cmp [fcopy $f_in $f_cp -size 16384]
725 [expr {$cmp / 1024}] \
733 _clone_failed $this [mc "Unable to copy object: %s" $err]
740 method _link_files {objdir tolink} {
741 set total [llength $tolink]
743 [mc "Linking objects"] \
745 for {set i 0} {$i < $total} {} {
746 set p [lindex $tolink $i]
749 [file join .git objects $p] \
750 [file join $objdir $p]
752 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
758 $o_cons update $i $total
765 method _clone_refs {} {
767 if {[catch {cd $origin_url} err]} {
768 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
771 set fd_in [git_read for-each-ref \
773 {--format=list %(refname) %(objectname) %(*objectname)}]
776 set fd [open [gitdir packed-refs] w]
777 fconfigure $fd -translation binary
778 puts $fd "# pack-refs with: peeled"
779 while {[gets $fd_in line] >= 0} {
780 set line [eval $line]
781 set refn [lindex $line 0]
782 set robj [lindex $line 1]
783 set tobj [lindex $line 2]
785 if {[regsub ^refs/heads/ $refn \
786 "refs/remotes/$origin_name/" refn]} {
787 puts $fd "$robj $refn"
788 } elseif {[string match refs/tags/* $refn]} {
789 puts $fd "$robj $refn"
800 method _do_clone_tags {ok} {
803 [list git fetch --tags -k $origin_name] \
807 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
811 method _do_clone_HEAD {ok} {
814 [list git fetch $origin_name HEAD] \
815 [cb _do_clone_full_end]
818 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
822 method _do_clone_full_end {ok} {
829 if {[file exists [gitdir FETCH_HEAD]]} {
830 set fd [open [gitdir FETCH_HEAD] r]
831 while {[gets $fd line] >= 0} {
832 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
839 catch {git pack-refs}
840 _do_clone_checkout $this $HEAD
842 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
846 method _clone_failed {{why {}}} {
847 if {[catch {file delete -force $local_path} err]} {
851 [mc "Unable to cleanup %s" $local_path] \
857 error_popup [strcat [mc "Clone failed."] "\n" $why]
861 method _do_clone_checkout {HEAD} {
864 [mc "No default branch obtained."] \
866 [mc "The 'master' branch has not been initialized."] \
872 git update-ref HEAD $HEAD^0
875 [mc "Cannot resolve %s as a commit." $HEAD^0] \
878 [mc "The 'master' branch has not been initialized."] \
884 set o_cons [status_bar::two_line $w_body]
885 pack $w_body -fill x -padx 10 -pady 10
887 [mc "Creating working directory"] \
891 set fd [git_read --stderr read-tree \
898 fconfigure $fd -blocking 0 -translation binary
899 fileevent $fd readable [cb _readtree_wait $fd]
902 method _readtree_wait {fd} {
904 $o_cons update_meter $buf
905 append readtree_err $buf
907 fconfigure $fd -blocking 1
909 fconfigure $fd -blocking 0
913 if {[catch {close $fd}]} {
914 set err $readtree_err
915 regsub {^fatal: } $err {} err
916 error_popup [strcat \
917 [mc "Initial file checkout failed."] \
925 ######################################################################
927 ## Open Existing Repository
932 -command [cb _do_open2] \
938 -text [mc "Open Existing Repository"]
939 pack $w_body.h -side top -fill x -pady 10
940 pack $w_body -fill x -padx 10
943 label $w_body.where.l -text [mc "Repository:"]
944 entry $w_body.where.t \
945 -textvariable @local_path \
948 button $w_body.where.b \
949 -text [mc "Browse"] \
950 -command [cb _open_local_path]
952 pack $w_body.where.b -side right
953 pack $w_body.where.l -side left
954 pack $w_body.where.t -fill x
955 pack $w_body.where -fill x
957 trace add variable @local_path write [cb _write_local_path]
958 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
960 focus $w_body.where.t
963 method _open_local_path {} {
964 if {$local_path ne {}} {
970 set p [tk_chooseDirectory \
973 -title [mc "Git Repository"] \
977 set p [file normalize $p]
978 if {![_is_git [file join $p .git]]} {
979 error_popup [mc "Not a Git repository: %s" [file tail $p]]
985 method _do_open2 {} {
986 if {![_is_git [file join $local_path .git]]} {
987 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
991 if {[catch {cd $local_path} err]} {
992 error_popup [strcat \
993 [mc "Failed to open repository %s:" $local_path] \
998 _append_recentrepos [pwd]