git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Possible regression: overwriting untracked files in a fresh repo

From
Jeff King <peff@peff.net>
Date
Aug 25, 2009, 01:36 UTC
Message-ID
<20090825013601.GA3515@coredump.intra.peff.net>
In-Reply-To
<7vab1o3ikz.fsf@alter.siamese.dyndns.org>
On Mon, Aug 24, 2009 at 04:20:12PM -0700, Junio C Hamano wrote:
Show 16 quoted lines
> I do not think that this is the correct fix, but it should be a good start
> for other people to take a look at the issue.  With this change, any
> leftover work tree files will remain, but it has an interesting effect.
> 
>     $ git checkout maint
>     $ echo 'ref: refs/heads/nosuch' >.git/HEAD
>     $ git checkout -b foo master
> 
> You will notice that the index matches master (as expected), but the work
> tree mostly matches maint.  Knowing what these files are (i.e. "these are
> git.git source files that match 'maint' branch, and are vastly behind what
> are in 'master' branch we are switching to"), this result is utterly
> counterintuitive and feels wrong, but if you consider a case like what
> Dscho brought up originally in the thread of having a freshly initialized
> empty repository with some uncommitted files, totally unrelated to what
> you are checking out, I think you could argue that it is the right thing.

I don't think this is the right thing to do. We have _no_ current branch, which means that everything in the working tree can be considered untracked (and therefore precious). So I think the right thing to do is barf and say "this untracked file would be overwritten". The user can sort it out as appropriate, either deleting files that are in the way, or using "-f" themselves (after they make the decision that what they have can be overwritten).

Something like the patch below seems to work on the test case I posted earlier, and passes the test suite (but other than that, I am very unconfident, never having really looked at the checkout code before).

As an aside, if an unborn branch stops implying "-f", should we bother even mentioning it? It seems superfluous now.

---
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 8a9a474..1f2f84d 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -402,7 +402,9 @@ static int merge_working_tree(struct checkout_opts *opts,
 		topts.dir = xcalloc(1, sizeof(*topts.dir));
 		topts.dir->flags |= DIR_SHOW_IGNORED;
 		topts.dir->exclude_per_dir = ".gitignore";
-		tree = parse_tree_indirect(old->commit->object.sha1);
+		tree = parse_tree_indirect(old->commit ?
+					   old->commit->object.sha1 :
+					   (unsigned char *)EMPTY_TREE_SHA1_BIN);
 		init_tree_desc(&trees[0], tree->buffer, tree->size);
 		tree = parse_tree_indirect(new->commit->object.sha1);
 		init_tree_desc(&trees[1], tree->buffer, tree->size);
@@ -541,13 +543,8 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
 		parse_commit(new->commit);
 	}
 
-	if (!old.commit && !opts->force) {
-		if (!opts->quiet) {
-			warning("You appear to be on a branch yet to be born.");
-			warning("Forcing checkout of %s.", new->name);
-		}
-		opts->force = 1;
-	}
+	if (!old.commit && !opts->force && !opts->quiet)
+		warning("You appear to be on a branch yet to be born.");
 
 	ret = merge_working_tree(opts, &old, new);
 	if (ret)
Previous: Junio C HamanoNext: Jeff King
Message 5 of 9 in “Possible regression: overwriting untracked files in a fresh repo”
  1. Johannes SchindelinAug 24, 2009
  2. Jeff KingAug 24, 2009
  3. Junio C HamanoAug 24, 2009
  4. Junio C HamanoAug 24, 2009
  5. Jeff KingAug 25, 2009
  6. Jeff KingAug 25, 2009
  7. Junio C HamanoAug 25, 2009
  8. Jeff KingAug 25, 2009
  9. Johannes SchindelinAug 25, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.