1# git-gui revision chooser
2# Copyright (C) 2006, 2007 Shawn Pearce
34
class choose_rev {
56
image create photo ::choose_rev::img_find -data {R0lGODlhEAAQAIYAAPwCBCQmJDw+PBQSFAQCBMza3NTm5MTW1HyChOT29Ozq7MTq7Kze5Kzm7Oz6/NTy9Iza5GzGzKzS1Nzy9Nz29Kzq9HTGzHTK1Lza3AwKDLzu9JTi7HTW5GTCzITO1Mzq7Hza5FTK1ESyvHzKzKzW3DQyNDyqtDw6PIzW5HzGzAT+/Dw+RKyurNTOzMTGxMS+tJSGdATCxHRydLSqpLymnLSijBweHERCRNze3Pz69PTy9Oze1OTSxOTGrMSqlLy+vPTu5OzSvMymjNTGvNS+tMy2pMyunMSefAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAQABAAAAe4gACCAAECA4OIiAIEBQYHBAKJgwIICQoLDA0IkZIECQ4PCxARCwSSAxITFA8VEBYXGBmJAQYLGhUbHB0eH7KIGRIMEBAgISIjJKaIJQQLFxERIialkieUGigpKRoIBCqJKyyLBwvJAioEyoICLS4v6QQwMQQyLuqLli8zNDU2BCf1lN3AkUPHDh49fAQAAEnGD1MCCALZEaSHkIUMBQS8wWMIkSJGhBzBmFEGgRsBUqpMiSgdAD+BAAAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
78
field w ; # our megawidget path
9field w_list ; # list of currently filtered specs
10field w_filter ; # filter entry for $w_list
1112
field c_expr {}; # current revision expression
13field filter ; # current filter string
14field revtype head; # type of revision chosen
15field cur_specs [list]; # list of specs for $revtype
16field spec_head ; # list of all head specs
17field spec_trck ; # list of all tracking branch specs
18field spec_tag ; # list of all tag specs
1920
constructor new {path {title {}}} {
21global all_heads current_branch
2223
set w $path
2425
if {$title ne {}} {
26labelframe $w -text $title
27} else {
28frame $w
29}
30bind $w <Destroy> [cb _delete %W]
3132
radiobutton $w.expr_r \
33-text {Revision Expression:} \
34-value expr \
35-variable @revtype
36entry $w.expr_t \
37-borderwidth 1 \
38-relief sunken \
39-width 50 \
40-textvariable @c_expr \
41-validate key \
42-validatecommand [cb _validate %d %S]
43grid $w.expr_r $w.expr_t -sticky we -padx {0 5}
4445
frame $w.types
46radiobutton $w.types.head_r \
47-text {Local Branch} \
48-value head \
49-variable @revtype
50pack $w.types.head_r -side left
51radiobutton $w.types.trck_r \
52-text {Tracking Branch} \
53-value trck \
54-variable @revtype
55pack $w.types.trck_r -side left
56radiobutton $w.types.tag_r \
57-text {Tag} \
58-value tag \
59-variable @revtype
60pack $w.types.tag_r -side left
61set w_filter $w.types.filter
62entry $w_filter \
63-borderwidth 1 \
64-relief sunken \
65-width 12 \
66-textvariable @filter \
67-validate key \
68-validatecommand [cb _filter %P]
69pack $w_filter -side right
70pack [label $w.types.filter_icon \
71-image ::choose_rev::img_find \
72] -side right
73grid $w.types -sticky we -padx {0 5} -columnspan 2
7475
frame $w.list
76set w_list $w.list.l
77listbox $w_list \
78-font font_diff \
79-width 50 \
80-height 5 \
81-selectmode browse \
82-exportselection false \
83-xscrollcommand [cb _sb_set $w.list.sbx h] \
84-yscrollcommand [cb _sb_set $w.list.sby v]
85pack $w_list -fill both -expand 1
86grid $w.list -sticky nswe -padx {20 5} -columnspan 2
8788
grid columnconfigure $w 1 -weight 1
89grid rowconfigure $w 2 -weight 1
9091
trace add variable @revtype write [cb _select]
92bind $w_filter <Key-Return> [list focus $w_list]\;break
93bind $w_filter <Key-Down> [list focus $w_list]
9495
set spec_head [list]
96foreach name $all_heads {
97lappend spec_head [list $name refs/heads/$name]
98}
99100
set spec_trck [list]
101foreach spec [all_tracking_branches] {
102set name [lindex $spec 0]
103regsub ^refs/(heads|remotes)/ $name {} name
104lappend spec_trck [concat $name $spec]
105}
106107
set spec_tag [list]
108foreach name [load_all_tags] {
109lappend spec_tag [list $name refs/tags/$name]
110}
111112
if {[llength $spec_head] > 0} { set revtype head
113} elseif {[llength $spec_trck] > 0} { set revtype trck
114} elseif {[llength $spec_tag ] > 0} { set revtype tag
115} else { set revtype expr
116}
117118
if {$revtype eq {head} && $current_branch ne {}} {
119set i 0
120foreach spec $spec_head {
121if {[lindex $spec 0] eq $current_branch} {
122$w_list selection set $i
123break
124}
125incr i
126}
127}
128129
return $this
130}
131132
method none {text} {
133if {![winfo exists $w.none_r]} {
134radiobutton $w.none_r \
135-anchor w \
136-value none \
137-variable @revtype
138grid $w.none_r -sticky we -padx {0 5} -columnspan 2
139}
140$w.none_r configure -text $text
141}
142143
method get {} {
144switch -- $revtype {
145head -
146trck -
147tag {
148set i [$w_list curselection]
149if {$i ne {}} {
150return [lindex $cur_specs $i 0]
151} else {
152return {}
153}
154}
155156
expr { return $c_expr }
157none { return {} }
158default { error "unknown type of revision" }
159}
160}
161162
method get_tracking_branch {} {
163set i [$w_list curselection]
164if {$i eq {} || $revtype ne {trck}} {
165return {}
166}
167return [lrange [lindex $cur_specs $i] 1 end]
168}
169170
method get_commit {} {
171set e [_expr $this]
172if {$e eq {}} {
173return {}
174}
175return [git rev-parse --verify "$e^0"]
176}
177178
method commit_or_die {} {
179if {[catch {set new [get_commit $this]} err]} {
180181
# Cleanup the not-so-friendly error from rev-parse.
182#
183regsub {^fatal:\s*} $err {} err
184if {$err eq {Needed a single revision}} {
185set err {}
186}
187188
set top [winfo toplevel $w]
189set msg "Invalid revision: [get $this]\n\n$err"
190tk_messageBox \
191-icon error \
192-type ok \
193-title [wm title $top] \
194-parent $top \
195-message $msg
196error $msg
197}
198return $new
199}
200201
method _expr {} {
202switch -- $revtype {
203head -
204trck -
205tag {
206set i [$w_list curselection]
207if {$i ne {}} {
208return [lindex $cur_specs $i 1]
209} else {
210error "No revision selected."
211}
212}
213214
expr {
215if {$c_expr ne {}} {
216return $c_expr
217} else {
218error "Revision expression is empty."
219}
220}
221none { return {} }
222default { error "unknown type of revision" }
223}
224}
225226
method _validate {d S} {
227if {$d == 1} {
228if {[regexp {\s} $S]} {
229return 0
230}
231if {[string length $S] > 0} {
232set revtype expr
233}
234}
235return 1
236}
237238
method _filter {P} {
239if {[regexp {\s} $P]} {
240return 0
241}
242_rebuild $this $P
243return 1
244}
245246
method _select {args} {
247_rebuild $this $filter
248if {[$w_filter cget -state] eq {normal}} {
249focus $w_filter
250}
251}
252253
method _rebuild {pat} {
254set ste normal
255switch -- $revtype {
256head { set new $spec_head }
257trck { set new $spec_trck }
258tag { set new $spec_tag }
259expr -
260none {
261set new [list]
262set ste disabled
263}
264}
265266
if {[$w_list cget -state] eq {disabled}} {
267$w_list configure -state normal
268}
269$w_list delete 0 end
270271
if {$pat ne {}} {
272set pat *${pat}*
273}
274set cur_specs [list]
275foreach spec $new {
276set txt [lindex $spec 0]
277if {$pat eq {} || [string match $pat $txt]} {
278lappend cur_specs $spec
279$w_list insert end $txt
280}
281}
282283
if {[$w_filter cget -state] ne $ste} {
284$w_list configure -state $ste
285$w_filter configure -state $ste
286}
287}
288289
method _delete {current} {
290if {$current eq $w} {
291delete_this
292}
293}
294295
method _sb_set {sb orient first last} {
296set old_focus [focus -lastfor $w]
297298
if {$first == 0 && $last == 1} {
299if {[winfo exists $sb]} {
300destroy $sb
301if {$old_focus ne {}} {
302update
303focus $old_focus
304}
305}
306return
307}
308309
if {![winfo exists $sb]} {
310if {$orient eq {h}} {
311scrollbar $sb -orient h -command [list $w_list xview]
312pack $sb -fill x -side bottom -before $w_list
313} else {
314scrollbar $sb -orient v -command [list $w_list yview]
315pack $sb -fill y -side right -before $w_list
316}
317if {$old_focus ne {}} {
318update
319focus $old_focus
320}
321}
322$sb set $first $last
323}
324325
}