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)
25 global M1T M1B use_ttk NS
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 [get_bg_color
$w_body] \
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"]
135 ${NS
}::label $w_body.space
136 ${NS
}::label $w_body.recentlabel
\
138 -text [mc
"Open Recent Repository:"]
139 set w_recentlist
$w_body.recentlist
141 -cursor $::cursor_ptr \
143 -background [get_bg_color
$w_body.recentlabel
] \
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
179 ${NS
}::frame $w.buttons
180 set w_next
$w.buttons.next
181 set w_quit
$w.buttons.quit
182 ${NS
}::button $w_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
208 eval destroy [winfo children
$top]
213 if {[catch {set h
$::env(HOME
)}]
214 ||
![file isdirectory
$h]} {
221 set nx
[winfo reqwidth
$top]
222 set ny
[winfo reqheight
$top]
223 set rx
[expr {([winfo screenwidth
$top] - $nx) / 3}]
224 set ry
[expr {([winfo screenheight
$top] - $ny) / 3}]
225 wm geometry
$top [format {+%d
+%d
} $rx $ry]
228 method _invoke_next
{} {
229 if {[winfo exists
$w_next]} {
230 uplevel #0 [$w_next cget -command]
234 proc _get_recentrepos
{} {
236 foreach p
[get_config gui.recentrepo
] {
237 if {[_is_git
[file join $p .git
]]} {
243 return [lsort $recent]
246 proc _unset_recentrepo
{p
} {
247 regsub -all -- {([()\[\]{}\.^
$+*?
\\])} $p {\\\1} p
248 git config
--global --unset gui.recentrepo
"^$p\$"
252 proc _append_recentrepos
{path
} {
253 set path
[file normalize
$path]
254 set recent
[get_config gui.recentrepo
]
256 if {[lindex $recent end
] eq
$path} {
260 set i
[lsearch $recent $path]
262 _unset_recentrepo
$path
263 set recent
[lreplace $recent $i $i]
267 git config
--global --add gui.recentrepo
$path
270 while {[llength $recent] > 10} {
271 _unset_recentrepo
[lindex $recent 0]
272 set recent
[lrange $recent 1 end
]
276 method _open_recent
{xy
} {
277 set id
[lindex [split [$w_recentlist index
@$xy] .
] 0]
278 set local_path
[lindex $sorted_recent [expr {$id - 1}]]
282 method _open_recent_path
{p
} {
287 method _next
{action
} {
290 if {![winfo exists
$w_next]} {
291 ${NS
}::button $w_next -default active
292 pack $w_next -side right
-padx 5 -before $w_quit
297 method _write_local_path
{args
} {
298 if {$local_path eq
{}} {
299 $w_next conf
-state disabled
301 $w_next conf
-state normal
305 method _git_init
{} {
306 if {[catch {file mkdir
$local_path} err
]} {
307 error_popup
[strcat
\
308 [mc
"Failed to create repository %s:" $local_path] \
313 if {[catch {cd $local_path} err
]} {
314 error_popup
[strcat
\
315 [mc
"Failed to create repository %s:" $local_path] \
320 if {[catch {git init
} err
]} {
321 error_popup
[strcat
\
322 [mc
"Failed to create repository %s:" $local_path] \
327 _append_recentrepos
[pwd]
333 proc _is_git
{path
} {
334 if {[file exists
[file join $path HEAD
]]
335 && [file exists
[file join $path objects
]]
336 && [file exists
[file join $path config
]]} {
340 if {[file exists
[file join $path HEAD
]]
341 && [file exists
[file join $path objects.lnk
]]
342 && [file exists
[file join $path config.lnk
]]} {
349 proc _objdir
{path
} {
350 set objdir
[file join $path .git objects
]
351 if {[file isdirectory
$objdir]} {
355 set objdir
[file join $path objects
]
356 if {[file isdirectory
$objdir]} {
361 set objdir
[file join $path .git objects.lnk
]
362 if {[file isfile
$objdir]} {
363 return [win32_read_lnk
$objdir]
366 set objdir
[file join $path objects.lnk
]
367 if {[file isfile
$objdir]} {
368 return [win32_read_lnk
$objdir]
375 ######################################################################
377 ## Create New Repository
383 -command [cb _do_new2
] \
387 ${NS
}::label $w_body.h
\
388 -font font_uibold
-anchor center
\
389 -text [mc
"Create New Repository"]
390 pack $w_body.h
-side top
-fill x
-pady 10
391 pack $w_body -fill x
-padx 10
393 ${NS
}::frame $w_body.where
394 ${NS
}::label $w_body.where.l
-text [mc
"Directory:"]
395 ${NS
}::entry $w_body.where.t
\
396 -textvariable @local_path
\
398 ${NS
}::button $w_body.where.b
\
399 -text [mc
"Browse"] \
400 -command [cb _new_local_path
]
401 set w_localpath
$w_body.where.t
403 grid $w_body.where.l
$w_body.where.t
$w_body.where.b
-sticky ew
404 pack $w_body.where
-fill x
406 grid columnconfigure
$w_body.where
1 -weight 1
408 trace add
variable @local_path write
[cb _write_local_path
]
409 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
411 focus $w_body.where.t
414 method _new_local_path
{} {
415 if {$local_path ne
{}} {
416 set p
[file dirname
$local_path]
421 set p
[tk_chooseDirectory \
424 -title [mc
"Git Repository"] \
428 set p
[file normalize
$p]
433 $w_localpath icursor end
437 if {![_new_ok
$local_path]} {
440 if {![_git_init
$this]} {
447 if {[file isdirectory
$p]} {
448 if {[_is_git
[file join $p .git
]]} {
449 error_popup
[mc
"Directory %s already exists." $p]
452 } elseif
{[file exists
$p]} {
453 error_popup
[mc
"File %s already exists." $p]
459 ######################################################################
461 ## Clone Existing Repository
463 method _do_clone
{} {
467 -command [cb _do_clone2
] \
471 ${NS
}::label $w_body.h
\
472 -font font_uibold
-anchor center
\
473 -text [mc
"Clone Existing Repository"]
474 pack $w_body.h
-side top
-fill x
-pady 10
475 pack $w_body -fill x
-padx 10
477 set args
$w_body.args
478 ${NS
}::frame $w_body.args
479 pack $args -fill both
481 ${NS
}::label $args.origin_l
-text [mc
"Source Location:"]
482 ${NS
}::entry $args.origin_t
\
483 -textvariable @origin_url
\
485 ${NS
}::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 ${NS
}::label $args.where_l
-text [mc
"Target Directory:"]
491 ${NS
}::entry $args.where_t
\
492 -textvariable @local_path
\
494 ${NS
}::button $args.where_b
\
495 -text [mc
"Browse"] \
496 -command [cb _new_local_path
]
497 grid $args.where_l
$args.where_t
$args.where_b
-sticky ew
498 set w_localpath
$args.where_t
500 ${NS
}::label $args.type_l
-text [mc
"Clone Type:"]
501 ${NS
}::frame $args.type_f
503 lappend w_types
[${NS
}::radiobutton $args.type_f.hardlink
\
505 -text [mc
"Standard (Fast, Semi-Redundant, Hardlinks)"] \
506 -variable @clone_type
\
508 lappend w_types
[${NS
}::radiobutton $args.type_f.full
\
510 -text [mc
"Full Copy (Slower, Redundant Backup)"] \
511 -variable @clone_type
\
513 lappend w_types
[${NS
}::radiobutton $args.type_f.shared
\
515 -text [mc
"Shared (Fastest, Not Recommended, No Backup)"] \
516 -variable @clone_type
\
521 grid $args.type_l
$args.type_f
-sticky new
523 grid columnconfigure
$args 1 -weight 1
525 trace add
variable @local_path write
[cb _update_clone
]
526 trace add
variable @origin_url write
[cb _update_clone
]
527 bind $w_body.h
<Destroy
> "
528 [list trace remove variable @local_path write [cb _update_clone]]
529 [list trace remove variable @origin_url write [cb _update_clone]]
535 method _open_origin
{} {
536 if {$origin_url ne
{} && [file isdirectory
$origin_url]} {
542 set p
[tk_chooseDirectory \
545 -title [mc
"Git Repository"] \
549 set p
[file normalize
$p]
550 if {![_is_git
[file join $p .git
]] && ![_is_git
$p]} {
551 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
557 method _update_clone
{args
} {
558 if {$local_path ne
{} && $origin_url ne
{}} {
559 $w_next conf
-state normal
561 $w_next conf
-state disabled
564 if {$origin_url ne
{} &&
565 ( [_is_git
[file join $origin_url .git
]]
566 ||
[_is_git
$origin_url])} {
568 if {[[lindex $w_types 0] cget
-state] eq
{disabled
}} {
569 set clone_type hardlink
581 method _do_clone2
{} {
582 if {[file isdirectory
$origin_url]} {
583 set origin_url
[file normalize
$origin_url]
586 if {$clone_type eq
{hardlink
} && ![file isdirectory
$origin_url]} {
587 error_popup
[mc
"Standard only available for local repository."]
590 if {$clone_type eq
{shared
} && ![file isdirectory
$origin_url]} {
591 error_popup
[mc
"Shared only available for local repository."]
595 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
596 set objdir
[_objdir
$origin_url]
598 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
603 set giturl
$origin_url
604 if {[is_Cygwin
] && [file isdirectory
$giturl]} {
605 set giturl
[exec cygpath
--unix --absolute $giturl]
606 if {$clone_type eq
{shared
}} {
607 set objdir
[exec cygpath
--unix --absolute $objdir]
611 if {[file exists
$local_path]} {
612 error_popup
[mc
"Location %s already exists." $local_path]
616 if {![_git_init
$this]} return
620 git config remote.
$origin_name.url
$giturl
621 git config remote.
$origin_name.fetch
+refs
/heads
/*:refs
/remotes
/$origin_name/*
623 error_popup
[strcat
[mc
"Failed to configure origin"] "\n\n$err"]
627 destroy $w_body $w_next
629 switch -exact -- $clone_type {
631 set o_cons
[status_bar
::two_line $w_body]
632 pack $w_body -fill x
-padx 10 -pady 10
635 [mc
"Counting objects"] \
639 if {[file exists
[file join $objdir info alternates
]]} {
642 file mkdir
[gitdir objects
info]
643 set f_in
[open [file join $objdir info alternates
] r
]
644 set f_cp
[open [gitdir objects
info alternates
] w
]
645 fconfigure $f_in -translation binary -encoding binary
646 fconfigure $f_cp -translation binary -encoding binary
648 while {[gets $f_in line
] >= 0} {
650 puts $f_cp [exec cygpath
--unix --absolute $line]
652 puts $f_cp [file normalize
$line]
660 _clone_failed
$this [mc
"Unable to copy objects/info/alternates: %s" $err]
669 -directory [file join $objdir] ??
]
670 set bcnt
[expr {[llength $buckets] + 2}]
672 $o_cons update $bcur $bcnt
675 file mkdir
[file join .git objects
pack]
676 foreach i
[glob -tails -nocomplain \
677 -directory [file join $objdir pack] *] {
678 lappend tolink
[file join pack $i]
680 $o_cons update [incr bcur
] $bcnt
684 file mkdir
[file join .git objects
$i]
685 foreach j
[glob -tails -nocomplain \
686 -directory [file join $objdir $i] *] {
687 lappend tolink
[file join $i $j]
689 $o_cons update [incr bcur
] $bcnt
696 [mc
"Nothing to clone from %s." $origin_url] \
698 [mc
"The 'master' branch has not been initialized."] \
705 set i
[lindex $tolink 0]
708 [file join .git objects
$i] \
709 [file join $objdir $i]
711 info_popup
[mc
"Hardlinks are unavailable. Falling back to copying."]
712 set i
[_copy_files
$this $objdir $tolink]
714 set i
[_link_files
$this $objdir [lrange $tolink 1 end
]]
721 set o_cons
[console::embed \
723 [mc
"Cloning from %s" $origin_url]]
724 pack $w_body -fill both
-expand 1 -padx 10
726 [list git fetch
--no-tags
-k $origin_name] \
730 set fd
[open [gitdir objects
info alternates
] w
]
731 fconfigure $fd -translation binary
737 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
738 if {![_clone_refs
$this]} return
742 set HEAD
[git rev-parse
--verify HEAD^
0]
744 _clone_failed
$this [mc
"Not a Git repository: %s" [file tail
$origin_url]]
748 _do_clone_checkout
$this $HEAD
752 method _copy_files
{objdir tocopy
} {
754 [mc
"Copying objects"] \
759 incr tot
[file size
[file join $objdir $p]]
763 set f_in
[open [file join $objdir $p] r
]
764 set f_cp
[open [file join .git objects
$p] w
]
765 fconfigure $f_in -translation binary -encoding binary
766 fconfigure $f_cp -translation binary -encoding binary
768 while {![eof $f_in]} {
769 incr cmp
[fcopy $f_in $f_cp -size 16384]
771 [expr {$cmp / 1024}] \
779 _clone_failed
$this [mc
"Unable to copy object: %s" $err]
786 method _link_files
{objdir tolink
} {
787 set total
[llength $tolink]
789 [mc
"Linking objects"] \
791 for {set i
0} {$i < $total} {} {
792 set p
[lindex $tolink $i]
795 [file join .git objects
$p] \
796 [file join $objdir $p]
798 _clone_failed
$this [mc
"Unable to hardlink object: %s" $err]
804 $o_cons update $i $total
811 method _clone_refs
{} {
813 if {[catch {cd $origin_url} err
]} {
814 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
817 set fd_in
[git_read for-each-ref
\
819 {--format=list %(refname
) %(objectname
) %(*objectname
)}]
822 set fd
[open [gitdir packed-refs
] w
]
823 fconfigure $fd -translation binary
824 puts $fd "# pack-refs with: peeled"
825 while {[gets $fd_in line
] >= 0} {
826 set line
[eval $line]
827 set refn
[lindex $line 0]
828 set robj
[lindex $line 1]
829 set tobj
[lindex $line 2]
831 if {[regsub ^refs
/heads
/ $refn \
832 "refs/remotes/$origin_name/" refn
]} {
833 puts $fd "$robj $refn"
834 } elseif
{[string match refs
/tags
/* $refn]} {
835 puts $fd "$robj $refn"
846 method _do_clone_tags
{ok
} {
849 [list git fetch
--tags -k $origin_name] \
853 _clone_failed
$this [mc
"Cannot fetch branches and objects. See console output for details."]
857 method _do_clone_HEAD
{ok
} {
860 [list git fetch
$origin_name HEAD
] \
861 [cb _do_clone_full_end
]
864 _clone_failed
$this [mc
"Cannot fetch tags. See console output for details."]
868 method _do_clone_full_end
{ok
} {
875 if {[file exists
[gitdir FETCH_HEAD
]]} {
876 set fd
[open [gitdir FETCH_HEAD
] r
]
877 while {[gets $fd line
] >= 0} {
878 if {[regexp "^(.{40})\t\t" $line line HEAD
]} {
885 catch {git pack-refs
}
886 _do_clone_checkout
$this $HEAD
888 _clone_failed
$this [mc
"Cannot determine HEAD. See console output for details."]
892 method _clone_failed
{{why
{}}} {
893 if {[catch {file delete
-force $local_path} err
]} {
897 [mc
"Unable to cleanup %s" $local_path] \
903 error_popup
[strcat
[mc
"Clone failed."] "\n" $why]
907 method _do_clone_checkout
{HEAD
} {
910 [mc
"No default branch obtained."] \
912 [mc
"The 'master' branch has not been initialized."] \
918 git update-ref HEAD
$HEAD^
0
921 [mc
"Cannot resolve %s as a commit." $HEAD^
0] \
924 [mc
"The 'master' branch has not been initialized."] \
930 set o_cons
[status_bar
::two_line $w_body]
931 pack $w_body -fill x
-padx 10 -pady 10
933 [mc
"Creating working directory"] \
937 set fd
[git_read
--stderr read-tree
\
944 fconfigure $fd -blocking 0 -translation binary
945 fileevent $fd readable
[cb _readtree_wait
$fd]
948 method _readtree_wait
{fd
} {
950 $o_cons update_meter
$buf
951 append readtree_err
$buf
953 fconfigure $fd -blocking 1
955 fconfigure $fd -blocking 0
959 if {[catch {close $fd}]} {
960 set err
$readtree_err
961 regsub {^fatal
: } $err {} err
962 error_popup
[strcat
\
963 [mc
"Initial file checkout failed."] \
968 # -- Run the post-checkout hook.
970 set fd_ph
[githook_read post-checkout
[string repeat
0 40] \
971 [git rev-parse HEAD
] 1]
975 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
976 fileevent $fd_ph readable
[cb _postcheckout_wait
$fd_ph]
982 method _postcheckout_wait
{fd_ph
} {
985 append pch_error
[read $fd_ph]
986 fconfigure $fd_ph -blocking 1
988 if {[catch {close $fd_ph}]} {
989 hook_failed_popup post-checkout
$pch_error 0
995 fconfigure $fd_ph -blocking 0
998 ######################################################################
1000 ## Open Existing Repository
1002 method _do_open
{} {
1006 -command [cb _do_open2
] \
1009 ${NS
}::frame $w_body
1010 ${NS
}::label $w_body.h
\
1011 -font font_uibold
-anchor center
\
1012 -text [mc
"Open Existing Repository"]
1013 pack $w_body.h
-side top
-fill x
-pady 10
1014 pack $w_body -fill x
-padx 10
1016 ${NS
}::frame $w_body.where
1017 ${NS
}::label $w_body.where.l
-text [mc
"Repository:"]
1018 ${NS
}::entry $w_body.where.t
\
1019 -textvariable @local_path
\
1021 ${NS
}::button $w_body.where.b
\
1022 -text [mc
"Browse"] \
1023 -command [cb _open_local_path
]
1025 grid $w_body.where.l
$w_body.where.t
$w_body.where.b
-sticky ew
1026 pack $w_body.where
-fill x
1028 grid columnconfigure
$w_body.where
1 -weight 1
1030 trace add
variable @local_path write
[cb _write_local_path
]
1031 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
1033 focus $w_body.where.t
1036 method _open_local_path
{} {
1037 if {$local_path ne
{}} {
1043 set p
[tk_chooseDirectory \
1046 -title [mc
"Git Repository"] \
1048 if {$p eq
{}} return
1050 set p
[file normalize
$p]
1051 if {![_is_git
[file join $p .git
]]} {
1052 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
1058 method _do_open2
{} {
1059 if {![_is_git
[file join $local_path .git
]]} {
1060 error_popup
[mc
"Not a Git repository: %s" [file tail
$local_path]]
1064 if {[catch {cd $local_path} err
]} {
1065 error_popup
[strcat
\
1066 [mc
"Failed to open repository %s:" $local_path] \
1071 _append_recentrepos
[pwd]