git-gui: right half window is paned
[git-gui.git] / lib / choose_repository.tcl
blob3c10bc656d207553c8c093abcca275dd253a57fb
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 use_ttk NS
27 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
28 set maxrecent 10
31 make_dialog top w
32 wm title $top [mc "Git Gui"]
34 if {$top eq {.}} {
35 menu $w.mbar -tearoff 0
36 $top configure -menu $w.mbar
38 set m_repo $w.mbar.repository
39 $w.mbar add cascade \
40 -label [mc Repository] \
41 -menu $m_repo
42 menu $m_repo
44 if {[is_MacOSX]} {
45 $w.mbar add cascade -label Apple -menu .mbar.apple
46 menu $w.mbar.apple
47 $w.mbar.apple add command \
48 -label [mc "About %s" [appname]] \
49 -command do_about
50 $w.mbar.apple add command \
51 -label [mc "Show SSH Key"] \
52 -command do_ssh_key
53 } else {
54 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
55 menu $w.mbar.help
56 $w.mbar.help add command \
57 -label [mc "About %s" [appname]] \
58 -command do_about
59 $w.mbar.help add command \
60 -label [mc "Show SSH Key"] \
61 -command do_ssh_key
64 wm protocol $top WM_DELETE_WINDOW exit
65 bind $top <$M1B-q> exit
66 bind $top <$M1B-Q> exit
67 bind $top <Key-Escape> exit
68 } else {
69 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
70 bind $top <Key-Escape> [list destroy $top]
71 set m_repo {}
74 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
76 set w_body $w.body
77 set opts $w_body.options
78 ${NS}::frame $w_body
79 text $opts \
80 -cursor $::cursor_ptr \
81 -relief flat \
82 -background [get_bg_color $w_body] \
83 -wrap none \
84 -spacing1 5 \
85 -width 50 \
86 -height 3
87 pack $opts -anchor w -fill x
89 $opts tag conf link_new -foreground blue -underline 1
90 $opts tag bind link_new <1> [cb _next new]
91 $opts insert end [mc "Create New Repository"] link_new
92 $opts insert end "\n"
93 if {$m_repo ne {}} {
94 $m_repo add command \
95 -command [cb _next new] \
96 -accelerator $M1T-N \
97 -label [mc "New..."]
98 bind $top <$M1B-n> [cb _next new]
99 bind $top <$M1B-N> [cb _next new]
102 $opts tag conf link_clone -foreground blue -underline 1
103 $opts tag bind link_clone <1> [cb _next clone]
104 $opts insert end [mc "Clone Existing Repository"] link_clone
105 $opts insert end "\n"
106 if {$m_repo ne {}} {
107 if {[tk windowingsystem] eq "win32"} {
108 set key L
109 } else {
110 set key C
112 $m_repo add command \
113 -command [cb _next clone] \
114 -accelerator $M1T-$key \
115 -label [mc "Clone..."]
116 bind $top <$M1B-[string tolower $key]> [cb _next clone]
117 bind $top <$M1B-[string toupper $key]> [cb _next clone]
120 $opts tag conf link_open -foreground blue -underline 1
121 $opts tag bind link_open <1> [cb _next open]
122 $opts insert end [mc "Open Existing Repository"] link_open
123 $opts insert end "\n"
124 if {$m_repo ne {}} {
125 $m_repo add command \
126 -command [cb _next open] \
127 -accelerator $M1T-O \
128 -label [mc "Open..."]
129 bind $top <$M1B-o> [cb _next open]
130 bind $top <$M1B-O> [cb _next open]
133 $opts conf -state disabled
135 set sorted_recent [_get_recentrepos]
136 if {[llength $sorted_recent] > 0} {
137 if {$m_repo ne {}} {
138 $m_repo add separator
139 $m_repo add command \
140 -state disabled \
141 -label [mc "Recent Repositories"]
144 ${NS}::label $w_body.space
145 ${NS}::label $w_body.recentlabel \
146 -anchor w \
147 -text [mc "Open Recent Repository:"]
148 set w_recentlist $w_body.recentlist
149 text $w_recentlist \
150 -cursor $::cursor_ptr \
151 -relief flat \
152 -background [get_bg_color $w_body.recentlabel] \
153 -wrap none \
154 -width 50 \
155 -height $maxrecent
156 $w_recentlist tag conf link \
157 -foreground blue \
158 -underline 1
159 set home $::env(HOME)
160 if {[is_Cygwin]} {
161 set home [exec cygpath --windows --absolute $home]
163 set home "[file normalize $home]/"
164 set hlen [string length $home]
165 foreach p $sorted_recent {
166 set path $p
167 if {[string equal -length $hlen $home $p]} {
168 set p "~/[string range $p $hlen end]"
170 regsub -all "\n" $p "\\n" p
171 $w_recentlist insert end $p link
172 $w_recentlist insert end "\n"
174 if {$m_repo ne {}} {
175 $m_repo add command \
176 -command [cb _open_recent_path $path] \
177 -label " $p"
180 $w_recentlist conf -state disabled
181 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
182 pack $w_body.space -anchor w -fill x
183 pack $w_body.recentlabel -anchor w -fill x
184 pack $w_recentlist -anchor w -fill x
186 pack $w_body -fill x -padx 10 -pady 10
188 ${NS}::frame $w.buttons
189 set w_next $w.buttons.next
190 set w_quit $w.buttons.quit
191 ${NS}::button $w_quit \
192 -text [mc "Quit"] \
193 -command exit
194 pack $w_quit -side right -padx 5
195 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
197 if {$m_repo ne {}} {
198 $m_repo add separator
199 $m_repo add command \
200 -label [mc Quit] \
201 -command exit \
202 -accelerator $M1T-Q
205 bind $top <Return> [cb _invoke_next]
206 bind $top <Visibility> "
207 [cb _center]
208 grab $top
209 focus $top
210 bind $top <Visibility> {}
212 wm deiconify $top
213 tkwait variable @done
215 grab release $top
216 if {$top eq {.}} {
217 eval destroy [winfo children $top]
221 method _center {} {
222 set nx [winfo reqwidth $top]
223 set ny [winfo reqheight $top]
224 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
225 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
226 wm geometry $top [format {+%d+%d} $rx $ry]
229 method _invoke_next {} {
230 if {[winfo exists $w_next]} {
231 uplevel #0 [$w_next cget -command]
235 proc _get_recentrepos {} {
236 set recent [list]
237 foreach p [get_config gui.recentrepo] {
238 if {[_is_git [file join $p .git]]} {
239 lappend recent $p
240 } else {
241 _unset_recentrepo $p
244 return [lsort $recent]
247 proc _unset_recentrepo {p} {
248 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
249 git config --global --unset gui.recentrepo "^$p\$"
250 load_config 1
253 proc _append_recentrepos {path} {
254 set path [file normalize $path]
255 set recent [get_config gui.recentrepo]
257 if {[lindex $recent end] eq $path} {
258 return
261 set i [lsearch $recent $path]
262 if {$i >= 0} {
263 _unset_recentrepo $path
264 set recent [lreplace $recent $i $i]
267 lappend recent $path
268 git config --global --add gui.recentrepo $path
269 load_config 1
271 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
272 set maxrecent 10
275 while {[llength $recent] > $maxrecent} {
276 _unset_recentrepo [lindex $recent 0]
277 set recent [lrange $recent 1 end]
281 method _open_recent {xy} {
282 set id [lindex [split [$w_recentlist index @$xy] .] 0]
283 set local_path [lindex $sorted_recent [expr {$id - 1}]]
284 _do_open2 $this
287 method _open_recent_path {p} {
288 set local_path $p
289 _do_open2 $this
292 method _next {action} {
293 global NS
294 destroy $w_body
295 if {![winfo exists $w_next]} {
296 ${NS}::button $w_next -default active
297 set pos -before
298 if {[tk windowingsystem] eq "win32"} { set pos -after }
299 pack $w_next -side right -padx 5 $pos $w_quit
301 _do_$action $this
304 method _write_local_path {args} {
305 if {$local_path eq {}} {
306 $w_next conf -state disabled
307 } else {
308 $w_next conf -state normal
312 method _git_init {} {
313 if {[catch {file mkdir $local_path} err]} {
314 error_popup [strcat \
315 [mc "Failed to create repository %s:" $local_path] \
316 "\n\n$err"]
317 return 0
320 if {[catch {cd $local_path} err]} {
321 error_popup [strcat \
322 [mc "Failed to create repository %s:" $local_path] \
323 "\n\n$err"]
324 return 0
327 if {[catch {git init} err]} {
328 error_popup [strcat \
329 [mc "Failed to create repository %s:" $local_path] \
330 "\n\n$err"]
331 return 0
334 _append_recentrepos [pwd]
335 set ::_gitdir .git
336 set ::_prefix {}
337 return 1
340 proc _is_git {path} {
341 if {[file exists [file join $path HEAD]]
342 && [file exists [file join $path objects]]
343 && [file exists [file join $path config]]} {
344 return 1
346 if {[is_Cygwin]} {
347 if {[file exists [file join $path HEAD]]
348 && [file exists [file join $path objects.lnk]]
349 && [file exists [file join $path config.lnk]]} {
350 return 1
353 return 0
356 proc _objdir {path} {
357 set objdir [file join $path .git objects]
358 if {[file isdirectory $objdir]} {
359 return $objdir
362 set objdir [file join $path objects]
363 if {[file isdirectory $objdir]} {
364 return $objdir
367 if {[is_Cygwin]} {
368 set objdir [file join $path .git objects.lnk]
369 if {[file isfile $objdir]} {
370 return [win32_read_lnk $objdir]
373 set objdir [file join $path objects.lnk]
374 if {[file isfile $objdir]} {
375 return [win32_read_lnk $objdir]
379 return {}
382 ######################################################################
384 ## Create New Repository
386 method _do_new {} {
387 global use_ttk NS
388 $w_next conf \
389 -state disabled \
390 -command [cb _do_new2] \
391 -text [mc "Create"]
393 ${NS}::frame $w_body
394 ${NS}::label $w_body.h \
395 -font font_uibold -anchor center \
396 -text [mc "Create New Repository"]
397 pack $w_body.h -side top -fill x -pady 10
398 pack $w_body -fill x -padx 10
400 ${NS}::frame $w_body.where
401 ${NS}::label $w_body.where.l -text [mc "Directory:"]
402 ${NS}::entry $w_body.where.t \
403 -textvariable @local_path \
404 -width 50
405 ${NS}::button $w_body.where.b \
406 -text [mc "Browse"] \
407 -command [cb _new_local_path]
408 set w_localpath $w_body.where.t
410 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
411 pack $w_body.where -fill x
413 grid columnconfigure $w_body.where 1 -weight 1
415 trace add variable @local_path write [cb _write_local_path]
416 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
417 update
418 focus $w_body.where.t
421 method _new_local_path {} {
422 if {$local_path ne {}} {
423 set p [file dirname $local_path]
424 } else {
425 set p [pwd]
428 set p [tk_chooseDirectory \
429 -initialdir $p \
430 -parent $top \
431 -title [mc "Git Repository"] \
432 -mustexist false]
433 if {$p eq {}} return
435 set p [file normalize $p]
436 if {![_new_ok $p]} {
437 return
439 set local_path $p
440 $w_localpath icursor end
443 method _do_new2 {} {
444 if {![_new_ok $local_path]} {
445 return
447 if {![_git_init $this]} {
448 return
450 set done 1
453 proc _new_ok {p} {
454 if {[file isdirectory $p]} {
455 if {[_is_git [file join $p .git]]} {
456 error_popup [mc "Directory %s already exists." $p]
457 return 0
459 } elseif {[file exists $p]} {
460 error_popup [mc "File %s already exists." $p]
461 return 0
463 return 1
466 ######################################################################
468 ## Clone Existing Repository
470 method _do_clone {} {
471 global use_ttk NS
472 $w_next conf \
473 -state disabled \
474 -command [cb _do_clone2] \
475 -text [mc "Clone"]
477 ${NS}::frame $w_body
478 ${NS}::label $w_body.h \
479 -font font_uibold -anchor center \
480 -text [mc "Clone Existing Repository"]
481 pack $w_body.h -side top -fill x -pady 10
482 pack $w_body -fill x -padx 10
484 set args $w_body.args
485 ${NS}::frame $w_body.args
486 pack $args -fill both
488 ${NS}::label $args.origin_l -text [mc "Source Location:"]
489 ${NS}::entry $args.origin_t \
490 -textvariable @origin_url \
491 -width 50
492 ${NS}::button $args.origin_b \
493 -text [mc "Browse"] \
494 -command [cb _open_origin]
495 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
497 ${NS}::label $args.where_l -text [mc "Target Directory:"]
498 ${NS}::entry $args.where_t \
499 -textvariable @local_path \
500 -width 50
501 ${NS}::button $args.where_b \
502 -text [mc "Browse"] \
503 -command [cb _new_local_path]
504 grid $args.where_l $args.where_t $args.where_b -sticky ew
505 set w_localpath $args.where_t
507 ${NS}::label $args.type_l -text [mc "Clone Type:"]
508 ${NS}::frame $args.type_f
509 set w_types [list]
510 lappend w_types [${NS}::radiobutton $args.type_f.hardlink \
511 -state disabled \
512 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
513 -variable @clone_type \
514 -value hardlink]
515 lappend w_types [${NS}::radiobutton $args.type_f.full \
516 -state disabled \
517 -text [mc "Full Copy (Slower, Redundant Backup)"] \
518 -variable @clone_type \
519 -value full]
520 lappend w_types [${NS}::radiobutton $args.type_f.shared \
521 -state disabled \
522 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
523 -variable @clone_type \
524 -value shared]
525 foreach r $w_types {
526 pack $r -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
611 if {[is_Cygwin] && [file isdirectory $giturl]} {
612 set giturl [exec cygpath --unix --absolute $giturl]
613 if {$clone_type eq {shared}} {
614 set objdir [exec cygpath --unix --absolute $objdir]
618 if {[file exists $local_path]} {
619 error_popup [mc "Location %s already exists." $local_path]
620 return
623 if {![_git_init $this]} return
624 set local_path [pwd]
626 if {[catch {
627 git config remote.$origin_name.url $giturl
628 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
629 } err]} {
630 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
631 return
634 destroy $w_body $w_next
636 switch -exact -- $clone_type {
637 hardlink {
638 set o_cons [status_bar::two_line $w_body]
639 pack $w_body -fill x -padx 10 -pady 10
641 $o_cons start \
642 [mc "Counting objects"] \
643 [mc "buckets"]
644 update
646 if {[file exists [file join $objdir info alternates]]} {
647 set pwd [pwd]
648 if {[catch {
649 file mkdir [gitdir objects info]
650 set f_in [open [file join $objdir info alternates] r]
651 set f_cp [open [gitdir objects info alternates] w]
652 fconfigure $f_in -translation binary -encoding binary
653 fconfigure $f_cp -translation binary -encoding binary
654 cd $objdir
655 while {[gets $f_in line] >= 0} {
656 if {[is_Cygwin]} {
657 puts $f_cp [exec cygpath --unix --absolute $line]
658 } else {
659 puts $f_cp [file normalize $line]
662 close $f_in
663 close $f_cp
664 cd $pwd
665 } err]} {
666 catch {cd $pwd}
667 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
668 return
672 set tolink [list]
673 set buckets [glob \
674 -tails \
675 -nocomplain \
676 -directory [file join $objdir] ??]
677 set bcnt [expr {[llength $buckets] + 2}]
678 set bcur 1
679 $o_cons update $bcur $bcnt
680 update
682 file mkdir [file join .git objects pack]
683 foreach i [glob -tails -nocomplain \
684 -directory [file join $objdir pack] *] {
685 lappend tolink [file join pack $i]
687 $o_cons update [incr bcur] $bcnt
688 update
690 foreach i $buckets {
691 file mkdir [file join .git objects $i]
692 foreach j [glob -tails -nocomplain \
693 -directory [file join $objdir $i] *] {
694 lappend tolink [file join $i $j]
696 $o_cons update [incr bcur] $bcnt
697 update
699 $o_cons stop
701 if {$tolink eq {}} {
702 info_popup [strcat \
703 [mc "Nothing to clone from %s." $origin_url] \
704 "\n" \
705 [mc "The 'master' branch has not been initialized."] \
707 destroy $w_body
708 set done 1
709 return
712 set i [lindex $tolink 0]
713 if {[catch {
714 file link -hard \
715 [file join .git objects $i] \
716 [file join $objdir $i]
717 } err]} {
718 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
719 set i [_copy_files $this $objdir $tolink]
720 } else {
721 set i [_link_files $this $objdir [lrange $tolink 1 end]]
723 if {!$i} return
725 destroy $w_body
727 full {
728 set o_cons [console::embed \
729 $w_body \
730 [mc "Cloning from %s" $origin_url]]
731 pack $w_body -fill both -expand 1 -padx 10
732 $o_cons exec \
733 [list git fetch --no-tags -k $origin_name] \
734 [cb _do_clone_tags]
736 shared {
737 set fd [open [gitdir objects info alternates] w]
738 fconfigure $fd -translation binary
739 puts $fd $objdir
740 close $fd
744 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
745 if {![_clone_refs $this]} return
746 set pwd [pwd]
747 if {[catch {
748 cd $origin_url
749 set HEAD [git rev-parse --verify HEAD^0]
750 } err]} {
751 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
752 return 0
754 cd $pwd
755 _do_clone_checkout $this $HEAD
759 method _copy_files {objdir tocopy} {
760 $o_cons start \
761 [mc "Copying objects"] \
762 [mc "KiB"]
763 set tot 0
764 set cmp 0
765 foreach p $tocopy {
766 incr tot [file size [file join $objdir $p]]
768 foreach p $tocopy {
769 if {[catch {
770 set f_in [open [file join $objdir $p] r]
771 set f_cp [open [file join .git objects $p] w]
772 fconfigure $f_in -translation binary -encoding binary
773 fconfigure $f_cp -translation binary -encoding binary
775 while {![eof $f_in]} {
776 incr cmp [fcopy $f_in $f_cp -size 16384]
777 $o_cons update \
778 [expr {$cmp / 1024}] \
779 [expr {$tot / 1024}]
780 update
783 close $f_in
784 close $f_cp
785 } err]} {
786 _clone_failed $this [mc "Unable to copy object: %s" $err]
787 return 0
790 return 1
793 method _link_files {objdir tolink} {
794 set total [llength $tolink]
795 $o_cons start \
796 [mc "Linking objects"] \
797 [mc "objects"]
798 for {set i 0} {$i < $total} {} {
799 set p [lindex $tolink $i]
800 if {[catch {
801 file link -hard \
802 [file join .git objects $p] \
803 [file join $objdir $p]
804 } err]} {
805 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
806 return 0
809 incr i
810 if {$i % 5 == 0} {
811 $o_cons update $i $total
812 update
815 return 1
818 method _clone_refs {} {
819 set pwd [pwd]
820 if {[catch {cd $origin_url} err]} {
821 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
822 return 0
824 set fd_in [git_read for-each-ref \
825 --tcl \
826 {--format=list %(refname) %(objectname) %(*objectname)}]
827 cd $pwd
829 set fd [open [gitdir packed-refs] w]
830 fconfigure $fd -translation binary
831 puts $fd "# pack-refs with: peeled"
832 while {[gets $fd_in line] >= 0} {
833 set line [eval $line]
834 set refn [lindex $line 0]
835 set robj [lindex $line 1]
836 set tobj [lindex $line 2]
838 if {[regsub ^refs/heads/ $refn \
839 "refs/remotes/$origin_name/" refn]} {
840 puts $fd "$robj $refn"
841 } elseif {[string match refs/tags/* $refn]} {
842 puts $fd "$robj $refn"
843 if {$tobj ne {}} {
844 puts $fd "^$tobj"
848 close $fd_in
849 close $fd
850 return 1
853 method _do_clone_tags {ok} {
854 if {$ok} {
855 $o_cons exec \
856 [list git fetch --tags -k $origin_name] \
857 [cb _do_clone_HEAD]
858 } else {
859 $o_cons done $ok
860 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
864 method _do_clone_HEAD {ok} {
865 if {$ok} {
866 $o_cons exec \
867 [list git fetch $origin_name HEAD] \
868 [cb _do_clone_full_end]
869 } else {
870 $o_cons done $ok
871 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
875 method _do_clone_full_end {ok} {
876 $o_cons done $ok
878 if {$ok} {
879 destroy $w_body
881 set HEAD {}
882 if {[file exists [gitdir FETCH_HEAD]]} {
883 set fd [open [gitdir FETCH_HEAD] r]
884 while {[gets $fd line] >= 0} {
885 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
886 break
889 close $fd
892 catch {git pack-refs}
893 _do_clone_checkout $this $HEAD
894 } else {
895 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
899 method _clone_failed {{why {}}} {
900 if {[catch {file delete -force $local_path} err]} {
901 set why [strcat \
902 $why \
903 "\n\n" \
904 [mc "Unable to cleanup %s" $local_path] \
905 "\n\n" \
906 $err]
908 if {$why ne {}} {
909 update
910 error_popup [strcat [mc "Clone failed."] "\n" $why]
914 method _do_clone_checkout {HEAD} {
915 if {$HEAD eq {}} {
916 info_popup [strcat \
917 [mc "No default branch obtained."] \
918 "\n" \
919 [mc "The 'master' branch has not been initialized."] \
921 set done 1
922 return
924 if {[catch {
925 git update-ref HEAD $HEAD^0
926 } err]} {
927 info_popup [strcat \
928 [mc "Cannot resolve %s as a commit." $HEAD^0] \
929 "\n $err" \
930 "\n" \
931 [mc "The 'master' branch has not been initialized."] \
933 set done 1
934 return
937 set o_cons [status_bar::two_line $w_body]
938 pack $w_body -fill x -padx 10 -pady 10
939 $o_cons start \
940 [mc "Creating working directory"] \
941 [mc "files"]
943 set readtree_err {}
944 set fd [git_read --stderr read-tree \
945 -m \
946 -u \
947 -v \
948 HEAD \
949 HEAD \
951 fconfigure $fd -blocking 0 -translation binary
952 fileevent $fd readable [cb _readtree_wait $fd]
955 method _readtree_wait {fd} {
956 set buf [read $fd]
957 $o_cons update_meter $buf
958 append readtree_err $buf
960 fconfigure $fd -blocking 1
961 if {![eof $fd]} {
962 fconfigure $fd -blocking 0
963 return
966 if {[catch {close $fd}]} {
967 set err $readtree_err
968 regsub {^fatal: } $err {} err
969 error_popup [strcat \
970 [mc "Initial file checkout failed."] \
971 "\n\n$err"]
972 return
975 # -- Run the post-checkout hook.
977 set fd_ph [githook_read post-checkout [string repeat 0 40] \
978 [git rev-parse HEAD] 1]
979 if {$fd_ph ne {}} {
980 global pch_error
981 set pch_error {}
982 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
983 fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
984 } else {
985 set done 1
989 method _postcheckout_wait {fd_ph} {
990 global pch_error
992 append pch_error [read $fd_ph]
993 fconfigure $fd_ph -blocking 1
994 if {[eof $fd_ph]} {
995 if {[catch {close $fd_ph}]} {
996 hook_failed_popup post-checkout $pch_error 0
998 unset pch_error
999 set done 1
1000 return
1002 fconfigure $fd_ph -blocking 0
1005 ######################################################################
1007 ## Open Existing Repository
1009 method _do_open {} {
1010 global NS
1011 $w_next conf \
1012 -state disabled \
1013 -command [cb _do_open2] \
1014 -text [mc "Open"]
1016 ${NS}::frame $w_body
1017 ${NS}::label $w_body.h \
1018 -font font_uibold -anchor center \
1019 -text [mc "Open Existing Repository"]
1020 pack $w_body.h -side top -fill x -pady 10
1021 pack $w_body -fill x -padx 10
1023 ${NS}::frame $w_body.where
1024 ${NS}::label $w_body.where.l -text [mc "Repository:"]
1025 ${NS}::entry $w_body.where.t \
1026 -textvariable @local_path \
1027 -width 50
1028 ${NS}::button $w_body.where.b \
1029 -text [mc "Browse"] \
1030 -command [cb _open_local_path]
1032 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
1033 pack $w_body.where -fill x
1035 grid columnconfigure $w_body.where 1 -weight 1
1037 trace add variable @local_path write [cb _write_local_path]
1038 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
1039 update
1040 focus $w_body.where.t
1043 method _open_local_path {} {
1044 if {$local_path ne {}} {
1045 set p $local_path
1046 } else {
1047 set p [pwd]
1050 set p [tk_chooseDirectory \
1051 -initialdir $p \
1052 -parent $top \
1053 -title [mc "Git Repository"] \
1054 -mustexist true]
1055 if {$p eq {}} return
1057 set p [file normalize $p]
1058 if {![_is_git [file join $p .git]]} {
1059 error_popup [mc "Not a Git repository: %s" [file tail $p]]
1060 return
1062 set local_path $p
1065 method _do_open2 {} {
1066 if {![_is_git [file join $local_path .git]]} {
1067 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
1068 return
1071 if {[catch {cd $local_path} err]} {
1072 error_popup [strcat \
1073 [mc "Failed to open repository %s:" $local_path] \
1074 "\n\n$err"]
1075 return
1078 _append_recentrepos [pwd]
1079 set ::_gitdir .git
1080 set ::_prefix {}
1081 set done 1