Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <junkio@cox.net>
To: git@vger.kernel.org
Subject: [PATCH 1/6] ref locking: allow 'foo' when 'foo/bar' used to exist but not anymore.
Date: Sat, 30 Sep 2006 15:29:43 -0700	[thread overview]
Message-ID: <7vzmch9eeg.fsf@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: 7v1wptc7ou.fsf@assigned-by-dhcp.cox.net

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>
---
 * This and the next one are essentially the same as my previous
   two patch series.

 refs.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/refs.c b/refs.c
index 3d4cdd1..b433c0c 100644
--- a/refs.c
+++ b/refs.c
@@ -473,6 +473,59 @@ static struct ref_lock *verify_lock(stru
 	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_ba
 	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",
-- 
1.4.2.1.g5a98f

  reply	other threads:[~2006-09-30 22:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-30 20:01 [PATCH 1/2] Move code resolving packed refs into its own function Christian Couder
2006-09-30 20:33 ` Junio C Hamano
2006-09-30 22:26   ` [PATCH 0/6] ref deletion and D/F conflict avoidance with packed-refs Junio C Hamano
2006-09-30 22:29     ` Junio C Hamano [this message]
2006-09-30 22:30     ` [PATCH 2/6] refs: minor restructuring of cached refs data Junio C Hamano
2006-09-30 22:30     ` [PATCH 3/6] lock_ref_sha1(): do not sometimes error() and sometimes die() Junio C Hamano
2006-09-30 22:30     ` [PATCH 4/6] lock_ref_sha1(): check D/F conflict with packed ref when creating Junio C Hamano
2006-09-30 22:30     ` [PATCH 5/6] delete_ref(): delete packed ref Junio C Hamano
2006-09-30 22:30     ` [PATCH 6/6] git-branch: remove D/F check done by hand Junio C Hamano
2006-09-30 22:36     ` [PATCH 0/6] ref deletion and D/F conflict avoidance with packed-refs Jeff King
2006-10-01  4:06   ` [PATCH 1/2] Move code resolving packed refs into its own function Christian Couder
2006-10-01  9:58     ` Junio C Hamano
2006-10-03 12:10       ` Jakub Narebski
2006-10-03 17:39         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vzmch9eeg.fsf@assigned-by-dhcp.cox.net \
    --to=junkio@cox.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox