diff --git a/builtin/fast-export.c b/builtin/fast-export.c index d412c0a8f3..cff8d0fc5b 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -634,11 +634,10 @@ static void handle_tail(struct object_array *commits, struct rev_info *revs) { struct commit *commit; while (commits->nr) { - commit = (struct commit *)commits->objects[commits->nr - 1].item; + commit = (struct commit *)object_array_pop(commits); if (has_unshown_parent(commit)) return; handle_commit(commit, revs); - commits->nr--; } } diff --git a/builtin/fsck.c b/builtin/fsck.c index d18244ab54..7d4ad02733 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -181,12 +181,7 @@ static int traverse_reachable(void) if (show_progress) progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2); while (pending.nr) { - struct object_array_entry *entry; - struct object *obj; - - entry = pending.objects + --pending.nr; - obj = entry->item; - result |= traverse_one_object(obj); + result |= traverse_one_object(object_array_pop(&pending)); display_progress(progress, ++nr); } stop_progress(&progress); diff --git a/builtin/reflog.c b/builtin/reflog.c index 6b34f23e78..2067cca5b1 100644 --- a/builtin/reflog.c +++ b/builtin/reflog.c @@ -126,7 +126,7 @@ static int commit_is_complete(struct commit *commit) struct commit *c; struct commit_list *parent; - c = (struct commit *)study.objects[--study.nr].item; + c = (struct commit *)object_array_pop(&study); if (!c->object.parsed && !parse_object(&c->object.oid)) c->object.flags |= INCOMPLETE; diff --git a/object.c b/object.c index 321d7e9201..b9a4a0e501 100644 --- a/object.c +++ b/object.c @@ -353,6 +353,19 @@ static void object_array_release_entry(struct object_array_entry *ent) free(ent->path); } +struct object *object_array_pop(struct object_array *array) +{ + struct object *ret; + + if (!array->nr) + return NULL; + + ret = array->objects[array->nr - 1].item; + object_array_release_entry(&array->objects[array->nr - 1]); + array->nr--; + return ret; +} + void object_array_filter(struct object_array *array, object_array_each_func_t want, void *cb_data) { diff --git a/object.h b/object.h index 0a419ba8da..df8abe96f7 100644 --- a/object.h +++ b/object.h @@ -116,6 +116,14 @@ int object_list_contains(struct object_list *list, struct object *obj); void add_object_array(struct object *obj, const char *name, struct object_array *array); void add_object_array_with_path(struct object *obj, const char *name, struct object_array *array, unsigned mode, const char *path); +/* + * Returns NULL if the array is empty. Otherwise, returns the last object + * after removing its entry from the array. Other resources associated + * with that object are left in an unspecified state and should not be + * examined. + */ +struct object *object_array_pop(struct object_array *array); + typedef int (*object_array_each_func_t)(struct object_array_entry *, void *); /* diff --git a/shallow.c b/shallow.c index 54359d5490..901ac97917 100644 --- a/shallow.c +++ b/shallow.c @@ -99,7 +99,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth, cur_depth = 0; } else { commit = (struct commit *) - stack.objects[--stack.nr].item; + object_array_pop(&stack); cur_depth = *(int *)commit->util; } }