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

[PATCH 3/5] diff: buffer dirstat output

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 16, 2012, 04:47 UTC
Message-ID
<1326689281-23788-4-git-send-email-pclouds@gmail.com>
In-Reply-To
<1326689281-23788-1-git-send-email-pclouds@gmail.com>
This allows us to decide whether to print it later on.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 diff.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/diff.c b/diff.c
index 203b054..8c448b5 100644
--- a/diff.c
+++ b/diff.c
@@ -1548,6 +1548,7 @@ struct dirstat_file {
 struct dirstat_dir {
 	struct dirstat_file *files;
 	int alloc, nr, permille, cumulative;
+	struct strbuf *sb;
 };
 
 static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
@@ -1597,8 +1598,8 @@ static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
 		if (this_dir) {
 			int permille = this_dir * 1000 / changed;
 			if (permille >= dir->permille) {
-				fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
-					permille / 10, permille % 10, baselen, base);
+				strbuf_addf(dir->sb, "%s%4d.%01d%% %.*s\n", line_prefix,
+					    permille / 10, permille % 10, baselen, base);
 				if (!dir->cumulative)
 					return 0;
 			}
@@ -1614,18 +1615,20 @@ static int dirstat_compare(const void *_a, const void *_b)
 	return strcmp(a->name, b->name);
 }
 
-static void show_dirstat(struct diff_options *options)
+static void show_dirstat(struct diff_options *options, struct strbuf *output)
 {
 	int i;
 	unsigned long changed;
 	struct dirstat_dir dir;
 	struct diff_queue_struct *q = &diff_queued_diff;
+	struct strbuf sb = STRBUF_INIT;
 
 	dir.files = NULL;
 	dir.alloc = 0;
 	dir.nr = 0;
 	dir.permille = options->dirstat_permille;
 	dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
+	dir.sb = output ? output : &sb;
 
 	changed = 0;
 	for (i = 0; i < q->nr; i++) {
@@ -1711,6 +1714,9 @@ found_damage:
 	/* Show all directories with more than x% of the changes */
 	qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
 	gather_dirstat(options, &dir, changed, "", 0);
+	if (!output)
+		fputs(sb.buf, options->file);
+	strbuf_release(&sb);
 }
 
 static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
@@ -1718,6 +1724,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
 	int i;
 	unsigned long changed;
 	struct dirstat_dir dir;
+	struct strbuf sb = STRBUF_INIT;
 
 	if (data->nr == 0)
 		return;
@@ -1727,6 +1734,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
 	dir.nr = 0;
 	dir.permille = options->dirstat_permille;
 	dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
+	dir.sb = &sb;
 
 	changed = 0;
 	for (i = 0; i < data->nr; i++) {
@@ -1755,6 +1763,8 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
 	/* Show all directories with more than x% of the changes */
 	qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
 	gather_dirstat(options, &dir, changed, "", 0);
+	fputs(sb.buf, options->file);
+	strbuf_release(&sb);
 }
 
 static void free_diffstat_info(struct diffstat_t *diffstat)
@@ -4239,7 +4249,7 @@ void diff_flush(struct diff_options *options)
 		separator++;
 	}
 	if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
-		show_dirstat(options);
+		show_dirstat(options, NULL);
 
 	if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
 		for (i = 0; i < q->nr; i++) {
-- 
1.7.3.1.256.g2539c.dirty
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 4 of 6 in “Dynamic diff summary for merge/pull”
  1. 0/5 Dynamic diff summary for merge/pullNguyễn Thái Ngọc Duy, Jan 16, 2012
  2. 1/5 diff: add space around "="Nguyễn Thái Ngọc Duy, Jan 16, 2012
  3. 2/5 diff: do not duplicate shortstat with "git diff --stat --shortstat"Nguyễn Thái Ngọc Duy, Jan 16, 2012
  4. 3/5 diff: buffer dirstat outputNguyễn Thái Ngọc Duy, Jan 16, 2012
  5. 4/5 diff: add --dynstatNguyễn Thái Ngọc Duy, Jan 16, 2012
  6. 5/5 merge: support setting merge.[diff]stat to dynstatNguyễn Thái Ngọc Duy, Jan 16, 2012

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.