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)
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 [mc Apple
] -menu .mbar.apple
43 $w.mbar.apple add command
\
44 -label [mc
"About %s" [appname
]] \
47 $w.mbar add cascade
-label [mc Help
] -menu $w.mbar.help
49 $w.mbar.help add command
\
50 -label [mc
"About %s" [appname
]] \
54 wm protocol
$top WM_DELETE_WINDOW
exit
55 bind $top <$M1B-q
> exit
56 bind $top <$M1B-Q
> exit
57 bind $top <Key-Escape
> exit
59 wm geometry
$top "+[winfo rootx .]+[winfo rooty .]"
60 bind $top <Key-Escape
> [list destroy $top]
64 pack [git_logo
$w.git_logo
] -side left
-fill y
-padx 10 -pady 10
67 set opts
$w_body.
options
70 -cursor $::cursor_ptr \
72 -background [$w_body cget
-background] \
77 pack $opts -anchor w
-fill x
79 $opts tag conf link_new
-foreground blue
-underline 1
80 $opts tag
bind link_new
<1> [cb _next new
]
81 $opts insert end
[mc
"Create New Repository"] link_new
85 -command [cb _next new
] \
88 bind $top <$M1B-n
> [cb _next new
]
89 bind $top <$M1B-N
> [cb _next new
]
92 $opts tag conf link_clone
-foreground blue
-underline 1
93 $opts tag
bind link_clone
<1> [cb _next clone
]
94 $opts insert end
[mc
"Clone Existing Repository"] link_clone
98 -command [cb _next clone
] \
100 -label [mc
"Clone..."]
101 bind $top <$M1B-c
> [cb _next clone
]
102 bind $top <$M1B-C
> [cb _next clone
]
105 $opts tag conf link_open
-foreground blue
-underline 1
106 $opts tag
bind link_open
<1> [cb _next
open]
107 $opts insert end
[mc
"Open Existing Repository"] link_open
108 $opts insert end
"\n"
110 $m_repo add command
\
111 -command [cb _next
open] \
112 -accelerator $M1T-O
\
113 -label [mc
"Open..."]
114 bind $top <$M1B-o
> [cb _next
open]
115 bind $top <$M1B-O
> [cb _next
open]
118 $opts conf
-state disabled
120 set sorted_recent
[_get_recentrepos
]
121 if {[llength $sorted_recent] > 0} {
123 $m_repo add separator
124 $m_repo add command
\
126 -label [mc
"Recent Repositories"]
130 label $w_body.recentlabel
\
132 -text [mc
"Open Recent Repository:"]
133 set w_recentlist
$w_body.recentlist
135 -cursor $::cursor_ptr \
137 -background [$w_body.recentlabel cget
-background] \
141 $w_recentlist tag conf link
\
144 set home
$::env(HOME
)
146 set home
[exec cygpath
--windows --absolute $home]
148 set home
"[file normalize $home]/"
149 set hlen
[string length
$home]
150 foreach p
$sorted_recent {
152 if {[string equal
-length $hlen $home $p]} {
153 set p
"~/[string range $p $hlen end]"
155 regsub -all "\n" $p "\\n" p
156 $w_recentlist insert end
$p link
157 $w_recentlist insert end
"\n"
160 $m_repo add command
\
161 -command [cb _open_recent_path
$path] \
165 $w_recentlist conf
-state disabled
166 $w_recentlist tag
bind link
<1> [cb _open_recent
%x
,%y
]
167 pack $w_body.space
-anchor w
-fill x
168 pack $w_body.recentlabel
-anchor w
-fill x
169 pack $w_recentlist -anchor w
-fill x
171 pack $w_body -fill x
-padx 10 -pady 10
174 set w_next
$w.buttons.next
175 set w_quit
$w.buttons.quit
179 pack $w_quit -side right
-padx 5
180 pack $w.buttons
-side bottom
-fill x
-padx 10 -pady 10
183 $m_repo add separator
184 $m_repo add command
\
190 bind $top <Return
> [cb _invoke_next
]
191 bind $top <Visibility
> "
195 bind $top <Visibility> {}
198 tkwait variable @done
201 eval destroy [winfo children
$top]
206 if {[catch {set h
$::env(HOME
)}]
207 ||
![file isdirectory
$h]} {
214 set nx
[winfo reqwidth
$top]
215 set ny
[winfo reqheight
$top]
216 set rx
[expr {([winfo screenwidth
$top] - $nx) / 3}]
217 set ry
[expr {([winfo screenheight
$top] - $ny) / 3}]
218 wm geometry
$top [format {+%d
+%d
} $rx $ry]
221 method _invoke_next
{} {
222 if {[winfo exists
$w_next]} {
223 uplevel #0 [$w_next cget -command]
227 proc _get_recentrepos
{} {
229 foreach p
[get_config gui.recentrepo
] {
230 if {[_is_git
[file join $p .git
]]} {
234 return [lsort $recent]
237 proc _unset_recentrepo
{p
} {
238 regsub -all -- {([()\[\]{}\.^
$+*?
\\])} $p {\\\1} p
239 git config
--global --unset gui.recentrepo
"^$p\$"
242 proc _append_recentrepos
{path
} {
243 set path
[file normalize
$path]
244 set recent
[get_config gui.recentrepo
]
246 if {[lindex $recent end
] eq
$path} {
250 set i
[lsearch $recent $path]
252 _unset_recentrepo
$path
253 set recent
[lreplace $recent $i $i]
257 git config
--global --add gui.recentrepo
$path
259 while {[llength $recent] > 10} {
260 _unset_recentrepo
[lindex $recent 0]
261 set recent
[lrange $recent 1 end
]
265 method _open_recent
{xy
} {
266 set id
[lindex [split [$w_recentlist index
@$xy] .
] 0]
267 set local_path
[lindex $sorted_recent [expr {$id - 1}]]
271 method _open_recent_path
{p
} {
276 method _next
{action
} {
278 if {![winfo exists
$w_next]} {
279 button $w_next -default active
280 pack $w_next -side right
-padx 5 -before $w_quit
285 method _write_local_path
{args
} {
286 if {$local_path eq
{}} {
287 $w_next conf
-state disabled
289 $w_next conf
-state normal
293 method _git_init
{} {
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
]
389 set w_localpath
$w_body.where.t
391 pack $w_body.where.b
-side right
392 pack $w_body.where.l
-side left
393 pack $w_body.where.t
-fill x
394 pack $w_body.where
-fill x
396 trace add
variable @local_path write
[cb _write_local_path
]
397 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
399 focus $w_body.where.t
402 method _new_local_path
{} {
403 if {$local_path ne
{}} {
404 set p
[file dirname
$local_path]
409 set p
[tk_chooseDirectory \
412 -title [mc
"Git Repository"] \
416 set p
[file normalize
$p]
421 $w_localpath icursor end
425 if {![_new_ok
$local_path]} {
428 if {![_git_init
$this]} {
435 if {[file isdirectory
$p]} {
436 if {[_is_git
[file join $p .git
]]} {
437 error_popup
[mc
"Directory %s already exists." $p]
440 } elseif
{[file exists
$p]} {
441 error_popup
[mc
"File %s already exists." $p]
447 ######################################################################
449 ## Clone Existing Repository
451 method _do_clone
{} {
454 -command [cb _do_clone2
] \
460 -text [mc
"Clone Existing Repository"]
461 pack $w_body.h
-side top
-fill x
-pady 10
462 pack $w_body -fill x
-padx 10
464 set args
$w_body.args
466 pack $args -fill both
468 label $args.origin_l
-text [mc
"URL:"]
469 entry $args.origin_t
\
470 -textvariable @origin_url
\
473 button $args.origin_b
\
474 -text [mc
"Browse"] \
475 -command [cb _open_origin
]
476 grid $args.origin_l
$args.origin_t
$args.origin_b
-sticky ew
478 label $args.where_l
-text [mc
"Directory:"]
479 entry $args.where_t
\
480 -textvariable @local_path
\
483 button $args.where_b
\
484 -text [mc
"Browse"] \
485 -command [cb _new_local_path
]
486 grid $args.where_l
$args.where_t
$args.where_b
-sticky ew
487 set w_localpath
$args.where_t
489 label $args.type_l
-text [mc
"Clone Type:"]
492 lappend w_types
[radiobutton $args.type_f.hardlink
\
495 -text [mc
"Standard (Fast, Semi-Redundant, Hardlinks)"] \
496 -variable @clone_type
\
498 lappend w_types
[radiobutton $args.type_f.full
\
501 -text [mc
"Full Copy (Slower, Redundant Backup)"] \
502 -variable @clone_type
\
504 lappend w_types
[radiobutton $args.type_f.shared
\
507 -text [mc
"Shared (Fastest, Not Recommended, No Backup)"] \
508 -variable @clone_type
\
513 grid $args.type_l
$args.type_f
-sticky new
515 grid columnconfigure
$args 1 -weight 1
517 trace add
variable @local_path write
[cb _update_clone
]
518 trace add
variable @origin_url write
[cb _update_clone
]
519 bind $w_body.h
<Destroy
> "
520 [list trace remove variable @local_path write [cb _update_clone]]
521 [list trace remove variable @origin_url write [cb _update_clone]]
527 method _open_origin
{} {
528 if {$origin_url ne
{} && [file isdirectory
$origin_url]} {
534 set p
[tk_chooseDirectory \
537 -title [mc
"Git Repository"] \
541 set p
[file normalize
$p]
542 if {![_is_git
[file join $p .git
]] && ![_is_git
$p]} {
543 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
549 method _update_clone
{args
} {
550 if {$local_path ne
{} && $origin_url ne
{}} {
551 $w_next conf
-state normal
553 $w_next conf
-state disabled
556 if {$origin_url ne
{} &&
557 ( [_is_git
[file join $origin_url .git
]]
558 ||
[_is_git
$origin_url])} {
560 if {[[lindex $w_types 0] cget
-state] eq
{disabled
}} {
561 set clone_type hardlink
573 method _do_clone2
{} {
574 if {[file isdirectory
$origin_url]} {
575 set origin_url
[file normalize
$origin_url]
578 if {$clone_type eq
{hardlink
} && ![file isdirectory
$origin_url]} {
579 error_popup
[mc
"Standard only available for local repository."]
582 if {$clone_type eq
{shared
} && ![file isdirectory
$origin_url]} {
583 error_popup
[mc
"Shared only available for local repository."]
587 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
588 set objdir
[_objdir
$origin_url]
590 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
595 set giturl
$origin_url
596 if {[is_Cygwin
] && [file isdirectory
$giturl]} {
597 set giturl
[exec cygpath
--unix --absolute $giturl]
598 if {$clone_type eq
{shared
}} {
599 set objdir
[exec cygpath
--unix --absolute $objdir]
603 if {[file exists
$local_path]} {
604 error_popup
[mc
"Location %s already exists." $local_path]
608 if {![_git_init
$this]} return
612 git config remote.
$origin_name.url
$giturl
613 git config remote.
$origin_name.fetch
+refs
/heads
/*:refs
/remotes
/$origin_name/*
615 error_popup
[strcat
[mc
"Failed to configure origin"] "\n\n$err"]
619 destroy $w_body $w_next
621 switch -exact -- $clone_type {
623 set o_cons
[status_bar
::two_line $w_body]
624 pack $w_body -fill x
-padx 10 -pady 10
627 [mc
"Counting objects"] \
631 if {[file exists
[file join $objdir info alternates
]]} {
634 file mkdir
[gitdir objects
info]
635 set f_in
[open [file join $objdir info alternates
] r
]
636 set f_cp
[open [gitdir objects
info alternates
] w
]
637 fconfigure $f_in -translation binary -encoding binary
638 fconfigure $f_cp -translation binary -encoding binary
640 while {[gets $f_in line
] >= 0} {
642 puts $f_cp [exec cygpath
--unix --absolute $line]
644 puts $f_cp [file normalize
$line]
652 _clone_failed
$this [mc
"Unable to copy objects/info/alternates: %s" $err]
661 -directory [file join $objdir] ??
]
662 set bcnt
[expr {[llength $buckets] + 2}]
664 $o_cons update $bcur $bcnt
667 file mkdir
[file join .git objects
pack]
668 foreach i
[glob -tails -nocomplain \
669 -directory [file join $objdir pack] *] {
670 lappend tolink
[file join pack $i]
672 $o_cons update [incr bcur
] $bcnt
676 file mkdir
[file join .git objects
$i]
677 foreach j
[glob -tails -nocomplain \
678 -directory [file join $objdir $i] *] {
679 lappend tolink
[file join $i $j]
681 $o_cons update [incr bcur
] $bcnt
688 [mc
"Nothing to clone from %s." $origin_url] \
690 [mc
"The 'master' branch has not been initialized."] \
697 set i
[lindex $tolink 0]
700 [file join .git objects
$i] \
701 [file join $objdir $i]
703 info_popup
[mc
"Hardlinks are unavailable. Falling back to copying."]
704 set i
[_copy_files
$this $objdir $tolink]
706 set i
[_link_files
$this $objdir [lrange $tolink 1 end
]]
713 set o_cons
[console::embed \
715 [mc
"Cloning from %s" $origin_url]]
716 pack $w_body -fill both
-expand 1 -padx 10
718 [list git fetch
--no-tags
-k $origin_name] \
722 set fd
[open [gitdir objects
info alternates
] w
]
723 fconfigure $fd -translation binary
729 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
730 if {![_clone_refs
$this]} return
734 set HEAD
[git rev-parse
--verify HEAD^
0]
736 _clone_failed
$this [mc
"Not a Git repository: %s" [file tail
$origin_url]]
740 _do_clone_checkout
$this $HEAD
744 method _copy_files
{objdir tocopy
} {
746 [mc
"Copying objects"] \
751 incr tot
[file size
[file join $objdir $p]]
755 set f_in
[open [file join $objdir $p] r
]
756 set f_cp
[open [file join .git objects
$p] w
]
757 fconfigure $f_in -translation binary -encoding binary
758 fconfigure $f_cp -translation binary -encoding binary
760 while {![eof $f_in]} {
761 incr cmp
[fcopy $f_in $f_cp -size 16384]
763 [expr {$cmp / 1024}] \
771 _clone_failed
$this [mc
"Unable to copy object: %s" $err]
778 method _link_files
{objdir tolink
} {
779 set total
[llength $tolink]
781 [mc
"Linking objects"] \
783 for {set i
0} {$i < $total} {} {
784 set p
[lindex $tolink $i]
787 [file join .git objects
$p] \
788 [file join $objdir $p]
790 _clone_failed
$this [mc
"Unable to hardlink object: %s" $err]
796 $o_cons update $i $total
803 method _clone_refs
{} {
805 if {[catch {cd $origin_url} err
]} {
806 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
809 set fd_in
[git_read for-each-ref
\
811 {--format=list %(refname
) %(objectname
) %(*objectname
)}]
814 set fd
[open [gitdir packed-refs
] w
]
815 fconfigure $fd -translation binary
816 puts $fd "# pack-refs with: peeled"
817 while {[gets $fd_in line
] >= 0} {
818 set line
[eval $line]
819 set refn
[lindex $line 0]
820 set robj
[lindex $line 1]
821 set tobj
[lindex $line 2]
823 if {[regsub ^refs
/heads
/ $refn \
824 "refs/remotes/$origin_name/" refn
]} {
825 puts $fd "$robj $refn"
826 } elseif
{[string match refs
/tags
/* $refn]} {
827 puts $fd "$robj $refn"
838 method _do_clone_tags
{ok
} {
841 [list git fetch
--tags -k $origin_name] \
845 _clone_failed
$this [mc
"Cannot fetch branches and objects. See console output for details."]
849 method _do_clone_HEAD
{ok
} {
852 [list git fetch
$origin_name HEAD
] \
853 [cb _do_clone_full_end
]
856 _clone_failed
$this [mc
"Cannot fetch tags. See console output for details."]
860 method _do_clone_full_end
{ok
} {
867 if {[file exists
[gitdir FETCH_HEAD
]]} {
868 set fd
[open [gitdir FETCH_HEAD
] r
]
869 while {[gets $fd line
] >= 0} {
870 if {[regexp "^(.{40})\t\t" $line line HEAD
]} {
877 catch {git pack-refs
}
878 _do_clone_checkout
$this $HEAD
880 _clone_failed
$this [mc
"Cannot determine HEAD. See console output for details."]
884 method _clone_failed
{{why
{}}} {
885 if {[catch {file delete
-force $local_path} err
]} {
889 [mc
"Unable to cleanup %s" $local_path] \
895 error_popup
[strcat
[mc
"Clone failed."] "\n" $why]
899 method _do_clone_checkout
{HEAD
} {
902 [mc
"No default branch obtained."] \
904 [mc
"The 'master' branch has not been initialized."] \
910 git update-ref HEAD
$HEAD^
0
913 [mc
"Cannot resolve %s as a commit." $HEAD^
0] \
916 [mc
"The 'master' branch has not been initialized."] \
922 set o_cons
[status_bar
::two_line $w_body]
923 pack $w_body -fill x
-padx 10 -pady 10
925 [mc
"Creating working directory"] \
929 set fd
[git_read
--stderr read-tree
\
936 fconfigure $fd -blocking 0 -translation binary
937 fileevent $fd readable
[cb _readtree_wait
$fd]
940 method _readtree_wait
{fd
} {
942 $o_cons update_meter
$buf
943 append readtree_err
$buf
945 fconfigure $fd -blocking 1
947 fconfigure $fd -blocking 0
951 if {[catch {close $fd}]} {
952 set err
$readtree_err
953 regsub {^fatal
: } $err {} err
954 error_popup
[strcat
\
955 [mc
"Initial file checkout failed."] \
963 ######################################################################
965 ## Open Existing Repository
970 -command [cb _do_open2
] \
976 -text [mc
"Open Existing Repository"]
977 pack $w_body.h
-side top
-fill x
-pady 10
978 pack $w_body -fill x
-padx 10
981 label $w_body.where.l
-text [mc
"Repository:"]
982 entry $w_body.where.t
\
983 -textvariable @local_path
\
986 button $w_body.where.b
\
987 -text [mc
"Browse"] \
988 -command [cb _open_local_path
]
990 pack $w_body.where.b
-side right
991 pack $w_body.where.l
-side left
992 pack $w_body.where.t
-fill x
993 pack $w_body.where
-fill x
995 trace add
variable @local_path write
[cb _write_local_path
]
996 bind $w_body.h
<Destroy
> [list trace remove
variable @local_path write
[cb _write_local_path
]]
998 focus $w_body.where.t
1001 method _open_local_path
{} {
1002 if {$local_path ne
{}} {
1008 set p
[tk_chooseDirectory \
1011 -title [mc
"Git Repository"] \
1013 if {$p eq
{}} return
1015 set p
[file normalize
$p]
1016 if {![_is_git
[file join $p .git
]]} {
1017 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
1023 method _do_open2
{} {
1024 if {![_is_git
[file join $local_path .git
]]} {
1025 error_popup
[mc
"Not a Git repository: %s" [file tail
$local_path]]
1029 if {[catch {cd $local_path} err
]} {
1030 error_popup
[strcat
\
1031 [mc
"Failed to open repository %s:" $local_path] \
1036 _append_recentrepos
[pwd]