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 <gitster@pobox.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Phillip Wood committed
Oct 17, 2022 at 13:17 UTC
b8dbfd030ccc8e5be6b22bc28a342119504e0c2b
3 files changed
+25
-4
builtin/rebase.c
+4
-4
@@ -429,9 +429,9 @@ static int read_basic_state(struct rebase_options *opts)
429
opts->head_name = starts_with(head_name.buf, "refs/") ?
430
xstrdup(head_name.buf) : NULL;
431
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);
435
436
/*
437
* 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)
446
} else if (!read_oneliner(&buf, state_dir_path("head", opts),
447
READ_ONELINER_WARN_MISSING))
448
return -1;
449
- if (get_oid(buf.buf, &opts->orig_head))
449
+ if (get_oid_hex(buf.buf, &opts->orig_head))
450
return error(_("invalid orig-head: '%s'"), buf.buf);
451
452
if (file_exists(state_dir_path("quiet", opts)))
commit.c
+8
@@ -59,6 +59,14 @@ struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref
59
return c;
60
}
61
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
+
70
struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
71
{
72
struct object *obj = lookup_object(r, oid);
commit.h
+13
@@ -64,6 +64,19 @@ enum decoration_type {
64
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj);
65
const struct name_decoration *get_name_decoration(const struct object *obj);
66
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
+ */
80
struct commit *lookup_commit(struct repository *r, const struct object_id *oid);
81
struct commit *lookup_commit_reference(struct repository *r,
82
const struct object_id *oid);