From: David Turner <dturner@twopensource.com>
To: git@vger.kernel.org, mhagger@alum.mit.edu
Cc: David Turner <dturner@twopensource.com>
Subject: [PATCH v3 39/44] refs: always handle non-normal refs in files backend
Date: Mon, 12 Oct 2015 17:52:00 -0400 [thread overview]
Message-ID: <1444686725-27660-41-git-send-email-dturner@twopensource.com> (raw)
In-Reply-To: <1444686725-27660-1-git-send-email-dturner@twopensource.com>
Always handle non-normal (per-worktree or pseudo) refs in the files
backend instead of alternate backends.
Sometimes a ref transaction will update both a per-worktree ref and a
normal ref. For instance, an ordinary commit might update
refs/heads/master and HEAD (or at least HEAD's reflog).
We handle three cases here:
1. updates to normal refs continue to go through the chosen backend
2. updates to non-normal refs with REF_NODEREF or to non-symbolic refs
are moved to a separate files backend transaction.
3. updates to symbolic refs are dereferenced to their base ref. The
update to the base ref then goes through the ordinary backend, while
the files backend is directly called to update the symref's reflog.
Signed-off-by: David Turner <dturner@twopensource.com>
---
refs.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 139 insertions(+), 2 deletions(-)
diff --git a/refs.c b/refs.c
index b00f910..025cc08 100644
--- a/refs.c
+++ b/refs.c
@@ -7,6 +7,11 @@
#include "object.h"
#include "tag.h"
+const char split_transaction_fail_warning[] =
+ "A ref transaction was split across two refs backends. Part of the "
+ "transaction succeeded, but then the update to the per-worktree refs "
+ "failed. Your repository may be in an inconsistent state.";
+
/*
* We always have a files backend and it is the default.
*/
@@ -963,6 +968,13 @@ void ref_transaction_free(struct ref_transaction *transaction)
free(transaction);
}
+static void add_update_obj(struct ref_transaction *transaction,
+ struct ref_update *update)
+{
+ ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
+ transaction->updates[transaction->nr++] = update;
+}
+
static struct ref_update *add_update(struct ref_transaction *transaction,
const char *refname)
{
@@ -970,8 +982,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
struct ref_update *update = xcalloc(1, sizeof(*update) + len);
memcpy((char *)update->refname, refname, len); /* includes NUL */
- ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
- transaction->updates[transaction->nr++] = update;
+ add_update_obj(transaction, update);
return update;
}
@@ -1160,11 +1171,87 @@ int refs_initdb(struct strbuf *err, int shared)
return the_refs_backend->initdb(err, shared);
}
+/*
+ * Special case for non-normal refs. For symbolic-refs when
+ * REF_NODEREF is not turned on, we dereference them here and replace
+ * updates to the symbolic refs with updates to the underlying ref.
+ * Then we do our own reflogging for the symbolic ref.
+ *
+ * We move other non-normal ref updates with into a specially-created
+ * files-backend transaction
+ */
+static int move_abnormal_ref_updates(struct ref_transaction *transaction,
+ struct ref_transaction *files_transaction,
+ struct string_list *symrefs)
+{
+ int i;
+
+ for (i = 0; i < transaction->nr; i++) {
+ struct ref_update *update = transaction->updates[i];
+ const char *resolved;
+ int flags = 0;
+ unsigned char sha1[20];
+
+ if (ref_type(update->refname) == REF_TYPE_NORMAL)
+ continue;
+
+ resolved = resolve_ref_unsafe(update->refname, 0, sha1, &flags);
+
+ if (update->flags & REF_NODEREF || !(flags & REF_ISSYMREF)) {
+ int last;
+
+ add_update_obj(files_transaction, update);
+ /*
+ * Replace this transaction with the
+ * last transaction, removing it from
+ * the list of backend transactions
+ */
+ last = --transaction->nr;
+ transaction->updates[i] = transaction->updates[last];
+ continue;
+ }
+
+ if (resolved) {
+ struct ref_update *new_update;
+ struct string_list_item *item;
+
+ if (ref_type(resolved) != REF_TYPE_NORMAL)
+ die("Non-normal symbolic ref `%s` points to non-normal ref `%s`", update->refname, resolved);
+
+ new_update = xmalloc(sizeof(*new_update) +
+ strlen(resolved) + 1);
+ memcpy(new_update, update, sizeof(*update));
+
+ if (update->flags & REF_HAVE_OLD &&
+ hashcmp(sha1, update->old_sha1)) {
+ /* consistency check failed */
+ free(new_update);
+ return -1;
+ } else {
+ hashcpy(update->old_sha1, sha1);
+ }
+
+ strcpy((char *)new_update->refname, resolved);
+ transaction->updates[i] = new_update;
+
+ item = string_list_append(symrefs, update->refname);
+ item->util = new_update;
+ free(update);
+ }
+ }
+
+ return 0;
+}
+
int ref_transaction_commit(struct ref_transaction *transaction,
struct strbuf *err)
{
int ret = -1;
struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+ struct string_list files_affected_refnames = STRING_LIST_INIT_NODUP;
+ struct string_list symrefs = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+ struct ref_transaction *files_transaction = NULL;
assert(err);
@@ -1176,6 +1263,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,
return 0;
}
+ if (the_refs_backend != &refs_be_files) {
+ files_transaction = ref_transaction_begin(err);
+ if (!files_transaction)
+ die("%s", err->buf);
+
+ ret = move_abnormal_ref_updates(transaction, files_transaction,
+ &symrefs);
+ if (ret)
+ goto done;
+
+ /* files backend commit */
+ if (ref_update_reject_duplicates(files_transaction,
+ &files_affected_refnames,
+ err)) {
+ ret = TRANSACTION_GENERIC_ERROR;
+ goto done;
+ }
+ }
+
+ /* main backend commit */
if (ref_update_reject_duplicates(transaction, &affected_refnames, err)) {
ret = TRANSACTION_GENERIC_ERROR;
goto done;
@@ -1183,8 +1290,35 @@ int ref_transaction_commit(struct ref_transaction *transaction,
ret = the_refs_backend->transaction_commit(transaction,
&affected_refnames, err);
+ if (ret)
+ goto done;
+
+ if (the_refs_backend != &refs_be_files) {
+ ret = refs_be_files.transaction_commit(files_transaction,
+ &files_affected_refnames,
+ err);
+ if (ret) {
+ warning(split_transaction_fail_warning);
+ goto done;
+ }
+
+ /* reflogging for dereferenced symbolic refs */
+ for_each_string_list_item(item, &symrefs) {
+ struct ref_update *update = item->util;
+ if (files_log_ref_write(item->string, update->old_sha1,
+ update->new_sha1,
+ update->msg, update->flags, err))
+ warning("failed to log ref update for symref %s",
+ item->string);
+ }
+ }
+
done:
string_list_clear(&affected_refnames, 0);
+ string_list_clear(&files_affected_refnames, 0);
+ if (files_transaction)
+ ref_transaction_free(files_transaction);
+ string_list_clear(&symrefs, 0);
return ret;
}
@@ -1235,6 +1369,9 @@ int peel_ref(const char *refname, unsigned char *sha1)
int create_symref(const char *ref_target, const char *refs_heads_master,
const char *logmsg)
{
+ if (ref_type(ref_target) != REF_TYPE_NORMAL)
+ return refs_be_files.create_symref(ref_target, refs_heads_master,
+ logmsg);
return the_refs_backend->create_symref(ref_target, refs_heads_master,
logmsg);
}
--
2.4.2.644.g97b850b-twtrsrc
next prev parent reply other threads:[~2015-10-12 21:53 UTC|newest]
Thread overview: 71+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-12 21:51 [PATCH v3 00/44] alternate refs backends David Turner
2015-10-12 21:51 ` [PATCH v3 01/44] refs.c: create a public version of verify_refname_available David Turner
2015-10-13 2:28 ` Michael Haggerty
2015-10-12 21:51 ` [PATCH v3 02/44] refs: make repack_without_refs and is_branch public David Turner
2015-10-13 2:39 ` Michael Haggerty
2015-10-13 7:23 ` Michael Haggerty
2015-10-13 21:18 ` David Turner
2015-10-12 21:51 ` [PATCH v3 03/44] refs-be-files.c: rename refs to refs-be-files David Turner
2015-10-12 21:51 ` [PATCH v3 04/44] refs.c: add a new refs.c file to hold all common refs code David Turner
2015-10-12 21:51 ` [PATCH v3 05/44] refs.c: move update_ref to refs.c David Turner
2015-10-13 3:41 ` Michael Haggerty
2015-10-13 20:40 ` David Turner
2015-10-14 1:01 ` David Turner
2015-10-12 21:51 ` [PATCH v3 06/44] refs.c: move delete_ref and delete_refs to the common code David Turner
2015-10-13 3:51 ` Michael Haggerty
2015-10-12 21:51 ` [PATCH v3 07/44] refs.c: move read_ref_at to the common refs file David Turner
2015-10-12 21:51 ` [PATCH v3 08/44] refs.c: move the hidden refs functions to the common code David Turner
2015-10-12 21:51 ` [PATCH v3 09/44] refs.c: move dwim and friend functions to the common refs code David Turner
2015-10-12 21:51 ` [PATCH v3 10/44] refs.c: move warn_if_dangling_symref* to the common code David Turner
2015-10-12 21:51 ` [PATCH v3 11/44] refs.c: move read_ref, read_ref_full and ref_exists " David Turner
2015-10-12 21:51 ` [PATCH v3 12/44] refs.c: move resolve_refdup to common David Turner
2015-10-12 21:51 ` [PATCH v3 13/44] refs.c: move check_refname_format to the common code David Turner
2015-10-12 21:51 ` [PATCH v3 14/44] refs.c: move is_branch " David Turner
2015-10-12 21:51 ` [PATCH v3 15/44] refs.c: move prettify_refname " David Turner
2015-10-12 21:51 ` [PATCH v3 16/44] refs.c: move ref iterators " David Turner
2015-10-13 4:12 ` Michael Haggerty
2015-10-12 21:51 ` [PATCH v3 17/44] refs.c: move head_ref_namespaced " David Turner
2015-10-12 21:51 ` [PATCH v3 18/44] refs: move transaction functions into " David Turner
2015-10-13 6:15 ` Michael Haggerty
2015-10-13 11:29 ` Michael Haggerty
2015-10-13 22:21 ` David Turner
2015-10-13 23:05 ` David Turner
2015-10-12 21:51 ` [PATCH v3 19/44] refs: add a backend method structure with transaction functions David Turner
2015-10-12 21:51 ` [PATCH v3 19/44] refs-be-files.c: " David Turner
2015-10-12 22:25 ` David Turner
2015-10-13 7:56 ` Michael Haggerty
2015-10-13 18:28 ` David Turner
2015-10-13 21:12 ` Michael Haggerty
2015-10-12 21:51 ` [PATCH v3 20/44] refs-be-files.c: add methods for misc ref operations David Turner
2015-10-13 7:44 ` Michael Haggerty
2015-10-12 21:51 ` [PATCH v3 21/44] refs-be-files.c: add methods for the ref iterators David Turner
2015-10-12 21:51 ` [PATCH v3 22/44] refs-be-files.c: add method for for_each_reftype_ David Turner
2015-10-12 21:51 ` [PATCH v3 23/44] refs-be-files.c: add do_for_each_per_worktree_ref David Turner
2015-10-12 21:51 ` [PATCH v3 24/44] refs.c: move refname_is_safe to the common code David Turner
2015-10-12 21:51 ` [PATCH v3 25/44] refs.h: document make refname_is_safe and add it to header David Turner
2015-10-13 7:33 ` Michael Haggerty
2015-10-13 9:18 ` Michael Haggerty
2015-10-13 18:32 ` David Turner
2015-10-12 21:51 ` [PATCH v3 26/44] refs.c: move copy_msg to the common code David Turner
2015-10-13 9:25 ` Michael Haggerty
2015-10-12 21:51 ` [PATCH v3 27/44] refs.c: move peel_object " David Turner
2015-10-13 9:34 ` Michael Haggerty
2015-10-12 21:51 ` [PATCH v3 28/44] refs.c: move should_autocreate_reflog to " David Turner
2015-10-12 21:51 ` [PATCH v3 29/44] refs.c: add ref backend init function David Turner
2015-10-12 21:51 ` [PATCH v3 30/44] refs.c: add methods for reflog David Turner
2015-10-12 21:51 ` [PATCH v3 31/44] refs.c: add method for initial ref transaction commit David Turner
2015-10-12 21:51 ` [PATCH v3 32/44] initdb: move safe_create_dir into common code David Turner
2015-10-12 21:51 ` [PATCH v3 33/44] refs.c: add method for initializing refs db David Turner
2015-10-12 21:51 ` [PATCH v3 34/44] refs-be-files.c: add method to rename refs David Turner
2015-10-12 21:51 ` [PATCH v3 35/44] refs: make lock generic David Turner
2015-10-12 21:51 ` [PATCH v3 36/44] refs: make files_log_ref_write functions public David Turner
2015-10-12 21:51 ` [PATCH v3 37/44] refs: break out a ref conflict check David Turner
2015-10-13 10:25 ` Michael Haggerty
2015-10-13 18:46 ` David Turner
2015-10-12 21:51 ` [PATCH v3 38/44] refs: move duplicate check to common code David Turner
2015-10-12 21:52 ` David Turner [this message]
2015-10-12 21:52 ` [PATCH v3 40/44] refs: allow ref backend to be set for clone David Turner
2015-10-12 21:52 ` [PATCH v3 41/44] refs: add register_refs_backend David Turner
2015-10-12 21:52 ` [PATCH v3 42/44] introduce "extensions" form of core.repositoryformatversion David Turner
2015-10-12 21:52 ` [PATCH v3 43/44] refs: add LMDB refs backend David Turner
2015-10-12 21:52 ` [PATCH v3 44/44] refs: tests for db backend David Turner
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=1444686725-27660-41-git-send-email-dturner@twopensource.com \
--to=dturner@twopensource.com \
--cc=git@vger.kernel.org \
--cc=mhagger@alum.mit.edu \
/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;
as well as URLs for NNTP newsgroup(s).