threads / rfc / 28531

RFC patchgit checkout $tree path

Subject: [RFC/PATCH] git checkout $tree path

## tl;dr

8 messages between Sep 29, 2011 and Oct 5, 2011. Diffs are folded; open one to read it.

replies: 7people: 4as markdown or json

Junio C Hamano· Sep 29, 2011, 22:46 UTC · lore

Suppose you have two branches, "master" with dir/file1 and file2, and "next" with dir/file3 and file4. You are currently on "next" branch.

    $ rm dir/file3
    $ git status -suno
     D dir/file3
Now, what should "git checkout master dir" do?
Checking paths out of a tree is (currently) defined to:
 - Grab the paths from the named tree that match the given pathspec,
   and add them to the index; and then
 - Check out the contents from the index for paths that match the
   pathspec to the working tree;
 - While at it, if the given pathspec did not match anything, suspect
   a typo from the command line and error out without updating the index
   nor the working tree.

According to that definition, because "master" has dir/file1, and the index is unchanged since "next", we would add dir/file1 to the index, and then check dir/file1 and dir/file3 out of the index. Hence, we end up resurrecting dir/file3 out of the index, even though "master" does not have that path.

This is somewhat surprising.

It may make sense to tweak the semantics a little bit. We can grab the paths out of the named tree ("master" in this case), update the index, and update the working tree with only with these paths we grabbed from the named tree. By doing so, we will keep the local modification to dir/file3 (in this case, the modification is to "delete", but the above observation hold equally true if dir/file3 were modified).

An alternative semantics could be to first remove paths that match the given pathspec from the index, then update the index with paths taken from the named tree, and update the working tree. "git checkout master dir" would then mean "replace anything currently in dir with whatever is in dir in master". It is more dangerous, and it can easily emulated by doing:

    $ git rm -rf dir
    $ git checkout master dir
so I did not go that far with this patch.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This is a behaviour change, but it may qualify as a bugfix. I dunno.
 builtin/checkout.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
Show changes to builtin/checkout.c +5 −1
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 5e356a6..75dbe76 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -71,7 +71,7 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
 	hashcpy(ce->sha1, sha1);
 	memcpy(ce->name, base, baselen);
 	memcpy(ce->name + baselen, pathname, len - baselen);
-	ce->ce_flags = create_ce_flags(len, 0);
+	ce->ce_flags = create_ce_flags(len, 0) | CE_UPDATE;
 	ce->ce_mode = create_ce_mode(mode);
 	add_cache_entry(ce, ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE);
 	return 0;
@@ -228,6 +228,8 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
 
 	for (pos = 0; pos < active_nr; pos++) {
 		struct cache_entry *ce = active_cache[pos];
+		if (source_tree && !(ce->ce_flags & CE_UPDATE))
+			continue;
 		match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, ps_matched);
 	}
 
