git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Mark Lodato <lodatom@gmail.com>
Cc: git list <git@vger.kernel.org>
Subject: Re: [BUG] bare repository detection does not work with aliases
Date: Fri, 8 Mar 2013 00:48:25 -0500	[thread overview]
Message-ID: <20130308054824.GA24429@sigill.intra.peff.net> (raw)
In-Reply-To: <CAHREChhuX82ibNEDQnQUeS9TEeyMFGpuNhyXzt1Pn-Tt2BVOQA@mail.gmail.com>

On Thu, Mar 07, 2013 at 05:47:45PM -0500, Mark Lodato wrote:

> It seems that the fallback bare repository detection in the absence of
> core.bare fails for aliases.

This triggered some deja vu for me, so I went digging. And indeed, this
has been a bug since at least 2008. This patch (which never got applied)
fixed it:

  http://thread.gmane.org/gmane.comp.version-control.git/72792

The issue is that we treat:

  GIT_DIR=/some/path git ...

as if the current directory is the work tree, unless core.bare is
explicitly set, or unless an explicit work tree is given (via
GIT_WORK_TREE, "git --work-tree", or in the config).  This is handy, and
backwards compatible.

Inside setup_git_directory, when we find the directory we put it in
$GIT_DIR for later reference by ourselves or sub-programs (since we are
typically moving to the top of the working tree next, we need to record
the original path, and can't rely on discovery finding the same path
again). But we don't set $GIT_WORK_TREE. So if you don't have core.bare
set, the above rule will kick in for sub-programs, or for aliases (which
will call setup_git_directory again).

The solution is that when we set $GIT_DIR like this, we need to also say
"no, there is no working tree; we are bare". And that's what that patch
does. It's 5 years old now, so not surprisingly, it does not apply
cleanly. The moral equivalent in today's code base would be something
like:

diff --git a/environment.c b/environment.c
index 89d6c70..8edaedd 100644
--- a/environment.c
+++ b/environment.c
@@ -200,7 +200,8 @@ void set_git_work_tree(const char *new_work_tree)
 		return;
 	}
 	git_work_tree_initialized = 1;
-	work_tree = xstrdup(real_path(new_work_tree));
+	if (*new_work_tree)
+		work_tree = xstrdup(real_path(new_work_tree));
 }
 
 const char *get_git_work_tree(void)
diff --git a/setup.c b/setup.c
index e1cfa48..f0e1251 100644
--- a/setup.c
+++ b/setup.c
@@ -544,7 +544,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
 	worktree = get_git_work_tree();
 
 	/* both get_git_work_tree() and cwd are already normalized */
-	if (!strcmp(cwd, worktree)) { /* cwd == worktree */
+	if (!worktree || !strcmp(cwd, worktree)) { /* cwd == worktree */
 		set_git_dir(gitdirenv);
 		free(gitfile);
 		return NULL;
@@ -636,6 +636,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
 	}
 	else
 		set_git_dir(".");
+
+	setenv(GIT_WORK_TREE_ENVIRONMENT, "", 1);
 	return NULL;
 }
 

which passes your test. Unfortunately, this patch runs afoul of the same
complaints that prevented the original from being acceptable (weirdness
on Windows with empty environment variables).

Having read the discussion again, I _think_ the more sane thing is to
actually just have a new variable, $GIT_BARE, which overrides any
core.bare config (just as $GIT_WORK_TREE override core.worktree). And
then we set that explicitly when we are in a bare $GIT_DIR, propagating
our auto-detection to sub-processes.

-Peff

  reply	other threads:[~2013-03-08  5:48 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-07 22:47 [BUG] bare repository detection does not work with aliases Mark Lodato
2013-03-08  5:48 ` Jeff King [this message]
2013-03-08  6:27   ` Junio C Hamano
2013-03-08  6:37     ` Jeff King
2013-03-08  7:15   ` [PATCH] setup: suppress implicit "." work-tree for bare repos Jeff King
2013-03-08  7:44     ` Johannes Sixt
2013-03-08  8:42       ` Jeff King
2013-03-08  9:28         ` [PATCHv2] setup and GIT_IMPLICIT_WORK_TREE Jeff King
2013-03-08  9:29           ` [PATCH v2 1/3] cache.h: drop LOCAL_REPO_ENV_SIZE Jeff King
2013-03-08  9:30           ` [PATCH v2 2/3] environment: add GIT_PREFIX to local_repo_env Jeff King
2013-03-08 21:39             ` Eric Sunshine
2013-03-08 21:44               ` Jeff King
2013-03-08 23:03                 ` Junio C Hamano
2013-03-08  9:32           ` [PATCH v2 3/3] setup: suppress implicit "." work-tree for bare repos Jeff King
2013-03-08  7:54     ` [PATCH] " Junio C Hamano
2013-03-08  8:43       ` Jeff King

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=20130308054824.GA24429@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=lodatom@gmail.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 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).