Skip to content

Commit 6979bf6

Browse files
peffgitster
authored andcommitted
tree-diff: make list tail-passing more explicit
The ll_diff_tree_paths() function and its helpers all take a pointer to a list tail, possibly add to it, and then return the new tail. This works but has two downsides: - The top-level caller (diff_tree_paths() in this case) has to make a fake combine_diff_path struct to act as the list head. This is especially weird here, as it's a flexible-sized struct which will have an empty FLEX_ARRAY field. That used to be a portability problem, though these days it is legal because our FLEX_ARRAY macro over-allocates if necessary. It's still kind of ugly, though. - Besides the name "tail", it's not immediately obvious that the entry we pass around will not be examined by each function. Using a pointer-to-pointer or similar makes it more obvious we only care about the pointer itself, not its contents. We can solve both by passing around a pointer to the tail instead. That gets rid of the return value entirely, though note that because of the recursion we actually need a three-star pointer for this to work. The result is fairly readable, as we only need to dereference the tail in one spot. If we wanted to make it simpler we could wrap the tail in a struct, which we pass around. Another option is to convert combine_diff to use our generic list_head API. I tried that and found the result became much harder to read overall. It means that _all_ code that looks at combine_diff_path structs needs to be modified, since the "next" pointer is now inside a list_head which has to be dereferenced with list_entry(). And we lose some type safety, since we're just passing around a list_head struct everywhere, and everybody who looks at it has to specify the type to list_entry themselves. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6632bcb commit 6979bf6

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

tree-diff.c

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
free((x)); \
4949
} while(0)
5050

51-
static struct combine_diff_path *ll_diff_tree_paths(
52-
struct combine_diff_path *tail, const struct object_id *oid,
51+
static void ll_diff_tree_paths(
52+
struct combine_diff_path ***tail, const struct object_id *oid,
5353
const struct object_id **parents_oid, int nparent,
5454
struct strbuf *base, struct diff_options *opt,
5555
int depth);
@@ -134,10 +134,10 @@ static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_
134134
* t, tp -> path modified/added
135135
* (M for tp[i]=tp[imin], A otherwise)
136136
*/
137-
static struct combine_diff_path *emit_path(struct combine_diff_path *tail,
138-
struct strbuf *base, struct diff_options *opt, int nparent,
139-
struct tree_desc *t, struct tree_desc *tp,
140-
int imin, int depth)
137+
static void emit_path(struct combine_diff_path ***tail,
138+
struct strbuf *base, struct diff_options *opt,
139+
int nparent, struct tree_desc *t, struct tree_desc *tp,
140+
int imin, int depth)
141141
{
142142
unsigned short mode;
143143
const char *path;
@@ -219,8 +219,8 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *tail,
219219
keep = opt->pathchange(opt, p);
220220

221221
if (keep) {
222-
tail->next = p;
223-
tail = p;
222+
**tail = p;
223+
*tail = &p->next;
224224
} else {
225225
free(p);
226226
}
@@ -239,13 +239,12 @@ static struct combine_diff_path *emit_path(struct combine_diff_path *tail,
239239

240240
strbuf_add(base, path, pathlen);
241241
strbuf_addch(base, '/');
242-
tail = ll_diff_tree_paths(tail, oid, parents_oid, nparent, base, opt,
243-
depth + 1);
242+
ll_diff_tree_paths(tail, oid, parents_oid, nparent, base, opt,
243+
depth + 1);
244244
FAST_ARRAY_FREE(parents_oid, nparent);
245245
}
246246

247247
strbuf_setlen(base, old_baselen);
248-
return tail;
249248
}
250249

251250
static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
@@ -358,8 +357,8 @@ static inline void update_tp_entries(struct tree_desc *tp, int nparent)
358357
update_tree_entry(&tp[i]);
359358
}
360359

361-
static struct combine_diff_path *ll_diff_tree_paths(
362-
struct combine_diff_path *tail, const struct object_id *oid,
360+
static void ll_diff_tree_paths(
361+
struct combine_diff_path ***tail, const struct object_id *oid,
363362
const struct object_id **parents_oid, int nparent,
364363
struct strbuf *base, struct diff_options *opt,
365364
int depth)
@@ -463,8 +462,8 @@ static struct combine_diff_path *ll_diff_tree_paths(
463462
}
464463

465464
/* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */
466-
tail = emit_path(tail, base, opt, nparent,
467-
&t, tp, imin, depth);
465+
emit_path(tail, base, opt, nparent,
466+
&t, tp, imin, depth);
468467

469468
skip_emit_t_tp:
470469
/* t↓, ∀ pi=p[imin] pi↓ */
@@ -475,8 +474,8 @@ static struct combine_diff_path *ll_diff_tree_paths(
475474
/* t < p[imin] */
476475
else if (cmp < 0) {
477476
/* D += "+t" */
478-
tail = emit_path(tail, base, opt, nparent,
479-
&t, /*tp=*/NULL, -1, depth);
477+
emit_path(tail, base, opt, nparent,
478+
&t, /*tp=*/NULL, -1, depth);
480479

481480
/* t↓ */
482481
update_tree_entry(&t);
@@ -491,8 +490,8 @@ static struct combine_diff_path *ll_diff_tree_paths(
491490
goto skip_emit_tp;
492491
}
493492

494-
tail = emit_path(tail, base, opt, nparent,
495-
/*t=*/NULL, tp, imin, depth);
493+
emit_path(tail, base, opt, nparent,
494+
/*t=*/NULL, tp, imin, depth);
496495

497496
skip_emit_tp:
498497
/* ∀ pi=p[imin] pi↓ */
@@ -505,20 +504,16 @@ static struct combine_diff_path *ll_diff_tree_paths(
505504
free(tptree[i]);
506505
FAST_ARRAY_FREE(tptree, nparent);
507506
FAST_ARRAY_FREE(tp, nparent);
508-
509-
return tail;
510507
}
511508

512509
struct combine_diff_path *diff_tree_paths(
513510
const struct object_id *oid,
514511
const struct object_id **parents_oid, int nparent,
515512
struct strbuf *base, struct diff_options *opt)
516513
{
517-
struct combine_diff_path head, *p;
518-
/* fake list head, so worker can assume it is non-NULL */
519-
head.next = NULL;
520-
p = ll_diff_tree_paths(&head, oid, parents_oid, nparent, base, opt, 0);
521-
return p;
514+
struct combine_diff_path *head = NULL, **tail = &head;
515+
ll_diff_tree_paths(&tail, oid, parents_oid, nparent, base, opt, 0);
516+
return head;
522517
}
523518

524519
/*

0 commit comments

Comments
 (0)