@@ -266,6 +268,8 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
 	state.refresh_cache = 1;
 	for (pos = 0; pos < active_nr; pos++) {
 		struct cache_entry *ce = active_cache[pos];
+		if (source_tree && !(ce->ce_flags & CE_UPDATE))
+			continue;
 		if (match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL)) {
 			if (!ce_stage(ce)) {
 				errs |= checkout_entry(ce, &state, NULL);
John Szakmeister· Sep 30, 2011, 01:02 UTC · re: Junio C Hamano · lore

Re: [RFC/PATCH] git checkout $tree path

On Thu, Sep 29, 2011 at 6:46 PM, Junio C Hamano <gitster@pobox.com> wrote: [snip]

Show 7 quoted lines
> According to that definition, because "master" has dir/file1, and the
> index is unchanged since "next", we would add dir/file1 to the index, and
> then check dir/file1 and dir/file3 out of the index. Hence, we end up
> resurrecting dir/file3 out of the index, even though "master" does not
> have that path.
>
> This is somewhat surprising.

That is surprising! It explains something I saw just yesterday which closely mirrors your recipe.

Show 6 quoted lines
> It may make sense to tweak the semantics a little bit. We can grab the
> paths out of the named tree ("master" in this case), update the index, and
> update the working tree with only with these paths we grabbed from the
> named tree. By doing so, we will keep the local modification to dir/file3
> (in this case, the modification is to "delete", but the above observation
> hold equally true if dir/file3 were modified).
That seems sane.
Show 5 quoted lines
> An alternative semantics could be to first remove paths that match the
> given pathspec from the index, then update the index with paths taken from
> the named tree, and update the working tree. "git checkout master dir"
> would then mean "replace anything currently in dir with whatever is in dir
> in master". It is more dangerous, and it can easily emulated by doing:
This is equally sane, but is probably closer to my expectation.
[snip]
>  * This is a behaviour change, but it may qualify as a bugfix. I dunno.
Personally, I lean towards it being a bugfix.
-John
Jeff King· Oct 3, 2011, 10:26 UTC · re: Junio C Hamano · lore

Re: [RFC/PATCH] git checkout $tree path

On Thu, Sep 29, 2011 at 03:46:31PM -0700, Junio C Hamano wrote:
Show 7 quoted lines
> According to that definition, because "master" has dir/file1, and the
> index is unchanged since "next", we would add dir/file1 to the index, and
> then check dir/file1 and dir/file3 out of the index. Hence, we end up
> resurrecting dir/file3 out of the index, even though "master" does not
> have that path.
> 
> This is somewhat surprising.
Agreed, it is surprising.
Show 6 quoted lines
> It may make sense to tweak the semantics a little bit. We can grab the
> paths out of the named tree ("master" in this case), update the index, and
> update the working tree with only with these paths we grabbed from the
> named tree. By doing so, we will keep the local modification to dir/file3
> (in this case, the modification is to "delete", but the above observation
> hold equally true if dir/file3 were modified).

Hmm. I can see that being what the user expects in some cases. For example, when "master" has nothing to do with dir/file3 in the first place. But I can also see this:

Show 5 quoted lines
> An alternative semantics could be to first remove paths that match the
> given pathspec from the index, then update the index with paths taken from
> the named tree, and update the working tree. "git checkout master dir"
> would then mean "replace anything currently in dir with whatever is in dir
> in master". It is more dangerous, and it can easily emulated by doing:

being what the user expects. As in, "master deleted this file; shouldn't checkout pull the deletion to my new branch when I ask it to?".

But we can't distinguish those two cases without actually having a merge base. And this isn't a merge; it's not about picking changes from master, it's about saying "make dir look like it does in master". So in that sense, the most straightforward thing is your second alternative: afterwards, we should have only the files in "dir" that master has.

A related question is what does this do:
  git reset master -- dir

My mental model is that it makes the index for "dir" look just like master:dir. And that seems pretty accurate; it deletes dir/file3 (which does not exist in "master") from the index.

My mental model of "git checkout master -- dir" is similar. It should make the index for "dir" look like master:dir, and then check that out. IOW, I think of it as:

  git reset master -- dir &&
  git checkout -- dir

Maybe that is not accurate (well, clearly it does not match the current behavior), but I think it is at least easy to explain and relatively sane. So it is something to shoot for, and makes "git checkout" consistent with "git reset".

>  * This is a behaviour change, but it may qualify as a bugfix. I dunno.

I think it is a bug. I can see both of the alternatives you outlined above making some sense, but checking out content that has _nothing_ to do with master is just confusing. Either make it look like master, or leave it alone.

-Peff
Junio C Hamano· Oct 3, 2011, 16:08 UTC · re: Jeff King · lore

Re: [RFC/PATCH] git checkout $tree path

Jeff King <peff@peff.net> writes:
Show 6 quoted lines
> But we can't distinguish those two cases without actually having a merge
> base. And this isn't a merge; it's not about picking changes from
> master, it's about saying "make dir look like it does in master". So
> in that sense, the most straightforward thing is your second
> alternative: afterwards, we should have only the files in "dir" that
> master has.

We can think of it both ways, but the "make it look like the other one" unfortunately is too big a departure from the traditional semantics. At least I wanted "checkout master -- $path" to mean "I want to copy $path out of master and _overlay_ that on top of what I have now", similar to the way how "tar xf master.tar $path" and "cp -r ../master/$path $path" would be used, so that the command can help the user advance what is in progress and already underway in $path in the current working tree.

Replacing could be easily done with "git rm -r [--cached] $path" followed by "git checkout $tree $path" under the original semantics, but overlaying is not very easily done if "git checkout $tree $path" had your "make $path look like it does in $tree" semantics.

The change brought in by the RFC/PATCH does change the behaviour, and I am fairly comfortable now to say that it is a bugfix ("copy and overlay" a la "tar xf" never clobbers/removes files not in the source, but the current code does).

Jeff King· Oct 4, 2011, 07:42 UTC · re: Junio C Hamano · lore

Re: [RFC/PATCH] git checkout $tree path

On Mon, Oct 03, 2011 at 09:08:31AM -0700, Junio C Hamano wrote:
Show 7 quoted lines
> We can think of it both ways, but the "make it look like the other one"
> unfortunately is too big a departure from the traditional semantics. At
> least I wanted "checkout master -- $path" to mean "I want to copy $path
> out of master and _overlay_ that on top of what I have now", similar to
> the way how "tar xf master.tar $path" and "cp -r ../master/$path $path"
> would be used, so that the command can help the user advance what is in
> progress and already underway in $path in the current working tree.

Yeah, I think the "overlay" semantics at least makes some sense. The strongest argument for "replace" semantics is that "git reset <path>" uses them, and it would be nice if they were consistent. But I agree that the overlay is closer to what happens now.

> Replacing could be easily done with "git rm -r [--cached] $path" followed
> by "git checkout $tree $path" under the original semantics, but overlaying
> is not very easily done if "git checkout $tree $path" had your "make $path
> look like it does in $tree" semantics.
True.
> The change brought in by the RFC/PATCH does change the behaviour, and I am
> fairly comfortable now to say that it is a bugfix ("copy and overlay" a la
> "tar xf" never clobbers/removes files not in the source, but the current
> code does).

That sounds good to me, and I think properly doing a pure overlay is way better than the prior behavior.

This is sufficiently tricky and subtle that it is probably worth future-proofing with some tests (e.g., the example you gave in the commit message).

-Peff
Junio C Hamano· Oct 4, 2011, 15:20 UTC · re: Jeff King · lore

Re: [RFC/PATCH] git checkout $tree path

Jeff King <peff@peff.net> writes:
> This is sufficiently tricky and subtle that it is probably worth
> future-proofing with some tests (e.g., the example you gave in the
> commit message).
Yes, see t2022 in 'pu'.
Jay Soffian· Oct 4, 2011, 15:05 UTC · re: Jeff King · lore

Re: [RFC/PATCH] git checkout $tree path

On Mon, Oct 3, 2011 at 6:26 AM, Jeff King <peff@peff.net> wrote:
Show 16 quoted lines
> On Thu, Sep 29, 2011 at 03:46:31PM -0700, Junio C Hamano wrote:
>> An alternative semantics could be to first remove paths that match the
>> given pathspec from the index, then update the index with paths taken from
>> the named tree, and update the working tree. "git checkout master dir"
>> would then mean "replace anything currently in dir with whatever is in dir
>> in master". It is more dangerous, and it can easily emulated by doing:
>
> being what the user expects. As in, "master deleted this file; shouldn't
> checkout pull the deletion to my new branch when I ask it to?".
>
> But we can't distinguish those two cases without actually having a merge
> base. And this isn't a merge; it's not about picking changes from
> master, it's about saying "make dir look like it does in master". So
> in that sense, the most straightforward thing is your second
> alternative: afterwards, we should have only the files in "dir" that
> master has.
I think I'd expect the first behavior with:
$ git checkout master -- dir/*
And the second with:
$ git checkout master -- dir
$0.02.
j.
Junio C Hamano· Oct 5, 2011, 02:07 UTC · re: Jay Soffian · lore

Re: [RFC/PATCH] git checkout $tree path

Jay Soffian <jaysoffian@gmail.com> writes:
> I think I'd expect the first behavior with:
>
> $ git checkout master -- dir/*

If we did not have to worry about "dir/.gitignore", this proposal does have certain attractiveness.

← back to recent threads