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

[PATCH] --stat: ensure at least one '-' for deletions, and one '+' for additions

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Sep 28, 2006, 15:37 UTC
Message-ID
<Pine.LNX.4.63.0609281735040.14200@wbgn013.biozentrum.uni-wuerzburg.de>

The number of '-' and '+' is still linear. The idea is that scaled-length := floor(a * length + b) with the following constraints: if length == 1, scaled-length == 1, and the combined length of plusses and minusses should not be larger than the width by a small margin. Thus,

	a + b == 1
and
	a * max_plusses + b + a * max_minusses + b = width + 1
The solution is
	a * x + b = ((width - 1) * (x - 1) + max_change - 1)
		 / (max_change - 1)
Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---
	While testing this, I hit a bug which was hard to squash: commit
	v1.3.3~14 _always_ showed no minusses and plusses in the diffstat.
	Until I realized that the offending diffstat was in the commit
	_message_ :-)
 diff.c |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/diff.c b/diff.c
index 98c29bf..53c30bd 100644
--- a/diff.c
+++ b/diff.c
@@ -640,9 +640,12 @@ const char mime_boundary_leader[] = "---
 static int scale_linear(int it, int width, int max_change)
 {
 	/*
-	 * round(width * it / max_change);
+	 * make sure that at least one '-' is printed if there were deletions,
+	 * and likewise for '+'.
 	 */
-	return (it * width * 2 + max_change) / (max_change * 2);
+	if (max_change < 2)
+		return it;
+	return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
 }
 
 static void show_name(const char *prefix, const char *name, int len,
@@ -774,9 +777,9 @@ static void show_stats(struct diffstat_t
 		dels += del;
 
 		if (width <= max_change) {
-			total = scale_linear(total, width, max_change);
 			add = scale_linear(add, width, max_change);
-			del = total - add;
+			del = scale_linear(del, width, max_change);
+			total = add + del;
 		}
 		show_name(prefix, name, len, reset, set);
 		printf("%5d ", added + deleted);
-- 
1.4.2.1.g89d5d-dirty
Next: Junio C Hamano
Message 1 of 5 in “--stat: ensure at least one '-' for deletions, and one '+' for additions”
  1. --stat: ensure at least one '-' for deletions, and one '+' for additionsJohannes Schindelin, Sep 28, 2006
  2. Junio C HamanoSep 28, 2006
  3. Johannes SchindelinSep 28, 2006
  4. Junio C HamanoSep 29, 2006
  5. Johannes SchindelinSep 29, 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.