Skip to content

Commit 688d82f

Browse files
phillipwoodttaylorr
authored andcommitted
sequencer: tighten label lookups
The `label` command creates a ref refs/rewritten/<label> that the `reset` and `merge` commands resolve by calling lookup_label(). That uses lookup_commit_reference_by_name() to look up the label ref. As lookup_commit_reference_by_name() uses the dwim rules when looking up the label it will look for a branch named refs/heads/refs/rewritten/<label> and return that instead of an error if the branch exists and the label does not. Fix this by using read_ref() followed by lookup_commit_object() when looking up labels. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 82766b2 commit 688d82f

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

sequencer.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3694,15 +3694,17 @@ static const char *reflog_message(struct replay_opts *opts,
36943694
return buf.buf;
36953695
}
36963696

3697-
static struct commit *lookup_label(const char *label, int len,
3698-
struct strbuf *buf)
3697+
static struct commit *lookup_label(struct repository *r, const char *label,
3698+
int len, struct strbuf *buf)
36993699
{
37003700
struct commit *commit;
3701+
struct object_id oid;
37013702

37023703
strbuf_reset(buf);
37033704
strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
3704-
commit = lookup_commit_reference_by_name(buf->buf);
3705-
if (!commit) {
3705+
if (!read_ref(buf->buf, &oid)) {
3706+
commit = lookup_commit_object(r, &oid);
3707+
} else {
37063708
/* fall back to non-rewritten ref or commit */
37073709
strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
37083710
commit = lookup_commit_reference_by_name(buf->buf);
@@ -3753,7 +3755,7 @@ static int do_reset(struct repository *r,
37533755
break;
37543756
len = i;
37553757

3756-
commit = lookup_label(name, len, &ref_name);
3758+
commit = lookup_label(r, name, len, &ref_name);
37573759
if (!commit) {
37583760
ret = -1;
37593761
goto cleanup;
@@ -3852,7 +3854,7 @@ static int do_merge(struct repository *r,
38523854
k = strcspn(p, " \t\n");
38533855
if (!k)
38543856
continue;
3855-
merge_commit = lookup_label(p, k, &ref_name);
3857+
merge_commit = lookup_label(r, p, k, &ref_name);
38563858
if (!merge_commit) {
38573859
ret = error(_("unable to parse '%.*s'"), k, p);
38583860
goto leave_merge;

t/t3430-rebase-merges.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ test_expect_success '`reset` rejects trees' '
146146
test_must_be_empty out
147147
'
148148

149+
test_expect_success '`reset` only looks for labels under refs/rewritten/' '
150+
test_when_finished "test_might_fail git rebase --abort" &&
151+
git branch refs/rewritten/my-label A &&
152+
test_must_fail env GIT_SEQUENCE_EDITOR="echo reset my-label >" \
153+
git rebase -i B C >out 2>err &&
154+
grep "could not resolve ${SQ}my-label${SQ}" err &&
155+
test_must_be_empty out
156+
'
157+
149158
test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
150159
test_when_finished "test_might_fail git rebase --abort" &&
151160
git checkout -b conflicting-merge A &&

0 commit comments

Comments
 (0)