All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Sergii Shkarnikov <sergii.shkarnikov@globallogic.com>,
	Eric Sunshine <sunshine@sunshineco.com>,
	Git List <git@vger.kernel.org>
Subject: Re: Possible bug with git restore
Date: Thu, 20 Aug 2020 14:27:20 -0400	[thread overview]
Message-ID: <20200820182720.GA2537643@coredump.intra.peff.net> (raw)
In-Reply-To: <c3f0d51a-d0e3-ed0a-c9ed-da092704da5c@web.de>

On Thu, Aug 20, 2020 at 07:48:48PM +0200, René Scharfe wrote:

> >   - shouldn't that wildcard pathspec match those files? I've confirmed
> >     that the glob characters make it into Git's pathspec machinery, and
> >     since it doesn't have slashes, I think we'd match a basename (and
> >     certainly "git ls-files *test_file.*" does what I expect).
> 
> No, because restore doesn't interpret pathspecs recursively.  I don't
> know why that causes files to disappear, though.  But here's a fix.

I think it's because of this comment from bc96cc87dbb:

  When pathspec.recursive == 0, the behavior depends on match functions:
  non-recursive for tree_entry_interesting() and recursive for
  match_pathspec{,_depth}

So when we read the tree, we don't match recursively, and those entries
don't appear. But then we correlate that with the index:

          /*
           * Make sure all pathspecs participated in locating the paths
           * to be checked out.
           */
          for (pos = 0; pos < active_nr; pos++)
                  if (opts->overlay_mode)
                          mark_ce_for_checkout_overlay(active_cache[pos],
                                                       ps_matched,
                                                       opts);
                  else
                          mark_ce_for_checkout_no_overlay(active_cache[pos],
                                                          ps_matched,
                                                          opts);

And in no-overlay mode (the default for restore), we do:

  
  static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
                                              char *ps_matched,
                                              const struct checkout_opts *opts)
  {
          ce->ce_flags &= ~CE_MATCHED;
          if (!opts->ignore_skipworktree && ce_skip_worktree(ce))
                  return;
          if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched)) {
                  ce->ce_flags |= CE_MATCHED;
                  if (opts->source_tree && !(ce->ce_flags & CE_UPDATE))
                          /*
                           * In overlay mode, but the path is not in
                           * tree-ish, which means we should remove it
                           * from the index and the working tree.
                           */
                          ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE;
          }
  }

And that ce_path_match() _does_ treat the pathspec recursively. So we
say "yes, it matches in the index but wasn't in the tree, and therefore
we must delete it".

So the fundamental issue is treating the pathspec in two different ways,
and then correlating the results. We need to either do a recursive match
for the tree match (as your patch does), or do non-recursive for this
index match (which I don't think is trivial, because of the way the
recursive flag works).

> No sign-off because I don't understand why pathspec recursiveness is a
> thing that can be turned off -- I'd expect pathspec syntax to be
> consistent for all commands.  So there might be a good reason why it was
> not enabled for restore (and switch and checkout).

I think it was originally done this way for compatibility of some
commands as we unified the pathspec code. But I'm having trouble digging
up the exact details.

However, it seems particularly egregious in checkout/restore, because we
may also be using the index as a source, in which case the pathspecs
_would_ be recursive by default. E.g., in the test repo we've been
discussing:

  [make the index and working tree differ]
  $ git reset HEAD^
  Unstaged changes after reset:
  M	incl/test_file.hpp
  M	src/test_file.cpp

  [restore using a wildcard, but out of the index rather than a tree]
  $ git restore -- '*.hpp'

  [and check that we did indeed match]
  $ git status
  On branch master
  Changes not staged for commit:
	modified:   src/test_file.cpp

So I think this inconsistency in pathspec matching between trees and the
index has probably existed in git-checkout for ages (and I guess people
don't do wildcards with trees often enough for anybody to have noticed).
But it didn't cause the index-deletion problem, because that only
appeared more recently with the --no-overlay mode. That's the default
for restore, but you can trigger the problem with checkout, too:

  $ git reset --hard
  $ git checkout --no-overlay HEAD^ '*.hpp'
  Updated 0 paths from 2668463
  $ git status
  On branch master
  Changes to be committed:
	deleted:    incl/test_file.hpp

-Peff

  reply	other threads:[~2020-08-20 18:27 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-12 18:51 Possible bug with git restore Sergii Shkarnikov
2020-08-14 22:41 ` Eric Sunshine
2020-08-20 12:59   ` Sergii Shkarnikov
2020-08-20 13:40     ` Jeff King
2020-08-20 17:48       ` René Scharfe
2020-08-20 18:27         ` Jeff King [this message]
2020-08-22  8:57           ` [PATCH] checkout, restore: make pathspec recursive René Scharfe
2020-08-24 20:21             ` Jeff King
2020-08-22 10:29           ` Possible bug with git restore René Scharfe
2020-08-24 20:25             ` Jeff King
2020-08-22 19:36           ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200820182720.GA2537643@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=sergii.shkarnikov@globallogic.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.