Skip to content

Commit 6bc1a23

Browse files
artagnongitster
authored andcommitted
revert: make commit subjects in insn sheet optional
Change the instruction sheet format subtly so that the subject of the commit message that follows the object name is optional. As a result, an instruction sheet like this is now perfectly valid: pick 35b0426 pick fbd5bbcbc2e pick 7362160f While at it, also fix a bug introduced by 5a5d80f (revert: Introduce --continue to continue the operation, 2011-08-04) that failed to read lines that are too long to fit on the commit-id-shaped buffer we currently use; eliminate the need for the buffer altogether. In addition to literal SHA-1 hexes, you can now safely use expressions like the following in the instruction sheet: featurebranch~4 rr/revert-cherry-pick-continue^2~12@{12 days ago} [jc: simplify parsing] Suggested-by: Jonathan Nieder <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf3de2b commit 6bc1a23

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

builtin/revert.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -714,31 +714,27 @@ static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
714714
return 0;
715715
}
716716

717-
static struct commit *parse_insn_line(char *start, struct replay_opts *opts)
717+
static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts)
718718
{
719719
unsigned char commit_sha1[20];
720-
char sha1_abbrev[40];
721720
enum replay_action action;
722-
int insn_len = 0;
723-
char *p, *q;
721+
char *end_of_object_name;
722+
int saved, status;
724723

725-
if (!prefixcmp(start, "pick ")) {
724+
if (!prefixcmp(bol, "pick ")) {
726725
action = CHERRY_PICK;
727-
insn_len = strlen("pick");
728-
p = start + insn_len + 1;
729-
} else if (!prefixcmp(start, "revert ")) {
726+
bol += strlen("pick ");
727+
} else if (!prefixcmp(bol, "revert ")) {
730728
action = REVERT;
731-
insn_len = strlen("revert");
732-
p = start + insn_len + 1;
729+
bol += strlen("revert ");
733730
} else
734731
return NULL;
735732

736-
q = strchr(p, ' ');
737-
if (!q)
738-
return NULL;
739-
q++;
740-
741-
strlcpy(sha1_abbrev, p, q - p);
733+
end_of_object_name = bol + strcspn(bol, " \n");
734+
saved = *end_of_object_name;
735+
*end_of_object_name = '\0';
736+
status = get_sha1(bol, commit_sha1);
737+
*end_of_object_name = saved;
742738

743739
/*
744740
* Verify that the action matches up with the one in
@@ -751,7 +747,7 @@ static struct commit *parse_insn_line(char *start, struct replay_opts *opts)
751747
return NULL;
752748
}
753749

754-
if (get_sha1(sha1_abbrev, commit_sha1) < 0)
750+
if (status < 0)
755751
return NULL;
756752

757753
return lookup_commit_reference(commit_sha1);
@@ -766,13 +762,12 @@ static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
766762
int i;
767763

768764
for (i = 1; *p; i++) {
769-
commit = parse_insn_line(p, opts);
765+
char *eol = strchrnul(p, '\n');
766+
commit = parse_insn_line(p, eol, opts);
770767
if (!commit)
771768
return error(_("Could not parse line %d."), i);
772769
next = commit_list_append(commit, next);
773-
p = strchrnul(p, '\n');
774-
if (*p)
775-
p++;
770+
p = *eol ? eol + 1 : eol;
776771
}
777772
if (!*todo_list)
778773
return error(_("No commits parsed."));

t/t3510-cherry-pick-sequence.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ test_description='Test cherry-pick continuation features
1414

1515
. ./test-lib.sh
1616

17+
# Repeat first match 10 times
18+
_r10='\1\1\1\1\1\1\1\1\1\1'
19+
1720
pristine_detach () {
1821
git cherry-pick --quit &&
1922
git checkout -f "$1^0" &&
@@ -478,4 +481,29 @@ test_expect_success 'empty commit set' '
478481
test_expect_code 128 git cherry-pick base..base
479482
'
480483

484+
test_expect_success 'malformed instruction sheet 3' '
485+
pristine_detach initial &&
486+
test_must_fail git cherry-pick base..anotherpick &&
487+
echo "resolved" >foo &&
488+
git add foo &&
489+
git commit &&
490+
sed "s/pick \([0-9a-f]*\)/pick $_r10/" .git/sequencer/todo >new_sheet &&
491+
cp new_sheet .git/sequencer/todo &&
492+
test_must_fail git cherry-pick --continue
493+
'
494+
495+
test_expect_success 'commit descriptions in insn sheet are optional' '
496+
pristine_detach initial &&
497+
test_must_fail git cherry-pick base..anotherpick &&
498+
echo "c" >foo &&
499+
git add foo &&
500+
git commit &&
501+
cut -d" " -f1,2 .git/sequencer/todo >new_sheet &&
502+
cp new_sheet .git/sequencer/todo &&
503+
git cherry-pick --continue &&
504+
test_path_is_missing .git/sequencer &&
505+
git rev-list HEAD >commits &&
506+
test_line_count = 4 commits
507+
'
508+
481509
test_done

0 commit comments

Comments
 (0)