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 5a5d80f4 (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 <jrnieder@gmail.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Dec 14, 2011 at 22:24 UTC 6bc1a235b1260a8395045261f8004ba9f12677c7
2 files changed +44 -21
builtin/revert.c
+16 -21
@@ -714,31 +714,27 @@ static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
714 return 0;
715 }
716
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)
718 {
719 unsigned char commit_sha1[20];
720 - char sha1_abbrev[40];
720 enum replay_action action;
722 - int insn_len = 0;
723 - char *p, *q;
721 + char *end_of_object_name;
722 + int saved, status;
723
725 - if (!prefixcmp(start, "pick ")) {
724 + if (!prefixcmp(bol, "pick ")) {
725 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 ")) {
728 action = REVERT;
731 - insn_len = strlen("revert");
732 - p = start + insn_len + 1;
729 + bol += strlen("revert ");
730 } else
731 return NULL;
732
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;
738
739 /*
740 * 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)
747 return NULL;
748 }
749
754 - if (get_sha1(sha1_abbrev, commit_sha1) < 0)
750 + if (status < 0)
751 return NULL;
752
753 return lookup_commit_reference(commit_sha1);
@@ -766,13 +762,12 @@ static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
762 int i;
763
764 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);
767 if (!commit)
768 return error(_("Could not parse line %d."), i);
769 next = commit_list_append(commit, next);
773 - p = strchrnul(p, '\n');
774 - if (*p)
775 - p++;
770 + p = *eol ? eol + 1 : eol;
771 }
772 if (!*todo_list)
773 return error(_("No commits parsed."));
t/t3510-cherry-pick-sequence.sh
+28
@@ -14,6 +14,9 @@ test_description='Test cherry-pick continuation features
14
15 . ./test-lib.sh
16
17 +# Repeat first match 10 times
18 +_r10='\1\1\1\1\1\1\1\1\1\1'
19 +
20 pristine_detach () {
21 git cherry-pick --quit &&
22 git checkout -f "$1^0" &&
@@ -478,4 +481,29 @@ test_expect_success 'empty commit set' '
481 test_expect_code 128 git cherry-pick base..base
482 '
483
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 +
509 test_done