# Should 'git status' understand a .git containing "gitdir: dir"?

5 messages from 2011-06-30 to 2011-06-30. Participants: Eric Raible, Fredrik Gustafsson, Nguyen Thai Ngoc Duy, Junio C Hamano.
Thread: https://gitlist.dev/t/27733

## Eric Raible, 2011-06-30 09:04

Subject: Should 'git status' understand a .git containing "gitdir: dir"?
Message-ID: <4E0C3C1D.1050406@nextest.com>
URL: https://gitlist.dev/e/4E0C3C1D.1050406%40nextest.com

```
The following sequence sets up a trivial repo that uses "gitdir:":

$ git init gitdir-test
$ cd gitdir-test
$ mv .git real-git-dir
$ echo "gitdir: real-git-dir" > .git
$ git status

Fine so far.  But git-status shows that "real-git-dir" is untracked:

$ git status -sb
## Initial commit on master
?? real-git-dir/

Which strikes one as a bit inconsistent (since other pars of git-status
knows to look in real-git-dir to find the index).

Sorry - no time to investigate.

```

## Fredrik Gustafsson, 2011-06-30 11:46

Subject: [RFC] status - don't show gitdir
Message-ID: <1309434388-3536-1-git-send-email-iveqy@iveqy.com>
URL: https://gitlist.dev/e/1309434388-3536-1-git-send-email-iveqy%40iveqy.com
In-Reply-To: <4E0C3C1D.1050406@nextest.com>

```
When gitdir is inside the working dir, don't show it as a untracked
directory.

Signed-off-by: Fredrik Gustafsson <iveqy@iveqy.com>
---
This patch works but have a few weaknesses. The string manipulations done
is cumbersome and can possible be done nicer (not what I could to with
support of strbuf).

Also, this patch doesn't know why .git isn't showed as untracked. Maybe
this code is at the wrong place.

 dir.c             |   28 ++++++++++++++++++++++++++++
 t/t7508-status.sh |   10 ++++++++++
 2 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/dir.c b/dir.c
index 08281d2..9cabc29 100644
--- a/dir.c
+++ b/dir.c
@@ -1215,6 +1215,13 @@ int remove_dir_recursively(struct strbuf *path, int flag)
 void setup_standard_excludes(struct dir_struct *dir)
 {
 	const char *path;
+	const char *work_tree;
+	const char *git_dir;
+	struct strbuf wt_full;
+	struct strbuf gd_base;
+	struct strbuf gd_relpath;
+	struct strbuf gd;
+	int i = 0;
 
 	dir->exclude_per_dir = ".gitignore";
 	path = git_path("info/exclude");
@@ -1222,6 +1229,27 @@ void setup_standard_excludes(struct dir_struct *dir)
 		add_excludes_from_file(dir, path);
 	if (excludes_file && !access(excludes_file, R_OK))
 		add_excludes_from_file(dir, excludes_file);
+	work_tree = get_git_work_tree();
+	strbuf_init(&wt_full,0);
+	strbuf_addstr(&wt_full,work_tree);
+	strbuf_addstr(&wt_full,"/.git");
+	git_dir = read_gitfile_gently(wt_full.buf);
+	strbuf_remove(&wt_full,wt_full.len-5,5);
+	if(git_dir) {
+		strbuf_init(&gd,0);
+		strbuf_addstr(&gd,git_dir);
+		if(gd.len > wt_full.len) {
+			strbuf_init(&gd_base,0);
+			strbuf_add(&gd_base,wt_full.buf,wt_full.len);
+			if(!strbuf_cmp(&gd_base,&wt_full)) {
+				strbuf_init(&gd_relpath,0);
+				for(i = gd_base.len + 1; i < gd.len; i++) {
+					strbuf_addch(&gd_relpath,gd.buf[i]);
+				}
+				add_exclude(gd_relpath.buf,"",0,&dir->exclude_list[EXC_FILE]);
+			}
+		}
+	}
 }
 
 int remove_path(const char *name)
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index 1fdfbd3..1fed0cf 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -1139,4 +1139,14 @@ test_expect_failure '.git/config ignore=all suppresses submodule summary' '
 	git config -f .gitmodules  --remove-section submodule.subname
 '
 
+test_expect_success 'Check if git-dir is ignored when gitfile is used' '
+	git init gitdir-test &&
+	cd gitdir-test &&
+	mv .git real-git-dir &&
+	echo "gitdir: real-git-dir" > .git &&
+	git status -s real-git-dir > actual &&
+	> expect &&
+	test_cmp actual expect
+'
+
 test_done
-- 
1.7.6.rc3.dirty

```

