Skip to content

Commit 3b041ea

Browse files
committed
Merge branch 'pw/strict-label-lookups'
Correct an error where `git rebase` would mistakenly use a branch or tag named "refs/rewritten/xyz" when missing a rebase label. * pw/strict-label-lookups: sequencer: tighten label lookups sequencer: unify label lookup
2 parents 6adf170 + 688d82f commit 3b041ea

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

sequencer.c

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

3697+
static struct commit *lookup_label(struct repository *r, const char *label,
3698+
int len, struct strbuf *buf)
3699+
{
3700+
struct commit *commit;
3701+
struct object_id oid;
3702+
3703+
strbuf_reset(buf);
3704+
strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
3705+
if (!read_ref(buf->buf, &oid)) {
3706+
commit = lookup_commit_object(r, &oid);
3707+
} else {
3708+
/* fall back to non-rewritten ref or commit */
3709+
strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
3710+
commit = lookup_commit_reference_by_name(buf->buf);
3711+
}
3712+
3713+
if (!commit)
3714+
error(_("could not resolve '%s'"), buf->buf);
3715+
3716+
return commit;
3717+
}
3718+
36973719
static int do_reset(struct repository *r,
36983720
const char *name, int len,
36993721
struct replay_opts *opts)
@@ -3725,19 +3747,20 @@ static int do_reset(struct repository *r,
37253747
oidcpy(&oid, &opts->squash_onto);
37263748
} else {
37273749
int i;
3750+
struct commit *commit;
37283751

37293752
/* Determine the length of the label */
37303753
for (i = 0; i < len; i++)
37313754
if (isspace(name[i]))
37323755
break;
37333756
len = i;
37343757

3735-
strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
3736-
if (get_oid(ref_name.buf, &oid) &&
3737-
get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) {
3738-
ret = error(_("could not read '%s'"), ref_name.buf);
3758+
commit = lookup_label(r, name, len, &ref_name);
3759+
if (!commit) {
3760+
ret = -1;
37393761
goto cleanup;
37403762
}
3763+
oid = commit->object.oid;
37413764
}
37423765

37433766
setup_unpack_trees_porcelain(&unpack_tree_opts, "reset");
@@ -3785,26 +3808,6 @@ static int do_reset(struct repository *r,
37853808
return ret;
37863809
}
37873810

3788-
static struct commit *lookup_label(const char *label, int len,
3789-
struct strbuf *buf)
3790-
{
3791-
struct commit *commit;
3792-
3793-
strbuf_reset(buf);
3794-
strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
3795-
commit = lookup_commit_reference_by_name(buf->buf);
3796-
if (!commit) {
3797-
/* fall back to non-rewritten ref or commit */
3798-
strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
3799-
commit = lookup_commit_reference_by_name(buf->buf);
3800-
}
3801-
3802-
if (!commit)
3803-
error(_("could not resolve '%s'"), buf->buf);
3804-
3805-
return commit;
3806-
}
3807-
38083811
static int do_merge(struct repository *r,
38093812
struct commit *commit,
38103813
const char *arg, int arg_len,
@@ -3852,7 +3855,7 @@ static int do_merge(struct repository *r,
38523855
k = strcspn(p, " \t\n");
38533856
if (!k)
38543857
continue;
3855-
merge_commit = lookup_label(p, k, &ref_name);
3858+
merge_commit = lookup_label(r, p, k, &ref_name);
38563859
if (!merge_commit) {
38573860
ret = error(_("unable to parse '%.*s'"), k, p);
38583861
goto leave_merge;

t/t3430-rebase-merges.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ test_expect_success '`reset` refuses to overwrite untracked files' '
138138
git rebase --abort
139139
'
140140

141+
test_expect_success '`reset` rejects trees' '
142+
test_when_finished "test_might_fail git rebase --abort" &&
143+
test_must_fail env GIT_SEQUENCE_EDITOR="echo reset A^{tree} >" \
144+
git rebase -i B C >out 2>err &&
145+
grep "object .* is a tree" err &&
146+
test_must_be_empty out
147+
'
148+
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+
141158
test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
142159
test_when_finished "test_might_fail git rebase --abort" &&
143160
git checkout -b conflicting-merge A &&

0 commit comments

Comments
 (0)