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
27 if {[set maxrecent
[get_config gui.maxrecentrepo
]] eq
{}} {
32 wm title
$top [mc
"Git Gui"]
35 menu $w.mbar
-tearoff 0
36 $top configure
-menu $w.mbar
38 set m_repo
$w.mbar.repository
40 -label [mc Repository
] \
45 $w.mbar add cascade
-label Apple
-menu .mbar.apple
47 $w.mbar.apple add command
\
48 -label [mc
"About %s" [appname
]] \
50 $w.mbar.apple add command
\
51 -label [mc
"Show SSH Key"] \
54 $w.mbar add cascade
-label [mc Help
] -menu $w.mbar.help
56 $w.mbar.help add command
\
57 -label [mc
"About %s" [appname
]] \
59 $w.mbar.help add command
\
60 -label [mc
"Show SSH Key"] \
64 wm protocol
$top WM_DELETE_WINDOW
exit
65 bind $top <$M1B-q
> exit
66 bind $top <$M1B-Q
> exit
67 bind $top <Key-Escape
> exit
69 wm geometry
$top "+[winfo rootx .]+[winfo rooty .]"
70 bind $top <Key-Escape
> [list destroy $top]
74 pack [git_logo
$w.git_logo
] -side left
-fill y
-padx 10 -pady 10
77 set opts
$w_body.
options
80 -cursor $::cursor_ptr \
82 -background [get_bg_color
$w_body] \
87 pack $opts -anchor w
-fill x
89 $opts tag conf link_new
-foreground blue
-underline 1
90 $opts tag
bind link_new
<1> [cb _next new
]
91 $opts insert end
[mc
"Create New Repository"] link_new
95 -command [cb _next new
] \
98 bind $top <$M1B-n
> [cb _next new
]
99 bind $top <$M1B-N
> [cb _next new
]
102 $opts tag conf link_clone
-foreground blue
-underline 1
103 $opts tag
bind link_clone
<1> [cb _next clone
]
104 $opts insert end
[mc
"Clone Existing Repository"] link_clone
105 $opts insert end
"\n"
107 if {[tk windowingsystem
] eq
"win32"} {
112 $m_repo add command
\
113 -command [cb _next clone
] \
114 -accelerator $M1T-$key \
115 -label [mc
"Clone..."]
116 bind $top <$M1B-[string tolower
$key]> [cb _next clone
]
117 bind $top <$M1B-[string toupper
$key]> [cb _next clone
]
120 $opts tag conf link_open
-foreground blue
-underline 1
121 $opts tag
bind link_open
<1> [cb _next
open]
122 $opts insert end
[mc
"Open Existing Repository"] link_open
123 $opts insert end
"\n"
125 $m_repo add command
\
126 -command [cb _next
open] \
127 -accelerator $M1T-O
\
128 -label [mc
"Open..."]
129 bind $top <$M1B-o
> [cb _next
open]
130 bind $top <$M1B-O
> [cb _next
open]
133 $opts conf
-state disabled
135 set sorted_recent
[_get_recentrepos
]
136 if {[llength $sorted_recent] > 0} {
138 $m_repo add separator
139 $m_repo add command
\
141 -label [mc
"Recent Repositories"]
144 ${NS
}::label $w_body.space
145 ${NS
}::label $w_body.recentlabel
\
147 -text [mc
"Open Recent Repository:"]
148 set w_recentlist
$w_body.recentlist
150 -cursor $::cursor_ptr \
152 -background [get_bg_color
$w_body.recentlabel
] \
156 $w_recentlist tag conf link
\
159 set home
$::env(HOME
)
161 set home
[exec cygpath
--windows --absolute $home]
163 set home
"[file normalize $home]/"
164 set hlen
[string length
$home]
165 foreach p
$sorted_recent {
167 if {[string equal
-length $hlen $home $p]} {
168 set p
"~/[string range $p $hlen end]"
170 regsub -all "\n" $p "\\n" p
171 $w_recentlist insert end
$p link
172 $w_recentlist insert end
"\n"
175 $m_repo add command
\
176 -command [cb _open_recent_path
$path] \
180 $w_recentlist conf
-state disabled
181 $w_recentlist tag
bind link
<1> [cb _open_recent
%x
,%y
]
182 pack $w_body.space
-anchor w
-fill x
183 pack $w_body.recentlabel
-anchor w
-fill x
184 pack $w_recentlist -anchor w
-fill x
186 pack $w_body -fill x
-padx 10 -pady 10
188 ${NS
}::frame $w.buttons
189 set w_next
$w.buttons.next
190 set w_quit
$w.buttons.quit
191 ${NS
}::button $w_quit \
194 pack $w_quit -side right
-padx 5
195 pack $w.buttons
-side bottom
-fill x
-padx 10 -pady 10
198 $m_repo add separator
199 $m_repo add command
\
205 bind $top <Return
> [cb _invoke_next
]
206 bind $top <Visibility
> "
210 bind $top <Visibility> {}
213 tkwait variable @done
217 eval destroy [winfo children
$top]
222 set nx
[winfo reqwidth
$top]
223 set ny
[winfo reqheight
$top]
224 set rx
[expr {([winfo screenwidth
$top] - $nx) / 3}]
225 set ry
[expr {([winfo screenheight
$top] - $ny) / 3}]
226 wm geometry
$top [format {+%d
+%d
} $rx $ry]
229 method _invoke_next
{} {
230 if {[winfo exists
$w_next]} {
231 uplevel #0 [$w_next cget -command]
235 proc _get_recentrepos
{} {
237 foreach p
[get_config gui.recentrepo
] {
238 if {[_is_git
[file join $p .git
]]} {
244 return [lsort $recent]
247 proc _unset_recentrepo
{p
} {
248 regsub -all -- {([()\[\]{}\.^
$+*?
\\])} $p {\\\1} p
249 git config
--global --unset gui.recentrepo
"^$p\$"
253 proc _append_recentrepos
{path
} {
254 set path
[file normalize
$path]
255 set recent
[get_config gui.recentrepo
]
257 if {[lindex $recent end
] eq
$path} {
261 set i
[lsearch $recent $path]
263 _unset_recentrepo
$path
264 set recent
[lreplace $recent $i $i]
268 git config
--global --add gui.recentrepo
$path
271 if {[set maxrecent
[get_config gui.maxrecentrepo
]] eq
{}} {
275 while {[llength $recent] > $maxrecent} {
276 _unset_recentrepo
[lindex $recent 0]
277 set recent
[lrange $recent 1 end
]
281 method _open_recent
{xy
} {
282 set id
[lindex [split [$w_recentlist index
@$xy] .
] 0]
283 set local_path
[lindex $sorted_recent [expr {$id - 1}]]
287 method _open_recent_path
{p
} {
292 method _next
{action
} {
295 if {![winfo exists
$w_next]} {
296 ${NS
}::button $w_next -default active
298 if {[tk windowingsystem
] eq
"win32"} { set pos
-after }
299 pack $w_next -side right
-padx 5 $pos $w_quit
304 method _write_local_path
{args
} {
305 if {$local_path eq
{}} {
306 $w_next conf
-state disabled
308 $w_next conf
-state normal
312 method _git_init
{} {
313 if {[catch {file mkdir
$local_path} err
]} {
314 error_popup
[strcat
\
315 [mc
"Failed to create repository %s:" $local_path] \
320 if {[catch {cd $local_path} err
]} {
321 error_popup
[strcat
\
322 [mc
"Failed to create repository %s:" $local_path] \
327 if {[catch {git init
} err
]} {
328 error_popup
[strcat
\
329 [mc
"Failed to create repository %s:" $local_path] \
334 _append_recentrepos
[pwd]
340 proc _is_git
{path
} {
341 if {[file exists
[file join $path HEAD
]]
342 && [file exists
[file join $path objects
]]
343 && [file exists
[file join $path config
]]} {
347 if {[file exists
[file join $path HEAD
]]
348 && [file exists
[file join $path objects.lnk
]]
349 && [file exists
[file join $path config.lnk
]]} {
356 proc _objdir
{path
} {
357 set objdir
[file join $path .git objects
]
358 if {[file isdirectory
$objdir]} {
362 set objdir
[file join $path objects
]
363 if {[file isdirectory
$objdir]} {
368 set objdir
[file join $path .git objects.lnk
]
369 if {[file isfile
$objdir]} {
370 return [win32_read_lnk
$objdir]
373 set objdir
[file join $path objects.lnk
]
374 if {[file isfile
$objdir]} {
375 return [win32_read_lnk
$objdir]
382 ######################################################################
384 ## Create New Repository
390 -command [cb _do_new2
] \
394 ${NS
}::label $w_body.h
\
395 -font font_uibold
-anchor center
\
396 -text [mc
"Create New Repository"]
397 pack $w_body.h
-side top
-fill x
-pady 10
398 pack $w_body -fill x
-padx 10
400 ${NS
}::frame $w_body.where
401 ${NS
}::label $w_body.where.l
-text [mc
"Directory:"]
402 ${NS
}::entry $w_body.where.t
\
403 -textvariable @local_path
\
405 ${NS
}::button $w_body.where.b
\
406 -text [mc
"Browse"] \
407 -command [cb _new_local_path
]
408 set w_localpath
$w_body.where.t
410 grid $w_body.where.l
$w_body.where.t
$w_body.where.b
-sticky ew
411 pack $w_body.where
-fill x
413 grid columnconfigure
$w_body.where
1 -weight 1
415 trace add
variable @local_path write
[cb _write_local_path
]
416 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
418 focus $w_body.where.t
421 method _new_local_path
{} {
422 if {$local_path ne
{}} {
423 set p
[file dirname
$local_path]
428 set p
[tk_chooseDirectory \
431 -title [mc
"Git Repository"] \
435 set p
[file normalize
$p]
440 $w_localpath icursor end
444 if {![_new_ok
$local_path]} {
447 if {![_git_init
$this]} {
454 if {[file isdirectory
$p]} {
455 if {[_is_git
[file join $p .git
]]} {
456 error_popup
[mc
"Directory %s already exists." $p]
459 } elseif
{[file exists
$p]} {
460 error_popup
[mc
"File %s already exists." $p]
466 ######################################################################
468 ## Clone Existing Repository
470 method _do_clone
{} {
474 -command [cb _do_clone2
] \
478 ${NS
}::label $w_body.h
\
479 -font font_uibold
-anchor center
\
480 -text [mc
"Clone Existing Repository"]
481 pack $w_body.h
-side top
-fill x
-pady 10
482 pack $w_body -fill x
-padx 10
484 set args
$w_body.args
485 ${NS
}::frame $w_body.args
486 pack $args -fill both
488 ${NS
}::label $args.origin_l
-text [mc
"Source Location:"]
489 ${NS
}::entry $args.origin_t
\
490 -textvariable @origin_url
\
492 ${NS
}::button $args.origin_b
\
493 -text [mc
"Browse"] \
494 -command [cb _open_origin
]
495 grid $args.origin_l
$args.origin_t
$args.origin_b
-sticky ew
497 ${NS
}::label $args.where_l
-text [mc
"Target Directory:"]
498 ${NS
}::entry $args.where_t
\
499 -textvariable @local_path
\
501 ${NS
}::button $args.where_b
\
502 -text [mc
"Browse"] \
503 -command [cb _new_local_path
]
504 grid $args.where_l
$args.where_t
$args.where_b
-sticky ew
505 set w_localpath
$args.where_t
507 ${NS
}::label $args.type_l
-text [mc
"Clone Type:"]
508 ${NS
}::frame $args.type_f
510 lappend w_types
[${NS
}::radiobutton $args.type_f.hardlink
\
512 -text [mc
"Standard (Fast, Semi-Redundant, Hardlinks)"] \
513 -variable @clone_type
\
515 lappend w_types
[${NS
}::radiobutton $args.type_f.full
\
517 -text [mc
"Full Copy (Slower, Redundant Backup)"] \
518 -variable @clone_type
\
520 lappend w_types
[${NS
}::radiobutton $args.type_f.shared
\
522 -text [mc
"Shared (Fastest, Not Recommended, No Backup)"] \
523 -variable @clone_type
\
528 grid $args.type_l
$args.type_f
-sticky new
530 grid columnconfigure
$args 1 -weight 1
532 trace add
variable @local_path write
[cb _update_clone
]
533 trace add
variable @origin_url write
[cb _update_clone
]
534 bind $w_body.h
<Destroy
> "
535 [list trace remove variable @local_path write [cb _update_clone]]
536 [list trace remove variable @origin_url write [cb _update_clone]]
542 method _open_origin
{} {
543 if {$origin_url ne
{} && [file isdirectory
$origin_url]} {
549 set p
[tk_chooseDirectory \
552 -title [mc
"Git Repository"] \
556 set p
[file normalize
$p]
557 if {![_is_git
[file join $p .git
]] && ![_is_git
$p]} {
558 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
564 method _update_clone
{args
} {
565 if {$local_path ne
{} && $origin_url ne
{}} {
566 $w_next conf
-state normal
568 $w_next conf
-state disabled
571 if {$origin_url ne
{} &&
572 ( [_is_git
[file join $origin_url .git
]]
573 ||
[_is_git
$origin_url])} {
575 if {[[lindex $w_types 0] cget
-state] eq
{disabled
}} {
576 set clone_type hardlink
588 method _do_clone2
{} {
589 if {[file isdirectory
$origin_url]} {
590 set origin_url
[file normalize
$origin_url]
593 if {$clone_type eq
{hardlink
} && ![file isdirectory
$origin_url]} {
594 error_popup
[mc
"Standard only available for local repository."]
597 if {$clone_type eq
{shared
} && ![file isdirectory
$origin_url]} {
598 error_popup
[mc
"Shared only available for local repository."]
602 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
603 set objdir
[_objdir
$origin_url]
605 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
610 set giturl
$origin_url
611 if {[is_Cygwin
] && [file isdirectory
$giturl]} {
612 set giturl
[exec cygpath
--unix --absolute $giturl]
613 if {$clone_type eq
{shared
}} {
614 set objdir
[exec cygpath
--unix --absolute $objdir]
618 if {[file exists
$local_path]} {
619 error_popup
[mc
"Location %s already exists." $local_path]
623 if {![_git_init
$this]} return
627 git config remote.
$origin_name.url
$giturl
628 git config remote.
$origin_name.fetch
+refs
/heads
/*:refs
/remotes
/$origin_name/*
630 error_popup
[strcat
[mc
"Failed to configure origin"] "\n\n$err"]
634 destroy $w_body $w_next
636 switch -exact -- $clone_type {
638 set o_cons
[status_bar
::two_line $w_body]
639 pack $w_body -fill x
-padx 10 -pady 10
642 [mc
"Counting objects"] \
646 if {[file exists
[file join $objdir info alternates
]]} {
649 file mkdir
[gitdir objects
info]
650 set f_in
[open [file join $objdir info alternates
] r
]
651 set f_cp
[open [gitdir objects
info alternates
] w
]
652 fconfigure $f_in -translation binary -encoding binary
653 fconfigure $f_cp -translation binary -encoding binary
655 while {[gets $f_in line
] >= 0} {
657 puts $f_cp [exec cygpath
--unix --absolute $line]
659 puts $f_cp [file normalize
$line]
667 _clone_failed
$this [mc
"Unable to copy objects/info/alternates: %s" $err]
676 -directory [file join $objdir] ??
]
677 set bcnt
[expr {[llength $buckets] + 2}]
679 $o_cons update $bcur $bcnt
682 file mkdir
[file join .git objects
pack]
683 foreach i
[glob -tails -nocomplain \
684 -directory [file join $objdir pack] *] {
685 lappend tolink
[file join pack $i]
687 $o_cons update [incr bcur
] $bcnt
691 file mkdir
[file join .git objects
$i]
692 foreach j
[glob -tails -nocomplain \
693 -directory [file join $objdir $i] *] {
694 lappend tolink
[file join $i $j]
696 $o_cons update [incr bcur
] $bcnt
703 [mc
"Nothing to clone from %s." $origin_url] \
705 [mc
"The 'master' branch has not been initialized."] \
712 set i
[lindex $tolink 0]
715 [file join .git objects
$i] \
716 [file join $objdir $i]
718 info_popup
[mc
"Hardlinks are unavailable. Falling back to copying."]
719 set i
[_copy_files
$this $objdir $tolink]
721 set i
[_link_files
$this $objdir [lrange $tolink 1 end
]]
728 set o_cons
[console::embed \
730 [mc
"Cloning from %s" $origin_url]]
731 pack $w_body -fill both
-expand 1 -padx 10
733 [list git fetch
--no-tags
-k $origin_name] \
737 set fd
[open [gitdir objects
info alternates
] w
]
738 fconfigure $fd -translation binary
744 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
745 if {![_clone_refs
$this]} return
749 set HEAD
[git rev-parse
--verify HEAD^
0]
751 _clone_failed
$this [mc
"Not a Git repository: %s" [file tail
$origin_url]]
755 _do_clone_checkout
$this $HEAD
759 method _copy_files
{objdir tocopy
} {
761 [mc
"Copying objects"] \
766 incr tot
[file size
[file join $objdir $p]]
770 set f_in
[open [file join $objdir $p] r
]
771 set f_cp
[open [file join .git objects
$p] w
]
772 fconfigure $f_in -translation binary -encoding binary
773 fconfigure $f_cp -translation binary -encoding binary
775 while {![eof $f_in]} {
776 incr cmp
[fcopy $f_in $f_cp -size 16384]
778 [expr {$cmp / 1024}] \
786 _clone_failed
$this [mc
"Unable to copy object: %s" $err]
793 method _link_files
{objdir tolink
} {
794 set total
[llength $tolink]
796 [mc
"Linking objects"] \
798 for {set i
0} {$i < $total} {} {
799 set p
[lindex $tolink $i]
802 [file join .git objects
$p] \
803 [file join $objdir $p]
805 _clone_failed
$this [mc
"Unable to hardlink object: %s" $err]
811 $o_cons update $i $total
818 method _clone_refs
{} {
820 if {[catch {cd $origin_url} err
]} {
821 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
824 set fd_in
[git_read for-each-ref
\
826 {--format=list %(refname
) %(objectname
) %(*objectname
)}]
829 set fd
[open [gitdir packed-refs
] w
]
830 fconfigure $fd -translation binary
831 puts $fd "# pack-refs with: peeled"
832 while {[gets $fd_in line
] >= 0} {
833 set line
[eval $line]
834 set refn
[lindex $line 0]
835 set robj
[lindex $line 1]
836 set tobj
[lindex $line 2]
838 if {[regsub ^refs
/heads
/ $refn \
839 "refs/remotes/$origin_name/" refn
]} {
840 puts $fd "$robj $refn"
841 } elseif
{[string match refs
/tags
/* $refn]} {
842 puts $fd "$robj $refn"
853 method _do_clone_tags
{ok
} {
856 [list git fetch
--tags -k $origin_name] \
860 _clone_failed
$this [mc
"Cannot fetch branches and objects. See console output for details."]
864 method _do_clone_HEAD
{ok
} {
867 [list git fetch
$origin_name HEAD
] \
868 [cb _do_clone_full_end
]
871 _clone_failed
$this [mc
"Cannot fetch tags. See console output for details."]
875 method _do_clone_full_end
{ok
} {
882 if {[file exists
[gitdir FETCH_HEAD
]]} {
883 set fd
[open [gitdir FETCH_HEAD
] r
]
884 while {[gets $fd line
] >= 0} {
885 if {[regexp "^(.{40})\t\t" $line line HEAD
]} {
892 catch {git pack-refs
}
893 _do_clone_checkout
$this $HEAD
895 _clone_failed
$this [mc
"Cannot determine HEAD. See console output for details."]
899 method _clone_failed
{{why
{}}} {
900 if {[catch {file delete
-force $local_path} err
]} {
904 [mc
"Unable to cleanup %s" $local_path] \
910 error_popup
[strcat
[mc
"Clone failed."] "\n" $why]
914 method _do_clone_checkout
{HEAD
} {
917 [mc
"No default branch obtained."] \
919 [mc
"The 'master' branch has not been initialized."] \
925 git update-ref HEAD
$HEAD^
0
928 [mc
"Cannot resolve %s as a commit." $HEAD^
0] \
931 [mc
"The 'master' branch has not been initialized."] \
937 set o_cons
[status_bar
::two_line $w_body]
938 pack $w_body -fill x
-padx 10 -pady 10
940 [mc
"Creating working directory"] \
944 set fd
[git_read
--stderr read-tree
\
951 fconfigure $fd -blocking 0 -translation binary
952 fileevent $fd readable
[cb _readtree_wait
$fd]
955 method _readtree_wait
{fd
} {
957 $o_cons update_meter
$buf
958 append readtree_err
$buf
960 fconfigure $fd -blocking 1
962 fconfigure $fd -blocking 0
966 if {[catch {close $fd}]} {
967 set err
$readtree_err
968 regsub {^fatal
: } $err {} err
969 error_popup
[strcat
\
970 [mc
"Initial file checkout failed."] \
975 # -- Run the post-checkout hook.
977 set fd_ph
[githook_read post-checkout
[string repeat
0 40] \
978 [git rev-parse HEAD
] 1]
982 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
983 fileevent $fd_ph readable
[cb _postcheckout_wait
$fd_ph]
989 method _postcheckout_wait
{fd_ph
} {
992 append pch_error
[read $fd_ph]
993 fconfigure $fd_ph -blocking 1
995 if {[catch {close $fd_ph}]} {
996 hook_failed_popup post-checkout
$pch_error 0
1002 fconfigure $fd_ph -blocking 0
1005 ######################################################################
1007 ## Open Existing Repository
1009 method _do_open
{} {
1013 -command [cb _do_open2
] \
1016 ${NS
}::frame $w_body
1017 ${NS
}::label $w_body.h
\
1018 -font font_uibold
-anchor center
\
1019 -text [mc
"Open Existing Repository"]
1020 pack $w_body.h
-side top
-fill x
-pady 10
1021 pack $w_body -fill x
-padx 10
1023 ${NS
}::frame $w_body.where
1024 ${NS
}::label $w_body.where.l
-text [mc
"Repository:"]
1025 ${NS
}::entry $w_body.where.t
\
1026 -textvariable @local_path
\
1028 ${NS
}::button $w_body.where.b
\
1029 -text [mc
"Browse"] \
1030 -command [cb _open_local_path
]
1032 grid $w_body.where.l
$w_body.where.t
$w_body.where.b
-sticky ew
1033 pack $w_body.where
-fill x
1035 grid columnconfigure
$w_body.where
1 -weight 1
1037 trace add
variable @local_path write
[cb _write_local_path
]
1038 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
1040 focus $w_body.where.t
1043 method _open_local_path
{} {
1044 if {$local_path ne
{}} {
1050 set p
[tk_chooseDirectory \
1053 -title [mc
"Git Repository"] \
1055 if {$p eq
{}} return
1057 set p
[file normalize
$p]
1058 if {![_is_git
[file join $p .git
]]} {
1059 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
1065 method _do_open2
{} {
1066 if {![_is_git
[file join $local_path .git
]]} {
1067 error_popup
[mc
"Not a Git repository: %s" [file tail
$local_path]]
1071 if {[catch {cd $local_path} err
]} {
1072 error_popup
[strcat
\
1073 [mc
"Failed to open repository %s:" $local_path] \
1078 _append_recentrepos
[pwd]