@matheusbsbs / matheus-bagliano / commits / 03e4ec5364
Fork 0

git-gui: Always indicate the file in the diff viewer.

When we did a rescan to update the file lists we lost the tag which indicated which file was currently in the diff viewer. This occurs because we delete every line from the two file list boxes (thus removing the tag) and then redisplay the diff in the diff viewer but then fail to restore the tag in the file list. Now we restore that tag by searching for the file in the file lists and adding the tag back when the diff viewer displays something. We also no longer obtain the file path directly from the file list text box. Instead we now keep two Tcl lists, one for each file list, holding the file names in sorted order. These lists can be searched with the native [lsearch -sorted] operation (which should be faster than our crude bsearch) or can be quickly accessed by index to return the file path. This should help make things safer should we ever be given a file name which contains an LF within it (as that would span two lines in the file list, not one). Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Nov 11, 2006 at 17:52 UTC 03e4ec5364f955f5e1af0775166c679ed60f7fb4
1 file changed +36 -36
git-gui
+36 -36
@@ -150,7 +150,7 @@ proc repository_state {hdvar ctvar} {
150 proc update_status {{final Ready.}} {
151 global HEAD PARENT commit_type
152 global ui_index ui_other ui_status_value ui_comm
153 - global status_active file_states
153 + global status_active file_states file_lists
154 global cfg_trust_mtime
155
156 if {$status_active || ![lock_index read]} return
@@ -166,6 +166,7 @@ proc update_status {{final Ready.}} {
166 }
167
168 array unset file_states
169 + array unset file_lists
170 foreach w [list $ui_index $ui_other] {
171 $w conf -state normal
172 $w delete 0.0 end
@@ -196,7 +197,7 @@ proc update_status {{final Ready.}} {
197 proc update_status_stage2 {fd final} {
198 global gitdir PARENT commit_type
199 global ui_index ui_other ui_status_value ui_comm
199 - global status_active file_states
200 + global status_active
201 global buf_rdi buf_rdf buf_rlo
202
203 if {$fd != {}} {
@@ -346,22 +347,40 @@ proc status_eof {fd buf final} {
347 ## diff
348
349 proc clear_diff {} {
349 - global ui_diff ui_fname_value ui_fstatus_value
350 + global ui_diff ui_fname_value ui_fstatus_value ui_index ui_other
351
352 $ui_diff conf -state normal
353 $ui_diff delete 0.0 end
354 $ui_diff conf -state disabled
355 +
356 set ui_fname_value {}
357 set ui_fstatus_value {}
358 +
359 + $ui_index tag remove in_diff 0.0 end
360 + $ui_other tag remove in_diff 0.0 end
361 }
362
358 -proc show_diff {path} {
359 - global file_states PARENT diff_3way diff_active
363 +proc show_diff {path {w {}} {lno {}}} {
364 + global file_states file_lists
365 + global PARENT diff_3way diff_active
366 global ui_diff ui_fname_value ui_fstatus_value ui_status_value
367
368 if {$diff_active || ![lock_index read]} return
369
370 clear_diff
371 + if {$w == {} || $lno == {}} {
372 + foreach w [array names file_lists] {
373 + set lno [lsearch -sorted $file_lists($w) $path]
374 + if {$lno >= 0} {
375 + incr lno
376 + break
377 + }
378 + }
379 + }
380 + if {$w != {} && $lno >= 1} {
381 + $w tag add in_diff $lno.0 [expr $lno + 1].0
382 + }
383 +
384 set s $file_states($path)
385 set m [lindex $s 0]
386 set diff_3way 0
@@ -823,27 +842,6 @@ proc mapdesc {state path} {
842 return $r
843 }
844
826 -proc bsearch {w path} {
827 - set hi [expr [lindex [split [$w index end] .] 0] - 2]
828 - if {$hi == 0} {
829 - return -1
830 - }
831 - set lo 0
832 - while {$lo < $hi} {
833 - set mi [expr [expr $lo + $hi] / 2]
834 - set ti [expr $mi + 1]
835 - set cmp [string compare [$w get $ti.1 $ti.end] $path]
836 - if {$cmp < 0} {
837 - set lo $ti
838 - } elseif {$cmp == 0} {
839 - return $mi
840 - } else {
841 - set hi $mi
842 - }
843 - }
844 - return -[expr $lo + 1]
845 -}
846 -
845 set next_icon_id 0
846
847 proc merge_state {path new_state} {
@@ -877,7 +875,8 @@ proc merge_state {path new_state} {
875 }
876
877 proc display_file {path state} {
880 - global ui_index ui_other file_states status_active
878 + global ui_index ui_other
879 + global file_states file_lists status_active
880
881 set old_m [merge_state $path $state]
882 if {$status_active} return
@@ -889,7 +888,7 @@ proc display_file {path state} {
888 set new_icon [mapicon $new_m $path]
889
890 if {$new_w != $old_w} {
892 - set lno [bsearch $old_w $path]
891 + set lno [lsearch -sorted $file_lists($old_w) $path]
892 if {$lno >= 0} {
893 incr lno
894 $old_w conf -state normal
@@ -897,7 +896,10 @@ proc display_file {path state} {
896 $old_w conf -state disabled
897 }
898
900 - set lno [expr abs([bsearch $new_w $path] + 1) + 1]
899 + lappend file_lists($new_w) $path
900 + set file_lists($new_w) [lsort $file_lists($new_w)]
901 + set lno [lsearch -sorted $file_lists($new_w) $path]
902 + incr lno
903 $new_w conf -state normal
904 $new_w image create $lno.0 \
905 -align center -padx 5 -pady 1 \
@@ -913,7 +915,7 @@ proc display_file {path state} {
915 }
916
917 proc display_all_files {} {
916 - global ui_index ui_other file_states
918 + global ui_index ui_other file_states file_lists
919
920 $ui_index conf -state normal
921 $ui_other conf -state normal
@@ -922,6 +924,7 @@ proc display_all_files {} {
924 set s $file_states($path)
925 set m [lindex $s 0]
926 set w [mapcol $m $path]
927 + lappend file_lists($w) $path
928 $w image create end \
929 -align center -padx 5 -pady 1 \
930 -name [lindex $s 1] \
@@ -1484,19 +1487,16 @@ proc do_commit {} {
1487 # shift == 1: left click
1488 # 3: right click
1489 proc click {w x y shift wx wy} {
1487 - global ui_index ui_other
1490 + global ui_index ui_other file_lists
1491
1492 set pos [split [$w index @$x,$y] .]
1493 set lno [lindex $pos 0]
1494 set col [lindex $pos 1]
1492 - set path [$w get $lno.1 $lno.end]
1495 + set path [lindex $file_lists($w) [expr $lno - 1]]
1496 if {$path == {}} return
1497
1498 if {$col > 0 && $shift == 1} {
1496 - $ui_index tag remove in_diff 0.0 end
1497 - $ui_other tag remove in_diff 0.0 end
1498 - $w tag add in_diff $lno.0 [expr $lno + 1].0
1499 - show_diff $path
1499 + show_diff $path $w $lno
1500 }
1501 }
1502