blob: 86be6342861be40da9edbf2f1a2002fa4abc0cda [file] [log] [blame]
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001#define USE_THE_INDEX_VARIABLE
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00002#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Junio C Hamano3ebfd4a2005-04-27 09:21:00 -07004#include "diff.h"
Linus Torvaldse3bc7a32005-06-01 08:34:23 -07005#include "commit.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00006#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00007#include "hex.h"
Junio C Hamano5f1c3f02006-04-09 01:11:11 -07008#include "log-tree.h"
Jens Lehmann302ad7a2010-08-06 00:40:48 +02009#include "submodule.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000010#include "read-cache-ll.h"
Stefan Beller109cd762018-06-28 18:21:51 -070011#include "repository.h"
Elijah Newren0fd2e212023-05-16 06:33:58 +000012#include "revision.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000013#include "tree.h"
Linus Torvalds91740262005-04-09 13:00:54 -070014
Linus Torvaldscd2bdc52006-04-14 16:52:13 -070015static struct rev_info log_tree_opt;
Linus Torvaldsb11645b2005-05-18 13:06:47 -070016
Brandon Williams315f49f2017-05-30 10:31:04 -070017static int diff_tree_commit_oid(const struct object_id *oid)
Junio C Hamano45392a62006-02-05 23:00:41 -080018{
Stefan Beller2122f672018-06-28 18:21:58 -070019 struct commit *commit = lookup_commit_reference(the_repository, oid);
Junio C Hamano45392a62006-02-05 23:00:41 -080020 if (!commit)
21 return -1;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070022 return log_tree_commit(&log_tree_opt, commit);
Junio C Hamano45392a62006-02-05 23:00:41 -080023}
24
Karl Hasselströma57114c2008-08-08 22:48:23 +020025/* Diff one or more commits. */
brian m. carlson5f5e9362017-02-21 23:47:21 +000026static int stdin_diff_commit(struct commit *commit, const char *p)
Linus Torvaldse0965d82005-05-06 10:03:17 -070027{
brian m. carlson5f5e9362017-02-21 23:47:21 +000028 struct object_id oid;
29 struct commit_list **pptr = NULL;
Junio C Hamano45392a62006-02-05 23:00:41 -080030
brian m. carlson5f5e9362017-02-21 23:47:21 +000031 /* Graft the fake parents locally to the commit */
32 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
Stefan Bellerc1f5eb42018-06-28 18:21:59 -070033 struct commit *parent = lookup_commit(the_repository, &oid);
brian m. carlson5f5e9362017-02-21 23:47:21 +000034 if (!pptr) {
35 /* Free the real parent list */
36 free_commit_list(commit->parents);
37 commit->parents = NULL;
38 pptr = &(commit->parents);
39 }
40 if (parent) {
41 pptr = &commit_list_insert(parent, pptr)->next;
Junio C Hamano45392a62006-02-05 23:00:41 -080042 }
Linus Torvaldse0965d82005-05-06 10:03:17 -070043 }
Junio C Hamano5f1c3f02006-04-09 01:11:11 -070044 return log_tree_commit(&log_tree_opt, commit);
Linus Torvaldse0965d82005-05-06 10:03:17 -070045}
46
Karl Hasselström140b3782008-08-10 18:12:58 +020047/* Diff two trees. */
brian m. carlson5f5e9362017-02-21 23:47:21 +000048static int stdin_diff_trees(struct tree *tree1, const char *p)
Karl Hasselström140b3782008-08-10 18:12:58 +020049{
brian m. carlson5f5e9362017-02-21 23:47:21 +000050 struct object_id oid;
Karl Hasselström140b3782008-08-10 18:12:58 +020051 struct tree *tree2;
brian m. carlson5f5e9362017-02-21 23:47:21 +000052 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
Karl Hasselström140b3782008-08-10 18:12:58 +020053 return error("Need exactly two trees, separated by a space");
Stefan Bellerf86bcc72018-06-28 18:21:56 -070054 tree2 = lookup_tree(the_repository, &oid);
Karl Hasselström140b3782008-08-10 18:12:58 +020055 if (!tree2 || parse_tree(tree2))
56 return -1;
brian m. carlsonf2fd0762015-11-10 02:22:28 +000057 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
58 oid_to_hex(&tree2->object.oid));
Brandon Williams66f414f2017-05-30 10:31:03 -070059 diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
60 "", &log_tree_opt.diffopt);
Karl Hasselström140b3782008-08-10 18:12:58 +020061 log_tree_diff_flush(&log_tree_opt);
62 return 0;
63}
64
Karl Hasselströma57114c2008-08-08 22:48:23 +020065static int diff_tree_stdin(char *line)
66{
67 int len = strlen(line);
brian m. carlson5f5e9362017-02-21 23:47:21 +000068 struct object_id oid;
Karl Hasselström140b3782008-08-10 18:12:58 +020069 struct object *obj;
brian m. carlson5f5e9362017-02-21 23:47:21 +000070 const char *p;
Karl Hasselströma57114c2008-08-08 22:48:23 +020071
72 if (!len || line[len-1] != '\n')
73 return -1;
74 line[len-1] = 0;
brian m. carlson5f5e9362017-02-21 23:47:21 +000075 if (parse_oid_hex(line, &oid, &p))
Karl Hasselströma57114c2008-08-08 22:48:23 +020076 return -1;
Stefan Beller109cd762018-06-28 18:21:51 -070077 obj = parse_object(the_repository, &oid);
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)
brian m. carlson5f5e9362017-02-21 23:47:21 +000081 return stdin_diff_commit((struct commit *)obj, p);
Karl Hasselström140b3782008-08-10 18:12:58 +020082 if (obj->type == OBJ_TREE)
brian m. carlson5f5e9362017-02-21 23:47:21 +000083 return stdin_diff_trees((struct tree *)obj, p);
Karl Hasselström140b3782008-08-10 18:12:58 +020084 error("Object %s is a %s, not a commit or tree",
Brandon Williamsdebca9d2018-02-14 10:59:24 -080085 oid_to_hex(&oid), type_name(obj->type));
Karl Hasselström140b3782008-08-10 18:12:58 +020086 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[] =
Ævar Arnfjörð Bjarmason320ee662022-10-13 17:39:22 +020090"git diff-tree [--stdin] [-m] [-s] [-v] [--no-commit-id] [--pretty]\n"
91" [-t] [-r] [-c | --cc] [--combined-all-paths] [--root] [--merge-base]\n"
92" [<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
Ævar Arnfjörð Bjarmasonacf78282022-10-13 17:39:01 +020093"\n"
Chris Shoemaker50b8e352005-10-28 13:04:49 -040094" -r diff recursively\n"
Elijah Newrend76ce4f2019-02-07 17:12:46 -080095" -c show combined diff for merge commits\n"
96" --cc show combined diff for merge commits removing uninteresting hunks\n"
97" --combined-all-paths\n"
98" show name of file in all parents for combined diffs\n"
Chris Shoemaker50b8e352005-10-28 13:04:49 -040099" --root include the initial commit as diff against /dev/null\n"
Junio C Hamanodda2d792005-07-13 12:52:35 -0700100COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoa8db1652005-06-12 17:44:21 -0700101
Jeff Kingcc88afa2023-07-03 02:44:16 -0400102static void diff_tree_tweak_rev(struct rev_info *rev)
Junio C Hamanob4490052010-03-08 23:27:25 -0800103{
104 if (!rev->diffopt.output_format) {
105 if (rev->dense_combined_merges)
106 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
107 else
108 rev->diffopt.output_format = DIFF_FORMAT_RAW;
109 }
110}
111
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700112int cmd_diff_tree(int argc, const char **argv, const char *prefix)
Linus Torvalds73134b62005-04-10 14:03:58 -0700113{
Linus Torvaldse0965d82005-05-06 10:03:17 -0700114 char line[1000];
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700115 struct object *tree1, *tree2;
116 static struct rev_info *opt = &log_tree_opt;
Junio C Hamanob4490052010-03-08 23:27:25 -0800117 struct setup_revision_opt s_r_opt;
Taylor Blau5778b222020-04-20 18:13:15 -0600118 struct userformat_want w;
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700119 int read_stdin = 0;
Denton Liu3d09c222020-09-14 11:36:52 -0700120 int merge_base = 0;
Linus Torvalds73134b62005-04-10 14:03:58 -0700121
Junio C Hamano5a88f972017-06-01 13:38:16 +0900122 if (argc == 2 && !strcmp(argv[1], "-h"))
123 usage(diff_tree_usage);
124
Marc Branchaud37590ce2017-05-08 12:03:37 -0400125 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
Shuqi Liang48c5fbf2023-05-18 11:44:54 -0400126
127 prepare_repo_settings(the_repository);
128 the_repository->settings.command_requires_full_index = 0;
129
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200130 repo_init_revisions(the_repository, opt, prefix);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100131 if (repo_read_index(the_repository) < 0)
Brandon Williamsfd66bcc2017-12-06 14:02:56 -0800132 die(_("index file corrupt"));
Junio C Hamano8e8f9982006-04-14 22:19:38 -0700133 opt->abbrev = 0;
Linus Torvalds91539832006-04-17 11:59:32 -0700134 opt->diff = 1;
Junio C Hamano8b3dce52009-11-03 06:59:18 -0800135 opt->disable_stdin = 1;
Junio C Hamanob4490052010-03-08 23:27:25 -0800136 memset(&s_r_opt, 0, sizeof(s_r_opt));
137 s_r_opt.tweak = diff_tree_tweak_rev;
Alexander Rinass90a78b82016-05-13 22:41:02 +0200138
Torsten Bögershausen5c327502021-02-03 17:28:23 +0100139 prefix = precompose_argv_prefix(argc, argv, prefix);
Junio C Hamanob4490052010-03-08 23:27:25 -0800140 argc = setup_revisions(argc, argv, opt, &s_r_opt);
Junio C Hamano6b5ee132005-09-21 00:00:47 -0700141
Taylor Blau5778b222020-04-20 18:13:15 -0600142 memset(&w, 0, sizeof(w));
143 userformat_find_requirements(NULL, &w);
144
145 if (!opt->show_notes_given && w.notes)
146 opt->show_notes = 1;
147 if (opt->show_notes)
148 load_display_notes(&opt->notes_opt);
149
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700150 while (--argc > 0) {
151 const char *arg = *++argv;
Linus Torvaldsc5b42382005-04-23 22:08:00 -0700152
Linus Torvaldse0965d82005-05-06 10:03:17 -0700153 if (!strcmp(arg, "--stdin")) {
154 read_stdin = 1;
155 continue;
156 }
Denton Liu3d09c222020-09-14 11:36:52 -0700157 if (!strcmp(arg, "--merge-base")) {
158 merge_base = 1;
159 continue;
160 }
Junio C Hamanoc5bac172005-04-20 19:49:16 -0700161 usage(diff_tree_usage);
Linus Torvalds73134b62005-04-10 14:03:58 -0700162 }
163
Denton Liu3d09c222020-09-14 11:36:52 -0700164 if (read_stdin && merge_base)
Jean-Noël Avila43ea6352022-01-05 20:02:14 +0000165 die(_("options '%s' and '%s' cannot be used together"), "--stdin", "--merge-base");
Denton Liu3d09c222020-09-14 11:36:52 -0700166 if (merge_base && opt->pending.nr != 2)
167 die(_("--merge-base only works with two commits"));
168
Junio C Hamano1eb41362021-02-11 11:57:50 -0800169 opt->diffopt.rotate_to_strict = 1;
170
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700171 /*
Junio C Hamano8ba74bf2017-06-02 11:34:15 +0900172 * NOTE! We expect "a..b" to expand to "^a b" but it is
173 * perfectly valid for revision range parser to yield "b ^a",
174 * which means the same thing. If we get the latter, i.e. the
175 * second one is marked UNINTERESTING, we recover the original
176 * order the user gave, i.e. "a..b", by swapping the trees.
Linus Torvaldscd2bdc52006-04-14 16:52:13 -0700177 */
Brandon Williams315f49f2017-05-30 10:31:04 -0700178 switch (opt->pending.nr) {
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700179 case 0:
180 if (!read_stdin)
181 usage(diff_tree_usage);
182 break;
183 case 1:
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700184 tree1 = opt->pending.objects[0].item;
Brandon Williams315f49f2017-05-30 10:31:04 -0700185 diff_tree_commit_oid(&tree1->oid);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700186 break;
187 case 2:
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700188 tree1 = opt->pending.objects[0].item;
189 tree2 = opt->pending.objects[1].item;
Denton Liu3d09c222020-09-14 11:36:52 -0700190 if (merge_base) {
191 struct object_id oid;
192
193 diff_get_merge_base(opt, &oid);
194 tree1 = lookup_object(the_repository, &oid);
195 } else if (tree2->flags & UNINTERESTING) {
René Scharfe35d803b2017-01-28 22:40:58 +0100196 SWAP(tree2, tree1);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700197 }
Brandon Williams66f414f2017-05-30 10:31:03 -0700198 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
Junio C Hamano5f1c3f02006-04-09 01:11:11 -0700199 log_tree_diff_flush(opt);
Linus Torvalds0a8365a2005-05-18 13:10:17 -0700200 break;
201 }
202
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100203 if (read_stdin) {
Junio C Hamanof31027c2011-01-06 13:50:06 -0800204 int saved_nrl = 0;
205 int saved_dcctc = 0;
206
Junio C Hamano1eb41362021-02-11 11:57:50 -0800207 opt->diffopt.rotate_to_strict = 0;
Junio C Hamanof8781bf2022-04-26 09:11:44 -0700208 opt->diffopt.no_free = 1;
Nguyễn Thái Ngọc Duyff7fe372018-08-13 18:14:18 +0200209 if (opt->diffopt.detect_rename) {
210 if (!the_index.cache)
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700211 repo_read_index(the_repository);
Nguyễn Thái Ngọc Duyff7fe372018-08-13 18:14:18 +0200212 opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
213 }
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100214 while (fgets(line, sizeof(line), stdin)) {
brian m. carlson5f5e9362017-02-21 23:47:21 +0000215 struct object_id oid;
Linus Torvaldse0965d82005-05-06 10:03:17 -0700216
brian m. carlson5f5e9362017-02-21 23:47:21 +0000217 if (get_oid_hex(line, &oid)) {
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100218 fputs(line, stdout);
219 fflush(stdout);
220 }
Junio C Hamanof31027c2011-01-06 13:50:06 -0800221 else {
Wincent Colaiuta62c64892007-12-13 21:24:52 +0100222 diff_tree_stdin(line);
Junio C Hamanof31027c2011-01-06 13:50:06 -0800223 if (saved_nrl < opt->diffopt.needed_rename_limit)
224 saved_nrl = opt->diffopt.needed_rename_limit;
225 if (opt->diffopt.degraded_cc_to_c)
226 saved_dcctc = 1;
227 }
Paul Mackerrase0c97ca2006-05-29 19:01:38 -0700228 }
Junio C Hamanof31027c2011-01-06 13:50:06 -0800229 opt->diffopt.degraded_cc_to_c = saved_dcctc;
230 opt->diffopt.needed_rename_limit = saved_nrl;
Junio C Hamanof8781bf2022-04-26 09:11:44 -0700231 opt->diffopt.no_free = 0;
232 diff_free(&opt->diffopt);
Paul Mackerrase0c97ca2006-05-29 19:01:38 -0700233 }
Junio C Hamanoda31b352007-12-13 23:40:27 -0800234
Jeff King5cc6b2d2023-08-21 16:20:46 -0400235 return diff_result_code(&opt->diffopt);
Linus Torvalds91740262005-04-09 13:00:54 -0700236}