Skip to content

Commit 0af22d6

Browse files
committed
Merge branch 'rs/pop-commit' into maint
Code simplification. * rs/pop-commit: use pop_commit() for consuming the first entry of a struct commit_list
2 parents 8c0a546 + e510ab8 commit 0af22d6

File tree

10 files changed

+31
-92
lines changed

10 files changed

+31
-92
lines changed

builtin/fmt-merge-msg.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
537537
static void find_merge_parents(struct merge_parents *result,
538538
struct strbuf *in, unsigned char *head)
539539
{
540-
struct commit_list *parents, *next;
540+
struct commit_list *parents;
541541
struct commit *head_commit;
542542
int pos = 0, i, j;
543543

@@ -576,13 +576,10 @@ static void find_merge_parents(struct merge_parents *result,
576576
parents = reduce_heads(parents);
577577

578578
while (parents) {
579+
struct commit *cmit = pop_commit(&parents);
579580
for (i = 0; i < result->nr; i++)
580-
if (!hashcmp(result->item[i].commit,
581-
parents->item->object.sha1))
581+
if (!hashcmp(result->item[i].commit, cmit->object.sha1))
582582
result->item[i].used = 1;
583-
next = parents->next;
584-
free(parents);
585-
parents = next;
586583
}
587584

588585
for (i = j = 0; i < result->nr; i++) {

builtin/merge.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
10191019
int *head_subsumed,
10201020
struct commit_list *remoteheads)
10211021
{
1022-
struct commit_list *parents, *next, **remotes = &remoteheads;
1022+
struct commit_list *parents, **remotes;
10231023

10241024
/*
10251025
* Is the current HEAD reachable from another commit being
@@ -1033,16 +1033,14 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
10331033
/* Find what parents to record by checking independent ones. */
10341034
parents = reduce_heads(remoteheads);
10351035

1036-
for (remoteheads = NULL, remotes = &remoteheads;
1037-
parents;
1038-
parents = next) {
1039-
struct commit *commit = parents->item;
1040-
next = parents->next;
1036+
remoteheads = NULL;
1037+
remotes = &remoteheads;
1038+
while (parents) {
1039+
struct commit *commit = pop_commit(&parents);
10411040
if (commit == head_commit)
10421041
*head_subsumed = 0;
10431042
else
10441043
remotes = &commit_list_insert(commit, remotes)->next;
1045-
free(parents);
10461044
}
10471045
return remoteheads;
10481046
}

builtin/reflog.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ static int keep_entry(struct commit **it, unsigned char *sha1)
218218
*/
219219
static void mark_reachable(struct expire_reflog_policy_cb *cb)
220220
{
221-
struct commit *commit;
222221
struct commit_list *pending;
223222
unsigned long expire_limit = cb->mark_limit;
224223
struct commit_list *leftover = NULL;
@@ -228,11 +227,8 @@ static void mark_reachable(struct expire_reflog_policy_cb *cb)
228227

229228
pending = cb->mark_list;
230229
while (pending) {
231-
struct commit_list *entry = pending;
232230
struct commit_list *parent;
233-
pending = entry->next;
234-
commit = entry->item;
235-
free(entry);
231+
struct commit *commit = pop_commit(&pending);
236232
if (commit->object.flags & REACHABLE)
237233
continue;
238234
if (parse_commit(commit))

builtin/rev-parse.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,8 @@ static int try_difference(const char *arg)
281281
b = lookup_commit_reference(end);
282282
exclude = get_merge_bases(a, b);
283283
while (exclude) {
284-
struct commit_list *n = exclude->next;
285-
show_rev(REVERSED,
286-
exclude->item->object.sha1,NULL);
287-
free(exclude);
288-
exclude = n;
284+
struct commit *commit = pop_commit(&exclude);
285+
show_rev(REVERSED, commit->object.sha1, NULL);
289286
}
290287
}
291288
*dotdot = '.';

builtin/show-branch.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ static struct commit *interesting(struct commit_list *list)
5353
return NULL;
5454
}
5555

56-
static struct commit *pop_one_commit(struct commit_list **list_p)
57-
{
58-
struct commit *commit;
59-
struct commit_list *list;
60-
list = *list_p;
61-
commit = list->item;
62-
*list_p = list->next;
63-
free(list);
64-
return commit;
65-
}
66-
6756
struct commit_name {
6857
const char *head_name; /* which head's ancestor? */
6958
int generation; /* how many parents away from head_name */
@@ -213,7 +202,7 @@ static void join_revs(struct commit_list **list_p,
213202
while (*list_p) {
214203
struct commit_list *parents;
215204
int still_interesting = !!interesting(*list_p);
216-
struct commit *commit = pop_one_commit(list_p);
205+
struct commit *commit = pop_commit(list_p);
217206
int flags = commit->object.flags & all_mask;
218207

219208
if (!still_interesting && extra <= 0)
@@ -504,7 +493,7 @@ static int show_merge_base(struct commit_list *seen, int num_rev)
504493
int exit_status = 1;
505494

506495
while (seen) {
507-
struct commit *commit = pop_one_commit(&seen);
496+
struct commit *commit = pop_commit(&seen);
508497
int flags = commit->object.flags & all_mask;
509498
if (!(flags & UNINTERESTING) &&
510499
((flags & all_revs) == all_revs)) {
@@ -927,7 +916,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
927916
all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
928917

929918
while (seen) {
930-
struct commit *commit = pop_one_commit(&seen);
919+
struct commit *commit = pop_commit(&seen);
931920
int this_flag = commit->object.flags;
932921
int is_merge_point = ((this_flag & all_revs) == all_revs);
933922

commit.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,8 @@ struct commit_list *copy_commit_list(struct commit_list *list)
455455

456456
void free_commit_list(struct commit_list *list)
457457
{
458-
while (list) {
459-
struct commit_list *temp = list;
460-
list = temp->next;
461-
free(temp);
462-
}
458+
while (list)
459+
pop_commit(&list);
463460
}
464461

465462
struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
@@ -505,12 +502,8 @@ void commit_list_sort_by_date(struct commit_list **list)
505502
struct commit *pop_most_recent_commit(struct commit_list **list,
506503
unsigned int mark)
507504
{
508-
struct commit *ret = (*list)->item;
505+
struct commit *ret = pop_commit(list);
509506
struct commit_list *parents = ret->parents;
510-
struct commit_list *old = *list;
511-
512-
*list = (*list)->next;
513-
free(old);
514507

515508
while (parents) {
516509
struct commit *commit = parents->item;
@@ -861,11 +854,9 @@ static struct commit_list *merge_bases_many(struct commit *one, int n, struct co
861854
list = paint_down_to_common(one, n, twos);
862855

863856
while (list) {
864-
struct commit_list *next = list->next;
865-
if (!(list->item->object.flags & STALE))
866-
commit_list_insert_by_date(list->item, &result);
867-
free(list);
868-
list = next;
857+
struct commit *commit = pop_commit(&list);
858+
if (!(commit->object.flags & STALE))
859+
commit_list_insert_by_date(commit, &result);
869860
}
870861
return result;
871862
}
@@ -1546,13 +1537,9 @@ int commit_tree_extended(const char *msg, size_t msg_len,
15461537
* if everything else stays the same.
15471538
*/
15481539
while (parents) {
1549-
struct commit_list *next = parents->next;
1550-
struct commit *parent = parents->item;
1551-
1540+
struct commit *parent = pop_commit(&parents);
15521541
strbuf_addf(&buffer, "parent %s\n",
15531542
sha1_to_hex(parent->object.sha1));
1554-
free(parents);
1555-
parents = next;
15561543
}
15571544

15581545
/* Person/date information */

remote.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,10 +1975,8 @@ int resolve_remote_symref(struct ref *ref, struct ref *list)
19751975
static void unmark_and_free(struct commit_list *list, unsigned int mark)
19761976
{
19771977
while (list) {
1978-
struct commit_list *temp = list;
1979-
temp->item->object.flags &= ~mark;
1980-
list = temp->next;
1981-
free(temp);
1978+
struct commit *commit = pop_commit(&list);
1979+
commit->object.flags &= ~mark;
19821980
}
19831981
}
19841982

revision.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ void mark_parents_uninteresting(struct commit *commit)
153153
commit_list_insert(l->item, &parents);
154154

155155
while (parents) {
156-
struct commit *commit = parents->item;
157-
l = parents;
158-
parents = parents->next;
159-
free(l);
156+
struct commit *commit = pop_commit(&parents);
160157

161158
while (commit) {
162159
/*
@@ -1102,14 +1099,10 @@ static int limit_list(struct rev_info *revs)
11021099
}
11031100

11041101
while (list) {
1105-
struct commit_list *entry = list;
1106-
struct commit *commit = list->item;
1102+
struct commit *commit = pop_commit(&list);
11071103
struct object *obj = &commit->object;
11081104
show_early_output_fn_t show;
11091105

1110-
list = list->next;
1111-
free(entry);
1112-
11131106
if (commit == interesting_cache)
11141107
interesting_cache = NULL;
11151108

@@ -2733,10 +2726,7 @@ static void simplify_merges(struct rev_info *revs)
27332726
yet_to_do = NULL;
27342727
tail = &yet_to_do;
27352728
while (list) {
2736-
commit = list->item;
2737-
next = list->next;
2738-
free(list);
2739-
list = next;
2729+
commit = pop_commit(&list);
27402730
tail = simplify_one(revs, commit, tail);
27412731
}
27422732
}
@@ -2748,10 +2738,7 @@ static void simplify_merges(struct rev_info *revs)
27482738
while (list) {
27492739
struct merge_simplify_state *st;
27502740

2751-
commit = list->item;
2752-
next = list->next;
2753-
free(list);
2754-
list = next;
2741+
commit = pop_commit(&list);
27552742
st = locate_simplify_state(revs, commit);
27562743
if (st->simplified == commit)
27572744
tail = &commit_list_insert(commit, tail)->next;
@@ -3125,11 +3112,7 @@ static struct commit *get_revision_1(struct rev_info *revs)
31253112
return NULL;
31263113

31273114
do {
3128-
struct commit_list *entry = revs->commits;
3129-
struct commit *commit = entry->item;
3130-
3131-
revs->commits = entry->next;
3132-
free(entry);
3115+
struct commit *commit = pop_commit(&revs->commits);
31333116

31343117
if (revs->reflog_info) {
31353118
save_parents(revs, commit);

shallow.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,9 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
401401
commit_list_insert(c, &head);
402402
while (head) {
403403
struct commit_list *p;
404-
struct commit *c = head->item;
404+
struct commit *c = pop_commit(&head);
405405
uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
406406

407-
p = head;
408-
head = head->next;
409-
free(p);
410-
411407
/* XXX check "UNINTERESTING" from pack bitmaps if available */
412408
if (c->object.flags & (SEEN | UNINTERESTING))
413409
continue;

upload-pack.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,8 @@ static int reachable(struct commit *want)
316316

317317
commit_list_insert_by_date(want, &work);
318318
while (work) {
319-
struct commit_list *list = work->next;
320-
struct commit *commit = work->item;
321-
free(work);
322-
work = list;
319+
struct commit_list *list;
320+
struct commit *commit = pop_commit(&work);
323321

324322
if (commit->object.flags & THEY_HAVE) {
325323
want->object.flags |= COMMON_KNOWN;

0 commit comments

Comments
 (0)