]> asedeno.scripts.mit.edu Git - git.git/blob - lib/choose_repository.tcl
git-gui: Fix bind errors when switching repository chooser panels
[git.git] / lib / choose_repository.tcl
1 # git-gui Git repository chooser
2 # Copyright (C) 2007 Shawn Pearce
3
4 class choose_repository {
5
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
15 field done              0 ; # Finished picking the repository?
16 field local_path       {} ; # Where this repository is locally
17 field origin_url       {} ; # Where we are cloning from
18 field origin_name  origin ; # What we shall call 'origin'
19 field clone_type hardlink ; # Type of clone to construct
20 field readtree_err        ; # Error output from read-tree (if any)
21 field sorted_recent       ; # recent repositories (sorted)
22
23 constructor pick {} {
24         global M1T M1B
25
26         make_toplevel top w
27         wm title $top [mc "Git Gui"]
28
29         if {$top eq {.}} {
30                 menu $w.mbar -tearoff 0
31                 $top configure -menu $w.mbar
32
33                 set m_repo $w.mbar.repository
34                 $w.mbar add cascade \
35                         -label [mc Repository] \
36                         -menu $m_repo
37                 menu $m_repo
38
39                 if {[is_MacOSX]} {
40                         $w.mbar add cascade -label [mc Apple] -menu .mbar.apple
41                         menu $w.mbar.apple
42                         $w.mbar.apple add command \
43                                 -label [mc "About %s" [appname]] \
44                                 -command do_about
45                 } else {
46                         $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
47                         menu $w.mbar.help
48                         $w.mbar.help add command \
49                                 -label [mc "About %s" [appname]] \
50                                 -command do_about
51                 }
52
53                 wm protocol $top WM_DELETE_WINDOW exit
54                 bind $top <$M1B-q> exit
55                 bind $top <$M1B-Q> exit
56                 bind $top <Key-Escape> exit
57         } else {
58                 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
59                 bind $top <Key-Escape> [list destroy $top]
60                 set m_repo {}
61         }
62
63         pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
64
65         set w_body $w.body
66         set opts $w_body.options
67         frame $w_body
68         text $opts \
69                 -cursor $::cursor_ptr \
70                 -relief flat \
71                 -background [$w_body cget -background] \
72                 -wrap none \
73                 -spacing1 5 \
74                 -width 50 \
75                 -height 3
76         pack $opts -anchor w -fill x
77
78         $opts tag conf link_new -foreground blue -underline 1
79         $opts tag bind link_new <1> [cb _next new]
80         $opts insert end [mc "Create New Repository"] link_new
81         $opts insert end "\n"
82         if {$m_repo ne {}} {
83                 $m_repo add command \
84                         -command [cb _next new] \
85                         -accelerator $M1T-N \
86                         -label [mc "New..."]
87         }
88
89         $opts tag conf link_clone -foreground blue -underline 1
90         $opts tag bind link_clone <1> [cb _next clone]
91         $opts insert end [mc "Clone Existing Repository"] link_clone
92         $opts insert end "\n"
93         if {$m_repo ne {}} {
94                 $m_repo add command \
95                         -command [cb _next clone] \
96                         -accelerator $M1T-C \
97                         -label [mc "Clone..."]
98         }
99
100         $opts tag conf link_open -foreground blue -underline 1
101         $opts tag bind link_open <1> [cb _next open]
102         $opts insert end [mc "Open Existing Repository"] link_open
103         $opts insert end "\n"
104         if {$m_repo ne {}} {
105                 $m_repo add command \
106                         -command [cb _next open] \
107                         -accelerator $M1T-O \
108                         -label [mc "Open..."]
109         }
110
111         set sorted_recent [_get_recentrepos]
112         if {[llength $sorted_recent] > 0} {
113                 if {$m_repo ne {}} {
114                         $m_repo add separator
115                         $m_repo add command \
116                                 -state disabled \
117                                 -label [mc "Recent Repositories"]
118                 }
119
120                 label $w_body.space
121                 label $w_body.recentlabel \
122                         -anchor w \
123                         -text [mc "Open Recent Repository:"]
124                 set w_recentlist $w_body.recentlist
125                 text $w_recentlist \
126                         -cursor $::cursor_ptr \
127                         -relief flat \
128                         -background [$w_body.recentlabel cget -background] \
129                         -wrap none \
130                         -width 50 \
131                         -height 10
132                 $w_recentlist tag conf link \
133                         -foreground blue \
134                         -underline 1
135                 set home "[file normalize $::env(HOME)][file separator]"
136                 set hlen [string length $home]
137                 foreach p $sorted_recent {
138                         set path $p
139                         if {[string equal -length $hlen $home $p]} {
140                                 set p "~[file separator][string range $p $hlen end]"
141                         }
142                         regsub -all "\n" $p "\\n" p
143                         $w_recentlist insert end $p link
144                         $w_recentlist insert end "\n"
145
146                         if {$m_repo ne {}} {
147                                 $m_repo add command \
148                                         -command [cb _open_recent_path $path] \
149                                         -label "    $p"
150                         }
151                 }
152                 $w_recentlist conf -state disabled
153                 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
154                 pack $w_body.space -anchor w -fill x
155                 pack $w_body.recentlabel -anchor w -fill x
156                 pack $w_recentlist -anchor w -fill x
157         }
158         pack $w_body -fill x -padx 10 -pady 10
159
160         frame $w.buttons
161         set w_next $w.buttons.next
162         set w_quit $w.buttons.quit
163         button $w_quit \
164                 -text [mc "Quit"] \
165                 -command exit
166         pack $w_quit -side right -padx 5
167         pack $w.buttons -side bottom -fill x -padx 10 -pady 10
168
169         if {$m_repo ne {}} {
170                 $m_repo add separator
171                 $m_repo add command \
172                         -label [mc Quit] \
173                         -command exit \
174                         -accelerator $M1T-Q
175         }
176
177         bind $top <Return> [cb _invoke_next]
178         bind $top <Visibility> "
179                 [cb _center]
180                 grab $top
181                 focus $top
182                 bind $top <Visibility> {}
183         "
184         wm deiconify $top
185         tkwait variable @done
186
187         if {$top eq {.}} {
188                 eval destroy [winfo children $top]
189         }
190 }
191
192 proc _home {} {
193         if {[catch {set h $::env(HOME)}]
194                 || ![file isdirectory $h]} {
195                 set h .
196         }
197         return $h
198 }
199
200 method _center {} {
201         set nx [winfo reqwidth $top]
202         set ny [winfo reqheight $top]
203         set rx [expr {([winfo screenwidth  $top] - $nx) / 3}]
204         set ry [expr {([winfo screenheight $top] - $ny) / 3}]
205         wm geometry $top [format {+%d+%d} $rx $ry]
206 }
207
208 method _invoke_next {} {
209         if {[winfo exists $w_next]} {
210                 uplevel #0 [$w_next cget -command]
211         }
212 }
213
214 proc _get_recentrepos {} {
215         set recent [list]
216         foreach p [get_config gui.recentrepo] {
217                 if {[_is_git [file join $p .git]]} {
218                         lappend recent $p
219                 }
220         }
221         return [lsort $recent]
222 }
223
224 proc _unset_recentrepo {p} {
225         regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
226         git config --global --unset gui.recentrepo "^$p\$"
227 }
228
229 proc _append_recentrepos {path} {
230         set path [file normalize $path]
231         set recent [get_config gui.recentrepo]
232
233         if {[lindex $recent end] eq $path} {
234                 return
235         }
236
237         set i [lsearch $recent $path]
238         if {$i >= 0} {
239                 _unset_recentrepo $path
240                 set recent [lreplace $recent $i $i]
241         }
242
243         lappend recent $path
244         git config --global --add gui.recentrepo $path
245
246         while {[llength $recent] > 10} {
247                 _unset_recentrepo [lindex $recent 0]
248                 set recent [lrange $recent 1 end]
249         }
250 }
251
252 method _open_recent {xy} {
253         set id [lindex [split [$w_recentlist index @$xy] .] 0]
254         set local_path [lindex $sorted_recent [expr {$id - 1}]]
255         _do_open2 $this
256 }
257
258 method _open_recent_path {p} {
259         set local_path $p
260         _do_open2 $this
261 }
262
263 method _next {action} {
264         destroy $w_body
265         if {![winfo exists $w_next]} {
266                 button $w_next -default active
267                 pack $w_next -side right -padx 5 -before $w_quit
268         }
269         _do_$action $this
270 }
271
272 method _write_local_path {args} {
273         if {$local_path eq {}} {
274                 $w_next conf -state disabled
275         } else {
276                 $w_next conf -state normal
277         }
278 }
279
280 method _git_init {} {
281         if {[file exists $local_path]} {
282                 error_popup [mc "Location %s already exists." $local_path]
283                 return 0
284         }
285
286         if {[catch {file mkdir $local_path} err]} {
287                 error_popup [strcat \
288                         [mc "Failed to create repository %s:" $local_path] \
289                         "\n\n$err"]
290                 return 0
291         }
292
293         if {[catch {cd $local_path} err]} {
294                 error_popup [strcat \
295                         [mc "Failed to create repository %s:" $local_path] \
296                         "\n\n$err"]
297                 return 0
298         }
299
300         if {[catch {git init} err]} {
301                 error_popup [strcat \
302                         [mc "Failed to create repository %s:" $local_path] \
303                         "\n\n$err"]
304                 return 0
305         }
306
307         _append_recentrepos [pwd]
308         set ::_gitdir .git
309         set ::_prefix {}
310         return 1
311 }
312
313 proc _is_git {path} {
314         if {[file exists [file join $path HEAD]]
315          && [file exists [file join $path objects]]
316          && [file exists [file join $path config]]} {
317                 return 1
318         }
319         return 0
320 }
321
322 ######################################################################
323 ##
324 ## Create New Repository
325
326 method _do_new {} {
327         $w_next conf \
328                 -state disabled \
329                 -command [cb _do_new2] \
330                 -text [mc "Create"]
331
332         frame $w_body
333         label $w_body.h \
334                 -font font_uibold \
335                 -text [mc "Create New Repository"]
336         pack $w_body.h -side top -fill x -pady 10
337         pack $w_body -fill x -padx 10
338
339         frame $w_body.where
340         label $w_body.where.l -text [mc "Directory:"]
341         entry $w_body.where.t \
342                 -textvariable @local_path \
343                 -font font_diff \
344                 -width 50
345         button $w_body.where.b \
346                 -text [mc "Browse"] \
347                 -command [cb _new_local_path]
348
349         pack $w_body.where.b -side right
350         pack $w_body.where.l -side left
351         pack $w_body.where.t -fill x
352         pack $w_body.where -fill x
353
354         trace add variable @local_path write [cb _write_local_path]
355         bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
356         update
357         focus $w_body.where.t
358 }
359
360 method _new_local_path {} {
361         if {$local_path ne {}} {
362                 set p [file dirname $local_path]
363         } else {
364                 set p [_home]
365         }
366
367         set p [tk_chooseDirectory \
368                 -initialdir $p \
369                 -parent $top \
370                 -title [mc "Git Repository"] \
371                 -mustexist false]
372         if {$p eq {}} return
373
374         set p [file normalize $p]
375         if {[file isdirectory $p]} {
376                 foreach i [glob \
377                         -directory $p \
378                         -tails \
379                         -nocomplain \
380                         * .*] {
381                         switch -- $i {
382                          . continue
383                         .. continue
384                         default {
385                                 error_popup [mc "Directory %s already exists." $p]
386                                 return
387                         }
388                         }
389                 }
390                 if {[catch {file delete $p} err]} {
391                         error_popup [strcat \
392                                 [mc "Directory %s already exists." $p] \
393                                 "\n\n$err"]
394                         return
395                 }
396         } elseif {[file exists $p]} {
397                 error_popup [mc "File %s already exists." $p]
398                 return
399         }
400         set local_path $p
401 }
402
403 method _do_new2 {} {
404         if {![_git_init $this]} {
405                 return
406         }
407         set done 1
408 }
409
410 ######################################################################
411 ##
412 ## Clone Existing Repository
413
414 method _do_clone {} {
415         $w_next conf \
416                 -state disabled \
417                 -command [cb _do_clone2] \
418                 -text [mc "Clone"]
419
420         frame $w_body
421         label $w_body.h \
422                 -font font_uibold \
423                 -text [mc "Clone Existing Repository"]
424         pack $w_body.h -side top -fill x -pady 10
425         pack $w_body -fill x -padx 10
426
427         set args $w_body.args
428         frame $w_body.args
429         pack $args -fill both
430
431         label $args.origin_l -text [mc "URL:"]
432         entry $args.origin_t \
433                 -textvariable @origin_url \
434                 -font font_diff \
435                 -width 50
436         button $args.origin_b \
437                 -text [mc "Browse"] \
438                 -command [cb _open_origin]
439         grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
440
441         label $args.where_l -text [mc "Directory:"]
442         entry $args.where_t \
443                 -textvariable @local_path \
444                 -font font_diff \
445                 -width 50
446         button $args.where_b \
447                 -text [mc "Browse"] \
448                 -command [cb _new_local_path]
449         grid $args.where_l $args.where_t $args.where_b -sticky ew
450
451         label $args.type_l -text [mc "Clone Type:"]
452         frame $args.type_f
453         set w_types [list]
454         lappend w_types [radiobutton $args.type_f.hardlink \
455                 -state disabled \
456                 -anchor w \
457                 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
458                 -variable @clone_type \
459                 -value hardlink]
460         lappend w_types [radiobutton $args.type_f.full \
461                 -state disabled \
462                 -anchor w \
463                 -text [mc "Full Copy (Slower, Redundant Backup)"] \
464                 -variable @clone_type \
465                 -value full]
466         lappend w_types [radiobutton $args.type_f.shared \
467                 -state disabled \
468                 -anchor w \
469                 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
470                 -variable @clone_type \
471                 -value shared]
472         foreach r $w_types {
473                 pack $r -anchor w
474         }
475         grid $args.type_l $args.type_f -sticky new
476
477         grid columnconfigure $args 1 -weight 1
478
479         trace add variable @local_path write [cb _update_clone]
480         trace add variable @origin_url write [cb _update_clone]
481         bind $w_body.h <Destroy> "
482                 [list trace remove variable @local_path write [cb _update_clone]]
483                 [list trace remove variable @origin_url write [cb _update_clone]]
484         "
485         update
486         focus $args.origin_t
487 }
488
489 method _open_origin {} {
490         if {$origin_url ne {} && [file isdirectory $origin_url]} {
491                 set p $origin_url
492         } else {
493                 set p [_home]
494         }
495
496         set p [tk_chooseDirectory \
497                 -initialdir $p \
498                 -parent $top \
499                 -title [mc "Git Repository"] \
500                 -mustexist true]
501         if {$p eq {}} return
502
503         set p [file normalize $p]
504         if {![_is_git [file join $p .git]] && ![_is_git $p]} {
505                 error_popup [mc "Not a Git repository: %s" [file tail $p]]
506                 return
507         }
508         set origin_url $p
509 }
510
511 method _update_clone {args} {
512         if {$local_path ne {} && $origin_url ne {}} {
513                 $w_next conf -state normal
514         } else {
515                 $w_next conf -state disabled
516         }
517
518         if {$origin_url ne {} &&
519                 (  [_is_git [file join $origin_url .git]]
520                 || [_is_git $origin_url])} {
521                 set e normal
522                 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
523                         set clone_type hardlink
524                 }
525         } else {
526                 set e disabled
527                 set clone_type full
528         }
529
530         foreach r $w_types {
531                 $r conf -state $e
532         }
533 }
534
535 method _do_clone2 {} {
536         if {[file isdirectory $origin_url]} {
537                 set origin_url [file normalize $origin_url]
538         }
539
540         if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
541                 error_popup [mc "Standard only available for local repository."]
542                 return
543         }
544         if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
545                 error_popup [mc "Shared only available for local repository."]
546                 return
547         }
548
549         if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
550                 set objdir [file join $origin_url .git objects]
551                 if {![file isdirectory $objdir]} {
552                         set objdir [file join $origin_url objects]
553                         if {![file isdirectory $objdir]} {
554                                 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
555                                 return
556                         }
557                 }
558         }
559
560         set giturl $origin_url
561         if {[is_Cygwin] && [file isdirectory $giturl]} {
562                 set giturl [exec cygpath --unix --absolute $giturl]
563                 if {$clone_type eq {shared}} {
564                         set objdir [exec cygpath --unix --absolute $objdir]
565                 }
566         }
567
568         if {![_git_init $this]} return
569         set local_path [pwd]
570
571         if {[catch {
572                         git config remote.$origin_name.url $giturl
573                         git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
574                 } err]} {
575                 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
576                 return
577         }
578
579         destroy $w_body $w_next
580
581         switch -exact -- $clone_type {
582         hardlink {
583                 set o_cons [status_bar::two_line $w_body]
584                 pack $w_body -fill x -padx 10 -pady 10
585
586                 $o_cons start \
587                         [mc "Counting objects"] \
588                         [mc "buckets"]
589                 update
590
591                 if {[file exists [file join $objdir info alternates]]} {
592                         set pwd [pwd]
593                         if {[catch {
594                                 file mkdir [gitdir objects info]
595                                 set f_in [open [file join $objdir info alternates] r]
596                                 set f_cp [open [gitdir objects info alternates] w]
597                                 fconfigure $f_in -translation binary -encoding binary
598                                 fconfigure $f_cp -translation binary -encoding binary
599                                 cd $objdir
600                                 while {[gets $f_in line] >= 0} {
601                                         if {[is_Cygwin]} {
602                                                 puts $f_cp [exec cygpath --unix --absolute $line]
603                                         } else {
604                                                 puts $f_cp [file normalize $line]
605                                         }
606                                 }
607                                 close $f_in
608                                 close $f_cp
609                                 cd $pwd
610                         } err]} {
611                                 catch {cd $pwd}
612                                 _clone_failed $this [mc "Unable to copy objects/info/alternates: %s" $err]
613                                 return
614                         }
615                 }
616
617                 set tolink  [list]
618                 set buckets [glob \
619                         -tails \
620                         -nocomplain \
621                         -directory [file join $objdir] ??]
622                 set bcnt [expr {[llength $buckets] + 2}]
623                 set bcur 1
624                 $o_cons update $bcur $bcnt
625                 update
626
627                 file mkdir [file join .git objects pack]
628                 foreach i [glob -tails -nocomplain \
629                         -directory [file join $objdir pack] *] {
630                         lappend tolink [file join pack $i]
631                 }
632                 $o_cons update [incr bcur] $bcnt
633                 update
634
635                 foreach i $buckets {
636                         file mkdir [file join .git objects $i]
637                         foreach j [glob -tails -nocomplain \
638                                 -directory [file join $objdir $i] *] {
639                                 lappend tolink [file join $i $j]
640                         }
641                         $o_cons update [incr bcur] $bcnt
642                         update
643                 }
644                 $o_cons stop
645
646                 if {$tolink eq {}} {
647                         info_popup [strcat \
648                                 [mc "Nothing to clone from %s." $origin_url] \
649                                 "\n" \
650                                 [mc "The 'master' branch has not been initialized."] \
651                                 ]
652                         destroy $w_body
653                         set done 1
654                         return
655                 }
656
657                 set i [lindex $tolink 0]
658                 if {[catch {
659                                 file link -hard \
660                                         [file join .git objects $i] \
661                                         [file join $objdir $i]
662                         } err]} {
663                         info_popup [mc "Hardlinks are unavailable.  Falling back to copying."]
664                         set i [_copy_files $this $objdir $tolink]
665                 } else {
666                         set i [_link_files $this $objdir [lrange $tolink 1 end]]
667                 }
668                 if {!$i} return
669
670                 destroy $w_body
671         }
672         full {
673                 set o_cons [console::embed \
674                         $w_body \
675                         [mc "Cloning from %s" $origin_url]]
676                 pack $w_body -fill both -expand 1 -padx 10
677                 $o_cons exec \
678                         [list git fetch --no-tags -k $origin_name] \
679                         [cb _do_clone_tags]
680         }
681         shared {
682                 set fd [open [gitdir objects info alternates] w]
683                 fconfigure $fd -translation binary
684                 puts $fd $objdir
685                 close $fd
686         }
687         }
688
689         if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
690                 if {![_clone_refs $this]} return
691                 set pwd [pwd]
692                 if {[catch {
693                                 cd $origin_url
694                                 set HEAD [git rev-parse --verify HEAD^0]
695                         } err]} {
696                         _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
697                         return 0
698                 }
699                 cd $pwd
700                 _do_clone_checkout $this $HEAD
701         }
702 }
703
704 method _copy_files {objdir tocopy} {
705         $o_cons start \
706                 [mc "Copying objects"] \
707                 [mc "KiB"]
708         set tot 0
709         set cmp 0
710         foreach p $tocopy {
711                 incr tot [file size [file join $objdir $p]]
712         }
713         foreach p $tocopy {
714                 if {[catch {
715                                 set f_in [open [file join $objdir $p] r]
716                                 set f_cp [open [file join .git objects $p] w]
717                                 fconfigure $f_in -translation binary -encoding binary
718                                 fconfigure $f_cp -translation binary -encoding binary
719
720                                 while {![eof $f_in]} {
721                                         incr cmp [fcopy $f_in $f_cp -size 16384]
722                                         $o_cons update \
723                                                 [expr {$cmp / 1024}] \
724                                                 [expr {$tot / 1024}]
725                                         update
726                                 }
727
728                                 close $f_in
729                                 close $f_cp
730                         } err]} {
731                         _clone_failed $this [mc "Unable to copy object: %s" $err]
732                         return 0
733                 }
734         }
735         return 1
736 }
737
738 method _link_files {objdir tolink} {
739         set total [llength $tolink]
740         $o_cons start \
741                 [mc "Linking objects"] \
742                 [mc "objects"]
743         for {set i 0} {$i < $total} {} {
744                 set p [lindex $tolink $i]
745                 if {[catch {
746                                 file link -hard \
747                                         [file join .git objects $p] \
748                                         [file join $objdir $p]
749                         } err]} {
750                         _clone_failed $this [mc "Unable to hardlink object: %s" $err]
751                         return 0
752                 }
753
754                 incr i
755                 if {$i % 5 == 0} {
756                         $o_cons update $i $total
757                         update
758                 }
759         }
760         return 1
761 }
762
763 method _clone_refs {} {
764         set pwd [pwd]
765         if {[catch {cd $origin_url} err]} {
766                 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
767                 return 0
768         }
769         set fd_in [git_read for-each-ref \
770                 --tcl \
771                 {--format=list %(refname) %(objectname) %(*objectname)}]
772         cd $pwd
773
774         set fd [open [gitdir packed-refs] w]
775         fconfigure $fd -translation binary
776         puts $fd "# pack-refs with: peeled"
777         while {[gets $fd_in line] >= 0} {
778                 set line [eval $line]
779                 set refn [lindex $line 0]
780                 set robj [lindex $line 1]
781                 set tobj [lindex $line 2]
782
783                 if {[regsub ^refs/heads/ $refn \
784                         "refs/remotes/$origin_name/" refn]} {
785                         puts $fd "$robj $refn"
786                 } elseif {[string match refs/tags/* $refn]} {
787                         puts $fd "$robj $refn"
788                         if {$tobj ne {}} {
789                                 puts $fd "^$tobj"
790                         }
791                 }
792         }
793         close $fd_in
794         close $fd
795         return 1
796 }
797
798 method _do_clone_tags {ok} {
799         if {$ok} {
800                 $o_cons exec \
801                         [list git fetch --tags -k $origin_name] \
802                         [cb _do_clone_HEAD]
803         } else {
804                 $o_cons done $ok
805                 _clone_failed $this [mc "Cannot fetch branches and objects.  See console output for details."]
806         }
807 }
808
809 method _do_clone_HEAD {ok} {
810         if {$ok} {
811                 $o_cons exec \
812                         [list git fetch $origin_name HEAD] \
813                         [cb _do_clone_full_end]
814         } else {
815                 $o_cons done $ok
816                 _clone_failed $this [mc "Cannot fetch tags.  See console output for details."]
817         }
818 }
819
820 method _do_clone_full_end {ok} {
821         $o_cons done $ok
822
823         if {$ok} {
824                 destroy $w_body
825
826                 set HEAD {}
827                 if {[file exists [gitdir FETCH_HEAD]]} {
828                         set fd [open [gitdir FETCH_HEAD] r]
829                         while {[gets $fd line] >= 0} {
830                                 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
831                                         break
832                                 }
833                         }
834                         close $fd
835                 }
836
837                 catch {git pack-refs}
838                 _do_clone_checkout $this $HEAD
839         } else {
840                 _clone_failed $this [mc "Cannot determine HEAD.  See console output for details."]
841         }
842 }
843
844 method _clone_failed {{why {}}} {
845         if {[catch {file delete -force $local_path} err]} {
846                 set why [strcat \
847                         $why \
848                         "\n\n" \
849                         [mc "Unable to cleanup %s" $local_path] \
850                         "\n\n" \
851                         $err]
852         }
853         if {$why ne {}} {
854                 update
855                 error_popup [strcat [mc "Clone failed."] "\n" $why]
856         }
857 }
858
859 method _do_clone_checkout {HEAD} {
860         if {$HEAD eq {}} {
861                 info_popup [strcat \
862                         [mc "No default branch obtained."] \
863                         "\n" \
864                         [mc "The 'master' branch has not been initialized."] \
865                         ]
866                 set done 1
867                 return
868         }
869         if {[catch {
870                         git update-ref HEAD $HEAD^0
871                 } err]} {
872                 info_popup [strcat \
873                         [mc "Cannot resolve %s as a commit." $HEAD^0] \
874                         "\n  $err" \
875                         "\n" \
876                         [mc "The 'master' branch has not been initialized."] \
877                         ]
878                 set done 1
879                 return
880         }
881
882         set o_cons [status_bar::two_line $w_body]
883         pack $w_body -fill x -padx 10 -pady 10
884         $o_cons start \
885                 [mc "Creating working directory"] \
886                 [mc "files"]
887
888         set readtree_err {}
889         set fd [git_read --stderr read-tree \
890                 -m \
891                 -u \
892                 -v \
893                 HEAD \
894                 HEAD \
895                 ]
896         fconfigure $fd -blocking 0 -translation binary
897         fileevent $fd readable [cb _readtree_wait $fd]
898 }
899
900 method _readtree_wait {fd} {
901         set buf [read $fd]
902         $o_cons update_meter $buf
903         append readtree_err $buf
904
905         fconfigure $fd -blocking 1
906         if {![eof $fd]} {
907                 fconfigure $fd -blocking 0
908                 return
909         }
910
911         if {[catch {close $fd}]} {
912                 set err $readtree_err
913                 regsub {^fatal: } $err {} err
914                 error_popup [strcat \
915                         [mc "Initial file checkout failed."] \
916                         "\n\n$err"]
917                 return
918         }
919
920         set done 1
921 }
922
923 ######################################################################
924 ##
925 ## Open Existing Repository
926
927 method _do_open {} {
928         $w_next conf \
929                 -state disabled \
930                 -command [cb _do_open2] \
931                 -text [mc "Open"]
932
933         frame $w_body
934         label $w_body.h \
935                 -font font_uibold \
936                 -text [mc "Open Existing Repository"]
937         pack $w_body.h -side top -fill x -pady 10
938         pack $w_body -fill x -padx 10
939
940         frame $w_body.where
941         label $w_body.where.l -text [mc "Repository:"]
942         entry $w_body.where.t \
943                 -textvariable @local_path \
944                 -font font_diff \
945                 -width 50
946         button $w_body.where.b \
947                 -text [mc "Browse"] \
948                 -command [cb _open_local_path]
949
950         pack $w_body.where.b -side right
951         pack $w_body.where.l -side left
952         pack $w_body.where.t -fill x
953         pack $w_body.where -fill x
954
955         trace add variable @local_path write [cb _write_local_path]
956         bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
957         update
958         focus $w_body.where.t
959 }
960
961 method _open_local_path {} {
962         if {$local_path ne {}} {
963                 set p $local_path
964         } else {
965                 set p [_home]
966         }
967
968         set p [tk_chooseDirectory \
969                 -initialdir $p \
970                 -parent $top \
971                 -title [mc "Git Repository"] \
972                 -mustexist true]
973         if {$p eq {}} return
974
975         set p [file normalize $p]
976         if {![_is_git [file join $p .git]]} {
977                 error_popup [mc "Not a Git repository: %s" [file tail $p]]
978                 return
979         }
980         set local_path $p
981 }
982
983 method _do_open2 {} {
984         if {![_is_git [file join $local_path .git]]} {
985                 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
986                 return
987         }
988
989         if {[catch {cd $local_path} err]} {
990                 error_popup [strcat \
991                         [mc "Failed to open repository %s:" $local_path] \
992                         "\n\n$err"]
993                 return
994         }
995
996         _append_recentrepos [pwd]
997         set ::_gitdir .git
998         set ::_prefix {}
999         set done 1
1000 }
1001
1002 }