1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4#include "log-tree.h"
5#include "builtin.h"
67
static struct rev_info log_tree_opt;
89
static int diff_tree_commit_sha1(const unsigned char *sha1)
10{
11struct commit *commit = lookup_commit_reference(sha1);
12if (!commit)
13return -1;
14return log_tree_commit(&log_tree_opt, commit);
15}
1617
static int diff_tree_stdin(char *line)
18{
19int len = strlen(line);
20unsigned char sha1[20];
21struct commit *commit;
2223
if (!len || line[len-1] != '\n')
24return -1;
25line[len-1] = 0;
26if (get_sha1_hex(line, sha1))
27return -1;
28commit = lookup_commit(sha1);
29if (!commit || parse_commit(commit))
30return -1;
31if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
32/* Graft the fake parents locally to the commit */
33int pos = 41;
34struct commit_list **pptr, *parents;
3536
/* Free the real parent list */
37for (parents = commit->parents; parents; ) {
38struct commit_list *tmp = parents->next;
39free(parents);
40parents = tmp;
41}
42commit->parents = NULL;
43pptr = &(commit->parents);
44while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
45struct commit *parent = lookup_commit(sha1);
46if (parent) {
47pptr = &commit_list_insert(parent, pptr)->next;
48}
49pos += 41;
50}
51}
52return log_tree_commit(&log_tree_opt, commit);
53}
5455
static const char diff_tree_usage[] =
56"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
57"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
58" -r diff recursively\n"
59" --root include the initial commit as diff against /dev/null\n"
60COMMON_DIFF_OPTIONS_HELP;
6162
int cmd_diff_tree(int argc, const char **argv, const char *prefix)
63{
64int nr_sha1;
65char line[1000];
66struct object *tree1, *tree2;
67static struct rev_info *opt = &log_tree_opt;
68int read_stdin = 0;
6970
init_revisions(opt, prefix);
71git_config(git_default_config); /* no "diff" UI options */
72nr_sha1 = 0;
73opt->abbrev = 0;
74opt->diff = 1;
75argc = setup_revisions(argc, argv, opt, NULL);
7677
while (--argc > 0) {
78const char *arg = *++argv;
7980
if (!strcmp(arg, "--stdin")) {
81read_stdin = 1;
82continue;
83}
84usage(diff_tree_usage);
85}
8687
if (!opt->diffopt.output_format)
88opt->diffopt.output_format = DIFF_FORMAT_RAW;
8990
/*
91* NOTE! We expect "a ^b" to be equal to "a..b", so we
92* reverse the order of the objects if the second one
93* is marked UNINTERESTING.
94*/
95nr_sha1 = opt->pending.nr;
96switch (nr_sha1) {
97case 0:
98if (!read_stdin)
99usage(diff_tree_usage);
100break;
101case 1:
102tree1 = opt->pending.objects[0].item;
103diff_tree_commit_sha1(tree1->sha1);
104break;
105case 2:
106tree1 = opt->pending.objects[0].item;
107tree2 = opt->pending.objects[1].item;
108if (tree2->flags & UNINTERESTING) {
109struct object *tmp = tree2;
110tree2 = tree1;
111tree1 = tmp;
112}
113diff_tree_sha1(tree1->sha1,
114tree2->sha1,
115"", &opt->diffopt);
116log_tree_diff_flush(opt);
117break;
118}
119120
if (!read_stdin)
121return 0;
122123
if (opt->diffopt.detect_rename)
124opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
125DIFF_SETUP_USE_CACHE);
126while (fgets(line, sizeof(line), stdin)) {
127unsigned char sha1[20];
128129
if (get_sha1_hex(line, sha1)) {
130fputs(line, stdout);
131fflush(stdout);
132}
133else
134diff_tree_stdin(line);
135}
136return 0;
137}