try_remove_empty_parents(): teach to remove parents of reflogs, too
[gitweb.git] / refs / files-backend.c
index 846380f38d7e5b547ec5d2708daae36f11c019fe..bce00223459b16bbdb319b9efd68fdf4665e33d2 100644 (file)
@@ -2280,15 +2280,25 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
        return 0;
 }
 
+enum {
+       REMOVE_EMPTY_PARENTS_REF = 0x01,
+       REMOVE_EMPTY_PARENTS_REFLOG = 0x02
+};
+
 /*
- * Remove empty parents, but spare refs/ and immediate subdirs.
- * Note: munges *name.
+ * Remove empty parent directories associated with the specified
+ * reference and/or its reflog, but spare [logs/]refs/ and immediate
+ * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
+ * REMOVE_EMPTY_PARENTS_REFLOG.
  */
-static void try_remove_empty_parents(char *name)
+static void try_remove_empty_parents(const char *refname, unsigned int flags)
 {
+       struct strbuf buf = STRBUF_INIT;
        char *p, *q;
        int i;
-       p = name;
+
+       strbuf_addstr(&buf, refname);
+       p = buf.buf;
        for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
                while (*p && *p != '/')
                        p++;
@@ -2296,19 +2306,23 @@ static void try_remove_empty_parents(char *name)
                while (*p == '/')
                        p++;
        }
-       for (q = p; *q; q++)
-               ;
-       while (1) {
+       q = buf.buf + buf.len;
+       while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
                while (q > p && *q != '/')
                        q--;
                while (q > p && *(q-1) == '/')
                        q--;
                if (q == p)
                        break;
-               *q = '\0';
-               if (rmdir(git_path("%s", name)))
-                       break;
+               strbuf_setlen(&buf, q - buf.buf);
+               if ((flags & REMOVE_EMPTY_PARENTS_REF) &&
+                   rmdir(git_path("%s", buf.buf)))
+                       flags &= ~REMOVE_EMPTY_PARENTS_REF;
+               if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) &&
+                   rmdir(git_path("logs/%s", buf.buf)))
+                       flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
        }
+       strbuf_release(&buf);
 }
 
 /* make sure nobody touched the ref, and unlink */
@@ -2332,7 +2346,7 @@ static void prune_ref(struct ref_to_prune *r)
        }
        ref_transaction_free(transaction);
        strbuf_release(&err);
-       try_remove_empty_parents(r->name);
+       try_remove_empty_parents(r->name, REMOVE_EMPTY_PARENTS_REF);
 }
 
 static void prune_refs(struct ref_to_prune *r)
@@ -2421,24 +2435,6 @@ static int repack_without_refs(struct files_ref_store *refs,
        return ret;
 }
 
-static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
-{
-       assert(err);
-
-       if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
-               /*
-                * loose.  The loose file name is the same as the
-                * lockfile name, minus ".lock":
-                */
-               char *loose_filename = get_locked_file_path(lock->lk);
-               int res = unlink_or_msg(loose_filename, err);
-               free(loose_filename);
-               if (res)
-                       return 1;
-       }
-       return 0;
-}
-
 static int files_delete_refs(struct ref_store *ref_store,
                             struct string_list *refnames, unsigned int flags)
 {
@@ -2821,9 +2817,9 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
        return 0;
 }
 
-static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
-                          const unsigned char *new_sha1, const char *msg,
-                          int flags, struct strbuf *err)
+int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
+                       const unsigned char *new_sha1, const char *msg,
+                       int flags, struct strbuf *err)
 {
        int logfd, result;
 
@@ -2858,13 +2854,6 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
        return 0;
 }
 
-int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
-                       const unsigned char *new_sha1, const char *msg,
-                       int flags, struct strbuf *err)
-{
-       return log_ref_write_1(refname, old_sha1, new_sha1, msg, flags, err);
-}
-
 /*
  * Write sha1 into the open lockfile, then close the lockfile. On
  * errors, rollback the lockfile, fill in *err and
@@ -3798,9 +3787,13 @@ static int files_transaction_commit(struct ref_store *ref_store,
 
                if (update->flags & REF_DELETING &&
                    !(update->flags & REF_LOG_ONLY)) {
-                       if (delete_ref_loose(lock, update->type, err)) {
-                               ret = TRANSACTION_GENERIC_ERROR;
-                               goto cleanup;
+                       if (!(update->type & REF_ISPACKED) ||
+                           update->type & REF_ISSYMREF) {
+                               /* It is a loose reference. */
+                               if (unlink_or_msg(git_path("%s", lock->ref_name), err)) {
+                                       ret = TRANSACTION_GENERIC_ERROR;
+                                       goto cleanup;
+                               }
                        }
 
                        if (!(update->flags & REF_ISPRUNING))