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, char **envp)
63{
64int nr_sha1;
65char line[1000];
66struct object *tree1, *tree2;
67static struct rev_info *opt = &log_tree_opt;
68int read_stdin = 0;
6970
git_config(git_diff_config);
71nr_sha1 = 0;
72init_revisions(opt);
73opt->abbrev = 0;
74opt->diff = 1;
75opt->diffopt.msg_sep = "---\n";
76argc = setup_revisions(argc, argv, opt, NULL);
7778
while (--argc > 0) {
79const char *arg = *++argv;
8081
if (!strcmp(arg, "--stdin")) {
82read_stdin = 1;
83continue;
84}
85usage(diff_tree_usage);
86}
8788
if (!opt->diffopt.output_format)
89opt->diffopt.output_format = DIFF_FORMAT_RAW;
9091
/*
92* NOTE! We expect "a ^b" to be equal to "a..b", so we
93* reverse the order of the objects if the second one
94* is marked UNINTERESTING.
95*/
96nr_sha1 = opt->pending.nr;
97switch (nr_sha1) {
98case 0:
99if (!read_stdin)
100usage(diff_tree_usage);
101break;
102case 1:
103tree1 = opt->pending.objects[0].item;
104diff_tree_commit_sha1(tree1->sha1);
105break;
106case 2:
107tree1 = opt->pending.objects[0].item;
108tree2 = opt->pending.objects[1].item;
109if (tree2->flags & UNINTERESTING) {
110struct object *tmp = tree2;
111tree2 = tree1;
112tree1 = tmp;
113}
114diff_tree_sha1(tree1->sha1,
115tree2->sha1,
116"", &opt->diffopt);
117log_tree_diff_flush(opt);
118break;
119}
120121
if (!read_stdin)
122return 0;
123124
if (opt->diffopt.detect_rename)
125opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
126DIFF_SETUP_USE_CACHE);
127while (fgets(line, sizeof(line), stdin)) {
128unsigned char sha1[20];
129130
if (get_sha1_hex(line, sha1)) {
131fputs(line, stdout);
132fflush(stdout);
133}
134else
135diff_tree_stdin(line);
136}
137return 0;
138}