git-prompt: improve cherry-pick/revert detection

If the user commits or resets a conflict resolution in the middle of a sequence of cherry-picks or reverts then CHERRY_PICK_HEAD/REVERT_HEAD will be removed and so in the absence of those files we need to check .git/sequencer/todo to see if there is a cherry-pick or revert in progress. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jul 1, 2019 at 07:21 UTC e981bf75251ecab712fa70ea2fe995a973ed5dba
2 files changed +51 -6
contrib/completion/git-prompt.sh
+33 -4
@@ -286,6 +286,37 @@ __git_eread ()
286 test -r "$1" && IFS=$'\r\n' read "$2" <"$1"
287 }
288
289 +# see if a cherry-pick or revert is in progress, if the user has committed a
290 +# conflict resolution with 'git commit' in the middle of a sequence of picks or
291 +# reverts then CHERRY_PICK_HEAD/REVERT_HEAD will not exist so we have to read
292 +# the todo file.
293 +__git_sequencer_status ()
294 +{
295 + local todo
296 + if test -f "$g/CHERRY_PICK_HEAD"
297 + then
298 + r="|CHERRY-PICKING"
299 + return 0;
300 + elif test -f "$g/REVERT_HEAD"
301 + then
302 + r="|REVERTING"
303 + return 0;
304 + elif __git_eread "$g/sequencer/todo" todo
305 + then
306 + case "$todo" in
307 + p[\ \ ]|pick[\ \ ]*)
308 + r="|CHERRY-PICKING"
309 + return 0
310 + ;;
311 + revert[\ \ ]*)
312 + r="|REVERTING"
313 + return 0
314 + ;;
315 + esac
316 + fi
317 + return 1
318 +}
319 +
320 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
321 # when called from PS1 using command substitution
322 # in this mode it prints text to add to bash PS1 prompt (includes branch name)
@@ -417,10 +448,8 @@ __git_ps1 ()
448 fi
449 elif [ -f "$g/MERGE_HEAD" ]; then
450 r="|MERGING"
420 - elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
421 - r="|CHERRY-PICKING"
422 - elif [ -f "$g/REVERT_HEAD" ]; then
423 - r="|REVERTING"
451 + elif __git_sequencer_status; then
452 + :
453 elif [ -f "$g/BISECT_LOG" ]; then
454 r="|BISECTING"
455 fi
t/t9903-bash-prompt.sh
+18 -2
@@ -211,8 +211,24 @@ test_expect_success 'prompt - merge' '
211
212 test_expect_success 'prompt - cherry-pick' '
213 printf " (master|CHERRY-PICKING)" >expected &&
214 - test_must_fail git cherry-pick b1 &&
215 - test_when_finished "git reset --hard" &&
214 + test_must_fail git cherry-pick b1 b1^ &&
215 + test_when_finished "git cherry-pick --abort" &&
216 + __git_ps1 >"$actual" &&
217 + test_cmp expected "$actual" &&
218 + git reset --merge &&
219 + test_must_fail git rev-parse CHERRY_PICK_HEAD &&
220 + __git_ps1 >"$actual" &&
221 + test_cmp expected "$actual"
222 +'
223 +
224 +test_expect_success 'prompt - revert' '
225 + printf " (master|REVERTING)" >expected &&
226 + test_must_fail git revert b1^ b1 &&
227 + test_when_finished "git revert --abort" &&
228 + __git_ps1 >"$actual" &&
229 + test_cmp expected "$actual" &&
230 + git reset --merge &&
231 + test_must_fail git rev-parse REVERT_HEAD &&
232 __git_ps1 >"$actual" &&
233 test_cmp expected "$actual"
234 '