Raw
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
13 field w_types ; # List of type buttons in clone
14 field w_recentlist ; # Listbox containing recent repositories
15 field w_localpath ; # Entry widget bound to local_path
16
17 field done 0 ; # Finished picking the repository?
18 field pick_ok 0 ; # true if repo pick/clone succeeded
19 field local_path {} ; # Where this repository is locally
20 field origin_url {} ; # Where we are cloning from
21 field origin_name origin ; # What we shall call 'origin'
22 field clone_type hardlink ; # Type of clone to construct
23 field recursive true ; # Recursive cloning flag
24 field readtree_err ; # Error output from read-tree (if any)
25 field sorted_recent ; # recent repositories (sorted)
26
27 constructor pick {} {
28 global M1T M1B
29
30 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
31 set maxrecent 10
32 }
33
34 make_dialog top w
35 wm title $top [mc "Git Gui"]
36
37 if {$top eq {.}} {
38 menu $w.mbar -tearoff 0
39 $top configure -menu $w.mbar
40
41 set m_repo $w.mbar.repository
42 $w.mbar add cascade \
43 -label [mc Repository] \
44 -menu $m_repo
45 menu $m_repo
46
47 if {[is_MacOSX]} {
48 $w.mbar add cascade -label Apple -menu .mbar.apple
49 menu $w.mbar.apple
50 $w.mbar.apple add command \
51 -label [mc "About %s" [appname]] \
52 -command do_about
53 $w.mbar.apple add command \
54 -label [mc "Show SSH Key"] \
55 -command do_ssh_key
56 } else {
57 $w.mbar add cascade -label [mc Help] -menu $w.mbar.help
58 menu $w.mbar.help
59 $w.mbar.help add command \
60 -label [mc "About %s" [appname]] \
61 -command do_about
62 $w.mbar.help add command \
63 -label [mc "Show SSH Key"] \
64 -command do_ssh_key
65 }
66
67 wm protocol $top WM_DELETE_WINDOW exit
68 bind $top <$M1B-q> exit
69 bind $top <$M1B-Q> exit
70 bind $top <Key-Escape> exit
71 } else {
72 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
73 bind $top <Key-Escape> [list destroy $top]
74 set m_repo {}
75 }
76
77 pack [git_logo $w.git_logo] -side left -fill y -padx 10 -pady 10
78
79 set w_body $w.body
80 set opts $w_body.options
81 ttk::frame $w_body
82 text $opts \
83 -cursor $::cursor_ptr \
84 -relief flat \
85 -background [get_bg_color $w_body] \
86 -wrap none \
87 -spacing1 5 \
88 -width 50 \
89 -height 3
90 pack $opts -anchor w -fill x
91
92 $opts tag conf link_new -foreground blue -underline 1
93 $opts tag bind link_new <1> [cb _next new]
94 $opts insert end [mc "Create New Repository"] link_new
95 $opts insert end "\n"
96 if {$m_repo ne {}} {
97 $m_repo add command \
98 -command [cb _next new] \
99 -accelerator $M1T-N \
100 -label [mc "New..."]
101 bind $top <$M1B-n> [cb _next new]
102 bind $top <$M1B-N> [cb _next new]
103 }
104
105 $opts tag conf link_clone -foreground blue -underline 1
106 $opts tag bind link_clone <1> [cb _next clone]
107 $opts insert end [mc "Clone Existing Repository"] link_clone
108 $opts insert end "\n"
109 if {$m_repo ne {}} {
110 if {[tk windowingsystem] eq "win32"} {
111 set key L
112 } else {
113 set key C
114 }
115 $m_repo add command \
116 -command [cb _next clone] \
117 -accelerator $M1T-$key \
118 -label [mc "Clone..."]
119 bind $top <$M1B-[string tolower $key]> [cb _next clone]
120 bind $top <$M1B-[string toupper $key]> [cb _next clone]
121 }
122
123 $opts tag conf link_open -foreground blue -underline 1
124 $opts tag bind link_open <1> [cb _next open]
125 $opts insert end [mc "Open Existing Repository"] link_open
126 $opts insert end "\n"
127 if {$m_repo ne {}} {
128 $m_repo add command \
129 -command [cb _next open] \
130 -accelerator $M1T-O \
131 -label [mc "Open..."]
132 bind $top <$M1B-o> [cb _next open]
133 bind $top <$M1B-O> [cb _next open]
134 }
135
136 $opts conf -state disabled
137
138 set sorted_recent [_get_recentrepos]
139 if {[llength $sorted_recent] > 0} {
140 if {$m_repo ne {}} {
141 $m_repo add separator
142 $m_repo add command \
143 -state disabled \
144 -label [mc "Recent Repositories"]
145 }
146
147 if {[set lenrecent [llength $sorted_recent]] < $maxrecent} {
148 set lenrecent $maxrecent
149 }
150
151 ttk::label $w_body.space
152 ttk::label $w_body.recentlabel \
153 -anchor w \
154 -text [mc "Open Recent Repository:"]
155 set w_recentlist $w_body.recentlist
156 text $w_recentlist \
157 -cursor $::cursor_ptr \
158 -relief flat \
159 -background [get_bg_color $w_body.recentlabel] \
160 -wrap none \
161 -width 50 \
162 -height $lenrecent
163 $w_recentlist tag conf link \
164 -foreground blue \
165 -underline 1
166 set home $::env(HOME)
167 set home "[file normalize $home]/"
168 set hlen [string length $home]
169 foreach p $sorted_recent {
170 set path $p
171 if {[string equal -length $hlen $home $p]} {
172 set p "~/[string range $p $hlen end]"
173 }
174 regsub -all "\n" $p "\\n" p
175 $w_recentlist insert end $p link
176 $w_recentlist insert end "\n"
177
178 if {$m_repo ne {}} {
179 $m_repo add command \
180 -command [cb _open_recent_path $path] \
181 -label " $p"
182 }
183 }
184 $w_recentlist conf -state disabled
185 $w_recentlist tag bind link <1> [cb _open_recent %x,%y]
186 pack $w_body.space -anchor w -fill x
187 pack $w_body.recentlabel -anchor w -fill x
188 pack $w_recentlist -anchor w -fill x
189 }
190 pack $w_body -fill x -padx 10 -pady 10
191
192 ttk::frame $w.buttons
193 set w_next $w.buttons.next
194 set w_quit $w.buttons.quit
195 ttk::button $w_quit \
196 -text [mc "Quit"] \
197 -command exit
198 pack $w_quit -side right -padx 5
199 pack $w.buttons -side bottom -fill x -padx 10 -pady 10
200
201 if {$m_repo ne {}} {
202 $m_repo add separator
203 $m_repo add command \
204 -label [mc Quit] \
205 -command exit \
206 -accelerator $M1T-Q
207 }
208
209 bind $top <Return> [cb _invoke_next]
210 bind $top <Visibility> "
211 [cb _center]
212 grab $top
213 focus $top
214 bind $top <Visibility> {}
215 "
216 wm deiconify $top
217 tkwait variable @done
218
219 grab release $top
220 if {$top eq {.}} {
221 eval destroy [winfo children $top]
222 }
223
224 return $pick_ok
225 }
226
227 method _center {} {
228 set nx [winfo reqwidth $top]
229 set ny [winfo reqheight $top]
230 set rx [expr {([winfo screenwidth $top] - $nx) / 3}]
231 set ry [expr {([winfo screenheight $top] - $ny) / 3}]
232 wm geometry $top [format {+%d+%d} $rx $ry]
233 }
234
235 method _invoke_next {} {
236 if {[winfo exists $w_next]} {
237 uplevel #0 [$w_next cget -command]
238 }
239 }
240
241 proc _get_recentrepos {} {
242 set recent [list]
243 foreach p [lsort -unique [get_config gui.recentrepo]] {
244 if {[_is_git [file join $p .git]]} {
245 lappend recent $p
246 } else {
247 _unset_recentrepo $p
248 }
249 }
250 return $recent
251 }
252
253 proc _unset_recentrepo {p} {
254 regsub -all -- {([()\[\]{}\.^$+*?\\])} $p {\\\1} p
255 catch {git config --global --unset-all gui.recentrepo "^$p\$"}
256 load_config 1
257 }
258
259 proc _append_recentrepos {path} {
260 set path [file normalize $path]
261 set recent [get_config gui.recentrepo]
262
263 if {[lindex $recent end] eq $path} {
264 return
265 }
266
267 set i [lsearch $recent $path]
268 if {$i >= 0} {
269 _unset_recentrepo $path
270 }
271
272 git config --global --add gui.recentrepo $path
273 load_config 1
274 set recent [get_config gui.recentrepo]
275
276 if {[set maxrecent [get_config gui.maxrecentrepo]] eq {}} {
277 set maxrecent 10
278 }
279
280 while {[llength $recent] > $maxrecent} {
281 _unset_recentrepo [lindex $recent 0]
282 set recent [get_config gui.recentrepo]
283 }
284 }
285
286 method _open_recent {xy} {
287 set id [lindex [split [$w_recentlist index @$xy] .] 0]
288 set local_path [lindex $sorted_recent [expr {$id - 1}]]
289 _do_open2 $this
290 }
291
292 method _open_recent_path {p} {
293 set local_path $p
294 _do_open2 $this
295 }
296
297 method _next {action} {
298 destroy $w_body
299 if {![winfo exists $w_next]} {
300 ttk::button $w_next -default active
301 set pos -before
302 if {[tk windowingsystem] eq "win32"} { set pos -after }
303 pack $w_next -side right -padx 5 $pos $w_quit
304 }
305 _do_$action $this
306 }
307
308 method _write_local_path {args} {
309 if {$local_path eq {}} {
310 $w_next conf -state disabled
311 } else {
312 $w_next conf -state normal
313 }
314 }
315
316 method _git_init {} {
317 if {[catch {git init $local_path} err]} {
318 error_popup [strcat \
319 [mc "Failed to create repository %s:" $local_path] \
320 "\n\n$err"]
321 return 0
322 }
323
324 if {[catch {cd $local_path} err]} {
325 error_popup [strcat \
326 [mc "Failed to create repository %s:" $local_path] \
327 "\n\n$err"]
328 return 0
329 }
330
331 _append_recentrepos [pwd]
332 set pick_ok 1
333 return 1
334 }
335
336 proc _is_git {path {outdir_var ""}} {
337 if {$outdir_var ne ""} {
338 upvar 1 $outdir_var outdir
339 }
340 if {[catch {set outdir [git rev-parse --resolve-git-dir $path]}]} {
341 return 0
342 }
343 return 1
344 }
345
346 ######################################################################
347 ##
348 ## Create New Repository
349
350 method _do_new {} {
351 $w_next conf \
352 -state disabled \
353 -command [cb _do_new2] \
354 -text [mc "Create"]
355
356 ttk::frame $w_body
357 ttk::label $w_body.h \
358 -font font_uibold -anchor center \
359 -text [mc "Create New Repository"]
360 pack $w_body.h -side top -fill x -pady 10
361 pack $w_body -fill x -padx 10
362
363 ttk::frame $w_body.where
364 ttk::label $w_body.where.l -text [mc "Directory:"]
365 ttk::entry $w_body.where.t \
366 -textvariable @local_path \
367 -width 50
368 ttk::button $w_body.where.b \
369 -text [mc "Browse"] \
370 -command [cb _new_local_path]
371 set w_localpath $w_body.where.t
372
373 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
374 pack $w_body.where -fill x
375
376 grid columnconfigure $w_body.where 1 -weight 1
377
378 trace add variable @local_path write [cb _write_local_path]
379 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
380 update
381 focus $w_body.where.t
382 }
383
384 method _new_local_path {} {
385 if {$local_path ne {}} {
386 set p [file dirname $local_path]
387 } else {
388 set p [pwd]
389 }
390
391 set p [tk_chooseDirectory \
392 -initialdir $p \
393 -parent $top \
394 -title [mc "Git Repository"] \
395 -mustexist false]
396 if {$p eq {}} return
397
398 set p [file normalize $p]
399 if {![_new_ok $p]} {
400 return
401 }
402 set local_path $p
403 $w_localpath icursor end
404 }
405
406 method _do_new2 {} {
407 if {![_new_ok $local_path]} {
408 return
409 }
410 if {![_git_init $this]} {
411 return
412 }
413 set pick_ok 1
414 set done 1
415 }
416
417 proc _new_ok {p} {
418 if {[file isdirectory $p]} {
419 if {[_is_git [file join $p .git]]} {
420 error_popup [mc "Directory %s already exists." $p]
421 return 0
422 }
423 } elseif {[file exists $p]} {
424 error_popup [mc "File %s already exists." $p]
425 return 0
426 }
427 return 1
428 }
429
430 ######################################################################
431 ##
432 ## Clone Existing Repository
433
434 method _do_clone {} {
435 $w_next conf \
436 -state disabled \
437 -command [cb _do_clone2] \
438 -text [mc "Clone"]
439
440 ttk::frame $w_body
441 ttk::label $w_body.h \
442 -font font_uibold -anchor center \
443 -text [mc "Clone Existing Repository"]
444 pack $w_body.h -side top -fill x -pady 10
445 pack $w_body -fill x -padx 10
446
447 set args $w_body.args
448 ttk::frame $w_body.args
449 pack $args -fill both
450
451 ttk::label $args.origin_l -text [mc "Source Location:"]
452 ttk::entry $args.origin_t \
453 -textvariable @origin_url \
454 -width 50
455 ttk::button $args.origin_b \
456 -text [mc "Browse"] \
457 -command [cb _open_origin]
458 grid $args.origin_l $args.origin_t $args.origin_b -sticky ew
459
460 ttk::label $args.where_l -text [mc "Target Directory:"]
461 ttk::entry $args.where_t \
462 -textvariable @local_path \
463 -width 50
464 ttk::button $args.where_b \
465 -text [mc "Browse"] \
466 -command [cb _new_local_path]
467 grid $args.where_l $args.where_t $args.where_b -sticky ew
468 set w_localpath $args.where_t
469
470 ttk::label $args.type_l -text [mc "Clone Type:"]
471 ttk::frame $args.type_f
472 set w_types [list]
473 lappend w_types [ttk::radiobutton $args.type_f.hardlink \
474 -state disabled \
475 -text [mc "Standard (Fast, Semi-Redundant, Hardlinks)"] \
476 -variable @clone_type \
477 -value hardlink]
478 lappend w_types [ttk::radiobutton $args.type_f.full \
479 -state disabled \
480 -text [mc "Full Copy (Slower, Redundant Backup)"] \
481 -variable @clone_type \
482 -value full]
483 lappend w_types [ttk::radiobutton $args.type_f.shared \
484 -state disabled \
485 -text [mc "Shared (Fastest, Not Recommended, No Backup)"] \
486 -variable @clone_type \
487 -value shared]
488 foreach r $w_types {
489 pack $r -anchor w
490 }
491 ttk::checkbutton $args.type_f.recursive \
492 -text [mc "Recursively clone submodules too"] \
493 -variable @recursive \
494 -onvalue true -offvalue false
495 pack $args.type_f.recursive -anchor w
496 grid $args.type_l $args.type_f -sticky new
497
498 grid columnconfigure $args 1 -weight 1
499
500 trace add variable @local_path write [cb _update_clone]
501 trace add variable @origin_url write [cb _update_clone]
502 bind $w_body.h <Destroy> "
503 [list trace remove variable @local_path write [cb _update_clone]]
504 [list trace remove variable @origin_url write [cb _update_clone]]
505 "
506 update
507 focus $args.origin_t
508 }
509
510 method _open_origin {} {
511 if {$origin_url ne {} && [file isdirectory $origin_url]} {
512 set p $origin_url
513 } else {
514 set p [pwd]
515 }
516
517 set p [tk_chooseDirectory \
518 -initialdir $p \
519 -parent $top \
520 -title [mc "Git Repository"] \
521 -mustexist true]
522 if {$p eq {}} return
523
524 set p [file normalize $p]
525 if {![_is_git [file join $p .git]] && ![_is_git $p]} {
526 error_popup [mc "Not a Git repository: %s" [file tail $p]]
527 return
528 }
529 set origin_url $p
530 }
531
532 method _update_clone {args} {
533 if {$local_path ne {} && $origin_url ne {}} {
534 $w_next conf -state normal
535 } else {
536 $w_next conf -state disabled
537 }
538
539 if {$origin_url ne {} &&
540 ( [_is_git [file join $origin_url .git]]
541 || [_is_git $origin_url])} {
542 set e normal
543 if {[[lindex $w_types 0] cget -state] eq {disabled}} {
544 set clone_type hardlink
545 }
546 } else {
547 set e disabled
548 set clone_type full
549 }
550
551 foreach r $w_types {
552 $r conf -state $e
553 }
554 }
555
556 method _do_clone2 {} {
557 if {[file isdirectory $origin_url]} {
558 set origin_url [file normalize $origin_url]
559 if {$clone_type eq {hardlink}} {
560 # cannot use hardlinks if this is a linked worktree (.gitfile or git-new-workdir)
561 if {[git -C $origin_url rev-parse --is-inside-work-tree] == {true}} {
562 set islink 0
563 set dotgit [file join $origin_url .git]
564 if {[file isfile $dotgit]} {
565 set islink 1
566 } else {
567 set objdir [file join $dotgit objects]
568 if {[file exists $objdir] && [file type $objdir] == {link}} {
569 set islink 1
570 }
571 }
572 if {$islink} {
573 info_popup [mc "Hardlinks are unavailable. Falling back to copying."]
574 set clone_type full
575 }
576 }
577 }
578 }
579
580 if {$clone_type eq {hardlink} && ![file isdirectory $origin_url]} {
581 error_popup [mc "Standard only available for local repository."]
582 return
583 }
584 if {$clone_type eq {shared} && ![file isdirectory $origin_url]} {
585 error_popup [mc "Shared only available for local repository."]
586 return
587 }
588
589 set giturl $origin_url
590
591 if {[file exists $local_path]} {
592 error_popup [mc "Location %s already exists." $local_path]
593 return
594 }
595
596 set clone_options {--progress}
597 if {$recursive} {
598 append clone_options { --recurse-submodules}
599 }
600
601 destroy $w_body $w_next
602
603 switch -exact -- $clone_type {
604 full {
605 append clone_options { --no-hardlinks --no-local}
606 }
607 shared {
608 append clone_options { --shared}
609 }
610 }
611
612 if {[catch {
613 set o_cons [console::embed \
614 $w_body \
615 [mc "Cloning from %s" $origin_url]]
616 pack $w_body -fill both -expand 1 -padx 10
617 $o_cons exec \
618 [list git clone {*}$clone_options $origin_url $local_path] \
619 [cb _do_clone2_done]
620 } err]} {
621 error_popup [strcat [mc "Clone failed."] "\n" $err]
622 return
623 }
624
625 tkwait variable @done
626 if {!$pick_ok} {
627 error_popup [mc "Clone failed."]
628 return
629 }
630 }
631
632 method _do_clone2_done {ok} {
633 $o_cons done $ok
634 if {$ok} {
635 if {[catch {
636 cd $local_path
637 _append_recentrepos [pwd]
638 } err]} {
639 set ok 0
640 }
641 }
642 set pick_ok $ok
643 set done 1
644 }
645
646
647 ######################################################################
648 ##
649 ## Open Existing Repository
650
651 method _do_open {} {
652 $w_next conf \
653 -state disabled \
654 -command [cb _do_open2] \
655 -text [mc "Open"]
656
657 ttk::frame $w_body
658 ttk::label $w_body.h \
659 -font font_uibold -anchor center \
660 -text [mc "Open Existing Repository"]
661 pack $w_body.h -side top -fill x -pady 10
662 pack $w_body -fill x -padx 10
663
664 ttk::frame $w_body.where
665 ttk::label $w_body.where.l -text [mc "Repository:"]
666 ttk::entry $w_body.where.t \
667 -textvariable @local_path \
668 -width 50
669 ttk::button $w_body.where.b \
670 -text [mc "Browse"] \
671 -command [cb _open_local_path]
672
673 grid $w_body.where.l $w_body.where.t $w_body.where.b -sticky ew
674 pack $w_body.where -fill x
675
676 grid columnconfigure $w_body.where 1 -weight 1
677
678 trace add variable @local_path write [cb _write_local_path]
679 bind $w_body.h <Destroy> [list trace remove variable @local_path write [cb _write_local_path]]
680 update
681 focus $w_body.where.t
682 }
683
684 method _open_local_path {} {
685 if {$local_path ne {}} {
686 set p $local_path
687 } else {
688 set p [pwd]
689 }
690
691 set p [tk_chooseDirectory \
692 -initialdir $p \
693 -parent $top \
694 -title [mc "Git Repository"] \
695 -mustexist true]
696 if {$p eq {}} return
697
698 set p [file normalize $p]
699 if {![_is_git [file join $p .git]]} {
700 error_popup [mc "Not a Git repository: %s" [file tail $p]]
701 return
702 }
703 set local_path $p
704 }
705
706 method _do_open2 {} {
707 if {![_is_git [file join $local_path .git] actualgit]} {
708 error_popup [mc "Not a Git repository: %s" [file tail $local_path]]
709 return
710 }
711
712 if {[catch {cd $local_path} err]} {
713 error_popup [strcat \
714 [mc "Failed to open repository %s:" $local_path] \
715 "\n\n$err"]
716 return
717 }
718
719 _append_recentrepos [pwd]
720 set pick_ok 1
721 set done 1
722 }
723
724 }