Skip to contentSkip to conversation

Introduce the .git-file

PATCH In review Lars Hjemli 3 participants 13 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

cover Introduce the .git-file

This is a resend of my previous patches + an update to make git-submodule work with the .git-file + a litte documentation.

PS: Regarding the color of the bikeshed, it seems that everyone can live with "gitdir:" so that's what I ended up using.

Diffstat:

 Documentation/repository-layout.txt |    5 ++-
 git-submodule.sh                    |    4 +-
 setup.c                             |   40 +++++++++++++++++++
 t/t0002-gitfile.sh                  |   71 +++++++++++++++++++++++++++++++++++
 4 files changed, 117 insertions(+), 3 deletions(-)
#1 lore raw

PATCH 1/4 Add platform-independent .git "symlink"

This patch allows .git to be a regular textfile containing the path of the real git directory (formatted like "gitdir: <path>\n"), which is useful on platforms lacking support for real symlinks.

---
 setup.c |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+), 0 deletions(-)
setup.c +40 -0
index adede16..2cbda91 100644
@@ -239,6 +239,40 @@ static int check_repository_format_gently(int *nongit_ok)
 }
 
 /*
+ * Try to read the location of the git directory from the .git file,
+ * return path to git directory if found.
+ * Format of the .git file is
+ *    gitdir: <path>\n
+ */
+static const char *read_gitfile_gently(const char *path)
+{
+	static char buf[PATH_MAX + 9];  /* "gitdir: " + "\n" */
+	struct stat st;
+	int fd;
+	size_t len;
+
+	if (stat(path, &st))
+		return NULL;
+	if (!S_ISREG(st.st_mode) || st.st_size >= sizeof(buf))
+		return NULL;
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return NULL;
+	len = read_in_full(fd, buf, sizeof(buf));
+	close(fd);
+	if (len != st.st_size)
+		return NULL;
+	if (!len || buf[len - 1] != '\n')
+		return NULL;
+	buf[len - 1] = '\0';
+	if (prefixcmp(buf, "gitdir: "))
+		return NULL;
+	if (!is_git_directory(buf + 8))
+		return NULL;
+	return make_absolute_path(buf + 8);
+}
+
+/*
  * We cannot decide in this function whether we are in the work tree or
  * not, since the config can only be read _after_ this function was called.
  */
@@ -247,6 +281,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
 	static char cwd[PATH_MAX+1];
 	const char *gitdirenv;
