git-gui: Fix fetching from remotes when adding them
[git/dscho.git] / lib / choose_repository.tcl
blob909131689ea837cb67bd587a6acc8b5eb3127c4e
1 # git-gui Git repository chooser
2 # Copyright (C) 2007 Shawn Pearce
4 class choose_repository {
6 field top
7 field w
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)
24 constructor pick {} {
25 global M1T M1B
27 make_toplevel top w
28 wm title $top [mc "Git Gui"]
30 if {$top eq {.}} {
31 menu $w.mbar -tearoff 0
32 $top configure -menu $w.mbar
34 set m_repo $w.mbar.repository
35 $w.mbar add cascade \
36 -label [mc Repository] \
37 -menu $m_repo
38 menu $m_repo
40 if {[is_MacOSX]} {
41 $w.mbar add cascade -label Apple -menu .mbar.apple
42 menu $w.mbar.apple
43 $w.mbar.apple add command \
44 -label [mc "About %s" [appname]] \
45 -command do_about
46 } else {
47 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
48 menu $w.mbar.help
49 $w.mbar.help add command \
50 -label [mc "About %s" [appname]] \
51 -command do_about
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
58 } else {
59 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
60 bind $top <Key-Escape> [list destroy $top]
61 set m_repo {}
64 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
66 set w_body $w.body
67 set opts $w_body.options
68 frame $w_body
69 text $opts \
70 -cursor $::cursor_ptr \
71 -relief flat \
72 -background [$w_body cget -background] \
73 -wrap none \
74 -spacing1 5 \
75 -width 50 \
76 -height 3
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
82 $opts insert end "\n"
83 if {$m_repo ne {}} {
84 $m_repo add command \
85 -command [cb _next new] \
86 -accelerator $M1T-N \
87 -label [mc "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
95 $opts insert end "\n"
96 if {$m_repo ne {}} {
97 $m_repo add command \
98 -command [cb _next clone] \
99 -accelerator $M1T-C \
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"
109 if {$m_repo ne {}} {
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} {
122 if {$m_repo ne {}} {
123 $m_repo add separator
124 $m_repo add command \
125 -state disabled \
126 -label [mc "Recent Repositories"]
129 label $w_body.space
130 label $w_body.recentlabel \
131 -anchor w \
132 -text [mc "Open Recent Repository:"]
133 set w_recentlist $w_body.recentlist
134 text $w_recentlist \
135 -cursor $::cursor_ptr \
136 -relief flat \
137 -background [$w_body.recentlabel cget -background] \
138 -wrap none \
139 -width 50 \
140 -height 10
141 $w_recentlist tag conf link \
142 -foreground blue \
143 -underline 1
144 set home $::env(HOME)
145 if {[is_Cygwin]} {
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 {
151 set path $p
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"
159 if {$m_repo ne {}} {
160 $m_repo add command \
161 -command [cb _open_recent_path $path] \
162 -label " $p"
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
173 frame $w.buttons
174 set w_next $w.buttons.next
175 set w_quit $w.buttons.quit
176 button $w_quit \
177 -text [mc "Quit"] \
178 -command exit
179 pack $w_quit -side right -padx 5
180 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
182 if {$m_repo ne {}} {
183 $m_repo add separator
184 $m_repo add command \
185 -label [mc Quit] \
186 -command exit \
187 -accelerator $M1T-Q
190 bind $top <Return> [cb _invoke_next]
191 bind $top <Visibility> "
192 [cb _center]
193 grab $top
194 focus $top
195 bind $top <Visibility> {}
197 wm deiconify $top
198 tkwait variable @done
200 if {$top eq {.}} {
201 eval destroy [winfo children $top]
205 proc _home {} {
206 if {[catch {set h $::env(HOME)}]
207 || ![file isdirectory $h]} {
208 set h .
210 return $h
213 method _center {} {
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 {} {
228 set recent [list]
229 foreach p [get_config gui.recentrepo] {
230 if {[_is_git [file join $p .git]]} {
231 lappend recent $p
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} {
247 return
250 set i [lsearch $recent $path]
251 if {$i >= 0} {
252 _unset_recentrepo $path
253 set recent [lreplace $recent $i $i]
256 lappend recent $path
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}]]
268 _do_open2 $this
271 method _open_recent_path {p} {
272 set local_path $p
273 _do_open2 $this
276 method _next {action} {
277 destroy $w_body
278 if {![winfo exists $w_next]} {
279 button $w_next -default active
280 pack $w_next -side right -padx 5 -before $w_quit
282 _do_$action $this
285 method _write_local_path {args} {
286 if {$local_path eq {}} {
287 $w_next conf -state disabled
288 } else {
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] \
297 "\n\n$err"]
298 return 0
301 if {[catch {cd $local_path} err]} {
302 error_popup [strcat \
303 [mc "Failed to create repository %s:" $local_path] \
304 "\n\n$err"]
305 return 0
308 if {[catch {git init} err]} {
309 error_popup [strcat \
310 [mc "Failed to create repository %s:" $local_path] \
311 "\n\n$err"]
312 return 0
315 _append_recentrepos [pwd]
316 set ::_gitdir .git
317 set ::_prefix {}
318 return 1
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]]} {
325 return 1
327 if {[is_Cygwin]} {
328 if {[file exists [file join $path HEAD]]
329 && [file exists [file join $path objects.lnk]]
330 && [file exists [file join $path config.lnk]]} {
331 return 1
334 return 0
337 proc _objdir {path} {
338 set objdir [file join $path .git objects]
339 if {[file isdirectory $objdir]} {
340 return $objdir
343 set objdir [file join $path objects]
344 if {[file isdirectory $objdir]} {
345 return $objdir
348 if {[is_Cygwin]} {
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]
360 return {}
363 ######################################################################
365 ## Create New Repository
367 method _do_new {} {
368 $w_next conf \
369 -state disabled \
370 -command [cb _do_new2] \
371 -text [mc "Create"]
373 frame $w_body
374 label $w_body.h \
375 -font font_uibold \
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
380 frame $w_body.where
381 label $w_body.where.l -text [mc "Directory:"]
382 entry $w_body.where.t \
383 -textvariable @local_path \
384 -borderwidth 1 \
385 -relief sunken \
386 -width 50
387 button $w_body.where.b \
388 -text [mc "Browse"] \
389 -command [cb _new_local_path]
390 set w_localpath $w_body.where.t
392 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
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]]
397 update
398 focus $w_body.where.t
401 method _new_local_path {} {
402 if {$local_path ne {}} {
403 set p [file dirname $local_path]
404 } else {
405 set p [_home]
408 set p [tk_chooseDirectory \
409 -initialdir $p \
410 -parent $top \
411 -title [mc "Git Repository"] \
412 -mustexist false]
413 if {$p eq {}} return
415 set p [file normalize $p]
416 if {![_new_ok $p]} {
417 return
419 set local_path $p
420 $w_localpath icursor end
423 method _do_new2 {} {
424 if {![_new_ok $local_path]} {
425 return
427 if {![_git_init $this]} {
428 return
430 set done 1
433 proc _new_ok {p} {
434 if {[file isdirectory $p]} {
435 if {[_is_git [file join $p .git]]} {
436 error_popup [mc "Directory %s already exists." $p]
437 return 0
439 } elseif {[file exists $p]} {
440 error_popup [mc "File %s already exists." $p]
441 return 0
443 return 1
446 ######################################################################
448 ## Clone Existing Repository
450 method _do_clone {} {
451 $w_next conf \
452 -state disabled \
453 -command [cb _do_clone2] \
454 -text [mc "Clone"]
456 frame $w_body
457 label $w_body.h \
458 -font font_uibold \
459 -text [mc "Clone Existing Repository"]
460 pack $w_body.h -side top -fill x -pady 10
461 pack $w_body -fill x -padx 10
463 set args $w_body.args
464 frame $w_body.args
465 pack $args -fill both
467 label $args.origin_l -text [mc "Source Location:"]
468 entry $args.origin_t \
469 -textvariable @origin_url \
470 -borderwidth 1 \
471 -relief sunken \
472 -width 50
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 "Target Directory:"]
479 entry $args.where_t \
480 -textvariable @local_path \
481 -borderwidth 1 \
482 -relief sunken \
483 -width 50
484 button $args.where_b \
485 -text [mc "Browse"] \
486 -command [cb _new_local_path]
487 grid $args.where_l $args.where_t $args.where_b -sticky ew
488 set w_localpath $args.where_t
490 label $args.type_l -text [mc "Clone Type:"]
491 frame $args.type_f
492 set w_types [list]
493 lappend w_types [radiobutton $args.type_f.hardlink \
494 -state disabled \
495 -anchor w \
496 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
497 -variable @clone_type \
498 -value hardlink]
499 lappend w_types [radiobutton $args.type_f.full \
500 -state disabled \
501 -anchor w \
502 -text [mc "Full Copy (Slower, Redundant Backup)"] \
503 -variable @clone_type \
504 -value full]
505 lappend w_types [radiobutton $args.type_f.shared \
506 -state disabled \
507 -anchor w \
508 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
509 -variable @clone_type \
510 -value shared]
511 foreach r $w_types {
512 pack $r -anchor w
514 grid $args.type_l $args.type_f -sticky new
516 grid columnconfigure $args 1 -weight 1
518 trace add variable @local_path write [cb _update_clone]
519 trace add variable @origin_url write [cb _update_clone]
520 bind $w_body.h <Destroy> "
521 [list trace remove variable @local_path write [cb _update_clone]]
522 [list trace remove variable @origin_url write [cb _update_clone]]
524 update
525 focus $args.origin_t
528 method _open_origin {} {
529 if {$origin_url ne {} && [file isdirectory $origin_url]} {
530 set p $origin_url
531 } else {
532 set p [_home]
535 set p [tk_chooseDirectory \
536 -initialdir $p \
537 -parent $top \
538 -title [mc "Git Repository"] \
539 -mustexist true]
540 if {$p eq {}} return
542 set p [file normalize $p]
543 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
544 error_popup [mc "Not a Git repository: %s" [file tail $p]]
545 return
547 set origin_url $p
550 method _update_clone {args} {
551 if {$local_path ne {} && $origin_url ne {}} {
552 $w_next conf -state normal
553 } else {
554 $w_next conf -state disabled
557 if {$origin_url ne {} &&
558 ( [_is_git [file join $origin_url .git]]
559 || [_is_git $origin_url])} {
560 set e normal
561 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
562 set clone_type hardlink
564 } else {
565 set e disabled
566 set clone_type full
569 foreach r $w_types {
570 $r conf -state $e
574 method _do_clone2 {} {
575 if {[file isdirectory $origin_url]} {
576 set origin_url [file normalize $origin_url]
579 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
580 error_popup [mc "Standard only available for local repository."]
581 return
583 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
584 error_popup [mc "Shared only available for local repository."]
585 return
588 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
589 set objdir [_objdir $origin_url]
590 if {$objdir eq {}} {
591 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
592 return
596 set giturl $origin_url
597 if {[is_Cygwin] && [file isdirectory $giturl]} {
598 set giturl [exec cygpath --unix --absolute $giturl]
599 if {$clone_type eq {shared}} {
600 set objdir [exec cygpath --unix --absolute $objdir]
604 if {[file exists $local_path]} {
605 error_popup [mc "Location %s already exists." $local_path]
606 return
609 if {![_git_init $this]} return
610 set local_path [pwd]
612 if {[catch {
613 git config remote.$origin_name.url $giturl
614 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
615 } err]} {
616 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
617 return
620 destroy $w_body $w_next
622 switch -exact -- $clone_type {
623 hardlink {
624 set o_cons [status_bar::two_line $w_body]
625 pack $w_body -fill x -padx 10 -pady 10
627 $o_cons start \
628 [mc "Counting objects"] \
629 [mc "buckets"]
630 update
632 if {[file exists [file join $objdir info alternates]]} {
633 set pwd [pwd]
634 if {[catch {
635 file mkdir [gitdir objects info]
636 set f_in [open [file join $objdir info alternates] r]
637 set f_cp [open [gitdir objects info alternates] w]
638 fconfigure $f_in -translation binary -encoding binary
639 fconfigure $f_cp -translation binary -encoding binary
640 cd $objdir
641 while {[gets $f_in line] >= 0} {
642 if {[is_Cygwin]} {
643 puts $f_cp [exec cygpath --unix --absolute $line]
644 } else {
645 puts $f_cp [file normalize $line]
648 close $f_in
649 close $f_cp
650 cd $pwd
651 } err]} {
652 catch {cd $pwd}
653 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
654 return
658 set tolink [list]
659 set buckets [glob \
660 -tails \
661 -nocomplain \
662 -directory [file join $objdir] ??]
663 set bcnt [expr {[llength $buckets] + 2}]
664 set bcur 1
665 $o_cons update $bcur $bcnt
666 update
668 file mkdir [file join .git objects pack]
669 foreach i [glob -tails -nocomplain \
670 -directory [file join $objdir pack] *] {
671 lappend tolink [file join pack $i]
673 $o_cons update [incr bcur] $bcnt
674 update
676 foreach i $buckets {
677 file mkdir [file join .git objects $i]
678 foreach j [glob -tails -nocomplain \
679 -directory [file join $objdir $i] *] {
680 lappend tolink [file join $i $j]
682 $o_cons update [incr bcur] $bcnt
683 update
685 $o_cons stop
687 if {$tolink eq {}} {
688 info_popup [strcat \
689 [mc "Nothing to clone from %s." $origin_url] \
690 "\n" \
691 [mc "The 'master' branch has not been initialized."] \
693 destroy $w_body
694 set done 1
695 return
698 set i [lindex $tolink 0]
699 if {[catch {
700 file link -hard \
701 [file join .git objects $i] \
702 [file join $objdir $i]
703 } err]} {
704 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
705 set i [_copy_files $this $objdir $tolink]
706 } else {
707 set i [_link_files $this $objdir [lrange $tolink 1 end]]
709 if {!$i} return
711 destroy $w_body
713 full {
714 set o_cons [console::embed \
715 $w_body \
716 [mc "Cloning from %s" $origin_url]]
717 pack $w_body -fill both -expand 1 -padx 10
718 $o_cons exec \
719 [list git fetch --no-tags -k $origin_name] \
720 [cb _do_clone_tags]
722 shared {
723 set fd [open [gitdir objects info alternates] w]
724 fconfigure $fd -translation binary
725 puts $fd $objdir
726 close $fd
730 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
731 if {![_clone_refs $this]} return
732 set pwd [pwd]
733 if {[catch {
734 cd $origin_url
735 set HEAD [git rev-parse --verify HEAD^0]
736 } err]} {
737 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
738 return 0
740 cd $pwd
741 _do_clone_checkout $this $HEAD
745 method _copy_files {objdir tocopy} {
746 $o_cons start \
747 [mc "Copying objects"] \
748 [mc "KiB"]
749 set tot 0
750 set cmp 0
751 foreach p $tocopy {
752 incr tot [file size [file join $objdir $p]]
754 foreach p $tocopy {
755 if {[catch {
756 set f_in [open [file join $objdir $p] r]
757 set f_cp [open [file join .git objects $p] w]
758 fconfigure $f_in -translation binary -encoding binary
759 fconfigure $f_cp -translation binary -encoding binary
761 while {![eof $f_in]} {
762 incr cmp [fcopy $f_in $f_cp -size 16384]
763 $o_cons update \
764 [expr {$cmp / 1024}] \
765 [expr {$tot / 1024}]
766 update
769 close $f_in
770 close $f_cp
771 } err]} {
772 _clone_failed $this [mc "Unable to copy object: %s" $err]
773 return 0
776 return 1
779 method _link_files {objdir tolink} {
780 set total [llength $tolink]
781 $o_cons start \
782 [mc "Linking objects"] \
783 [mc "objects"]
784 for {set i 0} {$i < $total} {} {
785 set p [lindex $tolink $i]
786 if {[catch {
787 file link -hard \
788 [file join .git objects $p] \
789 [file join $objdir $p]
790 } err]} {
791 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
792 return 0
795 incr i
796 if {$i % 5 == 0} {
797 $o_cons update $i $total
798 update
801 return 1
804 method _clone_refs {} {
805 set pwd [pwd]
806 if {[catch {cd $origin_url} err]} {
807 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
808 return 0
810 set fd_in [git_read for-each-ref \
811 --tcl \
812 {--format=list %(refname) %(objectname) %(*objectname)}]
813 cd $pwd
815 set fd [open [gitdir packed-refs] w]
816 fconfigure $fd -translation binary
817 puts $fd "# pack-refs with: peeled"
818 while {[gets $fd_in line] >= 0} {
819 set line [eval $line]
820 set refn [lindex $line 0]
821 set robj [lindex $line 1]
822 set tobj [lindex $line 2]
824 if {[regsub ^refs/heads/ $refn \
825 "refs/remotes/$origin_name/" refn]} {
826 puts $fd "$robj $refn"
827 } elseif {[string match refs/tags/* $refn]} {
828 puts $fd "$robj $refn"
829 if {$tobj ne {}} {
830 puts $fd "^$tobj"
834 close $fd_in
835 close $fd
836 return 1
839 method _do_clone_tags {ok} {
840 if {$ok} {
841 $o_cons exec \
842 [list git fetch --tags -k $origin_name] \
843 [cb _do_clone_HEAD]
844 } else {
845 $o_cons done $ok
846 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
850 method _do_clone_HEAD {ok} {
851 if {$ok} {
852 $o_cons exec \
853 [list git fetch $origin_name HEAD] \
854 [cb _do_clone_full_end]
855 } else {
856 $o_cons done $ok
857 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
861 method _do_clone_full_end {ok} {
862 $o_cons done $ok
864 if {$ok} {
865 destroy $w_body
867 set HEAD {}
868 if {[file exists [gitdir FETCH_HEAD]]} {
869 set fd [open [gitdir FETCH_HEAD] r]
870 while {[gets $fd line] >= 0} {
871 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
872 break
875 close $fd
878 catch {git pack-refs}
879 _do_clone_checkout $this $HEAD
880 } else {
881 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
885 method _clone_failed {{why {}}} {
886 if {[catch {file delete -force $local_path} err]} {
887 set why [strcat \
888 $why \
889 "\n\n" \
890 [mc "Unable to cleanup %s" $local_path] \
891 "\n\n" \
892 $err]
894 if {$why ne {}} {
895 update
896 error_popup [strcat [mc "Clone failed."] "\n" $why]
900 method _do_clone_checkout {HEAD} {
901 if {$HEAD eq {}} {
902 info_popup [strcat \
903 [mc "No default branch obtained."] \
904 "\n" \
905 [mc "The 'master' branch has not been initialized."] \
907 set done 1
908 return
910 if {[catch {
911 git update-ref HEAD $HEAD^0
912 } err]} {
913 info_popup [strcat \
914 [mc "Cannot resolve %s as a commit." $HEAD^0] \
915 "\n $err" \
916 "\n" \
917 [mc "The 'master' branch has not been initialized."] \
919 set done 1
920 return
923 set o_cons [status_bar::two_line $w_body]
924 pack $w_body -fill x -padx 10 -pady 10
925 $o_cons start \
926 [mc "Creating working directory"] \
927 [mc "files"]
929 set readtree_err {}
930 set fd [git_read --stderr read-tree \
931 -m \
932 -u \
933 -v \
934 HEAD \
935 HEAD \
937 fconfigure $fd -blocking 0 -translation binary
938 fileevent $fd readable [cb _readtree_wait $fd]
941 method _readtree_wait {fd} {
942 set buf [read $fd]
943 $o_cons update_meter $buf
944 append readtree_err $buf
946 fconfigure $fd -blocking 1
947 if {![eof $fd]} {
948 fconfigure $fd -blocking 0
949 return
952 if {[catch {close $fd}]} {
953 set err $readtree_err
954 regsub {^fatal: } $err {} err
955 error_popup [strcat \
956 [mc "Initial file checkout failed."] \
957 "\n\n$err"]
958 return
961 set done 1
964 ######################################################################
966 ## Open Existing Repository
968 method _do_open {} {
969 $w_next conf \
970 -state disabled \
971 -command [cb _do_open2] \
972 -text [mc "Open"]
974 frame $w_body
975 label $w_body.h \
976 -font font_uibold \
977 -text [mc "Open Existing Repository"]
978 pack $w_body.h -side top -fill x -pady 10
979 pack $w_body -fill x -padx 10
981 frame $w_body.where
982 label $w_body.where.l -text [mc "Repository:"]
983 entry $w_body.where.t \
984 -textvariable @local_path \
985 -borderwidth 1 \
986 -relief sunken \
987 -width 50
988 button $w_body.where.b \
989 -text [mc "Browse"] \
990 -command [cb _open_local_path]
992 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
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]]
997 update
998 focus $w_body.where.t
1001 method _open_local_path {} {
1002 if {$local_path ne {}} {
1003 set p $local_path
1004 } else {
1005 set p [_home]
1008 set p [tk_chooseDirectory \
1009 -initialdir $p \
1010 -parent $top \
1011 -title [mc "Git Repository"] \
1012 -mustexist true]
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]]
1018 return
1020 set local_path $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]]
1026 return
1029 if {[catch {cd $local_path} err]} {
1030 error_popup [strcat \
1031 [mc "Failed to open repository %s:" $local_path] \
1032 "\n\n$err"]
1033 return
1036 _append_recentrepos [pwd]
1037 set ::_gitdir .git
1038 set ::_prefix {}
1039 set done 1