Make git-cherry handle root trees
This patch on top of 'next' makes built-in git-cherry handle root commits. It moves the static function log-tree.c::diff_root_tree() to tree-diff.c and makes it more similar to diff_tree_sha1() by shuffling around arguments and factoring out the call to log_tree_diff_flush(). Consequently the name is changed to diff_root_tree_sha1(). It is a version of diff_tree_sha1() that compares the empty tree (= root tree) against a single 'real' tree. This function is then used in get_patch_id() to compute patch IDs for initial commits instead of SEGFAULTing, as the current code does if confronted with parentless commits. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>
Rene Scharfe committed
Oct 26, 2006 at 18:52 UTC
2b60356da5369dd60ab26eabaa91d95b6badf209
4 files changed
+29
-24
builtin-log.c
+5
-2
@@ -171,8 +171,11 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
171
static int get_patch_id(struct commit *commit, struct diff_options *options,
172
unsigned char *sha1)
173
{
174
- diff_tree_sha1(commit->parents->item->object.sha1, commit->object.sha1,
175
- "", options);
174
+ if (commit->parents)
175
+ diff_tree_sha1(commit->parents->item->object.sha1,
176
+ commit->object.sha1, "", options);
177
+ else
178
+ diff_root_tree_sha1(commit->object.sha1, "", options);
179
diffcore_std(options);
180
return diff_flush_patch_id(options, sha1);
181
}
diff.h
+2
@@ -102,6 +102,8 @@ extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
102
const char *base, struct diff_options *opt);
103
extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
104
const char *base, struct diff_options *opt);
105
+extern int diff_root_tree_sha1(const unsigned char *new, const char *base,
106
+ struct diff_options *opt);
107
108
struct combine_diff_path {
109
struct combine_diff_path *next;
log-tree.c
+4
-22
@@ -252,26 +252,6 @@ int log_tree_diff_flush(struct rev_info *opt)
252
return 1;
253
}
254
255
-static int diff_root_tree(struct rev_info *opt,
256
- const unsigned char *new, const char *base)
257
-{
258
- int retval;
259
- void *tree;
260
- struct tree_desc empty, real;
261
-
262
- tree = read_object_with_reference(new, tree_type, &real.size, NULL);
263
- if (!tree)
264
- die("unable to read root tree (%s)", sha1_to_hex(new));
265
- real.buf = tree;
266
-
267
- empty.buf = "";
268
- empty.size = 0;
269
- retval = diff_tree(&empty, &real, base, &opt->diffopt);
270
- free(tree);
271
- log_tree_diff_flush(opt);
272
- return retval;
273
-}
274
-
255
static int do_diff_combined(struct rev_info *opt, struct commit *commit)
256
{
257
unsigned const char *sha1 = commit->object.sha1;
@@ -297,8 +277,10 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
277
/* Root commit? */
278
parents = commit->parents;
279
if (!parents) {
300
- if (opt->show_root_diff)
301
- diff_root_tree(opt, sha1, "");
280
+ if (opt->show_root_diff) {
281
+ diff_root_tree_sha1(sha1, "", &opt->diffopt);
282
+ log_tree_diff_flush(opt);
283
+ }
284
return !opt->loginfo;
285
}
286
tree-diff.c
+18
@@ -215,6 +215,24 @@ int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const cha
215
return retval;
216
}
217
218
+int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
219
+{
220
+ int retval;
221
+ void *tree;
222
+ struct tree_desc empty, real;
223
+
224
+ tree = read_object_with_reference(new, tree_type, &real.size, NULL);
225
+ if (!tree)
226
+ die("unable to read root tree (%s)", sha1_to_hex(new));
227
+ real.buf = tree;
228
+
229
+ empty.size = 0;
230
+ empty.buf = "";
231
+ retval = diff_tree(&empty, &real, base, opt);
232
+ free(tree);
233
+ return retval;
234
+}
235
+
236
static int count_paths(const char **paths)
237
{
238
int i = 0;