ref locking: allow 'foo' when 'foo/bar' used to exist but not anymore.
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>
Junio C Hamano committed
Sep 30, 2006 at 02:25 UTC
bc7127ef0f81d996e9691c6c898b926867aae89f
1 file changed
+64
refs.c
+64
@@ -473,6 +473,59 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,
473
return lock;
474
}
475
476
+static int remove_empty_dir_recursive(char *path, int len)
477
+{
478
+ DIR *dir = opendir(path);
479
+ struct dirent *e;
480
+ int ret = 0;
481
+
482
+ if (!dir)
483
+ return -1;
484
+ if (path[len-1] != '/')
485
+ path[len++] = '/';
486
+ while ((e = readdir(dir)) != NULL) {
487
+ struct stat st;
488
+ int namlen;
489
+ if ((e->d_name[0] == '.') &&
490
+ ((e->d_name[1] == 0) ||
491
+ ((e->d_name[1] == '.') && e->d_name[2] == 0)))
492
+ continue; /* "." and ".." */
493
+
494
+ namlen = strlen(e->d_name);
495
+ if ((len + namlen < PATH_MAX) &&
496
+ strcpy(path + len, e->d_name) &&
497
+ !lstat(path, &st) &&
498
+ S_ISDIR(st.st_mode) &&
499
+ remove_empty_dir_recursive(path, len + namlen))
500
+ continue; /* happy */
501
+
502
+ /* path too long, stat fails, or non-directory still exists */
503
+ ret = -1;
504
+ break;
505
+ }
506
+ closedir(dir);
507
+ if (!ret) {
508
+ path[len] = 0;
509
+ ret = rmdir(path);
510
+ }
511
+ return ret;
512
+}
513
+
514
+static int remove_empty_directories(char *file)
515
+{
516
+ /* we want to create a file but there is a directory there;
517
+ * if that is an empty directory (or a directory that contains
518
+ * only empty directories), remove them.
519
+ */
520
+ char path[PATH_MAX];
521
+ int len = strlen(file);
522
+
523
+ if (len >= PATH_MAX) /* path too long ;-) */
524
+ return -1;
525
+ strcpy(path, file);
526
+ return remove_empty_dir_recursive(path, len);
527
+}
528
+
529
static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1)
530
{
531
char *ref_file;
@@ -485,6 +538,17 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
538
lock->lock_fd = -1;
539
540
ref = resolve_ref(ref, lock->old_sha1, mustexist, NULL);
541
+ if (!ref && errno == EISDIR) {
542
+ /* we are trying to lock foo but we used to
543
+ * have foo/bar which now does not exist;
544
+ * it is normal for the empty directory 'foo'
545
+ * to remain.
546
+ */
547
+ ref_file = git_path("%s", orig_ref);
548
+ if (remove_empty_directories(ref_file))
549
+ die("there are still refs under '%s'", orig_ref);
550
+ ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, NULL);
551
+ }
552
if (!ref) {
553
int last_errno = errno;
554
error("unable to resolve reference %s: %s",