git-gui: remove duplicate entries from .gitconfig's gui.recentrepo
[git.git] / lib / choose_repository.tcl
blob133ca0ac351b16f737dc7d4a3d1ccdc19db510ad
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 recursive true ; # Recursive cloning flag
22 field readtree_err ; # Error output from read-tree (if any)
23 field sorted_recent ; # recent repositories (sorted)
25 constructor pick {} {
26 global M1T M1B use_ttk NS
28 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
29 set maxrecent 10
32 make_dialog top w
33 wm title $top [mc "Git Gui"]
35 if {$top eq {.}} {
36 menu $w.mbar -tearoff 0
37 $top configure -menu $w.mbar
39 set m_repo $w.mbar.repository
40 $w.mbar add cascade \
41 -label [mc Repository] \
42 -menu $m_repo
43 menu $m_repo
45 if {[is_MacOSX]} {
46 $w.mbar add cascade -label Apple -menu .mbar.apple
47 menu $w.mbar.apple
48 $w.mbar.apple add command \
49 -label [mc "About %s" [appname]] \
50 -command do_about
51 $w.mbar.apple add command \
52 -label [mc "Show SSH Key"] \
53 -command do_ssh_key
54 } else {
55 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
56 menu $w.mbar.help
57 $w.mbar.help add command \
58 -label [mc "About %s" [appname]] \
59 -command do_about
60 $w.mbar.help add command \
61 -label [mc "Show SSH Key"] \
62 -command do_ssh_key
65 wm protocol $top WM_DELETE_WINDOW exit
66 bind $top <$M1B-q> exit
67 bind $top <$M1B-Q> exit
68 bind $top <Key-Escape> exit
69 } else {
70 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
71 bind $top <Key-Escape> [list destroy $top]
72 set m_repo {}
75 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
77 set w_body $w.body
78 set opts $w_body.options
79 ${NS}::frame $w_body
80 text $opts \
81 -cursor $::cursor_ptr \
82 -relief flat \
83 -background [get_bg_color $w_body] \
84 -wrap none \
85 -spacing1 5 \
86 -width 50 \
87 -height 3
88 pack $opts -anchor w -fill x
90 $opts tag conf link_new -foreground blue -underline 1
91 $opts tag bind link_new <1> [cb _next new]
92 $opts insert end [mc "Create New Repository"] link_new
93 $opts insert end "\n"
94 if {$m_repo ne {}} {
95 $m_repo add command \
96 -command [cb _next new] \
97 -accelerator $M1T-N \
98 -label [mc "New..."]
99 bind $top <$M1B-n> [cb _next new]
100 bind $top <$M1B-N> [cb _next new]
103 $opts tag conf link_clone -foreground blue -underline 1
104 $opts tag bind link_clone <1> [cb _next clone]
105 $opts insert end [mc "Clone Existing Repository"] link_clone
106 $opts insert end "\n"
107 if {$m_repo ne {}} {
108 if {[tk windowingsystem] eq "win32"} {
109 set key L
110 } else {
111 set key C
113 $m_repo add command \
114 -command [cb _next clone] \
115 -accelerator $M1T-$key \
116 -label [mc "Clone..."]
117 bind $top <$M1B-[string tolower $key]> [cb _next clone]
118 bind $top <$M1B-[string toupper $key]> [cb _next clone]
121 $opts tag conf link_open -foreground blue -underline 1
122 $opts tag bind link_open <1> [cb _next open]
123 $opts insert end [mc "Open Existing Repository"] link_open
124 $opts insert end "\n"
125 if {$m_repo ne {}} {
126 $m_repo add command \
127 -command [cb _next open] \
128 -accelerator $M1T-O \
129 -label [mc "Open..."]
130 bind $top <$M1B-o> [cb _next open]
131 bind $top <$M1B-O> [cb _next open]
134 $opts conf -state disabled
136 set sorted_recent [_get_recentrepos]
137 if {[llength $sorted_recent] > 0} {
138 if {$m_repo ne {}} {
139 $m_repo add separator
140 $m_repo add command \
141 -state disabled \
142 -label [mc "Recent Repositories"]
145 ${NS}::label $w_body.space
146 ${NS}::label $w_body.recentlabel \
147 -anchor w \
148 -text [mc "Open Recent Repository:"]
149 set w_recentlist $w_body.recentlist
150 text $w_recentlist \
151 -cursor $::cursor_ptr \
152 -relief flat \
153 -background [get_bg_color $w_body.recentlabel] \
154 -wrap none \
155 -width 50 \
156 -height $maxrecent
157 $w_recentlist tag conf link \
158 -foreground blue \
159 -underline 1
160 set home $::env(HOME)
161 if {[is_Cygwin]} {
162 set home [exec cygpath --windows --absolute $home]
164 set home "[file normalize $home]/"
165 set hlen [string length $home]
166 foreach p $sorted_recent {
167 set path $p
168 if {[string equal -length $hlen $home $p]} {
169 set p "~/[string range $p $hlen end]"
171 regsub -all "\n" $p "\\n" p
172 $w_recentlist insert end $p link
173 $w_recentlist insert end "\n"
175 if {$m_repo ne {}} {
176 $m_repo add command \
177 -command [cb _open_recent_path $path] \
178 -label " $p"
181 $w_recentlist conf -state disabled
182 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
183 pack $w_body.space -anchor w -fill x
184 pack $w_body.recentlabel -anchor w -fill x
185 pack $w_recentlist -anchor w -fill x
187 pack $w_body -fill x -padx 10 -pady 10
189 ${NS}::frame $w.buttons
190 set w_next $w.buttons.next
191 set w_quit $w.buttons.quit
192 ${NS}::button $w_quit \
193 -text [mc "Quit"] \
194 -command exit
195 pack $w_quit -side right -padx 5
196 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
198 if {$m_repo ne {}} {
199 $m_repo add separator
200 $m_repo add command \
201 -label [mc Quit] \
202 -command exit \
203 -accelerator $M1T-Q
206 bind $top <Return> [cb _invoke_next]
207 bind $top <Visibility> "
208 [cb _center]
209 grab $top
210 focus $top
211 bind $top <Visibility> {}
213 wm deiconify $top
214 tkwait variable @done
216 grab release $top
217 if {$top eq {.}} {
218 eval destroy [winfo children $top]
222 method _center {} {
223 set nx [winfo reqwidth $top]
224 set ny [winfo reqheight $top]
225 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
226 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
227 wm geometry $top [format {+%d+%d} $rx $ry]
230 method _invoke_next {} {
231 if {[winfo exists $w_next]} {
232 uplevel #0 [$w_next cget -command]
236 proc _get_recentrepos {} {
237 set recent [list]
238 foreach p [get_config gui.recentrepo] {
239 if {[_is_git [file join $p .git]]} {
240 lappend recent $p
241 } else {
242 _unset_recentrepo $p
245 return [lsort $recent]
248 proc _unset_recentrepo {p} {
249 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
250 git config --global --unset-all gui.recentrepo "^$p\$"
251 load_config 1
254 proc _append_recentrepos {path} {
255 set path [file normalize $path]
256 set recent [get_config gui.recentrepo]
258 if {[lindex $recent end] eq $path} {
259 return
262 set i [lsearch $recent $path]
263 if {$i >= 0} {
264 _unset_recentrepo $path
265 set recent [lreplace $recent $i $i]
268 lappend recent $path
269 git config --global --add gui.recentrepo $path
270 load_config 1
272 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
273 set maxrecent 10
276 while {[llength $recent] > $maxrecent} {
277 _unset_recentrepo [lindex $recent 0]
278 set recent [lrange $recent 1 end]
282 method _open_recent {xy} {
283 set id [lindex [split [$w_recentlist index @$xy] .] 0]
284 set local_path [lindex $sorted_recent [expr {$id - 1}]]
285 _do_open2 $this
288 method _open_recent_path {p} {
289 set local_path $p
290 _do_open2 $this
293 method _next {action} {
294 global NS
295 destroy $w_body
296 if {![winfo exists $w_next]} {
297 ${NS}::button $w_next -default active
298 set pos -before
299 if {[tk windowingsystem] eq "win32"} { set pos -after }
300 pack $w_next -side right -padx 5 $pos $w_quit
302 _do_$action $this
305 method _write_local_path {args} {
306 if {$local_path eq {}} {
307 $w_next conf -state disabled
308 } else {
309 $w_next conf -state normal
313 method _git_init {} {
314 if {[catch {file mkdir $local_path} err]} {
315 error_popup [strcat \
316 [mc "Failed to create repository %s:" $local_path] \
317 "\n\n$err"]
318 return 0
321 if {[catch {cd $local_path} err]} {
322 error_popup [strcat \
323 [mc "Failed to create repository %s:" $local_path] \
324 "\n\n$err"]
325 return 0
328 if {[catch {git init} err]} {
329 error_popup [strcat \
330 [mc "Failed to create repository %s:" $local_path] \
331 "\n\n$err"]
332 return 0
335 _append_recentrepos [pwd]
336 set ::_gitdir .git
337 set ::_prefix {}
338 return 1
341 proc _is_git {path {outdir_var ""}} {
342 if {$outdir_var ne ""} {
343 upvar 1 $outdir_var outdir
345 if {[file isfile $path]} {
346 set fp [open $path r]
347 gets $fp line
348 close $fp
349 if {[regexp "^gitdir: (.+)$" $line line link_target]} {
350 set path [file join [file dirname $path] $link_target]
351 set path [file normalize $path]
355 if {[file exists [file join $path HEAD]]
356 && [file exists [file join $path objects]]
357 && [file exists [file join $path config]]} {
358 set outdir $path
359 return 1
361 if {[is_Cygwin]} {
362 if {[file exists [file join $path HEAD]]
363 && [file exists [file join $path objects.lnk]]
364 && [file exists [file join $path config.lnk]]} {
365 set outdir $path
366 return 1
369 return 0
372 proc _objdir {path} {
373 set objdir [file join $path .git objects]
374 if {[file isdirectory $objdir]} {
375 return $objdir
378 set objdir [file join $path objects]
379 if {[file isdirectory $objdir]} {
380 return $objdir
383 if {[is_Cygwin]} {
384 set objdir [file join $path .git objects.lnk]
385 if {[file isfile $objdir]} {
386 return [win32_read_lnk $objdir]
389 set objdir [file join $path objects.lnk]
390 if {[file isfile $objdir]} {
391 return [win32_read_lnk $objdir]
395 return {}
398 ######################################################################
400 ## Create New Repository
402 method _do_new {} {
403 global use_ttk NS
404 $w_next conf \
405 -state disabled \
406 -command [cb _do_new2] \
407 -text [mc "Create"]
409 ${NS}::frame $w_body
410 ${NS}::label $w_body.h \
411 -font font_uibold -anchor center \
412 -text [mc "Create New Repository"]
413 pack $w_body.h -side top -fill x -pady 10
414 pack $w_body -fill x -padx 10
416 ${NS}::frame $w_body.where
417 ${NS}::label $w_body.where.l -text [mc "Directory:"]
418 ${NS}::entry $w_body.where.t \
419 -textvariable @local_path \
420 -width 50
421 ${NS}::button $w_body.where.b \
422 -text [mc "Browse"] \
423 -command [cb _new_local_path]
424 set w_localpath $w_body.where.t
426 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
427 pack $w_body.where -fill x
429 grid columnconfigure $w_body.where 1 -weight 1
431 trace add variable @local_path write [cb _write_local_path]
432 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
433 update
434 focus $w_body.where.t
437 method _new_local_path {} {
438 if {$local_path ne {}} {
439 set p [file dirname $local_path]
440 } else {
441 set p [pwd]
444 set p [tk_chooseDirectory \
445 -initialdir $p \
446 -parent $top \
447 -title [mc "Git Repository"] \
448 -mustexist false]
449 if {$p eq {}} return
451 set p [file normalize $p]
452 if {![_new_ok $p]} {
453 return
455 set local_path $p
456 $w_localpath icursor end
459 method _do_new2 {} {
460 if {![_new_ok $local_path]} {
461 return
463 if {![_git_init $this]} {
464 return
466 set done 1
469 proc _new_ok {p} {
470 if {[file isdirectory $p]} {
471 if {[_is_git [file join $p .git]]} {
472 error_popup [mc "Directory %s already exists." $p]
473 return 0
475 } elseif {[file exists $p]} {
476 error_popup [mc "File %s already exists." $p]
477 return 0
479 return 1
482 ######################################################################
484 ## Clone Existing Repository
486 method _do_clone {} {
487 global use_ttk NS
488 $w_next conf \
489 -state disabled \
490 -command [cb _do_clone2] \
491 -text [mc "Clone"]
493 ${NS}::frame $w_body
494 ${NS}::label $w_body.h \
495 -font font_uibold -anchor center \
496 -text [mc "Clone Existing Repository"]
497 pack $w_body.h -side top -fill x -pady 10
498 pack $w_body -fill x -padx 10
500 set args $w_body.args
501 ${NS}::frame $w_body.args
502 pack $args -fill both
504 ${NS}::label $args.origin_l -text [mc "Source Location:"]
505 ${NS}::entry $args.origin_t \
506 -textvariable @origin_url \
507 -width 50
508 ${NS}::button $args.origin_b \
509 -text [mc "Browse"] \
510 -command [cb _open_origin]
511 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
513 ${NS}::label $args.where_l -text [mc "Target Directory:"]
514 ${NS}::entry $args.where_t \
515 -textvariable @local_path \
516 -width 50
517 ${NS}::button $args.where_b \
518 -text [mc "Browse"] \
519 -command [cb _new_local_path]
520 grid $args.where_l $args.where_t $args.where_b -sticky ew
521 set w_localpath $args.where_t
523 ${NS}::label $args.type_l -text [mc "Clone Type:"]
524 ${NS}::frame $args.type_f
525 set w_types [list]
526 lappend w_types [${NS}::radiobutton $args.type_f.hardlink \
527 -state disabled \
528 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
529 -variable @clone_type \
530 -value hardlink]
531 lappend w_types [${NS}::radiobutton $args.type_f.full \
532 -state disabled \
533 -text [mc "Full Copy (Slower, Redundant Backup)"] \
534 -variable @clone_type \
535 -value full]
536 lappend w_types [${NS}::radiobutton $args.type_f.shared \
537 -state disabled \
538 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
539 -variable @clone_type \
540 -value shared]
541 foreach r $w_types {
542 pack $r -anchor w
544 ${NS}::checkbutton $args.type_f.recursive \
545 -text [mc "Recursively clone submodules too"] \
546 -variable @recursive \
547 -onvalue true -offvalue false
548 pack $args.type_f.recursive -anchor w
549 grid $args.type_l $args.type_f -sticky new
551 grid columnconfigure $args 1 -weight 1
553 trace add variable @local_path write [cb _update_clone]
554 trace add variable @origin_url write [cb _update_clone]
555 bind $w_body.h <Destroy> "
556 [list trace remove variable @local_path write [cb _update_clone]]
557 [list trace remove variable @origin_url write [cb _update_clone]]
559 update
560 focus $args.origin_t
563 method _open_origin {} {
564 if {$origin_url ne {} && [file isdirectory $origin_url]} {
565 set p $origin_url
566 } else {
567 set p [pwd]
570 set p [tk_chooseDirectory \
571 -initialdir $p \
572 -parent $top \
573 -title [mc "Git Repository"] \
574 -mustexist true]
575 if {$p eq {}} return
577 set p [file normalize $p]
578 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
579 error_popup [mc "Not a Git repository: %s" [file tail $p]]
580 return
582 set origin_url $p
585 method _update_clone {args} {
586 if {$local_path ne {} && $origin_url ne {}} {
587 $w_next conf -state normal
588 } else {
589 $w_next conf -state disabled
592 if {$origin_url ne {} &&
593 ( [_is_git [file join $origin_url .git]]
594 || [_is_git $origin_url])} {
595 set e normal
596 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
597 set clone_type hardlink
599 } else {
600 set e disabled
601 set clone_type full
604 foreach r $w_types {
605 $r conf -state $e
609 method _do_clone2 {} {
610 if {[file isdirectory $origin_url]} {
611 set origin_url [file normalize $origin_url]
614 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
615 error_popup [mc "Standard only available for local repository."]
616 return
618 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
619 error_popup [mc "Shared only available for local repository."]
620 return
623 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
624 set objdir [_objdir $origin_url]
625 if {$objdir eq {}} {
626 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
627 return
631 set giturl $origin_url
632 if {[is_Cygwin] && [file isdirectory $giturl]} {
633 set giturl [exec cygpath --unix --absolute $giturl]
634 if {$clone_type eq {shared}} {
635 set objdir [exec cygpath --unix --absolute $objdir]
639 if {[file exists $local_path]} {
640 error_popup [mc "Location %s already exists." $local_path]
641 return
644 if {![_git_init $this]} return
645 set local_path [pwd]
647 if {[catch {
648 git config remote.$origin_name.url $giturl
649 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
650 } err]} {
651 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
652 return
655 destroy $w_body $w_next
657 switch -exact -- $clone_type {
658 hardlink {
659 set o_cons [status_bar::two_line $w_body]
660 pack $w_body -fill x -padx 10 -pady 10
662 $o_cons start \
663 [mc "Counting objects"] \
664 [mc "buckets"]
665 update
667 if {[file exists [file join $objdir info alternates]]} {
668 set pwd [pwd]
669 if {[catch {
670 file mkdir [gitdir objects info]
671 set f_in [open [file join $objdir info alternates] r]
672 set f_cp [open [gitdir objects info alternates] w]
673 fconfigure $f_in -translation binary -encoding binary
674 fconfigure $f_cp -translation binary -encoding binary
675 cd $objdir
676 while {[gets $f_in line] >= 0} {
677 if {[is_Cygwin]} {
678 puts $f_cp [exec cygpath --unix --absolute $line]
679 } else {
680 puts $f_cp [file normalize $line]
683 close $f_in
684 close $f_cp
685 cd $pwd
686 } err]} {
687 catch {cd $pwd}
688 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
689 return
693 set tolink [list]
694 set buckets [glob \
695 -tails \
696 -nocomplain \
697 -directory [file join $objdir] ??]
698 set bcnt [expr {[llength $buckets] + 2}]
699 set bcur 1
700 $o_cons update $bcur $bcnt
701 update
703 file mkdir [file join .git objects pack]
704 foreach i [glob -tails -nocomplain \
705 -directory [file join $objdir pack] *] {
706 lappend tolink [file join pack $i]
708 $o_cons update [incr bcur] $bcnt
709 update
711 foreach i $buckets {
712 file mkdir [file join .git objects $i]
713 foreach j [glob -tails -nocomplain \
714 -directory [file join $objdir $i] *] {
715 lappend tolink [file join $i $j]
717 $o_cons update [incr bcur] $bcnt
718 update
720 $o_cons stop
722 if {$tolink eq {}} {
723 info_popup [strcat \
724 [mc "Nothing to clone from %s." $origin_url] \
725 "\n" \
726 [mc "The 'master' branch has not been initialized."] \
728 destroy $w_body
729 set done 1
730 return
733 set i [lindex $tolink 0]
734 if {[catch {
735 file link -hard \
736 [file join .git objects $i] \
737 [file join $objdir $i]
738 } err]} {
739 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
740 set i [_copy_files $this $objdir $tolink]
741 } else {
742 set i [_link_files $this $objdir [lrange $tolink 1 end]]
744 if {!$i} return
746 destroy $w_body
748 full {
749 set o_cons [console::embed \
750 $w_body \
751 [mc "Cloning from %s" $origin_url]]
752 pack $w_body -fill both -expand 1 -padx 10
753 $o_cons exec \
754 [list git fetch --no-tags -k $origin_name] \
755 [cb _do_clone_tags]
757 shared {
758 set fd [open [gitdir objects info alternates] w]
759 fconfigure $fd -translation binary
760 puts $fd $objdir
761 close $fd
765 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
766 if {![_clone_refs $this]} return
767 set pwd [pwd]
768 if {[catch {
769 cd $origin_url
770 set HEAD [git rev-parse --verify HEAD^0]
771 } err]} {
772 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
773 return 0
775 cd $pwd
776 _do_clone_checkout $this $HEAD
780 method _copy_files {objdir tocopy} {
781 $o_cons start \
782 [mc "Copying objects"] \
783 [mc "KiB"]
784 set tot 0
785 set cmp 0
786 foreach p $tocopy {
787 incr tot [file size [file join $objdir $p]]
789 foreach p $tocopy {
790 if {[catch {
791 set f_in [open [file join $objdir $p] r]
792 set f_cp [open [file join .git objects $p] w]
793 fconfigure $f_in -translation binary -encoding binary
794 fconfigure $f_cp -translation binary -encoding binary
796 while {![eof $f_in]} {
797 incr cmp [fcopy $f_in $f_cp -size 16384]
798 $o_cons update \
799 [expr {$cmp / 1024}] \
800 [expr {$tot / 1024}]
801 update
804 close $f_in
805 close $f_cp
806 } err]} {
807 _clone_failed $this [mc "Unable to copy object: %s" $err]
808 return 0
811 return 1
814 method _link_files {objdir tolink} {
815 set total [llength $tolink]
816 $o_cons start \
817 [mc "Linking objects"] \
818 [mc "objects"]
819 for {set i 0} {$i < $total} {} {
820 set p [lindex $tolink $i]
821 if {[catch {
822 file link -hard \
823 [file join .git objects $p] \
824 [file join $objdir $p]
825 } err]} {
826 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
827 return 0
830 incr i
831 if {$i % 5 == 0} {
832 $o_cons update $i $total
833 update
836 return 1
839 method _clone_refs {} {
840 set pwd [pwd]
841 if {[catch {cd $origin_url} err]} {
842 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
843 return 0
845 set fd_in [git_read for-each-ref \
846 --tcl \
847 {--format=list %(refname) %(objectname) %(*objectname)}]
848 cd $pwd
850 set fd [open [gitdir packed-refs] w]
851 fconfigure $fd -translation binary
852 puts $fd "# pack-refs with: peeled"
853 while {[gets $fd_in line] >= 0} {
854 set line [eval $line]
855 set refn [lindex $line 0]
856 set robj [lindex $line 1]
857 set tobj [lindex $line 2]
859 if {[regsub ^refs/heads/ $refn \
860 "refs/remotes/$origin_name/" refn]} {
861 puts $fd "$robj $refn"
862 } elseif {[string match refs/tags/* $refn]} {
863 puts $fd "$robj $refn"
864 if {$tobj ne {}} {
865 puts $fd "^$tobj"
869 close $fd_in
870 close $fd
871 return 1
874 method _do_clone_tags {ok} {
875 if {$ok} {
876 $o_cons exec \
877 [list git fetch --tags -k $origin_name] \
878 [cb _do_clone_HEAD]
879 } else {
880 $o_cons done $ok
881 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
885 method _do_clone_HEAD {ok} {
886 if {$ok} {
887 $o_cons exec \
888 [list git fetch $origin_name HEAD] \
889 [cb _do_clone_full_end]
890 } else {
891 $o_cons done $ok
892 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
896 method _do_clone_full_end {ok} {
897 $o_cons done $ok
899 if {$ok} {
900 destroy $w_body
902 set HEAD {}
903 if {[file exists [gitdir FETCH_HEAD]]} {
904 set fd [open [gitdir FETCH_HEAD] r]
905 while {[gets $fd line] >= 0} {
906 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
907 break
910 close $fd
913 catch {git pack-refs}
914 _do_clone_checkout $this $HEAD
915 } else {
916 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
920 method _clone_failed {{why {}}} {
921 if {[catch {file delete -force $local_path} err]} {
922 set why [strcat \
923 $why \
924 "\n\n" \
925 [mc "Unable to cleanup %s" $local_path] \
926 "\n\n" \
927 $err]
929 if {$why ne {}} {
930 update
931 error_popup [strcat [mc "Clone failed."] "\n" $why]
935 method _do_clone_checkout {HEAD} {
936 if {$HEAD eq {}} {
937 info_popup [strcat \
938 [mc "No default branch obtained."] \
939 "\n" \
940 [mc "The 'master' branch has not been initialized."] \
942 set done 1
943 return
945 if {[catch {
946 git update-ref HEAD $HEAD^0
947 } err]} {
948 info_popup [strcat \
949 [mc "Cannot resolve %s as a commit." $HEAD^0] \
950 "\n $err" \
951 "\n" \
952 [mc "The 'master' branch has not been initialized."] \
954 set done 1
955 return
958 set o_cons [status_bar::two_line $w_body]
959 pack $w_body -fill x -padx 10 -pady 10
960 $o_cons start \
961 [mc "Creating working directory"] \
962 [mc "files"]
964 set readtree_err {}
965 set fd [git_read --stderr read-tree \
966 -m \
967 -u \
968 -v \
969 HEAD \
970 HEAD \
972 fconfigure $fd -blocking 0 -translation binary
973 fileevent $fd readable [cb _readtree_wait $fd]
976 method _do_validate_submodule_cloning {ok} {
977 if {$ok} {
978 $o_cons done $ok
979 set done 1
980 } else {
981 _clone_failed $this [mc "Cannot clone submodules."]
985 method _do_clone_submodules {} {
986 if {$recursive eq {true}} {
987 destroy $w_body
988 set o_cons [console::embed \
989 $w_body \
990 [mc "Cloning submodules"]]
991 pack $w_body -fill both -expand 1 -padx 10
992 $o_cons exec \
993 [list git submodule update --init --recursive] \
994 [cb _do_validate_submodule_cloning]
995 } else {
996 set done 1
1000 method _readtree_wait {fd} {
1001 set buf [read $fd]
1002 $o_cons update_meter $buf
1003 append readtree_err $buf
1005 fconfigure $fd -blocking 1
1006 if {![eof $fd]} {
1007 fconfigure $fd -blocking 0
1008 return
1011 if {[catch {close $fd}]} {
1012 set err $readtree_err
1013 regsub {^fatal: } $err {} err
1014 error_popup [strcat \
1015 [mc "Initial file checkout failed."] \
1016 "\n\n$err"]
1017 return
1020 # -- Run the post-checkout hook.
1022 set fd_ph [githook_read post-checkout [string repeat 0 40] \
1023 [git rev-parse HEAD] 1]
1024 if {$fd_ph ne {}} {
1025 global pch_error
1026 set pch_error {}
1027 fconfigure $fd_ph -blocking 0 -translation binary -eofchar {}
1028 fileevent $fd_ph readable [cb _postcheckout_wait $fd_ph]
1029 } else {
1030 _do_clone_submodules $this
1034 method _postcheckout_wait {fd_ph} {
1035 global pch_error
1037 append pch_error [read $fd_ph]
1038 fconfigure $fd_ph -blocking 1
1039 if {[eof $fd_ph]} {
1040 if {[catch {close $fd_ph}]} {
1041 hook_failed_popup post-checkout $pch_error 0
1043 unset pch_error
1044 _do_clone_submodules $this
1045 return
1047 fconfigure $fd_ph -blocking 0
1050 ######################################################################
1052 ## Open Existing Repository
1054 method _do_open {} {
1055 global NS
1056 $w_next conf \
1057 -state disabled \
1058 -command [cb _do_open2] \
1059 -text [mc "Open"]
1061 ${NS}::frame $w_body
1062 ${NS}::label $w_body.h \
1063 -font font_uibold -anchor center \
1064 -text [mc "Open Existing Repository"]
1065 pack $w_body.h -side top -fill x -pady 10
1066 pack $w_body -fill x -padx 10
1068 ${NS}::frame $w_body.where
1069 ${NS}::label $w_body.where.l -text [mc "Repository:"]
1070 ${NS}::entry $w_body.where.t \
1071 -textvariable @local_path \
1072 -width 50
1073 ${NS}::button $w_body.where.b \
1074 -text [mc "Browse"] \
1075 -command [cb _open_local_path]
1077 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
1078 pack $w_body.where -fill x
1080 grid columnconfigure $w_body.where 1 -weight 1
1082 trace add variable @local_path write [cb _write_local_path]
1083 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
1084 update
1085 focus $w_body.where.t
1088 method _open_local_path {} {
1089 if {$local_path ne {}} {
1090 set p $local_path
1091 } else {
1092 set p [pwd]
1095 set p [tk_chooseDirectory \
1096 -initialdir $p \
1097 -parent $top \
1098 -title [mc "Git Repository"] \
1099 -mustexist true]
1100 if {$p eq {}} return
1102 set p [file normalize $p]
1103 if {![_is_git [file join $p .git]]} {
1104 error_popup [mc "Not a Git repository: %s" [file tail $p]]
1105 return
1107 set local_path $p
1110 method _do_open2 {} {
1111 if {![_is_git [file join $local_path .git] actualgit]} {
1112 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
1113 return
1116 if {[catch {cd $local_path} err]} {
1117 error_popup [strcat \
1118 [mc "Failed to open repository %s:" $local_path] \
1119 "\n\n$err"]
1120 return
1123 _append_recentrepos [pwd]
1124 set ::_gitdir $actualgit
1125 set ::_prefix {}
1126 set done 1