Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 1 | # git-gui branch checkout support |
| 2 | # Copyright (C) 2007 Shawn Pearce |
| 3 | |
| 4 | class branch_checkout { |
| 5 | |
| 6 | field w ; # widget path |
| 7 | field w_rev ; # mega-widget to pick the initial revision |
| 8 | |
| 9 | field opt_fetch 1; # refetch tracking branch if used? |
| 10 | field opt_detach 0; # force a detached head case? |
| 11 | |
| 12 | constructor dialog {} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 13 | global use_ttk NS |
| 14 | make_dialog top w |
| 15 | wm withdraw $w |
Vasco Almeida | a3d97af | 2016-05-08 10:52:57 +0000 | [diff] [blame] | 16 | wm title $top [mc "%s (%s): Checkout Branch" [appname] [reponame]] |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 17 | if {$top ne {.}} { |
| 18 | wm geometry $top "+[winfo rootx .]+[winfo rooty .]" |
| 19 | } |
| 20 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 21 | ${NS}::label $w.header -text [mc "Checkout Branch"] \ |
| 22 | -font font_uibold -anchor center |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 23 | pack $w.header -side top -fill x |
| 24 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 25 | ${NS}::frame $w.buttons |
| 26 | ${NS}::button $w.buttons.create -text [mc Checkout] \ |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 27 | -default active \ |
| 28 | -command [cb _checkout] |
| 29 | pack $w.buttons.create -side right |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 30 | ${NS}::button $w.buttons.cancel -text [mc Cancel] \ |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 31 | -command [list destroy $w] |
| 32 | pack $w.buttons.cancel -side right -padx 5 |
| 33 | pack $w.buttons -side bottom -fill x -pady 10 -padx 10 |
| 34 | |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 35 | set w_rev [::choose_rev::new $w.rev [mc Revision]] |
Shawn O. Pearce | 827c711 | 2007-07-08 21:34:28 -0400 | [diff] [blame] | 36 | $w_rev bind_listbox <Double-Button-1> [cb _checkout] |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 37 | pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5 |
| 38 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 39 | ${NS}::labelframe $w.options -text [mc Options] |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 40 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 41 | ${NS}::checkbutton $w.options.fetch \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 42 | -text [mc "Fetch Tracking Branch"] \ |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 43 | -variable @opt_fetch |
| 44 | pack $w.options.fetch -anchor nw |
| 45 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 46 | ${NS}::checkbutton $w.options.detach \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 47 | -text [mc "Detach From Local Branch"] \ |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 48 | -variable @opt_detach |
| 49 | pack $w.options.detach -anchor nw |
| 50 | |
| 51 | pack $w.options -anchor nw -fill x -pady 5 -padx 5 |
| 52 | |
| 53 | bind $w <Visibility> [cb _visible] |
| 54 | bind $w <Key-Escape> [list destroy $w] |
| 55 | bind $w <Key-Return> [cb _checkout]\;break |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 56 | wm deiconify $w |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 57 | tkwait window $w |
| 58 | } |
| 59 | |
| 60 | method _checkout {} { |
| 61 | set spec [$w_rev get_tracking_branch] |
| 62 | if {$spec ne {} && $opt_fetch} { |
| 63 | set new {} |
| 64 | } elseif {[catch {set new [$w_rev commit_or_die]}]} { |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | if {$opt_detach} { |
| 69 | set ref {} |
| 70 | } else { |
| 71 | set ref [$w_rev get_local_branch] |
| 72 | } |
| 73 | |
| 74 | set co [::checkout_op::new [$w_rev get] $new $ref] |
| 75 | $co parent $w |
| 76 | $co enable_checkout 1 |
| 77 | if {$spec ne {} && $opt_fetch} { |
| 78 | $co enable_fetch $spec |
| 79 | } |
| 80 | |
| 81 | if {[$co run]} { |
| 82 | destroy $w |
| 83 | } else { |
| 84 | $w_rev focus_filter |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | method _visible {} { |
| 89 | grab $w |
| 90 | $w_rev focus_filter |
| 91 | } |
| 92 | |
| 93 | } |