Code

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