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

[PATCH] diff: Make numstat machine friendly also for renames (and copies)

From
Jakub Narebski <jnareb@gmail.com>
Date
Dec 10, 2007, 22:32 UTC
Message-ID
<200712102332.53114.jnareb@gmail.com>

"git diff --numstat" used the same format as "git diff --stat" for renamed (and copied) files, except that filenames were not shortened when they didn't fit in the column width. This format is suitable for human consumption, but it cannot be unambiguously parsed.

Instead of that always use final file name ("to" name) for numstat. It is possible to find name before rename when name after is known.

This required to use pprint_rename (pretty print rename) during output (in the show_stats function) and not during parsing (in diffstat_add function).

Adding from_name field to struct diffstat_t makes is_renamed bitfield redundant; nevertheless for the sake of clarity, readability and making this patch minimal (and because it would not reduce memory footprint) it was not removed, and its used not replaced by checking from_name field.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
This would be useful for gitweb, later.
I hope I have made it in time before feature freeze...
 diff.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/diff.c b/diff.c
index f780e3e..38b9367 100644
--- a/diff.c
+++ b/diff.c
@@ -735,6 +735,7 @@ struct diffstat_t {
 	int alloc;
 	struct diffstat_file {
 		char *name;
+		char *from_name;
 		unsigned is_unmerged:1;
 		unsigned is_binary:1;
 		unsigned is_renamed:1;
@@ -755,11 +756,14 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 	}
 	diffstat->files[diffstat->nr++] = x;
 	if (name_b) {
-		x->name = pprint_rename(name_a, name_b);
+		x->from_name = xstrdup(name_a);
+		x->name      = xstrdup(name_b);
 		x->is_renamed = 1;
 	}
-	else
+	else {
+		x->from_name = NULL;
 		x->name = xstrdup(name_a);
+	}
 	return x;
 }
 
@@ -837,7 +841,7 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
 		struct diffstat_file *file = data->files[i];
 		int change = file->added + file->deleted;
 
-		if (!file->is_renamed) {  /* renames are already quoted by pprint_rename */
+		if (!file->is_renamed) {  /* renames will be quoted by pprint_rename */
 			struct strbuf buf;
 			strbuf_init(&buf, 0);
 			if (quote_c_style(file->name, &buf, NULL, 0)) {
@@ -846,6 +850,11 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
 			} else {
 				strbuf_release(&buf);
 			}
+		} else {
+			char *qname = pprint_rename(file->from_name, file->name);
+			free(file->name);
+			free(file->from_name);
+			file->name = qname;
 		}
 
 		len = strlen(file->name);
-- 
1.5.3.7
Next: Jakub Narebski
Message 1 of 12 in “diff: Make numstat machine friendly also for renames (and copies)”
  1. diff: Make numstat machine friendly also for renames (and copies)Jakub Narebski, Dec 10, 2007
  2. diff: Make numstat machine friendly also for renames (and copies)Jakub Narebski, Dec 10, 2007
  3. Junio C HamanoDec 11, 2007
  4. Jakub NarebskiDec 11, 2007
  5. Junio C HamanoDec 11, 2007
  6. Jakub NarebskiDec 11, 2007
  7. Junio C HamanoDec 12, 2007
  8. Jakub NarebskiDec 12, 2007
  9. Jakub NarebskiDec 12, 2007
  10. Junio C HamanoDec 12, 2007
  11. Junio C HamanoDec 10, 2007
  12. Jakub NarebskiDec 10, 2007

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.