Skip to content

Commit b8dbfd0

Browse files
phillipwoodgitster
authored andcommitted
rebase: be stricter when reading state files containing oids
The state files for 'onto' and 'orig_head' should contain a full hex oid, change the reading functions from get_oid() to get_oid_hex() to reflect this. They should also name commits and not tags so add and use a function that looks up a commit from an oid like lookup_commit_reference() but without dereferencing tags. Suggested-by: Junio C Hamano <[email protected]> Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 05ec418 commit b8dbfd0

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

builtin/rebase.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,9 @@ static int read_basic_state(struct rebase_options *opts)
429429
opts->head_name = starts_with(head_name.buf, "refs/") ?
430430
xstrdup(head_name.buf) : NULL;
431431
strbuf_release(&head_name);
432-
if (get_oid(buf.buf, &oid))
433-
return error(_("could not get 'onto': '%s'"), buf.buf);
434-
opts->onto = lookup_commit_or_die(&oid, buf.buf);
432+
if (get_oid_hex(buf.buf, &oid) ||
433+
!(opts->onto = lookup_commit_object(the_repository, &oid)))
434+
return error(_("invalid onto: '%s'"), buf.buf);
435435

436436
/*
437437
* We always write to orig-head, but interactive rebase used to write to
@@ -446,7 +446,7 @@ static int read_basic_state(struct rebase_options *opts)
446446
} else if (!read_oneliner(&buf, state_dir_path("head", opts),
447447
READ_ONELINER_WARN_MISSING))
448448
return -1;
449-
if (get_oid(buf.buf, &opts->orig_head))
449+
if (get_oid_hex(buf.buf, &opts->orig_head))
450450
return error(_("invalid orig-head: '%s'"), buf.buf);
451451

452452
if (file_exists(state_dir_path("quiet", opts)))

commit.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref
5959
return c;
6060
}
6161

62+
struct commit *lookup_commit_object(struct repository *r,
63+
const struct object_id *oid)
64+
{
65+
struct object *obj = parse_object(r, oid);
66+
return obj ? object_as_type(obj, OBJ_COMMIT, 0) : NULL;
67+
68+
}
69+
6270
struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
6371
{
6472
struct object *obj = lookup_object(r, oid);

commit.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ enum decoration_type {
6464
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj);
6565
const struct name_decoration *get_name_decoration(const struct object *obj);
6666

67+
/*
68+
* Look up commit named by "oid" respecting replacement objects.
69+
* Returns NULL if "oid" is not a commit or does not exist.
70+
*/
71+
struct commit *lookup_commit_object(struct repository *r, const struct object_id *oid);
72+
73+
/*
74+
* Look up commit named by "oid" without replacement objects or
75+
* checking for object existence. Returns the requested commit if it
76+
* is found in the object cache, NULL if "oid" is in the object cache
77+
* but is not a commit and a newly allocated unparsed commit object if
78+
* "oid" is not in the object cache.
79+
*/
6780
struct commit *lookup_commit(struct repository *r, const struct object_id *oid);
6881
struct commit *lookup_commit_reference(struct repository *r,
6982
const struct object_id *oid);

0 commit comments

Comments
 (0)