]> Kevux Git Server - rit/commit
checkout: special case error messages during noop switching
authorJunio C Hamano <gitster@pobox.com>
Tue, 2 Jul 2024 20:51:43 +0000 (13:51 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 2 Jul 2024 20:53:56 +0000 (13:53 -0700)
commitd1e6c61272737926cd049a6126431fe9c12e4a54
tree9410a90866796a43b3f6915edab546d8d256686c
parent337b4d400023d22207bcc3c29e9ebab31bf96fc2
checkout: special case error messages during noop switching

"git checkout" ran with no branch and no pathspec behaves like
switching the branch to the current branch (in other words, a
no-op, except that it gives a side-effect "here are the modified
paths" report).  But unlike "git checkout HEAD" or "git checkout
main" (when you are on the 'main' branch), the user is much less
conscious that they are "switching" to the current branch.

This twists end-user expectation in a strange way.  There are
options (like "--ours") that make sense only when we are checking
out paths out of either the tree-ish or out of the index.  So the
error message the command below gives

    $ git checkout --ours
    fatal: '--ours/theirs' cannot be used with switching branches

is technically correct, but because the end-user may not even be
aware of the fact that the command they are issuing is about no-op
branch switching [*], they may find the error confusing.

Let's refactor the code to make it easier to special case the "no-op
branch switching" situation, and then customize the exact error
message for "--ours/--theirs".  Since it is more likely that the
end-user forgot to give pathspec that is required by the option,
let's make it say

    $ git checkout --ours
    fatal: '--ours/theirs' needs the paths to check out

instead.

Among the other options that are incompatible with branch switching,
there may be some that benefit by having messages tweaked when a
no-op branch switching is done, but I'll leave them as #leftoverbits
material.

[Footnote]

 * Yes, the end-users are irrational.  When they did not give
   "--ours", they take it granted that "git checkout" gives a short
   status, e.g..

    $ git checkout
    M builtin/checkout.c
    M t/t7201-co.sh

   exactly as a branch switching command.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
t/t7201-co.sh