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
] \
87 bind $top <$M1B-n
> [cb _next new
]
88 bind $top <$M1B-N
> [cb _next new
]
91 $opts tag conf link_clone
-foreground blue
-underline 1
92 $opts tag
bind link_clone
<1> [cb _next clone
]
93 $opts insert end
[mc
"Clone Existing Repository"] link_clone
97 -command [cb _next clone
] \
99 -label [mc
"Clone..."]
100 bind $top <$M1B-c
> [cb _next clone
]
101 bind $top <$M1B-C
> [cb _next clone
]
104 $opts tag conf link_open
-foreground blue
-underline 1
105 $opts tag
bind link_open
<1> [cb _next
open]
106 $opts insert end
[mc
"Open Existing Repository"] link_open
107 $opts insert end
"\n"
109 $m_repo add command
\
110 -command [cb _next
open] \
111 -accelerator $M1T-O
\
112 -label [mc
"Open..."]
113 bind $top <$M1B-o
> [cb _next
open]
114 bind $top <$M1B-O
> [cb _next
open]
117 $opts conf
-state disabled
119 set sorted_recent
[_get_recentrepos
]
120 if {[llength $sorted_recent] > 0} {
122 $m_repo add separator
123 $m_repo add command
\
125 -label [mc
"Recent Repositories"]
129 label $w_body.recentlabel
\
131 -text [mc
"Open Recent Repository:"]
132 set w_recentlist
$w_body.recentlist
134 -cursor $::cursor_ptr \
136 -background [$w_body.recentlabel cget
-background] \
140 $w_recentlist tag conf link
\
143 set home
"[file normalize $::env(HOME)][file separator]"
144 set hlen
[string length
$home]
145 foreach p
$sorted_recent {
147 if {[string equal
-length $hlen $home $p]} {
148 set p
"~[file separator][string range $p $hlen end]"
150 regsub -all "\n" $p "\\n" p
151 $w_recentlist insert end
$p link
152 $w_recentlist insert end
"\n"
155 $m_repo add command
\
156 -command [cb _open_recent_path
$path] \
160 $w_recentlist conf
-state disabled
161 $w_recentlist tag
bind link
<1> [cb _open_recent
%x
,%y
]
162 pack $w_body.space
-anchor w
-fill x
163 pack $w_body.recentlabel
-anchor w
-fill x
164 pack $w_recentlist -anchor w
-fill x
166 pack $w_body -fill x
-padx 10 -pady 10
169 set w_next
$w.buttons.next
170 set w_quit
$w.buttons.quit
174 pack $w_quit -side right
-padx 5
175 pack $w.buttons
-side bottom
-fill x
-padx 10 -pady 10
178 $m_repo add separator
179 $m_repo add command
\
185 bind $top <Return
> [cb _invoke_next
]
186 bind $top <Visibility
> "
190 bind $top <Visibility> {}
193 tkwait variable @done
196 eval destroy [winfo children
$top]
201 if {[catch {set h
$::env(HOME
)}]
202 ||
![file isdirectory
$h]} {
209 set nx
[winfo reqwidth
$top]
210 set ny
[winfo reqheight
$top]
211 set rx
[expr {([winfo screenwidth
$top] - $nx) / 3}]
212 set ry
[expr {([winfo screenheight
$top] - $ny) / 3}]
213 wm geometry
$top [format {+%d
+%d
} $rx $ry]
216 method _invoke_next
{} {
217 if {[winfo exists
$w_next]} {
218 uplevel #0 [$w_next cget -command]
222 proc _get_recentrepos
{} {
224 foreach p
[get_config gui.recentrepo
] {
225 if {[_is_git
[file join $p .git
]]} {
229 return [lsort $recent]
232 proc _unset_recentrepo
{p
} {
233 regsub -all -- {([()\[\]{}\.^
$+*?
\\])} $p {\\\1} p
234 git config
--global --unset gui.recentrepo
"^$p\$"
237 proc _append_recentrepos
{path
} {
238 set path
[file normalize
$path]
239 set recent
[get_config gui.recentrepo
]
241 if {[lindex $recent end
] eq
$path} {
245 set i
[lsearch $recent $path]
247 _unset_recentrepo
$path
248 set recent
[lreplace $recent $i $i]
252 git config
--global --add gui.recentrepo
$path
254 while {[llength $recent] > 10} {
255 _unset_recentrepo
[lindex $recent 0]
256 set recent
[lrange $recent 1 end
]
260 method _open_recent
{xy
} {
261 set id
[lindex [split [$w_recentlist index
@$xy] .
] 0]
262 set local_path
[lindex $sorted_recent [expr {$id - 1}]]
266 method _open_recent_path
{p
} {
271 method _next
{action
} {
273 if {![winfo exists
$w_next]} {
274 button $w_next -default active
275 pack $w_next -side right
-padx 5 -before $w_quit
280 method _write_local_path
{args
} {
281 if {$local_path eq
{}} {
282 $w_next conf
-state disabled
284 $w_next conf
-state normal
288 method _git_init
{} {
289 if {[file exists
$local_path]} {
290 error_popup
[mc
"Location %s already exists." $local_path]
294 if {[catch {file mkdir
$local_path} err
]} {
295 error_popup
[strcat
\
296 [mc
"Failed to create repository %s:" $local_path] \
301 if {[catch {cd $local_path} err
]} {
302 error_popup
[strcat
\
303 [mc
"Failed to create repository %s:" $local_path] \
308 if {[catch {git init
} err
]} {
309 error_popup
[strcat
\
310 [mc
"Failed to create repository %s:" $local_path] \
315 _append_recentrepos
[pwd]
321 proc _is_git
{path
} {
322 if {[file exists
[file join $path HEAD
]]
323 && [file exists
[file join $path objects
]]
324 && [file exists
[file join $path config
]]} {
328 if {[file exists
[file join $path HEAD
]]
329 && [file exists
[file join $path objects.lnk
]]
330 && [file exists
[file join $path config.lnk
]]} {
337 proc _objdir
{path
} {
338 set objdir
[file join $path .git objects
]
339 if {[file isdirectory
$objdir]} {
343 set objdir
[file join $path objects
]
344 if {[file isdirectory
$objdir]} {
349 set objdir
[file join $path .git objects.lnk
]
350 if {[file isfile
$objdir]} {
351 return [win32_read_lnk
$objdir]
354 set objdir
[file join $path objects.lnk
]
355 if {[file isfile
$objdir]} {
356 return [win32_read_lnk
$objdir]
363 ######################################################################
365 ## Create New Repository
370 -command [cb _do_new2
] \
376 -text [mc
"Create New Repository"]
377 pack $w_body.h
-side top
-fill x
-pady 10
378 pack $w_body -fill x
-padx 10
381 label $w_body.where.l
-text [mc
"Directory:"]
382 entry $w_body.where.t
\
383 -textvariable @local_path
\
386 button $w_body.where.b
\
387 -text [mc
"Browse"] \
388 -command [cb _new_local_path
]
390 pack $w_body.where.b
-side right
391 pack $w_body.where.l
-side left
392 pack $w_body.where.t
-fill x
393 pack $w_body.where
-fill x
395 trace add
variable @local_path write
[cb _write_local_path
]
396 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
398 focus $w_body.where.t
401 method _new_local_path
{} {
402 if {$local_path ne
{}} {
403 set p
[file dirname
$local_path]
408 set p
[tk_chooseDirectory \
411 -title [mc
"Git Repository"] \
415 set p
[file normalize
$p]
416 if {[file isdirectory
$p]} {
426 error_popup
[mc
"Directory %s already exists." $p]
431 if {[catch {file delete
$p} err
]} {
432 error_popup
[strcat
\
433 [mc
"Directory %s already exists." $p] \
437 } elseif
{[file exists
$p]} {
438 error_popup
[mc
"File %s already exists." $p]
445 if {![_git_init
$this]} {
451 ######################################################################
453 ## Clone Existing Repository
455 method _do_clone
{} {
458 -command [cb _do_clone2
] \
464 -text [mc
"Clone Existing Repository"]
465 pack $w_body.h
-side top
-fill x
-pady 10
466 pack $w_body -fill x
-padx 10
468 set args
$w_body.args
470 pack $args -fill both
472 label $args.origin_l
-text [mc
"URL:"]
473 entry $args.origin_t
\
474 -textvariable @origin_url
\
477 button $args.origin_b
\
478 -text [mc
"Browse"] \
479 -command [cb _open_origin
]
480 grid $args.origin_l
$args.origin_t
$args.origin_b
-sticky ew
482 label $args.where_l
-text [mc
"Directory:"]
483 entry $args.where_t
\
484 -textvariable @local_path
\
487 button $args.where_b
\
488 -text [mc
"Browse"] \
489 -command [cb _new_local_path
]
490 grid $args.where_l
$args.where_t
$args.where_b
-sticky ew
492 label $args.type_l
-text [mc
"Clone Type:"]
495 lappend w_types
[radiobutton $args.type_f.hardlink
\
498 -text [mc
"Standard (Fast, Semi-Redundant, Hardlinks)"] \
499 -variable @clone_type
\
501 lappend w_types
[radiobutton $args.type_f.full
\
504 -text [mc
"Full Copy (Slower, Redundant Backup)"] \
505 -variable @clone_type
\
507 lappend w_types
[radiobutton $args.type_f.shared
\
510 -text [mc
"Shared (Fastest, Not Recommended, No Backup)"] \
511 -variable @clone_type
\
516 grid $args.type_l
$args.type_f
-sticky new
518 grid columnconfigure
$args 1 -weight 1
520 trace add
variable @local_path write
[cb _update_clone
]
521 trace add
variable @origin_url write
[cb _update_clone
]
522 bind $w_body.h
<Destroy
> "
523 [list trace remove variable @local_path write [cb _update_clone]]
524 [list trace remove variable @origin_url write [cb _update_clone]]
530 method _open_origin
{} {
531 if {$origin_url ne
{} && [file isdirectory
$origin_url]} {
537 set p
[tk_chooseDirectory \
540 -title [mc
"Git Repository"] \
544 set p
[file normalize
$p]
545 if {![_is_git
[file join $p .git
]] && ![_is_git
$p]} {
546 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
552 method _update_clone
{args
} {
553 if {$local_path ne
{} && $origin_url ne
{}} {
554 $w_next conf
-state normal
556 $w_next conf
-state disabled
559 if {$origin_url ne
{} &&
560 ( [_is_git
[file join $origin_url .git
]]
561 ||
[_is_git
$origin_url])} {
563 if {[[lindex $w_types 0] cget
-state] eq
{disabled
}} {
564 set clone_type hardlink
576 method _do_clone2
{} {
577 if {[file isdirectory
$origin_url]} {
578 set origin_url
[file normalize
$origin_url]
581 if {$clone_type eq
{hardlink
} && ![file isdirectory
$origin_url]} {
582 error_popup
[mc
"Standard only available for local repository."]
585 if {$clone_type eq
{shared
} && ![file isdirectory
$origin_url]} {
586 error_popup
[mc
"Shared only available for local repository."]
590 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
591 set objdir
[_objdir
$origin_url]
593 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
598 set giturl
$origin_url
599 if {[is_Cygwin
] && [file isdirectory
$giturl]} {
600 set giturl
[exec cygpath
--unix --absolute $giturl]
601 if {$clone_type eq
{shared
}} {
602 set objdir
[exec cygpath
--unix --absolute $objdir]
606 if {![_git_init
$this]} return
610 git config remote.
$origin_name.url
$giturl
611 git config remote.
$origin_name.fetch
+refs
/heads
/*:refs
/remotes
/$origin_name/*
613 error_popup
[strcat
[mc
"Failed to configure origin"] "\n\n$err"]
617 destroy $w_body $w_next
619 switch -exact -- $clone_type {
621 set o_cons
[status_bar
::two_line $w_body]
622 pack $w_body -fill x
-padx 10 -pady 10
625 [mc
"Counting objects"] \
629 if {[file exists
[file join $objdir info alternates
]]} {
632 file mkdir
[gitdir objects
info]
633 set f_in
[open [file join $objdir info alternates
] r
]
634 set f_cp
[open [gitdir objects
info alternates
] w
]
635 fconfigure $f_in -translation binary -encoding binary
636 fconfigure $f_cp -translation binary -encoding binary
638 while {[gets $f_in line
] >= 0} {
640 puts $f_cp [exec cygpath
--unix --absolute $line]
642 puts $f_cp [file normalize
$line]
650 _clone_failed
$this [mc
"Unable to copy objects/info/alternates: %s" $err]
659 -directory [file join $objdir] ??
]
660 set bcnt
[expr {[llength $buckets] + 2}]
662 $o_cons update $bcur $bcnt
665 file mkdir
[file join .git objects
pack]
666 foreach i
[glob -tails -nocomplain \
667 -directory [file join $objdir pack] *] {
668 lappend tolink
[file join pack $i]
670 $o_cons update [incr bcur
] $bcnt
674 file mkdir
[file join .git objects
$i]
675 foreach j
[glob -tails -nocomplain \
676 -directory [file join $objdir $i] *] {
677 lappend tolink
[file join $i $j]
679 $o_cons update [incr bcur
] $bcnt
686 [mc
"Nothing to clone from %s." $origin_url] \
688 [mc
"The 'master' branch has not been initialized."] \
695 set i
[lindex $tolink 0]
698 [file join .git objects
$i] \
699 [file join $objdir $i]
701 info_popup
[mc
"Hardlinks are unavailable. Falling back to copying."]
702 set i
[_copy_files
$this $objdir $tolink]
704 set i
[_link_files
$this $objdir [lrange $tolink 1 end
]]
711 set o_cons
[console::embed \
713 [mc
"Cloning from %s" $origin_url]]
714 pack $w_body -fill both
-expand 1 -padx 10
716 [list git fetch
--no-tags
-k $origin_name] \
720 set fd
[open [gitdir objects
info alternates
] w
]
721 fconfigure $fd -translation binary
727 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
728 if {![_clone_refs
$this]} return
732 set HEAD
[git rev-parse
--verify HEAD^
0]
734 _clone_failed
$this [mc
"Not a Git repository: %s" [file tail
$origin_url]]
738 _do_clone_checkout
$this $HEAD
742 method _copy_files
{objdir tocopy
} {
744 [mc
"Copying objects"] \
749 incr tot
[file size
[file join $objdir $p]]
753 set f_in
[open [file join $objdir $p] r
]
754 set f_cp
[open [file join .git objects
$p] w
]
755 fconfigure $f_in -translation binary -encoding binary
756 fconfigure $f_cp -translation binary -encoding binary
758 while {![eof $f_in]} {
759 incr cmp
[fcopy $f_in $f_cp -size 16384]
761 [expr {$cmp / 1024}] \
769 _clone_failed
$this [mc
"Unable to copy object: %s" $err]
776 method _link_files
{objdir tolink
} {
777 set total
[llength $tolink]
779 [mc
"Linking objects"] \
781 for {set i
0} {$i < $total} {} {
782 set p
[lindex $tolink $i]
785 [file join .git objects
$p] \
786 [file join $objdir $p]
788 _clone_failed
$this [mc
"Unable to hardlink object: %s" $err]
794 $o_cons update $i $total
801 method _clone_refs
{} {
803 if {[catch {cd $origin_url} err
]} {
804 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
807 set fd_in
[git_read for-each-ref
\
809 {--format=list %(refname
) %(objectname
) %(*objectname
)}]
812 set fd
[open [gitdir packed-refs
] w
]
813 fconfigure $fd -translation binary
814 puts $fd "# pack-refs with: peeled"
815 while {[gets $fd_in line
] >= 0} {
816 set line
[eval $line]
817 set refn
[lindex $line 0]
818 set robj
[lindex $line 1]
819 set tobj
[lindex $line 2]
821 if {[regsub ^refs
/heads
/ $refn \
822 "refs/remotes/$origin_name/" refn
]} {
823 puts $fd "$robj $refn"
824 } elseif
{[string match refs
/tags
/* $refn]} {
825 puts $fd "$robj $refn"
836 method _do_clone_tags
{ok
} {
839 [list git fetch
--tags -k $origin_name] \
843 _clone_failed
$this [mc
"Cannot fetch branches and objects. See console output for details."]
847 method _do_clone_HEAD
{ok
} {
850 [list git fetch
$origin_name HEAD
] \
851 [cb _do_clone_full_end
]
854 _clone_failed
$this [mc
"Cannot fetch tags. See console output for details."]
858 method _do_clone_full_end
{ok
} {
865 if {[file exists
[gitdir FETCH_HEAD
]]} {
866 set fd
[open [gitdir FETCH_HEAD
] r
]
867 while {[gets $fd line
] >= 0} {
868 if {[regexp "^(.{40})\t\t" $line line HEAD
]} {
875 catch {git pack-refs
}
876 _do_clone_checkout
$this $HEAD
878 _clone_failed
$this [mc
"Cannot determine HEAD. See console output for details."]
882 method _clone_failed
{{why
{}}} {
883 if {[catch {file delete
-force $local_path} err
]} {
887 [mc
"Unable to cleanup %s" $local_path] \
893 error_popup
[strcat
[mc
"Clone failed."] "\n" $why]
897 method _do_clone_checkout
{HEAD
} {
900 [mc
"No default branch obtained."] \
902 [mc
"The 'master' branch has not been initialized."] \
908 git update-ref HEAD
$HEAD^
0
911 [mc
"Cannot resolve %s as a commit." $HEAD^
0] \
914 [mc
"The 'master' branch has not been initialized."] \
920 set o_cons
[status_bar
::two_line $w_body]
921 pack $w_body -fill x
-padx 10 -pady 10
923 [mc
"Creating working directory"] \
927 set fd
[git_read
--stderr read-tree
\
934 fconfigure $fd -blocking 0 -translation binary
935 fileevent $fd readable
[cb _readtree_wait
$fd]
938 method _readtree_wait
{fd
} {
940 $o_cons update_meter
$buf
941 append readtree_err
$buf
943 fconfigure $fd -blocking 1
945 fconfigure $fd -blocking 0
949 if {[catch {close $fd}]} {
950 set err
$readtree_err
951 regsub {^fatal
: } $err {} err
952 error_popup
[strcat
\
953 [mc
"Initial file checkout failed."] \
961 ######################################################################
963 ## Open Existing Repository
968 -command [cb _do_open2
] \
974 -text [mc
"Open Existing Repository"]
975 pack $w_body.h
-side top
-fill x
-pady 10
976 pack $w_body -fill x
-padx 10
979 label $w_body.where.l
-text [mc
"Repository:"]
980 entry $w_body.where.t
\
981 -textvariable @local_path
\
984 button $w_body.where.b
\
985 -text [mc
"Browse"] \
986 -command [cb _open_local_path
]
988 pack $w_body.where.b
-side right
989 pack $w_body.where.l
-side left
990 pack $w_body.where.t
-fill x
991 pack $w_body.where
-fill x
993 trace add
variable @local_path write
[cb _write_local_path
]
994 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
996 focus $w_body.where.t
999 method _open_local_path
{} {
1000 if {$local_path ne
{}} {
1006 set p
[tk_chooseDirectory \
1009 -title [mc
"Git Repository"] \
1011 if {$p eq
{}} return
1013 set p
[file normalize
$p]
1014 if {![_is_git
[file join $p .git
]]} {
1015 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
1021 method _do_open2
{} {
1022 if {![_is_git
[file join $local_path .git
]]} {
1023 error_popup
[mc
"Not a Git repository: %s" [file tail
$local_path]]
1027 if {[catch {cd $local_path} err
]} {
1028 error_popup
[strcat
\
1029 [mc
"Failed to open repository %s:" $local_path] \
1034 _append_recentrepos
[pwd]