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