[PATCH] rerere: make sure it works even in a workdir attached to a young repository

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The git-new-workdir script in contrib/ makes a new work tree by sharing
many subdirectories of the .git directory with the original repository.
When rerere.enabled is set in the original repository, but the user has
not encountered any conflicts yet, the original repository may not yet
have .git/rr-cache directory.

When rerere wants to run in a new work tree created from such a young
original repository, it fails to mkdir(2) .git/rr-cache that is a symlink
to a yet-to-be-created directory.

There are three possible approaches to this:

 - A naive solution is not to create a symlink in the git-new-workdir
   script to a directory the original does not have (yet).  This is not a
   solution, as we tend to lazily create subdirectories of .git/, and
   having rerere.enabled configuration set is a strong indication that the
   user _wants_ to have this lazy creation to happen;

 - We could always create .git/rr-cache upon repository creation.  This is
   tempting but will not help people with existing repositories.

 - Detect this case by seeing that mkdir(2) failed with EEXIST, checking
   that the path is a symlink, and try running mkdir(2) on the link
   target.

This patch solves the issue by doing the third one.

Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx>

---

 * As the third test in the new test t1021 says, this is incomplete in
   that it does not attempt to handle relative symbolic link that points
   into the original repository.  It is good enough for a workdir the
   contrib script creates, but I wouldn't be surprised if there are
   scripts that creates a similar thin workdir using relative symbolic
   links, and this _will_ fail in such a setting.

 cache.h                      |    1 +
 rerere.c                     |    3 +-
 sha1_file.c                  |   29 +++++++++++++++++++++++
 t/t1021-rerere-in-workdir.sh |   52 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 2 deletions(-)
 create mode 100755 t/t1021-rerere-in-workdir.sh

diff --git a/cache.h b/cache.h
index 6e54993..2eddee7 100644
--- a/cache.h
+++ b/cache.h
@@ -674,6 +674,7 @@ int set_shared_perm(const char *path, int mode);
 #define adjust_shared_perm(path) set_shared_perm((path), 0)
 int safe_create_leading_directories(char *path);
 int safe_create_leading_directories_const(const char *path);
+int mkdir_in_gitdir(const char *path);
 extern char *expand_user_path(const char *path);
 char *enter_repo(char *path, int strict);
 static inline int is_absolute_path(const char *path)
diff --git a/rerere.c b/rerere.c
index a59f74f..2d8de61 100644
--- a/rerere.c
+++ b/rerere.c
@@ -522,8 +522,7 @@ static int is_rerere_enabled(void)
 	if (rerere_enabled < 0)
 		return rr_cache_exists;
 
-	if (!rr_cache_exists &&
-	    (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
+	if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
 		die("Could not create directory %s", rr_cache);
 	return 1;
 }
diff --git a/sha1_file.c b/sha1_file.c
index c23cc5e..0926643 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -42,6 +42,35 @@ static inline int offset_1st_component(const char *path)
 	return *path == '/';
 }
 
+int mkdir_in_gitdir(const char *path)
+{
+	if (mkdir(path, 0777)) {
+		int saved_errno = errno;
+		struct stat st;
+		struct strbuf sb = STRBUF_INIT;
+
+		if (errno != EEXIST)
+			return -1;
+		/*
+		 * Are we looking at a path in a symlinked worktree
+		 * whose original repository does not yet have it?
+		 * e.g. .git/rr-cache pointing at its original
+		 * repository in which the user hasn't performed any
+		 * conflict resolution yet?
+		 */
+		if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
+		    strbuf_readlink(&sb, path, st.st_size) ||
+		    !is_absolute_path(sb.buf) ||
+		    mkdir(sb.buf, 0777)) {
+			strbuf_release(&sb);
+			errno = saved_errno;
+			return -1;
+		}
+		strbuf_release(&sb);
+	}
+	return adjust_shared_perm(path);
+}
+
 int safe_create_leading_directories(char *path)
 {
 	char *pos = path + offset_1st_component(path);
diff --git a/t/t1021-rerere-in-workdir.sh b/t/t1021-rerere-in-workdir.sh
new file mode 100755
index 0000000..ec8621c
--- /dev/null
+++ b/t/t1021-rerere-in-workdir.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='rerere run in a workdir'
+. ./test-lib.sh
+
+test_expect_success SYMLINKS setup '
+	git config rerere.enabled true &&
+	>world &&
+	git add world &&
+	test_tick &&
+	git commit -m initial &&
+
+	echo hello >world &&
+	test_tick &&
+	git commit -a -m hello &&
+
+	git checkout -b side HEAD^ &&
+	echo goodbye >world &&
+	test_tick &&
+	git commit -a -m goodbye &&
+
+	git checkout master
+'
+
+test_expect_success SYMLINKS 'rerere in workdir' '
+	rm -rf .git/rr-cache &&
+	"$SHELL_PATH" "$TEST_DIRECTORY/../contrib/workdir/git-new-workdir" . work &&
+	(
+		cd work &&
+		test_must_fail git merge side &&
+		git rerere status >actual &&
+		echo world >expect &&
+		test_cmp expect actual
+	)
+'
+
+# This fails because we don't resolve relative symlink in mkdir_in_gitdir()
+test_expect_failure SYMLINKS 'rerere in workdir (relative)' '
+	rm -rf .git/rr-cache &&
+	"$SHELL_PATH" "$TEST_DIRECTORY/../contrib/workdir/git-new-workdir" . krow &&
+	(
+		cd krow &&
+		rm -f .git/rr-cache &&
+		ln -s ../.git/rr-cache .git/rr-cache &&
+		test_must_fail git merge side &&
+		git rerere status >actual &&
+		echo world >expect &&
+		test_cmp expect actual
+	)
+'
+
+test_done
-- 
1.7.4.1.373.g37629

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]