Skip to contentSkip to conversation

Add --dirstat-by-file diff option

PATCH In review Heikki Orsila 3 participants 3 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add --dirstat-by-file diff option

--dirstat-by-file is the same as --dirstat, but it counts "impacted files" instead of "impacted lines" (lines that are added or removed).

The patch also cleans --cumulative option handling. DIFF_OPT_CUMULATIVE_DIRSTAT is used instead of DIFF_FORMAT_CUMULATIVE.

---
This version takes notice of comments from Junio C Hamano and Jeff King.

Changes to earlier version:

* use --dirstat-by-file instead of --filedirstat

* cleaner handling of --cumulative

 Documentation/diff-options.txt |    3 +++
 diff.c                         |   18 +++++++++++++-----
 diff.h                         |    3 ++-
 3 files changed, 18 insertions(+), 6 deletions(-)
Documentation/diff-options.txt +3 -0
index 1759386..039452b 100644
@@ -66,6 +66,9 @@ endif::git-format-patch[]
 	the "--cumulative" flag, which adds up percentages recursively
 	even when they have been already reported for a sub-directory.
 
+--dirstat-by-file[=limit]::
+	Same as --dirstat, but counts changed files instead of lines.
+
 --summary::
 	Output a condensed summary of extended header information
 	such as creations, renames and mode changes.
diff.c +13 -5
index 135dec4..9b8010d 100644
@@ -1078,7 +1078,7 @@ static void show_dirstat(struct diff_options *options)
 	dir.alloc = 0;
 	dir.nr = 0;
 	dir.percent = options->dirstat_percent;
-	dir.cumulative = options->output_format & DIFF_FORMAT_CUMULATIVE;
+	dir.cumulative = DIFF_OPT_TST(options, CUMULATIVE_DIRSTAT);
 
 	changed = 0;
 	for (i = 0; i < q->nr; i++) {
@@ -1110,9 +1110,13 @@ static void show_dirstat(struct diff_options *options)
 		/*
 		 * Original minus copied is the removed material,
 		 * added is the new material.  They are both damages
-		 * made to the preimage.
+		 * made to the preimage. In --dirstat-by-file mode, count
+		 * damaged files, not damaged lines. This is done by
+		 * counting only a single damaged line per file.
 		 */
 		damage = (p->one->size - copied) + added;
+		if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE) && damage > 0)
+			damage = 1;
 
 		ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
 		dir.files[dir.nr].name = name;
@@ -2472,9 +2476,13 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 		options->output_format |= DIFF_FORMAT_SHORTSTAT;
 	else if (opt_arg(arg, 'X', "dirstat", &options->dirstat_percent))
 		options->output_format |= DIFF_FORMAT_DIRSTAT;
-	else if (!strcmp(arg, "--cumulative"))
-		options->output_format |= DIFF_FORMAT_CUMULATIVE;
-	else if (!strcmp(arg, "--check"))
+	else if (opt_arg(arg, 0, "dirstat-by-file", &options->dirstat_percent)) {
+		options->output_format |= DIFF_FORMAT_DIRSTAT;
+		DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
+	} else if (!strcmp(arg, "--cumulative")) {
+		options->output_format |= DIFF_FORMAT_DIRSTAT;
+		DIFF_OPT_SET(options, CUMULATIVE_DIRSTAT);
+	} else if (!strcmp(arg, "--check"))
 		options->output_format |= DIFF_FORMAT_CHECKDIFF;
 	else if (!strcmp(arg, "--summary"))
 		options->output_format |= DIFF_FORMAT_SUMMARY;
diff.h +2 -1
index 50fb5dd..102c38c 100644
@@ -31,7 +31,6 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
 #define DIFF_FORMAT_PATCH	0x0010
 #define DIFF_FORMAT_SHORTSTAT	0x0020
 #define DIFF_FORMAT_DIRSTAT	0x0040
-#define DIFF_FORMAT_CUMULATIVE	0x0080
 
 /* These override all above */
 #define DIFF_FORMAT_NAME	0x0100
@@ -64,6 +63,8 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
 #define DIFF_OPT_CHECK_FAILED        (1 << 16)
 #define DIFF_OPT_RELATIVE_NAME       (1 << 17)
 #define DIFF_OPT_IGNORE_SUBMODULES   (1 << 18)
