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

[PATCH v2] blame: correctly handle files regardless of autocrlf.

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Apr 26, 2014, 23:10 UTC
Message-ID
<1398553840-279066-1-git-send-email-sandals@crustytoothpaste.net>
In-Reply-To
<20140425150726.GA234593@vauxhall.crustytoothpaste.net>

If a file contained CRLF line endings in a repository with core.autocrlf=input, then blame always marked lines as "Not Committed Yet", even if they were unmodified. Don't attempt to convert the line endings when creating the fake commit so that blame works correctly regardless of the autocrlf setting.

Reported-by: Ephrim Khong <dr.khong@gmail.com>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
Show 9 quoted lines
> On Tue, Apr 22, 2014 at 04:12:42PM -0700, Junio C Hamano wrote:
> >  I have a feeling that a fix for this should be fairly isolated and
> >  trivial (it should be just the matter of paying attention to the
> >  crlf settings when synthesizing the fake commit)---perhaps somebody
> >  can squash in a fix to this?
>
> Last time I looked at this, I couldn't come up with an obvious fix for
> the problem, or I would have included one.  I'll try to have another go
> at it this weekend.

This was actually a very helpful hint, Junio. Thanks for the push in the right direction.

This includes the suggestion by Torsten Bögershausen to use the git -c for the blame command.

 builtin/blame.c               | 1 -
 t/t8003-blame-corner-cases.sh | 9 +++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/builtin/blame.c b/builtin/blame.c
index 88cb799..eef3e86 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2088,7 +2088,6 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
 		if (strbuf_read(&buf, 0, 0) < 0)
 			die_errno("failed to read from stdin");
 	}
-	convert_to_git(path, buf.buf, buf.len, &buf, 0);
 	origin->file.ptr = buf.buf;
 	origin->file.size = buf.len;
 	pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
index e7cac1d..2a3469b 100755
--- a/t/t8003-blame-corner-cases.sh
+++ b/t/t8003-blame-corner-cases.sh
@@ -191,4 +191,13 @@ test_expect_success 'indent of line numbers, ten lines' '
 	test $(grep -c "  " actual) = 9
 '
 
+test_expect_success 'blaming files with CRLF newlines' '
+	git config core.autocrlf false &&
+	printf "testcase\r\n" >crlffile &&
+	git add crlffile &&
+	git commit -m testcase &&
+	git -c core.autocrlf=input blame crlffile >actual &&
+	grep "A U Thor" actual
+'
+
 test_done
-- 
2.0.0.rc0
Previous: brian m. carlson
Message 4 of 4 in “What's cooking in git.git (Apr 2014, #07; Tue, 22)”
  1. Junio C HamanoApr 22, 2014
  2. Jiang XinApr 23, 2014
  3. brian m. carlsonApr 25, 2014
  4. blame: correctly handle files regardless of autocrlf.brian m. carlson, Apr 26, 2014

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.