git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC/PATCH] git checkout $tree path
@ 2011-09-29 22:46 Junio C Hamano
  2011-09-30  1:02 ` John Szakmeister
  2011-10-03 10:26 ` Jeff King
  0 siblings, 2 replies; 8+ messages in thread
From: Junio C Hamano @ 2011-09-29 22:46 UTC (permalink / raw)
  To: git

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(-)

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);

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

* Re: [RFC/PATCH] git checkout $tree path
  2011-09-29 22:46 [RFC/PATCH] git checkout $tree path Junio C Hamano
@ 2011-09-30  1:02 ` John Szakmeister
  2011-10-03 10:26 ` Jeff King
  1 sibling, 0 replies; 8+ messages in thread
From: John Szakmeister @ 2011-09-30  1:02 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

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

> 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.

> 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

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

* Re: [RFC/PATCH] git checkout $tree path
  2011-09-29 22:46 [RFC/PATCH] git checkout $tree path Junio C Hamano
  2011-09-30  1:02 ` John Szakmeister
@ 2011-10-03 10:26 ` Jeff King
  2011-10-03 16:08   ` Junio C Hamano
  2011-10-04 15:05   ` Jay Soffian
  1 sibling, 2 replies; 8+ messages in thread
From: Jeff King @ 2011-10-03 10:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Thu, Sep 29, 2011 at 03:46:31PM -0700, Junio C Hamano wrote:

> 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.

> 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:

> 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

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

* Re: [RFC/PATCH] git checkout $tree path
  2011-10-03 10:26 ` Jeff King
@ 2011-10-03 16:08   ` Junio C Hamano
  2011-10-04  7:42     ` Jeff King
  2011-10-04 15:05   ` Jay Soffian
  1 sibling, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2011-10-03 16:08 UTC (permalink / raw)
  To: Jeff King; +Cc: git

Jeff King <peff@peff.net> writes:

> 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).

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

* Re: [RFC/PATCH] git checkout $tree path
  2011-10-03 16:08   ` Junio C Hamano
@ 2011-10-04  7:42     ` Jeff King
  2011-10-04 15:20       ` Junio C Hamano
  0 siblings, 1 reply; 8+ messages in thread
From: Jeff King @ 2011-10-04  7:42 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Oct 03, 2011 at 09:08:31AM -0700, Junio C Hamano wrote:

> 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

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

* Re: [RFC/PATCH] git checkout $tree path
  2011-10-03 10:26 ` Jeff King
  2011-10-03 16:08   ` Junio C Hamano
@ 2011-10-04 15:05   ` Jay Soffian
  2011-10-05  2:07     ` Junio C Hamano
  1 sibling, 1 reply; 8+ messages in thread
From: Jay Soffian @ 2011-10-04 15:05 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, git

On Mon, Oct 3, 2011 at 6:26 AM, Jeff King <peff@peff.net> wrote:
> 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.

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

* Re: [RFC/PATCH] git checkout $tree path
  2011-10-04  7:42     ` Jeff King
@ 2011-10-04 15:20       ` Junio C Hamano
  0 siblings, 0 replies; 8+ messages in thread
From: Junio C Hamano @ 2011-10-04 15:20 UTC (permalink / raw)
  To: Jeff King; +Cc: git

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'.

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

* Re: [RFC/PATCH] git checkout $tree path
  2011-10-04 15:05   ` Jay Soffian
@ 2011-10-05  2:07     ` Junio C Hamano
  0 siblings, 0 replies; 8+ messages in thread
From: Junio C Hamano @ 2011-10-05  2:07 UTC (permalink / raw)
  To: Jay Soffian; +Cc: Jeff King, git

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.

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

end of thread, other threads:[~2011-10-05  2:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-29 22:46 [RFC/PATCH] git checkout $tree path Junio C Hamano
2011-09-30  1:02 ` John Szakmeister
2011-10-03 10:26 ` Jeff King
2011-10-03 16:08   ` Junio C Hamano
2011-10-04  7:42     ` Jeff King
2011-10-04 15:20       ` Junio C Hamano
2011-10-04 15:05   ` Jay Soffian
2011-10-05  2:07     ` 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).