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

Fix "git diff --stat" with long filenames

From
Linus Torvalds <torvalds@osdl.org>
Date
May 8, 2006, 16:46 UTC
Message-ID
<Pine.LNX.4.64.0605080942410.3718@g5.osdl.org>

When we cut off the front of a filename to make it fit on the line, we add a "..." in front. However, the way the "git diff" code was written, we will never reset the prefix back to the empty string, so every single filename afterwards will have the "..." prefix, whether appropriate or not.

You can see this with "git diff v2.6.16.." on the current kernel tree, since there are filenames with long names that changed there:

 [ snip snip ]
 Documentation/filesystems/vfs.txt                  |  229 
 .../firmware_class/firmware_sample_driver.c        |    3 
 .../firmware_sample_firmware_class.c               |    1 
 ...Documentation/fujitsu/frv/kernel-ABI.txt           |  192 
 ...Documentation/hwmon/w83627hf                       |    4 
 [ snip snip ]

notice how the two Documentation/firmware** filenames caused the "..." to be added, but then the later filenames don't want it, and it also screws up the alignment of the line numbering afterwards.

Trivially fixed by moving the declaration (and initial setting) of the "prefix" variable into the for-loop where it is used.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
diff --git a/diff.c b/diff.c
index c845c87..5315270 100644
--- a/diff.c
+++ b/diff.c
@@ -296,7 +296,6 @@ static const char minuses[]= "----------
 
 static void show_stats(struct diffstat_t* data)
 {
-	char *prefix = "";
 	int i, len, add, del, total, adds = 0, dels = 0;
 	int max, max_change = 0, max_len = 0;
 	int total_files = data->nr;
@@ -318,6 +317,7 @@ static void show_stats(struct diffstat_t
 	}
 
 	for (i = 0; i < data->nr; i++) {
+		char *prefix = "";
 		char *name = data->files[i]->name;
 		int added = data->files[i]->added;
 		int deleted = data->files[i]->deleted;
Message 1 of 1 in “Fix "git diff --stat" with long filenames”
  1. Linus TorvaldsMay 8, 2006

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.