files_ref_store: put the packed files lock directly in this struct
[gitweb.git] / refs / files-backend.c
index 2c360a4107d343cb9d9d9ad3bfdfb9c05abb2e62..1db40432af1db5a648d8ea9fd2a6fced5a648f21 100644 (file)
@@ -43,15 +43,6 @@ struct packed_ref_cache {
         */
        unsigned int referrers;
 
-       /*
-        * Iff the packed-refs file associated with this instance is
-        * currently locked for writing, this points at the associated
-        * lock (which is owned by somebody else).  The referrer count
-        * is also incremented when the file is locked and decremented
-        * when it is unlocked.
-        */
-       struct lock_file *lock;
-
        /* The metadata from when this packed-refs cache was read */
        struct stat_validity validity;
 };
@@ -70,10 +61,13 @@ struct files_ref_store {
 
        struct ref_cache *loose;
        struct packed_ref_cache *packed;
-};
 
-/* Lock used for the main packed-refs file: */
-static struct lock_file packlock;
+       /*
+        * Lock used for the "packed-refs" file. Note that this (and
+        * thus the enclosing `files_ref_store`) must not be freed.
+        */
+       struct lock_file packed_refs_lock;
+};
 
 /*
  * Increment the reference count of *packed_refs.
@@ -104,8 +98,8 @@ static void clear_packed_ref_cache(struct files_ref_store *refs)
        if (refs->packed) {
                struct packed_ref_cache *packed_refs = refs->packed;
 
-               if (packed_refs->lock)
-                       die("internal error: packed-ref cache cleared while locked");
+               if (is_lock_file_locked(&refs->packed_refs_lock))
+                       die("BUG: packed-ref cache cleared while locked");
                refs->packed = NULL;
                release_packed_ref_cache(packed_refs);
        }
@@ -396,8 +390,8 @@ static void add_packed_ref(struct files_ref_store *refs,
 {
        struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
 
-       if (!packed_ref_cache->lock)
-               die("internal error: packed refs not locked");
+       if (!is_lock_file_locked(&refs->packed_refs_lock))
+               die("BUG: packed refs not locked");
        add_ref_entry(get_packed_ref_dir(packed_ref_cache),
                      create_ref_entry(refname, oid, REF_ISPACKED, 1));
 }
@@ -1290,7 +1284,7 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
        }
 
        if (hold_lock_file_for_update_timeout(
-                           &packlock, files_packed_refs_path(refs),
+                           &refs->packed_refs_lock, files_packed_refs_path(refs),
                            flags, timeout_value) < 0)
                return -1;
        /*
@@ -1300,7 +1294,6 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
         * the packed-refs file.
         */
        packed_ref_cache = get_packed_ref_cache(refs);
-       packed_ref_cache->lock = &packlock;
        /* Increment the reference count to prevent it from being freed: */
        acquire_packed_ref_cache(packed_ref_cache);
        return 0;
@@ -1323,10 +1316,10 @@ static int commit_packed_refs(struct files_ref_store *refs)
 
        files_assert_main_repository(refs, "commit_packed_refs");
 
-       if (!packed_ref_cache->lock)
-               die("internal error: packed-refs not locked");
+       if (!is_lock_file_locked(&refs->packed_refs_lock))
+               die("BUG: packed-refs not locked");
 
-       out = fdopen_lock_file(packed_ref_cache->lock, "w");
+       out = fdopen_lock_file(&refs->packed_refs_lock, "w");
        if (!out)
                die_errno("unable to fdopen packed-refs descriptor");
 
@@ -1344,11 +1337,10 @@ static int commit_packed_refs(struct files_ref_store *refs)
        if (ok != ITER_DONE)
                die("error while iterating over references");
 
-       if (commit_lock_file(packed_ref_cache->lock)) {
+       if (commit_lock_file(&refs->packed_refs_lock)) {
                save_errno = errno;
                error = -1;
        }
-       packed_ref_cache->lock = NULL;
        release_packed_ref_cache(packed_ref_cache);
        errno = save_errno;
        return error;
@@ -1366,10 +1358,9 @@ static void rollback_packed_refs(struct files_ref_store *refs)
 
        files_assert_main_repository(refs, "rollback_packed_refs");
 
-       if (!packed_ref_cache->lock)
-               die("internal error: packed-refs not locked");
-       rollback_lock_file(packed_ref_cache->lock);
-       packed_ref_cache->lock = NULL;
+       if (!is_lock_file_locked(&refs->packed_refs_lock))
+               die("BUG: packed-refs not locked");
+       rollback_lock_file(&refs->packed_refs_lock);
        release_packed_ref_cache(packed_ref_cache);
        clear_packed_ref_cache(refs);
 }