+#define DIFF_OPT_CUMULATIVE_DIRSTAT  (1 << 19)
+#define DIFF_OPT_DIRSTAT_BY_FILE     (1 << 20)
 #define DIFF_OPT_TST(opts, flag)    ((opts)->flags & DIFF_OPT_##flag)
 #define DIFF_OPT_SET(opts, flag)    ((opts)->flags |= DIFF_OPT_##flag)
 #define DIFF_OPT_CLR(opts, flag)    ((opts)->flags &= ~DIFF_OPT_##flag)
Signature · 1 line
1.6.0.1
#1 lore raw
Quoted 7 lines · Heikki Orsila

--dirstat-by-file is the same as --dirstat, but it counts "impacted files" instead of "impacted lines" (lines that are added or removed).

The patch also cleans --cumulative option handling. DIFF_OPT_CUMULATIVE_DIRSTAT is used instead of DIFF_FORMAT_CUMULATIVE.

I was going to suggest that this actually be split into two patches for readability, but lo and behold, Junio has already committed a fix for --cumulative to next. So you probably should rebase your other changes (which look reasonable to me) on top of that.

-Peff

#2 lore raw
Quoted 6 lines · Jeff King
Quoted 4 lines · Heikki Orsila

--dirstat-by-file is the same as --dirstat, but it counts "impacted files" instead of "impacted lines" (lines that are added or removed).

Quoted 8 lines
Quoted 3 lines

The patch also cleans --cumulative option handling. DIFF_OPT_CUMULATIVE_DIRSTAT is used instead of DIFF_FORMAT_CUMULATIVE.

I was going to suggest that this actually be split into two patches for readability, but lo and behold, Junio has already committed a fix for --cumulative to next. So you probably should rebase your other changes (which look reasonable to me) on top of that.

The fix was a maint material (the bug being "'diff --cumulative' is a no-op"), so it is everywhere not just next.

Heikki, thanks for the patch, and no need to rebase nor resend.

"am -3" works wonders in cases like this, and I can just remove the part that talk about cumulative from your proposed log message.

---

 Documentation/diff-options.txt |    3 +++
 diff.c                         |   10 +++++++++-
 diff.h                         |    1 +
 3 files changed, 13 insertions(+), 1 deletions(-)
Documentation/diff-options.txt +3 -0
index 6e26832..7788d4f 100644
@@ -65,6 +65,9 @@ endif::git-format-patch[]
 	can be set with "--dirstat=limit". Changes in a child directory is not
 	counted for the parent directory, unless "--cumulative" is used.
 
+--dirstat-by-file[=limit]::
+	Same as --dirstat, but counts changed files instead of lines.
+
 --summary::
 	Output a condensed summary of extended header information
 	such as creations, renames and mode changes.
diff.c +9 -1
index cbd151b..2de86eb 100644
@@ -1110,9 +1110,13 @@ static void show_dirstat(struct diff_options *options)
 		/*
 		 * Original minus copied is the removed material,
 		 * added is the new material.  They are both damages
-		 * made to the preimage.
+		 * made to the preimage. In --dirstat-by-file mode, count
+		 * damaged files, not damaged lines. This is done by
+		 * counting only a single damaged line per file.
 		 */
 		damage = (p->one->size - copied) + added;
+		if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE) && damage > 0)
+			damage = 1;
 
 		ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
 		dir.files[dir.nr].name = name;
@@ -2476,6 +2480,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 	else if (!strcmp(arg, "--cumulative")) {
 		options->output_format |= DIFF_FORMAT_DIRSTAT;
 		DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
+	} else if (opt_arg(arg, 0, "dirstat-by-file",
+			   &options->dirstat_percent)) {
+		options->output_format |= DIFF_FORMAT_DIRSTAT;
+		DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
 	}
 	else if (!strcmp(arg, "--check"))
 		options->output_format |= DIFF_FORMAT_CHECKDIFF;
diff.h +1 -0
index 7f53beb..c346888 100644
@@ -64,6 +64,7 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
 #define DIFF_OPT_RELATIVE_NAME       (1 << 17)
 #define DIFF_OPT_IGNORE_SUBMODULES   (1 << 18)
 #define DIFF_OPT_DIRSTAT_CUMULATIVE  (1 << 19)
+#define DIFF_OPT_DIRSTAT_BY_FILE     (1 << 20)
 #define DIFF_OPT_TST(opts, flag)    ((opts)->flags & DIFF_OPT_##flag)
 #define DIFF_OPT_SET(opts, flag)    ((opts)->flags |= DIFF_OPT_##flag)
 #define DIFF_OPT_CLR(opts, flag)    ((opts)->flags &= ~DIFF_OPT_##flag)