## Nguyen Thai Ngoc Duy, 2011-06-30 12:32

Subject: Re: [RFC] status - don't show gitdir
Message-ID: <BANLkTikCmWLar4ztZgrWdB-md2X3bUxATg@mail.gmail.com>
URL: https://gitlist.dev/e/BANLkTikCmWLar4ztZgrWdB-md2X3bUxATg%40mail.gmail.com
In-Reply-To: <1309434388-3536-1-git-send-email-iveqy@iveqy.com>

```
On Thu, Jun 30, 2011 at 6:46 PM, Fredrik Gustafsson <iveqy@iveqy.com> wrote:
> When gitdir is inside the working dir, don't show it as a untracked
> directory.

read_directory_recursive() and friends ignore .git by default. I think
if you want to ignore the directory that .git file points to, you
should update there. I'm too lazy to check the code (my
read_directory_recursive is heavily modified currently), but I think
it does not check file type for .git entries now.

But if we step back a bit, why do you want git to automatically ignore
dirs that .git file points to? .git files are used to save real repo
somewhere safe, e.g. outside working directory. You do not handle
another case in your patch, where .git is a real symlink. In both
cases, it's not hard to add "real-git-dir" to .git/info/excludes.
-- 
Duy

```

## Junio C Hamano, 2011-06-30 15:49

Subject: Re: Should 'git status' understand a .git containing "gitdir: dir"?
Message-ID: <7vsjqrqpzn.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vsjqrqpzn.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4E0C3C1D.1050406@nextest.com>

```
Eric Raible <raible@nextest.com> writes:

> The following sequence sets up a trivial repo that uses "gitdir:":
>
> $ git init gitdir-test
> $ cd gitdir-test
> $ mv .git real-git-dir
> $ echo "gitdir: real-git-dir" > .git
> $ git status
>
> Fine so far.  But git-status shows that "real-git-dir" is untracked:
>
> $ git status -sb
> ## Initial commit on master
> ?? real-git-dir/
>
> Which strikes one as a bit inconsistent (since other pars of git-status
> knows to look in real-git-dir to find the index).
>
> Sorry - no time to investigate.

You could even have a real git dir of some completely unrelated repository
in your working tree, it will get reported as untracked, and you would
probably not want to track its contents, either (or you might want to if
you are trying to be funny, I dunno).

So I do not see there is anything to investigate. What you observed looks
perfectly expected to me, except for the "mv .git real-git-dir" bit that
makes a situation that confuses yourself (but not git).

```

## Eric Raible, 2011-06-30 16:48

Subject: Re: Should 'git status' understand a .git containing "gitdir: dir"?
Message-ID: <4E0CA8F9.1080601@nextest.com>
URL: https://gitlist.dev/e/4E0CA8F9.1080601%40nextest.com
In-Reply-To: <7vsjqrqpzn.fsf@alter.siamese.dyndns.org>

```
On 6/30/2011 8:49 AM, Junio C Hamano wrote:
> Eric Raible <raible@nextest.com> writes:
> 
>> The following sequence sets up a trivial repo that uses "gitdir:":
>>
>> $ git init gitdir-test
>> $ cd gitdir-test
>> $ mv .git real-git-dir
>> $ echo "gitdir: real-git-dir" > .git
>> $ git status
>>
>> Fine so far.  But git-status shows that "real-git-dir" is untracked:
>>
>> $ git status -sb
>> ## Initial commit on master
>> ?? real-git-dir/
>>
>> Which strikes one as a bit inconsistent (since other pars of git-status
>> knows to look in real-git-dir to find the index).
>>
>> Sorry - no time to investigate.
> 
> You could even have a real git dir of some completely unrelated repository
> in your working tree, it will get reported as untracked, and you would
> probably not want to track its contents, either (or you might want to if
> you are trying to be funny, I dunno).
> 
> So I do not see there is anything to investigate. What you observed looks
> perfectly expected to me, except for the "mv .git real-git-dir" bit that
> makes a situation that confuses yourself (but not git).
> .

The fact that the repo is stored in .git is an implementation detail -
and one which git-status knows about (in the normal case).

In the gidir: case one part of git status understands the details
(after all - it reads real-git-dir/index) while another part doesn't
(after all - it show the actual repo as a normal directory).

Sure, git-real-dir could be added to git-real-dir/info/exclude.
But by that logic we could insist on adding .git to .git/info/exclude.

The argument about an unrelated repo in the working tree is irrelevant -
.git wouldn't point to it, so there's nothings special about it.

But it's obviously not a big deal either way and I'm gonna drop it.

```
