git-gui: Fix diff parsing for lines starting with "--" or "++"
Languages like Lua and SQL use "--" to mark a line as commented out. If this appears at column 0 and is part of the pre-image we may see "--- foo" in the diff, indicating that the line whose content is "-- foo" has been removed from the new version. git-gui was incorrectly parsing "--- foo" as the old file name in the file header, causing it to generate a bad patch file when the user tried to stage or unstage a hunk or the selected line. We need to keep track of where we are in the parsing so that we do not misread a deletion or addition record as part of the header. Reported-by: Alexander Gladysh <agladysh@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Sep 4, 2008 at 21:46 UTC
ca53c3fdcf69f620331f3822cef36d95b5cf5640
1 file changed
+11
-7
lib/diff.tcl
+11
-7
@@ -192,6 +192,7 @@ proc show_diff {path w {lno {}} {scroll_pos {}}} {
192
return
193
}
194
195
+ set ::current_diff_inheader 1
196
fconfigure $fd \
197
-blocking 0 \
198
-encoding binary \
@@ -207,18 +208,21 @@ proc read_diff {fd scroll_pos} {
208
while {[gets $fd line] >= 0} {
209
# -- Cleanup uninteresting diff header lines.
210
#
210
- if { [string match {diff --git *} $line]
211
- || [string match {diff --cc *} $line]
212
- || [string match {diff --combined *} $line]
213
- || [string match {--- *} $line]
214
- || [string match {+++ *} $line]} {
215
- append current_diff_header $line "\n"
216
- continue
211
+ if {$::current_diff_inheader} {
212
+ if { [string match {diff --git *} $line]
213
+ || [string match {diff --cc *} $line]
214
+ || [string match {diff --combined *} $line]
215
+ || [string match {--- *} $line]
216
+ || [string match {+++ *} $line]} {
217
+ append current_diff_header $line "\n"
218
+ continue
219
+ }
220
}
221
if {[string match {index *} $line]} continue
222
if {$line eq {deleted file mode 120000}} {
223
set line "deleted symlink"
224
}
225
+ set ::current_diff_inheader 0
226
227
# -- Automatically detect if this is a 3 way diff.
228
#