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;
68struct object_list *list;
69int read_stdin = 0;
7071
git_config(git_diff_config);
72nr_sha1 = 0;
73init_revisions(opt);
74opt->abbrev = 0;
75opt->diff = 1;
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
/*
89* NOTE! "setup_revisions()" will have inserted the revisions
90* it parsed in reverse order. So if you do
91*
92* git-diff-tree a b
93*
94* the commit list will be "b" -> "a" -> NULL, so we reverse
95* the order of the objects if the first one is not marked
96* UNINTERESTING.
97*/
98nr_sha1 = 0;
99list = opt->pending_objects;
100if (list) {
101nr_sha1++;
102tree1 = list->item;
103list = list->next;
104if (list) {
105nr_sha1++;
106tree2 = tree1;
107tree1 = list->item;
108if (list->next)
109usage(diff_tree_usage);
110/* Switch them around if the second one was uninteresting.. */
111if (tree2->flags & UNINTERESTING) {
112struct object *tmp = tree2;
113tree2 = tree1;
114tree1 = tmp;
115}
116}
117}
118119
switch (nr_sha1) {
120case 0:
121if (!read_stdin)
122usage(diff_tree_usage);
123break;
124case 1:
125diff_tree_commit_sha1(tree1->sha1);
126break;
127case 2:
128diff_tree_sha1(tree1->sha1,
129tree2->sha1,
130"", &opt->diffopt);
131log_tree_diff_flush(opt);
132break;
133}
134135
if (!read_stdin)
136return 0;
137138
if (opt->diffopt.detect_rename)
139opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
140DIFF_SETUP_USE_CACHE);
141while (fgets(line, sizeof(line), stdin)) {
142unsigned char sha1[20];
143144
if (get_sha1_hex(line, sha1)) {
145fputs(line, stdout);
146fflush(stdout);
147}
148else
149diff_tree_stdin(line);
150}
151return 0;
152}