Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 1 | # git-gui branch create support |
| 2 | # Copyright (C) 2006, 2007 Shawn Pearce |
| 3 | |
| 4 | class branch_create { |
| 5 | |
| 6 | field w ; # widget path |
| 7 | field w_rev ; # mega-widget to pick the initial revision |
| 8 | field w_name ; # new branch name widget |
| 9 | |
| 10 | field name {}; # name of the branch the user has chosen |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 11 | field name_type user; # type of branch name to use |
| 12 | |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 13 | field opt_merge ff; # type of merge to apply to existing branch |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 14 | field opt_checkout 1; # automatically checkout the new branch? |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 15 | field opt_fetch 1; # refetch tracking branch if used? |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 16 | field reset_ok 0; # did the user agree to reset? |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 17 | |
| 18 | constructor dialog {} { |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 19 | global repo_config use_ttk NS |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 20 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 21 | make_dialog top w |
| 22 | wm withdraw $w |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 23 | wm title $top [append "[appname] ([reponame]): " [mc "Create Branch"]] |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 24 | if {$top ne {.}} { |
| 25 | wm geometry $top "+[winfo rootx .]+[winfo rooty .]" |
| 26 | } |
| 27 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 28 | ${NS}::label $w.header -text [mc "Create New Branch"] \ |
| 29 | -font font_uibold -anchor center |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 30 | pack $w.header -side top -fill x |
| 31 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 32 | ${NS}::frame $w.buttons |
| 33 | ${NS}::button $w.buttons.create -text [mc Create] \ |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 34 | -default active \ |
| 35 | -command [cb _create] |
| 36 | pack $w.buttons.create -side right |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 37 | ${NS}::button $w.buttons.cancel -text [mc Cancel] \ |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 38 | -command [list destroy $w] |
| 39 | pack $w.buttons.cancel -side right -padx 5 |
| 40 | pack $w.buttons -side bottom -fill x -pady 10 -padx 10 |
| 41 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 42 | ${NS}::labelframe $w.desc -text [mc "Branch Name"] |
| 43 | ${NS}::radiobutton $w.desc.name_r \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 44 | -text [mc "Name:"] \ |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 45 | -value user \ |
| 46 | -variable @name_type |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 47 | if {!$use_ttk} {$w.desc.name_r configure -anchor w} |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 48 | set w_name $w.desc.name_t |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 49 | ${NS}::entry $w_name \ |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 50 | -width 40 \ |
| 51 | -textvariable @name \ |
| 52 | -validate key \ |
| 53 | -validatecommand [cb _validate %d %S] |
| 54 | grid $w.desc.name_r $w_name -sticky we -padx {0 5} |
| 55 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 56 | ${NS}::radiobutton $w.desc.match_r \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 57 | -text [mc "Match Tracking Branch Name"] \ |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 58 | -value match \ |
| 59 | -variable @name_type |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 60 | if {!$use_ttk} {$w.desc.match_r configure -anchor w} |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 61 | grid $w.desc.match_r -sticky we -padx {0 5} -columnspan 2 |
| 62 | |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 63 | grid columnconfigure $w.desc 1 -weight 1 |
| 64 | pack $w.desc -anchor nw -fill x -pady 5 -padx 5 |
| 65 | |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 66 | set w_rev [::choose_rev::new $w.rev [mc "Starting Revision"]] |
Shawn O. Pearce | 7618e6b | 2007-07-04 16:38:13 -0400 | [diff] [blame] | 67 | pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5 |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 68 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 69 | ${NS}::labelframe $w.options -text [mc Options] |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 70 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 71 | ${NS}::frame $w.options.merge |
| 72 | ${NS}::label $w.options.merge.l -text [mc "Update Existing Branch:"] |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 73 | pack $w.options.merge.l -side left |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 74 | ${NS}::radiobutton $w.options.merge.no \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 75 | -text [mc No] \ |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 76 | -value none \ |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 77 | -variable @opt_merge |
| 78 | pack $w.options.merge.no -side left |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 79 | ${NS}::radiobutton $w.options.merge.ff \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 80 | -text [mc "Fast Forward Only"] \ |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 81 | -value ff \ |
| 82 | -variable @opt_merge |
| 83 | pack $w.options.merge.ff -side left |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 84 | ${NS}::radiobutton $w.options.merge.reset \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 85 | -text [mc Reset] \ |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 86 | -value reset \ |
| 87 | -variable @opt_merge |
| 88 | pack $w.options.merge.reset -side left |
| 89 | pack $w.options.merge -anchor nw |
| 90 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 91 | ${NS}::checkbutton $w.options.fetch \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 92 | -text [mc "Fetch Tracking Branch"] \ |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 93 | -variable @opt_fetch |
| 94 | pack $w.options.fetch -anchor nw |
| 95 | |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 96 | ${NS}::checkbutton $w.options.checkout \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 97 | -text [mc "Checkout After Creation"] \ |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 98 | -variable @opt_checkout |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 99 | pack $w.options.checkout -anchor nw |
| 100 | pack $w.options -anchor nw -fill x -pady 5 -padx 5 |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 101 | |
Shawn O. Pearce | 7cf0442 | 2007-07-05 01:07:06 -0400 | [diff] [blame] | 102 | trace add variable @name_type write [cb _select] |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 103 | |
Shawn O. Pearce | 7cf0442 | 2007-07-05 01:07:06 -0400 | [diff] [blame] | 104 | set name $repo_config(gui.newbranchtemplate) |
| 105 | if {[is_config_true gui.matchtrackingbranch]} { |
| 106 | set name_type match |
| 107 | } |
| 108 | |
| 109 | bind $w <Visibility> [cb _visible] |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 110 | bind $w <Key-Escape> [list destroy $w] |
| 111 | bind $w <Key-Return> [cb _create]\;break |
Pat Thoyts | c80d7be | 2010-01-26 00:05:31 +0000 | [diff] [blame] | 112 | wm deiconify $w |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 113 | tkwait window $w |
| 114 | } |
| 115 | |
| 116 | method _create {} { |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 117 | global repo_config |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 118 | global M1B |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 119 | |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 120 | set spec [$w_rev get_tracking_branch] |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 121 | switch -- $name_type { |
| 122 | user { |
| 123 | set newbranch $name |
| 124 | } |
| 125 | match { |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 126 | if {$spec eq {}} { |
| 127 | tk_messageBox \ |
| 128 | -icon error \ |
| 129 | -type ok \ |
| 130 | -title [wm title $w] \ |
| 131 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 132 | -message [mc "Please select a tracking branch."] |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 133 | return |
| 134 | } |
| 135 | if {![regsub ^refs/heads/ [lindex $spec 2] {} newbranch]} { |
| 136 | tk_messageBox \ |
| 137 | -icon error \ |
| 138 | -type ok \ |
| 139 | -title [wm title $w] \ |
| 140 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 141 | -message [mc "Tracking branch %s is not a branch in the remote repository." [$w get]] |
Shawn O. Pearce | dd87efc | 2007-07-04 02:19:53 -0400 | [diff] [blame] | 142 | return |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 147 | if {$newbranch eq {} |
| 148 | || $newbranch eq $repo_config(gui.newbranchtemplate)} { |
| 149 | tk_messageBox \ |
| 150 | -icon error \ |
| 151 | -type ok \ |
| 152 | -title [wm title $w] \ |
| 153 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 154 | -message [mc "Please supply a branch name."] |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 155 | focus $w_name |
| 156 | return |
| 157 | } |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 158 | |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 159 | if {[catch {git check-ref-format "heads/$newbranch"}]} { |
| 160 | tk_messageBox \ |
| 161 | -icon error \ |
| 162 | -type ok \ |
| 163 | -title [wm title $w] \ |
| 164 | -parent $w \ |
Christian Stimming | 1ac1795 | 2007-07-21 14:21:34 +0200 | [diff] [blame] | 165 | -message [mc "'%s' is not an acceptable branch name." $newbranch] |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 166 | focus $w_name |
| 167 | return |
| 168 | } |
| 169 | |
Shawn O. Pearce | ba1964b | 2007-07-05 02:23:53 -0400 | [diff] [blame] | 170 | if {$spec ne {} && $opt_fetch} { |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 171 | set new {} |
| 172 | } elseif {[catch {set new [$w_rev commit_or_die]}]} { |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 173 | return |
| 174 | } |
| 175 | |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 176 | set co [::checkout_op::new \ |
| 177 | [$w_rev get] \ |
| 178 | $new \ |
| 179 | refs/heads/$newbranch] |
| 180 | $co parent $w |
| 181 | $co enable_create 1 |
| 182 | $co enable_merge $opt_merge |
| 183 | $co enable_checkout $opt_checkout |
| 184 | if {$spec ne {} && $opt_fetch} { |
| 185 | $co enable_fetch $spec |
| 186 | } |
Shawn O. Pearce | fe70225 | 2008-05-08 20:16:43 -0400 | [diff] [blame] | 187 | if {$spec ne {}} { |
| 188 | $co remote_source $spec |
| 189 | } |
Shawn O. Pearce | d41b43e | 2007-07-08 18:40:56 -0400 | [diff] [blame] | 190 | |
| 191 | if {[$co run]} { |
| 192 | destroy $w |
| 193 | } else { |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 194 | focus $w_name |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 195 | } |
Shawn O. Pearce | 774173a | 2007-07-04 04:21:57 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 198 | method _validate {d S} { |
| 199 | if {$d == 1} { |
| 200 | if {[regexp {[~^:?*\[\0- ]} $S]} { |
| 201 | return 0 |
| 202 | } |
| 203 | if {[string length $S] > 0} { |
| 204 | set name_type user |
| 205 | } |
| 206 | } |
| 207 | return 1 |
| 208 | } |
| 209 | |
Shawn O. Pearce | 7cf0442 | 2007-07-05 01:07:06 -0400 | [diff] [blame] | 210 | method _select {args} { |
| 211 | if {$name_type eq {match}} { |
| 212 | $w_rev pick_tracking_branch |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | method _visible {} { |
| 217 | grab $w |
| 218 | if {$name_type eq {user}} { |
| 219 | $w_name icursor end |
| 220 | focus $w_name |
| 221 | } |
| 222 | } |
| 223 | |
Shawn O. Pearce | b1fa2bf | 2007-07-03 22:57:18 -0400 | [diff] [blame] | 224 | } |