t5332-multi-pack-reuse.sh: demonstrate duplicate packing failure
[git/gitster.git] / git-gui / lib / choose_repository.tcl
blobd23abedcb36fd93ab3f12694d607bf354d6cf208
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)
13 # Status mega-widget instance during _do_clone2 (used by _copy_files and
14 # _link_files). Widget is destroyed before _do_clone2 calls
15 # _do_clone_checkout
16 field o_status
18 # Operation displayed by status mega-widget during _do_clone_checkout =>
19 # _readtree_wait => _postcheckout_wait => _do_clone_submodules =>
20 # _do_validate_submodule_cloning. The status mega-widget is a different
21 # instance than that stored in $o_status in earlier operations.
22 field o_status_op
24 field w_types ; # List of type buttons in clone
25 field w_recentlist ; # Listbox containing recent repositories
26 field w_localpath ; # Entry widget bound to local_path
28 field done 0 ; # Finished picking the repository?
29 field local_path {} ; # Where this repository is locally
30 field origin_url {} ; # Where we are cloning from
31 field origin_name origin ; # What we shall call 'origin'
32 field clone_type hardlink ; # Type of clone to construct
33 field recursive true ; # Recursive cloning flag
34 field readtree_err ; # Error output from read-tree (if any)
35 field sorted_recent ; # recent repositories (sorted)
37 constructor pick {} {
38 global M1T M1B use_ttk NS
40 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
41 set maxrecent 10
44 make_dialog top w
45 wm title $top [mc "Git Gui"]
47 if {$top eq {.}} {
48 menu $w.mbar -tearoff 0
49 $top configure -menu $w.mbar
51 set m_repo $w.mbar.repository
52 $w.mbar add cascade \
53 -label [mc Repository] \
54 -menu $m_repo
55 menu $m_repo
57 if {[is_MacOSX]} {
58 $w.mbar add cascade -label Apple -menu .mbar.apple
59 menu $w.mbar.apple
60 $w.mbar.apple add command \
61 -label [mc "About %s" [appname]] \
62 -command do_about
63 $w.mbar.apple add command \
64 -label [mc "Show SSH Key"] \
65 -command do_ssh_key
66 } else {
67 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
68 menu $w.mbar.help
69 $w.mbar.help add command \
70 -label [mc "About %s" [appname]] \
71 -command do_about
72 $w.mbar.help add command \
73 -label [mc "Show SSH Key"] \
74 -command do_ssh_key
77 wm protocol $top WM_DELETE_WINDOW exit
78 bind $top <$M1B-q> exit
79 bind $top <$M1B-Q> exit
80 bind $top <Key-Escape> exit
81 } else {
82 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
83 bind $top <Key-Escape> [list destroy $top]
84 set m_repo {}
87 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
89 set w_body $w.body
90 set opts $w_body.options
91 ${NS}::frame $w_body
92 text $opts \
93 -cursor $::cursor_ptr \
94 -relief flat \
95 -background [get_bg_color $w_body] \
96 -wrap none \
97 -spacing1 5 \
98 -width 50 \
99 -height 3
100 pack $opts -anchor w -fill x
102 $opts tag conf link_new -foreground blue -underline 1
103 $opts tag bind link_new <1> [cb _next new]
104 $opts insert end [mc "Create New Repository"] link_new
105 $opts insert end "\n"
106 if {$m_repo ne {}} {
107 $m_repo add command \
108 -command [cb _next new] \
109 -accelerator $M1T-N \
110 -label [mc "New..."]
111 bind $top <$M1B-n> [cb _next new]
112 bind $top <$M1B-N> [cb _next new]
115 $opts tag conf link_clone -foreground blue -underline 1
116 $opts tag bind link_clone <1> [cb _next clone]
117 $opts insert end [mc "Clone Existing Repository"] link_clone
118 $opts insert end "\n"
119 if {$m_repo ne {}} {
120 if {[tk windowingsystem] eq "win32"} {
121 set key L
122 } else {
123 set key C
125 $m_repo add command \
126 -command [cb _next clone] \
127 -accelerator $M1T-$key \
128 -label [mc "Clone..."]
129 bind $top <$M1B-[string tolower $key]> [cb _next clone]
130 bind $top <$M1B-[string toupper $key]> [cb _next clone]
133 $opts tag conf link_open -foreground blue -underline 1
134 $opts tag bind link_open <1> [cb _next open]
135 $opts insert end [mc "Open Existing Repository"] link_open
136 $opts insert end "\n"
137 if {$m_repo ne {}} {
138 $m_repo add command \
139 -command [cb _next open] \
140 -accelerator $M1T-O \
141 -label [mc "Open..."]
142 bind $top <$M1B-o> [cb _next open]
143 bind $top <$M1B-O> [cb _next open]
146 $opts conf -state disabled
148 set sorted_recent [_get_recentrepos]
149 if {[llength $sorted_recent] > 0} {
150 if {$m_repo ne {}} {
151 $m_repo add separator
152 $m_repo add command \
153 -state disabled \
154 -label [mc "Recent Repositories"]
157 if {[set lenrecent [llength $sorted_recent]] < $maxrecent} {
158 set lenrecent $maxrecent
161 ${NS}::label $w_body.space
162 ${NS}::label $w_body.recentlabel \
163 -anchor w \
164 -text [mc "Open Recent Repository:"]
165 set w_recentlist $w_body.recentlist
166 text $w_recentlist \
167 -cursor $::cursor_ptr \
168 -relief flat \
169 -background [get_bg_color $w_body.recentlabel] \
170 -wrap none \
171 -width 50 \
172 -height $lenrecent
173 $w_recentlist tag conf link \
174 -foreground blue \
175 -underline 1
176 set home $::env(HOME)
177 set home "[file normalize $home]/"
178 set hlen [string length $home]
179 foreach p $sorted_recent {
180 set path $p
181 if {[string equal -length $hlen $home $p]} {
182 set p "~/[string range $p $hlen end]"
184 regsub -all "\n" $p "\\n" p
185 $w_recentlist insert end $p link
186 $w_recentlist insert end "\n"
188 if {$m_repo ne {}} {
189 $m_repo add command \
190 -command [cb _open_recent_path $path] \
191 -label " $p"
194 $w_recentlist conf -state disabled
195 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
196 pack $w_body.space -anchor w -fill x
197 pack $w_body.recentlabel -anchor w -fill x
198 pack $w_recentlist -anchor w -fill x
200 pack $w_body -fill x -padx 10 -pady 10
202 ${NS}::frame $w.buttons
203 set w_next $w.buttons.next
204 set w_quit $w.buttons.quit
205 ${NS}::button $w_quit \
206 -text [mc "Quit"] \
207 -command exit
208 pack $w_quit -side right -padx 5
209 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
211 if {$m_repo ne {}} {
212 $m_repo add separator
213 $m_repo add command \
214 -label [mc Quit] \
215 -command exit \
216 -accelerator $M1T-Q
219 bind $top <Return> [cb _invoke_next]
220 bind $top <Visibility> "
221 [cb _center]
222 grab $top
223 focus $top
224 bind $top <Visibility> {}
226 wm deiconify $top
227 tkwait variable @done
229 grab release $top
230 if {$top eq {.}} {
231 eval destroy [winfo children $top]
235 method _center {} {
236 set nx [winfo reqwidth $top]
237 set ny [winfo reqheight $top]
238 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
239 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
240 wm geometry $top [format {+%d+%d} $rx $ry]
243 method _invoke_next {} {
244 if {[winfo exists $w_next]} {
245 uplevel #0 [$w_next cget -command]
249 proc _get_recentrepos {} {
250 set recent [list]
251 foreach p [lsort -unique [get_config gui.recentrepo]] {
252 if {[_is_git [file join $p .git]]} {
253 lappend recent $p
254 } else {
255 _unset_recentrepo $p
258 return $recent
261 proc _unset_recentrepo {p} {
262 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
263 catch {git config --global --unset-all gui.recentrepo "^$p\$"}
264 load_config 1
267 proc _append_recentrepos {path} {
268 set path [file normalize $path]
269 set recent [get_config gui.recentrepo]
271 if {[lindex $recent end] eq $path} {
272 return
275 set i [lsearch $recent $path]
276 if {$i >= 0} {
277 _unset_recentrepo $path
280 git config --global --add gui.recentrepo $path
281 load_config 1
282 set recent [get_config gui.recentrepo]
284 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
285 set maxrecent 10
288 while {[llength $recent] > $maxrecent} {
289 _unset_recentrepo [lindex $recent 0]
290 set recent [get_config gui.recentrepo]
294 method _open_recent {xy} {
295 set id [lindex [split [$w_recentlist index @$xy] .] 0]
296 set local_path [lindex $sorted_recent [expr {$id - 1}]]
297 _do_open2 $this
300 method _open_recent_path {p} {
301 set local_path $p
302 _do_open2 $this
305 method _next {action} {
306 global NS
307 destroy $w_body
308 if {![winfo exists $w_next]} {
309 ${NS}::button $w_next -default active
310 set pos -before
311 if {[tk windowingsystem] eq "win32"} { set pos -after }
312 pack $w_next -side right -padx 5 $pos $w_quit
314 _do_$action $this
317 method _write_local_path {args} {
318 if {$local_path eq {}} {
319 $w_next conf -state disabled
320 } else {
321 $w_next conf -state normal
325 method _git_init {} {
326 if {[catch {file mkdir $local_path} err]} {
327 error_popup [strcat \
328 [mc "Failed to create repository %s:" $local_path] \
329 "\n\n$err"]
330 return 0
333 if {[catch {cd $local_path} err]} {
334 error_popup [strcat \
335 [mc "Failed to create repository %s:" $local_path] \
336 "\n\n$err"]
337 return 0
340 if {[catch {git init} err]} {
341 error_popup [strcat \
342 [mc "Failed to create repository %s:" $local_path] \
343 "\n\n$err"]
344 return 0
347 _append_recentrepos [pwd]
348 set ::_gitdir .git
349 set ::_prefix {}
350 return 1
353 proc _is_git {path {outdir_var ""}} {
354 if {$outdir_var ne ""} {
355 upvar 1 $outdir_var outdir
357 if {[catch {set outdir [git rev-parse --resolve-git-dir $path]}]} {
358 return 0
360 return 1
363 proc _objdir {path} {
364 set objdir [file join $path .git objects]
365 if {[file isdirectory $objdir]} {
366 return $objdir
369 set objdir [file join $path objects]
370 if {[file isdirectory $objdir]} {
371 return $objdir
374 return {}
377 ######################################################################
379 ## Create New Repository
381 method _do_new {} {
382 global use_ttk NS
383 $w_next conf \
384 -state disabled \
385 -command [cb _do_new2] \
386 -text [mc "Create"]
388 ${NS}::frame $w_body
389 ${NS}::label $w_body.h \
390 -font font_uibold -anchor center \
391 -text [mc "Create New Repository"]
392 pack $w_body.h -side top -fill x -pady 10
393 pack $w_body -fill x -padx 10
395 ${NS}::frame $w_body.where
396 ${NS}::label $w_body.where.l -text [mc "Directory:"]
397 ${NS}::entry $w_body.where.t \
398 -textvariable @local_path \
399 -width 50
400 ${NS}::button $w_body.where.b \
401 -text [mc "Browse"] \
402 -command [cb _new_local_path]
403 set w_localpath $w_body.where.t
405 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
406 pack $w_body.where -fill x
408 grid columnconfigure $w_body.where 1 -weight 1
410 trace add variable @local_path write [cb _write_local_path]
411 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
412 update
413 focus $w_body.where.t
416 method _new_local_path {} {
417 if {$local_path ne {}} {
418 set p [file dirname $local_path]
419 } else {
420 set p [pwd]
423 set p [tk_chooseDirectory \
424 -initialdir $p \
425 -parent $top \
426 -title [mc "Git Repository"] \
427 -mustexist false]
428 if {$p eq {}} return
430 set p [file normalize $p]
431 if {![_new_ok $p]} {
432 return
434 set local_path $p
435 $w_localpath icursor end
438 method _do_new2 {} {
439 if {![_new_ok $local_path]} {
440 return
442 if {![_git_init $this]} {
443 return
445 set done 1
448 proc _new_ok {p} {
449 if {[file isdirectory $p]} {
450 if {[_is_git [file join $p .git]]} {
451 error_popup [mc "Directory %s already exists." $p]
452 return 0
454 } elseif {[file exists $p]} {
455 error_popup [mc "File %s already exists." $p]
456 return 0
458 return 1
461 ######################################################################
463 ## Clone Existing Repository
465 method _do_clone {} {
466 global use_ttk NS
467 $w_next conf \
468 -state disabled \
469 -command [cb _do_clone2] \
470 -text [mc "Clone"]
472 ${NS}::frame $w_body
473 ${NS}::label $w_body.h \
474 -font font_uibold -anchor center \
475 -text [mc "Clone Existing Repository"]
476 pack $w_body.h -side top -fill x -pady 10
477 pack $w_body -fill x -padx 10
479 set args $w_body.args
480 ${NS}::frame $w_body.args
481 pack $args -fill both
483 ${NS}::label $args.origin_l -text [mc "Source Location:"]
484 ${NS}::entry $args.origin_t \
485 -textvariable @origin_url \
486 -width 50
487 ${NS}::button $args.origin_b \
488 -text [mc "Browse"] \
489 -command [cb _open_origin]
490 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
492 ${NS}::label $args.where_l -text [mc "Target Directory:"]
493 ${NS}::entry $args.where_t \
494 -textvariable @local_path \
495 -width 50
496 ${NS}::button $args.where_b \
497 -text [mc "Browse"] \
498 -command [cb _new_local_path]
499 grid $args.where_l $args.where_t $args.where_b -sticky ew
500 set w_localpath $args.where_t
502 ${NS}::label $args.type_l -text [mc "Clone Type:"]
503 ${NS}::frame $args.type_f
504 set w_types [list]
505 lappend w_types [${NS}::radiobutton $args.type_f.hardlink \
506 -state disabled \
507 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
508 -variable @clone_type \
509 -value hardlink]
510 lappend w_types [${NS}::radiobutton $args.type_f.full \
511 -state disabled \
512 -text [mc "Full Copy (Slower, Redundant Backup)"] \
513 -variable @clone_type \
514 -value full]
515 lappend w_types [${NS}::radiobutton $args.type_f.shared \
516 -state disabled \
517 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
518 -variable @clone_type \
519 -value shared]
520 foreach r $w_types {
521 pack $r -anchor w
523 ${NS}::checkbutton $args.type_f.recursive \
524 -text [mc "Recursively clone submodules too"] \
525 -variable @recursive \
526 -onvalue true -offvalue false
527 pack $args.type_f.recursive -anchor w
528 grid $args.type_l $args.type_f -sticky new
530 grid columnconfigure $args 1 -weight 1
532 trace add variable @local_path write [cb _update_clone]
533 trace add variable @origin_url write [cb _update_clone]
534 bind $w_body.h <Destroy> "
535 [list trace remove variable @local_path write [cb _update_clone]]
536 [list trace remove variable @origin_url write [cb _update_clone]]
538 update
539 focus $args.origin_t
542 method _open_origin {} {
543 if {$origin_url ne {} && [file isdirectory $origin_url]} {
544 set p $origin_url
545 } else {
546 set p [pwd]
549 set p [tk_chooseDirectory \
550 -initialdir $p \
551 -parent $top \
552 -title [mc "Git Repository"] \
553 -mustexist true]
554 if {$p eq {}} return
556 set p [file normalize $p]
557 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
558 error_popup [mc "Not a Git repository: %s" [file tail $p]]
559 return
561 set origin_url $p
564 method _update_clone {args} {
565 if {$local_path ne {} && $origin_url ne {}} {
566 $w_next conf -state normal
567 } else {
568 $w_next conf -state disabled
571 if {$origin_url ne {} &&
572 ( [_is_git [file join $origin_url .git]]
573 || [_is_git $origin_url])} {
574 set e normal
575 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
576 set clone_type hardlink
578 } else {
579 set e disabled
580 set clone_type full
583 foreach r $w_types {
584 $r conf -state $e
588 method _do_clone2 {} {
589 if {[file isdirectory $origin_url]} {
590 set origin_url [file normalize $origin_url]
593 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
594 error_popup [mc "Standard only available for local repository."]
595 return
597 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
598 error_popup [mc "Shared only available for local repository."]
599 return
602 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
603 set objdir [_objdir $origin_url]
604 if {$objdir eq {}} {
605 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
606 return
610 set giturl $origin_url
612 if {[file exists $local_path]} {
613 error_popup [mc "Location %s already exists." $local_path]
614 return
617 if {![_git_init $this]} return
618 set local_path [pwd]
620 if {[catch {
621 git config remote.$origin_name.url $giturl
622 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
623 } err]} {
624 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
625 return
628 destroy $w_body $w_next
630 switch -exact -- $clone_type {
631 hardlink {
632 set o_status [status_bar::two_line $w_body]
633 pack $w_body -fill x -padx 10 -pady 10
635 set status_op [$o_status start \
636 [mc "Counting objects"] \
637 [mc "buckets"]]
638 update
640 if {[file exists [file join $objdir info alternates]]} {
641 set pwd [pwd]
642 if {[catch {
643 file mkdir [gitdir objects info]
644 set f_in [open [file join $objdir info alternates] r]
645 set f_cp [open [gitdir objects info alternates] w]
646 fconfigure $f_in -translation binary -encoding binary
647 fconfigure $f_cp -translation binary -encoding binary
648 cd $objdir
649 while {[gets $f_in line] >= 0} {
650 puts $f_cp [file normalize $line]
652 close $f_in
653 close $f_cp
654 cd $pwd
655 } err]} {
656 catch {cd $pwd}
657 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
658 $status_op stop
659 return
663 set tolink [list]
664 set buckets [glob \
665 -tails \
666 -nocomplain \
667 -directory [file join $objdir] ??]
668 set bcnt [expr {[llength $buckets] + 2}]
669 set bcur 1
670 $status_op update $bcur $bcnt
671 update
673 file mkdir [file join .git objects pack]
674 foreach i [glob -tails -nocomplain \
675 -directory [file join $objdir pack] *] {
676 lappend tolink [file join pack $i]
678 $status_op update [incr bcur] $bcnt
679 update
681 foreach i $buckets {
682 file mkdir [file join .git objects $i]
683 foreach j [glob -tails -nocomplain \
684 -directory [file join $objdir $i] *] {
685 lappend tolink [file join $i $j]
687 $status_op update [incr bcur] $bcnt
688 update
690 $status_op stop
692 if {$tolink eq {}} {
693 info_popup [strcat \
694 [mc "Nothing to clone from %s." $origin_url] \
695 "\n" \
696 [mc "The 'master' branch has not been initialized."] \
698 destroy $w_body
699 set done 1
700 return
703 set i [lindex $tolink 0]
704 if {[catch {
705 file link -hard \
706 [file join .git objects $i] \
707 [file join $objdir $i]
708 } err]} {
709 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
710 set i [_copy_files $this $objdir $tolink]
711 } else {
712 set i [_link_files $this $objdir [lrange $tolink 1 end]]
714 if {!$i} return
716 destroy $w_body
718 set o_status {}
720 full {
721 set o_cons [console::embed \
722 $w_body \
723 [mc "Cloning from %s" $origin_url]]
724 pack $w_body -fill both -expand 1 -padx 10
725 $o_cons exec \
726 [list git fetch --no-tags -k $origin_name] \
727 [cb _do_clone_tags]
729 shared {
730 set fd [open [gitdir objects info alternates] w]
731 fconfigure $fd -translation binary
732 puts $fd $objdir
733 close $fd
737 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
738 if {![_clone_refs $this]} return
739 set pwd [pwd]
740 if {[catch {
741 cd $origin_url
742 set HEAD [git rev-parse --verify HEAD^0]
743 } err]} {
744 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
745 return 0
747 cd $pwd
748 _do_clone_checkout $this $HEAD
752 method _copy_files {objdir tocopy} {
753 set status_op [$o_status start \
754 [mc "Copying objects"] \
755 [mc "KiB"]]
756 set tot 0
757 set cmp 0
758 foreach p $tocopy {
759 incr tot [file size [file join $objdir $p]]
761 foreach p $tocopy {
762 if {[catch {
763 set f_in [open [file join $objdir $p] r]
764 set f_cp [open [file join .git objects $p] w]
765 fconfigure $f_in -translation binary -encoding binary
766 fconfigure $f_cp -translation binary -encoding binary
768 while {![eof $f_in]} {
769 incr cmp [fcopy $f_in $f_cp -size 16384]
770 $status_op update \
771 [expr {$cmp / 1024}] \
772 [expr {$tot / 1024}]
773 update
776 close $f_in
777 close $f_cp
778 } err]} {
779 _clone_failed $this [mc "Unable to copy object: %s" $err]
780 $status_op stop
781 return 0
784 $status_op stop
785 return 1
788 method _link_files {objdir tolink} {
789 set total [llength $tolink]
790 set status_op [$o_status start \
791 [mc "Linking objects"] \
792 [mc "objects"]]
793 for {set i 0} {$i < $total} {} {
794 set p [lindex $tolink $i]
795 if {[catch {
796 file link -hard \
797 [file join .git objects $p] \
798 [file join $objdir $p]
799 } err]} {
800 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
801 $status_op stop
802 return 0
805 incr i
806 if {$i % 5 == 0} {
807 $status_op update $i $total
808 update
811 $status_op stop
812 return 1
815 method _clone_refs {} {
816 set pwd [pwd]
817 if {[catch {cd $origin_url} err]} {
818 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
819 return 0
821 set fd_in [git_read for-each-ref \
822 --tcl \
823 {--format=list %(refname) %(objectname) %(*objectname)}]
824 cd $pwd
826 set fd [open [gitdir packed-refs] w]
827 fconfigure $fd -translation binary
828 puts $fd "# pack-refs with: peeled"
829 while {[gets $fd_in line] >= 0} {
830 set line [eval $line]
831 set refn [lindex $line 0]
832 set robj [lindex $line 1]
833 set tobj [lindex $line 2]
835 if {[regsub ^refs/heads/ $refn \
836 "refs/remotes/$origin_name/" refn]} {
837 puts $fd "$robj $refn"
838 } elseif {[string match refs/tags/* $refn]} {
839 puts $fd "$robj $refn"
840 if {$tobj ne {}} {
841 puts $fd "^$tobj"
845 close $fd_in
846 close $fd
847 return 1
850 method _do_clone_tags {ok} {
851 if {$ok} {
852 $o_cons exec \
853 [list git fetch --tags -k $origin_name] \
854 [cb _do_clone_HEAD]
855 } else {
856 $o_cons done $ok
857 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
861 method _do_clone_HEAD {ok} {
862 if {$ok} {
863 $o_cons exec \
864 [list git fetch $origin_name HEAD] \
865 [cb _do_clone_full_end]
866 } else {
867 $o_cons done $ok
868 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
872 method _do_clone_full_end {ok} {
873 $o_cons done $ok
875 if {$ok} {
876 destroy $w_body
878 set HEAD {}
879 if {[file exists [gitdir FETCH_HEAD]]} {
880 set fd [open [gitdir FETCH_HEAD] r]
881 while {[gets $fd line] >= 0} {
882 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
883 break
886 close $fd
889 catch {git pack-refs}
890 _do_clone_checkout $this $HEAD
891 } else {
892 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
896 method _clone_failed {{why {}}} {
897 if {[catch {file delete -force $local_path} err]} {
898 set why [strcat \
899 $why \
900 "\n\n" \
901 [mc "Unable to cleanup %s" $local_path] \
902 "\n\n" \
903 $err]
905 if {$why ne {}} {
906 update
907 error_popup [strcat [mc "Clone failed."] "\n" $why]
911 method _do_clone_checkout {HEAD} {
912 if {$HEAD eq {}} {
913 info_popup [strcat \
914 [mc "No default branch obtained."] \
915 "\n" \
916 [mc "The 'master' branch has not been initialized."] \
918 set done 1
919 return
921 if {[catch {
922 git update-ref HEAD $HEAD^0
923 } err]} {
924 info_popup [strcat \
925 [mc "Cannot resolve %s as a commit." $HEAD^0] \
926 "\n $err" \
927 "\n" \
928 [mc "The 'master' branch has not been initialized."] \
930 set done 1
931 return
934 set status [status_bar::two_line $w_body]
935 pack $w_body -fill x -padx 10 -pady 10
937 # We start the status operation here.
939 # This function calls _readtree_wait as a callback.
941 # _readtree_wait in turn either calls _do_clone_submodules directly,
942 # or calls _postcheckout_wait as a callback which then calls
943 # _do_clone_submodules.
945 # _do_clone_submodules calls _do_validate_submodule_cloning.
947 # _do_validate_submodule_cloning stops the status operation.
949 # There are no other calls into this chain from other code.
951 set o_status_op [$status start \
952 [mc "Creating working directory"] \
953 [mc "files"]]
955 set readtree_err {}
956 set fd [git_read --stderr read-tree \
957 -m \
958 -u \
959 -v \
960 HEAD \
961 HEAD \
963 fconfigure $fd -blocking 0 -translation binary
964 fileevent $fd readable [cb _readtree_wait $fd]
967 method _readtree_wait {fd} {
968 set buf [read $fd]
969 $o_status_op update_meter $buf
970 append readtree_err $buf
972 fconfigure $fd -blocking 1
973 if {![eof $fd]} {
974 fconfigure $fd -blocking 0
975 return
978 if {[catch {close $fd}]} {
979 set err $readtree_err
980 regsub {^fatal: } $err {} err
981 error_popup [strcat \
982 [mc "Initial file checkout failed."] \
983 "\n\n$err"]
984 return
987 # -- Run the post-checkout hook.
989 set fd_ph [githook_read post-checkout [string repeat 0 40] \
990 [git rev-parse HEAD] 1]
991 if {$fd_ph ne {}} {
992 global pch_error
993 set pch_error {}
994 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
995 fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
996 } else {
997 _do_clone_submodules $this
1001 method _postcheckout_wait {fd_ph} {
1002 global pch_error
1004 append pch_error [read $fd_ph]
1005 fconfigure $fd_ph -blocking 1
1006 if {[eof $fd_ph]} {
1007 if {[catch {close $fd_ph}]} {
1008 hook_failed_popup post-checkout $pch_error 0
1010 unset pch_error
1011 _do_clone_submodules $this
1012 return
1014 fconfigure $fd_ph -blocking 0
1017 method _do_clone_submodules {} {
1018 if {$recursive eq {true}} {
1019 $o_status_op stop
1020 set o_status_op {}
1022 destroy $w_body
1024 set o_cons [console::embed \
1025 $w_body \
1026 [mc "Cloning submodules"]]
1027 pack $w_body -fill both -expand 1 -padx 10
1028 $o_cons exec \
1029 [list git submodule update --init --recursive] \
1030 [cb _do_validate_submodule_cloning]
1031 } else {
1032 set done 1
1036 method _do_validate_submodule_cloning {ok} {
1037 if {$ok} {
1038 $o_cons done $ok
1039 set done 1
1040 } else {
1041 _clone_failed $this [mc "Cannot clone submodules."]
1045 ######################################################################
1047 ## Open Existing Repository
1049 method _do_open {} {
1050 global NS
1051 $w_next conf \
1052 -state disabled \
1053 -command [cb _do_open2] \
1054 -text [mc "Open"]
1056 ${NS}::frame $w_body
1057 ${NS}::label $w_body.h \
1058 -font font_uibold -anchor center \
1059 -text [mc "Open Existing Repository"]
1060 pack $w_body.h -side top -fill x -pady 10
1061 pack $w_body -fill x -padx 10
1063 ${NS}::frame $w_body.where
1064 ${NS}::label $w_body.where.l -text [mc "Repository:"]
1065 ${NS}::entry $w_body.where.t \
1066 -textvariable @local_path \
1067 -width 50
1068 ${NS}::button $w_body.where.b \
1069 -text [mc "Browse"] \
1070 -command [cb _open_local_path]
1072 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
1073 pack $w_body.where -fill x
1075 grid columnconfigure $w_body.where 1 -weight 1
1077 trace add variable @local_path write [cb _write_local_path]
1078 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
1079 update
1080 focus $w_body.where.t
1083 method _open_local_path {} {
1084 if {$local_path ne {}} {
1085 set p $local_path
1086 } else {
1087 set p [pwd]
1090 set p [tk_chooseDirectory \
1091 -initialdir $p \
1092 -parent $top \
1093 -title [mc "Git Repository"] \
1094 -mustexist true]
1095 if {$p eq {}} return
1097 set p [file normalize $p]
1098 if {![_is_git [file join $p .git]]} {
1099 error_popup [mc "Not a Git repository: %s" [file tail $p]]
1100 return
1102 set local_path $p
1105 method _do_open2 {} {
1106 if {![_is_git [file join $local_path .git] actualgit]} {
1107 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
1108 return
1111 if {[catch {cd $local_path} err]} {
1112 error_popup [strcat \
1113 [mc "Failed to open repository %s:" $local_path] \
1114 "\n\n$err"]
1115 return
1118 _append_recentrepos [pwd]
1119 set ::_gitdir $actualgit
1120 set ::_prefix {}
1121 set done 1