]>
Commit | Line | Data |
---|---|---|
9174026c | 1 | #include "cache.h" |
3ebfd4aa | 2 | #include "diff.h" |
e3bc7a3b | 3 | #include "commit.h" |
5f1c3f07 | 4 | #include "log-tree.h" |
9174026c | 5 | |
cd2bdc53 | 6 | static struct rev_info log_tree_opt; |
b11645be | 7 | |
45392a64 JH |
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; | |
5f1c3f07 | 13 | return log_tree_commit(&log_tree_opt, commit); |
45392a64 JH |
14 | } |
15 | ||
b11645be LT |
16 | static int diff_tree_stdin(char *line) |
17 | { | |
18 | int len = strlen(line); | |
45392a64 JH |
19 | unsigned char sha1[20]; |
20 | struct commit *commit; | |
b11645be LT |
21 | |
22 | if (!len || line[len-1] != '\n') | |
23 | return -1; | |
24 | line[len-1] = 0; | |
45392a64 JH |
25 | if (get_sha1_hex(line, sha1)) |
26 | return -1; | |
27 | commit = lookup_commit(sha1); | |
28 | if (!commit || parse_commit(commit)) | |
b11645be | 29 | return -1; |
45392a64 JH |
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 | } | |
b11645be | 50 | } |
5f1c3f07 | 51 | return log_tree_commit(&log_tree_opt, commit); |
e0965d83 LT |
52 | } |
53 | ||
4d1f1190 | 54 | static const char diff_tree_usage[] = |
d8f4790e | 55 | "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] " |
50b8e355 CS |
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" | |
dda2d79a | 59 | COMMON_DIFF_OPTIONS_HELP; |
a8db165e | 60 | |
6b5ee137 | 61 | int main(int argc, const char **argv) |
73134b6d | 62 | { |
0a8365a1 | 63 | int nr_sha1; |
e0965d83 | 64 | char line[1000]; |
cd2bdc53 LT |
65 | struct object *tree1, *tree2; |
66 | static struct rev_info *opt = &log_tree_opt; | |
67 | struct object_list *list; | |
5f1c3f07 | 68 | int read_stdin = 0; |
73134b6d | 69 | |
9ce392f4 | 70 | git_config(git_diff_config); |
0a8365a1 | 71 | nr_sha1 = 0; |
6b9c58f4 | 72 | init_revisions(opt); |
8e8f9987 | 73 | opt->abbrev = 0; |
91539833 | 74 | opt->diff = 1; |
cd2bdc53 | 75 | argc = setup_revisions(argc, argv, opt, NULL); |
6b5ee137 | 76 | |
cd2bdc53 LT |
77 | while (--argc > 0) { |
78 | const char *arg = *++argv; | |
c5b42386 | 79 | |
e0965d83 LT |
80 | if (!strcmp(arg, "--stdin")) { |
81 | read_stdin = 1; | |
82 | continue; | |
83 | } | |
c5bac17a | 84 | usage(diff_tree_usage); |
73134b6d LT |
85 | } |
86 | ||
cd2bdc53 LT |
87 | /* |
88 | * NOTE! "setup_revisions()" will have inserted the revisions | |
89 | * it parsed in reverse order. So if you do | |
90 | * | |
91 | * git-diff-tree a b | |
92 | * | |
93 | * the commit list will be "b" -> "a" -> NULL, so we reverse | |
94 | * the order of the objects if the first one is not marked | |
95 | * UNINTERESTING. | |
96 | */ | |
97 | nr_sha1 = 0; | |
98 | list = opt->pending_objects; | |
99 | if (list) { | |
100 | nr_sha1++; | |
101 | tree1 = list->item; | |
102 | list = list->next; | |
103 | if (list) { | |
104 | nr_sha1++; | |
105 | tree2 = tree1; | |
106 | tree1 = list->item; | |
107 | if (list->next) | |
108 | usage(diff_tree_usage); | |
109 | /* Switch them around if the second one was uninteresting.. */ | |
110 | if (tree2->flags & UNINTERESTING) { | |
111 | struct object *tmp = tree2; | |
112 | tree2 = tree1; | |
113 | tree1 = tmp; | |
114 | } | |
115 | } | |
116 | } | |
c5b42386 | 117 | |
0a8365a1 LT |
118 | switch (nr_sha1) { |
119 | case 0: | |
120 | if (!read_stdin) | |
121 | usage(diff_tree_usage); | |
122 | break; | |
123 | case 1: | |
cd2bdc53 | 124 | diff_tree_commit_sha1(tree1->sha1); |
0a8365a1 LT |
125 | break; |
126 | case 2: | |
cd2bdc53 LT |
127 | diff_tree_sha1(tree1->sha1, |
128 | tree2->sha1, | |
129 | "", &opt->diffopt); | |
5f1c3f07 | 130 | log_tree_diff_flush(opt); |
0a8365a1 LT |
131 | break; |
132 | } | |
133 | ||
e0965d83 | 134 | if (!read_stdin) |
0a8365a1 | 135 | return 0; |
e0965d83 | 136 | |
5f1c3f07 JH |
137 | if (opt->diffopt.detect_rename) |
138 | opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE | | |
6b5ee137 | 139 | DIFF_SETUP_USE_CACHE); |
e0965d83 LT |
140 | while (fgets(line, sizeof(line), stdin)) |
141 | diff_tree_stdin(line); | |
142 | ||
143 | return 0; | |
9174026c | 144 | } |