path: root/sha1_file.c
diff options
authorJunio C Hamano <>2011-03-11 00:02:50 (GMT)
committerJunio C Hamano <>2011-03-23 23:05:44 (GMT)
commit90a6464b4ad5887e4d12e660f51dabdbd5c00812 (patch)
tree1ccc1deb96f7dd9edb8b5929e61fa08d619c666d /sha1_file.c
parent547e8b9205349b47003ed25a82673e413e71c255 (diff)
rerere: make sure it works even in a workdir attached to a young repository
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. Strictly speaking, this is incomplete. It does not attempt to handle relative symbolic link that points into the original repository, but this is good enough to help people who use contrib/workdir/git-new-workdir script. Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'sha1_file.c')
1 files changed, 29 insertions, 0 deletions
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);