aboutsummaryrefslogtreecommitdiff
path: root/refs.c
diff options
context:
space:
mode:
authorMichael Haggerty <mhagger@alum.mit.edu>2014-04-07 15:48:10 +0200
committerJunio C Hamano <gitster@pobox.com>2014-04-07 12:09:14 -0700
commitcaa4046c4f480ceae5afb20e3172a437865cc51f (patch)
tree6b1af26f8d1ad2cae7b4f03e53451aafeb82a088 /refs.c
parentf11b09fb60556954c6a222f4809631470c81cae6 (diff)
downloadgit-caa4046c4f480ceae5afb20e3172a437865cc51f.tar.gz
git-caa4046c4f480ceae5afb20e3172a437865cc51f.tar.xz
refs: add a concept of a reference transaction
Build out the API for dealing with a bunch of reference checks and changes within a transaction. Define an opaque ref_transaction type that is managed entirely within refs.c. Introduce functions for beginning a transaction, adding updates to a transaction, and committing/rolling back a transaction. This API will soon replace update_refs(). Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/refs.c b/refs.c
index 1305eb1fc..f0b5764b3 100644
--- a/refs.c
+++ b/refs.c
@@ -3267,6 +3267,96 @@ static int update_ref_write(const char *action, const char *refname,
return 0;
}
+/*
+ * Data structure for holding a reference transaction, which can
+ * consist of checks and updates to multiple references, carried out
+ * as atomically as possible. This structure is opaque to callers.
+ */
+struct ref_transaction {
+ struct ref_update **updates;
+ size_t alloc;
+ size_t nr;
+};
+
+struct ref_transaction *ref_transaction_begin(void)
+{
+ return xcalloc(1, sizeof(struct ref_transaction));
+}
+
+static void ref_transaction_free(struct ref_transaction *transaction)
+{
+ int i;
+
+ for (i = 0; i < transaction->nr; i++) {
+ struct ref_update *update = transaction->updates[i];
+
+ free((char *)update->ref_name);
+ free(update);
+ }
+
+ free(transaction->updates);
+ free(transaction);
+}
+
+void ref_transaction_rollback(struct ref_transaction *transaction)
+{
+ ref_transaction_free(transaction);
+}
+
+static struct ref_update *add_update(struct ref_transaction *transaction,
+ const char *refname)
+{
+ struct ref_update *update = xcalloc(1, sizeof(*update));
+
+ update->ref_name = xstrdup(refname);
+ ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
+ transaction->updates[transaction->nr++] = update;
+ return update;
+}
+
+void ref_transaction_update(struct ref_transaction *transaction,
+ const char *refname,
+ unsigned char *new_sha1, unsigned char *old_sha1,
+ int flags, int have_old)
+{
+ struct ref_update *update = add_update(transaction, refname);
+
+ hashcpy(update->new_sha1, new_sha1);
+ update->flags = flags;
+ update->have_old = have_old;
+ if (have_old)
+ hashcpy(update->old_sha1, old_sha1);
+}
+
+void ref_transaction_create(struct ref_transaction *transaction,
+ const char *refname,
+ unsigned char *new_sha1,
+ int flags)
+{
+ struct ref_update *update = add_update(transaction, refname);
+
+ assert(!is_null_sha1(new_sha1));
+ hashcpy(update->new_sha1, new_sha1);
+ hashclr(update->old_sha1);
+ update->flags = flags;
+ update->have_old = 1;
+}
+
+void ref_transaction_delete(struct ref_transaction *transaction,
+ const char *refname,
+ unsigned char *old_sha1,
+ int flags, int have_old)
+{
+ struct ref_update *update = add_update(transaction, refname);
+
+ update->flags = flags;
+ update->have_old = have_old;
+ if (have_old) {
+ assert(!is_null_sha1(old_sha1));
+ hashcpy(update->old_sha1, old_sha1);
+ }
+}
+
int update_ref(const char *action, const char *refname,
const unsigned char *sha1, const unsigned char *oldval,
int flags, enum action_on_err onerr)
@@ -3378,6 +3468,15 @@ cleanup:
return ret;
}
+int ref_transaction_commit(struct ref_transaction *transaction,
+ const char *msg, enum action_on_err onerr)
+{
+ int ret = update_refs(msg, transaction->updates, transaction->nr,
+ onerr);
+ ref_transaction_free(transaction);
+ return ret;
+}
+
char *shorten_unambiguous_ref(const char *refname, int strict)
{
int i;