1 # git-gui revision chooser
2 # Copyright (C) 2006, 2007 Shawn Pearce
6 image create photo ::choose_rev::img_find -data {R0lGODlhEAAQAIYAAPwCBCQmJDw+PBQSFAQCBMza3NTm5MTW1HyChOT29Ozq7MTq7Kze5Kzm7Oz6/NTy9Iza5GzGzKzS1Nzy9Nz29Kzq9HTGzHTK1Lza3AwKDLzu9JTi7HTW5GTCzITO1Mzq7Hza5FTK1ESyvHzKzKzW3DQyNDyqtDw6PIzW5HzGzAT+/Dw+RKyurNTOzMTGxMS+tJSGdATCxHRydLSqpLymnLSijBweHERCRNze3Pz69PTy9Oze1OTSxOTGrMSqlLy+vPTu5OzSvMymjNTGvNS+tMy2pMyunMSefAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAQABAAAAe4gACCAAECA4OIiAIEBQYHBAKJgwIICQoLDA0IkZIECQ4PCxARCwSSAxITFA8VEBYXGBmJAQYLGhUbHB0eH7KIGRIMEBAgISIjJKaIJQQLFxERIialkieUGigpKRoIBCqJKyyLBwvJAioEyoICLS4v6QQwMQQyLuqLli8zNDU2BCf1lN3AkUPHDh49fAQAAEnGD1MCCALZEaSHkIUMBQS8wWMIkSJGhBzBmFEGgRsBUqpMiSgdAD+BAAAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
8 field w ; # our megawidget path
9 field w_list ; # list of currently filtered specs
10 field w_filter ; # filter entry for $w_list
12 field c_expr {}; # current revision expression
13 field filter ; # current filter string
14 field revtype head; # type of revision chosen
15 field cur_specs [list]; # list of specs for $revtype
16 field spec_head ; # list of all head specs
17 field spec_trck ; # list of all tracking branch specs
18 field spec_tag ; # list of all tag specs
19 field tip_data ; # array of tip commit info by refname
20 field log_last ; # array of reflog date by refname
22 field tooltip_wm {} ; # Current tooltip toplevel, if open
23 field tooltip_t {} ; # Text widget in $tooltip_wm
24 field tooltip_timer {} ; # Current timer event for our tooltip
26 proc new {path {title {}}} {
27 return [_new $path 0 $title]
30 proc new_unmerged {path {title {}}} {
31 return [_new $path 1 $title]
34 constructor _new {path unmerged_only title} {
35 global current_branch is_detached
40 labelframe $w -text $title
44 bind $w <Destroy> [cb _delete %W]
47 radiobutton $w.detachedhead_r \
49 -text {This Detached Checkout} \
52 grid $w.detachedhead_r -sticky we -padx {0 5} -columnspan 2
55 radiobutton $w.expr_r \
56 -text {Revision Expression:} \
63 -textvariable @c_expr \
65 -validatecommand [cb _validate %d %S]
66 grid $w.expr_r $w.expr_t -sticky we -padx {0 5}
69 radiobutton $w.types.head_r \
70 -text {Local Branch} \
73 pack $w.types.head_r -side left
74 radiobutton $w.types.trck_r \
75 -text {Tracking Branch} \
78 pack $w.types.trck_r -side left
79 radiobutton $w.types.tag_r \
83 pack $w.types.tag_r -side left
84 set w_filter $w.types.filter
89 -textvariable @filter \
91 -validatecommand [cb _filter %P]
92 pack $w_filter -side right
93 pack [label $w.types.filter_icon \
94 -image ::choose_rev::img_find \
96 grid $w.types -sticky we -padx {0 5} -columnspan 2
105 -exportselection false \
106 -xscrollcommand [cb _sb_set $w.list.sbx h] \
107 -yscrollcommand [cb _sb_set $w.list.sby v]
108 pack $w_list -fill both -expand 1
109 grid $w.list -sticky nswe -padx {20 5} -columnspan 2
110 bind $w_list <Any-Motion> [cb _show_tooltip @%x,%y]
111 bind $w_list <Any-Enter> [cb _hide_tooltip]
112 bind $w_list <Any-Leave> [cb _hide_tooltip]
113 bind $w_list <Destroy> [cb _hide_tooltip]
115 grid columnconfigure $w 1 -weight 1
117 grid rowconfigure $w 3 -weight 1
119 grid rowconfigure $w 2 -weight 1
122 trace add variable @revtype write [cb _select]
123 bind $w_filter <Key-Return> [list focus $w_list]\;break
124 bind $w_filter <Key-Down> [list focus $w_list]
127 append fmt { %(refname)}
129 append fmt { %(objecttype)}
130 append fmt { %(objectname)}
131 append fmt { [concat %(taggername) %(authorname)]}
132 append fmt { [concat %(taggerdate) %(authordate)]}
133 append fmt { %(subject)}
135 append fmt { %(*objecttype)}
136 append fmt { %(*objectname)}
137 append fmt { %(*authorname)}
138 append fmt { %(*authordate)}
139 append fmt { %(*subject)}
142 set fr_fd [git_read for-each-ref \
150 fconfigure $fr_fd -translation lf -encoding utf-8
151 while {[gets $fr_fd line] > 0} {
152 set line [eval $line]
153 if {[lindex $line 1 0] eq {tag}} {
154 if {[lindex $line 2 0] eq {commit}} {
155 set sha1 [lindex $line 2 1]
159 } elseif {[lindex $line 1 0] eq {commit}} {
160 set sha1 [lindex $line 1 1]
164 set refn [lindex $line 0]
165 set tip_data($refn) [lrange $line 1 end]
166 lappend cmt_refn($sha1) $refn
167 lappend all_refn $refn
171 if {$unmerged_only} {
172 set fr_fd [git_read rev-list --all ^$::HEAD]
173 while {[gets $fr_fd sha1] > 0} {
174 if {[catch {set rlst $cmt_refn($sha1)}]} continue
181 foreach refn $all_refn {
187 foreach name [load_all_heads] {
188 set refn refs/heads/$name
189 if {[info exists inc($refn)]} {
190 lappend spec_head [list $name $refn]
195 foreach spec [all_tracking_branches] {
196 set refn [lindex $spec 0]
197 if {[info exists inc($refn)]} {
198 regsub ^refs/(heads|remotes)/ $refn {} name
199 lappend spec_trck [concat $name $spec]
204 foreach name [load_all_tags] {
205 set refn refs/tags/$name
206 if {[info exists inc($refn)]} {
207 lappend spec_tag [list $name $refn]
211 if {$is_detached} { set revtype HEAD
212 } elseif {[llength $spec_head] > 0} { set revtype head
213 } elseif {[llength $spec_trck] > 0} { set revtype trck
214 } elseif {[llength $spec_tag ] > 0} { set revtype tag
215 } else { set revtype expr
218 if {$revtype eq {head} && $current_branch ne {}} {
220 foreach spec $spec_head {
221 if {[lindex $spec 0] eq $current_branch} {
222 $w_list selection clear 0 end
223 $w_list selection set $i
234 if {![winfo exists $w.none_r]} {
235 radiobutton $w.none_r \
239 grid $w.none_r -sticky we -padx {0 5} -columnspan 2
241 $w.none_r configure -text $text
249 set i [$w_list curselection]
251 return [lindex $cur_specs $i 0]
258 expr { return $c_expr }
260 default { error "unknown type of revision" }
264 method pick_tracking_branch {} {
268 method focus_filter {} {
269 if {[$w_filter cget -state] eq {normal}} {
274 method bind_listbox {event script} {
275 bind $w_list $event $script
278 method get_local_branch {} {
279 if {$revtype eq {head}} {
286 method get_tracking_branch {} {
287 set i [$w_list curselection]
288 if {$i eq {} || $revtype ne {trck}} {
291 return [lrange [lindex $cur_specs $i] 1 end]
294 method get_commit {} {
299 return [git rev-parse --verify "$e^0"]
302 method commit_or_die {} {
303 if {[catch {set new [get_commit $this]} err]} {
305 # Cleanup the not-so-friendly error from rev-parse.
307 regsub {^fatal:\s*} $err {} err
308 if {$err eq {Needed a single revision}} {
312 set top [winfo toplevel $w]
313 set msg "Invalid revision: [get $this]\n\n$err"
317 -title [wm title $top] \
330 set i [$w_list curselection]
332 return [lindex $cur_specs $i 1]
334 error "No revision selected."
342 error "Revision expression is empty."
347 default { error "unknown type of revision" }
351 method _validate {d S} {
353 if {[regexp {\s} $S]} {
356 if {[string length $S] > 0} {
364 if {[regexp {\s} $P]} {
371 method _select {args} {
372 _rebuild $this $filter
376 method _rebuild {pat} {
379 head { set new $spec_head }
380 trck { set new $spec_trck }
381 tag { set new $spec_tag }
390 if {[$w_list cget -state] eq {disabled}} {
391 $w_list configure -state normal
400 set txt [lindex $spec 0]
401 if {$pat eq {} || [string match $pat $txt]} {
402 lappend cur_specs $spec
403 $w_list insert end $txt
406 if {$cur_specs ne {}} {
407 $w_list selection clear 0 end
408 $w_list selection set 0
411 if {[$w_filter cget -state] ne $ste} {
412 $w_list configure -state $ste
413 $w_filter configure -state $ste
417 method _delete {current} {
418 if {$current eq $w} {
423 method _sb_set {sb orient first last} {
424 set old_focus [focus -lastfor $w]
426 if {$first == 0 && $last == 1} {
427 if {[winfo exists $sb]} {
429 if {$old_focus ne {}} {
437 if {![winfo exists $sb]} {
438 if {$orient eq {h}} {
439 scrollbar $sb -orient h -command [list $w_list xview]
440 pack $sb -fill x -side bottom -before $w_list
442 scrollbar $sb -orient v -command [list $w_list yview]
443 pack $sb -fill y -side right -before $w_list
445 if {$old_focus ne {}} {
453 method _show_tooltip {pos} {
454 if {$tooltip_wm ne {}} {
456 } elseif {$tooltip_timer eq {}} {
457 set tooltip_timer [after 1000 [cb _open_tooltip]]
461 method _open_tooltip {} {
465 set pos_x [winfo pointerx $w_list]
466 set pos_y [winfo pointery $w_list]
467 if {[winfo containing $pos_x $pos_y] ne $w_list} {
472 set pos @[join [list \
473 [expr {$pos_x - [winfo rootx $w_list]}] \
474 [expr {$pos_y - [winfo rooty $w_list]}]] ,]
475 set lno [$w_list index $pos]
481 set spec [lindex $cur_specs $lno]
482 set refn [lindex $spec 1]
488 if {$tooltip_wm eq {}} {
489 set tooltip_wm [toplevel $w_list.tooltip -borderwidth 1]
490 wm overrideredirect $tooltip_wm 1
491 wm transient $tooltip_wm [winfo toplevel $w_list]
492 set tooltip_t $tooltip_wm.label
495 -highlightthickness 0 \
499 -background lightyellow \
501 $tooltip_t tag conf section_header -font font_uibold
502 bind $tooltip_wm <Escape> [cb _hide_tooltip]
505 $tooltip_t conf -state normal
506 $tooltip_t delete 0.0 end
509 set data $tip_data($refn)
510 if {[lindex $data 0 0] eq {tag}} {
511 set tag [lindex $data 0]
512 if {[lindex $data 1 0] eq {commit}} {
513 set cmit [lindex $data 1]
517 } elseif {[lindex $data 0 0] eq {commit}} {
519 set cmit [lindex $data 0]
522 $tooltip_t insert end [lindex $spec 0]
523 set last [_reflog_last $this [lindex $spec 1]]
525 $tooltip_t insert end "\n"
526 $tooltip_t insert end "updated"
527 $tooltip_t insert end " $last"
529 $tooltip_t insert end "\n"
532 $tooltip_t insert end "\n"
533 $tooltip_t insert end "tag" section_header
534 $tooltip_t insert end " [lindex $tag 1]\n"
535 $tooltip_t insert end [lindex $tag 2]
536 $tooltip_t insert end " ([lindex $tag 3])\n"
537 $tooltip_t insert end [lindex $tag 4]
538 $tooltip_t insert end "\n"
542 $tooltip_t insert end "\n"
543 $tooltip_t insert end "commit" section_header
544 $tooltip_t insert end " [lindex $cmit 1]\n"
545 $tooltip_t insert end [lindex $cmit 2]
546 $tooltip_t insert end " ([lindex $cmit 3])\n"
547 $tooltip_t insert end [lindex $cmit 4]
550 if {[llength $spec] > 2} {
551 $tooltip_t insert end "\n"
552 $tooltip_t insert end "remote" section_header
553 $tooltip_t insert end " [lindex $spec 2]\n"
554 $tooltip_t insert end "url"
555 $tooltip_t insert end " $remote_url([lindex $spec 2])\n"
556 $tooltip_t insert end "branch"
557 $tooltip_t insert end " [lindex $spec 3]"
560 $tooltip_t conf -state disabled
561 _position_tooltip $this
564 method _reflog_last {name} {
565 if {[info exists reflog_last($name)]} {
566 return reflog_last($name)
570 if {[catch {set last [file mtime [gitdir $name]]}]
571 && ![catch {set g [open [gitdir logs $name] r]}]} {
572 fconfigure $g -translation binary
573 while {[gets $g line] >= 0} {
574 if {[regexp {> ([1-9][0-9]*) } $line line when]} {
582 set last [clock format $last -format {%a %b %e %H:%M:%S %Y}]
584 set reflog_last($name) $last
588 method _position_tooltip {} {
589 set max_h [lindex [split [$tooltip_t index end] .] 0]
591 for {set i 1} {$i <= $max_h} {incr i} {
592 set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
593 if {$c > $max_w} {set max_w $c}
595 $tooltip_t conf -width $max_w -height $max_h
597 set req_w [winfo reqwidth $tooltip_t]
598 set req_h [winfo reqheight $tooltip_t]
599 set pos_x [expr {[winfo pointerx .] + 5}]
600 set pos_y [expr {[winfo pointery .] + 10}]
602 set g "${req_w}x${req_h}"
603 if {$pos_x >= 0} {append g +}
605 if {$pos_y >= 0} {append g +}
608 wm geometry $tooltip_wm $g
612 method _hide_tooltip {} {
613 if {$tooltip_wm ne {}} {
617 if {$tooltip_timer ne {}} {
618 after cancel $tooltip_timer