git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* 'git reset -- NonExistingFile' succeeds
@ 2019-10-30 16:29 Alexandr Miloslavskiy
  2019-11-02  4:49 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Alexandr Miloslavskiy @ 2019-10-30 16:29 UTC (permalink / raw)
  To: git

Is that expected?

Other commands seem to not like that:

$ git checkout NonExistingFile
error: pathspec 'NonExistingFile' did not match any file(s) known to git

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: 'git reset -- NonExistingFile' succeeds
  2019-10-30 16:29 'git reset -- NonExistingFile' succeeds Alexandr Miloslavskiy
@ 2019-11-02  4:49 ` Junio C Hamano
  2019-11-04 10:24   ` Alexandr Miloslavskiy
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2019-11-02  4:49 UTC (permalink / raw)
  To: Alexandr Miloslavskiy; +Cc: git

Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> writes:

> Is that expected?
>
> Other commands seem to not like that:
>
> $ git checkout NonExistingFile
> error: pathspec 'NonExistingFile' did not match any file(s) known to git

It's sort of expected ;-)

"git $subcmd -- pathspec" is defined to perform $subcmd to the paths
that match the given pathspec.  "git checkout -- pathspec" checks
out the contents out of the index for such paths and "git reset --
pathspec" resets the contents in the index to that of HEAD for such
paths.

Historically, we did not consider it is an error for users to
specify an empty set (i.e. a pathspec with an element that does not
match any path).  Because it is easy to mistype "git checkout --
*.rv", let the shell pass "*.rv" literally (because in a project
where you meant to say "*.rb", it is very plausible that there is no
such path for the glob to match) to "git checkout" and nobody would
give you any indication of an error, we made it an error for "git
checkout" to be fed a pathspec element that does not match [*1*].

It is expected that other commands that may benefit from a similar
handholding mechanism against end-user typos have not learned to do
so, simply because nobody bothered to bring it up ;-).



[Footnote]

*1* This cuts both ways and it used to hurt a lot more back when it
was introduced.

When you have many tracked *.rb files and some throwaway *.rb files
that are not tracked,

    git checkout -- \*.rb

may checkout only the ones that are tracked (i.e. known to git---in
this case, known to the index) without molesting the untracked ones.
But people being people, they can be lazy and say

    git checkout -- *.rb

instead (notice the lack of quoting for the asterisk).  This would
first let your shell expand it and match untracked ones, and then
feed the resulting paths as a pathspec to "git checkout", so one of
the pathspec elements may be the "untracked.rb" throw-away file.
With the "it is an error for a pathspec element not to match any"
rule, such a use will now error out.

The good thing is that people can learn, and after getting such an
error message, they learn to quote the asterisk from the shell when
they want to Git expand it.  So it may not hurt that much these
days as it used to.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: 'git reset -- NonExistingFile' succeeds
  2019-11-02  4:49 ` Junio C Hamano
@ 2019-11-04 10:24   ` Alexandr Miloslavskiy
  2019-11-06  1:51     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Alexandr Miloslavskiy @ 2019-11-04 10:24 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On 02.11.2019 5:49, Junio C Hamano wrote:
> It's sort of expected ;-)

Didn't expect such a detailed reply, thanks!

Not really a problem for me, just I was surprised and thought that I'd 
ask about it.

I came across this issue when writing a test for 'git reset'. I'll 
simply change the test to verify the repository state instead of 
expecting 'git reset' to fail.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: 'git reset -- NonExistingFile' succeeds
  2019-11-04 10:24   ` Alexandr Miloslavskiy
@ 2019-11-06  1:51     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2019-11-06  1:51 UTC (permalink / raw)
  To: Alexandr Miloslavskiy; +Cc: git

Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> writes:

> On 02.11.2019 5:49, Junio C Hamano wrote:
>> It's sort of expected ;-)
>
> Didn't expect such a detailed reply, thanks!
>
> Not really a problem for me, just I was surprised and thought that I'd
> ask about it.
>
> I came across this issue when writing a test for 'git reset'. I'll
> simply change the test to verify the repository state instead of
> expecting 'git reset' to fail.

As I said, it is expected that Git is not perfect and there would be
many little corners like this that can use improvements.  IOW, it is
not unexpected that "git reset -- NoSuchFile" does not raise an
error, but that does not make it a bad idea to at least think about
teaching it to do so.  There _might_ be fallouts, though; there may
be scripts by people that rely on 'git reset -- "$variable"' with
some random pathspec in a $variable to quietly become no-op when no
paths match (in which case, we'd have to mimic ls-files and add a
"--error-unmatch" option, perhaps).



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-11-06  1:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-30 16:29 'git reset -- NonExistingFile' succeeds Alexandr Miloslavskiy
2019-11-02  4:49 ` Junio C Hamano
2019-11-04 10:24   ` Alexandr Miloslavskiy
2019-11-06  1:51     ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).