8 static struct rev_info log_tree_opt
;
10 static int diff_tree_commit_sha1(const unsigned char *sha1
)
12 struct commit
*commit
= lookup_commit_reference(sha1
);
15 return log_tree_commit(&log_tree_opt
, commit
);
18 /* Diff one or more commits. */
19 static int stdin_diff_commit(struct commit
*commit
, char *line
, int len
)
21 unsigned char sha1
[20];
22 if (isspace(line
[40]) && !get_sha1_hex(line
+41, sha1
)) {
23 /* Graft the fake parents locally to the commit */
25 struct commit_list
**pptr
, *parents
;
27 /* Free the real parent list */
28 for (parents
= commit
->parents
; parents
; ) {
29 struct commit_list
*tmp
= parents
->next
;
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
);
38 pptr
= &commit_list_insert(parent
, pptr
)->next
;
43 return log_tree_commit(&log_tree_opt
, commit
);
47 static int stdin_diff_trees(struct tree
*tree1
, char *line
, int len
)
49 unsigned char sha1
[20];
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
))
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
);
64 static int diff_tree_stdin(char *line
)
66 int len
= strlen(line
);
67 unsigned char sha1
[20];
70 if (!len
|| line
[len
-1] != '\n')
73 if (get_sha1_hex(line
, sha1
))
75 obj
= parse_object(sha1
);
78 if (obj
->type
== OBJ_COMMIT
)
79 return stdin_diff_commit((struct commit
*)obj
, line
, len
);
80 if (obj
->type
== OBJ_TREE
)
81 return stdin_diff_trees((struct tree
*)obj
, line
, len
);
82 error("Object %s is a %s, not a commit or tree",
83 sha1_to_hex(sha1
), typename(obj
->type
));
87 static const char diff_tree_usage
[] =
88 "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
89 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
90 " -r diff recursively\n"
91 " --root include the initial commit as diff against /dev/null\n"
92 COMMON_DIFF_OPTIONS_HELP
;
94 static void diff_tree_tweak_rev(struct rev_info
*rev
, struct setup_revision_opt
*opt
)
96 if (!rev
->diffopt
.output_format
) {
97 if (rev
->dense_combined_merges
)
98 rev
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
100 rev
->diffopt
.output_format
= DIFF_FORMAT_RAW
;
104 int cmd_diff_tree(int argc
, const char **argv
, const char *prefix
)
108 struct object
*tree1
, *tree2
;
109 static struct rev_info
*opt
= &log_tree_opt
;
110 struct setup_revision_opt s_r_opt
;
113 init_revisions(opt
, prefix
);
115 git_config(git_diff_basic_config
, NULL
); /* no "diff" UI options */
118 opt
->disable_stdin
= 1;
119 memset(&s_r_opt
, 0, sizeof(s_r_opt
));
120 s_r_opt
.tweak
= diff_tree_tweak_rev
;
121 argc
= setup_revisions(argc
, argv
, opt
, &s_r_opt
);
124 const char *arg
= *++argv
;
126 if (!strcmp(arg
, "--stdin")) {
130 usage(diff_tree_usage
);
134 * NOTE! We expect "a ^b" to be equal to "a..b", so we
135 * reverse the order of the objects if the second one
136 * is marked UNINTERESTING.
138 nr_sha1
= opt
->pending
.nr
;
142 usage(diff_tree_usage
);
145 tree1
= opt
->pending
.objects
[0].item
;
146 diff_tree_commit_sha1(tree1
->sha1
);
149 tree1
= opt
->pending
.objects
[0].item
;
150 tree2
= opt
->pending
.objects
[1].item
;
151 if (tree2
->flags
& UNINTERESTING
) {
152 struct object
*tmp
= tree2
;
156 diff_tree_sha1(tree1
->sha1
,
159 log_tree_diff_flush(opt
);
167 if (opt
->diffopt
.detect_rename
)
168 opt
->diffopt
.setup
|= (DIFF_SETUP_USE_SIZE_CACHE
|
169 DIFF_SETUP_USE_CACHE
);
170 while (fgets(line
, sizeof(line
), stdin
)) {
171 unsigned char sha1
[20];
173 if (get_sha1_hex(line
, sha1
)) {
178 diff_tree_stdin(line
);
179 if (saved_nrl
< opt
->diffopt
.needed_rename_limit
)
180 saved_nrl
= opt
->diffopt
.needed_rename_limit
;
181 if (opt
->diffopt
.degraded_cc_to_c
)
185 opt
->diffopt
.degraded_cc_to_c
= saved_dcctc
;
186 opt
->diffopt
.needed_rename_limit
= saved_nrl
;
189 return diff_result_code(&opt
->diffopt
, 0);