1 #define USE_THE_INDEX_VARIABLE
10 #include "read-cache-ll.h"
11 #include "repository.h"
15 static struct rev_info log_tree_opt
;
17 static int diff_tree_commit_oid(const struct object_id
*oid
)
19 struct commit
*commit
= lookup_commit_reference(the_repository
, oid
);
22 return log_tree_commit(&log_tree_opt
, commit
);
25 /* Diff one or more commits. */
26 static int stdin_diff_commit(struct commit
*commit
, const char *p
)
29 struct commit_list
**pptr
= NULL
;
31 /* Graft the fake parents locally to the commit */
32 while (isspace(*p
++) && !parse_oid_hex(p
, &oid
, &p
)) {
33 struct commit
*parent
= lookup_commit(the_repository
, &oid
);
35 /* Free the real parent list */
36 free_commit_list(commit
->parents
);
37 commit
->parents
= NULL
;
38 pptr
= &(commit
->parents
);
41 pptr
= &commit_list_insert(parent
, pptr
)->next
;
44 return log_tree_commit(&log_tree_opt
, commit
);
48 static int stdin_diff_trees(struct tree
*tree1
, const char *p
)
52 if (!isspace(*p
++) || parse_oid_hex(p
, &oid
, &p
) || *p
)
53 return error("Need exactly two trees, separated by a space");
54 tree2
= lookup_tree(the_repository
, &oid
);
55 if (!tree2
|| parse_tree(tree2
))
57 printf("%s %s\n", oid_to_hex(&tree1
->object
.oid
),
58 oid_to_hex(&tree2
->object
.oid
));
59 diff_tree_oid(&tree1
->object
.oid
, &tree2
->object
.oid
,
60 "", &log_tree_opt
.diffopt
);
61 log_tree_diff_flush(&log_tree_opt
);
65 static int diff_tree_stdin(char *line
)
67 int len
= strlen(line
);
72 if (!len
|| line
[len
-1] != '\n')
75 if (parse_oid_hex(line
, &oid
, &p
))
77 obj
= parse_object(the_repository
, &oid
);
80 if (obj
->type
== OBJ_COMMIT
)
81 return stdin_diff_commit((struct commit
*)obj
, p
);
82 if (obj
->type
== OBJ_TREE
)
83 return stdin_diff_trees((struct tree
*)obj
, p
);
84 error("Object %s is a %s, not a commit or tree",
85 oid_to_hex(&oid
), type_name(obj
->type
));
89 static const char diff_tree_usage
[] =
90 "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"
94 " -r diff recursively\n"
95 " -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"
99 " --root include the initial commit as diff against /dev/null\n"
100 COMMON_DIFF_OPTIONS_HELP
;
102 static void diff_tree_tweak_rev(struct rev_info
*rev
)
104 if (!rev
->diffopt
.output_format
) {
105 if (rev
->dense_combined_merges
)
106 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
108 rev
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
112 int cmd_diff_tree(int argc
, const char **argv
, const char *prefix
)
115 struct object
*tree1
, *tree2
;
116 static struct rev_info
*opt
= &log_tree_opt
;
117 struct setup_revision_opt s_r_opt
;
118 struct userformat_want w
;
122 if (argc
== 2 && !strcmp(argv
[1], "-h"))
123 usage(diff_tree_usage
);
125 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
127 prepare_repo_settings(the_repository
);
128 the_repository
->settings
.command_requires_full_index
= 0;
130 repo_init_revisions(the_repository
, opt
, prefix
);
131 if (repo_read_index(the_repository
) < 0)
132 die(_("index file corrupt"));
135 opt
->disable_stdin
= 1;
136 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
137 s_r_opt
.tweak
= diff_tree_tweak_rev
;
139 prefix
= precompose_argv_prefix(argc
, argv
, prefix
);
140 argc
= setup_revisions(argc
, argv
, opt
, &s_r_opt
);
142 memset(&w
, 0, sizeof(w
));
143 userformat_find_requirements(NULL
, &w
);
145 if (!opt
->show_notes_given
&& w
.notes
)
148 load_display_notes(&opt
->notes_opt
);
151 const char *arg
= *++argv
;
153 if (!strcmp(arg
, "--stdin")) {
157 if (!strcmp(arg
, "--merge-base")) {
161 usage(diff_tree_usage
);
164 if (read_stdin
&& merge_base
)
165 die(_("options '%s' and '%s' cannot be used together"), "--stdin", "--merge-base");
166 if (merge_base
&& opt
->pending
.nr
!= 2)
167 die(_("--merge-base only works with two commits"));
169 opt
->diffopt
.rotate_to_strict
= 1;
172 * 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.
178 switch (opt
->pending
.nr
) {
181 usage(diff_tree_usage
);
184 tree1
= opt
->pending
.objects
[0].item
;
185 diff_tree_commit_oid(&tree1
->oid
);
188 tree1
= opt
->pending
.objects
[0].item
;
189 tree2
= opt
->pending
.objects
[1].item
;
191 struct object_id oid
;
193 diff_get_merge_base(opt
, &oid
);
194 tree1
= lookup_object(the_repository
, &oid
);
195 } else if (tree2
->flags
& UNINTERESTING
) {
198 diff_tree_oid(&tree1
->oid
, &tree2
->oid
, "", &opt
->diffopt
);
199 log_tree_diff_flush(opt
);
207 opt
->diffopt
.rotate_to_strict
= 0;
208 opt
->diffopt
.no_free
= 1;
209 if (opt
->diffopt
.detect_rename
) {
210 if (!the_index
.cache
)
211 repo_read_index(the_repository
);
212 opt
->diffopt
.setup
|= DIFF_SETUP_USE_SIZE_CACHE
;
214 while (fgets(line
, sizeof(line
), stdin
)) {
215 struct object_id oid
;
217 if (get_oid_hex(line
, &oid
)) {
222 diff_tree_stdin(line
);
223 if (saved_nrl
< opt
->diffopt
.needed_rename_limit
)
224 saved_nrl
= opt
->diffopt
.needed_rename_limit
;
225 if (opt
->diffopt
.degraded_cc_to_c
)
229 opt
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
230 opt
->diffopt
.needed_rename_limit
= saved_nrl
;
231 opt
->diffopt
.no_free
= 0;
232 diff_free(&opt
->diffopt
);
235 return diff_result_code(&opt
->diffopt
, 0);