blob: be6417d166abf428d379a70b9d67f894da4641d1 [file] [log] [blame]
Linus Torvalds91740262005-04-09 13:00:54 -07001#include "cache.h"
Junio C Hamano3ebfd4a2005-04-27 09:21:00 -07002#include "diff.h"
Linus Torvaldse3bc7a32005-06-01 08:34:23 -07003#include "commit.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07004#include "log-tree.h"
Peter Eriksene8cc9cd2006-05-23 14:15:36 +02005#include "builtin.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +02006#include "submodule.h"
Linus Torvalds91740262005-04-09 13:00:54 -07007
Linus Torvaldscd2bdc52006-04-14 16:52:13 -07008static struct rev_info log_tree_opt;
Linus Torvaldsb11645b2005-05-18 13:06:47 -07009
Junio C Hamano45392a62006-02-05 23:00:41 -080010static int diff_tree_commit_sha1(const unsigned char *sha1)
11{
12 struct commit *commit = lookup_commit_reference(sha1);
13 if (!commit)
14 return -1;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070015 return log_tree_commit(&log_tree_opt, commit);
Junio C Hamano45392a62006-02-05 23:00:41 -080016}
17
Karl Hasselströma57114c2008-08-08 22:48:23 +020018/* Diff one or more commits. */
19static int stdin_diff_commit(struct commit *commit, char *line, int len)
Linus Torvaldse0965d82005-05-06 10:03:17 -070020{
Junio C Hamano45392a62006-02-05 23:00:41 -080021 unsigned char sha1[20];
Junio C Hamano45392a62006-02-05 23:00:41 -080022 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
23 /* Graft the fake parents locally to the commit */
24 int pos = 41;
25 struct commit_list **pptr, *parents;
26
27 /* Free the real parent list */
28 for (parents = commit->parents; parents; ) {
29 struct commit_list *tmp = parents->next;
30 free(parents);
31 parents = tmp;
32 }
33 commit->parents = NULL;
34 pptr = &(commit->parents);
35 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
36 struct commit *parent = lookup_commit(sha1);
37 if (parent) {
38 pptr = &commit_list_insert(parent, pptr)->next;
39 }
40 pos += 41;
41 }
Linus Torvaldse0965d82005-05-06 10:03:17 -070042 }
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070043 return log_tree_commit(&log_tree_opt, commit);
Linus Torvaldse0965d82005-05-06 10:03:17 -070044}
45
Karl Hasselström140b3782008-08-10 18:12:58 +020046/* Diff two trees. */
47static int stdin_diff_trees(struct tree *tree1, char *line, int len)
48{
49 unsigned char sha1[20];
50 struct tree *tree2;
51 if (len != 82 || !isspace(line[40]) || get_sha1_hex(line + 41, sha1))
52 return error("Need exactly two trees, separated by a space");
53 tree2 = lookup_tree(sha1);
54 if (!tree2 || parse_tree(tree2))
55 return -1;
56 printf("%s %s\n", sha1_to_hex(tree1->object.sha1),
57 sha1_to_hex(tree2->object.sha1));
58 diff_tree_sha1(tree1->object.sha1, tree2->object.sha1,
59 "", &log_tree_opt.diffopt);
60 log_tree_diff_flush(&log_tree_opt);
61 return 0;
62}
63
Karl Hasselströma57114c2008-08-08 22:48:23 +020064static int diff_tree_stdin(char *line)
65{
66 int len = strlen(line);
67 unsigned char sha1[20];
Karl Hasselström140b3782008-08-10 18:12:58 +020068 struct object *obj;
Karl Hasselströma57114c2008-08-08 22:48:23 +020069
70 if (!len || line[len-1] != '\n')
71 return -1;
72 line[len-1] = 0;
73 if (get_sha1_hex(line, sha1))
74 return -1;
Junio C Hamano628b06d2008-09-10 12:22:35 -070075 obj = lookup_unknown_object(sha1);
76 if (!obj || !obj->parsed)
77 obj = parse_object(sha1);
Karl Hasselström140b3782008-08-10 18:12:58 +020078 if (!obj)
Karl Hasselströma57114c2008-08-08 22:48:23 +020079 return -1;
Karl Hasselström140b3782008-08-10 18:12:58 +020080 if (obj->type == OBJ_COMMIT)
81 return stdin_diff_commit((struct commit *)obj, line, len);
82 if (obj->type == OBJ_TREE)
83 return stdin_diff_trees((struct tree *)obj, line, len);
84 error("Object %s is a %s, not a commit or tree",
85 sha1_to_hex(sha1), typename(obj->type));
86 return -1;
Karl Hasselströma57114c2008-08-08 22:48:23 +020087}
88
Petr Baudis4d1f1192005-07-29 11:01:26 +020089static const char diff_tree_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020090"git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
Chris Shoemaker50b8e352005-10-28 13:04:49 -040091"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
92" -r diff recursively\n"
93" --root include the initial commit as diff against /dev/null\n"
Junio C Hamanodda2d792005-07-13 12:52:35 -070094COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoa8db1652005-06-12 17:44:21 -070095
Junio C Hamanob4490052010-03-08 23:27:25 -080096static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
97{
98 if (!rev->diffopt.output_format) {
99 if (rev->dense_combined_merges)
100 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
101 else
102 rev->diffopt.output_format = DIFF_FORMAT_RAW;
103 }
104}
105
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700106int cmd_diff_tree(int argc, const char **argv, const char *prefix)
Linus Torvalds73134b62005-04-10 14:03:58 -0700107{
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700108 int nr_sha1;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700109 char line[1000];
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700110 struct object *tree1, *tree2;
111 static struct rev_info *opt = &log_tree_opt;
Junio C Hamanob4490052010-03-08 23:27:25 -0800112 struct setup_revision_opt s_r_opt;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700113 int read_stdin = 0;
Linus Torvalds73134b62005-04-10 14:03:58 -0700114
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700115 init_revisions(opt, prefix);
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200116 gitmodules_config();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100117 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Junio C Hamano8e8f9982006-04-14 22:19:38 -0700118 opt->abbrev = 0;
Linus Torvalds91539832006-04-17 11:59:32 -0700119 opt->diff = 1;
Junio C Hamano8b3dce52009-11-03 06:59:18 -0800120 opt->disable_stdin = 1;
Junio C Hamanob4490052010-03-08 23:27:25 -0800121 memset(&s_r_opt, 0, sizeof(s_r_opt));
122 s_r_opt.tweak = diff_tree_tweak_rev;
123 argc = setup_revisions(argc, argv, opt, &s_r_opt);
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700124
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700125 while (--argc > 0) {
126 const char *arg = *++argv;
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700127
Linus Torvaldse0965d82005-05-06 10:03:17 -0700128 if (!strcmp(arg, "--stdin")) {
129 read_stdin = 1;
130 continue;
131 }
Junio C Hamanoc5bac172005-04-20 19:49:16 -0700132 usage(diff_tree_usage);
Linus Torvalds73134b62005-04-10 14:03:58 -0700133 }
134
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700135 /*
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700136 * NOTE! We expect "a ^b" to be equal to "a..b", so we
137 * reverse the order of the objects if the second one
138 * is marked UNINTERESTING.
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700139 */
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700140 nr_sha1 = opt->pending.nr;
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700141 switch (nr_sha1) {
142 case 0:
143 if (!read_stdin)
144 usage(diff_tree_usage);
145 break;
146 case 1:
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700147 tree1 = opt->pending.objects[0].item;
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700148 diff_tree_commit_sha1(tree1->sha1);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700149 break;
150 case 2:
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700151 tree1 = opt->pending.objects[0].item;
152 tree2 = opt->pending.objects[1].item;
153 if (tree2->flags & UNINTERESTING) {
154 struct object *tmp = tree2;
155 tree2 = tree1;
156 tree1 = tmp;
157 }
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700158 diff_tree_sha1(tree1->sha1,
159 tree2->sha1,
160 "", &opt->diffopt);
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700161 log_tree_diff_flush(opt);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700162 break;
163 }
164
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100165 if (read_stdin) {
Junio C Hamanof31027c2011-01-06 13:50:06 -0800166 int saved_nrl = 0;
167 int saved_dcctc = 0;
168
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100169 if (opt->diffopt.detect_rename)
170 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
171 DIFF_SETUP_USE_CACHE);
172 while (fgets(line, sizeof(line), stdin)) {
173 unsigned char sha1[20];
Linus Torvaldse0965d82005-05-06 10:03:17 -0700174
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100175 if (get_sha1_hex(line, sha1)) {
176 fputs(line, stdout);
177 fflush(stdout);
178 }
Junio C Hamanof31027c2011-01-06 13:50:06 -0800179 else {
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100180 diff_tree_stdin(line);
Junio C Hamanof31027c2011-01-06 13:50:06 -0800181 if (saved_nrl < opt->diffopt.needed_rename_limit)
182 saved_nrl = opt->diffopt.needed_rename_limit;
183 if (opt->diffopt.degraded_cc_to_c)
184 saved_dcctc = 1;
185 }
Paul Mackerrase0c97ca2006-05-29 19:01:38 -0700186 }
Junio C Hamanof31027c2011-01-06 13:50:06 -0800187 opt->diffopt.degraded_cc_to_c = saved_dcctc;
188 opt->diffopt.needed_rename_limit = saved_nrl;
Paul Mackerrase0c97ca2006-05-29 19:01:38 -0700189 }
Junio C Hamanoda31b352007-12-13 23:40:27 -0800190
191 return diff_result_code(&opt->diffopt, 0);
Linus Torvalds91740262005-04-09 13:00:54 -0700192}