1 # git-gui Git repository chooser
2 # Copyright (C) 2007 Shawn Pearce
4 class choose_repository
{
6 # Henrik Nyh's alternative Git logo, from his blog post
7 # http://henrik.nyh.se/2007/06/alternative-git-logo-and-favicon
9 image create
photo ::choose_repository::git_logo -data {
10 R0lGODdhYQC8AIQbAGZmZtg4LW9vb3l5eYKCgoyMjEC
/TOJpYZWVlZ
+fn2
/PeKmpqbKysry8vMXF
11 xZ
/fpc
/Pz7fnvPXNytnZ2eLi4s
/v0vja1
+zs7Of36fX19f3z8v
///////////////////ywAAAAA
12 YQC8AAAF
/uAmjmRpnmiqrmzrvq4hz3RtGw
+s7zx5
/7dcb0hUAY8zYXHJRCKVzGjPeYRKry8q0Irt
13 GrVBr3gFDo
/PprKNix6ra
+y2902Ly7H05L2dl9n3UX04gGeCf4RFhohiiotdjY5XkJGBfYeUOpOY
14 iZablXmXURgPpKWmp6ipqYIKqq6vqREjFYK1trUKs7e7vFq5IrS9wsM0vxvBxMm8xsjKzqy6z9J5
15 zNPWatXX2k7Z29433d
/iMuHj3
+Xm2
+jp1
+vs0
+7vz
/HyyvT1xPf4wvr7y9H
+pBkbBasgLFYGE8ba
16 o8nTlE4OOYGKKJFOKIopGmLMAnHjDo0eWYAM
+WUiSRgj
/k
+eSKmyBMuWI17C3CATZs2WN1XmPLmT
17 ZM
+QPz0G3VihqNGjSJNWwDCzqdOnUKPu0SChqtWrWLNq3cq1q9evYCVYGCEhgNmzaNOqXcu2rdu3
18 cOMGOEBWrt27ePPCpSuirN6
/gAO35bvBr
+DDiPMSNpy4sWO2ix9Lnmw2MuXLiS1j3gxYM
+fPdz2D
19 Hv1WNOnTak2jXj23LuvXlV3DZq16Nujatjnjzo15N2
/Kvn9LDi7cMfHimaUqX868ufPn0KPPpOCA
20 AQMWCQBo3869u
/fv4MNrd3DlQoMC3QlkSJFdvPv38LVDWJLBAYHwE1LE38
+/+/UhGTAggHv5odDf
21 gfv9
/seDgPAVeAKCELqnIAwU3BefgyZEqOF3E7rAQH8Y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BAA
+wknTpC
40 field w_body
; # Widget holding the center content
41 field w_next
; # Next button
42 field o_cons
; # Console object (if active)
43 field w_types
; # List of type buttons in clone
45 field action new
; # What action are we going to perform?
46 field done
0 ; # Finished picking the repository?
47 field local_path
{} ; # Where this repository is locally
48 field origin_url
{} ; # Where we are cloning from
49 field origin_name origin
; # What we shall call 'origin'
50 field clone_type hardlink
; # Type of clone to construct
51 field readtree_err
; # Error output from read-tree (if any)
57 wm title
$top [mc
"Git Gui"]
60 menu $w.mbar
-tearoff 0
61 $top configure
-menu $w.mbar
64 -label [mc Repository
] \
65 -menu $w.mbar.repository
66 menu $w.mbar.repository
67 $w.mbar.repository add command
\
73 $w.mbar add cascade
-label [mc Apple
] -menu .mbar.apple
75 $w.mbar.apple add command
\
76 -label [mc
"About %s" [appname
]] \
79 $w.mbar add cascade
-label [mc Help
] -menu $w.mbar.help
81 $w.mbar.help add command
\
82 -label [mc
"About %s" [appname
]] \
86 wm protocol
$top WM_DELETE_WINDOW
exit
87 bind $top <$M1B-q
> exit
88 bind $top <$M1B-Q
> exit
89 bind $top <Key-Escape
> exit
91 wm geometry
$top "+[winfo rootx .]+[winfo rooty .]"
92 bind $top <Key-Escape
> [list destroy $top]
99 -image ::choose_repository::git_logo
100 pack $w.git_logo
-side left
-fill y
-padx 10 -pady 10
104 radiobutton $w_body.new
\
106 -text [mc
"Create New Repository"] \
109 radiobutton $w_body.clone
\
111 -text [mc
"Clone Existing Repository"] \
114 radiobutton $w_body.
open \
116 -text [mc
"Open Existing Repository"] \
119 pack $w_body.new
-anchor w
-fill x
120 pack $w_body.clone
-anchor w
-fill x
121 pack $w_body.
open -anchor w
-fill x
122 pack $w_body -fill x
-padx 10 -pady 10
125 set w_next
$w.buttons.next
128 -text [mc
"Next >"] \
130 pack $w_next -side right
-padx 5
131 button $w.buttons.quit
\
134 pack $w.buttons.quit
-side right
-padx 5
135 pack $w.buttons
-side bottom
-fill x
-padx 10 -pady 10
137 bind $top <Return
> [cb _invoke_next
]
138 bind $top <Visibility
> "
142 bind $top <Visibility> {}
145 tkwait variable @done
148 eval destroy [winfo children
$top]
153 if {[catch {set h
$::env(HOME
)}]
154 ||
![file isdirectory
$h]} {
161 set nx
[winfo reqwidth
$top]
162 set ny
[winfo reqheight
$top]
163 set rx
[expr {([winfo screenwidth
$top] - $nx) / 3}]
164 set ry
[expr {([winfo screenheight
$top] - $ny) / 3}]
165 wm geometry
$top [format {+%d
+%d
} $rx $ry]
168 method _invoke_next
{} {
169 if {[winfo exists
$w_next]} {
170 uplevel #0 [$w_next cget -command]
179 method _write_local_path
{args
} {
180 if {$local_path eq
{}} {
181 $w_next conf
-state disabled
183 $w_next conf
-state normal
187 method _git_init
{} {
188 if {[file exists
$local_path]} {
189 error_popup
[mc
"Location %s already exists." $local_path]
193 if {[catch {file mkdir
$local_path} err
]} {
194 error_popup
[strcat
\
195 [mc
"Failed to create repository %s:" $local_path] \
200 if {[catch {cd $local_path} err
]} {
201 error_popup
[strcat
\
202 [mc
"Failed to create repository %s:" $local_path] \
207 if {[catch {git init
} err
]} {
208 error_popup
[strcat
\
209 [mc
"Failed to create repository %s:" $local_path] \
219 proc _is_git
{path
} {
220 if {[file exists
[file join $path HEAD
]]
221 && [file exists
[file join $path objects
]]
222 && [file exists
[file join $path config
]]} {
228 ######################################################################
230 ## Create New Repository
235 -command [cb _do_new2
] \
241 -text [mc
"Create New Repository"]
242 pack $w_body.h
-side top
-fill x
-pady 10
243 pack $w_body -fill x
-padx 10
246 label $w_body.where.l
-text [mc
"Directory:"]
247 entry $w_body.where.t
\
248 -textvariable @local_path
\
251 button $w_body.where.b
\
252 -text [mc
"Browse"] \
253 -command [cb _new_local_path
]
255 pack $w_body.where.b
-side right
256 pack $w_body.where.l
-side left
257 pack $w_body.where.t
-fill x
258 pack $w_body.where
-fill x
260 trace add
variable @local_path write
[cb _write_local_path
]
262 focus $w_body.where.t
265 method _new_local_path
{} {
266 if {$local_path ne
{}} {
267 set p
[file dirname
$local_path]
272 set p
[tk_chooseDirectory \
275 -title [mc
"Git Repository"] \
279 set p
[file normalize
$p]
280 if {[file isdirectory
$p]} {
290 error_popup
[mc
"Directory %s already exists." $p]
295 if {[catch {file delete
$p} err
]} {
296 error_popup
[strcat
\
297 [mc
"Directory %s already exists." $p] \
301 } elseif
{[file exists
$p]} {
302 error_popup
[mc
"File %s already exists." $p]
309 if {![_git_init
$this]} {
315 ######################################################################
317 ## Clone Existing Repository
319 method _do_clone
{} {
322 -command [cb _do_clone2
] \
328 -text [mc
"Clone Existing Repository"]
329 pack $w_body.h
-side top
-fill x
-pady 10
330 pack $w_body -fill x
-padx 10
332 set args
$w_body.args
334 pack $args -fill both
336 label $args.origin_l
-text [mc
"URL:"]
337 entry $args.origin_t
\
338 -textvariable @origin_url
\
341 button $args.origin_b
\
342 -text [mc
"Browse"] \
343 -command [cb _open_origin
]
344 grid $args.origin_l
$args.origin_t
$args.origin_b
-sticky ew
346 label $args.where_l
-text [mc
"Directory:"]
347 entry $args.where_t
\
348 -textvariable @local_path
\
351 button $args.where_b
\
352 -text [mc
"Browse"] \
353 -command [cb _new_local_path
]
354 grid $args.where_l
$args.where_t
$args.where_b
-sticky ew
356 label $args.type_l
-text [mc
"Clone Type:"]
359 lappend w_types
[radiobutton $args.type_f.hardlink
\
362 -text [mc
"Standard (Fast, Semi-Redundant, Hardlinks)"] \
363 -variable @clone_type
\
365 lappend w_types
[radiobutton $args.type_f.full
\
368 -text [mc
"Full Copy (Slower, Redundant Backup)"] \
369 -variable @clone_type
\
371 lappend w_types
[radiobutton $args.type_f.shared
\
374 -text [mc
"Shared (Fastest, Not Recommended, No Backup)"] \
375 -variable @clone_type
\
380 grid $args.type_l
$args.type_f
-sticky new
382 grid columnconfigure
$args 1 -weight 1
384 trace add
variable @local_path write
[cb _update_clone
]
385 trace add
variable @origin_url write
[cb _update_clone
]
390 method _open_origin
{} {
391 if {$origin_url ne
{} && [file isdirectory
$origin_url]} {
397 set p
[tk_chooseDirectory \
400 -title [mc
"Git Repository"] \
404 set p
[file normalize
$p]
405 if {![_is_git
[file join $p .git
]] && ![_is_git
$p]} {
406 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
412 method _update_clone
{args
} {
413 if {$local_path ne
{} && $origin_url ne
{}} {
414 $w_next conf
-state normal
416 $w_next conf
-state disabled
419 if {$origin_url ne
{} &&
420 ( [_is_git
[file join $origin_url .git
]]
421 ||
[_is_git
$origin_url])} {
423 if {[[lindex $w_types 0] cget
-state] eq
{disabled
}} {
424 set clone_type hardlink
436 method _do_clone2
{} {
437 if {[file isdirectory
$origin_url]} {
438 set origin_url
[file normalize
$origin_url]
441 if {$clone_type eq
{hardlink
} && ![file isdirectory
$origin_url]} {
442 error_popup
[mc
"Standard only available for local repository."]
445 if {$clone_type eq
{shared
} && ![file isdirectory
$origin_url]} {
446 error_popup
[mc
"Shared only available for local repository."]
450 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
451 set objdir
[file join $origin_url .git objects
]
452 if {![file isdirectory
$objdir]} {
453 set objdir
[file join $origin_url objects
]
454 if {![file isdirectory
$objdir]} {
455 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
461 set giturl
$origin_url
462 if {[is_Cygwin
] && [file isdirectory
$giturl]} {
463 set giturl
[exec cygpath
--unix --absolute $giturl]
464 if {$clone_type eq
{shared
}} {
465 set objdir
[exec cygpath
--unix --absolute $objdir]
469 if {![_git_init
$this]} return
473 git config remote.
$origin_name.url
$giturl
474 git config remote.
$origin_name.fetch
+refs
/heads
/*:refs
/remotes
/$origin_name/*
476 error_popup
[strcat
[mc
"Failed to configure origin"] "\n\n$err"]
480 destroy $w_body $w_next
482 switch -exact -- $clone_type {
484 set o_cons
[status_bar
::new $w_body]
485 pack $w_body -fill x
-padx 10 -pady 10
488 [mc
"Counting objects"] \
492 if {[file exists
[file join $objdir info alternates
]]} {
495 file mkdir
[gitdir objects
info]
496 set f_in
[open [file join $objdir info alternates
] r
]
497 set f_cp
[open [gitdir objects
info alternates
] w
]
498 fconfigure $f_in -translation binary -encoding binary
499 fconfigure $f_cp -translation binary -encoding binary
501 while {[gets $f_in line
] >= 0} {
503 puts $f_cp [exec cygpath
--unix --absolute $line]
505 puts $f_cp [file normalize
$line]
513 _clone_failed
$this [mc
"Unable to copy objects/info/alternates: %s" $err]
522 -directory [file join $objdir] ??
]
523 set bcnt
[expr {[llength $buckets] + 2}]
525 $o_cons update $bcur $bcnt
528 file mkdir
[file join .git objects
pack]
529 foreach i
[glob -tails -nocomplain \
530 -directory [file join $objdir pack] *] {
531 lappend tolink
[file join pack $i]
533 $o_cons update [incr bcur
] $bcnt
537 file mkdir
[file join .git objects
$i]
538 foreach j
[glob -tails -nocomplain \
539 -directory [file join $objdir $i] *] {
540 lappend tolink
[file join $i $j]
542 $o_cons update [incr bcur
] $bcnt
549 [mc
"Nothing to clone from %s." $origin_url] \
551 [mc
"The 'master' branch has not been initialized."] \
558 set i
[lindex $tolink 0]
561 [file join .git objects
$i] \
562 [file join $objdir $i]
564 info_popup
[mc
"Hardlinks are unavailable. Falling back to copying."]
565 set i
[_copy_files
$this $objdir $tolink]
567 set i
[_link_files
$this $objdir [lrange $tolink 1 end
]]
574 set o_cons
[console::embed \
576 [mc
"Cloning from %s" $origin_url]]
577 pack $w_body -fill both
-expand 1 -padx 10
579 [list git fetch
--no-tags
-k $origin_name] \
583 set fd
[open [gitdir objects
info alternates
] w
]
584 fconfigure $fd -translation binary
590 if {$clone_type eq
{hardlink
} ||
$clone_type eq
{shared
}} {
591 if {![_clone_refs
$this]} return
595 set HEAD
[git rev-parse
--verify HEAD^
0]
597 _clone_failed
$this [mc
"Not a Git repository: %s" [file tail
$origin_url]]
601 _do_clone_checkout
$this $HEAD
605 method _copy_files
{objdir tocopy
} {
607 [mc
"Copying objects"] \
612 incr tot
[file size
[file join $objdir $p]]
616 set f_in
[open [file join $objdir $p] r
]
617 set f_cp
[open [file join .git objects
$p] w
]
618 fconfigure $f_in -translation binary -encoding binary
619 fconfigure $f_cp -translation binary -encoding binary
621 while {![eof $f_in]} {
622 incr cmp
[fcopy $f_in $f_cp -size 16384]
624 [expr {$cmp / 1024}] \
632 _clone_failed
$this [mc
"Unable to copy object: %s" $err]
639 method _link_files
{objdir tolink
} {
640 set total
[llength $tolink]
642 [mc
"Linking objects"] \
644 for {set i
0} {$i < $total} {} {
645 set p
[lindex $tolink $i]
648 [file join .git objects
$p] \
649 [file join $objdir $p]
651 _clone_failed
$this [mc
"Unable to hardlink object: %s" $err]
657 $o_cons update $i $total
664 method _clone_refs
{} {
666 if {[catch {cd $origin_url} err
]} {
667 error_popup
[mc
"Not a Git repository: %s" [file tail
$origin_url]]
670 set fd_in
[git_read for-each-ref
\
672 {--format=list %(refname
) %(objectname
) %(*objectname
)}]
675 set fd
[open [gitdir packed-refs
] w
]
676 fconfigure $fd -translation binary
677 puts $fd "# pack-refs with: peeled"
678 while {[gets $fd_in line
] >= 0} {
679 set line
[eval $line]
680 set refn
[lindex $line 0]
681 set robj
[lindex $line 1]
682 set tobj
[lindex $line 2]
684 if {[regsub ^refs
/heads
/ $refn \
685 "refs/remotes/$origin_name/" refn
]} {
686 puts $fd "$robj $refn"
687 } elseif
{[string match refs
/tags
/* $refn]} {
688 puts $fd "$robj $refn"
699 method _do_clone_tags
{ok
} {
702 [list git fetch
--tags -k $origin_name] \
706 _clone_failed
$this [mc
"Cannot fetch branches and objects. See console output for details."]
710 method _do_clone_HEAD
{ok
} {
713 [list git fetch
$origin_name HEAD
] \
714 [cb _do_clone_full_end
]
717 _clone_failed
$this [mc
"Cannot fetch tags. See console output for details."]
721 method _do_clone_full_end
{ok
} {
728 if {[file exists
[gitdir FETCH_HEAD
]]} {
729 set fd
[open [gitdir FETCH_HEAD
] r
]
730 while {[gets $fd line
] >= 0} {
731 if {[regexp "^(.{40})\t\t" $line line HEAD
]} {
738 catch {git pack-refs
}
739 _do_clone_checkout
$this $HEAD
741 _clone_failed
$this [mc
"Cannot determine HEAD. See console output for details."]
745 method _clone_failed
{{why
{}}} {
746 if {[catch {file delete
-force $local_path} err
]} {
750 [mc
"Unable to cleanup %s" $local_path] \
756 error_popup
[strcat
[mc
"Clone failed."] "\n" $why]
760 method _do_clone_checkout
{HEAD
} {
763 [mc
"No default branch obtained."] \
765 [mc
"The 'master' branch has not been initialized."] \
771 git update-ref HEAD
$HEAD^
0
774 [mc
"Cannot resolve %s as a commit." $HEAD^
0] \
777 [mc
"The 'master' branch has not been initialized."] \
783 set o_cons
[status_bar
::new $w_body]
784 pack $w_body -fill x
-padx 10 -pady 10
786 [mc
"Creating working directory"] \
790 set fd
[git_read
--stderr read-tree
\
797 fconfigure $fd -blocking 0 -translation binary
798 fileevent $fd readable
[cb _readtree_wait
$fd]
801 method _readtree_wait
{fd
} {
803 $o_cons update_meter
$buf
804 append readtree_err
$buf
806 fconfigure $fd -blocking 1
808 fconfigure $fd -blocking 0
812 if {[catch {close $fd}]} {
813 set err
$readtree_err
814 regsub {^fatal
: } $err {} err
815 error_popup
[strcat
\
816 [mc
"Initial file checkout failed."] \
824 ######################################################################
826 ## Open Existing Repository
831 -command [cb _do_open2
] \
837 -text [mc
"Open Existing Repository"]
838 pack $w_body.h
-side top
-fill x
-pady 10
839 pack $w_body -fill x
-padx 10
842 label $w_body.where.l
-text [mc
"Repository:"]
843 entry $w_body.where.t
\
844 -textvariable @local_path
\
847 button $w_body.where.b
\
848 -text [mc
"Browse"] \
849 -command [cb _open_local_path
]
851 pack $w_body.where.b
-side right
852 pack $w_body.where.l
-side left
853 pack $w_body.where.t
-fill x
854 pack $w_body.where
-fill x
856 trace add
variable @local_path write
[cb _write_local_path
]
858 focus $w_body.where.t
861 method _open_local_path
{} {
862 if {$local_path ne
{}} {
868 set p
[tk_chooseDirectory \
871 -title [mc
"Git Repository"] \
875 set p
[file normalize
$p]
876 if {![_is_git
[file join $p .git
]]} {
877 error_popup
[mc
"Not a Git repository: %s" [file tail
$p]]
883 method _do_open2
{} {
884 if {![_is_git
[file join $local_path .git
]]} {
885 error_popup
[mc
"Not a Git repository: %s" [file tail
$local_path]]
889 if {[catch {cd $local_path} err
]} {
890 error_popup
[strcat
\
891 [mc
"Failed to open repository %s:" $local_path] \