Skip to content

Commit e0965d8

Browse files
author
Linus Torvalds
committed
diff-tree: support list if input trees on stdin
This means that you can do git-rev-list HEAD --max-count=10 | git-diff-tree --stdin update-cache.c to see which (if any) of the last ten commits changed update-cache.c. Use the "-m" flag to see merges too. Normally they are suppressed.
1 parent fcfda02 commit e0965d8

File tree

1 file changed

+77
-9
lines changed

1 file changed

+77
-9
lines changed

diff-tree.c

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#include <ctype.h>
12
#include "cache.h"
23
#include "diff.h"
34

5+
static int ignore_merges = 1;
46
static int recursive = 0;
7+
static int read_stdin = 0;
58
static int line_termination = '\n';
69
static int generate_patch = 0;
710

@@ -250,20 +253,67 @@ static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, co
250253
return retval;
251254
}
252255

256+
static int diff_tree_stdin(char *line)
257+
{
258+
int len = strlen(line);
259+
unsigned char commit[20], parent[20];
260+
unsigned long size, offset;
261+
char *buf;
262+
263+
if (!len || line[len-1] != '\n')
264+
return -1;
265+
line[len-1] = 0;
266+
if (get_sha1_hex(line, commit))
267+
return -1;
268+
if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
269+
line[40] = ' ';
270+
line[81] = 0;
271+
printf("%s:\n", line);
272+
return diff_tree_sha1(parent, commit, "");
273+
}
274+
buf = read_object_with_reference(commit, "commit", &size, NULL);
275+
if (!buf)
276+
return -1;
277+
278+
/* More than one parent? */
279+
if (ignore_merges) {
280+
if (!memcmp(buf + 46 + 48, "parent ", 7))
281+
return 0;
282+
}
283+
284+
line[40] = 0;
285+
offset = 46;
286+
while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
287+
if (get_sha1_hex(buf + offset + 7, parent))
288+
return -1;
289+
printf("%s %s:\n", line, sha1_to_hex(parent));
290+
diff_tree_sha1(parent, commit, "");
291+
offset += 48;
292+
}
293+
return -1;
294+
}
295+
253296
static char *diff_tree_usage = "diff-tree [-p] [-r] [-z] <tree sha1> <tree sha1>";
254297

255298
int main(int argc, char **argv)
256299
{
300+
char line[1000];
257301
unsigned char old[20], new[20];
258302

259303
for (;;) {
260-
char *arg = argv[1];
304+
char *arg;
261305

306+
argv++;
307+
argc--;
308+
arg = *argv;
262309
if (!arg || *arg != '-')
263310
break;
264311

265-
argv++;
266-
argc--;
312+
if (!strcmp(arg, "-")) {
313+
argv++;
314+
argc--;
315+
break;
316+
}
267317
if (!strcmp(arg, "-r")) {
268318
recursive = 1;
269319
continue;
@@ -276,21 +326,39 @@ int main(int argc, char **argv)
276326
line_termination = '\0';
277327
continue;
278328
}
329+
if (!strcmp(arg, "-m")) {
330+
ignore_merges = 0;
331+
continue;
332+
}
333+
if (!strcmp(arg, "--stdin")) {
334+
read_stdin = 1;
335+
continue;
336+
}
279337
usage(diff_tree_usage);
280338
}
281339

282-
if (argc < 3 || get_sha1(argv[1], old) || get_sha1(argv[2], new))
283-
usage(diff_tree_usage);
340+
if (!read_stdin) {
341+
if (argc < 2 || get_sha1(argv[0], old) || get_sha1(argv[1], new))
342+
usage(diff_tree_usage);
343+
argv += 2;
344+
argc -= 2;
345+
}
284346

285-
if (argc > 3) {
347+
if (argc > 0) {
286348
int i;
287349

288-
paths = &argv[3];
289-
nr_paths = argc - 3;
350+
paths = argv;
351+
nr_paths = argc;
290352
pathlens = xmalloc(nr_paths * sizeof(int));
291353
for (i=0; i<nr_paths; i++)
292354
pathlens[i] = strlen(paths[i]);
293355
}
294356

295-
return diff_tree_sha1(old, new, "");
357+
if (!read_stdin)
358+
return diff_tree_sha1(old, new, "");
359+
360+
while (fgets(line, sizeof(line), stdin))
361+
diff_tree_stdin(line);
362+
363+
return 0;
296364
}

0 commit comments

Comments
 (0)