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
13 field action new
; # What action are we going to perform?
14 field done
0 ; # Finished picking the repository?
15 field local_path
{} ; # Where this repository is locally
16 field origin_url
{} ; # Where we are cloning from
17 field origin_name origin
; # What we shall call 'origin'
18 field clone_type hardlink
; # Type of clone to construct
19 field readtree_err
; # Error output from read-tree (if any)
25 wm title
$top [mc
"Git Gui"]
28 menu $w.mbar
-tearoff 0
29 $top configure
-menu $w.mbar
32 -label [mc Repository
] \
33 -menu $w.mbar.repository
34 menu $w.mbar.repository
35 $w.mbar.repository add command
\
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]
63 pack [git_logo
$w.git_logo
] -side left
-fill y
-padx 10 -pady 10
67 radiobutton $w_body.new
\
69 -text [mc
"Create New Repository"] \
72 radiobutton $w_body.clone
\
74 -text [mc
"Clone Existing Repository"] \
77 radiobutton $w_body.
open \
79 -text [mc
"Open Existing Repository"] \
82 pack $w_body.new
-anchor w
-fill x
83 pack $w_body.clone
-anchor w
-fill x
84 pack $w_body.
open -anchor w
-fill x
85 pack $w_body -fill x
-padx 10 -pady 10
88 set w_next
$w.buttons.next
93 pack $w_next -side right
-padx 5
94 button $w.buttons.quit
\
97 pack $w.buttons.quit
-side right
-padx 5
98 pack $w.buttons
-side bottom
-fill x
-padx 10 -pady 10
100 bind $top <Return
> [cb _invoke_next
]
101 bind $top <Visibility
> "
105 bind $top <Visibility> {}
108 tkwait variable @done
111 eval destroy [winfo children
$top]
116 if {[catch {set h
$::env(HOME
)}]
117 ||
![file isdirectory
$h]} {
124 set nx
[winfo reqwidth
$top]
125 set ny
[winfo reqheight
$top]
126 set rx
[expr {([winfo screenwidth
$top] - $nx) / 3}]
127 set ry
[expr {([winfo screenheight
$top] - $ny) / 3}]
128 wm geometry
$top [format {+%d
+%d
} $rx $ry]
131 method _invoke_next
{} {
132 if {[winfo exists
$w_next]} {
133 uplevel #0 [$w_next cget -command]
142 method _write_local_path
{args
} {
143 if {$local_path eq
{}} {
144 $w_next conf
-state disabled
146 $w_next conf
-state normal
150 method _git_init
{} {
151 if {[file exists
$local_path]} {
152 error_popup
[mc
"Location %s already exists." $local_path]
156 if {[catch {file mkdir
$local_path} err
]} {
157 error_popup
[strcat
\
158 [mc
"Failed to create repository %s:" $local_path] \
163 if {[catch {cd $local_path} err
]} {
164 error_popup
[strcat
\
165 [mc
"Failed to create repository %s:" $local_path] \
170 if {[catch {git init
} err
]} {
171 error_popup
[strcat
\
172 [mc
"Failed to create repository %s:" $local_path] \
182 proc _is_git
{path
} {
183 if {[file exists
[file join $path HEAD
]]
184 && [file exists
[file join $path objects
]]
185 && [file exists
[file join $path config
]]} {
191 ######################################################################
193 ## Create New Repository
198 -command [cb _do_new2
] \
204 -text [mc
"Create New Repository"]
205 pack $w_body.h
-side top
-fill x
-pady 10
206 pack $w_body -fill x
-padx 10
209 label $w_body.where.l
-text [mc
"Directory:"]
210 entry $w_body.where.t
\
211 -textvariable @local_path
\
214 button $w_body.where.b
\
215 -text [mc
"Browse"] \
216 -command [cb _new_local_path
]
218 pack $w_body.where.b
-side right
219 pack $w_body.where.l
-side left
220 pack $w_body.where.t
-fill x
221 pack $w_body.where
-fill x
223 trace add
variable @local_path write
[cb _write_local_path
]
225 focus $w_body.where.t
228 method _new_local_path
{} {
229 if {$local_path ne
{}} {
230 set p
[file dirname
$local_path]
235 set p
[tk_chooseDirectory \
238 -title [mc
"Git Repository"] \
242 set p
[file normalize
$p]
243 if {[file isdirectory
$p]} {
253 error_popup
[mc
"Directory %s already exists." $p]
258 if {[catch {file delete
$p} err
]} {
259 error_popup
[strcat
\
260 [mc
"Directory %s already exists." $p] \
264 } elseif
{[file exists
$p]} {
265 error_popup
[mc
"File %s already exists." $p]
272 if {![_git_init
$this]} {
278 ######################################################################
280 ## Clone Existing Repository
282 method _do_clone
{} {
285 -command [cb _do_clone2
] \
291 -text [mc
"Clone Existing Repository"]
292 pack $w_body.h
-side top
-fill x
-pady 10
293 pack $w_body -fill x
-padx 10
295 set args
$w_body.args
297 pack $args -fill both
299 label $args.origin_l
-text [mc
"URL:"]
300 entry $args.origin_t
\
301 -textvariable @origin_url
\
304 button $args.origin_b
\
305 -text [mc
"Browse"] \
306 -command [cb _open_origin
]
307 grid $args.origin_l
$args.origin_t
$args.origin_b
-sticky ew
309 label $args.where_l
-text [mc
"Directory:"]
310 entry $args.where_t
\
311 -textvariable @local_path
\
314 button $args.where_b
\
315 -text [mc
"Browse"] \
316 -command [cb _new_local_path
]
317 grid $args.where_l
$args.where_t
$args.where_b
-sticky ew
319 label $args.type_l
-text [mc
"Clone Type:"]
322 lappend w_types
[radiobutton $args.type_f.hardlink
\
325 -text [mc
"Standard (Fast, Semi-Redundant, Hardlinks)"] \
326 -variable @clone_type
\
328 lappend w_types
[radiobutton $args.type_f.full
\
331 -text [mc
"Full Copy (Slower, Redundant Backup)"] \
332 -variable @clone_type
\
334 lappend w_types
[radiobutton $args.type_f.shared
\
337 -text [mc
"Shared (Fastest, Not Recommended, No Backup)"] \
338 -variable @clone_type
\
343 grid $args.type_l
$args.type_f
-sticky new
345 grid columnconfigure
$args 1 -weight 1
347 trace add
variable @local_path write
[cb _update_clone
]
348 trace add
variable @origin_url write
[cb _update_clone
]
353 method _open_origin
{} {
354 if {$origin_url ne
{} && [file isdirectory
$origin_url]} {
360 set p
[tk_chooseDirectory \
363 -title [mc
"Git Repository"] \
367 set p
[file normalize
$p]
368 if {![_is_git
[file join $p .git
]] && ![_is_git
$p]} {
369 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
375 method _update_clone
{args
} {
376 if {$local_path ne
{} && $origin_url ne
{}} {
377 $w_next conf
-state normal
379 $w_next conf
-state disabled
382 if {$origin_url ne
{} &&
383 ( [_is_git
[file join $origin_url .git
]]
384 ||
[_is_git
$origin_url])} {
386 if {[[lindex $w_types 0] cget
-state] eq
{disabled
}} {
387 set clone_type hardlink
399 method _do_clone2
{} {
400 if {[file isdirectory
$origin_url]} {
401 set origin_url
[file normalize
$origin_url]
404 if {$clone_type eq
{hardlink
} && ![file isdirectory
$origin_url]} {
405 error_popup
[mc
"Standard only available for local repository."]
408 if {$clone_type eq
{shared
} && ![file isdirectory
$origin_url]} {
409 error_popup
[mc
"Shared only available for local repository."]
413 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
414 set objdir
[file join $origin_url .git objects
]
415 if {![file isdirectory
$objdir]} {
416 set objdir
[file join $origin_url objects
]
417 if {![file isdirectory
$objdir]} {
418 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
424 set giturl
$origin_url
425 if {[is_Cygwin
] && [file isdirectory
$giturl]} {
426 set giturl
[exec cygpath
--unix --absolute $giturl]
427 if {$clone_type eq
{shared
}} {
428 set objdir
[exec cygpath
--unix --absolute $objdir]
432 if {![_git_init
$this]} return
436 git config remote.
$origin_name.url
$giturl
437 git config remote.
$origin_name.fetch
+refs
/heads
/*:refs
/remotes
/$origin_name/*
439 error_popup
[strcat
[mc
"Failed to configure origin"] "\n\n$err"]
443 destroy $w_body $w_next
445 switch -exact -- $clone_type {
447 set o_cons
[status_bar
::two_line $w_body]
448 pack $w_body -fill x
-padx 10 -pady 10
451 [mc
"Counting objects"] \
455 if {[file exists
[file join $objdir info alternates
]]} {
458 file mkdir
[gitdir objects
info]
459 set f_in
[open [file join $objdir info alternates
] r
]
460 set f_cp
[open [gitdir objects
info alternates
] w
]
461 fconfigure $f_in -translation binary -encoding binary
462 fconfigure $f_cp -translation binary -encoding binary
464 while {[gets $f_in line
] >= 0} {
466 puts $f_cp [exec cygpath
--unix --absolute $line]
468 puts $f_cp [file normalize
$line]
476 _clone_failed
$this [mc
"Unable to copy objects/info/alternates: %s" $err]
485 -directory [file join $objdir] ??
]
486 set bcnt
[expr {[llength $buckets] + 2}]
488 $o_cons update $bcur $bcnt
491 file mkdir
[file join .git objects
pack]
492 foreach i
[glob -tails -nocomplain \
493 -directory [file join $objdir pack] *] {
494 lappend tolink
[file join pack $i]
496 $o_cons update [incr bcur
] $bcnt
500 file mkdir
[file join .git objects
$i]
501 foreach j
[glob -tails -nocomplain \
502 -directory [file join $objdir $i] *] {
503 lappend tolink
[file join $i $j]
505 $o_cons update [incr bcur
] $bcnt
512 [mc
"Nothing to clone from %s." $origin_url] \
514 [mc
"The 'master' branch has not been initialized."] \
521 set i
[lindex $tolink 0]
524 [file join .git objects
$i] \
525 [file join $objdir $i]
527 info_popup
[mc
"Hardlinks are unavailable. Falling back to copying."]
528 set i
[_copy_files
$this $objdir $tolink]
530 set i
[_link_files
$this $objdir [lrange $tolink 1 end
]]
537 set o_cons
[console::embed \
539 [mc
"Cloning from %s" $origin_url]]
540 pack $w_body -fill both
-expand 1 -padx 10
542 [list git fetch
--no-tags
-k $origin_name] \
546 set fd
[open [gitdir objects
info alternates
] w
]
547 fconfigure $fd -translation binary
553 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
554 if {![_clone_refs
$this]} return
558 set HEAD
[git rev-parse
--verify HEAD^
0]
560 _clone_failed
$this [mc
"Not a Git repository: %s" [file tail
$origin_url]]
564 _do_clone_checkout
$this $HEAD
568 method _copy_files
{objdir tocopy
} {
570 [mc
"Copying objects"] \
575 incr tot
[file size
[file join $objdir $p]]
579 set f_in
[open [file join $objdir $p] r
]
580 set f_cp
[open [file join .git objects
$p] w
]
581 fconfigure $f_in -translation binary -encoding binary
582 fconfigure $f_cp -translation binary -encoding binary
584 while {![eof $f_in]} {
585 incr cmp
[fcopy $f_in $f_cp -size 16384]
587 [expr {$cmp / 1024}] \
595 _clone_failed
$this [mc
"Unable to copy object: %s" $err]
602 method _link_files
{objdir tolink
} {
603 set total
[llength $tolink]
605 [mc
"Linking objects"] \
607 for {set i
0} {$i < $total} {} {
608 set p
[lindex $tolink $i]
611 [file join .git objects
$p] \
612 [file join $objdir $p]
614 _clone_failed
$this [mc
"Unable to hardlink object: %s" $err]
620 $o_cons update $i $total
627 method _clone_refs
{} {
629 if {[catch {cd $origin_url} err
]} {
630 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
633 set fd_in
[git_read for-each-ref
\
635 {--format=list %(refname
) %(objectname
) %(*objectname
)}]
638 set fd
[open [gitdir packed-refs
] w
]
639 fconfigure $fd -translation binary
640 puts $fd "# pack-refs with: peeled"
641 while {[gets $fd_in line
] >= 0} {
642 set line
[eval $line]
643 set refn
[lindex $line 0]
644 set robj
[lindex $line 1]
645 set tobj
[lindex $line 2]
647 if {[regsub ^refs
/heads
/ $refn \
648 "refs/remotes/$origin_name/" refn
]} {
649 puts $fd "$robj $refn"
650 } elseif
{[string match refs
/tags
/* $refn]} {
651 puts $fd "$robj $refn"
662 method _do_clone_tags
{ok
} {
665 [list git fetch
--tags -k $origin_name] \
669 _clone_failed
$this [mc
"Cannot fetch branches and objects. See console output for details."]
673 method _do_clone_HEAD
{ok
} {
676 [list git fetch
$origin_name HEAD
] \
677 [cb _do_clone_full_end
]
680 _clone_failed
$this [mc
"Cannot fetch tags. See console output for details."]
684 method _do_clone_full_end
{ok
} {
691 if {[file exists
[gitdir FETCH_HEAD
]]} {
692 set fd
[open [gitdir FETCH_HEAD
] r
]
693 while {[gets $fd line
] >= 0} {
694 if {[regexp "^(.{40})\t\t" $line line HEAD
]} {
701 catch {git pack-refs
}
702 _do_clone_checkout
$this $HEAD
704 _clone_failed
$this [mc
"Cannot determine HEAD. See console output for details."]
708 method _clone_failed
{{why
{}}} {
709 if {[catch {file delete
-force $local_path} err
]} {
713 [mc
"Unable to cleanup %s" $local_path] \
719 error_popup
[strcat
[mc
"Clone failed."] "\n" $why]
723 method _do_clone_checkout
{HEAD
} {
726 [mc
"No default branch obtained."] \
728 [mc
"The 'master' branch has not been initialized."] \
734 git update-ref HEAD
$HEAD^
0
737 [mc
"Cannot resolve %s as a commit." $HEAD^
0] \
740 [mc
"The 'master' branch has not been initialized."] \
746 set o_cons
[status_bar
::two_line $w_body]
747 pack $w_body -fill x
-padx 10 -pady 10
749 [mc
"Creating working directory"] \
753 set fd
[git_read
--stderr read-tree
\
760 fconfigure $fd -blocking 0 -translation binary
761 fileevent $fd readable
[cb _readtree_wait
$fd]
764 method _readtree_wait
{fd
} {
766 $o_cons update_meter
$buf
767 append readtree_err
$buf
769 fconfigure $fd -blocking 1
771 fconfigure $fd -blocking 0
775 if {[catch {close $fd}]} {
776 set err
$readtree_err
777 regsub {^fatal
: } $err {} err
778 error_popup
[strcat
\
779 [mc
"Initial file checkout failed."] \
787 ######################################################################
789 ## Open Existing Repository
794 -command [cb _do_open2
] \
800 -text [mc
"Open Existing Repository"]
801 pack $w_body.h
-side top
-fill x
-pady 10
802 pack $w_body -fill x
-padx 10
805 label $w_body.where.l
-text [mc
"Repository:"]
806 entry $w_body.where.t
\
807 -textvariable @local_path
\
810 button $w_body.where.b
\
811 -text [mc
"Browse"] \
812 -command [cb _open_local_path
]
814 pack $w_body.where.b
-side right
815 pack $w_body.where.l
-side left
816 pack $w_body.where.t
-fill x
817 pack $w_body.where
-fill x
819 trace add
variable @local_path write
[cb _write_local_path
]
821 focus $w_body.where.t
824 method _open_local_path
{} {
825 if {$local_path ne
{}} {
831 set p
[tk_chooseDirectory \
834 -title [mc
"Git Repository"] \
838 set p
[file normalize
$p]
839 if {![_is_git
[file join $p .git
]]} {
840 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
846 method _do_open2
{} {
847 if {![_is_git
[file join $local_path .git
]]} {
848 error_popup
[mc
"Not a Git repository: %s" [file tail
$local_path]]
852 if {[catch {cd $local_path} err
]} {
853 error_popup
[strcat
\
854 [mc
"Failed to open repository %s:" $local_path] \