git-gui: Don't bother showing OS error message about hardlinks
[git-gui/me-and.git] / lib / choose_repository.tcl
blob9074c1ba5ca1ede9321226caf37dbc57413fcf55
1 # git-gui Git repository chooser
2 # Copyright (C) 2007 Shawn Pearce
4 class choose_repository {
6 image create photo ::choose_repository::git_logo -data {
7 R0lGODlh3wA9AMIHAMAAAMIKCsMKCgCAAN/v3/319f///wAAACH5BAEKAAcALAAAAADfAD0AAAP+
8 aLrc/jDKSau9OOvNu/9gKI5kaZ5oqq5s675wLM90bd94ru987//AoHBILBqPyKSSFGg6n9BoUwCR
9 Wq3Ux3X7zC4Xg7B4HN4Bzui0eo2GsN9wN3ye/jLI+IGZzpfz535/b3ZgeWN7goMPiXGLjGyECoaH
10 Oo+QjpZ1mJlnkQaTYhigZR6cmg6mbZucnqN6F64fqZ2rmYGskbGwo7Kzt7a1lq28u6AfBcjJysvM
11 yRDN0NHP0dTKniS619pG2dveQd3f4jzh40eu3eh5EOrq5kPtk3fxoQ/0xO9A93jz9+z7hvKBA1hP
12 0r5/BMkIJIfvU0OE8opFXDjQWCGLEsplfEj+sUc6jg40RhDZMcdHjCNBpkQ5IuAHlx5gtjg5cWVN
13 CiRjrnu5Uye/FzRlQhQ6IWcHohqQZlCKImhPm0ztqeT506dCqlddOK0K9SnOqVYphS141OuKrVm7
14 cv3KUkRUC28rxMXW0KjDthvxgphb1GxSvynQilWblu1Nt4BFJZY4mIVgskMXhwQrN+GryQkj+zvx
15 mFTew5/5Eo6nmV5p0pzrUr4LejTkv5mlxsZMsKlqvf1an3691PJud7JrpwZFoDgBu3Zzi/6NjrlI
16 y5cD+74IPbrydowPBgfofPXe6QarW6eOuvLsBtC7424JnnX67djhtr++mTb3s+3Fe0avvexl2g3L
17 laaPd4QECN9+HBioFYF2KMjff7AVtkNyBUpmnoQRNjYhg184SN94GfI2U38LeUieiIpBOGJ9JVpo
18 mIYhIggUiQKZGB6GvanoGI0l9ejjj0AGKeSQRBZp5JFIJqnkkkxOkAAAOw==
21 field top
22 field w
23 field w_body ; # Widget holding the center content
24 field w_next ; # Next button
25 field o_cons ; # Console object (if active)
26 field w_types ; # List of type buttons in clone
28 field action new ; # What action are we going to perform?
29 field done 0 ; # Finished picking the repository?
30 field local_path {} ; # Where this repository is locally
31 field origin_url {} ; # Where we are cloning from
32 field origin_name origin ; # What we shall call 'origin'
33 field clone_type hardlink ; # Type of clone to construct
34 field readtree_err ; # Error output from read-tree (if any)
36 constructor pick {} {
37 global M1T M1B
39 make_toplevel top w
40 wm title $top [mc "Git Gui"]
42 if {$top eq {.}} {
43 menu $w.mbar -tearoff 0
44 $top configure -menu $w.mbar
46 $w.mbar add cascade \
47 -label [mc Repository] \
48 -menu $w.mbar.repository
49 menu $w.mbar.repository
50 $w.mbar.repository add command \
51 -label [mc Quit] \
52 -command exit \
53 -accelerator $M1T-Q
55 if {[is_MacOSX]} {
56 $w.mbar add cascade -label [mc Apple] -menu .mbar.apple
57 menu $w.mbar.apple
58 $w.mbar.apple add command \
59 -label [mc "About %s" [appname]] \
60 -command do_about
61 } else {
62 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
63 menu $w.mbar.help
64 $w.mbar.help add command \
65 -label [mc "About %s" [appname]] \
66 -command do_about
69 _center $top 500 350
70 wm protocol $top WM_DELETE_WINDOW exit
71 bind $top <$M1B-q> exit
72 bind $top <$M1B-Q> exit
73 bind $top <Key-Escape> exit
74 } else {
75 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
76 bind $top <Key-Escape> [list destroy $top]
79 label $w.git_logo \
80 -borderwidth 1 \
81 -relief sunken \
82 -background white \
83 -image ::choose_repository::git_logo
84 pack $w.git_logo -side top -fill x -padx 20 -pady 20
86 set w_body $w.body
87 frame $w_body
88 radiobutton $w_body.new \
89 -anchor w \
90 -text [mc "Create New Repository"] \
91 -variable @action \
92 -value new
93 radiobutton $w_body.clone \
94 -anchor w \
95 -text [mc "Clone Existing Repository"] \
96 -variable @action \
97 -value clone
98 radiobutton $w_body.open \
99 -anchor w \
100 -text [mc "Open Existing Repository"] \
101 -variable @action \
102 -value open
103 pack $w_body.new -anchor w -fill x
104 pack $w_body.clone -anchor w -fill x
105 pack $w_body.open -anchor w -fill x
106 pack $w_body -fill x -padx 10
108 frame $w.buttons
109 set w_next $w.buttons.next
110 button $w_next \
111 -default active \
112 -text [mc "Next >"] \
113 -command [cb _next]
114 pack $w_next -side right -padx 5
115 button $w.buttons.quit \
116 -text [mc "Quit"] \
117 -command exit
118 pack $w.buttons.quit -side right -padx 5
119 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
121 bind $top <Return> [cb _invoke_next]
122 bind $top <Visibility> "
123 grab $top
124 focus $top
126 wm deiconify $top
127 tkwait variable @done
129 if {$top eq {.}} {
130 eval destroy [winfo children $top]
131 _center $top 500 600
135 proc _home {} {
136 if {[catch {set h $::env(HOME)}]
137 || ![file isdirectory $h]} {
138 set h .
140 return $h
143 proc _center {top nx ny} {
144 set rx [expr {([winfo screenwidth $top] - $nx) / 2}]
145 set ry [expr {([winfo screenheight $top] - $ny) / 2}]
146 wm geometry $top [format {%dx%d+%d+%d} $nx $ny $rx $ry]
149 method _invoke_next {} {
150 if {[winfo exists $w_next]} {
151 uplevel #0 [$w_next cget -command]
155 method _next {} {
156 destroy $w_body
157 _do_$action $this
160 method _write_local_path {args} {
161 if {$local_path eq {}} {
162 $w_next conf -state disabled
163 } else {
164 $w_next conf -state normal
168 method _git_init {} {
169 if {[file exists $local_path]} {
170 error_popup [mc "Location %s already exists." $local_path]
171 return 0
174 if {[catch {file mkdir $local_path} err]} {
175 error_popup [strcat \
176 [mc "Failed to create repository %s:" $local_path] \
177 "\n\n$err"]
178 return 0
181 if {[catch {cd $local_path} err]} {
182 error_popup [strcat \
183 [mc "Failed to create repository %s:" $local_path] \
184 "\n\n$err"]
185 return 0
188 if {[catch {git init} err]} {
189 error_popup [strcat \
190 [mc "Failed to create repository %s:" $local_path] \
191 "\n\n$err"]
192 return 0
195 set ::_gitdir .git
196 set ::_prefix {}
197 return 1
200 proc _is_git {path} {
201 if {[file exists [file join $path HEAD]]
202 && [file exists [file join $path objects]]
203 && [file exists [file join $path config]]} {
204 return 1
206 return 0
209 ######################################################################
211 ## Create New Repository
213 method _do_new {} {
214 $w_next conf \
215 -state disabled \
216 -command [cb _do_new2] \
217 -text [mc "Create"]
219 frame $w_body
220 label $w_body.h \
221 -font font_uibold \
222 -text [mc "Create New Repository"]
223 pack $w_body.h -side top -fill x -pady 10
224 pack $w_body -fill x -padx 10
226 frame $w_body.where
227 label $w_body.where.l -text [mc "Directory:"]
228 entry $w_body.where.t \
229 -textvariable @local_path \
230 -font font_diff \
231 -width 50
232 button $w_body.where.b \
233 -text [mc "Browse"] \
234 -command [cb _new_local_path]
236 pack $w_body.where.b -side right
237 pack $w_body.where.l -side left
238 pack $w_body.where.t -fill x
239 pack $w_body.where -fill x
241 trace add variable @local_path write [cb _write_local_path]
242 update
243 focus $w_body.where.t
246 method _new_local_path {} {
247 if {$local_path ne {}} {
248 set p [file dirname $local_path]
249 } else {
250 set p [_home]
253 set p [tk_chooseDirectory \
254 -initialdir $p \
255 -parent $top \
256 -title [mc "Git Repository"] \
257 -mustexist false]
258 if {$p eq {}} return
260 set p [file normalize $p]
261 if {[file isdirectory $p]} {
262 foreach i [glob \
263 -directory $p \
264 -tails \
265 -nocomplain \
266 * .*] {
267 switch -- $i {
268 . continue
269 .. continue
270 default {
271 error_popup [mc "Directory %s already exists." $p]
272 return
276 if {[catch {file delete $p} err]} {
277 error_popup [strcat \
278 [mc "Directory %s already exists." $p] \
279 "\n\n$err"]
280 return
282 } elseif {[file exists $p]} {
283 error_popup [mc "File %s already exists." $p]
284 return
286 set local_path $p
289 method _do_new2 {} {
290 if {![_git_init $this]} {
291 return
293 set done 1
296 ######################################################################
298 ## Clone Existing Repository
300 method _do_clone {} {
301 $w_next conf \
302 -state disabled \
303 -command [cb _do_clone2] \
304 -text [mc "Clone"]
306 frame $w_body
307 label $w_body.h \
308 -font font_uibold \
309 -text [mc "Clone Existing Repository"]
310 pack $w_body.h -side top -fill x -pady 10
311 pack $w_body -fill x -padx 10
313 set args $w_body.args
314 frame $w_body.args
315 pack $args -fill both
317 label $args.origin_l -text [mc "URL:"]
318 entry $args.origin_t \
319 -textvariable @origin_url \
320 -font font_diff \
321 -width 50
322 button $args.origin_b \
323 -text [mc "Browse"] \
324 -command [cb _open_origin]
325 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
327 label $args.where_l -text [mc "Directory:"]
328 entry $args.where_t \
329 -textvariable @local_path \
330 -font font_diff \
331 -width 50
332 button $args.where_b \
333 -text [mc "Browse"] \
334 -command [cb _new_local_path]
335 grid $args.where_l $args.where_t $args.where_b -sticky ew
337 label $args.type_l -text [mc "Clone Type:"]
338 frame $args.type_f
339 set w_types [list]
340 lappend w_types [radiobutton $args.type_f.hardlink \
341 -state disabled \
342 -anchor w \
343 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
344 -variable @clone_type \
345 -value hardlink]
346 lappend w_types [radiobutton $args.type_f.full \
347 -state disabled \
348 -anchor w \
349 -text [mc "Full Copy (Slower, Redundant Backup)"] \
350 -variable @clone_type \
351 -value full]
352 lappend w_types [radiobutton $args.type_f.shared \
353 -state disabled \
354 -anchor w \
355 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
356 -variable @clone_type \
357 -value shared]
358 foreach r $w_types {
359 pack $r -anchor w
361 grid $args.type_l $args.type_f -sticky new
363 grid columnconfigure $args 1 -weight 1
365 trace add variable @local_path write [cb _update_clone]
366 trace add variable @origin_url write [cb _update_clone]
367 update
368 focus $args.origin_t
371 method _open_origin {} {
372 if {$origin_url ne {} && [file isdirectory $origin_url]} {
373 set p $origin_url
374 } else {
375 set p [_home]
378 set p [tk_chooseDirectory \
379 -initialdir $p \
380 -parent $top \
381 -title [mc "Git Repository"] \
382 -mustexist true]
383 if {$p eq {}} return
385 set p [file normalize $p]
386 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
387 error_popup [mc "Not a Git repository: %s" [file tail $p]]
388 return
390 set origin_url $p
393 method _update_clone {args} {
394 if {$local_path ne {} && $origin_url ne {}} {
395 $w_next conf -state normal
396 } else {
397 $w_next conf -state disabled
400 if {$origin_url ne {} &&
401 ( [_is_git [file join $origin_url .git]]
402 || [_is_git $origin_url])} {
403 set e normal
404 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
405 set clone_type hardlink
407 } else {
408 set e disabled
409 set clone_type full
412 foreach r $w_types {
413 $r conf -state $e
417 method _do_clone2 {} {
418 if {[file isdirectory $origin_url]} {
419 set origin_url [file normalize $origin_url]
422 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
423 error_popup [mc "Standard only available for local repository."]
424 return
426 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
427 error_popup [mc "Shared only available for local repository."]
428 return
431 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
432 set objdir [file join $origin_url .git objects]
433 if {![file isdirectory $objdir]} {
434 set objdir [file join $origin_url objects]
435 if {![file isdirectory $objdir]} {
436 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
437 return
442 set giturl $origin_url
443 if {[is_Cygwin] && [file isdirectory $giturl]} {
444 set giturl [exec cygpath --unix --absolute $giturl]
445 if {$clone_type eq {shared}} {
446 set objdir [exec cygpath --unix --absolute $objdir]
450 if {![_git_init $this]} return
451 set local_path [pwd]
453 if {[catch {
454 git config remote.$origin_name.url $giturl
455 git config remote.$origin_name.fetch +refs/heads/*:refs/remotes/$origin_name/*
456 } err]} {
457 error_popup [strcat [mc "Failed to configure origin"] "\n\n$err"]
458 return
461 destroy $w_body $w_next
463 switch -exact -- $clone_type {
464 hardlink {
465 set tolink [list]
466 file mkdir [file join .git objects pack]
467 foreach i [glob -tails -nocomplain \
468 -directory [file join $objdir pack] *] {
469 lappend tolink [file join pack $i]
471 foreach i [glob -tails -nocomplain \
472 -directory [file join $objdir] ??] {
473 file mkdir [file join .git objects $i]
474 foreach j [glob -tails -nocomplain \
475 -directory [file join $objdir $i] *] {
476 lappend tolink [file join $i $j]
480 if {$tolink eq {}} {
481 info_popup [strcat \
482 [mc "Nothing to clone from %s." $origin_url] \
483 "\n" \
484 [mc "The 'master' branch has not been initialized."] \
486 set done 1
487 return
490 set o_cons [status_bar::new $w_body]
491 pack $w_body -fill x -padx 10
493 set i [lindex $tolink 0]
494 if {[catch {
495 file link -hard \
496 [file join .git objects $i] \
497 [file join $objdir $i]
498 } err]} {
499 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
500 set i [_copy_files $this $objdir $tolink]
501 } else {
502 set i [_link_files $this $objdir [lrange $tolink 1 end]]
504 if {!$i} return
506 destroy $w_body
508 full {
509 set o_cons [console::embed \
510 $w_body \
511 [mc "Cloning from %s" $origin_url]]
512 pack $w_body -fill both -expand 1 -padx 10
513 $o_cons exec \
514 [list git fetch --no-tags -k $origin_name] \
515 [cb _do_clone_tags]
517 shared {
518 set fd [open [gitdir objects info alternates] w]
519 fconfigure $fd -translation binary
520 puts $fd $objdir
521 close $fd
525 if {$clone_type eq {hardlink} || $clone_type eq {shared}} {
526 if {![_clone_refs $this]} return
527 set pwd [pwd]
528 if {[catch {
529 cd $origin_url
530 set HEAD [git rev-parse --verify HEAD^0]
531 } err]} {
532 _clone_failed $this [mc "Not a Git repository: %s" [file tail $origin_url]]
533 return 0
535 cd $pwd
536 _do_clone_checkout $this $HEAD
540 method _copy_files {objdir tocopy} {
541 $o_cons start \
542 [mc "Copying objects"] \
543 [mc "KiB"]
544 set tot 0
545 set cmp 0
546 foreach p $tocopy {
547 incr tot [file size [file join $objdir $p]]
549 foreach p $tocopy {
550 if {[catch {
551 set f_in [open [file join $objdir $p] r]
552 set f_cp [open [file join .git objects $p] w]
553 fconfigure $f_in -translation binary -encoding binary
554 fconfigure $f_cp -translation binary -encoding binary
556 while {![eof $f_in]} {
557 incr cmp [fcopy $f_in $f_cp -size 16384]
558 $o_cons update \
559 [expr {$cmp / 1024}] \
560 [expr {$tot / 1024}]
561 update
564 close $f_in
565 close $f_cp
566 } err]} {
567 _clone_failed $this [mc "Unable to copy object: %s" $err]
568 return 0
571 return 1
574 method _link_files {objdir tolink} {
575 set total [llength $tolink]
576 $o_cons start \
577 [mc "Linking objects"] \
578 [mc "objects"]
579 for {set i 0} {$i < $total} {} {
580 set p [lindex $tolink $i]
581 if {[catch {
582 file link -hard \
583 [file join .git objects $p] \
584 [file join $objdir $p]
585 } err]} {
586 _clone_failed $this [mc "Unable to hardlink object: %s" $err]
587 return 0
590 incr i
591 if {$i % 5 == 0} {
592 $o_cons update $i $total
593 update
596 return 1
599 method _clone_refs {} {
600 set pwd [pwd]
601 if {[catch {cd $origin_url} err]} {
602 error_popup [mc "Not a Git repository: %s" [file tail $origin_url]]
603 return 0
605 set fd_in [git_read for-each-ref \
606 --tcl \
607 {--format=list %(refname) %(objectname) %(*objectname)}]
608 cd $pwd
610 set fd [open [gitdir packed-refs] w]
611 fconfigure $fd -translation binary
612 puts $fd "# pack-refs with: peeled"
613 while {[gets $fd_in line] >= 0} {
614 set line [eval $line]
615 set refn [lindex $line 0]
616 set robj [lindex $line 1]
617 set tobj [lindex $line 2]
619 if {[regsub ^refs/heads/ $refn \
620 "refs/remotes/$origin_name/" refn]} {
621 puts $fd "$robj $refn"
622 } elseif {[string match refs/tags/* $refn]} {
623 puts $fd "$robj $refn"
624 if {$tobj ne {}} {
625 puts $fd "^$tobj"
629 close $fd_in
630 close $fd
631 return 1
634 method _do_clone_tags {ok} {
635 if {$ok} {
636 $o_cons exec \
637 [list git fetch --tags -k $origin_name] \
638 [cb _do_clone_HEAD]
639 } else {
640 $o_cons done $ok
641 _clone_failed $this [mc "Cannot fetch branches and objects. See console output for details."]
645 method _do_clone_HEAD {ok} {
646 if {$ok} {
647 $o_cons exec \
648 [list git fetch $origin_name HEAD] \
649 [cb _do_clone_full_end]
650 } else {
651 $o_cons done $ok
652 _clone_failed $this [mc "Cannot fetch tags. See console output for details."]
656 method _do_clone_full_end {ok} {
657 $o_cons done $ok
659 if {$ok} {
660 destroy $w_body
662 set HEAD {}
663 if {[file exists [gitdir FETCH_HEAD]]} {
664 set fd [open [gitdir FETCH_HEAD] r]
665 while {[gets $fd line] >= 0} {
666 if {[regexp "^(.{40})\t\t" $line line HEAD]} {
667 break
670 close $fd
673 catch {git pack-refs}
674 _do_clone_checkout $this $HEAD
675 } else {
676 _clone_failed $this [mc "Cannot determine HEAD. See console output for details."]
680 method _clone_failed {{why {}}} {
681 if {[catch {file delete -force $local_path} err]} {
682 set why [strcat \
683 $why \
684 "\n\n" \
685 [mc "Unable to cleanup %s" $local_path] \
686 "\n\n" \
687 $err]
689 if {$why ne {}} {
690 update
691 error_popup [strcat [mc "Clone failed."] "\n" $why]
695 method _do_clone_checkout {HEAD} {
696 if {$HEAD eq {}} {
697 info_popup [strcat \
698 [mc "No default branch obtained."] \
699 "\n" \
700 [mc "The 'master' branch has not been initialized."] \
702 set done 1
703 return
705 if {[catch {
706 git update-ref HEAD $HEAD^0
707 } err]} {
708 info_popup [strcat \
709 [mc "Cannot resolve %s as a commit." $HEAD^0] \
710 "\n $err" \
711 "\n" \
712 [mc "The 'master' branch has not been initialized."] \
714 set done 1
715 return
718 set o_cons [status_bar::new $w_body]
719 pack $w_body -fill x -padx 10
720 $o_cons start \
721 [mc "Creating working directory"] \
722 [mc "files"]
724 set readtree_err {}
725 set fd [git_read --stderr read-tree \
726 -m \
727 -u \
728 -v \
729 HEAD \
730 HEAD \
732 fconfigure $fd -blocking 0 -translation binary
733 fileevent $fd readable [cb _readtree_wait $fd]
736 method _readtree_wait {fd} {
737 set buf [read $fd]
738 $o_cons update_meter $buf
739 append readtree_err $buf
741 fconfigure $fd -blocking 1
742 if {![eof $fd]} {
743 fconfigure $fd -blocking 0
744 return
747 if {[catch {close $fd}]} {
748 set err $readtree_err
749 regsub {^fatal: } $err {} err
750 error_popup [strcat \
751 [mc "Initial file checkout failed."] \
752 "\n\n$err"]
753 return
756 set done 1
759 ######################################################################
761 ## Open Existing Repository
763 method _do_open {} {
764 $w_next conf \
765 -state disabled \
766 -command [cb _do_open2] \
767 -text [mc "Open"]
769 frame $w_body
770 label $w_body.h \
771 -font font_uibold \
772 -text [mc "Open Existing Repository"]
773 pack $w_body.h -side top -fill x -pady 10
774 pack $w_body -fill x -padx 10
776 frame $w_body.where
777 label $w_body.where.l -text [mc "Repository:"]
778 entry $w_body.where.t \
779 -textvariable @local_path \
780 -font font_diff \
781 -width 50
782 button $w_body.where.b \
783 -text [mc "Browse"] \
784 -command [cb _open_local_path]
786 pack $w_body.where.b -side right
787 pack $w_body.where.l -side left
788 pack $w_body.where.t -fill x
789 pack $w_body.where -fill x
791 trace add variable @local_path write [cb _write_local_path]
792 update
793 focus $w_body.where.t
796 method _open_local_path {} {
797 if {$local_path ne {}} {
798 set p $local_path
799 } else {
800 set p [_home]
803 set p [tk_chooseDirectory \
804 -initialdir $p \
805 -parent $top \
806 -title [mc "Git Repository"] \
807 -mustexist true]
808 if {$p eq {}} return
810 set p [file normalize $p]
811 if {![_is_git [file join $p .git]]} {
812 error_popup [mc "Not a Git repository: %s" [file tail $p]]
813 return
815 set local_path $p
818 method _do_open2 {} {
819 if {![_is_git [file join $local_path .git]]} {
820 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
821 return
824 if {[catch {cd $local_path} err]} {
825 error_popup [strcat \
826 [mc "Failed to open repository %s:" $local_path] \
827 "\n\n$err"]
828 return
831 set ::_gitdir .git
832 set ::_prefix {}
833 set done 1