@@ -1595,7 +1586,7 @@ static int repack_without_refs(struct files_ref_store *refs,
        return ret;
 }
 
-static int files_delete_refs(struct ref_store *ref_store,
+static int files_delete_refs(struct ref_store *ref_store, const char *msg,
                             struct string_list *refnames, unsigned int flags)
 {
        struct files_ref_store *refs =
@@ -1627,7 +1618,7 @@ static int files_delete_refs(struct ref_store *ref_store,
        for (i = 0; i < refnames->nr; i++) {
                const char *refname = refnames->items[i].string;
 
-               if (refs_delete_ref(&refs->base, NULL, refname, NULL, flags))
+               if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))
                        result |= error(_("could not remove reference %s"), refname);
        }
 
@@ -2057,7 +2048,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
        struct object *o;
        int fd;
 
-       o = parse_object(oid->hash);
+       o = parse_object(oid);
        if (!o) {
                strbuf_addf(err,
                            "trying to write ref '%s' with nonexistent object %s",
@@ -2228,50 +2219,6 @@ static int files_create_symref(struct ref_store *ref_store,
        return ret;
 }
 
-int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
-{
-       /*
-        * FIXME: this obviously will not work well for future refs
-        * backends. This function needs to die.
-        */
-       struct files_ref_store *refs =
-               files_downcast(get_main_ref_store(),
-                              REF_STORE_WRITE,
-                              "set_head_symref");
-
-       static struct lock_file head_lock;
-       struct ref_lock *lock;
-       struct strbuf head_path = STRBUF_INIT;
-       const char *head_rel;
-       int ret;
-
-       strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
-       if (hold_lock_file_for_update(&head_lock, head_path.buf,
-                                     LOCK_NO_DEREF) < 0) {
-               struct strbuf err = STRBUF_INIT;
-               unable_to_lock_message(head_path.buf, errno, &err);
-               error("%s", err.buf);
-               strbuf_release(&err);
-               strbuf_release(&head_path);
-               return -1;
-       }
-
-       /* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
-          linked trees */
-       head_rel = remove_leading_path(head_path.buf,
-                                      absolute_path(get_git_common_dir()));
-       /* to make use of create_symref_locked(), initialize ref_lock */
-       lock = xcalloc(1, sizeof(struct ref_lock));
-       lock->lk = &head_lock;
-       lock->ref_name = xstrdup(head_rel);
-
-       ret = create_symref_locked(refs, lock, head_rel, target, logmsg);
-
-       unlock_ref(lock); /* will free lock */
-       strbuf_release(&head_path);
-       return ret;
-}
-
 static int files_reflog_exists(struct ref_store *ref_store,
                               const char *refname)
 {
@@ -2305,7 +2252,7 @@ static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *c
 {
        struct object_id ooid, noid;
        char *email_end, *message;
-       unsigned long timestamp;
+       timestamp_t timestamp;
        int tz;
        const char *p = sb->buf;
 
@@ -2315,7 +2262,7 @@ static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *c
            parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
            !(email_end = strchr(p, '>')) ||
            email_end[1] != ' ' ||
-           !(timestamp = strtoul(email_end + 2, &message, 10)) ||
+           !(timestamp = parse_timestamp(email_end + 2, &message, 10)) ||
            !message || message[0] != ' ' ||
            (message[1] != '+' && message[1] != '-') ||
            !isdigit(message[2]) || !isdigit(message[3]) ||
@@ -2894,7 +2841,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
        struct files_ref_store *refs =
                files_downcast(ref_store, REF_STORE_WRITE,
                               "ref_transaction_commit");
-       int ret = 0, i;
+       size_t i;
+       int ret = 0;
        struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
        struct string_list_item *ref_to_delete;
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
@@ -3101,7 +3049,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
        struct files_ref_store *refs =
                files_downcast(ref_store, REF_STORE_WRITE,
                               "initial_ref_transaction_commit");
-       int ret = 0, i;
+       size_t i;
+       int ret = 0;
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
 
        assert(err);
@@ -3187,7 +3136,7 @@ struct expire_reflog_cb {
 };
 
 static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
-                            const char *email, unsigned long timestamp, int tz,
+                            const char *email, timestamp_t timestamp, int tz,
                             const char *message, void *cb_data)
 {
        struct expire_reflog_cb *cb = cb_data;
@@ -3204,7 +3153,7 @@ static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
                        printf("prune %s", message);
        } else {
                if (cb->newlog) {
-                       fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s",
+                       fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s",
                                oid_to_hex(ooid), oid_to_hex(noid),
                                email, timestamp, tz, message);
                        oidcpy(&cb->last_kept_oid, noid);