+	const char *gitfile_dir;
 	int len, offset;
 
 	/*
@@ -302,6 +337,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	 */
 	offset = len = strlen(cwd);
 	for (;;) {
+		gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
+		if (gitfile_dir) {
+			setenv(GIT_DIR_ENVIRONMENT, gitfile_dir, 1);
+			break;
+		}
 		if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
 			break;
 		if (is_git_directory(".")) {
Signature · 1 line
1.5.4.5.g25d066
#2 lore raw

PATCH 2/4 Document the .git-file

---
 Documentation/repository-layout.txt |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
Documentation/repository-layout.txt +4 -1
index 6939130..e9db3a1 100644
@@ -3,7 +3,10 @@ git repository layout
 
 You may find these things in your git repository (`.git`
 directory for a repository associated with your working tree, or
-`'project'.git` directory for a public 'bare' repository).
+`'project'.git` directory for a public 'bare' repository. It is
+also possible to have a working tree where `.git` is a plain
+ascii file containing `gitdir: <path>\n`, i.e. the path to the
+real git repository).
 
 objects::
 	Object store associated with this repository.  Usually
Signature · 1 line
1.5.4.5.g25d066
#3 lore raw

PATCH 3/4 Add tests for .git file

Verify that the basic plumbing works when .git is a file pointing at the real git directory.

---
 t/t0002-gitfile.sh |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 71 insertions(+), 0 deletions(-)
 create mode 100755 t/t0002-gitfile.sh
t/t0002-gitfile.sh new file +71 -0
new file mode 100755
index 0000000..f8f39e6
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+test_description='.git file
+
+Verify that plumbing commands work when .git is a file
+'
+. ./test-lib.sh
+
+chkfile() {
+	D=$(echo $1 | cut -b 1-2) &&
+	F=$(echo $1 | cut -b 3-40) &&
+	if test ! -f $REAL/objects/$D/$F
+	then
+		echo "Object not found: $REAL/objects/$D/$F"
+		false
+	fi
+}
+
+test_expect_success 'setup' '
+	REAL=$(pwd)/.real &&
+	mv .git $REAL &&
+	echo "gitdir: $REAL" >.git
+'
+
+test_expect_success 'check rev-parse --git-dir' '
+	test "$REAL" = "$(git rev-parse --git-dir)"
+'
+
+test_expect_success 'check hash-object' '
+	echo "foo" >bar &&
+	SHA=$(cat bar | git hash-object -w --stdin) &&
+	chkfile $SHA
+'
+
+test_expect_success 'check cat-file' '
+	git cat-file blob $SHA >actual &&
+	diff -u bar actual
+'
+
+test_expect_success 'check update-index' '
+	if test -f $REAL/index
+	then
+		echo "Hmm, $REAL/index exists?"
+		false
+	fi &&
+	rm -rf $REAL/objects/* &&
+	git update-index --add bar &&
+	if ! test -f $REAL/index
+	then
+		echo "$REAL/index not found"
+		false
+	fi &&
+	chkfile $SHA
+'
+
+test_expect_success 'check write-tree' '
+	SHA=$(git write-tree) &&
+	chkfile $SHA
+'
+
+test_expect_success 'check commit-tree' '
+	SHA=$(echo "commit bar" | git commit-tree $SHA) &&
+	chkfile $SHA
+'
+
+test_expect_success 'check rev-list' '
+	echo $SHA >$REAL/HEAD &&
+	test "$SHA" = "$(git rev-list HEAD)"
+'
+
+test_done
Signature · 1 line
1.5.4.5.g25d066
#4 lore raw

PATCH 4/4 git-submodule: prepare for the .git-file

When git-submodule tried to detect 'active' submodules, it checked for the existence of a directory named '.git'. This isn't good enough now that .git can be a file pointing to the real $GIT_DIR so the tests are changed to reflect this.

---
 git-submodule.sh |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
git-submodule.sh +2 -2
index a6aaf40..220d64c 100755
@@ -288,7 +288,7 @@ cmd_update()
 			continue
 		fi
 
-		if ! test -d "$path"/.git
+		if ! test -e "$path"/.git
 		then
 			module_clone "$path" "$url" || exit
 			subsha1=
@@ -362,7 +362,7 @@ cmd_status()
 	do
 		name=$(module_name "$path") || exit
 		url=$(git config submodule."$name".url)
-		if test -z "url" || ! test -d "$path"/.git
+		if test -z "url" || ! test -e "$path"/.git
 		then
 			say "-$sha1 $path"
 			continue;
Signature · 1 line
1.5.4.5.g25d066
#5 lore raw

Re: [PATCH 4/4] git-submodule: prepare for the .git-file

git-submodule.sh cmd_update() · line 291

Quoted 2 lines · Lars Hjemli
git-submodule.sh +1 -1
-		if ! test -d "$path"/.git
+		if ! test -e "$path"/.git

Is test -e supported by all shells we support ? I have some doubts...

Mike

#6 lore raw

Re: [PATCH 4/4] git-submodule: prepare for the .git-file

Quoted 6 lines · Mike Hommey

git-submodule.sh cmd_update() · line 291

Quoted 2 lines · Lars Hjemli
git-submodule.sh +1 -1
-             if ! test -d "$path"/.git
+             if ! test -e "$path"/.git

Is test -e supported by all shells we support ? I have some doubts...

I dunno, but we already use it two other places in git-submodule.

I guess we can rewrite it to something like
test -d $path || test -f $path || test -L $path || exist=0
if needed.

Signature · 1 line
lh
#7 lore raw

Re: [PATCH 3/4] Add tests for .git file

In reply to Lars Hjemli
Quoted 17 lines · Lars Hjemli

Verify that the basic plumbing works when .git is a file pointing at the real git directory.

---
 t/t0002-gitfile.sh |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 71 insertions(+), 0 deletions(-)
 create mode 100755 t/t0002-gitfile.sh
t/t0002-gitfile.sh new file +2 -0
new file mode 100755
index 0000000..f8f39e6
@@ -0,0 +1,71 @@
+#!/bin/sh
+

t/t0002-gitfile.sh line 12

Quoted 10 lines
t/t0002-gitfile.sh new file +10 -0
+test_description='.git file
+
+Verify that plumbing commands work when .git is a file
+'
+. ./test-lib.sh
+
+chkfile() {
+	D=$(echo $1 | cut -b 1-2) &&
+	F=$(echo $1 | cut -b 3-40) &&
+	if test ! -f $REAL/objects/$D/$F
	P=$(echo $1 | sed -e 's|\(..\)\(.*\)|\1/\2\|') &&
        if test ! -f "$REAL/objects/$P"
		...

Although it probably is more efficient to do this with one process, s/cut/sed/ is mostly a style issue.

Be careful with quoting. $REAL might contain a $IFS whitespace as you grabbed it from $(pwd)...

t/t0002-gitfile.sh line 46

Quoted 13 lines
t/t0002-gitfile.sh new file +12 -0
+test_expect_success 'setup' '
+	REAL=$(pwd)/.real &&
+	mv .git $REAL &&
+	echo "gitdir: $REAL" >.git
+'
...
+test_expect_success 'check update-index' '
+	if test -f $REAL/index
+	then
+		echo "Hmm, $REAL/index exists?"
+		false
+	fi &&
+	rm -rf $REAL/objects/* &&

... and it matters especially if you do this ;-)

#8 lore raw

PATCH v2 3/4 Add tests for .git file

Verify that the basic plumbing works when .git is a file pointing at the real git directory.

---

This updated patch replaces cut with sed and tries to do proper quoting.

 t/t0002-gitfile.sh |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 74 insertions(+), 0 deletions(-)
 create mode 100755 t/t0002-gitfile.sh
t/t0002-gitfile.sh new file +74 -0
new file mode 100755
index 0000000..7f8ee2f
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+test_description='.git file
+
+Verify that plumbing commands work when .git is a file
+'
+. ./test-lib.sh
+
+objpath() {
+    echo $1 | sed -re 's|(..)(.+)|\1/\2|'
+}
+
+objck() {
+	p=$(objpath $1)
+	if test ! -f "$REAL/objects/$p"
+	then
+		echo "Object not found: $REAL/objects/$p"
+		false
+	fi
+}
+
+test_expect_success 'setup' '
+	REAL="$(pwd)/.real" &&
+	mv .git "$REAL" &&
+	echo "gitdir: $REAL" >.git
+'
+
+test_expect_success 'check rev-parse --git-dir' '
+	test "$REAL" = "$(git rev-parse --git-dir)"
+'
+
+test_expect_success 'check hash-object' '
+	echo "foo" >bar &&
+	SHA=$(cat bar | git hash-object -w --stdin) &&
+	objck $SHA
+'
+
+test_expect_success 'check cat-file' '
+	git cat-file blob $SHA >actual &&
+	diff -u bar actual
+'
+
+test_expect_success 'check update-index' '
+	if test -f "$REAL/index"
+	then
+		echo "Hmm, $REAL/index exists?"
+		false
+	fi &&
+	rm -f "$REAL/objects/$(objpath $SHA)" &&
+	git update-index --add bar &&
+	if ! test -f "$REAL/index"
+	then
+		echo "$REAL/index not found"
+		false
+	fi &&
+	objck $SHA
+'
+
+test_expect_success 'check write-tree' '
+	SHA=$(git write-tree) &&
+	objck $SHA
+'
+
+test_expect_success 'check commit-tree' '
+	SHA=$(echo "commit bar" | git commit-tree $SHA) &&
+	objck $SHA
+'
+
+test_expect_success 'check rev-list' '
+	echo $SHA >"$REAL/HEAD" &&
+	test "$SHA" = "$(git rev-list HEAD)"
+'
+
+test_done
Signature · 1 line
1.5.4.6.g6627
#9 lore raw

Re: [PATCH 3/4 v2] Add tests for .git file

Quoted 20 lines · Lars Hjemli

Verify that the basic plumbing works when .git is a file pointing at the real git directory.

---

This updated patch replaces cut with sed and tries to do proper quoting.

 t/t0002-gitfile.sh |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 74 insertions(+), 0 deletions(-)
 create mode 100755 t/t0002-gitfile.sh
t/t0002-gitfile.sh new file +1 -0
new file mode 100755
index 0000000..7f8ee2f
@@ -0,0 +1,74 @@
+#!/bin/sh

t/t0002-gitfile.sh line 11

Quoted 10 lines
t/t0002-gitfile.sh new file +10 -0
+
+test_description='.git file
+
+Verify that plumbing commands work when .git is a file
+'
+. ./test-lib.sh
+
+objpath() {
+    echo $1 | sed -re 's|(..)(.+)|\1/\2|'
+}

"sed -r"??? Please limit ourselves to the basics.

t/t0002-gitfile.sh line 72

Quoted 9 lines
t/t0002-gitfile.sh new file +9 -0
+test_expect_success 'check commit-tree' '
+	SHA=$(echo "commit bar" | git commit-tree $SHA) &&
+	objck $SHA
+'
+
+test_expect_success 'check rev-list' '
+	echo $SHA >"$REAL/HEAD" &&
+	test "$SHA" = "$(git rev-list HEAD)"
+'

Nicely done.

#10 lore raw

Re: [PATCH 3/4 v2] Add tests for .git file

Quoted 6 lines · Junio C Hamano

t/t0002-gitfile.sh line 11

Quoted 3 lines · Lars Hjemli
t/t0002-gitfile.sh new file +3 -0
+objpath() {
+    echo $1 | sed -re 's|(..)(.+)|\1/\2|'
+}

"sed -r"??? Please limit ourselves to the basics.

Sorry, I didn't realize 'sed -r' wasn't kosher. Should I resend or is this something you'd --amend?

--
larsh

#11 lore raw

Re: [PATCH 3/4 v2] Add tests for .git file

Quoted 10 lines · Lars Hjemli
Quoted 6 lines · Junio C Hamano

t/t0002-gitfile.sh line 11

Quoted 3 lines · Lars Hjemli
t/t0002-gitfile.sh new file +3 -0
+objpath() {
+    echo $1 | sed -re 's|(..)(.+)|\1/\2|'
+}

"sed -r"??? Please limit ourselves to the basics.

Sorry, I didn't realize 'sed -r' wasn't kosher. Should I resend or is this something you'd --amend?

If we live in POSIX only world it is fine, but I try to be conservative when I am able.

No need to resend; I've done something like this (there are other minor amends).

    diff --git a/t/t0002-gitfile.sh b/t/t0002-gitfile.sh
    new file mode 100755
    index 0000000..d280663
    --- /dev/null
    +++ b/t/t0002-gitfile.sh
    @@ -0,0 +1,74 @@
    +#!/bin/sh
    +
    +test_description='.git file
    +
    +Verify that plumbing commands work when .git is a file
    +'
    +. ./test-lib.sh
    +
    +objpath() {
    +    echo "$1" | sed -e 's|\(..\)|\1/|'
    +}
    + ...

But the result won't be pushed out til tomorrow morning.

#12 lore raw

Re: [PATCH 3/4 v2] Add tests for .git file

Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Lars Hjemli
Quoted 6 lines · Junio C Hamano

t/t0002-gitfile.sh line 11

Quoted 3 lines · Lars Hjemli
t/t0002-gitfile.sh new file +3 -0
+objpath() {
+    echo $1 | sed -re 's|(..)(.+)|\1/\2|'
+}

"sed -r"??? Please limit ourselves to the basics.

Quoted 5 lines
Quoted 2 lines

Sorry, I didn't realize 'sed -r' wasn't kosher. Should I resend or is this something you'd --amend?

If we live in POSIX only world it is fine, but I try to be conservative when I am able.

Ok, good to know that my lack of experience doesn't pollute the codebase ;-)

Quoted 6 lines

No need to resend; I've done something like this (there are other minor amends).
...

    +objpath() {
    +    echo "$1" | sed -e 's|\(..\)|\1/|'
    +}

Thanks.
--
larsh