Code

ref locking: allow 'foo' when 'foo/bar' used to exist but not anymore.
authorJunio C Hamano <junkio@cox.net>
Sat, 30 Sep 2006 09:25:30 +0000 (02:25 -0700)
committerJunio C Hamano <junkio@cox.net>
Sat, 30 Sep 2006 22:07:58 +0000 (15:07 -0700)
It is normal to have .git/refs/heads/foo directory which is
empty after the last branch whose name starts with foo/ is
removed.  Make sure we notice this case and allow creation of
branch foo by removing the empty directory.

Signed-off-by: Junio C Hamano <junkio@cox.net>
refs.c

diff --git a/refs.c b/refs.c
index 3d4cdd1eb9bda5fe6260d8e7ddba99a4278c3252..b433c0c2d09e1ba363bd9c10f7144f089dd66b70 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -473,6 +473,59 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,
        return lock;
 }
 
+static int remove_empty_dir_recursive(char *path, int len)
+{
+       DIR *dir = opendir(path);
+       struct dirent *e;
+       int ret = 0;
+
+       if (!dir)
+               return -1;
+       if (path[len-1] != '/')
+               path[len++] = '/';
+       while ((e = readdir(dir)) != NULL) {
+               struct stat st;
+               int namlen;
+               if ((e->d_name[0] == '.') &&
+                   ((e->d_name[1] == 0) ||
+                    ((e->d_name[1] == '.') && e->d_name[2] == 0)))
+                       continue; /* "." and ".." */
+
+               namlen = strlen(e->d_name);
+               if ((len + namlen < PATH_MAX) &&
+                   strcpy(path + len, e->d_name) &&
+                   !lstat(path, &st) &&
+                   S_ISDIR(st.st_mode) &&
+                   remove_empty_dir_recursive(path, len + namlen))
+                       continue; /* happy */
+
+               /* path too long, stat fails, or non-directory still exists */
+               ret = -1;
+               break;
+       }
+       closedir(dir);
+       if (!ret) {
+               path[len] = 0;
+               ret = rmdir(path);
+       }
+       return ret;
+}
+
+static int remove_empty_directories(char *file)
+{
+       /* we want to create a file but there is a directory there;
+        * if that is an empty directory (or a directory that contains
+        * only empty directories), remove them.
+        */
+       char path[PATH_MAX];
+       int len = strlen(file);
+
+       if (len >= PATH_MAX) /* path too long ;-) */
+               return -1;
+       strcpy(path, file);
+       return remove_empty_dir_recursive(path, len);
+}
+
 static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1)
 {
        char *ref_file;
@@ -485,6 +538,17 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
        lock->lock_fd = -1;
 
        ref = resolve_ref(ref, lock->old_sha1, mustexist, NULL);
+       if (!ref && errno == EISDIR) {
+               /* we are trying to lock foo but we used to
+                * have foo/bar which now does not exist;
+                * it is normal for the empty directory 'foo'
+                * to remain.
+                */
+               ref_file = git_path("%s", orig_ref);
+               if (remove_empty_directories(ref_file))
+                       die("there are still refs under '%s'", orig_ref);
+               ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, NULL);
+       }
        if (!ref) {
                int last_errno = errno;
                error("unable to resolve reference %s: %s",