Skip to content

Commit f4f21ce

Browse files
author
Linus Torvalds
committed
git-diff-tree: clean up output
This only shows the tree headers when something actually changed. Also, add a "silent" mode, which doesn't actually show the changes at all, just the commit information.
1 parent e0965d8 commit f4f21ce

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

diff-tree.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
#include "cache.h"
33
#include "diff.h"
44

5+
static int silent = 0;
56
static int ignore_merges = 1;
67
static int recursive = 0;
78
static int read_stdin = 0;
89
static int line_termination = '\n';
910
static int generate_patch = 0;
11+
static const char *header = NULL;
1012

1113
// What paths are we interested in?
1214
static int nr_paths = 0;
@@ -67,6 +69,14 @@ static void show_file(const char *prefix, void *tree, unsigned long size, const
6769
const char *path;
6870
const unsigned char *sha1 = extract(tree, size, &path, &mode);
6971

72+
if (header) {
73+
printf("%s", header);
74+
header = NULL;
75+
}
76+
77+
if (silent)
78+
return;
79+
7080
if (recursive && S_ISDIR(mode)) {
7181
char type[20];
7282
unsigned long size;
@@ -138,6 +148,13 @@ static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, uns
138148
return retval;
139149
}
140150

151+
if (header) {
152+
printf("%s", header);
153+
header = NULL;
154+
}
155+
if (silent)
156+
return 0;
157+
141158
if (generate_patch) {
142159
if (!S_ISDIR(mode1))
143160
diff_change(mode1, mode2, sha1, sha2, base, path1);
@@ -258,6 +275,7 @@ static int diff_tree_stdin(char *line)
258275
int len = strlen(line);
259276
unsigned char commit[20], parent[20];
260277
unsigned long size, offset;
278+
static char this_header[100];
261279
char *buf;
262280

263281
if (!len || line[len-1] != '\n')
@@ -266,9 +284,10 @@ static int diff_tree_stdin(char *line)
266284
if (get_sha1_hex(line, commit))
267285
return -1;
268286
if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
269-
line[40] = ' ';
287+
line[40] = 0;
270288
line[81] = 0;
271-
printf("%s:\n", line);
289+
sprintf(this_header, "%s (from %s)\n", line, line+41);
290+
header = this_header;
272291
return diff_tree_sha1(parent, commit, "");
273292
}
274293
buf = read_object_with_reference(commit, "commit", &size, NULL);
@@ -286,7 +305,8 @@ static int diff_tree_stdin(char *line)
286305
while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
287306
if (get_sha1_hex(buf + offset + 7, parent))
288307
return -1;
289-
printf("%s %s:\n", line, sha1_to_hex(parent));
308+
sprintf(this_header, "%s (from %s)\n", line, sha1_to_hex(parent));
309+
header = this_header;
290310
diff_tree_sha1(parent, commit, "");
291311
offset += 48;
292312
}
@@ -330,6 +350,10 @@ int main(int argc, char **argv)
330350
ignore_merges = 0;
331351
continue;
332352
}
353+
if (!strcmp(arg, "-s")) {
354+
silent = 1;
355+
continue;
356+
}
333357
if (!strcmp(arg, "--stdin")) {
334358
read_stdin = 1;
335359
continue;

0 commit comments

Comments
 (0)