git-gui: Abstract the revision picker into a mega widget
This rather large change pulls the "Starting Revision" part of the new branch dialog into a mega widget that we can use anytime we need to select a commit SHA-1. To make use of the mega widget I have also refactored the branch dialog to use the class system, much like the delete remote branch dialog already does. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jul 3, 2007 at 22:57 UTC
b1fa2bfff36933f94dada476ba5b3d3cb894b723
4 files changed
+287
-213
git-gui.sh
+3
-3
@@ -1496,7 +1496,7 @@ if {[is_enabled branch]} {
1496
menu .mbar.branch
1497
1498
.mbar.branch add command -label {Create...} \
1499
- -command do_create_branch \
1499
+ -command branch_create::dialog \
1500
-accelerator $M1T-N
1501
lappend disable_on_lock [list .mbar.branch entryconf \
1502
[.mbar.branch index last] -state]
@@ -2221,8 +2221,8 @@ bind $ui_diff <Control-Key-f> {catch {%W yview scroll 1 pages};break}
2221
bind $ui_diff <Button-1> {focus %W}
2222
2223
if {[is_enabled branch]} {
2224
- bind . <$M1B-Key-n> do_create_branch
2225
- bind . <$M1B-Key-N> do_create_branch
2224
+ bind . <$M1B-Key-n> branch_create::dialog
2225
+ bind . <$M1B-Key-N> branch_create::dialog
2226
}
2227
if {[is_enabled transport]} {
2228
bind . <$M1B-Key-p> do_push_anywhere
lib/branch.tcl
-210
@@ -61,221 +61,11 @@ proc populate_branch_menu {} {
61
}
62
}
63
64
-proc do_create_branch_action {w} {
65
- global all_heads null_sha1 repo_config
66
- global create_branch_checkout create_branch_revtype
67
- global create_branch_head create_branch_trackinghead
68
- global create_branch_name create_branch_revexp
69
- global create_branch_tag
70
-
71
- set newbranch $create_branch_name
72
- if {$newbranch eq {}
73
- || $newbranch eq $repo_config(gui.newbranchtemplate)} {
74
- tk_messageBox \
75
- -icon error \
76
- -type ok \
77
- -title [wm title $w] \
78
- -parent $w \
79
- -message "Please supply a branch name."
80
- focus $w.desc.name_t
81
- return
82
- }
83
- if {![catch {git show-ref --verify -- "refs/heads/$newbranch"}]} {
84
- tk_messageBox \
85
- -icon error \
86
- -type ok \
87
- -title [wm title $w] \
88
- -parent $w \
89
- -message "Branch '$newbranch' already exists."
90
- focus $w.desc.name_t
91
- return
92
- }
93
- if {[catch {git check-ref-format "heads/$newbranch"}]} {
94
- tk_messageBox \
95
- -icon error \
96
- -type ok \
97
- -title [wm title $w] \
98
- -parent $w \
99
- -message "We do not like '$newbranch' as a branch name."
100
- focus $w.desc.name_t
101
- return
102
- }
103
-
104
- set rev {}
105
- switch -- $create_branch_revtype {
106
- head {set rev $create_branch_head}
107
- tracking {set rev $create_branch_trackinghead}
108
- tag {set rev $create_branch_tag}
109
- expression {set rev $create_branch_revexp}
110
- }
111
- if {[catch {set cmt [git rev-parse --verify "${rev}^0"]}]} {
112
- tk_messageBox \
113
- -icon error \
114
- -type ok \
115
- -title [wm title $w] \
116
- -parent $w \
117
- -message "Invalid starting revision: $rev"
118
- return
119
- }
120
- if {[catch {
121
- git update-ref \
122
- -m "branch: Created from $rev" \
123
- "refs/heads/$newbranch" \
124
- $cmt \
125
- $null_sha1
126
- } err]} {
127
- tk_messageBox \
128
- -icon error \
129
- -type ok \
130
- -title [wm title $w] \
131
- -parent $w \
132
- -message "Failed to create '$newbranch'.\n\n$err"
133
- return
134
- }
135
-
136
- lappend all_heads $newbranch
137
- set all_heads [lsort $all_heads]
138
- populate_branch_menu
139
- destroy $w
140
- if {$create_branch_checkout} {
141
- switch_branch $newbranch
142
- }
143
-}
144
-
64
proc radio_selector {varname value args} {
65
upvar #0 $varname var
66
set var $value
67
}
68
150
-trace add variable create_branch_head write \
151
- [list radio_selector create_branch_revtype head]
152
-trace add variable create_branch_trackinghead write \
153
- [list radio_selector create_branch_revtype tracking]
154
-trace add variable create_branch_tag write \
155
- [list radio_selector create_branch_revtype tag]
156
-
157
-trace add variable delete_branch_head write \
158
- [list radio_selector delete_branch_checktype head]
159
-trace add variable delete_branch_trackinghead write \
160
- [list radio_selector delete_branch_checktype tracking]
161
-
162
-proc do_create_branch {} {
163
- global all_heads current_branch repo_config
164
- global create_branch_checkout create_branch_revtype
165
- global create_branch_head create_branch_trackinghead
166
- global create_branch_name create_branch_revexp
167
- global create_branch_tag
168
-
169
- set w .branch_editor
170
- toplevel $w
171
- wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
172
-
173
- label $w.header -text {Create New Branch} \
174
- -font font_uibold
175
- pack $w.header -side top -fill x
176
-
177
- frame $w.buttons
178
- button $w.buttons.create -text Create \
179
- -default active \
180
- -command [list do_create_branch_action $w]
181
- pack $w.buttons.create -side right
182
- button $w.buttons.cancel -text {Cancel} \
183
- -command [list destroy $w]
184
- pack $w.buttons.cancel -side right -padx 5
185
- pack $w.buttons -side bottom -fill x -pady 10 -padx 10
186
-
187
- labelframe $w.desc -text {Branch Description}
188
- label $w.desc.name_l -text {Name:}
189
- entry $w.desc.name_t \
190
- -borderwidth 1 \
191
- -relief sunken \
192
- -width 40 \
193
- -textvariable create_branch_name \
194
- -validate key \
195
- -validatecommand {
196
- if {%d == 1 && [regexp {[~^:?*\[\0- ]} %S]} {return 0}
197
- return 1
198
- }
199
- grid $w.desc.name_l $w.desc.name_t -sticky we -padx {0 5}
200
- grid columnconfigure $w.desc 1 -weight 1
201
- pack $w.desc -anchor nw -fill x -pady 5 -padx 5
202
-
203
- labelframe $w.from -text {Starting Revision}
204
- if {$all_heads ne {}} {
205
- radiobutton $w.from.head_r \
206
- -text {Local Branch:} \
207
- -value head \
208
- -variable create_branch_revtype
209
- eval tk_optionMenu $w.from.head_m create_branch_head $all_heads
210
- grid $w.from.head_r $w.from.head_m -sticky w
211
- }
212
- set all_trackings [all_tracking_branches]
213
- if {$all_trackings ne {}} {
214
- set create_branch_trackinghead [lindex $all_trackings 0]
215
- radiobutton $w.from.tracking_r \
216
- -text {Tracking Branch:} \
217
- -value tracking \
218
- -variable create_branch_revtype
219
- eval tk_optionMenu $w.from.tracking_m \
220
- create_branch_trackinghead \
221
- $all_trackings
222
- grid $w.from.tracking_r $w.from.tracking_m -sticky w
223
- }
224
- set all_tags [load_all_tags]
225
- if {$all_tags ne {}} {
226
- set create_branch_tag [lindex $all_tags 0]
227
- radiobutton $w.from.tag_r \
228
- -text {Tag:} \
229
- -value tag \
230
- -variable create_branch_revtype
231
- eval tk_optionMenu $w.from.tag_m create_branch_tag $all_tags
232
- grid $w.from.tag_r $w.from.tag_m -sticky w
233
- }
234
- radiobutton $w.from.exp_r \
235
- -text {Revision Expression:} \
236
- -value expression \
237
- -variable create_branch_revtype
238
- entry $w.from.exp_t \
239
- -borderwidth 1 \
240
- -relief sunken \
241
- -width 50 \
242
- -textvariable create_branch_revexp \
243
- -validate key \
244
- -validatecommand {
245
- if {%d == 1 && [regexp {\s} %S]} {return 0}
246
- if {%d == 1 && [string length %S] > 0} {
247
- set create_branch_revtype expression
248
- }
249
- return 1
250
- }
251
- grid $w.from.exp_r $w.from.exp_t -sticky we -padx {0 5}
252
- grid columnconfigure $w.from 1 -weight 1
253
- pack $w.from -anchor nw -fill x -pady 5 -padx 5
254
-
255
- labelframe $w.postActions -text {Post Creation Actions}
256
- checkbutton $w.postActions.checkout \
257
- -text {Checkout after creation} \
258
- -variable create_branch_checkout
259
- pack $w.postActions.checkout -anchor nw
260
- pack $w.postActions -anchor nw -fill x -pady 5 -padx 5
261
-
262
- set create_branch_checkout 1
263
- set create_branch_head $current_branch
264
- set create_branch_revtype head
265
- set create_branch_name $repo_config(gui.newbranchtemplate)
266
- set create_branch_revexp {}
267
-
268
- bind $w <Visibility> "
269
- grab $w
270
- $w.desc.name_t icursor end
271
- focus $w.desc.name_t
272
- "
273
- bind $w <Key-Escape> "destroy $w"
274
- bind $w <Key-Return> "do_create_branch_action $w;break"
275
- wm title $w "[appname] ([reponame]): Create Branch"
276
- tkwait window $w
277
-}
278
-
69
proc do_delete_branch_action {w} {
70
global all_heads
71
global delete_branch_checktype delete_branch_head delete_branch_trackinghead
lib/branch_create.tcl
new
+157
@@ -0,0 +1,157 @@
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
11
+field opt_checkout 1; # automatically checkout the new branch?
12
+
13
+constructor dialog {} {
14
+ global repo_config
15
+
16
+ make_toplevel top w
17
+ wm title $top "[appname] ([reponame]): Create Branch"
18
+ if {$top ne {.}} {
19
+ wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
20
+ }
21
+
22
+ label $w.header -text {Create New Branch} -font font_uibold
23
+ pack $w.header -side top -fill x
24
+
25
+ frame $w.buttons
26
+ button $w.buttons.create -text Create \
27
+ -default active \
28
+ -command [cb _create]
29
+ pack $w.buttons.create -side right
30
+ button $w.buttons.cancel -text {Cancel} \
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
+
35
+ labelframe $w.desc -text {Branch Description}
36
+ label $w.desc.name_r \
37
+ -anchor w \
38
+ -text {Name:}
39
+ set w_name $w.desc.name_t
40
+ entry $w_name \
41
+ -borderwidth 1 \
42
+ -relief sunken \
43
+ -width 40 \
44
+ -textvariable @name \
45
+ -validate key \
46
+ -validatecommand [cb _validate %d %S]
47
+ grid $w.desc.name_r $w_name -sticky we -padx {0 5}
48
+
49
+ grid columnconfigure $w.desc 1 -weight 1
50
+ pack $w.desc -anchor nw -fill x -pady 5 -padx 5
51
+
52
+ set w_rev [::choose_rev::new $w.rev {Starting Revision}]
53
+ pack $w.rev -anchor nw -fill x -pady 5 -padx 5
54
+
55
+ labelframe $w.postActions -text {Post Creation Actions}
56
+ checkbutton $w.postActions.checkout \
57
+ -text {Checkout after creation} \
58
+ -variable @opt_checkout
59
+ pack $w.postActions.checkout -anchor nw
60
+ pack $w.postActions -anchor nw -fill x -pady 5 -padx 5
61
+
62
+ set name $repo_config(gui.newbranchtemplate)
63
+
64
+ bind $w <Visibility> "
65
+ grab $w
66
+ $w_name icursor end
67
+ focus $w_name
68
+ "
69
+ bind $w <Key-Escape> [list destroy $w]
70
+ bind $w <Key-Return> [cb _create]\;break
71
+ tkwait window $w
72
+}
73
+
74
+method _create {} {
75
+ global null_sha1 repo_config
76
+ global all_heads
77
+
78
+ set newbranch $name
79
+ if {$newbranch eq {}
80
+ || $newbranch eq $repo_config(gui.newbranchtemplate)} {
81
+ tk_messageBox \
82
+ -icon error \
83
+ -type ok \
84
+ -title [wm title $w] \
85
+ -parent $w \
86
+ -message "Please supply a branch name."
87
+ focus $w_name
88
+ return
89
+ }
90
+ if {![catch {git show-ref --verify -- "refs/heads/$newbranch"}]} {
91
+ tk_messageBox \
92
+ -icon error \
93
+ -type ok \
94
+ -title [wm title $w] \
95
+ -parent $w \
96
+ -message "Branch '$newbranch' already exists."
97
+ focus $w_name
98
+ return
99
+ }
100
+ if {[catch {git check-ref-format "heads/$newbranch"}]} {
101
+ tk_messageBox \
102
+ -icon error \
103
+ -type ok \
104
+ -title [wm title $w] \
105
+ -parent $w \
106
+ -message "We do not like '$newbranch' as a branch name."
107
+ focus $w_name
108
+ return
109
+ }
110
+
111
+ if {[catch {set cmt [$w_rev get_commit]}]} {
112
+ tk_messageBox \
113
+ -icon error \
114
+ -type ok \
115
+ -title [wm title $w] \
116
+ -parent $w \
117
+ -message "Invalid starting revision: [$w_rev get]"
118
+ return
119
+ }
120
+ if {[catch {
121
+ git update-ref \
122
+ -m "branch: Created from [$w_rev get]" \
123
+ "refs/heads/$newbranch" \
124
+ $cmt \
125
+ $null_sha1
126
+ } err]} {
127
+ tk_messageBox \
128
+ -icon error \
129
+ -type ok \
130
+ -title [wm title $w] \
131
+ -parent $w \
132
+ -message "Failed to create '$newbranch'.\n\n$err"
133
+ return
134
+ }
135
+
136
+ lappend all_heads $newbranch
137
+ set all_heads [lsort $all_heads]
138
+ populate_branch_menu
139
+ destroy $w
140
+ if {$opt_checkout} {
141
+ switch_branch $newbranch
142
+ }
143
+}
144
+
145
+method _validate {d S} {
146
+ if {$d == 1} {
147
+ if {[regexp {[~^:?*\[\0- ]} $S]} {
148
+ return 0
149
+ }
150
+ if {[string length $S] > 0} {
151
+ set name_type user
152
+ }
153
+ }
154
+ return 1
155
+}
156
+
157
+}
lib/choose_rev.tcl
new
+127
@@ -0,0 +1,127 @@
1
+# git-gui revision chooser
2
+# Copyright (C) 2006, 2007 Shawn Pearce
3
+
4
+class choose_rev {
5
+
6
+field w ; # our megawidget path
7
+field revtype {}; # type of revision chosen
8
+
9
+field c_head {}; # selected local branch head
10
+field c_trck {}; # selected tracking branch
11
+field c_tag {}; # selected tag
12
+field c_expr {}; # current revision expression
13
+
14
+constructor new {path {title {}}} {
15
+ global all_heads current_branch
16
+
17
+ set w $path
18
+
19
+ if {$title ne {}} {
20
+ labelframe $w -text $title
21
+ } else {
22
+ frame $w
23
+ }
24
+ bind $w <Destroy> [cb _delete %W]
25
+
26
+ if {$all_heads ne {}} {
27
+ set c_head $current_branch
28
+ radiobutton $w.head_r \
29
+ -text {Local Branch:} \
30
+ -value head \
31
+ -variable @revtype
32
+ eval tk_optionMenu $w.head_m @c_head $all_heads
33
+ grid $w.head_r $w.head_m -sticky w
34
+ if {$revtype eq {}} {
35
+ set revtype head
36
+ }
37
+ trace add variable @c_head write [cb _select head]
38
+ }
39
+
40
+ set all_trackings [all_tracking_branches]
41
+ if {$all_trackings ne {}} {
42
+ set c_trck [lindex $all_trackings 0]
43
+ radiobutton $w.trck_r \
44
+ -text {Tracking Branch:} \
45
+ -value trck \
46
+ -variable @revtype
47
+ eval tk_optionMenu $w.trck_m @c_trck $all_trackings
48
+ grid $w.trck_r $w.trck_m -sticky w
49
+ if {$revtype eq {}} {
50
+ set revtype trck
51
+ }
52
+ trace add variable @c_trck write [cb _select trck]
53
+ }
54
+
55
+ set all_tags [load_all_tags]
56
+ if {$all_tags ne {}} {
57
+ set c_tag [lindex $all_tags 0]
58
+ radiobutton $w.tag_r \
59
+ -text {Tag:} \
60
+ -value tag \
61
+ -variable @revtype
62
+ eval tk_optionMenu $w.tag_m @c_tag $all_tags
63
+ grid $w.tag_r $w.tag_m -sticky w
64
+ if {$revtype eq {}} {
65
+ set revtype tag
66
+ }
67
+ trace add variable @c_tag write [cb _select tag]
68
+ }
69
+
70
+ radiobutton $w.expr_r \
71
+ -text {Revision Expression:} \
72
+ -value expr \
73
+ -variable @revtype
74
+ entry $w.expr_t \
75
+ -borderwidth 1 \
76
+ -relief sunken \
77
+ -width 50 \
78
+ -textvariable @c_expr \
79
+ -validate key \
80
+ -validatecommand [cb _validate %d %S]
81
+ grid $w.expr_r $w.expr_t -sticky we -padx {0 5}
82
+ if {$revtype eq {}} {
83
+ set revtype expr
84
+ }
85
+
86
+ grid columnconfigure $w 1 -weight 1
87
+ return $this
88
+}
89
+
90
+method get {} {
91
+ switch -- $revtype {
92
+ head { return $c_head }
93
+ trck { return $c_trck }
94
+ tag { return $c_tag }
95
+ expr { return $c_expr }
96
+ default { error "unknown type of revision" }
97
+ }
98
+}
99
+
100
+method get_commit {} {
101
+ set rev [get $this]
102
+ return [git rev-parse --verify "${rev}^0"]
103
+}
104
+
105
+method _validate {d S} {
106
+ if {$d == 1} {
107
+ if {[regexp {\s} $S]} {
108
+ return 0
109
+ }
110
+ if {[string length $S] > 0} {
111
+ set revtype expr
112
+ }
113
+ }
114
+ return 1
115
+}
116
+
117
+method _select {value args} {
118
+ set revtype $value
119
+}
120
+
121
+method _delete {current} {
122
+ if {$current eq $w} {
123
+ delete_this
124
+ }
125
+}
126
+
127
+}