status: do not report errors in sequencer/todo

commit 4a72486de9 ("fix cherry-pick/revert status after commit", 2019-04-16) used parse_insn_line() to parse the first line of the todo list to check if it was a pick or revert. However if the todo list is left over from an old cherry-pick or revert and references a commit that no longer exists then parse_insn_line() prints an error message which is confusing for users [1]. Instead parse just the command name so that the user is alerted to the presence of stale sequencer state by status reporting that a cherry-pick or revert is in progress. Note that we should not be leaving stale sequencer state lying around (or at least not as often) after commit b07d9bfd17 ("commit/reset: try to clean up sequencer state", 2019-04-16). However the user may still have stale state that predates that commit. Also avoid printing an error message if for some reason the user has a file called `sequencer` in $GIT_DIR. [1] https://public-inbox.org/git/3bc58c33-4268-4e7c-bf1a-fe349b3cb037@www.fastmail.com/ Reported-by: Espen Antonsen <espen@inspired.no> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jun 27, 2019 at 07:12 UTC ed5b1ca10b2644f1af3bdfa3da32c67f4df1aa46
2 files changed +24 -16
sequencer.c
+8 -16
@@ -2177,34 +2177,26 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
2177
2178 int sequencer_get_last_command(struct repository *r, enum replay_action *action)
2179 {
2180 - struct todo_item item;
2181 - char *eol;
2182 - const char *todo_file;
2180 + const char *todo_file, *bol;
2181 struct strbuf buf = STRBUF_INIT;
2184 - int ret = -1;
2182 + int ret = 0;
2183
2184 todo_file = git_path_todo_file();
2185 if (strbuf_read_file(&buf, todo_file, 0) < 0) {
2188 - if (errno == ENOENT)
2186 + if (errno == ENOENT || errno == ENOTDIR)
2187 return -1;
2188 else
2189 return error_errno("unable to open '%s'", todo_file);
2190 }
2193 - eol = strchrnul(buf.buf, '\n');
2194 - if (buf.buf != eol && eol[-1] == '\r')
2195 - eol--; /* strip Carriage Return */
2196 - if (parse_insn_line(r, &item, buf.buf, buf.buf, eol))
2197 - goto fail;
2198 - if (item.command == TODO_PICK)
2191 + bol = buf.buf + strspn(buf.buf, " \t\r\n");
2192 + if (is_command(TODO_PICK, &bol) && (*bol == ' ' || *bol == '\t'))
2193 *action = REPLAY_PICK;
2200 - else if (item.command == TODO_REVERT)
2194 + else if (is_command(TODO_REVERT, &bol) &&
2195 + (*bol == ' ' || *bol == '\t'))
2196 *action = REPLAY_REVERT;
2197 else
2203 - goto fail;
2204 -
2205 - ret = 0;
2198 + ret = -1;
2199
2207 - fail:
2200 strbuf_release(&buf);
2201
2202 return ret;
t/t7512-status-help.sh
+16
@@ -798,6 +798,22 @@ EOF
798 test_i18ncmp expected actual
799 '
800
801 +test_expect_success 'status shows cherry-pick with invalid oid' '
802 + mkdir .git/sequencer &&
803 + test_write_lines "pick invalid-oid" >.git/sequencer/todo &&
804 + git status --untracked-files=no >actual 2>err &&
805 + git cherry-pick --quit &&
806 + test_must_be_empty err &&
807 + test_i18ncmp expected actual
808 +'
809 +
810 +test_expect_success 'status does not show error if .git/sequencer is a file' '
811 + test_when_finished "rm .git/sequencer" &&
812 + test_write_lines hello >.git/sequencer &&
813 + git status --untracked-files=no 2>err &&
814 + test_must_be_empty err
815 +'
816 +
817 test_expect_success 'status showing detached at and from a tag' '
818 test_commit atag tagging &&
819 git checkout atag &&