e5787985373739dee8dd8874ea9f8f6ff073af27
[git.git] / diff-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5
6 static struct rev_info log_tree_opt;
7
8 static int diff_tree_commit_sha1(const unsigned char *sha1)
9 {
10         struct commit *commit = lookup_commit_reference(sha1);
11         if (!commit)
12                 return -1;
13         return log_tree_commit(&log_tree_opt, commit);
14 }
15
16 static int diff_tree_stdin(char *line)
17 {
18         int len = strlen(line);
19         unsigned char sha1[20];
20         struct commit *commit;
21
22         if (!len || line[len-1] != '\n')
23                 return -1;
24         line[len-1] = 0;
25         if (get_sha1_hex(line, sha1))
26                 return -1;
27         commit = lookup_commit(sha1);
28         if (!commit || parse_commit(commit))
29                 return -1;
30         if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
31                 /* Graft the fake parents locally to the commit */
32                 int pos = 41;
33                 struct commit_list **pptr, *parents;
34
35                 /* Free the real parent list */
36                 for (parents = commit->parents; parents; ) {
37                         struct commit_list *tmp = parents->next;
38                         free(parents);
39                         parents = tmp;
40                 }
41                 commit->parents = NULL;
42                 pptr = &(commit->parents);
43                 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
44                         struct commit *parent = lookup_commit(sha1);
45                         if (parent) {
46                                 pptr = &commit_list_insert(parent, pptr)->next;
47                         }
48                         pos += 41;
49                 }
50         }
51         return log_tree_commit(&log_tree_opt, commit);
52 }
53
54 static const char diff_tree_usage[] =
55 "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
56 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
57 "  -r            diff recursively\n"
58 "  --root        include the initial commit as diff against /dev/null\n"
59 COMMON_DIFF_OPTIONS_HELP;
60
61 int main(int argc, const char **argv)
62 {
63         int nr_sha1;
64         char line[1000];
65         struct object *tree1, *tree2;
66         static struct rev_info *opt = &log_tree_opt;
67         struct object_list *list;
68         int read_stdin = 0;
69
70         git_config(git_diff_config);
71         nr_sha1 = 0;
72         init_revisions(opt);
73         opt->abbrev = 0;
74         argc = setup_revisions(argc, argv, opt, NULL);
75
76         while (--argc > 0) {
77                 const char *arg = *++argv;
78
79                 if (!strcmp(arg, "--stdin")) {
80                         read_stdin = 1;
81                         continue;
82                 }
83                 usage(diff_tree_usage);
84         }
85
86         /*
87          * NOTE! "setup_revisions()" will have inserted the revisions
88          * it parsed in reverse order. So if you do
89          *
90          *      git-diff-tree a b
91          *
92          * the commit list will be "b" -> "a" -> NULL, so we reverse
93          * the order of the objects if the first one is not marked
94          * UNINTERESTING.
95          */
96         nr_sha1 = 0;
97         list = opt->pending_objects;
98         if (list) {
99                 nr_sha1++;
100                 tree1 = list->item;
101                 list = list->next;
102                 if (list) {
103                         nr_sha1++;
104                         tree2 = tree1;
105                         tree1 = list->item;
106                         if (list->next)
107                                 usage(diff_tree_usage);
108                         /* Switch them around if the second one was uninteresting.. */
109                         if (tree2->flags & UNINTERESTING) {
110                                 struct object *tmp = tree2;
111                                 tree2 = tree1;
112                                 tree1 = tmp;
113                         }
114                 }
115         }
116
117         switch (nr_sha1) {
118         case 0:
119                 if (!read_stdin)
120                         usage(diff_tree_usage);
121                 break;
122         case 1:
123                 diff_tree_commit_sha1(tree1->sha1);
124                 break;
125         case 2:
126                 diff_tree_sha1(tree1->sha1,
127                                tree2->sha1,
128                                "", &opt->diffopt);
129                 log_tree_diff_flush(opt);
130                 break;
131         }
132
133         if (!read_stdin)
134                 return 0;
135
136         if (opt->diffopt.detect_rename)
137                 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
138                                        DIFF_SETUP_USE_CACHE);
139         while (fgets(line, sizeof(line), stdin))
140                 diff_tree_stdin(line);
141
142         return 0;
143 }