git-gui: Convert merge dialog to use class system
I've found that the class code makes it a whole lot easier to create more complex GUI code, especially the dialogs. So before I make any major improvements to the merge dialog's interface I'm going to first switch it to use the class system, so the code is slightly cleaner. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jul 18, 2007 at 02:56 UTC
ff749c114a9642e79a1e93250828034f0338f1be
1 file changed
+65
-41
lib/merge.tcl
+65
-41
@@ -1,9 +1,13 @@
1
# git-gui branch merge support
2
# Copyright (C) 2006, 2007 Shawn Pearce
3
4
-namespace eval merge {
4
+class merge {
5
+
6
+field w ; # top level window
7
+field w_list ; # widget of available branches
8
+field list ; # list of available branches
9
6
-proc _can_merge {} {
10
+method _can_merge {} {
11
global HEAD commit_type file_states
12
13
if {[string match amend* $commit_type]} {
@@ -63,26 +67,26 @@ You should complete the current commit before starting a merge. Doing so will h
67
return 1
68
}
69
66
-proc _refs {w list} {
70
+method _refs {} {
71
set r {}
68
- foreach i [$w.source.l curselection] {
72
+ foreach i [$w_list curselection] {
73
lappend r [lindex [lindex $list $i] 0]
74
}
75
return $r
76
}
77
74
-proc _visualize {w list} {
75
- set revs [_refs $w $list]
78
+method _visualize {} {
79
+ set revs [_refs $this]
80
if {$revs eq {}} return
81
lappend revs --not HEAD
82
do_gitk $revs
83
}
84
81
-proc _start {w list} {
85
+method _start {} {
86
global HEAD current_branch
87
88
set cmd [list git merge]
85
- set names [_refs $w $list]
89
+ set names [_refs $this]
90
set revcnt [llength $names]
91
append cmd { } $names
92
@@ -123,15 +127,14 @@ Please select fewer branches. To merge more than 15 branches, merge the branche
127
set msg "Merging $current_branch, [join $names {, }]"
128
ui_status "$msg..."
129
set cons [console::new "Merge" $msg]
126
- console::exec $cons $cmd \
127
- [namespace code [list _finish $revcnt $cons]]
130
+ console::exec $cons $cmd [cb _finish $revcnt $cons]
131
132
wm protocol $w WM_DELETE_WINDOW {}
133
destroy $w
134
}
135
133
-proc _finish {revcnt w ok} {
134
- console::done $w $ok
136
+method _finish {revcnt cons ok} {
137
+ console::done $cons $ok
138
if {$ok} {
139
set msg {Merge completed successfully.}
140
} else {
@@ -148,8 +151,7 @@ You can attempt this merge again by merging only one branch at a time." $w
151
152
set fd [git_read read-tree --reset -u HEAD]
153
fconfigure $fd -blocking 0 -translation binary
151
- fileevent $fd readable \
152
- [namespace code [list _reset_wait $fd]]
154
+ fileevent $fd readable [cb _reset_wait $fd]
155
ui_status {Aborting... please wait...}
156
return
157
}
@@ -158,13 +160,17 @@ You can attempt this merge again by merging only one branch at a time." $w
160
}
161
unlock_index
162
rescan [list ui_status $msg]
163
+ delete_this
164
}
165
163
-proc dialog {} {
166
+constructor dialog {} {
167
global current_branch
168
global M1B
169
167
- if {![_can_merge]} return
170
+ if {![_can_merge $this]} {
171
+ delete_this
172
+ return
173
+ }
174
175
set fmt {list %(objectname) %(*objectname) %(refname) %(subject)}
176
set fr_fd [git_read for-each-ref \
@@ -187,23 +193,25 @@ proc dialog {} {
193
}
194
close $fr_fd
195
190
- set to_show {}
196
+ set list [list]
197
set fr_fd [git_read rev-list --all --not HEAD]
198
while {[gets $fr_fd line] > 0} {
199
if {[catch {set ref $sha1($line)}]} continue
200
foreach n $ref {
195
- lappend to_show [list $n $line]
201
+ lappend list [list $n $line]
202
}
203
}
204
close $fr_fd
199
- set to_show [lsort -unique $to_show]
205
+ set list [lsort -unique $list]
206
201
- set w .merge_setup
202
- toplevel $w
203
- wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
207
+ make_toplevel top w
208
+ wm title $top "[appname] ([reponame]): Merge"
209
+ if {$top ne {.}} {
210
+ wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
211
+ }
212
205
- set _visualize [namespace code [list _visualize $w $to_show]]
206
- set _start [namespace code [list _start $w $to_show]]
213
+ set _visualize [cb _visualize]
214
+ set _start [cb _start]
215
216
label $w.header \
217
-text "Merge Into $current_branch" \
@@ -217,49 +225,65 @@ proc dialog {} {
225
pack $w.buttons.create -side right
226
button $w.buttons.cancel \
227
-text {Cancel} \
220
- -command "unlock_index;destroy $w"
228
+ -command [cb _cancel]
229
pack $w.buttons.cancel -side right -padx 5
230
pack $w.buttons -side bottom -fill x -pady 10 -padx 10
231
232
labelframe $w.source -text {Source Branches}
225
- listbox $w.source.l \
233
+ set w_list $w.source.l
234
+ listbox $w_list \
235
-height 10 \
236
-width 70 \
237
-font font_diff \
238
-selectmode extended \
239
-yscrollcommand [list $w.source.sby set]
231
- scrollbar $w.source.sby -command [list $w.source.l yview]
240
+ scrollbar $w.source.sby -command [list $w_list yview]
241
pack $w.source.sby -side right -fill y
233
- pack $w.source.l -side left -fill both -expand 1
242
+ pack $w_list -side left -fill both -expand 1
243
pack $w.source -fill both -expand 1 -pady 5 -padx 5
244
236
- foreach ref $to_show {
245
+ foreach ref $list {
246
set n [lindex $ref 0]
247
if {[string length $n] > 20} {
248
set n "[string range $n 0 16]..."
249
}
241
- $w.source.l insert end [format {%s %-20s %s} \
250
+ $w_list insert end [format {%s %-20s %s} \
251
[string range [lindex $ref 1] 0 5] \
252
$n \
253
$subj([lindex $ref 0])]
254
}
255
247
- bind $w.source.l <Key-K> [list event generate %W <Shift-Key-Up>]
248
- bind $w.source.l <Key-J> [list event generate %W <Shift-Key-Down>]
249
- bind $w.source.l <Key-k> [list event generate %W <Key-Up>]
250
- bind $w.source.l <Key-j> [list event generate %W <Key-Down>]
251
- bind $w.source.l <Key-h> [list event generate %W <Key-Left>]
252
- bind $w.source.l <Key-l> [list event generate %W <Key-Right>]
253
- bind $w.source.l <Key-v> $_visualize
256
+ bind $w_list <Key-K> [list event generate %W <Shift-Key-Up>]
257
+ bind $w_list <Key-J> [list event generate %W <Shift-Key-Down>]
258
+ bind $w_list <Key-k> [list event generate %W <Key-Up>]
259
+ bind $w_list <Key-j> [list event generate %W <Key-Down>]
260
+ bind $w_list <Key-h> [list event generate %W <Key-Left>]
261
+ bind $w_list <Key-l> [list event generate %W <Key-Right>]
262
+ bind $w_list <Key-v> $_visualize
263
264
bind $w <$M1B-Key-Return> $_start
256
- bind $w <Visibility> "grab $w; focus $w.source.l"
257
- bind $w <Key-Escape> "unlock_index;destroy $w"
258
- wm protocol $w WM_DELETE_WINDOW "unlock_index;destroy $w"
259
- wm title $w "[appname] ([reponame]): Merge"
265
+ bind $w <Visibility> [cb _visible]
266
+ bind $w <Key-Escape> [cb _cancel]
267
+ wm protocol $w WM_DELETE_WINDOW [cb _cancel]
268
tkwait window $w
269
}
270
271
+method _visible {} {
272
+ grab $w
273
+ focus $w_list
274
+}
275
+
276
+method _cancel {} {
277
+ wm protocol $w WM_DELETE_WINDOW {}
278
+ unlock_index
279
+ destroy $w
280
+ delete_this
281
+}
282
+
283
+}
284
+
285
+namespace eval merge {
286
+
287
proc reset_hard {} {
288
global HEAD commit_type file_states
289