git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Git Gui does not want to work on chunk level

From
Shawn O. Pearce <spearce@spearce.org>
Date
Sep 5, 2008, 04:53 UTC
Message-ID
<20080905045327.GA31166@spearce.org>
In-Reply-To
<c6c947f60809040754g34e60633lcd5f872ac5e80158@mail.gmail.com>
Alexander Gladysh <agladysh@gmail.com> wrote:
Show 6 quoted lines
> 
> Aha! Thank you! That "--" is a comment in Lua (that is commented line
> that was removed in diff). First dash is from diff, next two came from
> file itself.
> 
> Steps to reproduce the bug:
...

So that was an awesome reproduction case. I have committed the following fix and will push it out in a few minutes:

--8<-- 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>
---
 lib/diff.tcl |   18 +++++++++++-------
 1 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/lib/diff.tcl b/lib/diff.tcl
index 4a7138b..1970b60 100644
--- a/lib/diff.tcl
+++ b/lib/diff.tcl
@@ -192,6 +192,7 @@ proc show_diff {path w {lno {}} {scroll_pos {}}} {
 		return
 	}
 
+	set ::current_diff_inheader 1
 	fconfigure $fd \
 		-blocking 0 \
 		-encoding binary \
@@ -207,18 +208,21 @@ proc read_diff {fd scroll_pos} {
 	while {[gets $fd line] >= 0} {
 		# -- Cleanup uninteresting diff header lines.
 		#
-		if {   [string match {diff --git *}      $line]
-			|| [string match {diff --cc *}       $line]
-			|| [string match {diff --combined *} $line]
-			|| [string match {--- *}             $line]
-			|| [string match {+++ *}             $line]} {
-			append current_diff_header $line "\n"
-			continue
+		if {$::current_diff_inheader} {
+			if {   [string match {diff --git *}      $line]
+			    || [string match {diff --cc *}       $line]
+			    || [string match {diff --combined *} $line]
+			    || [string match {--- *}             $line]
+			    || [string match {+++ *}             $line]} {
+				append current_diff_header $line "\n"
+				continue
+			}
 		}
 		if {[string match {index *} $line]} continue
 		if {$line eq {deleted file mode 120000}} {
 			set line "deleted symlink"
 		}
+		set ::current_diff_inheader 0
 
 		# -- Automatically detect if this is a 3 way diff.
 		#
-- 
1.6.0.1.274.g007e4


-- 
Shawn.
Previous: Alexander GladyshNext: Junio C Hamano
Message 6 of 15 in “Re: Git Gui does not want to work on chunk level”
  1. Alexander GladyshSep 3, 2008
  2. Shawn O. PearceSep 3, 2008
  3. Alexander GladyshSep 4, 2008
  4. Shawn O. PearceSep 4, 2008
  5. Alexander GladyshSep 4, 2008
  6. Shawn O. PearceSep 5, 2008
  7. Junio C HamanoSep 5, 2008
  8. Shawn O. PearceSep 5, 2008
  9. Johannes SixtSep 15, 2008
  10. Bert WesargNov 30, 2010
  11. Bert WesargNov 30, 2010
  12. Bert WesargDec 6, 2010
  13. Alexander GladyshSep 15, 2008
  14. Alexander GladyshSep 5, 2008
  15. Shawn O. PearceSep 15, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.