delete_refs(): add a flags argument
[gitweb.git] / refs / files-backend.c
index 7bc18322aaae00aada50a2842de09ed4e8efecc0..e15f7ae68bc005dd08eedbc628e1d79bab6e7944 100644 (file)
@@ -7,7 +7,6 @@
 
 struct ref_lock {
        char *ref_name;
-       char *orig_ref_name;
        struct lock_file *lk;
        struct object_id old_oid;
 };
@@ -543,7 +542,7 @@ static struct ref_entry *current_ref;
 typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
 
 struct ref_entry_cb {
-       const char *base;
+       const char *prefix;
        int trim;
        int flags;
        each_ref_fn *fn;
@@ -560,7 +559,7 @@ static int do_one_ref(struct ref_entry *entry, void *cb_data)
        struct ref_entry *old_current_ref;
        int retval;
 
-       if (!starts_with(entry->name, data->base))
+       if (!starts_with(entry->name, data->prefix))
                return 0;
 
        if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
@@ -1522,7 +1521,6 @@ static void unlock_ref(struct ref_lock *lock)
        if (lock->lk)
                rollback_lock_file(lock->lk);
        free(lock->ref_name);
-       free(lock->orig_ref_name);
        free(lock);
 }
 
@@ -1576,7 +1574,6 @@ static int lock_raw_ref(const char *refname, int mustexist,
        *lock_p = lock = xcalloc(1, sizeof(*lock));
 
        lock->ref_name = xstrdup(refname);
-       lock->orig_ref_name = xstrdup(refname);
        strbuf_git_path(&ref_file, "%s", refname);
 
 retry:
@@ -1827,12 +1824,12 @@ int peel_ref(const char *refname, unsigned char *sha1)
 
 /*
  * Call fn for each reference in the specified ref_cache, omitting
- * references not in the containing_dir of base.  fn is called for all
- * references, including broken ones.  If fn ever returns a non-zero
+ * references not in the containing_dir of prefix. Call fn for all
+ * references, including broken ones. If fn ever returns a non-zero
  * value, stop the iteration and return that value; otherwise, return
  * 0.
  */
-static int do_for_each_entry(struct ref_cache *refs, const char *base,
+static int do_for_each_entry(struct ref_cache *refs, const char *prefix,
                             each_ref_entry_fn fn, void *cb_data)
 {
        struct packed_ref_cache *packed_ref_cache;
@@ -1849,8 +1846,8 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
         * disk.
         */
        loose_dir = get_loose_refs(refs);
-       if (base && *base) {
-               loose_dir = find_containing_dir(loose_dir, base, 0);
+       if (prefix && *prefix) {
+               loose_dir = find_containing_dir(loose_dir, prefix, 0);
        }
        if (loose_dir)
                prime_ref_dir(loose_dir);
@@ -1858,8 +1855,8 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
        packed_ref_cache = get_packed_ref_cache(refs);
        acquire_packed_ref_cache(packed_ref_cache);
        packed_dir = get_packed_ref_dir(packed_ref_cache);
-       if (base && *base) {
-               packed_dir = find_containing_dir(packed_dir, base, 0);
+       if (prefix && *prefix) {
+               packed_dir = find_containing_dir(packed_dir, prefix, 0);
        }
 
        if (packed_dir && loose_dir) {
@@ -1881,23 +1878,14 @@ static int do_for_each_entry(struct ref_cache *refs, const char *base,
        return retval;
 }
 
-/*
- * Call fn for each reference in the specified ref_cache for which the
- * refname begins with base.  If trim is non-zero, then trim that many
- * characters off the beginning of each refname before passing the
- * refname to fn.  flags can be DO_FOR_EACH_INCLUDE_BROKEN to include
- * broken references in the iteration.  If fn ever returns a non-zero
- * value, stop the iteration and return that value; otherwise, return
- * 0.
- */
-int do_for_each_ref(const char *submodule, const char *base,
+int do_for_each_ref(const char *submodule, const char *prefix,
                    each_ref_fn fn, int trim, int flags, void *cb_data)
 {
        struct ref_entry_cb data;
        struct ref_cache *refs;
 
        refs = get_ref_cache(submodule);
-       data.base = base;
+       data.prefix = prefix;
        data.trim = trim;
        data.flags = flags;
        data.fn = fn;
@@ -1908,7 +1896,7 @@ int do_for_each_ref(const char *submodule, const char *base,
        if (ref_paranoia)
                data.flags |= DO_FOR_EACH_INCLUDE_BROKEN;
 
-       return do_for_each_entry(refs, base, do_one_ref, &data);
+       return do_for_each_entry(refs, prefix, do_one_ref, &data);
 }
 
 /*
@@ -1969,14 +1957,13 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                                            struct strbuf *err)
 {
        struct strbuf ref_file = STRBUF_INIT;
-       struct strbuf orig_ref_file = STRBUF_INIT;
-       const char *orig_refname = refname;
        struct ref_lock *lock;
        int last_errno = 0;
-       int lflags = 0;
+       int lflags = LOCK_NO_DEREF;
        int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
-       int resolve_flags = 0;
+       int resolve_flags = RESOLVE_REF_NO_RECURSE;
        int attempts_remaining = 3;
+       int resolved;
 
        assert(err);
 
@@ -1986,46 +1973,39 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                resolve_flags |= RESOLVE_REF_READING;
        if (flags & REF_DELETING)
                resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
-       if (flags & REF_NODEREF) {
-               resolve_flags |= RESOLVE_REF_NO_RECURSE;
-               lflags |= LOCK_NO_DEREF;
-       }
 
-       refname = resolve_ref_unsafe(refname, resolve_flags,
-                                    lock->old_oid.hash, type);
-       if (!refname && errno == EISDIR) {
+       strbuf_git_path(&ref_file, "%s", refname);
+       resolved = !!resolve_ref_unsafe(refname, resolve_flags,
+                                       lock->old_oid.hash, type);
+       if (!resolved && 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.
                 */
-               strbuf_git_path(&orig_ref_file, "%s", orig_refname);
-               if (remove_empty_directories(&orig_ref_file)) {
+               if (remove_empty_directories(&ref_file)) {
                        last_errno = errno;
-                       if (!verify_refname_available_dir(orig_refname, extras, skip,
+                       if (!verify_refname_available_dir(refname, extras, skip,
                                                          get_loose_refs(&ref_cache), err))
                                strbuf_addf(err, "there are still refs under '%s'",
-                                           orig_refname);
+                                           refname);
                        goto error_return;
                }
-               refname = resolve_ref_unsafe(orig_refname, resolve_flags,
-                                            lock->old_oid.hash, type);
+               resolved = !!resolve_ref_unsafe(refname, resolve_flags,
+                                               lock->old_oid.hash, type);
        }
-       if (!refname) {
+       if (!resolved) {
                last_errno = errno;
                if (last_errno != ENOTDIR ||
-                   !verify_refname_available_dir(orig_refname, extras, skip,
+                   !verify_refname_available_dir(refname, extras, skip,
                                                  get_loose_refs(&ref_cache), err))
                        strbuf_addf(err, "unable to resolve reference '%s': %s",
-                                   orig_refname, strerror(last_errno));
+                                   refname, strerror(last_errno));
 
                goto error_return;
        }
 
-       if (flags & REF_NODEREF)
-               refname = orig_refname;
-
        /*
         * If the ref did not exist and we are creating it, make sure
         * there is no existing packed ref whose name begins with our
@@ -2042,8 +2022,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
        lock->lk = xcalloc(1, sizeof(struct lock_file));
 
        lock->ref_name = xstrdup(refname);
-       lock->orig_ref_name = xstrdup(orig_refname);
-       strbuf_git_path(&ref_file, "%s", refname);
 
  retry:
        switch (safe_create_leading_directories_const(ref_file.buf)) {
@@ -2086,7 +2064,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 
  out:
        strbuf_release(&ref_file);
-       strbuf_release(&orig_ref_file);
        errno = last_errno;
        return lock;
 }
@@ -2426,7 +2403,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
        return 0;
 }
 
-int delete_refs(struct string_list *refnames)
+int delete_refs(struct string_list *refnames, unsigned int flags)
 {
        struct strbuf err = STRBUF_INIT;
        int i, result = 0;
@@ -2455,7 +2432,7 @@ int delete_refs(struct string_list *refnames)
        for (i = 0; i < refnames->nr; i++) {
                const char *refname = refnames->items[i].string;
 
-               if (delete_ref(refname, NULL, 0))
+               if (delete_ref(refname, NULL, flags))
                        result |= error(_("could not remove reference %s"), refname);
        }
 
@@ -2546,7 +2523,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
                                 const unsigned char *sha1, struct strbuf *err);
 static int commit_ref_update(struct ref_lock *lock,
                             const unsigned char *sha1, const char *logmsg,
-                            int flags, struct strbuf *err);
+                            struct strbuf *err);
 
 int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
 {
@@ -2622,7 +2599,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        hashcpy(lock->old_oid.hash, orig_sha1);
 
        if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
-           commit_ref_update(lock, orig_sha1, logmsg, 0, &err)) {
+           commit_ref_update(lock, orig_sha1, logmsg, &err)) {
                error("unable to write current sha1 into %s: %s", newrefname, err.buf);
                strbuf_release(&err);
                goto rollback;
@@ -2642,7 +2619,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        flag = log_all_ref_updates;
        log_all_ref_updates = 0;
        if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
-           commit_ref_update(lock, orig_sha1, NULL, 0, &err)) {
+           commit_ref_update(lock, orig_sha1, NULL, &err)) {
                error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
                strbuf_release(&err);
        }
@@ -2880,12 +2857,10 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
  */
 static int commit_ref_update(struct ref_lock *lock,
                             const unsigned char *sha1, const char *logmsg,
-                            int flags, struct strbuf *err)
+                            struct strbuf *err)
 {
        clear_loose_ref_cache(&ref_cache);
-       if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0 ||
-           (strcmp(lock->ref_name, lock->orig_ref_name) &&
-            log_ref_write(lock->orig_ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0)) {
+       if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {
                char *old_msg = strbuf_detach(err, NULL);
                strbuf_addf(err, "cannot update the ref '%s': %s",
                            lock->ref_name, old_msg);
@@ -2893,7 +2868,8 @@ static int commit_ref_update(struct ref_lock *lock,
                unlock_ref(lock);
                return -1;
        }
-       if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
+
+       if (strcmp(lock->ref_name, "HEAD") != 0) {
                /*
                 * Special hack: If a branch is updated directly and HEAD
                 * points to it (may happen on the remote side of a push
@@ -2909,6 +2885,7 @@ static int commit_ref_update(struct ref_lock *lock,
                unsigned char head_sha1[20];
                int head_flag;
                const char *head_ref;
+
                head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
                                              head_sha1, &head_flag);
                if (head_ref && (head_flag & REF_ISSYMREF) &&
@@ -2921,7 +2898,8 @@ static int commit_ref_update(struct ref_lock *lock,
                        }
                }
        }
-       if (!(flags & REF_LOG_ONLY) && commit_ref(lock)) {
+
+       if (commit_ref(lock)) {
                strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
                unlock_ref(lock);
                return -1;
@@ -3026,7 +3004,6 @@ int set_worktree_head_symref(const char *gitdir, const char *target)
        lock = xcalloc(1, sizeof(struct ref_lock));
        lock->lk = &head_lock;
        lock->ref_name = xstrdup(head_rel);
-       lock->orig_ref_name = xstrdup(head_rel);
 
        ret = create_symref_locked(lock, head_rel, target, NULL);
 
@@ -3376,14 +3353,32 @@ static int split_symref_update(struct ref_update *update,
                        update->new_sha1, update->old_sha1,
                        update->msg);
 
-       /* Change the symbolic ref update to log only: */
+       new_update->parent_update = update;
+
+       /*
+        * Change the symbolic ref update to log only. Also, it
+        * doesn't need to check its old SHA-1 value, as that will be
+        * done when new_update is processed.
+        */
        update->flags |= REF_LOG_ONLY | REF_NODEREF;
+       update->flags &= ~REF_HAVE_OLD;
 
        item->util = new_update;
 
        return 0;
 }
 
+/*
+ * Return the refname under which update was originally requested.
+ */
+static const char *original_update_refname(struct ref_update *update)
+{
+       while (update->parent_update)
+               update = update->parent_update;
+
+       return update->refname;
+}
+
 /*
  * Prepare for carrying out update:
  * - Lock the reference referred to by update.
@@ -3437,44 +3432,74 @@ static int lock_ref_for_update(struct ref_update *update,
        lock = update->lock;
 
        if (update->type & REF_ISSYMREF) {
-               if (read_ref_full(update->refname,
-                                 mustexist ? RESOLVE_REF_READING : 0,
-                                 lock->old_oid.hash, NULL)) {
-                       if (update->flags & REF_HAVE_OLD) {
-                               strbuf_addf(err, "cannot lock ref '%s': can't resolve old value",
-                                           update->refname);
+               if (update->flags & REF_NODEREF) {
+                       /*
+                        * We won't be reading the referent as part of
+                        * the transaction, so we have to read it here
+                        * to record and possibly check old_sha1:
+                        */
+                       if (read_ref_full(update->refname,
+                                         mustexist ? RESOLVE_REF_READING : 0,
+                                         lock->old_oid.hash, NULL)) {
+                               if (update->flags & REF_HAVE_OLD) {
+                                       strbuf_addf(err, "cannot lock ref '%s': "
+                                                   "can't resolve old value",
+                                                   update->refname);
+                                       return TRANSACTION_GENERIC_ERROR;
+                               } else {
+                                       hashclr(lock->old_oid.hash);
+                               }
+                       }
+                       if ((update->flags & REF_HAVE_OLD) &&
+                           hashcmp(lock->old_oid.hash, update->old_sha1)) {
+                               strbuf_addf(err, "cannot lock ref '%s': "
+                                           "is at %s but expected %s",
+                                           update->refname,
+                                           sha1_to_hex(lock->old_oid.hash),
+                                           sha1_to_hex(update->old_sha1));
                                return TRANSACTION_GENERIC_ERROR;
-                       } else {
-                               hashclr(lock->old_oid.hash);
                        }
-               }
-               if ((update->flags & REF_HAVE_OLD) &&
-                   hashcmp(lock->old_oid.hash, update->old_sha1)) {
-                       strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
-                                   update->refname,
-                                   sha1_to_hex(lock->old_oid.hash),
-                                   sha1_to_hex(update->old_sha1));
-                       return TRANSACTION_GENERIC_ERROR;
-               }
 
-               if (!(update->flags & REF_NODEREF)) {
+               } else {
+                       /*
+                        * Create a new update for the reference this
+                        * symref is pointing at. Also, we will record
+                        * and verify old_sha1 for this update as part
+                        * of processing the split-off update, so we
+                        * don't have to do it here.
+                        */
                        ret = split_symref_update(update, referent.buf, transaction,
                                                  affected_refnames, err);
                        if (ret)
                                return ret;
                }
-       } else if ((update->flags & REF_HAVE_OLD) &&
-                  hashcmp(lock->old_oid.hash, update->old_sha1)) {
-               if (is_null_sha1(update->old_sha1))
-                       strbuf_addf(err, "cannot lock ref '%s': reference already exists",
-                                   update->refname);
-               else
-                       strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
-                                   update->refname,
-                                   sha1_to_hex(lock->old_oid.hash),
-                                   sha1_to_hex(update->old_sha1));
+       } else {
+               struct ref_update *parent_update;
+
+               /*
+                * If this update is happening indirectly because of a
+                * symref update, record the old SHA-1 in the parent
+                * update:
+                */
+               for (parent_update = update->parent_update;
+                    parent_update;
+                    parent_update = parent_update->parent_update) {
+                       oidcpy(&parent_update->lock->old_oid, &lock->old_oid);
+               }
 
-               return TRANSACTION_GENERIC_ERROR;
+               if ((update->flags & REF_HAVE_OLD) &&
+                   hashcmp(lock->old_oid.hash, update->old_sha1)) {
+                       if (is_null_sha1(update->old_sha1))
+                               strbuf_addf(err, "cannot lock ref '%s': reference already exists",
+                                           original_update_refname(update));
+                       else
+                               strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
+                                           original_update_refname(update),
+                                           sha1_to_hex(lock->old_oid.hash),
+                                           sha1_to_hex(update->old_sha1));
+
+                       return TRANSACTION_GENERIC_ERROR;
+               }
        }
 
        if ((update->flags & REF_HAVE_NEW) &&