Linus Torvalds | 9174026 | 2005-04-09 13:00:54 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 3ebfd4a | 2005-04-27 09:21:00 -0700 | [diff] [blame] | 2 | #include "diff.h" |
Linus Torvalds | e3bc7a3 | 2005-06-01 08:34:23 -0700 | [diff] [blame] | 3 | #include "commit.h" |
Junio C Hamano | 5f1c3f0 | 2006-04-09 01:11:11 -0700 | [diff] [blame] | 4 | #include "log-tree.h" |
Peter Eriksen | e8cc9cd | 2006-05-23 14:15:36 +0200 | [diff] [blame] | 5 | #include "builtin.h" |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 6 | #include "submodule.h" |
Linus Torvalds | 9174026 | 2005-04-09 13:00:54 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 8 | static struct rev_info log_tree_opt; |
Linus Torvalds | b11645b | 2005-05-18 13:06:47 -0700 | [diff] [blame] | 9 | |
Junio C Hamano | 45392a6 | 2006-02-05 23:00:41 -0800 | [diff] [blame] | 10 | static 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 Hamano | 5f1c3f0 | 2006-04-09 01:11:11 -0700 | [diff] [blame] | 15 | return log_tree_commit(&log_tree_opt, commit); |
Junio C Hamano | 45392a6 | 2006-02-05 23:00:41 -0800 | [diff] [blame] | 16 | } |
| 17 | |
Karl Hasselström | a57114c | 2008-08-08 22:48:23 +0200 | [diff] [blame] | 18 | /* Diff one or more commits. */ |
| 19 | static int stdin_diff_commit(struct commit *commit, char *line, int len) |
Linus Torvalds | e0965d8 | 2005-05-06 10:03:17 -0700 | [diff] [blame] | 20 | { |
Junio C Hamano | 45392a6 | 2006-02-05 23:00:41 -0800 | [diff] [blame] | 21 | unsigned char sha1[20]; |
Junio C Hamano | 45392a6 | 2006-02-05 23:00:41 -0800 | [diff] [blame] | 22 | 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 Torvalds | e0965d8 | 2005-05-06 10:03:17 -0700 | [diff] [blame] | 42 | } |
Junio C Hamano | 5f1c3f0 | 2006-04-09 01:11:11 -0700 | [diff] [blame] | 43 | return log_tree_commit(&log_tree_opt, commit); |
Linus Torvalds | e0965d8 | 2005-05-06 10:03:17 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Karl Hasselström | 140b378 | 2008-08-10 18:12:58 +0200 | [diff] [blame] | 46 | /* Diff two trees. */ |
| 47 | static 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öm | a57114c | 2008-08-08 22:48:23 +0200 | [diff] [blame] | 64 | static int diff_tree_stdin(char *line) |
| 65 | { |
| 66 | int len = strlen(line); |
| 67 | unsigned char sha1[20]; |
Karl Hasselström | 140b378 | 2008-08-10 18:12:58 +0200 | [diff] [blame] | 68 | struct object *obj; |
Karl Hasselström | a57114c | 2008-08-08 22:48:23 +0200 | [diff] [blame] | 69 | |
| 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 Hamano | 628b06d | 2008-09-10 12:22:35 -0700 | [diff] [blame] | 75 | obj = lookup_unknown_object(sha1); |
| 76 | if (!obj || !obj->parsed) |
| 77 | obj = parse_object(sha1); |
Karl Hasselström | 140b378 | 2008-08-10 18:12:58 +0200 | [diff] [blame] | 78 | if (!obj) |
Karl Hasselström | a57114c | 2008-08-08 22:48:23 +0200 | [diff] [blame] | 79 | return -1; |
Karl Hasselström | 140b378 | 2008-08-10 18:12:58 +0200 | [diff] [blame] | 80 | 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öm | a57114c | 2008-08-08 22:48:23 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Petr Baudis | 4d1f119 | 2005-07-29 11:01:26 +0200 | [diff] [blame] | 89 | static const char diff_tree_usage[] = |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 90 | "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] " |
Chris Shoemaker | 50b8e35 | 2005-10-28 13:04:49 -0400 | [diff] [blame] | 91 | "[<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 Hamano | dda2d79 | 2005-07-13 12:52:35 -0700 | [diff] [blame] | 94 | COMMON_DIFF_OPTIONS_HELP; |
Junio C Hamano | a8db165 | 2005-06-12 17:44:21 -0700 | [diff] [blame] | 95 | |
Junio C Hamano | b449005 | 2010-03-08 23:27:25 -0800 | [diff] [blame] | 96 | static 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 Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 106 | int cmd_diff_tree(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 73134b6 | 2005-04-10 14:03:58 -0700 | [diff] [blame] | 107 | { |
Linus Torvalds | 0a8365a | 2005-05-18 13:10:17 -0700 | [diff] [blame] | 108 | int nr_sha1; |
Linus Torvalds | e0965d8 | 2005-05-06 10:03:17 -0700 | [diff] [blame] | 109 | char line[1000]; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 110 | struct object *tree1, *tree2; |
| 111 | static struct rev_info *opt = &log_tree_opt; |
Junio C Hamano | b449005 | 2010-03-08 23:27:25 -0800 | [diff] [blame] | 112 | struct setup_revision_opt s_r_opt; |
Junio C Hamano | 5f1c3f0 | 2006-04-09 01:11:11 -0700 | [diff] [blame] | 113 | int read_stdin = 0; |
Linus Torvalds | 73134b6 | 2005-04-10 14:03:58 -0700 | [diff] [blame] | 114 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 115 | init_revisions(opt, prefix); |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 116 | gitmodules_config(); |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 117 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
Junio C Hamano | 8e8f998 | 2006-04-14 22:19:38 -0700 | [diff] [blame] | 118 | opt->abbrev = 0; |
Linus Torvalds | 9153983 | 2006-04-17 11:59:32 -0700 | [diff] [blame] | 119 | opt->diff = 1; |
Junio C Hamano | 8b3dce5 | 2009-11-03 06:59:18 -0800 | [diff] [blame] | 120 | opt->disable_stdin = 1; |
Junio C Hamano | b449005 | 2010-03-08 23:27:25 -0800 | [diff] [blame] | 121 | 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 Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 124 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 125 | while (--argc > 0) { |
| 126 | const char *arg = *++argv; |
Linus Torvalds | c5b4238 | 2005-04-23 22:08:00 -0700 | [diff] [blame] | 127 | |
Linus Torvalds | e0965d8 | 2005-05-06 10:03:17 -0700 | [diff] [blame] | 128 | if (!strcmp(arg, "--stdin")) { |
| 129 | read_stdin = 1; |
| 130 | continue; |
| 131 | } |
Junio C Hamano | c5bac17 | 2005-04-20 19:49:16 -0700 | [diff] [blame] | 132 | usage(diff_tree_usage); |
Linus Torvalds | 73134b6 | 2005-04-10 14:03:58 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 135 | /* |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 136 | * 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 Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 139 | */ |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 140 | nr_sha1 = opt->pending.nr; |
Linus Torvalds | 0a8365a | 2005-05-18 13:10:17 -0700 | [diff] [blame] | 141 | switch (nr_sha1) { |
| 142 | case 0: |
| 143 | if (!read_stdin) |
| 144 | usage(diff_tree_usage); |
| 145 | break; |
| 146 | case 1: |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 147 | tree1 = opt->pending.objects[0].item; |
Linus Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 148 | diff_tree_commit_sha1(tree1->sha1); |
Linus Torvalds | 0a8365a | 2005-05-18 13:10:17 -0700 | [diff] [blame] | 149 | break; |
| 150 | case 2: |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 151 | 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 Torvalds | cd2bdc5 | 2006-04-14 16:52:13 -0700 | [diff] [blame] | 158 | diff_tree_sha1(tree1->sha1, |
| 159 | tree2->sha1, |
| 160 | "", &opt->diffopt); |
Junio C Hamano | 5f1c3f0 | 2006-04-09 01:11:11 -0700 | [diff] [blame] | 161 | log_tree_diff_flush(opt); |
Linus Torvalds | 0a8365a | 2005-05-18 13:10:17 -0700 | [diff] [blame] | 162 | break; |
| 163 | } |
| 164 | |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 165 | if (read_stdin) { |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 166 | int saved_nrl = 0; |
| 167 | int saved_dcctc = 0; |
| 168 | |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 169 | 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 Torvalds | e0965d8 | 2005-05-06 10:03:17 -0700 | [diff] [blame] | 174 | |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 175 | if (get_sha1_hex(line, sha1)) { |
| 176 | fputs(line, stdout); |
| 177 | fflush(stdout); |
| 178 | } |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 179 | else { |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 180 | diff_tree_stdin(line); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 181 | 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 Mackerras | e0c97ca | 2006-05-29 19:01:38 -0700 | [diff] [blame] | 186 | } |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 187 | opt->diffopt.degraded_cc_to_c = saved_dcctc; |
| 188 | opt->diffopt.needed_rename_limit = saved_nrl; |
Paul Mackerras | e0c97ca | 2006-05-29 19:01:38 -0700 | [diff] [blame] | 189 | } |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 190 | |
| 191 | return diff_result_code(&opt->diffopt, 0); |
Linus Torvalds | 9174026 | 2005-04-09 13:00:54 -0700 | [diff] [blame] | 192 | } |