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 o_cons
; # Console object (if active)
11 field w_types
; # List of type buttons in clone
12 field w_recentlist
; # Listbox containing recent repositories
14 field action new
; # What action are we going to perform?
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
34 -label [mc Repository
] \
35 -menu $w.mbar.repository
36 menu $w.mbar.repository
37 $w.mbar.repository add command
\
43 $w.mbar add cascade
-label [mc Apple
] -menu .mbar.apple
45 $w.mbar.apple add command
\
46 -label [mc
"About %s" [appname
]] \
49 $w.mbar add cascade
-label [mc Help
] -menu $w.mbar.help
51 $w.mbar.help add command
\
52 -label [mc
"About %s" [appname
]] \
56 wm protocol
$top WM_DELETE_WINDOW
exit
57 bind $top <$M1B-q
> exit
58 bind $top <$M1B-Q
> exit
59 bind $top <Key-Escape
> exit
61 wm geometry
$top "+[winfo rootx .]+[winfo rooty .]"
62 bind $top <Key-Escape
> [list destroy $top]
65 pack [git_logo
$w.git_logo
] -side left
-fill y
-padx 10 -pady 10
69 radiobutton $w_body.new
\
71 -text [mc
"Create New Repository"] \
74 radiobutton $w_body.clone
\
76 -text [mc
"Clone Existing Repository"] \
79 radiobutton $w_body.
open \
81 -text [mc
"Open Existing Repository"] \
84 pack $w_body.new
-anchor w
-fill x
85 pack $w_body.clone
-anchor w
-fill x
86 pack $w_body.
open -anchor w
-fill x
88 set sorted_recent
[_get_recentrepos
]
89 if {[llength $sorted_recent] > 0} {
91 label $w_body.recentlabel
\
93 -text [mc
"Open Recent Repository:"]
94 set w_recentlist
$w_body.recentlist
96 -cursor $::cursor_ptr \
98 -background [$w_body.recentlabel cget
-background] \
102 $w_recentlist tag conf link
\
105 set home
"[file normalize $::env(HOME)][file separator]"
106 set hlen
[string length
$home]
107 foreach p
$sorted_recent {
108 if {[string equal
-length $hlen $home $p]} {
109 set p
"~[file separator][string range $p $hlen end]"
111 regsub -all "\n" $p "\\n" p
112 $w_recentlist insert end
$p link
113 $w_recentlist insert end
"\n"
115 $w_recentlist conf
-state disabled
116 $w_recentlist tag
bind link
<1> [cb _open_recent
%x
,%y
]
117 pack $w_body.space
-anchor w
-fill x
118 pack $w_body.recentlabel
-anchor w
-fill x
119 pack $w_recentlist -anchor w
-fill x
121 pack $w_body -fill x
-padx 10 -pady 10
124 set w_next
$w.buttons.next
127 -text [mc
"Next >"] \
129 pack $w_next -side right
-padx 5
130 button $w.buttons.quit
\
133 pack $w.buttons.quit
-side right
-padx 5
134 pack $w.buttons
-side bottom
-fill x
-padx 10 -pady 10
136 bind $top <Return
> [cb _invoke_next
]
137 bind $top <Visibility
> "
141 bind $top <Visibility> {}
144 tkwait variable @done
147 eval destroy [winfo children
$top]
152 if {[catch {set h
$::env(HOME
)}]
153 ||
![file isdirectory
$h]} {
160 set nx
[winfo reqwidth
$top]
161 set ny
[winfo reqheight
$top]
162 set rx
[expr {([winfo screenwidth
$top] - $nx) / 3}]
163 set ry
[expr {([winfo screenheight
$top] - $ny) / 3}]
164 wm geometry
$top [format {+%d
+%d
} $rx $ry]
167 method _invoke_next
{} {
168 if {[winfo exists
$w_next]} {
169 uplevel #0 [$w_next cget -command]
173 proc _get_recentrepos
{} {
175 foreach p
[get_config gui.recentrepo
] {
176 if {[_is_git
[file join $p .git
]]} {
180 return [lsort $recent]
183 proc _unset_recentrepo
{p
} {
184 regsub -all -- {([()\[\]{}\.^
$+*?
\\])} $p {\\\1} p
185 git config
--global --unset gui.recentrepo
"^$p\$"
188 proc _append_recentrepos
{path
} {
189 set path
[file normalize
$path]
190 set recent
[get_config gui.recentrepo
]
192 if {[lindex $recent end
] eq
$path} {
196 set i
[lsearch $recent $path]
198 _unset_recentrepo
$path
199 set recent
[lreplace $recent $i $i]
203 git config
--global --add gui.recentrepo
$path
205 while {[llength $recent] > 10} {
206 _unset_recentrepo
[lindex $recent 0]
207 set recent
[lrange $recent 1 end
]
211 method _open_recent
{xy
} {
212 set id
[lindex [split [$w_recentlist index
@$xy] .
] 0]
213 set local_path
[lindex $sorted_recent [expr {$id - 1}]]
222 method _write_local_path
{args
} {
223 if {$local_path eq
{}} {
224 $w_next conf
-state disabled
226 $w_next conf
-state normal
230 method _git_init
{} {
231 if {[file exists
$local_path]} {
232 error_popup
[mc
"Location %s already exists." $local_path]
236 if {[catch {file mkdir
$local_path} err
]} {
237 error_popup
[strcat
\
238 [mc
"Failed to create repository %s:" $local_path] \
243 if {[catch {cd $local_path} err
]} {
244 error_popup
[strcat
\
245 [mc
"Failed to create repository %s:" $local_path] \
250 if {[catch {git init
} err
]} {
251 error_popup
[strcat
\
252 [mc
"Failed to create repository %s:" $local_path] \
257 _append_recentrepos
[pwd]
263 proc _is_git
{path
} {
264 if {[file exists
[file join $path HEAD
]]
265 && [file exists
[file join $path objects
]]
266 && [file exists
[file join $path config
]]} {
272 ######################################################################
274 ## Create New Repository
279 -command [cb _do_new2
] \
285 -text [mc
"Create New Repository"]
286 pack $w_body.h
-side top
-fill x
-pady 10
287 pack $w_body -fill x
-padx 10
290 label $w_body.where.l
-text [mc
"Directory:"]
291 entry $w_body.where.t
\
292 -textvariable @local_path
\
295 button $w_body.where.b
\
296 -text [mc
"Browse"] \
297 -command [cb _new_local_path
]
299 pack $w_body.where.b
-side right
300 pack $w_body.where.l
-side left
301 pack $w_body.where.t
-fill x
302 pack $w_body.where
-fill x
304 trace add
variable @local_path write
[cb _write_local_path
]
306 focus $w_body.where.t
309 method _new_local_path
{} {
310 if {$local_path ne
{}} {
311 set p
[file dirname
$local_path]
316 set p
[tk_chooseDirectory \
319 -title [mc
"Git Repository"] \
323 set p
[file normalize
$p]
324 if {[file isdirectory
$p]} {
334 error_popup
[mc
"Directory %s already exists." $p]
339 if {[catch {file delete
$p} err
]} {
340 error_popup
[strcat
\
341 [mc
"Directory %s already exists." $p] \
345 } elseif
{[file exists
$p]} {
346 error_popup
[mc
"File %s already exists." $p]
353 if {![_git_init
$this]} {
359 ######################################################################
361 ## Clone Existing Repository
363 method _do_clone
{} {
366 -command [cb _do_clone2
] \
372 -text [mc
"Clone Existing Repository"]
373 pack $w_body.h
-side top
-fill x
-pady 10
374 pack $w_body -fill x
-padx 10
376 set args
$w_body.args
378 pack $args -fill both
380 label $args.origin_l
-text [mc
"URL:"]
381 entry $args.origin_t
\
382 -textvariable @origin_url
\
385 button $args.origin_b
\
386 -text [mc
"Browse"] \
387 -command [cb _open_origin
]
388 grid $args.origin_l
$args.origin_t
$args.origin_b
-sticky ew
390 label $args.where_l
-text [mc
"Directory:"]
391 entry $args.where_t
\
392 -textvariable @local_path
\
395 button $args.where_b
\
396 -text [mc
"Browse"] \
397 -command [cb _new_local_path
]
398 grid $args.where_l
$args.where_t
$args.where_b
-sticky ew
400 label $args.type_l
-text [mc
"Clone Type:"]
403 lappend w_types
[radiobutton $args.type_f.hardlink
\
406 -text [mc
"Standard (Fast, Semi-Redundant, Hardlinks)"] \
407 -variable @clone_type
\
409 lappend w_types
[radiobutton $args.type_f.full
\
412 -text [mc
"Full Copy (Slower, Redundant Backup)"] \
413 -variable @clone_type
\
415 lappend w_types
[radiobutton $args.type_f.shared
\
418 -text [mc
"Shared (Fastest, Not Recommended, No Backup)"] \
419 -variable @clone_type
\
424 grid $args.type_l
$args.type_f
-sticky new
426 grid columnconfigure
$args 1 -weight 1
428 trace add
variable @local_path write
[cb _update_clone
]
429 trace add
variable @origin_url write
[cb _update_clone
]
434 method _open_origin
{} {
435 if {$origin_url ne
{} && [file isdirectory
$origin_url]} {
441 set p
[tk_chooseDirectory \
444 -title [mc
"Git Repository"] \
448 set p
[file normalize
$p]
449 if {![_is_git
[file join $p .git
]] && ![_is_git
$p]} {
450 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
456 method _update_clone
{args
} {
457 if {$local_path ne
{} && $origin_url ne
{}} {
458 $w_next conf
-state normal
460 $w_next conf
-state disabled
463 if {$origin_url ne
{} &&
464 ( [_is_git
[file join $origin_url .git
]]
465 ||
[_is_git
$origin_url])} {
467 if {[[lindex $w_types 0] cget
-state] eq
{disabled
}} {
468 set clone_type hardlink
480 method _do_clone2
{} {
481 if {[file isdirectory
$origin_url]} {
482 set origin_url
[file normalize
$origin_url]
485 if {$clone_type eq
{hardlink
} && ![file isdirectory
$origin_url]} {
486 error_popup
[mc
"Standard only available for local repository."]
489 if {$clone_type eq
{shared
} && ![file isdirectory
$origin_url]} {
490 error_popup
[mc
"Shared only available for local repository."]
494 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
495 set objdir
[file join $origin_url .git objects
]
496 if {![file isdirectory
$objdir]} {
497 set objdir
[file join $origin_url objects
]
498 if {![file isdirectory
$objdir]} {
499 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
505 set giturl
$origin_url
506 if {[is_Cygwin
] && [file isdirectory
$giturl]} {
507 set giturl
[exec cygpath
--unix --absolute $giturl]
508 if {$clone_type eq
{shared
}} {
509 set objdir
[exec cygpath
--unix --absolute $objdir]
513 if {![_git_init
$this]} return
517 git config remote.
$origin_name.url
$giturl
518 git config remote.
$origin_name.fetch
+refs
/heads
/*:refs
/remotes
/$origin_name/*
520 error_popup
[strcat
[mc
"Failed to configure origin"] "\n\n$err"]
524 destroy $w_body $w_next
526 switch -exact -- $clone_type {
528 set o_cons
[status_bar
::two_line $w_body]
529 pack $w_body -fill x
-padx 10 -pady 10
532 [mc
"Counting objects"] \
536 if {[file exists
[file join $objdir info alternates
]]} {
539 file mkdir
[gitdir objects
info]
540 set f_in
[open [file join $objdir info alternates
] r
]
541 set f_cp
[open [gitdir objects
info alternates
] w
]
542 fconfigure $f_in -translation binary -encoding binary
543 fconfigure $f_cp -translation binary -encoding binary
545 while {[gets $f_in line
] >= 0} {
547 puts $f_cp [exec cygpath
--unix --absolute $line]
549 puts $f_cp [file normalize
$line]
557 _clone_failed
$this [mc
"Unable to copy objects/info/alternates: %s" $err]
566 -directory [file join $objdir] ??
]
567 set bcnt
[expr {[llength $buckets] + 2}]
569 $o_cons update $bcur $bcnt
572 file mkdir
[file join .git objects
pack]
573 foreach i
[glob -tails -nocomplain \
574 -directory [file join $objdir pack] *] {
575 lappend tolink
[file join pack $i]
577 $o_cons update [incr bcur
] $bcnt
581 file mkdir
[file join .git objects
$i]
582 foreach j
[glob -tails -nocomplain \
583 -directory [file join $objdir $i] *] {
584 lappend tolink
[file join $i $j]
586 $o_cons update [incr bcur
] $bcnt
593 [mc
"Nothing to clone from %s." $origin_url] \
595 [mc
"The 'master' branch has not been initialized."] \
602 set i
[lindex $tolink 0]
605 [file join .git objects
$i] \
606 [file join $objdir $i]
608 info_popup
[mc
"Hardlinks are unavailable. Falling back to copying."]
609 set i
[_copy_files
$this $objdir $tolink]
611 set i
[_link_files
$this $objdir [lrange $tolink 1 end
]]
618 set o_cons
[console::embed \
620 [mc
"Cloning from %s" $origin_url]]
621 pack $w_body -fill both
-expand 1 -padx 10
623 [list git fetch
--no-tags
-k $origin_name] \
627 set fd
[open [gitdir objects
info alternates
] w
]
628 fconfigure $fd -translation binary
634 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
635 if {![_clone_refs
$this]} return
639 set HEAD
[git rev-parse
--verify HEAD^
0]
641 _clone_failed
$this [mc
"Not a Git repository: %s" [file tail
$origin_url]]
645 _do_clone_checkout
$this $HEAD
649 method _copy_files
{objdir tocopy
} {
651 [mc
"Copying objects"] \
656 incr tot
[file size
[file join $objdir $p]]
660 set f_in
[open [file join $objdir $p] r
]
661 set f_cp
[open [file join .git objects
$p] w
]
662 fconfigure $f_in -translation binary -encoding binary
663 fconfigure $f_cp -translation binary -encoding binary
665 while {![eof $f_in]} {
666 incr cmp
[fcopy $f_in $f_cp -size 16384]
668 [expr {$cmp / 1024}] \
676 _clone_failed
$this [mc
"Unable to copy object: %s" $err]
683 method _link_files
{objdir tolink
} {
684 set total
[llength $tolink]
686 [mc
"Linking objects"] \
688 for {set i
0} {$i < $total} {} {
689 set p
[lindex $tolink $i]
692 [file join .git objects
$p] \
693 [file join $objdir $p]
695 _clone_failed
$this [mc
"Unable to hardlink object: %s" $err]
701 $o_cons update $i $total
708 method _clone_refs
{} {
710 if {[catch {cd $origin_url} err
]} {
711 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
714 set fd_in
[git_read for-each-ref
\
716 {--format=list %(refname
) %(objectname
) %(*objectname
)}]
719 set fd
[open [gitdir packed-refs
] w
]
720 fconfigure $fd -translation binary
721 puts $fd "# pack-refs with: peeled"
722 while {[gets $fd_in line
] >= 0} {
723 set line
[eval $line]
724 set refn
[lindex $line 0]
725 set robj
[lindex $line 1]
726 set tobj
[lindex $line 2]
728 if {[regsub ^refs
/heads
/ $refn \
729 "refs/remotes/$origin_name/" refn
]} {
730 puts $fd "$robj $refn"
731 } elseif
{[string match refs
/tags
/* $refn]} {
732 puts $fd "$robj $refn"
743 method _do_clone_tags
{ok
} {
746 [list git fetch
--tags -k $origin_name] \
750 _clone_failed
$this [mc
"Cannot fetch branches and objects. See console output for details."]
754 method _do_clone_HEAD
{ok
} {
757 [list git fetch
$origin_name HEAD
] \
758 [cb _do_clone_full_end
]
761 _clone_failed
$this [mc
"Cannot fetch tags. See console output for details."]
765 method _do_clone_full_end
{ok
} {
772 if {[file exists
[gitdir FETCH_HEAD
]]} {
773 set fd
[open [gitdir FETCH_HEAD
] r
]
774 while {[gets $fd line
] >= 0} {
775 if {[regexp "^(.{40})\t\t" $line line HEAD
]} {
782 catch {git pack-refs
}
783 _do_clone_checkout
$this $HEAD
785 _clone_failed
$this [mc
"Cannot determine HEAD. See console output for details."]
789 method _clone_failed
{{why
{}}} {
790 if {[catch {file delete
-force $local_path} err
]} {
794 [mc
"Unable to cleanup %s" $local_path] \
800 error_popup
[strcat
[mc
"Clone failed."] "\n" $why]
804 method _do_clone_checkout
{HEAD
} {
807 [mc
"No default branch obtained."] \
809 [mc
"The 'master' branch has not been initialized."] \
815 git update-ref HEAD
$HEAD^
0
818 [mc
"Cannot resolve %s as a commit." $HEAD^
0] \
821 [mc
"The 'master' branch has not been initialized."] \
827 set o_cons
[status_bar
::two_line $w_body]
828 pack $w_body -fill x
-padx 10 -pady 10
830 [mc
"Creating working directory"] \
834 set fd
[git_read
--stderr read-tree
\
841 fconfigure $fd -blocking 0 -translation binary
842 fileevent $fd readable
[cb _readtree_wait
$fd]
845 method _readtree_wait
{fd
} {
847 $o_cons update_meter
$buf
848 append readtree_err
$buf
850 fconfigure $fd -blocking 1
852 fconfigure $fd -blocking 0
856 if {[catch {close $fd}]} {
857 set err
$readtree_err
858 regsub {^fatal
: } $err {} err
859 error_popup
[strcat
\
860 [mc
"Initial file checkout failed."] \
868 ######################################################################
870 ## Open Existing Repository
875 -command [cb _do_open2
] \
881 -text [mc
"Open Existing Repository"]
882 pack $w_body.h
-side top
-fill x
-pady 10
883 pack $w_body -fill x
-padx 10
886 label $w_body.where.l
-text [mc
"Repository:"]
887 entry $w_body.where.t
\
888 -textvariable @local_path
\
891 button $w_body.where.b
\
892 -text [mc
"Browse"] \
893 -command [cb _open_local_path
]
895 pack $w_body.where.b
-side right
896 pack $w_body.where.l
-side left
897 pack $w_body.where.t
-fill x
898 pack $w_body.where
-fill x
900 trace add
variable @local_path write
[cb _write_local_path
]
902 focus $w_body.where.t
905 method _open_local_path
{} {
906 if {$local_path ne
{}} {
912 set p
[tk_chooseDirectory \
915 -title [mc
"Git Repository"] \
919 set p
[file normalize
$p]
920 if {![_is_git
[file join $p .git
]]} {
921 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
927 method _do_open2
{} {
928 if {![_is_git
[file join $local_path .git
]]} {
929 error_popup
[mc
"Not a Git repository: %s" [file tail
$local_path]]
933 if {[catch {cd $local_path} err
]} {
934 error_popup
[strcat
\
935 [mc
"Failed to open repository %s:" $local_path] \
940 _append_recentrepos
[pwd]