git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v11 00/41] Use ref transactions
@ 2014-05-27 20:25 Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 01/41] refs.c: remove ref_transaction_rollback Ronnie Sahlberg
                   ` (41 more replies)
  0 siblings, 42 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

This patch series can also be found at
https://github.com/rsahlberg/git/tree/ref-transactions


This patch series is based on next and expands on the transaction API. It
converts all ref updates, inside refs.c as well as external, to use the
transaction API for updates. This makes most of the ref updates to become
atomic when there are failures locking or writing to a ref.

This version completes the work to convert all ref updates to use transactions.
Now that all updates are through transactions I will start working on
cleaning up the reading of refs and to create an api for managing reflogs but
all that will go in a different patch series.

Version 11:
 - Updates after JNs review of the series.

Ronnie Sahlberg (41):
  refs.c: remove ref_transaction_rollback
  refs.c: ref_transaction_commit should not free the transaction
  refs.c: constify the sha arguments for
    ref_transaction_create|delete|update
  refs.c: allow passing NULL to ref_transaction_free
  refs.c: add a strbuf argument to ref_transaction_commit for error
    logging
  refs.c: add an err argument to repack_without_refs
  refs.c: make ref_update_reject_duplicates take a strbuf argument for
    errors
  refs.c: add an err argument to delete_ref_loose
  refs.c: make update_ref_write update a strbuf on failure
  update-ref.c: log transaction error from the update_ref
  refs.c: remove the onerr argument to ref_transaction_commit
  refs.c: change ref_transaction_update() to do error checking and
    return status
  refs.c: change ref_transaction_create to do error checking and return
    status
  refs.c: update ref_transaction_delete to check for error and return
    status
  refs.c: make ref_transaction_begin take an err argument
  refs.c: add transaction.status and track OPEN/CLOSED/ERROR
  tag.c: use ref transactions when doing updates
  replace.c: use the ref transaction functions for updates
  commit.c: use ref transactions for updates
  sequencer.c: use ref transactions for all ref updates
  fast-import.c: change update_branch to use ref transactions
  branch.c: use ref transaction for all ref updates
  refs.c: change update_ref to use a transaction
  receive-pack.c: use a reference transaction for updating the refs
  fast-import.c: use a ref transaction when dumping tags
  walker.c: use ref transaction for ref updates
  refs.c: make lock_ref_sha1 static
  refs.c: remove the update_ref_lock function
  refs.c: remove the update_ref_write function
  refs.c: remove lock_ref_sha1
  refs.c: make prune_ref use a transaction to delete the ref
  refs.c: make delete_ref use a transaction
  refs.c: pass the ref log message to _create/delete/update instead of
    _commit
  refs.c: pass NULL as *flags to read_ref_full
  refs.c: pack all refs before we start to rename a ref
  refs.c: move the check for valid refname to lock_ref_sha1_basic
  refs.c: call lock_ref_sha1_basic directly from commit
  refs.c: pass a skip list to name_conflict_fn
  refs.c: propagate any errno==ENOTDIR from _commit back to the callers
  fetch.c: change s_update_ref to use a ref transaction
  refs.c: make write_ref_sha1 static

 branch.c               |  30 ++--
 builtin/commit.c       |  24 ++-
 builtin/fetch.c        |  25 +--
 builtin/receive-pack.c |  44 ++++--
 builtin/replace.c      |  15 +-
 builtin/tag.c          |  15 +-
 builtin/update-ref.c   |  34 ++--
 cache.h                |   2 +
 fast-import.c          |  42 +++--
 lockfile.c             |  21 +--
 refs.c                 | 420 ++++++++++++++++++++++++++++++++-----------------
 refs.h                 |  80 ++++++----
 sequencer.c            |  24 ++-
 t/t3200-branch.sh      |   6 +-
 walker.c               |  55 ++++---
 15 files changed, 531 insertions(+), 306 deletions(-)

-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply	[flat|nested] 89+ messages in thread

* [PATCH v11 01/41] refs.c: remove ref_transaction_rollback
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 02/41] refs.c: ref_transaction_commit should not free the transaction Ronnie Sahlberg
                   ` (40 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

We do not yet need both a rollback and a free function for transactions.
Remove ref_transaction_rollback and use ref_transaction_free instead.

At a later stage we may reintroduce a rollback function if we want to start
adding reusable transactions and similar.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c |  7 +------
 refs.h | 16 +++++++---------
 2 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/refs.c b/refs.c
index 6898263..48573e3 100644
--- a/refs.c
+++ b/refs.c
@@ -3308,7 +3308,7 @@ struct ref_transaction *ref_transaction_begin(void)
 	return xcalloc(1, sizeof(struct ref_transaction));
 }
 
-static void ref_transaction_free(struct ref_transaction *transaction)
+void ref_transaction_free(struct ref_transaction *transaction)
 {
 	int i;
 
@@ -3319,11 +3319,6 @@ static void ref_transaction_free(struct ref_transaction *transaction)
 	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)
 {
diff --git a/refs.h b/refs.h
index 09ff483..a07a5d0 100644
--- a/refs.h
+++ b/refs.h
@@ -216,18 +216,12 @@ enum action_on_err {
 
 /*
  * Begin a reference transaction.  The reference transaction must
- * eventually be commited using ref_transaction_commit() or rolled
- * back using ref_transaction_rollback().
+ * eventually be commited using ref_transaction_commit() or freed by
+ * calling ref_transaction_free().
  */
 struct ref_transaction *ref_transaction_begin(void);
 
 /*
- * Roll back a ref_transaction and free all associated data.
- */
-void ref_transaction_rollback(struct ref_transaction *transaction);
-
-
-/*
  * The following functions add a reference check or update to a
  * ref_transaction.  In all of them, refname is the name of the
  * reference to be affected.  The functions make internal copies of
@@ -235,7 +229,6 @@ void ref_transaction_rollback(struct ref_transaction *transaction);
  * can be REF_NODEREF; it is passed to update_ref_lock().
  */
 
-
 /*
  * Add a reference update to transaction.  new_sha1 is the value that
  * the reference should have after the update, or zeros if it should
@@ -277,6 +270,11 @@ void ref_transaction_delete(struct ref_transaction *transaction,
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   const char *msg, enum action_on_err onerr);
 
+/*
+ * Free an existing transaction and all associated data.
+ */
+void ref_transaction_free(struct ref_transaction *transaction);
+
 /** Lock a ref and then write its file */
 int update_ref(const char *action, const char *refname,
 		const unsigned char *sha1, const unsigned char *oldval,
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 02/41] refs.c: ref_transaction_commit should not free the transaction
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 01/41] refs.c: remove ref_transaction_rollback Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 23:20   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 03/41] refs.c: constify the sha arguments for ref_transaction_create|delete|update Ronnie Sahlberg
                   ` (39 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c | 1 +
 refs.c               | 1 -
 refs.h               | 5 ++---
 3 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 405267f..1fd7a89 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -369,6 +369,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		update_refs_stdin();
 		ret = ref_transaction_commit(transaction, msg,
 					     UPDATE_REFS_DIE_ON_ERR);
+		ref_transaction_free(transaction);
 		return ret;
 	}
 
diff --git a/refs.c b/refs.c
index 48573e3..33541f4 100644
--- a/refs.c
+++ b/refs.c
@@ -3483,7 +3483,6 @@ cleanup:
 		if (updates[i]->lock)
 			unlock_ref(updates[i]->lock);
 	free(delnames);
-	ref_transaction_free(transaction);
 	return ret;
 }
 
diff --git a/refs.h b/refs.h
index a07a5d0..306d833 100644
--- a/refs.h
+++ b/refs.h
@@ -216,8 +216,7 @@ enum action_on_err {
 
 /*
  * Begin a reference transaction.  The reference transaction must
- * eventually be commited using ref_transaction_commit() or freed by
- * calling ref_transaction_free().
+ * be freed by calling ref_transaction_free().
  */
 struct ref_transaction *ref_transaction_begin(void);
 
@@ -265,7 +264,7 @@ void ref_transaction_delete(struct ref_transaction *transaction,
 /*
  * Commit all of the changes that have been queued in transaction, as
  * atomically as possible.  Return a nonzero value if there is a
- * problem.  The ref_transaction is freed by this function.
+ * problem.
  */
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   const char *msg, enum action_on_err onerr);
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 03/41] refs.c: constify the sha arguments for ref_transaction_create|delete|update
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 01/41] refs.c: remove ref_transaction_rollback Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 02/41] refs.c: ref_transaction_commit should not free the transaction Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 04/41] refs.c: allow passing NULL to ref_transaction_free Ronnie Sahlberg
                   ` (38 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

ref_transaction_create|delete|update has no need to modify the sha1
arguments passed to it so it should use const unsigned char* instead
of unsigned char*.

Some functions, such as fast_forward_to(), already have its old/new
sha1 arguments as consts. This function will at some point need to
use ref_transaction_update() in which case this change is required.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 7 ++++---
 refs.h | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/refs.c b/refs.c
index 33541f4..a767ef6 100644
--- a/refs.c
+++ b/refs.c
@@ -3333,7 +3333,8 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
 
 void ref_transaction_update(struct ref_transaction *transaction,
 			    const char *refname,
-			    unsigned char *new_sha1, unsigned char *old_sha1,
+			    const unsigned char *new_sha1,
+			    const unsigned char *old_sha1,
 			    int flags, int have_old)
 {
 	struct ref_update *update = add_update(transaction, refname);
@@ -3347,7 +3348,7 @@ void ref_transaction_update(struct ref_transaction *transaction,
 
 void ref_transaction_create(struct ref_transaction *transaction,
 			    const char *refname,
-			    unsigned char *new_sha1,
+			    const unsigned char *new_sha1,
 			    int flags)
 {
 	struct ref_update *update = add_update(transaction, refname);
@@ -3361,7 +3362,7 @@ void ref_transaction_create(struct ref_transaction *transaction,
 
 void ref_transaction_delete(struct ref_transaction *transaction,
 			    const char *refname,
-			    unsigned char *old_sha1,
+			    const unsigned char *old_sha1,
 			    int flags, int have_old)
 {
 	struct ref_update *update = add_update(transaction, refname);
diff --git a/refs.h b/refs.h
index 306d833..b893838 100644
--- a/refs.h
+++ b/refs.h
@@ -237,7 +237,8 @@ struct ref_transaction *ref_transaction_begin(void);
  */
 void ref_transaction_update(struct ref_transaction *transaction,
 			    const char *refname,
-			    unsigned char *new_sha1, unsigned char *old_sha1,
+			    const unsigned char *new_sha1,
+			    const unsigned char *old_sha1,
 			    int flags, int have_old);
 
 /*
@@ -248,7 +249,7 @@ void ref_transaction_update(struct ref_transaction *transaction,
  */
 void ref_transaction_create(struct ref_transaction *transaction,
 			    const char *refname,
-			    unsigned char *new_sha1,
+			    const unsigned char *new_sha1,
 			    int flags);
 
 /*
@@ -258,7 +259,7 @@ void ref_transaction_create(struct ref_transaction *transaction,
  */
 void ref_transaction_delete(struct ref_transaction *transaction,
 			    const char *refname,
-			    unsigned char *old_sha1,
+			    const unsigned char *old_sha1,
 			    int flags, int have_old);
 
 /*
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 04/41] refs.c: allow passing NULL to ref_transaction_free
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (2 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 03/41] refs.c: constify the sha arguments for ref_transaction_create|delete|update Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 05/41] refs.c: add a strbuf argument to ref_transaction_commit for error logging Ronnie Sahlberg
                   ` (37 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Allow ref_transaction_free(NULL) as a no-op. This makes ref_transaction_free
easier to use and more similar to plain 'free'.

In particular, it lets us rollback unconditionally as part of cleanup code
after setting 'transaction = NULL' if a transaction has been committed or
rolled back already.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/refs.c b/refs.c
index a767ef6..0faed29 100644
--- a/refs.c
+++ b/refs.c
@@ -3312,6 +3312,9 @@ void ref_transaction_free(struct ref_transaction *transaction)
 {
 	int i;
 
+	if (!transaction)
+		return;
+
 	for (i = 0; i < transaction->nr; i++)
 		free(transaction->updates[i]);
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 05/41] refs.c: add a strbuf argument to ref_transaction_commit for error logging
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (3 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 04/41] refs.c: allow passing NULL to ref_transaction_free Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs Ronnie Sahlberg
                   ` (36 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Add a strbuf argument to _commit so that we can pass an error string back to
the caller. So that we can do error logging from the caller instead of from
_commit.

Longer term plan is to first convert all callers to use onerr==QUIET_ON_ERR
and craft any log messages from the callers themselves and finally remove the
onerr argument completely.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c | 2 +-
 refs.c               | 6 +++++-
 refs.h               | 5 ++++-
 3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 1fd7a89..22617af 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -367,7 +367,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		if (end_null)
 			line_termination = '\0';
 		update_refs_stdin();
-		ret = ref_transaction_commit(transaction, msg,
+		ret = ref_transaction_commit(transaction, msg, NULL,
 					     UPDATE_REFS_DIE_ON_ERR);
 		ref_transaction_free(transaction);
 		return ret;
diff --git a/refs.c b/refs.c
index 0faed29..25c3a93 100644
--- a/refs.c
+++ b/refs.c
@@ -3418,7 +3418,8 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 }
 
 int ref_transaction_commit(struct ref_transaction *transaction,
-			   const char *msg, enum action_on_err onerr)
+			   const char *msg, struct strbuf *err,
+			   enum action_on_err onerr)
 {
 	int ret = 0, delnum = 0, i;
 	const char **delnames;
@@ -3447,6 +3448,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 					       update->flags,
 					       &update->type, onerr);
 		if (!update->lock) {
+			if (err)
+				strbuf_addf(err, "Cannot lock the ref '%s'.",
+					    update->refname);
 			ret = 1;
 			goto cleanup;
 		}
diff --git a/refs.h b/refs.h
index b893838..94d4cd4 100644
--- a/refs.h
+++ b/refs.h
@@ -266,9 +266,12 @@ void ref_transaction_delete(struct ref_transaction *transaction,
  * Commit all of the changes that have been queued in transaction, as
  * atomically as possible.  Return a nonzero value if there is a
  * problem.
+ * If err is non-NULL we will add an error string to it to explain why
+ * the transaction failed. The string does not end in newline.
  */
 int ref_transaction_commit(struct ref_transaction *transaction,
-			   const char *msg, enum action_on_err onerr);
+			   const char *msg, struct strbuf *err,
+			   enum action_on_err onerr);
 
 /*
  * Free an existing transaction and all associated data.
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (4 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 05/41] refs.c: add a strbuf argument to ref_transaction_commit for error logging Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28  0:11   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 07/41] refs.c: make ref_update_reject_duplicates take a strbuf argument for errors Ronnie Sahlberg
                   ` (35 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Update repack_without_refs to take an err argument and update it if there
is a failure. Pass the err variable from ref_transaction_commit to this
function so that callers can print a meaningful error message if _commit
fails due to a problem in repack_without_refs.

Add a new function unable_to_lock_strbuf that takes a strbuf argument and
fills in the reason for the failure.

In commit_packed_refs, make sure that we propagate any errno that
commit_lock_file might have set back to our caller.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 cache.h    |  2 ++
 lockfile.c | 21 ++++++++++++---------
 refs.c     | 25 +++++++++++++++++++------
 3 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/cache.h b/cache.h
index 8c6cdc2..48548d6 100644
--- a/cache.h
+++ b/cache.h
@@ -559,6 +559,8 @@ struct lock_file {
 #define LOCK_DIE_ON_ERROR 1
 #define LOCK_NODEREF 2
 extern int unable_to_lock_error(const char *path, int err);
+extern void unable_to_lock_strbuf(const char *path, int err,
+				  struct strbuf *buf);
 extern NORETURN void unable_to_lock_index_die(const char *path, int err);
 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
 extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
diff --git a/lockfile.c b/lockfile.c
index 8fbcb6a..9e04b43 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -157,33 +157,36 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 	return lk->fd;
 }
 
-static char *unable_to_lock_message(const char *path, int err)
+void unable_to_lock_strbuf(const char *path, int err, struct strbuf *buf)
 {
-	struct strbuf buf = STRBUF_INIT;
 
 	if (err == EEXIST) {
-		strbuf_addf(&buf, "Unable to create '%s.lock': %s.\n\n"
+		strbuf_addf(buf, "Unable to create '%s.lock': %s.\n\n"
 		    "If no other git process is currently running, this probably means a\n"
 		    "git process crashed in this repository earlier. Make sure no other git\n"
 		    "process is running and remove the file manually to continue.",
 			    absolute_path(path), strerror(err));
 	} else
-		strbuf_addf(&buf, "Unable to create '%s.lock': %s",
+		strbuf_addf(buf, "Unable to create '%s.lock': %s",
 			    absolute_path(path), strerror(err));
-	return strbuf_detach(&buf, NULL);
 }
 
 int unable_to_lock_error(const char *path, int err)
 {
-	char *msg = unable_to_lock_message(path, err);
-	error("%s", msg);
-	free(msg);
+	struct strbuf buf = STRBUF_INIT;
+
+	unable_to_lock_strbuf(path, err, &buf);
+	error("%s", buf.buf);
+	strbuf_release(&buf);
 	return -1;
 }
 
 NORETURN void unable_to_lock_index_die(const char *path, int err)
 {
-	die("%s", unable_to_lock_message(path, err));
+	struct strbuf buf = STRBUF_INIT;
+
+	unable_to_lock_strbuf(path, err, &buf);
+	die("%s", buf.buf);
 }
 
 int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
diff --git a/refs.c b/refs.c
index 25c3a93..2da62ac 100644
--- a/refs.c
+++ b/refs.c
@@ -2208,6 +2208,7 @@ int commit_packed_refs(void)
 	struct packed_ref_cache *packed_ref_cache =
 		get_packed_ref_cache(&ref_cache);
 	int error = 0;
+	int save_errno = 0;
 
 	if (!packed_ref_cache->lock)
 		die("internal error: packed-refs not locked");
@@ -2217,10 +2218,13 @@ int commit_packed_refs(void)
 	do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
 				 0, write_packed_entry_fn,
 				 &packed_ref_cache->lock->fd);
-	if (commit_lock_file(packed_ref_cache->lock))
+	if (commit_lock_file(packed_ref_cache->lock)) {
+		save_errno = errno;
 		error = -1;
+	}
 	packed_ref_cache->lock = NULL;
 	release_packed_ref_cache(packed_ref_cache);
+	errno = save_errno;
 	return error;
 }
 
@@ -2427,12 +2431,12 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
 	return 0;
 }
 
-static int repack_without_refs(const char **refnames, int n)
+static int repack_without_refs(const char **refnames, int n, struct strbuf *err)
 {
 	struct ref_dir *packed;
 	struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
 	struct string_list_item *ref_to_delete;
-	int i, removed = 0;
+	int i, ret, removed = 0;
 
 	/* Look for a packed ref */
 	for (i = 0; i < n; i++)
@@ -2444,6 +2448,11 @@ static int repack_without_refs(const char **refnames, int n)
 		return 0; /* no refname exists in packed refs */
 
 	if (lock_packed_refs(0)) {
+		if (err) {
+			unable_to_lock_strbuf(git_path("packed-refs"), errno,
+					      err);
+			return -1;
+		}
 		unable_to_lock_error(git_path("packed-refs"), errno);
 		return error("cannot delete '%s' from packed refs", refnames[i]);
 	}
@@ -2470,12 +2479,16 @@ static int repack_without_refs(const char **refnames, int n)
 	}
 
 	/* Write what remains */
-	return commit_packed_refs();
+	ret = commit_packed_refs();
+	if (ret && err)
+		strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
+			    strerror(errno));
+	return ret;
 }
 
 static int repack_without_ref(const char *refname)
 {
-	return repack_without_refs(&refname, 1);
+	return repack_without_refs(&refname, 1, NULL);
 }
 
 static int delete_ref_loose(struct ref_lock *lock, int flag)
@@ -3481,7 +3494,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 		}
 	}
 
-	ret |= repack_without_refs(delnames, delnum);
+	ret |= repack_without_refs(delnames, delnum, err);
 	for (i = 0; i < delnum; i++)
 		unlink_or_warn(git_path("logs/%s", delnames[i]));
 	clear_loose_ref_cache(&ref_cache);
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 07/41] refs.c: make ref_update_reject_duplicates take a strbuf argument for errors
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (5 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 08/41] refs.c: add an err argument to delete_ref_loose Ronnie Sahlberg
                   ` (34 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Make ref_update_reject_duplicates return any error that occurs through a
new strbuf argument. This means that when a transaction commit fails in
this function we will now be able to pass a helpful error message back to the
caller.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/refs.c b/refs.c
index 2da62ac..4c7f4f7 100644
--- a/refs.c
+++ b/refs.c
@@ -3410,6 +3410,7 @@ static int ref_update_compare(const void *r1, const void *r2)
 }
 
 static int ref_update_reject_duplicates(struct ref_update **updates, int n,
+					struct strbuf *err,
 					enum action_on_err onerr)
 {
 	int i;
@@ -3417,6 +3418,9 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 		if (!strcmp(updates[i - 1]->refname, updates[i]->refname)) {
 			const char *str =
 				"Multiple updates for ref '%s' not allowed.";
+			if (err)
+				strbuf_addf(err, str, updates[i]->refname);
+
 			switch (onerr) {
 			case UPDATE_REFS_MSG_ON_ERR:
 				error(str, updates[i]->refname); break;
@@ -3447,7 +3451,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 	/* Copy, sort, and reject duplicate refs */
 	qsort(updates, n, sizeof(*updates), ref_update_compare);
-	ret = ref_update_reject_duplicates(updates, n, onerr);
+	ret = ref_update_reject_duplicates(updates, n, err, onerr);
 	if (ret)
 		goto cleanup;
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 08/41] refs.c: add an err argument to delete_ref_loose
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (6 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 07/41] refs.c: make ref_update_reject_duplicates take a strbuf argument for errors Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28  0:25   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 09/41] refs.c: make update_ref_write update a strbuf on failure Ronnie Sahlberg
                   ` (33 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Add an err argument to delete_loose_ref so that we can pass a descriptive
error string back to the caller. Pass the err argument from transaction
commit to this function so that transaction users will have a nice error
string if the transaction failed due to delete_loose_ref.

Add a new function unlink_or_err that we can call from delete_ref_loose. This
function is similar to unlink_or_warn except that we can pass it an err
argument. If err is non-NULL the function will populate err instead of
printing a warning().

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)

diff --git a/refs.c b/refs.c
index 4c7f4f7..891b80c 100644
--- a/refs.c
+++ b/refs.c
@@ -2491,17 +2491,43 @@ static int repack_without_ref(const char *refname)
 	return repack_without_refs(&refname, 1, NULL);
 }
 
-static int delete_ref_loose(struct ref_lock *lock, int flag)
+static int add_err_if_unremovable(const char *op, const char *file,
+				  struct strbuf *e, int rc)
+{
+	int err = errno;
+	if (rc < 0 && err != ENOENT) {
+		strbuf_addf(e, "unable to %s %s: %s",
+			    op, file, strerror(errno));
+		errno = err;
+	}
+	return rc;
+}
+
+static int unlink_or_err(const char *file, struct strbuf *err)
+{
+	if (err)
+		return add_err_if_unremovable("unlink", file, err,
+					      unlink(file));
+	else
+		return unlink_or_warn(file);
+}
+
+static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
 {
 	if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
 		/* loose */
-		int err, i = strlen(lock->lk->filename) - 5; /* .lock */
+		int res, i = strlen(lock->lk->filename) - 5; /* .lock */
 
 		lock->lk->filename[i] = 0;
-		err = unlink_or_warn(lock->lk->filename);
+		res = unlink_or_err(lock->lk->filename, err);
 		lock->lk->filename[i] = '.';
-		if (err && errno != ENOENT)
+		if (res && errno != ENOENT) {
+			if (err)
+				strbuf_addf(err,
+					    "failed to delete loose ref '%s'",
+					    lock->lk->filename);
 			return 1;
+		}
 	}
 	return 0;
 }
@@ -2514,7 +2540,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
 	lock = lock_ref_sha1_basic(refname, sha1, delopt, &flag);
 	if (!lock)
 		return 1;
-	ret |= delete_ref_loose(lock, flag);
+	ret |= delete_ref_loose(lock, flag, NULL);
 
 	/* removing the loose one could have resurrected an earlier
 	 * packed one.  Also, if it was not loose we need to repack
@@ -3494,7 +3520,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 		if (update->lock) {
 			delnames[delnum++] = update->lock->ref_name;
-			ret |= delete_ref_loose(update->lock, update->type);
+			ret |= delete_ref_loose(update->lock, update->type,
+						err);
 		}
 	}
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 09/41] refs.c: make update_ref_write update a strbuf on failure
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (7 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 08/41] refs.c: add an err argument to delete_ref_loose Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref Ronnie Sahlberg
                   ` (32 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change update_ref_write to also update an error strbuf on failure.
This makes the error available to ref_transaction_commit callers if the
transaction failed due to update_ref_sha1/write_ref_sha1 failures.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/refs.c b/refs.c
index 891b80c..b28f7d4 100644
--- a/refs.c
+++ b/refs.c
@@ -3301,10 +3301,13 @@ static struct ref_lock *update_ref_lock(const char *refname,
 
 static int update_ref_write(const char *action, const char *refname,
 			    const unsigned char *sha1, struct ref_lock *lock,
-			    enum action_on_err onerr)
+			    struct strbuf *err, enum action_on_err onerr)
 {
 	if (write_ref_sha1(lock, sha1, action) < 0) {
 		const char *str = "Cannot update the ref '%s'.";
+		if (err)
+			strbuf_addf(err, str, refname);
+
 		switch (onerr) {
 		case UPDATE_REFS_MSG_ON_ERR: error(str, refname); break;
 		case UPDATE_REFS_DIE_ON_ERR: die(str, refname); break;
@@ -3425,7 +3428,7 @@ int update_ref(const char *action, const char *refname,
 	lock = update_ref_lock(refname, oldval, flags, NULL, onerr);
 	if (!lock)
 		return 1;
-	return update_ref_write(action, refname, sha1, lock, onerr);
+	return update_ref_write(action, refname, sha1, lock, NULL, onerr);
 }
 
 static int ref_update_compare(const void *r1, const void *r2)
@@ -3507,7 +3510,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 			ret = update_ref_write(msg,
 					       update->refname,
 					       update->new_sha1,
-					       update->lock, onerr);
+					       update->lock, err, onerr);
 			update->lock = NULL; /* freed by update_ref_write */
 			if (ret)
 				goto cleanup;
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (8 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 09/41] refs.c: make update_ref_write update a strbuf on failure Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28  0:27   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 11/41] refs.c: remove the onerr argument to ref_transaction_commit Ronnie Sahlberg
                   ` (31 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Call ref_transaction_commit with QUIET_ON_ERR and use the strbuf that is
returned to print a log message if/after the transaction fails.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 22617af..aec9004 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -342,6 +342,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	const char *refname, *oldval, *msg = NULL;
 	unsigned char sha1[20], oldsha1[20];
 	int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
+	struct strbuf err = STRBUF_INIT;
 	struct option options[] = {
 		OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
 		OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
@@ -359,18 +360,17 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		die("Refusing to perform update with empty message.");
 
 	if (read_stdin) {
-		int ret;
 		transaction = ref_transaction_begin();
-
 		if (delete || no_deref || argc > 0)
 			usage_with_options(git_update_ref_usage, options);
 		if (end_null)
 			line_termination = '\0';
 		update_refs_stdin();
-		ret = ref_transaction_commit(transaction, msg, NULL,
-					     UPDATE_REFS_DIE_ON_ERR);
+		if (ref_transaction_commit(transaction, msg, &err,
+					   UPDATE_REFS_QUIET_ON_ERR))
+			die("%s", err.buf);
 		ref_transaction_free(transaction);
-		return ret;
+		return 0;
 	}
 
 	if (end_null)
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 11/41] refs.c: remove the onerr argument to ref_transaction_commit
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (9 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 12/41] refs.c: change ref_transaction_update() to do error checking and return status Ronnie Sahlberg
                   ` (30 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Since all callers now use QUIET_ON_ERR we no longer need to provide an onerr
argument any more. Remove the onerr argument from the ref_transaction_commit
signature.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c |  3 +--
 refs.c               | 22 +++++++---------------
 refs.h               |  3 +--
 3 files changed, 9 insertions(+), 19 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index aec9004..88ab785 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -366,8 +366,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		if (end_null)
 			line_termination = '\0';
 		update_refs_stdin();
-		if (ref_transaction_commit(transaction, msg, &err,
-					   UPDATE_REFS_QUIET_ON_ERR))
+		if (ref_transaction_commit(transaction, msg, &err))
 			die("%s", err.buf);
 		ref_transaction_free(transaction);
 		return 0;
diff --git a/refs.c b/refs.c
index b28f7d4..00b6e9a 100644
--- a/refs.c
+++ b/refs.c
@@ -3439,8 +3439,7 @@ static int ref_update_compare(const void *r1, const void *r2)
 }
 
 static int ref_update_reject_duplicates(struct ref_update **updates, int n,
-					struct strbuf *err,
-					enum action_on_err onerr)
+					struct strbuf *err)
 {
 	int i;
 	for (i = 1; i < n; i++)
@@ -3450,22 +3449,13 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 			if (err)
 				strbuf_addf(err, str, updates[i]->refname);
 
-			switch (onerr) {
-			case UPDATE_REFS_MSG_ON_ERR:
-				error(str, updates[i]->refname); break;
-			case UPDATE_REFS_DIE_ON_ERR:
-				die(str, updates[i]->refname); break;
-			case UPDATE_REFS_QUIET_ON_ERR:
-				break;
-			}
 			return 1;
 		}
 	return 0;
 }
 
 int ref_transaction_commit(struct ref_transaction *transaction,
-			   const char *msg, struct strbuf *err,
-			   enum action_on_err onerr)
+			   const char *msg, struct strbuf *err)
 {
 	int ret = 0, delnum = 0, i;
 	const char **delnames;
@@ -3480,7 +3470,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 	/* Copy, sort, and reject duplicate refs */
 	qsort(updates, n, sizeof(*updates), ref_update_compare);
-	ret = ref_update_reject_duplicates(updates, n, err, onerr);
+	ret = ref_update_reject_duplicates(updates, n, err);
 	if (ret)
 		goto cleanup;
 
@@ -3492,7 +3482,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 					       (update->have_old ?
 						update->old_sha1 : NULL),
 					       update->flags,
-					       &update->type, onerr);
+					       &update->type,
+					       UPDATE_REFS_QUIET_ON_ERR);
 		if (!update->lock) {
 			if (err)
 				strbuf_addf(err, "Cannot lock the ref '%s'.",
@@ -3510,7 +3501,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 			ret = update_ref_write(msg,
 					       update->refname,
 					       update->new_sha1,
-					       update->lock, err, onerr);
+					       update->lock, err,
+					       UPDATE_REFS_QUIET_ON_ERR);
 			update->lock = NULL; /* freed by update_ref_write */
 			if (ret)
 				goto cleanup;
diff --git a/refs.h b/refs.h
index 94d4cd4..6ee9c9e 100644
--- a/refs.h
+++ b/refs.h
@@ -270,8 +270,7 @@ void ref_transaction_delete(struct ref_transaction *transaction,
  * the transaction failed. The string does not end in newline.
  */
 int ref_transaction_commit(struct ref_transaction *transaction,
-			   const char *msg, struct strbuf *err,
-			   enum action_on_err onerr);
+			   const char *msg, struct strbuf *err);
 
 /*
  * Free an existing transaction and all associated data.
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 12/41] refs.c: change ref_transaction_update() to do error checking and return status
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (10 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 11/41] refs.c: remove the onerr argument to ref_transaction_commit Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 13/41] refs.c: change ref_transaction_create " Ronnie Sahlberg
                   ` (29 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Update ref_transaction_update() do some basic error checking and return
non-zero on error. Update all callers to check ref_transaction_update() for
error. There are currently no conditions in _update that will return error but
there will be in the future. Add an err argument that will be updated on
failure. In future patches we will start doing both locking and checking
for name conflicts in _update instead of _commit at which time this function
will start returning errors for these conditions.

Also check for BUGs during update and die(BUG:...) if we are calling
_update with have_old but the old_sha1 pointer is NULL.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c | 12 +++++++-----
 refs.c               | 18 ++++++++++++------
 refs.h               | 14 +++++++++-----
 3 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 88ab785..3067b11 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -16,6 +16,7 @@ static struct ref_transaction *transaction;
 
 static char line_termination = '\n';
 static int update_flags;
+static struct strbuf err = STRBUF_INIT;
 
 /*
  * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
@@ -197,8 +198,9 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)
 	if (*next != line_termination)
 		die("update %s: extra input: %s", refname, next);
 
-	ref_transaction_update(transaction, refname, new_sha1, old_sha1,
-			       update_flags, have_old);
+	if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
+				   update_flags, have_old, &err))
+		die("%s", err.buf);
 
 	update_flags = 0;
 	free(refname);
@@ -286,8 +288,9 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)
 	if (*next != line_termination)
 		die("verify %s: extra input: %s", refname, next);
 
-	ref_transaction_update(transaction, refname, new_sha1, old_sha1,
-			       update_flags, have_old);
+	if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
+				   update_flags, have_old, &err))
+		die("%s", err.buf);
 
 	update_flags = 0;
 	free(refname);
@@ -342,7 +345,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	const char *refname, *oldval, *msg = NULL;
 	unsigned char sha1[20], oldsha1[20];
 	int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
-	struct strbuf err = STRBUF_INIT;
 	struct option options[] = {
 		OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
 		OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
diff --git a/refs.c b/refs.c
index 00b6e9a..79a4477 100644
--- a/refs.c
+++ b/refs.c
@@ -3376,19 +3376,25 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
 	return update;
 }
 
-void ref_transaction_update(struct ref_transaction *transaction,
-			    const char *refname,
-			    const unsigned char *new_sha1,
-			    const unsigned char *old_sha1,
-			    int flags, int have_old)
+int ref_transaction_update(struct ref_transaction *transaction,
+			   const char *refname,
+			   const unsigned char *new_sha1,
+			   const unsigned char *old_sha1,
+			   int flags, int have_old,
+			   struct strbuf *err)
 {
-	struct ref_update *update = add_update(transaction, refname);
+	struct ref_update *update;
+
+	if (have_old && !old_sha1)
+		die("BUG: have_old is true but old_sha1 is NULL");
 
+	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);
+	return 0;
 }
 
 void ref_transaction_create(struct ref_transaction *transaction,
diff --git a/refs.h b/refs.h
index 6ee9c9e..32edf3f 100644
--- a/refs.h
+++ b/refs.h
@@ -234,12 +234,16 @@ struct ref_transaction *ref_transaction_begin(void);
  * be deleted.  If have_old is true, then old_sha1 holds the value
  * that the reference should have had before the update, or zeros if
  * it must not have existed beforehand.
+ * Function returns 0 on success and non-zero on failure. A failure to update
+ * means that the transaction as a whole has failed and will need to be
+ * rolled back. On failure the err buffer will be updated.
  */
-void ref_transaction_update(struct ref_transaction *transaction,
-			    const char *refname,
-			    const unsigned char *new_sha1,
-			    const unsigned char *old_sha1,
-			    int flags, int have_old);
+int ref_transaction_update(struct ref_transaction *transaction,
+			   const char *refname,
+			   const unsigned char *new_sha1,
+			   const unsigned char *old_sha1,
+			   int flags, int have_old,
+			   struct strbuf *err);
 
 /*
  * Add a reference creation to transaction.  new_sha1 is the value
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (11 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 12/41] refs.c: change ref_transaction_update() to do error checking and return status Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28  0:42   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 14/41] refs.c: update ref_transaction_delete to check for error " Ronnie Sahlberg
                   ` (28 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Do basic error checking in ref_transaction_create() and make it return
non-zero on error. Update all callers to check the result of
ref_transaction_create(). There are currently no conditions in _create that
will return error but there will be in the future. Add an err argument that
will be updated on failure.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c |  4 +++-
 refs.c               | 18 ++++++++++++------
 refs.h               | 25 ++++++++++++++++++-------
 3 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 3067b11..41121fa 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -226,7 +226,9 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)
 	if (*next != line_termination)
 		die("create %s: extra input: %s", refname, next);
 
-	ref_transaction_create(transaction, refname, new_sha1, update_flags);
+	if (ref_transaction_create(transaction, refname, new_sha1,
+				   update_flags, &err))
+		die("%s", err.buf);
 
 	update_flags = 0;
 	free(refname);
diff --git a/refs.c b/refs.c
index 79a4477..709758d 100644
--- a/refs.c
+++ b/refs.c
@@ -3397,18 +3397,24 @@ int ref_transaction_update(struct ref_transaction *transaction,
 	return 0;
 }
 
-void ref_transaction_create(struct ref_transaction *transaction,
-			    const char *refname,
-			    const unsigned char *new_sha1,
-			    int flags)
+int ref_transaction_create(struct ref_transaction *transaction,
+			   const char *refname,
+			   const unsigned char *new_sha1,
+			   int flags,
+			   struct strbuf *err)
 {
-	struct ref_update *update = add_update(transaction, refname);
+	struct ref_update *update;
+
+	if (!new_sha1 || is_null_sha1(new_sha1))
+		die("BUG: create ref with null new_sha1");
+
+	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;
+	return 0;
 }
 
 void ref_transaction_delete(struct ref_transaction *transaction,
diff --git a/refs.h b/refs.h
index 32edf3f..22b8cc3 100644
--- a/refs.h
+++ b/refs.h
@@ -215,6 +215,15 @@ enum action_on_err {
 };
 
 /*
+ * Transaction functions that take an err argument will append an error
+ * string to this buffer if there was a failure.
+ * This string is not cleared on each call and may contain an aggregate of
+ * errors from several previous calls.
+ * If the caller needs a guarantee that the buffer will only contain the
+ * current or most recent error it must call strbuf_reset before calling
+ * the transaction function.
+ */
+/*
  * Begin a reference transaction.  The reference transaction must
  * be freed by calling ref_transaction_free().
  */
@@ -236,7 +245,7 @@ struct ref_transaction *ref_transaction_begin(void);
  * it must not have existed beforehand.
  * Function returns 0 on success and non-zero on failure. A failure to update
  * means that the transaction as a whole has failed and will need to be
- * rolled back. On failure the err buffer will be updated.
+ * rolled back.
  */
 int ref_transaction_update(struct ref_transaction *transaction,
 			   const char *refname,
@@ -250,11 +259,15 @@ int ref_transaction_update(struct ref_transaction *transaction,
  * that the reference should have after the update; it must not be the
  * null SHA-1.  It is verified that the reference does not exist
  * already.
+ * Function returns 0 on success and non-zero on failure. A failure to create
+ * means that the transaction as a whole has failed and will need to be
+ * rolled back.
  */
-void ref_transaction_create(struct ref_transaction *transaction,
-			    const char *refname,
-			    const unsigned char *new_sha1,
-			    int flags);
+int ref_transaction_create(struct ref_transaction *transaction,
+			   const char *refname,
+			   const unsigned char *new_sha1,
+			   int flags,
+			   struct strbuf *err);
 
 /*
  * Add a reference deletion to transaction.  If have_old is true, then
@@ -270,8 +283,6 @@ void ref_transaction_delete(struct ref_transaction *transaction,
  * Commit all of the changes that have been queued in transaction, as
  * atomically as possible.  Return a nonzero value if there is a
  * problem.
- * If err is non-NULL we will add an error string to it to explain why
- * the transaction failed. The string does not end in newline.
  */
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   const char *msg, struct strbuf *err);
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 14/41] refs.c: update ref_transaction_delete to check for error and return status
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (12 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 13/41] refs.c: change ref_transaction_create " Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 18:42   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 15/41] refs.c: make ref_transaction_begin take an err argument Ronnie Sahlberg
                   ` (27 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change ref_transaction_delete() to do basic error checking and return
non-zero of error. Update all callers to check the return for
ref_transaction_delete(). There are currently no conditions in _delete that
will return error but there will be in the future. Add an err argument that
will be updated on failure.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c |  5 +++--
 refs.c               | 16 +++++++++++-----
 refs.h               | 12 ++++++++----
 3 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 41121fa..7c9c248 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -258,8 +258,9 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)
 	if (*next != line_termination)
 		die("delete %s: extra input: %s", refname, next);
 
-	ref_transaction_delete(transaction, refname, old_sha1,
-			       update_flags, have_old);
+	if (ref_transaction_delete(transaction, refname, old_sha1,
+				   update_flags, have_old, &err))
+		die("%s", err.buf);
 
 	update_flags = 0;
 	free(refname);
diff --git a/refs.c b/refs.c
index 709758d..beddce0 100644
--- a/refs.c
+++ b/refs.c
@@ -3417,19 +3417,25 @@ int ref_transaction_create(struct ref_transaction *transaction,
 	return 0;
 }
 
-void ref_transaction_delete(struct ref_transaction *transaction,
-			    const char *refname,
-			    const unsigned char *old_sha1,
-			    int flags, int have_old)
+int ref_transaction_delete(struct ref_transaction *transaction,
+			   const char *refname,
+			   const unsigned char *old_sha1,
+			   int flags, int have_old,
+			   struct strbuf *err)
 {
-	struct ref_update *update = add_update(transaction, refname);
+	struct ref_update *update;
 
+	if (have_old && !old_sha1)
+		die("BUG: have_old is true but old_sha1 is NULL");
+
+	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);
 	}
+	return 0;
 }
 
 int update_ref(const char *action, const char *refname,
diff --git a/refs.h b/refs.h
index 22b8cc3..e0ab989 100644
--- a/refs.h
+++ b/refs.h
@@ -273,11 +273,15 @@ int ref_transaction_create(struct ref_transaction *transaction,
  * Add a reference deletion to transaction.  If have_old is true, then
  * old_sha1 holds the value that the reference should have had before
  * the update (which must not be the null SHA-1).
+ * Function returns 0 on success and non-zero on failure. A failure to delete
+ * means that the transaction as a whole has failed and will need to be
+ * rolled back.
  */
-void ref_transaction_delete(struct ref_transaction *transaction,
-			    const char *refname,
-			    const unsigned char *old_sha1,
-			    int flags, int have_old);
+int ref_transaction_delete(struct ref_transaction *transaction,
+			   const char *refname,
+			   const unsigned char *old_sha1,
+			   int flags, int have_old,
+			   struct strbuf *err);
 
 /*
  * Commit all of the changes that have been queued in transaction, as
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 15/41] refs.c: make ref_transaction_begin take an err argument
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (13 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 14/41] refs.c: update ref_transaction_delete to check for error " Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR Ronnie Sahlberg
                   ` (26 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Add an err argument to _begin so that on non-fatal failures in future ref
backends we can report a nice error back to the caller.
While _begin can currently never fail for other reasons than OOM, in which
case we die() anyway, we may add other types of backends in the future.
For example, a hypothetical MySQL backend could fail in _being with
"Can not connect to MySQL server. No route to host".

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/update-ref.c | 2 +-
 refs.c               | 2 +-
 refs.h               | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 7c9c248..c6ad0be 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -365,7 +365,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		die("Refusing to perform update with empty message.");
 
 	if (read_stdin) {
-		transaction = ref_transaction_begin();
+		transaction = ref_transaction_begin(&err);
 		if (delete || no_deref || argc > 0)
 			usage_with_options(git_update_ref_usage, options);
 		if (end_null)
diff --git a/refs.c b/refs.c
index beddce0..1c660e0 100644
--- a/refs.c
+++ b/refs.c
@@ -3345,7 +3345,7 @@ struct ref_transaction {
 	size_t nr;
 };
 
-struct ref_transaction *ref_transaction_begin(void)
+struct ref_transaction *ref_transaction_begin(struct strbuf *err)
 {
 	return xcalloc(1, sizeof(struct ref_transaction));
 }
diff --git a/refs.h b/refs.h
index e0ab989..6c830f2 100644
--- a/refs.h
+++ b/refs.h
@@ -227,7 +227,7 @@ enum action_on_err {
  * Begin a reference transaction.  The reference transaction must
  * be freed by calling ref_transaction_free().
  */
-struct ref_transaction *ref_transaction_begin(void);
+struct ref_transaction *ref_transaction_begin(struct strbuf *err);
 
 /*
  * The following functions add a reference check or update to a
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (14 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 15/41] refs.c: make ref_transaction_begin take an err argument Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 18:51   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 17/41] tag.c: use ref transactions when doing updates Ronnie Sahlberg
                   ` (25 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Track the status of a transaction in a new status field. Check the field for
sanity, i.e. that status must be OPEN when _commit/_create/_delete or
_update is called or else die(BUG:...)

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/refs.c b/refs.c
index 1c660e0..9992da4 100644
--- a/refs.c
+++ b/refs.c
@@ -3335,6 +3335,27 @@ struct ref_update {
 };
 
 /*
+ * Transaction states.
+ * OPEN:   The transaction is in a valid state and can accept new updates.
+ *         An OPEN transaction can be committed.
+ * CLOSED: If an open transaction is successfully committed the state will
+ *         change to CLOSED. No further changes can be made to a CLOSED
+ *         transaction.
+ *         CLOSED means that all updates have been successfully committed and
+ *         the only thing that remains is to free the completed transaction.
+ * ERROR:  The transaction has failed. No further updates are possible
+ *         and any calls except free will return an error.
+ *         An ERRORed transaction can not be committed.
+ *         An ERRORed transaction can be rolled back and discarded by calling
+ *         by calling transaction_free.
+ */
+enum ref_transaction_state {
+	REF_TRANSACTION_OPEN   = 0,
+	REF_TRANSACTION_CLOSED = 1,
+	REF_TRANSACTION_ERROR  = 2,
+};
+
+/*
  * 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.
@@ -3343,6 +3364,8 @@ struct ref_transaction {
 	struct ref_update **updates;
 	size_t alloc;
 	size_t nr;
+	enum ref_transaction_state state;
+	int status;
 };
 
 struct ref_transaction *ref_transaction_begin(struct strbuf *err)
@@ -3385,6 +3408,9 @@ int ref_transaction_update(struct ref_transaction *transaction,
 {
 	struct ref_update *update;
 
+	if (transaction->state != REF_TRANSACTION_OPEN)
+		return -1;
+
 	if (have_old && !old_sha1)
 		die("BUG: have_old is true but old_sha1 is NULL");
 
@@ -3405,6 +3431,9 @@ int ref_transaction_create(struct ref_transaction *transaction,
 {
 	struct ref_update *update;
 
+	if (transaction->state != REF_TRANSACTION_OPEN)
+		return -1;
+
 	if (!new_sha1 || is_null_sha1(new_sha1))
 		die("BUG: create ref with null new_sha1");
 
@@ -3425,6 +3454,9 @@ int ref_transaction_delete(struct ref_transaction *transaction,
 {
 	struct ref_update *update;
 
+	if (transaction->state != REF_TRANSACTION_OPEN)
+		return -1;
+
 	if (have_old && !old_sha1)
 		die("BUG: have_old is true but old_sha1 is NULL");
 
@@ -3480,8 +3512,13 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	int n = transaction->nr;
 	struct ref_update **updates = transaction->updates;
 
-	if (!n)
+	if (transaction->state != REF_TRANSACTION_OPEN)
+		return transaction->status;
+
+	if (!n) {
+		transaction->state = REF_TRANSACTION_CLOSED;
 		return 0;
+	}
 
 	/* Allocate work space */
 	delnames = xmalloc(sizeof(*delnames) * n);
@@ -3544,6 +3581,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	clear_loose_ref_cache(&ref_cache);
 
 cleanup:
+	transaction->state = ret ? REF_TRANSACTION_ERROR
+		: REF_TRANSACTION_CLOSED;
+
 	for (i = 0; i < n; i++)
 		if (updates[i]->lock)
 			unlock_ref(updates[i]->lock);
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 17/41] tag.c: use ref transactions when doing updates
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (15 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 18/41] replace.c: use the ref transaction functions for updates Ronnie Sahlberg
                   ` (24 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change tag.c to use ref transactions for all ref updates.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/tag.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/builtin/tag.c b/builtin/tag.c
index c6e8a71..c9bfc9a 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -548,7 +548,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct strbuf ref = STRBUF_INIT;
 	unsigned char object[20], prev[20];
 	const char *object_ref, *tag;
-	struct ref_lock *lock;
 	struct create_tag_options opt;
 	char *cleanup_arg = NULL;
 	int annotate = 0, force = 0, lines = -1;
@@ -556,6 +555,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	const char *msgfile = NULL, *keyid = NULL;
 	struct msg_arg msg = { 0, STRBUF_INIT };
 	struct commit_list *with_commit = NULL;
+	struct ref_transaction *transaction;
+	struct strbuf err = STRBUF_INIT;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
 		{ OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
@@ -701,11 +702,13 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	if (annotate)
 		create_tag(object, tag, &buf, &opt, prev, object);
 
-	lock = lock_any_ref_for_update(ref.buf, prev, 0, NULL);
-	if (!lock)
-		die(_("%s: cannot lock the ref"), ref.buf);
-	if (write_ref_sha1(lock, object, NULL) < 0)
-		die(_("%s: cannot update the ref"), ref.buf);
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_update(transaction, ref.buf, object, prev,
+				   0, !is_null_sha1(prev), &err) ||
+	    ref_transaction_commit(transaction, NULL, &err))
+		die("%s", err.buf);
+	ref_transaction_free(transaction);
 	if (force && !is_null_sha1(prev) && hashcmp(prev, object))
 		printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 18/41] replace.c: use the ref transaction functions for updates
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (16 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 17/41] tag.c: use ref transactions when doing updates Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 19/41] commit.c: use ref transactions " Ronnie Sahlberg
                   ` (23 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Update replace.c to use ref transactions for updates.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/replace.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/builtin/replace.c b/builtin/replace.c
index 3da1bae..f24d814 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -133,7 +133,8 @@ static int replace_object_sha1(const char *object_ref,
 	unsigned char prev[20];
 	enum object_type obj_type, repl_type;
 	char ref[PATH_MAX];
-	struct ref_lock *lock;
+	struct ref_transaction *transaction;
+	struct strbuf err = STRBUF_INIT;
 
 	if (snprintf(ref, sizeof(ref),
 		     "refs/replace/%s",
@@ -156,12 +157,14 @@ static int replace_object_sha1(const char *object_ref,
 	else if (!force)
 		die("replace ref '%s' already exists", ref);
 
-	lock = lock_any_ref_for_update(ref, prev, 0, NULL);
-	if (!lock)
-		die("%s: cannot lock the ref", ref);
-	if (write_ref_sha1(lock, repl, NULL) < 0)
-		die("%s: cannot update the ref", ref);
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_update(transaction, ref, repl, prev,
+				   0, !is_null_sha1(prev), &err) ||
+	    ref_transaction_commit(transaction, NULL, &err))
+		die("%s", err.buf);
 
+	ref_transaction_free(transaction);
 	return 0;
 }
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 19/41] commit.c: use ref transactions for updates
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (17 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 18/41] replace.c: use the ref transaction functions for updates Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 20/41] sequencer.c: use ref transactions for all ref updates Ronnie Sahlberg
                   ` (22 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change commit.c to use ref transactions for all ref updates.
Make sure we pass a NULL pointer to ref_transaction_update if have_old
is false.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/commit.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index d28505a..7db194f 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1581,11 +1581,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	const char *index_file, *reflog_msg;
 	char *nl;
 	unsigned char sha1[20];
-	struct ref_lock *ref_lock;
 	struct commit_list *parents = NULL, **pptr = &parents;
 	struct stat statbuf;
 	struct commit *current_head = NULL;
 	struct commit_extra_header *extra = NULL;
+	struct ref_transaction *transaction;
+	struct strbuf err = STRBUF_INIT;
 
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1707,16 +1708,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	strbuf_release(&author_ident);
 	free_commit_extra_headers(extra);
 
-	ref_lock = lock_any_ref_for_update("HEAD",
-					   !current_head
-					   ? NULL
-					   : current_head->object.sha1,
-					   0, NULL);
-	if (!ref_lock) {
-		rollback_index_files();
-		die(_("cannot lock HEAD ref"));
-	}
-
 	nl = strchr(sb.buf, '\n');
 	if (nl)
 		strbuf_setlen(&sb, nl + 1 - sb.buf);
@@ -1725,10 +1716,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
 	strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
 
-	if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_update(transaction, "HEAD", sha1,
+				   current_head ?
+				   current_head->object.sha1 : NULL,
+				   0, !!current_head, &err) ||
+	    ref_transaction_commit(transaction, sb.buf, &err)) {
 		rollback_index_files();
-		die(_("cannot update HEAD ref"));
+		die("%s", err.buf);
 	}
+	ref_transaction_free(transaction);
 
 	unlink(git_path("CHERRY_PICK_HEAD"));
 	unlink(git_path("REVERT_HEAD"));
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 20/41] sequencer.c: use ref transactions for all ref updates
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (18 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 19/41] commit.c: use ref transactions " Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 18:53   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 21/41] fast-import.c: change update_branch to use ref transactions Ronnie Sahlberg
                   ` (21 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change to use ref transactions for all updates to refs.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 sequencer.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/sequencer.c b/sequencer.c
index 0a80c58..fd8acaf 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -272,23 +272,31 @@ static int error_dirty_index(struct replay_opts *opts)
 static int fast_forward_to(const unsigned char *to, const unsigned char *from,
 			int unborn, struct replay_opts *opts)
 {
-	struct ref_lock *ref_lock;
+	struct ref_transaction *transaction;
 	struct strbuf sb = STRBUF_INIT;
-	int ret;
+	struct strbuf err = STRBUF_INIT;
 
 	read_cache();
 	if (checkout_fast_forward(from, to, 1))
 		exit(1); /* the callee should have complained already */
-	ref_lock = lock_any_ref_for_update("HEAD", unborn ? null_sha1 : from,
-					   0, NULL);
-	if (!ref_lock)
-		return error(_("Failed to lock HEAD during fast_forward_to"));
 
 	strbuf_addf(&sb, "%s: fast-forward", action_name(opts));
-	ret = write_ref_sha1(ref_lock, to, sb.buf);
+
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_update(transaction, "HEAD", to, from,
+				   0, !unborn, &err) ||
+	    ref_transaction_commit(transaction, sb.buf, &err)) {
+		ref_transaction_free(transaction);
+		error("%s", err.buf);
+		strbuf_release(&sb);
+		strbuf_release(&err);
+		return -1;
+	}
 
 	strbuf_release(&sb);
-	return ret;
+	ref_transaction_free(transaction);
+	return 0;
 }
 
 static int do_recursive_merge(struct commit *base, struct commit *next,
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 21/41] fast-import.c: change update_branch to use ref transactions
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (19 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 20/41] sequencer.c: use ref transactions for all ref updates Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 18:54   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 22/41] branch.c: use ref transaction for all ref updates Ronnie Sahlberg
                   ` (20 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change update_branch() to use ref transactions for updates.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 fast-import.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/fast-import.c b/fast-import.c
index 6707a66..4a7b196 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1679,39 +1679,44 @@ found_entry:
 static int update_branch(struct branch *b)
 {
 	static const char *msg = "fast-import";
-	struct ref_lock *lock;
+	struct ref_transaction *transaction;
 	unsigned char old_sha1[20];
+	struct strbuf err = STRBUF_INIT;
 
 	if (read_ref(b->name, old_sha1))
 		hashclr(old_sha1);
+
 	if (is_null_sha1(b->sha1)) {
 		if (b->delete)
 			delete_ref(b->name, old_sha1, 0);
 		return 0;
 	}
-	lock = lock_any_ref_for_update(b->name, old_sha1, 0, NULL);
-	if (!lock)
-		return error("Unable to lock %s", b->name);
 	if (!force_update && !is_null_sha1(old_sha1)) {
 		struct commit *old_cmit, *new_cmit;
 
 		old_cmit = lookup_commit_reference_gently(old_sha1, 0);
 		new_cmit = lookup_commit_reference_gently(b->sha1, 0);
-		if (!old_cmit || !new_cmit) {
-			unlock_ref(lock);
+		if (!old_cmit || !new_cmit)
 			return error("Branch %s is missing commits.", b->name);
-		}
 
 		if (!in_merge_bases(old_cmit, new_cmit)) {
-			unlock_ref(lock);
 			warning("Not updating %s"
 				" (new tip %s does not contain %s)",
 				b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
 			return -1;
 		}
 	}
-	if (write_ref_sha1(lock, b->sha1, msg) < 0)
-		return error("Unable to update %s", b->name);
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_update(transaction, b->name, b->sha1, old_sha1,
+				   0, 1, &err) ||
+	    ref_transaction_commit(transaction, msg, &err)) {
+		ref_transaction_free(transaction);
+		error("%s", err.buf);
+		strbuf_release(&err);
+		return -1;
+	}
+	ref_transaction_free(transaction);
 	return 0;
 }
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 22/41] branch.c: use ref transaction for all ref updates
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (20 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 21/41] fast-import.c: change update_branch to use ref transactions Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 23/41] refs.c: change update_ref to use a transaction Ronnie Sahlberg
                   ` (19 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change create_branch to use a ref transaction when creating the new branch.

This also fixes a race condition in the old code where two concurrent
create_branch could race since the lock_any_ref_for_update/write_ref_sha1
did not protect against the ref already existing. I.e. one thread could end up
overwriting a branch even if the forcing flag is false.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 branch.c | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/branch.c b/branch.c
index 660097b..c1eae00 100644
--- a/branch.c
+++ b/branch.c
@@ -226,7 +226,6 @@ void create_branch(const char *head,
 		   int force, int reflog, int clobber_head,
 		   int quiet, enum branch_track track)
 {
-	struct ref_lock *lock = NULL;
 	struct commit *commit;
 	unsigned char sha1[20];
 	char *real_ref, msg[PATH_MAX + 20];
@@ -285,15 +284,6 @@ void create_branch(const char *head,
 		die(_("Not a valid branch point: '%s'."), start_name);
 	hashcpy(sha1, commit->object.sha1);
 
-	if (!dont_change_ref) {
-		lock = lock_any_ref_for_update(ref.buf, NULL, 0, NULL);
-		if (!lock)
-			die_errno(_("Failed to lock ref for update"));
-	}
-
-	if (reflog)
-		log_all_ref_updates = 1;
-
 	if (forcing)
 		snprintf(msg, sizeof msg, "branch: Reset to %s",
 			 start_name);
@@ -301,13 +291,25 @@ void create_branch(const char *head,
 		snprintf(msg, sizeof msg, "branch: Created from %s",
 			 start_name);
 
+	if (reflog)
+		log_all_ref_updates = 1;
+
+	if (!dont_change_ref) {
+		struct ref_transaction *transaction;
+		struct strbuf err = STRBUF_INIT;
+
+		transaction = ref_transaction_begin(&err);
+		if (!transaction ||
+		    ref_transaction_update(transaction, ref.buf, sha1,
+					   null_sha1, 0, !forcing, &err) ||
+		    ref_transaction_commit(transaction, msg, &err))
+			die("%s", err.buf);
+		ref_transaction_free(transaction);
+	}
+
 	if (real_ref && track)
 		setup_tracking(ref.buf + 11, real_ref, track, quiet);
 
-	if (!dont_change_ref)
-		if (write_ref_sha1(lock, sha1, msg) < 0)
-			die_errno(_("Failed to write ref"));
-
 	strbuf_release(&ref);
 	free(real_ref);
 }
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 23/41] refs.c: change update_ref to use a transaction
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (21 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 22/41] branch.c: use ref transaction for all ref updates Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 19:31   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 24/41] receive-pack.c: use a reference transaction for updating the refs Ronnie Sahlberg
                   ` (18 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change the update_ref helper function to use a ref transaction internally.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/refs.c b/refs.c
index 9992da4..fd8f3cf 100644
--- a/refs.c
+++ b/refs.c
@@ -3474,11 +3474,28 @@ int update_ref(const char *action, const char *refname,
 	       const unsigned char *sha1, const unsigned char *oldval,
 	       int flags, enum action_on_err onerr)
 {
-	struct ref_lock *lock;
-	lock = update_ref_lock(refname, oldval, flags, NULL, onerr);
-	if (!lock)
+	struct ref_transaction *t;
+	struct strbuf err = STRBUF_INIT;
+
+	t = ref_transaction_begin(&err);
+	if ((!t ||
+	    ref_transaction_update(t, refname, sha1, oldval, flags,
+				   !!oldval, &err)) ||
+	    (ref_transaction_commit(t, action, &err) && !(t = NULL))) {
+		const char *str = "update_ref failed for ref '%s': %s";
+
+		ref_transaction_free(t);
+		switch (onerr) {
+		case UPDATE_REFS_MSG_ON_ERR:
+			error(str, refname, err.buf); break;
+		case UPDATE_REFS_DIE_ON_ERR:
+			die(str, refname, err.buf); break;
+		case UPDATE_REFS_QUIET_ON_ERR: break;
+		}
+		strbuf_release(&err);
 		return 1;
-	return update_ref_write(action, refname, sha1, lock, NULL, onerr);
+	}
+	return 0;
 }
 
 static int ref_update_compare(const void *r1, const void *r2)
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 24/41] receive-pack.c: use a reference transaction for updating the refs
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (22 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 23/41] refs.c: change update_ref to use a transaction Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags Ronnie Sahlberg
                   ` (17 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Wrap all the ref updates inside a transaction.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/receive-pack.c | 43 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 34 insertions(+), 9 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index c323081..c88dc03 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -46,6 +46,8 @@ static void *head_name_to_free;
 static int sent_capabilities;
 static int shallow_update;
 static const char *alt_shallow_file;
+static int num_strings;
+static const char **error_strings;
 
 static enum deny_action parse_deny_action(const char *var, const char *value)
 {
@@ -468,6 +470,13 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
 	return 0;
 }
 
+static const char *add_error_string(const char *str)
+{
+	error_strings = xrealloc(error_strings,
+				 sizeof(*error_strings) * ++num_strings);
+	return error_strings[num_strings - 1] = xstrdup(str);
+}
+
 static const char *update(struct command *cmd, struct shallow_info *si)
 {
 	const char *name = cmd->ref_name;
@@ -475,7 +484,6 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 	const char *namespaced_name;
 	unsigned char *old_sha1 = cmd->old_sha1;
 	unsigned char *new_sha1 = cmd->new_sha1;
-	struct ref_lock *lock;
 
 	/* only refs/... are allowed */
 	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@@ -576,19 +584,27 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 		return NULL; /* good */
 	}
 	else {
+		struct strbuf err = STRBUF_INIT;
+		struct ref_transaction *transaction;
+
 		if (shallow_update && si->shallow_ref[cmd->index] &&
 		    update_shallow_ref(cmd, si))
 			return "shallow error";
 
-		lock = lock_any_ref_for_update(namespaced_name, old_sha1,
-					       0, NULL);
-		if (!lock) {
-			rp_error("failed to lock %s", name);
-			return "failed to lock";
-		}
-		if (write_ref_sha1(lock, new_sha1, "push")) {
-			return "failed to write"; /* error() already called */
+		transaction = ref_transaction_begin(&err);
+		if (!transaction ||
+		    ref_transaction_update(transaction, namespaced_name,
+					   new_sha1, old_sha1, 0, 1, &err) ||
+		    ref_transaction_commit(transaction, "push", &err)) {
+			const char *str = add_error_string(err.buf);
+			ref_transaction_free(transaction);
+			strbuf_release(&err);
+			rp_error("%s", str);
+			return str;
 		}
+
+		ref_transaction_free(transaction);
+		strbuf_release(&err);
 		return NULL; /* good */
 	}
 }
@@ -1120,6 +1136,14 @@ static int delete_only(struct command *commands)
 	return 1;
 }
 
+static void free_all_strings(void)
+{
+	int i;
+	for (i = 0; i < num_strings; i++)
+		free((void *)error_strings[i]);
+	free(error_strings);
+}
+
 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
 {
 	int advertise_refs = 0;
@@ -1166,6 +1190,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
 		usage(receive_pack_usage);
 
 	setup_path();
+	atexit(free_all_strings);
 
 	if (!enter_repo(dir, 0))
 		die("'%s' does not appear to be a git repository", dir);
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (23 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 24/41] receive-pack.c: use a reference transaction for updating the refs Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 19:47   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 26/41] walker.c: use ref transaction for ref updates Ronnie Sahlberg
                   ` (16 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 fast-import.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/fast-import.c b/fast-import.c
index 4a7b196..3db5b3d 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1735,15 +1735,22 @@ static void dump_tags(void)
 {
 	static const char *msg = "fast-import";
 	struct tag *t;
-	struct ref_lock *lock;
 	char ref_name[PATH_MAX];
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *transaction;
 
+	transaction = ref_transaction_begin(&err);
 	for (t = first_tag; t; t = t->next_tag) {
-		sprintf(ref_name, "tags/%s", t->name);
-		lock = lock_ref_sha1(ref_name, NULL);
-		if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
-			failure |= error("Unable to update %s", ref_name);
+		snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);
+
+		if (ref_transaction_update(transaction, ref_name, t->sha1,
+					   NULL, 0, 0, &err))
+			break;
 	}
+	if (ref_transaction_commit(transaction, msg, &err))
+		failure |= error("%s", err.buf);
+	ref_transaction_free(transaction);
+	strbuf_release(&err);
 }
 
 static void dump_marks_helper(FILE *f,
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 26/41] walker.c: use ref transaction for ref updates
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (24 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 19:56   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 27/41] refs.c: make lock_ref_sha1 static Ronnie Sahlberg
                   ` (15 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Switch to using ref transactions in walker_fetch(). As part of the refactoring
to use ref transactions we also fix a potential memory leak where in the
original code if write_ref_sha1() would fail we would end up returning from
the function without free()ing the msg string.

This changes the locking slightly for walker_fetch. Previously the code would
lock all refs before writing them but now we do not lock the refs until the
commit stage. There is thus a very short window where changes could be done
locally during the fetch which would be overwritten when the fetch completes
and commits its transaction. But this window should be reasonably short.
Even if this race does trigger, since both the old code and the new code
just overwrites the refs to the new values without checking or comparing
them with the previous value, this is not too dissimilar to a similar scenario
where you first do a ref change locally and then later do a fetch that
overwrites the local change. With this in mind I do not see the change in
locking semantics to be critical.

Note that this function is only called when fetching from a remote HTTP
repository onto the local (most of the time single-user) repository which
likely means that the type of collissions that the previous locking would
protect against and cause the fetch to fail for to be even more rare.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 walker.c | 56 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 33 insertions(+), 23 deletions(-)

diff --git a/walker.c b/walker.c
index 1dd86b8..51ce1c6 100644
--- a/walker.c
+++ b/walker.c
@@ -251,39 +251,37 @@ void walker_targets_free(int targets, char **target, const char **write_ref)
 int walker_fetch(struct walker *walker, int targets, char **target,
 		 const char **write_ref, const char *write_ref_log_details)
 {
-	struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
+	struct strbuf ref_name = STRBUF_INIT;
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *transaction;
 	unsigned char *sha1 = xmalloc(targets * 20);
 	char *msg;
-	int ret;
 	int i;
 
 	save_commit_buffer = 0;
 
-	for (i = 0; i < targets; i++) {
-		if (!write_ref || !write_ref[i])
-			continue;
-
-		lock[i] = lock_ref_sha1(write_ref[i], NULL);
-		if (!lock[i]) {
-			error("Can't lock ref %s", write_ref[i]);
-			goto unlock_and_fail;
+	if (write_ref) {
+		transaction = ref_transaction_begin(&err);
+		if (!transaction) {
+			error("%s", err.buf);
+			strbuf_release(&err);
+			return -1;
 		}
 	}
-
 	if (!walker->get_recover)
 		for_each_ref(mark_complete, NULL);
 
 	for (i = 0; i < targets; i++) {
 		if (interpret_target(walker, target[i], &sha1[20 * i])) {
 			error("Could not interpret response from server '%s' as something to pull", target[i]);
-			goto unlock_and_fail;
+			goto rollback_and_fail;
 		}
 		if (process(walker, lookup_unknown_object(&sha1[20 * i])))
-			goto unlock_and_fail;
+			goto rollback_and_fail;
 	}
 
 	if (loop(walker))
-		goto unlock_and_fail;
+		goto rollback_and_fail;
 
 	if (write_ref_log_details) {
 		msg = xmalloc(strlen(write_ref_log_details) + 12);
@@ -294,19 +292,31 @@ int walker_fetch(struct walker *walker, int targets, char **target,
 	for (i = 0; i < targets; i++) {
 		if (!write_ref || !write_ref[i])
 			continue;
-		ret = write_ref_sha1(lock[i], &sha1[20 * i], msg ? msg : "fetch (unknown)");
-		lock[i] = NULL;
-		if (ret)
-			goto unlock_and_fail;
+		strbuf_reset(&ref_name);
+		strbuf_addf(&ref_name, "refs/%s", write_ref[i]);
+		if (ref_transaction_update(transaction, ref_name.buf,
+					   &sha1[20 * i], NULL, 0, 0,
+					   &err))
+			break;
+	}
+	if (write_ref) {
+		if (ref_transaction_commit(transaction,
+					   msg ? msg : "fetch (unknown)",
+					   &err)) {
+			error("%s", err.buf);
+			ref_transaction_free(transaction);
+			goto rollback_and_fail;
+		}
+		ref_transaction_free(transaction);
 	}
-	free(msg);
 
+	free(msg);
 	return 0;
 
-unlock_and_fail:
-	for (i = 0; i < targets; i++)
-		if (lock[i])
-			unlock_ref(lock[i]);
+rollback_and_fail:
+	free(msg);
+	strbuf_release(&err);
+	strbuf_release(&ref_name);
 
 	return -1;
 }
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 27/41] refs.c: make lock_ref_sha1 static
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (25 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 26/41] walker.c: use ref transaction for ref updates Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 28/41] refs.c: remove the update_ref_lock function Ronnie Sahlberg
                   ` (14 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

No external callers reference lock_ref_sha1 any more so lets declare it static.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 2 +-
 refs.h | 3 ---
 2 files changed, 1 insertion(+), 4 deletions(-)

diff --git a/refs.c b/refs.c
index fd8f3cf..01d8ac4 100644
--- a/refs.c
+++ b/refs.c
@@ -2124,7 +2124,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 	return NULL;
 }
 
-struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)
+static struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)
 {
 	char refpath[PATH_MAX];
 	if (check_refname_format(refname, 0))
diff --git a/refs.h b/refs.h
index 6c830f2..1e25e1c 100644
--- a/refs.h
+++ b/refs.h
@@ -132,9 +132,6 @@ extern int ref_exists(const char *);
  */
 extern int peel_ref(const char *refname, unsigned char *sha1);
 
-/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/
-extern struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1);
-
 /** Locks any ref (for 'HEAD' type refs). */
 #define REF_NODEREF	0x01
 extern struct ref_lock *lock_any_ref_for_update(const char *refname,
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 28/41] refs.c: remove the update_ref_lock function
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (26 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 27/41] refs.c: make lock_ref_sha1 static Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 29/41] refs.c: remove the update_ref_write function Ronnie Sahlberg
                   ` (13 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Since we now only call update_ref_lock with onerr==QUIET_ON_ERR we no longer
need this function and can replace it with just calling lock_any_ref_for_update
directly.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)

diff --git a/refs.c b/refs.c
index 01d8ac4..0f82cdb 100644
--- a/refs.c
+++ b/refs.c
@@ -3281,24 +3281,6 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
 	return retval;
 }
 
-static struct ref_lock *update_ref_lock(const char *refname,
-					const unsigned char *oldval,
-					int flags, int *type_p,
-					enum action_on_err onerr)
-{
-	struct ref_lock *lock;
-	lock = lock_any_ref_for_update(refname, oldval, flags, type_p);
-	if (!lock) {
-		const char *str = "Cannot lock the ref '%s'.";
-		switch (onerr) {
-		case UPDATE_REFS_MSG_ON_ERR: error(str, refname); break;
-		case UPDATE_REFS_DIE_ON_ERR: die(str, refname); break;
-		case UPDATE_REFS_QUIET_ON_ERR: break;
-		}
-	}
-	return lock;
-}
-
 static int update_ref_write(const char *action, const char *refname,
 			    const unsigned char *sha1, struct ref_lock *lock,
 			    struct strbuf *err, enum action_on_err onerr)
@@ -3550,12 +3532,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	for (i = 0; i < n; i++) {
 		struct ref_update *update = updates[i];
 
-		update->lock = update_ref_lock(update->refname,
-					       (update->have_old ?
-						update->old_sha1 : NULL),
-					       update->flags,
-					       &update->type,
-					       UPDATE_REFS_QUIET_ON_ERR);
+		update->lock = lock_any_ref_for_update(update->refname,
+						       (update->have_old ?
+							update->old_sha1 :
+							NULL),
+						       update->flags,
+						       &update->type);
 		if (!update->lock) {
 			if (err)
 				strbuf_addf(err, "Cannot lock the ref '%s'.",
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 29/41] refs.c: remove the update_ref_write function
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (27 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 28/41] refs.c: remove the update_ref_lock function Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 20:39   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 30/41] refs.c: remove lock_ref_sha1 Ronnie Sahlberg
                   ` (12 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Since we only call update_ref_write from a single place and we only call it
with onerr==QUIET_ON_ERR we can just as well get rid of it and just call
write_ref_sha1 directly. This changes the return status for _commit from
1 to -1 on failures when writing to the ref. Eventually we will want
_commit to start returning more detailed error conditions than the current
simple success/failure. For example if the commit failed due to name
conflicts etc.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 35 +++++++++--------------------------
 1 file changed, 9 insertions(+), 26 deletions(-)

diff --git a/refs.c b/refs.c
index 0f82cdb..979b79a 100644
--- a/refs.c
+++ b/refs.c
@@ -3281,25 +3281,6 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
 	return retval;
 }
 
-static int update_ref_write(const char *action, const char *refname,
-			    const unsigned char *sha1, struct ref_lock *lock,
-			    struct strbuf *err, enum action_on_err onerr)
-{
-	if (write_ref_sha1(lock, sha1, action) < 0) {
-		const char *str = "Cannot update the ref '%s'.";
-		if (err)
-			strbuf_addf(err, str, refname);
-
-		switch (onerr) {
-		case UPDATE_REFS_MSG_ON_ERR: error(str, refname); break;
-		case UPDATE_REFS_DIE_ON_ERR: die(str, refname); break;
-		case UPDATE_REFS_QUIET_ON_ERR: break;
-		}
-		return 1;
-	}
-	return 0;
-}
-
 /**
  * Information needed for a single ref update.  Set new_sha1 to the
  * new value or to zero to delete the ref.  To check the old value
@@ -3552,14 +3533,16 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 		struct ref_update *update = updates[i];
 
 		if (!is_null_sha1(update->new_sha1)) {
-			ret = update_ref_write(msg,
-					       update->refname,
-					       update->new_sha1,
-					       update->lock, err,
-					       UPDATE_REFS_QUIET_ON_ERR);
-			update->lock = NULL; /* freed by update_ref_write */
-			if (ret)
+			ret = write_ref_sha1(update->lock, update->new_sha1,
+					     msg);
+			update->lock = NULL; /* freed by write_ref_sha1 */
+			if (ret) {
+				const char *str = "Cannot update the ref '%s'.";
+
+				if (err)
+					strbuf_addf(err, str, update->refname);
 				goto cleanup;
+			}
 		}
 	}
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 30/41] refs.c: remove lock_ref_sha1
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (28 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 29/41] refs.c: remove the update_ref_write function Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 31/41] refs.c: make prune_ref use a transaction to delete the ref Ronnie Sahlberg
                   ` (11 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

lock_ref_sha1 was only called from one place in refc.c and only provided
a check that the refname was sane before adding back the initial "refs/"
part of the ref path name, the initial "refs/" that this caller had already
stripped off before calling lock_ref_sha1.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/refs.c b/refs.c
index 979b79a..4ca84f7 100644
--- a/refs.c
+++ b/refs.c
@@ -2124,15 +2124,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 	return NULL;
 }
 
-static struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1)
-{
-	char refpath[PATH_MAX];
-	if (check_refname_format(refname, 0))
-		return NULL;
-	strcpy(refpath, mkpath("refs/%s", refname));
-	return lock_ref_sha1_basic(refpath, old_sha1, 0, NULL);
-}
-
 struct ref_lock *lock_any_ref_for_update(const char *refname,
 					 const unsigned char *old_sha1,
 					 int flags, int *type_p)
@@ -2337,8 +2328,12 @@ static void try_remove_empty_parents(char *name)
 /* make sure nobody touched the ref, and unlink */
 static void prune_ref(struct ref_to_prune *r)
 {
-	struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
+	struct ref_lock *lock;
+
+	if (check_refname_format(r->name + 5, 0))
+		return;
 
+	lock = lock_ref_sha1_basic(r->name, r->sha1, 0, NULL);
 	if (lock) {
 		unlink_or_warn(git_path("%s", r->name));
 		unlock_ref(lock);
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 31/41] refs.c: make prune_ref use a transaction to delete the ref
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (29 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 30/41] refs.c: remove lock_ref_sha1 Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-28 21:51   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 32/41] refs.c: make delete_ref use a transaction Ronnie Sahlberg
                   ` (10 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change prune_ref to delete the ref using a ref transaction. To do this we also
need to add a new flag REF_ISPRUNING that will tell the transaction that we
do not want to delete this ref from the packed refs. This flag is private to
refs.c and not exposed to external callers.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 28 +++++++++++++++++++++-------
 refs.h |  5 +++++
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/refs.c b/refs.c
index 4ca84f7..1819434 100644
--- a/refs.c
+++ b/refs.c
@@ -30,6 +30,12 @@ static inline int bad_ref_char(int ch)
 }
 
 /*
+ * Used as a flag to ref_transaction_delete when a loose ref is being
+ * pruned.
+ */
+#define REF_ISPRUNING	0x0100
+
+/*
  * Try to read one refname component from the front of refname.  Return
  * the length of the component found, or -1 if the component is not
  * legal.
@@ -2328,17 +2334,24 @@ static void try_remove_empty_parents(char *name)
 /* make sure nobody touched the ref, and unlink */
 static void prune_ref(struct ref_to_prune *r)
 {
-	struct ref_lock *lock;
+	struct ref_transaction *transaction;
+	struct strbuf err = STRBUF_INIT;
 
 	if (check_refname_format(r->name + 5, 0))
 		return;
 
-	lock = lock_ref_sha1_basic(r->name, r->sha1, 0, NULL);
-	if (lock) {
-		unlink_or_warn(git_path("%s", r->name));
-		unlock_ref(lock);
-		try_remove_empty_parents(r->name);
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_delete(transaction, r->name, r->sha1,
+				   REF_ISPRUNING, 1, &err) ||
+	    ref_transaction_commit(transaction, NULL, &err)) {
+		ref_transaction_free(transaction);
+		error("%s", err.buf);
+		strbuf_release(&err);
+		return;
 	}
+	ref_transaction_free(transaction);
+	try_remove_empty_parents(r->name);
 }
 
 static void prune_refs(struct ref_to_prune *r)
@@ -3546,9 +3559,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 		struct ref_update *update = updates[i];
 
 		if (update->lock) {
-			delnames[delnum++] = update->lock->ref_name;
 			ret |= delete_ref_loose(update->lock, update->type,
 						err);
+			if (!(update->flags & REF_ISPRUNING))
+				delnames[delnum++] = update->lock->ref_name;
 		}
 	}
 
diff --git a/refs.h b/refs.h
index 1e25e1c..39c97f8 100644
--- a/refs.h
+++ b/refs.h
@@ -235,6 +235,11 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
  */
 
 /*
+ * ref_transaction_update ref_transaction_create and ref_transaction_delete
+ * all take a flag argument. Currently the only public flag is REF_NODEREF.
+ * Flag values >= 0x100 are reserved for internal use.
+ */
+/*
  * Add a reference update to transaction.  new_sha1 is the value that
  * the reference should have after the update, or zeros if it should
  * be deleted.  If have_old is true, then old_sha1 holds the value
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 32/41] refs.c: make delete_ref use a transaction
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (30 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 31/41] refs.c: make prune_ref use a transaction to delete the ref Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-30 17:28   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit Ronnie Sahlberg
                   ` (9 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change delete_ref to use a ref transaction for the deletion. At the same time
since we no longer have any callers of repack_without_ref we can now delete
this function.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)

diff --git a/refs.c b/refs.c
index 1819434..88e7bb6 100644
--- a/refs.c
+++ b/refs.c
@@ -2494,11 +2494,6 @@ static int repack_without_refs(const char **refnames, int n, struct strbuf *err)
 	return ret;
 }
 
-static int repack_without_ref(const char *refname)
-{
-	return repack_without_refs(&refname, 1, NULL);
-}
-
 static int add_err_if_unremovable(const char *op, const char *file,
 				  struct strbuf *e, int rc)
 {
@@ -2542,24 +2537,21 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
 
 int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
 {
-	struct ref_lock *lock;
-	int ret = 0, flag = 0;
+	struct ref_transaction *transaction;
+	struct strbuf err = STRBUF_INIT;
 
-	lock = lock_ref_sha1_basic(refname, sha1, delopt, &flag);
-	if (!lock)
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_delete(transaction, refname, sha1, delopt,
+				   sha1 && !is_null_sha1(sha1), &err) ||
+	    ref_transaction_commit(transaction, NULL, &err)) {
+		error("%s", err.buf);
+		ref_transaction_free(transaction);
+		strbuf_release(&err);
 		return 1;
-	ret |= delete_ref_loose(lock, flag, NULL);
-
-	/* removing the loose one could have resurrected an earlier
-	 * packed one.  Also, if it was not loose we need to repack
-	 * without it.
-	 */
-	ret |= repack_without_ref(lock->ref_name);
-
-	unlink_or_warn(git_path("logs/%s", lock->ref_name));
-	clear_loose_ref_cache(&ref_cache);
-	unlock_ref(lock);
-	return ret;
+	}
+	ref_transaction_free(transaction);
+	return 0;
 }
 
 /*
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (31 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 32/41] refs.c: make delete_ref use a transaction Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-30 17:38   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 34/41] refs.c: pass NULL as *flags to read_ref_full Ronnie Sahlberg
                   ` (8 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change the reference transactions so that we pass the reflog message
through to the create/delete/update function instead of the commit message.
This allows for individual messages for each change in a multi ref
transaction.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 branch.c               |  4 ++--
 builtin/commit.c       |  4 ++--
 builtin/fetch.c        |  3 +--
 builtin/receive-pack.c |  5 +++--
 builtin/replace.c      |  4 ++--
 builtin/tag.c          |  4 ++--
 builtin/update-ref.c   | 13 +++++++------
 fast-import.c          |  8 ++++----
 refs.c                 | 36 ++++++++++++++++++++++--------------
 refs.h                 |  8 ++++----
 sequencer.c            |  4 ++--
 walker.c               |  5 ++---
 12 files changed, 53 insertions(+), 45 deletions(-)

diff --git a/branch.c b/branch.c
index c1eae00..e0439af 100644
--- a/branch.c
+++ b/branch.c
@@ -301,8 +301,8 @@ void create_branch(const char *head,
 		transaction = ref_transaction_begin(&err);
 		if (!transaction ||
 		    ref_transaction_update(transaction, ref.buf, sha1,
-					   null_sha1, 0, !forcing, &err) ||
-		    ref_transaction_commit(transaction, msg, &err))
+					   null_sha1, 0, !forcing, msg, &err) ||
+		    ref_transaction_commit(transaction, &err))
 			die("%s", err.buf);
 		ref_transaction_free(transaction);
 	}
diff --git a/builtin/commit.c b/builtin/commit.c
index 7db194f..608296c 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1721,8 +1721,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	    ref_transaction_update(transaction, "HEAD", sha1,
 				   current_head ?
 				   current_head->object.sha1 : NULL,
-				   0, !!current_head, &err) ||
-	    ref_transaction_commit(transaction, sb.buf, &err)) {
+				   0, !!current_head, sb.buf, &err) ||
+	    ref_transaction_commit(transaction, &err)) {
 		rollback_index_files();
 		die("%s", err.buf);
 	}
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 55f457c..faa1233 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -673,10 +673,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 			}
 		}
 	}
-
 	if (rc & STORE_REF_ERROR_DF_CONFLICT)
 		error(_("some local refs could not be updated; try running\n"
-		      " 'git remote prune %s' to remove any old, conflicting "
+		      "'git remote prune %s' to remove any old, conflicting "
 		      "branches"), remote_name);
 
  abort:
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index c88dc03..c91d3d6 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -594,8 +594,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 		transaction = ref_transaction_begin(&err);
 		if (!transaction ||
 		    ref_transaction_update(transaction, namespaced_name,
-					   new_sha1, old_sha1, 0, 1, &err) ||
-		    ref_transaction_commit(transaction, "push", &err)) {
+					   new_sha1, old_sha1, 0, 1, "push",
+					   &err) ||
+		    ref_transaction_commit(transaction, &err)) {
 			const char *str = add_error_string(err.buf);
 			ref_transaction_free(transaction);
 			strbuf_release(&err);
diff --git a/builtin/replace.c b/builtin/replace.c
index f24d814..09bfd40 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -160,8 +160,8 @@ static int replace_object_sha1(const char *object_ref,
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_update(transaction, ref, repl, prev,
-				   0, !is_null_sha1(prev), &err) ||
-	    ref_transaction_commit(transaction, NULL, &err))
+				   0, !is_null_sha1(prev), NULL, &err) ||
+	    ref_transaction_commit(transaction, &err))
 		die("%s", err.buf);
 
 	ref_transaction_free(transaction);
diff --git a/builtin/tag.c b/builtin/tag.c
index c9bfc9a..74af63e 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -705,8 +705,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_update(transaction, ref.buf, object, prev,
-				   0, !is_null_sha1(prev), &err) ||
-	    ref_transaction_commit(transaction, NULL, &err))
+				   0, !is_null_sha1(prev), NULL, &err) ||
+	    ref_transaction_commit(transaction, &err))
 		die("%s", err.buf);
 	ref_transaction_free(transaction);
 	if (force && !is_null_sha1(prev) && hashcmp(prev, object))
diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index c6ad0be..28b478a 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -16,6 +16,7 @@ static struct ref_transaction *transaction;
 
 static char line_termination = '\n';
 static int update_flags;
+static const char *msg;
 static struct strbuf err = STRBUF_INIT;
 
 /*
@@ -199,7 +200,7 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)
 		die("update %s: extra input: %s", refname, next);
 
 	if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
-				   update_flags, have_old, &err))
+				   update_flags, have_old, msg, &err))
 		die("%s", err.buf);
 
 	update_flags = 0;
@@ -227,7 +228,7 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)
 		die("create %s: extra input: %s", refname, next);
 
 	if (ref_transaction_create(transaction, refname, new_sha1,
-				   update_flags, &err))
+				   update_flags, msg, &err))
 		die("%s", err.buf);
 
 	update_flags = 0;
@@ -259,7 +260,7 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)
 		die("delete %s: extra input: %s", refname, next);
 
 	if (ref_transaction_delete(transaction, refname, old_sha1,
-				   update_flags, have_old, &err))
+				   update_flags, have_old, msg, &err))
 		die("%s", err.buf);
 
 	update_flags = 0;
@@ -292,7 +293,7 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)
 		die("verify %s: extra input: %s", refname, next);
 
 	if (ref_transaction_update(transaction, refname, new_sha1, old_sha1,
-				   update_flags, have_old, &err))
+				   update_flags, have_old, msg, &err))
 		die("%s", err.buf);
 
 	update_flags = 0;
@@ -345,7 +346,7 @@ static void update_refs_stdin(void)
 
 int cmd_update_ref(int argc, const char **argv, const char *prefix)
 {
-	const char *refname, *oldval, *msg = NULL;
+	const char *refname, *oldval;
 	unsigned char sha1[20], oldsha1[20];
 	int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
 	struct option options[] = {
@@ -371,7 +372,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		if (end_null)
 			line_termination = '\0';
 		update_refs_stdin();
-		if (ref_transaction_commit(transaction, msg, &err))
+		if (ref_transaction_commit(transaction, &err))
 			die("%s", err.buf);
 		ref_transaction_free(transaction);
 		return 0;
diff --git a/fast-import.c b/fast-import.c
index 3db5b3d..b98091d 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1709,8 +1709,8 @@ static int update_branch(struct branch *b)
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_update(transaction, b->name, b->sha1, old_sha1,
-				   0, 1, &err) ||
-	    ref_transaction_commit(transaction, msg, &err)) {
+				   0, 1, msg, &err) ||
+	    ref_transaction_commit(transaction, &err)) {
 		ref_transaction_free(transaction);
 		error("%s", err.buf);
 		strbuf_release(&err);
@@ -1744,10 +1744,10 @@ static void dump_tags(void)
 		snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);
 
 		if (ref_transaction_update(transaction, ref_name, t->sha1,
-					   NULL, 0, 0, &err))
+					   NULL, 0, 0, msg, &err))
 			break;
 	}
-	if (ref_transaction_commit(transaction, msg, &err))
+	if (ref_transaction_commit(transaction, &err))
 		failure |= error("%s", err.buf);
 	ref_transaction_free(transaction);
 	strbuf_release(&err);
diff --git a/refs.c b/refs.c
index 88e7bb6..e485d2f 100644
--- a/refs.c
+++ b/refs.c
@@ -2343,8 +2343,8 @@ static void prune_ref(struct ref_to_prune *r)
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_delete(transaction, r->name, r->sha1,
-				   REF_ISPRUNING, 1, &err) ||
-	    ref_transaction_commit(transaction, NULL, &err)) {
+				   REF_ISPRUNING, 1, NULL, &err) ||
+	    ref_transaction_commit(transaction, &err)) {
 		ref_transaction_free(transaction);
 		error("%s", err.buf);
 		strbuf_release(&err);
@@ -2543,8 +2543,8 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_delete(transaction, refname, sha1, delopt,
-				   sha1 && !is_null_sha1(sha1), &err) ||
-	    ref_transaction_commit(transaction, NULL, &err)) {
+				   sha1 && !is_null_sha1(sha1), NULL, &err) ||
+	    ref_transaction_commit(transaction, &err)) {
 		error("%s", err.buf);
 		ref_transaction_free(transaction);
 		strbuf_release(&err);
@@ -3294,6 +3294,7 @@ struct ref_update {
 	int have_old; /* 1 if old_sha1 is valid, 0 otherwise */
 	struct ref_lock *lock;
 	int type;
+	char *msg;
 	const char refname[FLEX_ARRAY];
 };
 
@@ -3343,9 +3344,10 @@ void ref_transaction_free(struct ref_transaction *transaction)
 	if (!transaction)
 		return;
 
-	for (i = 0; i < transaction->nr; i++)
+	for (i = 0; i < transaction->nr; i++) {
+		free(transaction->updates[i]->msg);
 		free(transaction->updates[i]);
-
+	}
 	free(transaction->updates);
 	free(transaction);
 }
@@ -3366,7 +3368,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
 			   const char *refname,
 			   const unsigned char *new_sha1,
 			   const unsigned char *old_sha1,
-			   int flags, int have_old,
+			   int flags, int have_old, const char *msg,
 			   struct strbuf *err)
 {
 	struct ref_update *update;
@@ -3383,13 +3385,15 @@ int ref_transaction_update(struct ref_transaction *transaction,
 	update->have_old = have_old;
 	if (have_old)
 		hashcpy(update->old_sha1, old_sha1);
+	if (msg)
+		update->msg = xstrdup(msg);
 	return 0;
 }
 
 int ref_transaction_create(struct ref_transaction *transaction,
 			   const char *refname,
 			   const unsigned char *new_sha1,
-			   int flags,
+			   int flags, const char *msg,
 			   struct strbuf *err)
 {
 	struct ref_update *update;
@@ -3406,13 +3410,15 @@ int ref_transaction_create(struct ref_transaction *transaction,
 	hashclr(update->old_sha1);
 	update->flags = flags;
 	update->have_old = 1;
+	if (msg)
+		update->msg = xstrdup(msg);
 	return 0;
 }
 
 int ref_transaction_delete(struct ref_transaction *transaction,
 			   const char *refname,
 			   const unsigned char *old_sha1,
-			   int flags, int have_old,
+			   int flags, int have_old, const char *msg,
 			   struct strbuf *err)
 {
 	struct ref_update *update;
@@ -3430,6 +3436,8 @@ int ref_transaction_delete(struct ref_transaction *transaction,
 		assert(!is_null_sha1(old_sha1));
 		hashcpy(update->old_sha1, old_sha1);
 	}
+	if (msg)
+		update->msg = xstrdup(msg);
 	return 0;
 }
 
@@ -3441,10 +3449,10 @@ int update_ref(const char *action, const char *refname,
 	struct strbuf err = STRBUF_INIT;
 
 	t = ref_transaction_begin(&err);
-	if ((!t ||
+	if (!t ||
 	    ref_transaction_update(t, refname, sha1, oldval, flags,
-				   !!oldval, &err)) ||
-	    (ref_transaction_commit(t, action, &err) && !(t = NULL))) {
+				   !!oldval, action, &err) ||
+	    ref_transaction_commit(t, &err)) {
 		const char *str = "update_ref failed for ref '%s': %s";
 
 		ref_transaction_free(t);
@@ -3485,7 +3493,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 }
 
 int ref_transaction_commit(struct ref_transaction *transaction,
-			   const char *msg, struct strbuf *err)
+			   struct strbuf *err)
 {
 	int ret = 0, delnum = 0, i;
 	const char **delnames;
@@ -3534,7 +3542,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 		if (!is_null_sha1(update->new_sha1)) {
 			ret = write_ref_sha1(update->lock, update->new_sha1,
-					     msg);
+					     update->msg);
 			update->lock = NULL; /* freed by write_ref_sha1 */
 			if (ret) {
 				const char *str = "Cannot update the ref '%s'.";
diff --git a/refs.h b/refs.h
index 39c97f8..84e12b5 100644
--- a/refs.h
+++ b/refs.h
@@ -253,7 +253,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
 			   const char *refname,
 			   const unsigned char *new_sha1,
 			   const unsigned char *old_sha1,
-			   int flags, int have_old,
+			   int flags, int have_old, const char *msg,
 			   struct strbuf *err);
 
 /*
@@ -268,7 +268,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
 int ref_transaction_create(struct ref_transaction *transaction,
 			   const char *refname,
 			   const unsigned char *new_sha1,
-			   int flags,
+			   int flags, const char *msg,
 			   struct strbuf *err);
 
 /*
@@ -282,7 +282,7 @@ int ref_transaction_create(struct ref_transaction *transaction,
 int ref_transaction_delete(struct ref_transaction *transaction,
 			   const char *refname,
 			   const unsigned char *old_sha1,
-			   int flags, int have_old,
+			   int flags, int have_old, const char *msg,
 			   struct strbuf *err);
 
 /*
@@ -291,7 +291,7 @@ int ref_transaction_delete(struct ref_transaction *transaction,
  * problem.
  */
 int ref_transaction_commit(struct ref_transaction *transaction,
-			   const char *msg, struct strbuf *err);
+			   struct strbuf *err);
 
 /*
  * Free an existing transaction and all associated data.
diff --git a/sequencer.c b/sequencer.c
index fd8acaf..f9906ef 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -285,8 +285,8 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_update(transaction, "HEAD", to, from,
-				   0, !unborn, &err) ||
-	    ref_transaction_commit(transaction, sb.buf, &err)) {
+				   0, !unborn, sb.buf, &err) ||
+	    ref_transaction_commit(transaction, &err)) {
 		ref_transaction_free(transaction);
 		error("%s", err.buf);
 		strbuf_release(&sb);
diff --git a/walker.c b/walker.c
index 51ce1c6..584189f 100644
--- a/walker.c
+++ b/walker.c
@@ -296,13 +296,12 @@ int walker_fetch(struct walker *walker, int targets, char **target,
 		strbuf_addf(&ref_name, "refs/%s", write_ref[i]);
 		if (ref_transaction_update(transaction, ref_name.buf,
 					   &sha1[20 * i], NULL, 0, 0,
+					   msg ? msg : "fetch (unknown)",
 					   &err))
 			break;
 	}
 	if (write_ref) {
-		if (ref_transaction_commit(transaction,
-					   msg ? msg : "fetch (unknown)",
-					   &err)) {
+		if (ref_transaction_commit(transaction, &err)) {
 			error("%s", err.buf);
 			ref_transaction_free(transaction);
 			goto rollback_and_fail;
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 34/41] refs.c: pass NULL as *flags to read_ref_full
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (32 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 35/41] refs.c: pack all refs before we start to rename a ref Ronnie Sahlberg
                   ` (7 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

We call read_ref_full with a pointer to flags from rename_ref but since
we never actually use the returned flags we can just pass NULL here instead.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/refs.c b/refs.c
index e485d2f..3cdfc72 100644
--- a/refs.c
+++ b/refs.c
@@ -2642,7 +2642,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 		goto rollback;
 	}
 
-	if (!read_ref_full(newrefname, sha1, 1, &flag) &&
+	if (!read_ref_full(newrefname, sha1, 1, NULL) &&
 	    delete_ref(newrefname, sha1, REF_NODEREF)) {
 		if (errno==EISDIR) {
 			if (remove_empty_directories(git_path("%s", newrefname))) {
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 35/41] refs.c: pack all refs before we start to rename a ref
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (33 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 34/41] refs.c: pass NULL as *flags to read_ref_full Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-30 18:08   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic Ronnie Sahlberg
                   ` (6 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

This means that most loose refs will no longer be present after the rename
which triggered a test failure since it assumes the file for an unrelated
ref would still be present after the rename.

We want to do this to make it easier to handle atomic renames in rename_ref for
the case 'git branch -m foo/bar foo'. If we can guarantee that foo/bar does not
exist as a loose ref we can avoid locking foo/bar.lock during transaction
commit and thus make it possible to delete the foo directory and re-create
it as a file(branch) in a single transaction.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c            | 3 +++
 t/t3200-branch.sh | 6 +++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/refs.c b/refs.c
index 3cdfc72..08dde5b 100644
--- a/refs.c
+++ b/refs.c
@@ -2637,6 +2637,9 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 		return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
 			oldrefname, strerror(errno));
 
+	if (pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE))
+		return error("unable to pack refs");
+
 	if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
 		error("unable to delete old %s", oldrefname);
 		goto rollback;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index ac31b71..de0c2b9 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -288,9 +288,9 @@ test_expect_success 'deleting a dangling symref' '
 test_expect_success 'renaming a symref is not allowed' '
 	git symbolic-ref refs/heads/master2 refs/heads/master &&
 	test_must_fail git branch -m master2 master3 &&
-	git symbolic-ref refs/heads/master2 &&
-	test_path_is_file .git/refs/heads/master &&
-	test_path_is_missing .git/refs/heads/master3
+	git rev-parse --verify refs/heads/master &&
+	test_must_fail git symbolic-ref refs/heads/master3 &&
+	test_must_fail git rev-parse refs/heads/master3
 '
 
 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (34 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 35/41] refs.c: pack all refs before we start to rename a ref Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-30 18:12   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 37/41] refs.c: call lock_ref_sha1_basic directly from commit Ronnie Sahlberg
                   ` (5 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Move the check for check_refname_format from lock_any_ref_for_update
to lock_ref_sha1_basic. At some later stage we will get rid of
lock_any_ref_for_update completely.

This leaves lock_any_ref_for_updates as a no-op wrapper which could be removed.
But this wrapper is also called from an external caller and we will soon
make changes to the signature to lock_ref_sha1_basic that we do not want to
expose to that caller.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/refs.c b/refs.c
index 08dde5b..2952871 100644
--- a/refs.c
+++ b/refs.c
@@ -2043,6 +2043,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 	int missing = 0;
 	int attempts_remaining = 3;
 
+	if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
+		return NULL;
+
 	lock = xcalloc(1, sizeof(struct ref_lock));
 	lock->lock_fd = -1;
 
@@ -2134,8 +2137,6 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,
 					 const unsigned char *old_sha1,
 					 int flags, int *type_p)
 {
-	if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
-		return NULL;
 	return lock_ref_sha1_basic(refname, old_sha1, flags, type_p);
 }
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 37/41] refs.c: call lock_ref_sha1_basic directly from commit
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (35 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 38/41] refs.c: pass a skip list to name_conflict_fn Ronnie Sahlberg
                   ` (4 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Skip using the lock_any_ref_for_update wrapper and call lock_ref_sha1_basic
directly from the commit function.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/refs.c b/refs.c
index 2952871..28138ea 100644
--- a/refs.c
+++ b/refs.c
@@ -3525,12 +3525,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	for (i = 0; i < n; i++) {
 		struct ref_update *update = updates[i];
 
-		update->lock = lock_any_ref_for_update(update->refname,
-						       (update->have_old ?
-							update->old_sha1 :
-							NULL),
-						       update->flags,
-						       &update->type);
+		update->lock = lock_ref_sha1_basic(update->refname,
+						   (update->have_old ?
+						    update->old_sha1 :
+						    NULL),
+						   update->flags,
+						   &update->type);
 		if (!update->lock) {
 			if (err)
 				strbuf_addf(err, "Cannot lock the ref '%s'.",
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 38/41] refs.c: pass a skip list to name_conflict_fn
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (36 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 37/41] refs.c: call lock_ref_sha1_basic directly from commit Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:25 ` [PATCH v11 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers Ronnie Sahlberg
                   ` (3 subsequent siblings)
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Allow passing a list of refs to skip checking to name_conflict_fn.
There are some conditions where we want to allow a temporary conflict and skip
checking those refs. For example if we have a transaction that
1, guarantees that m is a packed refs and there is no loose ref for m
2, the transaction will delete m from the packed ref
3, the transaction will create conflicting m/m

For this case we want to be able to lock and create m/m since we know that the
conflict is only transient. I.e. the conflict will be automatically resolved
by the transaction when it deletes m.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)

diff --git a/refs.c b/refs.c
index 28138ea..d3812b7 100644
--- a/refs.c
+++ b/refs.c
@@ -793,11 +793,17 @@ struct name_conflict_cb {
 	const char *refname;
 	const char *oldrefname;
 	const char *conflicting_refname;
+	const char **skip;
+	int skipnum;
 };
 
 static int name_conflict_fn(struct ref_entry *entry, void *cb_data)
 {
 	struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data;
+	int i;
+	for (i = 0; i < data->skipnum; i++)
+		if (!strcmp(entry->name, data->skip[i]))
+			return 0;
 	if (data->oldrefname && !strcmp(data->oldrefname, entry->name))
 		return 0;
 	if (names_conflict(data->refname, entry->name)) {
@@ -812,15 +818,19 @@ static int name_conflict_fn(struct ref_entry *entry, void *cb_data)
  * conflicting with the name of an existing reference in dir.  If
  * oldrefname is non-NULL, ignore potential conflicts with oldrefname
  * (e.g., because oldrefname is scheduled for deletion in the same
- * operation).
+ * operation). skip contains a list of refs we want to skip checking for
+ * conflicts with.
  */
 static int is_refname_available(const char *refname, const char *oldrefname,
-				struct ref_dir *dir)
+				struct ref_dir *dir,
+				const char **skip, int skipnum)
 {
 	struct name_conflict_cb data;
 	data.refname = refname;
 	data.oldrefname = oldrefname;
 	data.conflicting_refname = NULL;
+	data.skip = skip;
+	data.skipnum = skipnum;
 
 	sort_ref_dir(dir);
 	if (do_for_each_entry_in_dir(dir, 0, name_conflict_fn, &data)) {
@@ -2032,7 +2042,8 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
 
 static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 					    const unsigned char *old_sha1,
-					    int flags, int *type_p)
+					    int flags, int *type_p,
+					    const char **skip, int skipnum)
 {
 	char *ref_file;
 	const char *orig_refname = refname;
@@ -2079,7 +2090,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 	 * name is a proper prefix of our refname.
 	 */
 	if (missing &&
-	     !is_refname_available(refname, NULL, get_packed_refs(&ref_cache))) {
+	     !is_refname_available(refname, NULL,
+				   get_packed_refs(&ref_cache),
+				   skip, skipnum)) {
 		last_errno = ENOTDIR;
 		goto error_return;
 	}
@@ -2137,7 +2150,7 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,
 					 const unsigned char *old_sha1,
 					 int flags, int *type_p)
 {
-	return lock_ref_sha1_basic(refname, old_sha1, flags, type_p);
+	return lock_ref_sha1_basic(refname, old_sha1, flags, type_p, NULL, 0);
 }
 
 /*
@@ -2618,6 +2631,9 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
 	const char *symref = NULL;
 
+	if (!strcmp(oldrefname, newrefname))
+		return 0;
+
 	if (log && S_ISLNK(loginfo.st_mode))
 		return error("reflog for %s is a symlink", oldrefname);
 
@@ -2628,10 +2644,12 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	if (!symref)
 		return error("refname %s not found", oldrefname);
 
-	if (!is_refname_available(newrefname, oldrefname, get_packed_refs(&ref_cache)))
+	if (!is_refname_available(newrefname, oldrefname,
+				  get_packed_refs(&ref_cache), NULL, 0))
 		return 1;
 
-	if (!is_refname_available(newrefname, oldrefname, get_loose_refs(&ref_cache)))
+	if (!is_refname_available(newrefname, oldrefname,
+				  get_loose_refs(&ref_cache), NULL, 0))
 		return 1;
 
 	if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
@@ -2664,7 +2682,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 
 	logmoved = log;
 
-	lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL);
+	lock = lock_ref_sha1_basic(newrefname, NULL, 0, NULL, NULL, 0);
 	if (!lock) {
 		error("unable to lock %s for update", newrefname);
 		goto rollback;
@@ -2679,7 +2697,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 	return 0;
 
  rollback:
-	lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL);
+	lock = lock_ref_sha1_basic(oldrefname, NULL, 0, NULL, NULL, 0);
 	if (!lock) {
 		error("unable to lock %s for rollback", oldrefname);
 		goto rollbacklog;
@@ -3530,7 +3548,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 						    update->old_sha1 :
 						    NULL),
 						   update->flags,
-						   &update->type);
+						   &update->type,
+						   delnames, delnum);
 		if (!update->lock) {
 			if (err)
 				strbuf_addf(err, "Cannot lock the ref '%s'.",
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (37 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 38/41] refs.c: pass a skip list to name_conflict_fn Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-31  0:22   ` Jonathan Nieder
  2014-05-27 20:25 ` [PATCH v11 40/41] fetch.c: change s_update_ref to use a ref transaction Ronnie Sahlberg
                   ` (2 subsequent siblings)
  41 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

In _commit, ENOTDIR can happen in the call to lock_ref_sha1_basic, either when
we lstat the new refname and it returns ENOTDIR or if the name checking
function reports that the same type of conflict happened. In both cases it
means that we can not create the new ref due to a name conflict.

For these cases, save the errno value and abort and make sure that the caller
can see errno==ENOTDIR.

Also start defining specific return codes for _commit, assign -1 as a generic
error and -2 as the error that refers to a name conflict. Callers can (and
should) use that return value inspecting errno directly.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 23 ++++++++++++++++-------
 refs.h |  4 ++++
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/refs.c b/refs.c
index d3812b7..c108007 100644
--- a/refs.c
+++ b/refs.c
@@ -3517,7 +3517,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
-	int ret = 0, delnum = 0, i;
+	int ret = 0, delnum = 0, i, save_errno = 0;
 	const char **delnames;
 	int n = transaction->nr;
 	struct ref_update **updates = transaction->updates;
@@ -3535,9 +3535,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 	/* Copy, sort, and reject duplicate refs */
 	qsort(updates, n, sizeof(*updates), ref_update_compare);
-	ret = ref_update_reject_duplicates(updates, n, err);
-	if (ret)
+	if (ref_update_reject_duplicates(updates, n, err)) {
+		ret = -1;
 		goto cleanup;
+	}
 
 	/* Acquire all locks while verifying old values */
 	for (i = 0; i < n; i++) {
@@ -3551,10 +3552,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 						   &update->type,
 						   delnames, delnum);
 		if (!update->lock) {
+			if (errno == ENOTDIR)
+				save_errno = errno;
 			if (err)
 				strbuf_addf(err, "Cannot lock the ref '%s'.",
 					    update->refname);
-			ret = 1;
+			ret = -1;
 			goto cleanup;
 		}
 	}
@@ -3572,6 +3575,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 				if (err)
 					strbuf_addf(err, str, update->refname);
+				ret = -1;
 				goto cleanup;
 			}
 		}
@@ -3582,14 +3586,16 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 		struct ref_update *update = updates[i];
 
 		if (update->lock) {
-			ret |= delete_ref_loose(update->lock, update->type,
-						err);
+			if (delete_ref_loose(update->lock, update->type, err))
+				ret = -1;
+
 			if (!(update->flags & REF_ISPRUNING))
 				delnames[delnum++] = update->lock->ref_name;
 		}
 	}
 
-	ret |= repack_without_refs(delnames, delnum, err);
+	if (repack_without_refs(delnames, delnum, err))
+		ret = -1;
 	for (i = 0; i < delnum; i++)
 		unlink_or_warn(git_path("logs/%s", delnames[i]));
 	clear_loose_ref_cache(&ref_cache);
@@ -3602,6 +3608,9 @@ cleanup:
 		if (updates[i]->lock)
 			unlock_ref(updates[i]->lock);
 	free(delnames);
+	errno = save_errno;
+	if (save_errno == ENOTDIR)
+		ret = -2;
 	return ret;
 }
 
diff --git a/refs.h b/refs.h
index 84e12b5..2823b72 100644
--- a/refs.h
+++ b/refs.h
@@ -289,6 +289,10 @@ int ref_transaction_delete(struct ref_transaction *transaction,
  * Commit all of the changes that have been queued in transaction, as
  * atomically as possible.  Return a nonzero value if there is a
  * problem.
+ * If the transaction is already in failed state this function will return
+ * an error.
+ * Function returns 0 on success, -1 for generic failures and -2 if the
+ * failure was due to a name collision (ENOTDIR).
  */
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err);
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 40/41] fetch.c: change s_update_ref to use a ref transaction
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (38 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers Ronnie Sahlberg
@ 2014-05-27 20:25 ` Ronnie Sahlberg
  2014-05-27 20:26 ` [PATCH v11 41/41] refs.c: make write_ref_sha1 static Ronnie Sahlberg
  2014-06-03 18:31 ` [PATCH v11 00/41] Use ref transactions Jonathan Nieder
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:25 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

Change s_update_ref to use a ref transaction for the ref update.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/fetch.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/builtin/fetch.c b/builtin/fetch.c
index faa1233..c46ccd9 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -45,6 +45,7 @@ static struct transport *gsecondary;
 static const char *submodule_prefix = "";
 static const char *recurse_submodules_default;
 static int shown_url = 0;
+static struct strbuf err = STRBUF_INIT;
 
 static int option_parse_recurse_submodules(const struct option *opt,
 				   const char *arg, int unset)
@@ -375,22 +376,27 @@ static int s_update_ref(const char *action,
 {
 	char msg[1024];
 	char *rla = getenv("GIT_REFLOG_ACTION");
-	static struct ref_lock *lock;
+	struct ref_transaction *transaction;
 
 	if (dry_run)
 		return 0;
 	if (!rla)
 		rla = default_rla.buf;
 	snprintf(msg, sizeof(msg), "%s: %s", rla, action);
-	lock = lock_any_ref_for_update(ref->name,
-				       check_old ? ref->old_sha1 : NULL,
-				       0, NULL);
-	if (!lock)
-		return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
-					  STORE_REF_ERROR_OTHER;
-	if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
+
+	transaction = ref_transaction_begin(&err);
+	if (!transaction ||
+	    ref_transaction_update(transaction, ref->name, ref->new_sha1,
+				   ref->old_sha1, 0, check_old, msg, &err) ||
+	    ref_transaction_commit(transaction, &err)) {
+		ref_transaction_free(transaction);
+		error("%s", err.buf);
+		strbuf_release(&err);
 		return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
 					  STORE_REF_ERROR_OTHER;
+	}
+	strbuf_release(&err);
+	ref_transaction_free(transaction);
 	return 0;
 }
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* [PATCH v11 41/41] refs.c: make write_ref_sha1 static
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (39 preceding siblings ...)
  2014-05-27 20:25 ` [PATCH v11 40/41] fetch.c: change s_update_ref to use a ref transaction Ronnie Sahlberg
@ 2014-05-27 20:26 ` Ronnie Sahlberg
  2014-06-03 18:31 ` [PATCH v11 00/41] Use ref transactions Jonathan Nieder
  41 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-27 20:26 UTC (permalink / raw
  To: git; +Cc: mhagger, Ronnie Sahlberg

No external users call write_ref_sha1 any more so lets declare it static.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 6 +++++-
 refs.h | 3 ---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/refs.c b/refs.c
index c108007..2b51547 100644
--- a/refs.c
+++ b/refs.c
@@ -2622,6 +2622,9 @@ static int rename_tmp_log(const char *newrefname)
 	return 0;
 }
 
+static int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1,
+			  const char *logmsg);
+
 int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
 {
 	unsigned char sha1[20], orig_sha1[20];
@@ -2855,7 +2858,8 @@ static int is_branch(const char *refname)
 	return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
 }
 
-int write_ref_sha1(struct ref_lock *lock,
+/** Writes sha1 into the ref specified by the lock. **/
+static int write_ref_sha1(struct ref_lock *lock,
 	const unsigned char *sha1, const char *logmsg)
 {
 	static char term = '\n';
diff --git a/refs.h b/refs.h
index 2823b72..47775e8 100644
--- a/refs.h
+++ b/refs.h
@@ -147,9 +147,6 @@ extern int commit_ref(struct ref_lock *lock);
 /** Release any lock taken but not written. **/
 extern void unlock_ref(struct ref_lock *lock);
 
-/** Writes sha1 into the ref specified by the lock. **/
-extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
-
 /** Setup reflog before using. **/
 int log_ref_setup(const char *refname, char *logfile, int bufsize);
 
-- 
2.0.0.rc3.474.g0203784

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 02/41] refs.c: ref_transaction_commit should not free the transaction
  2014-05-27 20:25 ` [PATCH v11 02/41] refs.c: ref_transaction_commit should not free the transaction Ronnie Sahlberg
@ 2014-05-27 23:20   ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-27 23:20 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  builtin/update-ref.c | 1 +
>  refs.c               | 1 -
>  refs.h               | 5 ++---
>  3 files changed, 3 insertions(+), 4 deletions(-)

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs
  2014-05-27 20:25 ` [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs Ronnie Sahlberg
@ 2014-05-28  0:11   ` Jonathan Nieder
  2014-05-28 15:31     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28  0:11 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Hi,

Comments from http://marc.info/?l=git&m=140079653930751&w=2:

Ronnie Sahlberg wrote:

> --- a/cache.h
> +++ b/cache.h
> @@ -559,6 +559,8 @@ struct lock_file {
>  #define LOCK_DIE_ON_ERROR 1
>  #define LOCK_NODEREF 2
>  extern int unable_to_lock_error(const char *path, int err);
> +extern void unable_to_lock_strbuf(const char *path, int err,
> +				  struct strbuf *buf);

"unable_to_lock_strbuf" could be called "unable_to_lock_message" (which
would make its behavior more obvious IMHO).

[...]
> --- a/refs.c
> +++ b/refs.c
> @@ -2208,6 +2208,7 @@ int commit_packed_refs(void)
>  	struct packed_ref_cache *packed_ref_cache =
>  		get_packed_ref_cache(&ref_cache);
>  	int error = 0;
> +	int save_errno = 0;

This is about making errno meaningful when commit_packed_refs returns
an error.  Probably its API documentation should say so to make it
obvious when people modify it in the future that they should preserve
that property or audit callers.

[...]
> @@ -2444,6 +2448,11 @@ static int repack_without_refs(const char **refnames, int n)
>  		return 0; /* no refname exists in packed refs */
>  
>  	if (lock_packed_refs(0)) {
> +		if (err) {
> +			unable_to_lock_strbuf(git_path("packed-refs"), errno,
> +					      err);
> +			return -1;
> +		}
>  		unable_to_lock_error(git_path("packed-refs"), errno);

Via the new call to unable_to_lock_..., repack_without_refs cares
about errno after a failed call to lock_packed_refs.  lock_packed_refs
can only fail in hold_lock_file_for_update.  hold_lock_file_for_update
is a thin wrapper around lockfile.c::lock_file.  lock_file can error
out because

	strlen(path) >= max_path_len
	adjust_shared_perm failed [calls error(), clobbers errno]
	open failed

So lock_file needs a similar kind of fix, and it's probably worth
updating API documentation for these calls to make it clear that their
errno is used (though that's not a new problem since
repack_without_refs already called unable_to_lock_error).  Could be a
separate, earlier patch (or a TODO comment in this patch to be
addressed with a later patch) since it's fixing an existing bug.

Hope that helps,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 08/41] refs.c: add an err argument to delete_ref_loose
  2014-05-27 20:25 ` [PATCH v11 08/41] refs.c: add an err argument to delete_ref_loose Ronnie Sahlberg
@ 2014-05-28  0:25   ` Jonathan Nieder
  2014-05-28 14:43     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28  0:25 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Hi,

Comments from http://marc.info/?l=git&m=140079653930751&w=2:

Ronnie Sahlberg wrote:

[...]
> --- a/refs.c
> +++ b/refs.c
> @@ -2491,17 +2491,43 @@ static int repack_without_ref(const char *refname)
>  	return repack_without_refs(&refname, 1, NULL);
>  }
>  
> -static int delete_ref_loose(struct ref_lock *lock, int flag)
> +static int add_err_if_unremovable(const char *op, const char *file,
> +				  struct strbuf *e, int rc)
> +{
> +	int err = errno;
> +	if (rc < 0 && err != ENOENT) {
> +		strbuf_addf(e, "unable to %s %s: %s",
> +			    op, file, strerror(errno));
> +		errno = err;
> +	}
> +	return rc;
> +}
> +
> +static int unlink_or_err(const char *file, struct strbuf *err)
> +{
> +	if (err)
> +		return add_err_if_unremovable("unlink", file, err,
> +					      unlink(file));
> +	else
> +		return unlink_or_warn(file);
> +}

The new unlink_or_err has an odd contract when the err argument is passed.
On error:

 * if errno != ENOENT, it will append a message to err and return -1 (good)
 * if errno == ENOENT, it will not append a message to err but will
   still return -1.

This means the caller has to check errno to figure out whether err is
meaningful when it returns an error.

Perhaps it should return 0 when errno == ENOENT.  After all, in that
case the file does not exist any more, which is all we wanted.


> +
> +static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
>  {
>  	if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
>  		/* loose */
> -		int err, i = strlen(lock->lk->filename) - 5; /* .lock */
> +		int res, i = strlen(lock->lk->filename) - 5; /* .lock */
>  
>  		lock->lk->filename[i] = 0;
> -		err = unlink_or_warn(lock->lk->filename);
> +		res = unlink_or_err(lock->lk->filename, err);
>  		lock->lk->filename[i] = '.';
> -		if (err && errno != ENOENT)
> +		if (res && errno != ENOENT) {
> +			if (err)
> +				strbuf_addf(err,
> +					    "failed to delete loose ref '%s'",
> +					    lock->lk->filename);

On failure we seem to add our own message to err, too, so the
resulting message would be something like

	fatal: unable to unlink .git/refs/heads/master: \
	Permission deniedfailed to delete loose ref '.git/refs/heads/master.lock'

Is the second strbuf_addf a typo?

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref
  2014-05-27 20:25 ` [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref Ronnie Sahlberg
@ 2014-05-28  0:27   ` Jonathan Nieder
  2014-05-28 14:46     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28  0:27 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Hi,

Comments from http://marc.info/?l=git&m=140079653930751&w=2:

Ronnie Sahlberg wrote:

> [Subject: update-ref.c: log transaction error from the update_ref]

The above description suggests that this is going to add new logging,
or in other words that update_ref was being silent about transaction
errors before.

The actual intent is for there to be no functional change, right?  I'd
say something like "update-ref: use err argument to get error from
ref_transaction_commit" or something similar to make it clearer that
this is just about changing APIs.  Or if there's an intended
functional change, then the commit message could say something about
that.

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-27 20:25 ` [PATCH v11 13/41] refs.c: change ref_transaction_create " Ronnie Sahlberg
@ 2014-05-28  0:42   ` Jonathan Nieder
  2014-05-28 14:55     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28  0:42 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Hi,

Ronnie Sahlberg wrote:

> --- a/refs.h
> +++ b/refs.h
> @@ -215,6 +215,15 @@ enum action_on_err {
>  };
>  
>  /*
> + * Transaction functions that take an err argument will append an error
> + * string to this buffer if there was a failure.
> + * This string is not cleared on each call and may contain an aggregate of
> + * errors from several previous calls.
> + * If the caller needs a guarantee that the buffer will only contain the
> + * current or most recent error it must call strbuf_reset before calling
> + * the transaction function.
> + */
> +/*

If I look at the documentation for ref_transaction_create(), it is not
obvious I should look up here for how it handles errors.  Not sure how
to fix that --- maybe this should go in a new
Documentation/technical/api-ref-transactions.txt file?  Or maybe the
top of refs.h where struct ref_transaction is declared.

The content seems odd to me.  It says the string will contain an
aggregate of errors from previous calls, but what it doesn't say is
that that aggregate is a bunch of error messages juxtaposed without a
newline or space between.  Is the idea that some callers will want
this aggregate?

Wouldn't it be clearer to say how the err argument is meant to be used
from the caller's perspective?  E.g.,

	On error, transaction functions append a message about what
	went wrong to the 'err' argument.  The message mentions what
	ref was being updated (if any) when the error occurred so it
	can be passed to 'die' or 'error' as-is:

		if (ref_transaction_update(..., &err)) {
			ret = error("%s", err.buf);
			goto cleanup;
		}

If it's expected that the err argument will be used to aggregate
multiple messages in some callers then it would be useful to give an
example of that, too, but I don't think that's needed.

Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 08/41] refs.c: add an err argument to delete_ref_loose
  2014-05-28  0:25   ` Jonathan Nieder
@ 2014-05-28 14:43     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 14:43 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Tue, May 27, 2014 at 5:25 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Hi,
>
> Comments from http://marc.info/?l=git&m=140079653930751&w=2:
>
> Ronnie Sahlberg wrote:
>
> [...]
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -2491,17 +2491,43 @@ static int repack_without_ref(const char *refname)
>>       return repack_without_refs(&refname, 1, NULL);
>>  }
>>
>> -static int delete_ref_loose(struct ref_lock *lock, int flag)
>> +static int add_err_if_unremovable(const char *op, const char *file,
>> +                               struct strbuf *e, int rc)
>> +{
>> +     int err = errno;
>> +     if (rc < 0 && err != ENOENT) {
>> +             strbuf_addf(e, "unable to %s %s: %s",
>> +                         op, file, strerror(errno));
>> +             errno = err;
>> +     }
>> +     return rc;
>> +}
>> +
>> +static int unlink_or_err(const char *file, struct strbuf *err)
>> +{
>> +     if (err)
>> +             return add_err_if_unremovable("unlink", file, err,
>> +                                           unlink(file));
>> +     else
>> +             return unlink_or_warn(file);
>> +}
>
> The new unlink_or_err has an odd contract when the err argument is passed.
> On error:
>
>  * if errno != ENOENT, it will append a message to err and return -1 (good)
>  * if errno == ENOENT, it will not append a message to err but will
>    still return -1.
>
> This means the caller has to check errno to figure out whether err is
> meaningful when it returns an error.
>
> Perhaps it should return 0 when errno == ENOENT.  After all, in that
> case the file does not exist any more, which is all we wanted.

Good idea.
Thanks. Done.

>
>
>> +
>> +static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
>>  {
>>       if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
>>               /* loose */
>> -             int err, i = strlen(lock->lk->filename) - 5; /* .lock */
>> +             int res, i = strlen(lock->lk->filename) - 5; /* .lock */
>>
>>               lock->lk->filename[i] = 0;
>> -             err = unlink_or_warn(lock->lk->filename);
>> +             res = unlink_or_err(lock->lk->filename, err);
>>               lock->lk->filename[i] = '.';
>> -             if (err && errno != ENOENT)
>> +             if (res && errno != ENOENT) {
>> +                     if (err)
>> +                             strbuf_addf(err,
>> +                                         "failed to delete loose ref '%s'",
>> +                                         lock->lk->filename);
>
> On failure we seem to add our own message to err, too, so the
> resulting message would be something like
>
>         fatal: unable to unlink .git/refs/heads/master: \
>         Permission deniedfailed to delete loose ref '.git/refs/heads/master.lock'
>
> Is the second strbuf_addf a typo?

Yeah, we don't need it.
Removed.

>
> Thanks,
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref
  2014-05-28  0:27   ` Jonathan Nieder
@ 2014-05-28 14:46     ` Ronnie Sahlberg
  2014-05-28 16:49       ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 14:46 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Tue, May 27, 2014 at 5:27 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Hi,
>
> Comments from http://marc.info/?l=git&m=140079653930751&w=2:
>
> Ronnie Sahlberg wrote:
>
>> [Subject: update-ref.c: log transaction error from the update_ref]
>
> The above description suggests that this is going to add new logging,
> or in other words that update_ref was being silent about transaction
> errors before.
>
> The actual intent is for there to be no functional change, right?  I'd
> say something like "update-ref: use err argument to get error from
> ref_transaction_commit" or something similar to make it clearer that
> this is just about changing APIs.  Or if there's an intended
> functional change, then the commit message could say something about
> that.

Thanks.
I reworded the commit message.

>
> Thanks,
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-28  0:42   ` Jonathan Nieder
@ 2014-05-28 14:55     ` Ronnie Sahlberg
  2014-05-28 17:07       ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 14:55 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Tue, May 27, 2014 at 5:42 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Hi,
>
> Ronnie Sahlberg wrote:
>
>> --- a/refs.h
>> +++ b/refs.h
>> @@ -215,6 +215,15 @@ enum action_on_err {
>>  };
>>
>>  /*
>> + * Transaction functions that take an err argument will append an error
>> + * string to this buffer if there was a failure.
>> + * This string is not cleared on each call and may contain an aggregate of
>> + * errors from several previous calls.
>> + * If the caller needs a guarantee that the buffer will only contain the
>> + * current or most recent error it must call strbuf_reset before calling
>> + * the transaction function.
>> + */
>> +/*
>
> If I look at the documentation for ref_transaction_create(), it is not
> obvious I should look up here for how it handles errors.  Not sure how
> to fix that --- maybe this should go in a new
> Documentation/technical/api-ref-transactions.txt file?  Or maybe the
> top of refs.h where struct ref_transaction is declared.
>
> The content seems odd to me.  It says the string will contain an
> aggregate of errors from previous calls, but what it doesn't say is
> that that aggregate is a bunch of error messages juxtaposed without a
> newline or space between.  Is the idea that some callers will want
> this aggregate?
>
> Wouldn't it be clearer to say how the err argument is meant to be used
> from the caller's perspective?  E.g.,
>
>         On error, transaction functions append a message about what
>         went wrong to the 'err' argument.  The message mentions what
>         ref was being updated (if any) when the error occurred so it
>         can be passed to 'die' or 'error' as-is:
>
>                 if (ref_transaction_update(..., &err)) {
>                         ret = error("%s", err.buf);
>                         goto cleanup;
>                 }
>
> If it's expected that the err argument will be used to aggregate
> multiple messages in some callers then it would be useful to give an
> example of that, too, but I don't think that's needed.

Thanks. Updated the comment in refs.h

>
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs
  2014-05-28  0:11   ` Jonathan Nieder
@ 2014-05-28 15:31     ` Ronnie Sahlberg
  2014-05-28 18:30       ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 15:31 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Tue, May 27, 2014 at 5:11 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Hi,
>
> Comments from http://marc.info/?l=git&m=140079653930751&w=2:
>
> Ronnie Sahlberg wrote:
>
>> --- a/cache.h
>> +++ b/cache.h
>> @@ -559,6 +559,8 @@ struct lock_file {
>>  #define LOCK_DIE_ON_ERROR 1
>>  #define LOCK_NODEREF 2
>>  extern int unable_to_lock_error(const char *path, int err);
>> +extern void unable_to_lock_strbuf(const char *path, int err,
>> +                               struct strbuf *buf);
>
> "unable_to_lock_strbuf" could be called "unable_to_lock_message" (which
> would make its behavior more obvious IMHO).

Renamed.  Thanks.

>
> [...]
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -2208,6 +2208,7 @@ int commit_packed_refs(void)
>>       struct packed_ref_cache *packed_ref_cache =
>>               get_packed_ref_cache(&ref_cache);
>>       int error = 0;
>> +     int save_errno = 0;
>
> This is about making errno meaningful when commit_packed_refs returns
> an error.  Probably its API documentation should say so to make it
> obvious when people modify it in the future that they should preserve
> that property or audit callers.
>
> [...]
>> @@ -2444,6 +2448,11 @@ static int repack_without_refs(const char **refnames, int n)
>>               return 0; /* no refname exists in packed refs */
>>
>>       if (lock_packed_refs(0)) {
>> +             if (err) {
>> +                     unable_to_lock_strbuf(git_path("packed-refs"), errno,
>> +                                           err);
>> +                     return -1;
>> +             }
>>               unable_to_lock_error(git_path("packed-refs"), errno);
>
> Via the new call to unable_to_lock_..., repack_without_refs cares
> about errno after a failed call to lock_packed_refs.  lock_packed_refs
> can only fail in hold_lock_file_for_update.  hold_lock_file_for_update
> is a thin wrapper around lockfile.c::lock_file.  lock_file can error
> out because
>
>         strlen(path) >= max_path_len
>         adjust_shared_perm failed [calls error(), clobbers errno]
>         open failed
>
> So lock_file needs a similar kind of fix, and it's probably worth
> updating API documentation for these calls to make it clear that their
> errno is used (though that's not a new problem since
> repack_without_refs already called unable_to_lock_error).  Could be a
> separate, earlier patch (or a TODO comment in this patch to be
> addressed with a later patch) since it's fixing an existing bug.

I will add it to my todo list.
I think passing of errno around is too fragile and that we should
avoid ad-hoc save_errno hacks and implement dedicated return codes to
replace errno.
We should only inspect errno immediately after a syscall has failed.

>
> Hope that helps,
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref
  2014-05-28 14:46     ` Ronnie Sahlberg
@ 2014-05-28 16:49       ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 16:49 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Ronnie Sahlberg wrote:

> I reworded the commit message.

Thanks much.  Looks good.

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-28 14:55     ` Ronnie Sahlberg
@ 2014-05-28 17:07       ` Jonathan Nieder
  2014-05-28 17:15         ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 17:07 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Ronnie Sahlberg wrote:

> Updated the comment in refs.h

Thanks.

> +++ b/refs.h
> @@ -215,6 +215,31 @@ enum action_on_err {
>  };
>  
>  /*
> + * On error, transaction functions append a message about what
> + * went wrong to the 'err' argument.  The message mentions what
> + * ref was being updated (if any) when the error occurred so it
> + * can be passed to 'die' or 'error' as-is:
> + *
> + *     if (ref_transaction_update(..., &err)) {
> + *         ret = error("%s", err.buf);
> + *         goto cleanup;
> + *     }
> + *
> + * The message is appended to err without first clearing err.
> + * This allow shte caller to prepare preamble text to the generated

s/allow shte/allows the/

> + * error message:
> + *
> + *     strbuf_addf(&err, "Error while doing foo-bar: ");
> + *     if (ref_transaction_update(..., &err)) {
> + *         ret = error("%s", err.buf);
> + *         goto cleanup;
> + *     }

Nice.

> + *
> + * If the caller wants err to only contain the message for the current error
> + * and nothing else caller will need to guarantee that err is empty or
> + * call strbuf_reset before calling the transaction function.

I don't think this paragraph is needed --- especially with the
clarification about how to add a preamble, the contract is clear.

> + */
> +/*
>   * Begin a reference transaction.  The reference transaction must
>   * be freed by calling ref_transaction_free().
>   */

Now that the comment is longer, it's harder to miss, but it still is
in an odd place for someone looking to understand what the 'err'
argument to ref_transaction_update() means.

How about this patch for squashing in?

diff --git i/refs.h w/refs.h
index c741a6c..913ca93 100644
--- i/refs.h
+++ w/refs.h
@@ -10,6 +10,45 @@ struct ref_lock {
 	int force_write;
 };
 
+/*
+ * A ref_transaction represents a collection of ref updates
+ * that should succeed or fail together.
+ *
+ * Calling sequence
+ * ----------------
+ * - Allocate and initialize a `struct ref_transaction` by calling
+ *   `ref_transaction_begin()`.
+ *
+ * - List intended ref updates by calling functions like
+ *   `ref_transaction_update()` and `ref_transaction_create()`.
+ *
+ * - Call `ref_transaction_commit()` to execute the transaction.
+ *   If this succeeds, the ref updates will have taken place and
+ *   the transaction cannot be rolled back.
+ *
+ * - At any time call `ref_transaction_free()` to discard the
+ *   transaction and free associated resources.  In particular,
+ *   this rolls back the transaction if it has not been
+ *   successfully committed.
+ *
+ * Error handling
+ * --------------
+ *
+ * On error, transaction functions append a message about what
+ * went wrong to the 'err' argument.  The message mentions what
+ * ref was being updated (if any) when the error occurred so it
+ * can be passed to 'die' or 'error' as-is.
+ *
+ * The message is appended to err without first clearing err.
+ * This allows the caller to prepare preamble text to the generated
+ * error message:
+ *
+ *     strbuf_addf(&err, "Error while doing foo-bar: ");
+ *     if (ref_transaction_update(..., &err)) {
+ *         ret = error("%s", err.buf);
+ *         goto cleanup;
+ *     }
+ */
 struct ref_transaction;
 
 /*
@@ -215,31 +254,6 @@ enum action_on_err {
 };
 
 /*
- * On error, transaction functions append a message about what
- * went wrong to the 'err' argument.  The message mentions what
- * ref was being updated (if any) when the error occurred so it
- * can be passed to 'die' or 'error' as-is:
- *
- *     if (ref_transaction_update(..., &err)) {
- *         ret = error("%s", err.buf);
- *         goto cleanup;
- *     }
- *
- * The message is appended to err without first clearing err.
- * This allow shte caller to prepare preamble text to the generated
- * error message:
- *
- *     strbuf_addf(&err, "Error while doing foo-bar: ");
- *     if (ref_transaction_update(..., &err)) {
- *         ret = error("%s", err.buf);
- *         goto cleanup;
- *     }
- *
- * If the caller wants err to only contain the message for the current error
- * and nothing else caller will need to guarantee that err is empty or
- * call strbuf_reset before calling the transaction function.
- */
-/*
  * Begin a reference transaction.  The reference transaction must
  * be freed by calling ref_transaction_free().
  */

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-28 17:07       ` Jonathan Nieder
@ 2014-05-28 17:15         ` Ronnie Sahlberg
  2014-05-28 17:25           ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 17:15 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Wed, May 28, 2014 at 10:07 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> Updated the comment in refs.h
>
> Thanks.
>
>> +++ b/refs.h
>> @@ -215,6 +215,31 @@ enum action_on_err {
>>  };
>>
>>  /*
>> + * On error, transaction functions append a message about what
>> + * went wrong to the 'err' argument.  The message mentions what
>> + * ref was being updated (if any) when the error occurred so it
>> + * can be passed to 'die' or 'error' as-is:
>> + *
>> + *     if (ref_transaction_update(..., &err)) {
>> + *         ret = error("%s", err.buf);
>> + *         goto cleanup;
>> + *     }
>> + *
>> + * The message is appended to err without first clearing err.
>> + * This allow shte caller to prepare preamble text to the generated
>
> s/allow shte/allows the/
>
>> + * error message:
>> + *
>> + *     strbuf_addf(&err, "Error while doing foo-bar: ");
>> + *     if (ref_transaction_update(..., &err)) {
>> + *         ret = error("%s", err.buf);
>> + *         goto cleanup;
>> + *     }
>
> Nice.
>
>> + *
>> + * If the caller wants err to only contain the message for the current error
>> + * and nothing else caller will need to guarantee that err is empty or
>> + * call strbuf_reset before calling the transaction function.
>
> I don't think this paragraph is needed --- especially with the
> clarification about how to add a preamble, the contract is clear.
>
>> + */
>> +/*
>>   * Begin a reference transaction.  The reference transaction must
>>   * be freed by calling ref_transaction_free().
>>   */
>
> Now that the comment is longer, it's harder to miss, but it still is
> in an odd place for someone looking to understand what the 'err'
> argument to ref_transaction_update() means.
>
> How about this patch for squashing in?
>
> diff --git i/refs.h w/refs.h
> index c741a6c..913ca93 100644
> --- i/refs.h
> +++ w/refs.h
> @@ -10,6 +10,45 @@ struct ref_lock {
>         int force_write;
>  };
>
> +/*
> + * A ref_transaction represents a collection of ref updates
> + * that should succeed or fail together.
> + *
> + * Calling sequence
> + * ----------------
> + * - Allocate and initialize a `struct ref_transaction` by calling
> + *   `ref_transaction_begin()`.
> + *
> + * - List intended ref updates by calling functions like
> + *   `ref_transaction_update()` and `ref_transaction_create()`.
> + *
> + * - Call `ref_transaction_commit()` to execute the transaction.
> + *   If this succeeds, the ref updates will have taken place and
> + *   the transaction cannot be rolled back.
> + *
> + * - At any time call `ref_transaction_free()` to discard the
> + *   transaction and free associated resources.  In particular,
> + *   this rolls back the transaction if it has not been
> + *   successfully committed.
> + *
> + * Error handling
> + * --------------
> + *
> + * On error, transaction functions append a message about what
> + * went wrong to the 'err' argument.  The message mentions what
> + * ref was being updated (if any) when the error occurred so it
> + * can be passed to 'die' or 'error' as-is.
> + *
> + * The message is appended to err without first clearing err.
> + * This allows the caller to prepare preamble text to the generated
> + * error message:
> + *
> + *     strbuf_addf(&err, "Error while doing foo-bar: ");
> + *     if (ref_transaction_update(..., &err)) {
> + *         ret = error("%s", err.buf);
> + *         goto cleanup;
> + *     }
> + */
>  struct ref_transaction;
>
>  /*
> @@ -215,31 +254,6 @@ enum action_on_err {
>  };
>
>  /*
> - * On error, transaction functions append a message about what
> - * went wrong to the 'err' argument.  The message mentions what
> - * ref was being updated (if any) when the error occurred so it
> - * can be passed to 'die' or 'error' as-is:
> - *
> - *     if (ref_transaction_update(..., &err)) {
> - *         ret = error("%s", err.buf);
> - *         goto cleanup;
> - *     }
> - *
> - * The message is appended to err without first clearing err.
> - * This allow shte caller to prepare preamble text to the generated
> - * error message:
> - *
> - *     strbuf_addf(&err, "Error while doing foo-bar: ");
> - *     if (ref_transaction_update(..., &err)) {
> - *         ret = error("%s", err.buf);
> - *         goto cleanup;
> - *     }
> - *
> - * If the caller wants err to only contain the message for the current error
> - * and nothing else caller will need to guarantee that err is empty or
> - * call strbuf_reset before calling the transaction function.
> - */
> -/*
>   * Begin a reference transaction.  The reference transaction must
>   * be freed by calling ref_transaction_free().
>   */

Done.
Please re-review.

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-28 17:15         ` Ronnie Sahlberg
@ 2014-05-28 17:25           ` Jonathan Nieder
  2014-05-28 18:01             ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 17:25 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Ronnie Sahlberg wrote:

> Please re-review.

06df8942 is
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

Thanks.

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-28 17:25           ` Jonathan Nieder
@ 2014-05-28 18:01             ` Ronnie Sahlberg
  2014-05-28 18:09               ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 18:01 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

Thanks

Can you re-review these patches for me :

please review

67b8fce refs.c: add an err argument to repack_without_refs
738ac43 refs.c: add an err argument to delete_ref_loose
b78b0e0 refs.c: update ref_transaction_delete to check for error and
return status
e558f96 refs.c: add transaction.status and track OPEN/CLOSED/ERROR
5a7d9bf sequencer.c: use ref transactions for all ref updates
0ea69df fast-import.c: change update_branch to use ref transactions
57c92fb refs.c: change update_ref to use a transaction





On Wed, May 28, 2014 at 10:25 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> Please re-review.
>
> 06df8942 is
> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
>
> Thanks.

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 13/41] refs.c: change ref_transaction_create to do error checking and return status
  2014-05-28 18:01             ` Ronnie Sahlberg
@ 2014-05-28 18:09               ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 18:09 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Ronnie Sahlberg wrote:

> Can you re-review these patches for me :
>
> please review
>
> 67b8fce refs.c: add an err argument to repack_without_refs
> 738ac43 refs.c: add an err argument to delete_ref_loose
> b78b0e0 refs.c: update ref_transaction_delete to check for error and
> return status
> e558f96 refs.c: add transaction.status and track OPEN/CLOSED/ERROR
> 5a7d9bf sequencer.c: use ref transactions for all ref updates
> 0ea69df fast-import.c: change update_branch to use ref transactions
> 57c92fb refs.c: change update_ref to use a transaction

The easiest way for me is if you send them to the list so I can
comment inline (and others can chime in with other comments, etc).
This works like a reroll of an entire series (using format-patch -v if
you use format-patch, etc) except that instead of sending as a new
thread the updated patch is just sent in response to some review
comments or the earlier version of that patch.

git send-email has an --in-reply-to option for this.  That involves
getting the Message-Id, which can be a little fussy.

Others who use send-email might know better tricks for this.  I never
ended up learning how to use git send-email (and I'm a little scared
of it) so I just hit "reply" in mutt and put the patch there.

Thanks and sorry for the fuss,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs
  2014-05-28 15:31     ` Ronnie Sahlberg
@ 2014-05-28 18:30       ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 18:30 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Hi,

Looking at 67b8fcee:

Ronnie Sahlberg wrote:
> On Tue, May 27, 2014 at 5:11 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
>> Ronnie Sahlberg wrote:

>>> --- a/refs.c
>>> +++ b/refs.c
>>> @@ -2208,6 +2208,7 @@ int commit_packed_refs(void)
>>>       struct packed_ref_cache *packed_ref_cache =
>>>               get_packed_ref_cache(&ref_cache);
>>>       int error = 0;
>>> +     int save_errno = 0;
>>
>> This is about making errno meaningful when commit_packed_refs returns
>> an error.  Probably its API documentation should say so to make it
>> obvious when people modify it in the future that they should preserve
>> that property or audit callers.

67b8fcee doesn't address this.  While it's often even better to return
an error message or meaningful error number so the caller doesn't have
to worry about errno at all, I think being explicit in the comment
above a declaration about which functions leave behind a meaningful
errno can make error handling saner.

So I still think that the above would be a good idea.

[...]
>> [...]
>>> @@ -2444,6 +2448,11 @@ static int repack_without_refs(const char **refnames, int n)
>>>               return 0; /* no refname exists in packed refs */
>>>
>>>       if (lock_packed_refs(0)) {
>>> +             if (err) {
>>> +                     unable_to_lock_strbuf(git_path("packed-refs"), errno,
>>> +                                           err);
>>> +                     return -1;
>>> +             }
>>>               unable_to_lock_error(git_path("packed-refs"), errno);
>>
>> Via the new call to unable_to_lock_..., repack_without_refs cares
>> about errno after a failed call to lock_packed_refs.  lock_packed_refs
>> can only fail in hold_lock_file_for_update.  hold_lock_file_for_update
>> is a thin wrapper around lockfile.c::lock_file.  lock_file can error
>> out because
>>
>>         strlen(path) >= max_path_len
[...]
>>                                                            Could be a
>> separate, earlier patch (or a TODO comment in this patch to be
>> addressed with a later patch) since it's fixing an existing bug.
>
> I will add it to my todo list.

My worry with that is that it is too easy to forget that there is a
problem at all.  That's not *that* bad --- if no one remembers, how
serious of a problem was it, really?

Except that it makes reading code difficult.  It's easier to read
some code that prints strerror(errno) in a place that is known to
sometimes have errno==0 if there is a comment

	/*
	 * NEEDSWORK: Tweak lock_packed_refs to either reliably
	 * set errno to a sane value on error or to propagate
	 * back error information another way.
	 */
	perror(...);

Otherwise, the reader has to keep scratching their head, wondering
"how did this ever work?".

That's why I suggested adding a TODO comment.

> I think passing of errno around is too fragile and that we should
> avoid ad-hoc save_errno hacks and implement dedicated return codes to
> replace errno.
> We should only inspect errno immediately after a syscall has failed.

Sure, agreed in principle. ;-)

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 14/41] refs.c: update ref_transaction_delete to check for error and return status
  2014-05-27 20:25 ` [PATCH v11 14/41] refs.c: update ref_transaction_delete to check for error " Ronnie Sahlberg
@ 2014-05-28 18:42   ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 18:42 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  builtin/update-ref.c |  5 +++--
>  refs.c               | 16 +++++++++++-----
>  refs.h               | 12 ++++++++----
>  3 files changed, 22 insertions(+), 11 deletions(-)

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

[...]
> +++ b/refs.c
> @@ -3417,19 +3417,25 @@ int ref_transaction_create(struct ref_transaction *transaction,
>  	return 0;
>  }
>  
> -void ref_transaction_delete(struct ref_transaction *transaction,
> -			    const char *refname,
> -			    const unsigned char *old_sha1,
> -			    int flags, int have_old)
> +int ref_transaction_delete(struct ref_transaction *transaction,
> +			   const char *refname,
> +			   const unsigned char *old_sha1,
> +			   int flags, int have_old,
> +			   struct strbuf *err)
>  {
> -	struct ref_update *update = add_update(transaction, refname);
> +	struct ref_update *update;
>  
> +	if (have_old && !old_sha1)
> +		die("BUG: have_old is true but old_sha1 is NULL");
> +
> +	update = add_update(transaction, refname);
>  	update->flags = flags;
>  	update->have_old = have_old;
>  	if (have_old) {
>  		assert(!is_null_sha1(old_sha1));

Could combine this into the 'if (have_old &&' check.

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR
  2014-05-27 20:25 ` [PATCH v11 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR Ronnie Sahlberg
@ 2014-05-28 18:51   ` Jonathan Nieder
  2014-05-28 21:50     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 18:51 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> --- a/refs.c
> +++ b/refs.c
[...]
> @@ -3385,6 +3408,9 @@ int ref_transaction_update(struct ref_transaction *transaction,
>  {
>  	struct ref_update *update;
>  
> +	if (transaction->state != REF_TRANSACTION_OPEN)
> +		return -1;

I still think this is a step in the wrong direction.  It means that
instead of being able to do

	if (ref_transaction_update(..., &err))
		die("%s", err.buf);

and be certain that err.buf has a meaningful message, now I have to
worry that if the state were not REF_TRANSACTION_OPEN (e.g., due to
someone else's code forgetting to handle an error) then the result
would be a plain

	fatal:

The behavior would be much easier to debug if the code were

	if (transaction->state != REF_TRANSACTION_OPEN)
		die("BUG: update on transaction that is not open");

since then the symptom would be

	fatal: BUG: update on transaction that is not open

which is easier to work with.

If a non-OPEN state were a normal, recoverable error, then it could
append a message to the *err argument.  But there's no message that
could be put there that would be meaningful to the end-user.  So I
suspect it's not a normal, recoverable error.

If we want to collect errors to make _commit() later fail with a
message like '38 refs failed to update' or something (or a
string_list, if the API is to change that way in the future), then
_update() should not fail.  It can record information about what is
wrong with this update in the transaction and succeed so the caller
knows to keep going and collect other updates before the _commit()
that will fail.

Of course it's easily possible I'm missing something.  That's just how
I see it now.

Does that make sense?

Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 20/41] sequencer.c: use ref transactions for all ref updates
  2014-05-27 20:25 ` [PATCH v11 20/41] sequencer.c: use ref transactions for all ref updates Ronnie Sahlberg
@ 2014-05-28 18:53   ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 18:53 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  sequencer.c | 24 ++++++++++++++++--------
>  1 file changed, 16 insertions(+), 8 deletions(-)

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 21/41] fast-import.c: change update_branch to use ref transactions
  2014-05-27 20:25 ` [PATCH v11 21/41] fast-import.c: change update_branch to use ref transactions Ronnie Sahlberg
@ 2014-05-28 18:54   ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 18:54 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  fast-import.c | 25 +++++++++++++++----------
>  1 file changed, 15 insertions(+), 10 deletions(-)

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 23/41] refs.c: change update_ref to use a transaction
  2014-05-27 20:25 ` [PATCH v11 23/41] refs.c: change update_ref to use a transaction Ronnie Sahlberg
@ 2014-05-28 19:31   ` Jonathan Nieder
  2014-05-28 21:14     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 19:31 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> --- a/refs.c
> +++ b/refs.c
> @@ -3474,11 +3474,28 @@ int update_ref(const char *action, const char *refname,
>  	       const unsigned char *sha1, const unsigned char *oldval,
>  	       int flags, enum action_on_err onerr)
>  {
> -	struct ref_lock *lock;
> -	lock = update_ref_lock(refname, oldval, flags, NULL, onerr);
> -	if (!lock)
> +	struct ref_transaction *t;
> +	struct strbuf err = STRBUF_INIT;
> +
> +	t = ref_transaction_begin(&err);
> +	if ((!t ||
> +	    ref_transaction_update(t, refname, sha1, oldval, flags,
> +				   !!oldval, &err)) ||

(style) Extra parens.

> +	    (ref_transaction_commit(t, action, &err) && !(t = NULL))) {

No need for this assignment-in-if.

With the following squashed in,
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

diff --git i/refs.c w/refs.c
index 568b358..fb462a3 100644
--- i/refs.c
+++ w/refs.c
@@ -3474,10 +3474,10 @@ int update_ref(const char *action, const char *refname,
 	struct strbuf err = STRBUF_INIT;
 
 	t = ref_transaction_begin(&err);
-	if ((!t ||
+	if (!t ||
 	    ref_transaction_update(t, refname, sha1, oldval, flags,
-				   !!oldval, &err)) ||
-	    (ref_transaction_commit(t, action, &err) && !(t = NULL))) {
+				   !!oldval, &err) ||
+	    ref_transaction_commit(t, action, &err)) {
 		const char *str = "update_ref failed for ref '%s': %s";
 
 		ref_transaction_free(t);

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-27 20:25 ` [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags Ronnie Sahlberg
@ 2014-05-28 19:47   ` Jonathan Nieder
  2014-05-28 22:06     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 19:47 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> --- a/fast-import.c
> +++ b/fast-import.c
> @@ -1735,15 +1735,22 @@ static void dump_tags(void)
>  {
>  	static const char *msg = "fast-import";
>  	struct tag *t;
> -	struct ref_lock *lock;
>  	char ref_name[PATH_MAX];
> +	struct strbuf err = STRBUF_INIT;
> +	struct ref_transaction *transaction;
>  
> +	transaction = ref_transaction_begin(&err);
>  	for (t = first_tag; t; t = t->next_tag) {
> -		sprintf(ref_name, "tags/%s", t->name);
> +		snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);

That ignores the error if the refname happens to go longer than
PATH_MAX.

This is the part of fast-import that doesn't need to be super
fast ;-).  (The objects have been written to the pack, and now
we just need to write some refs.)  Could this use a strbuf?  By that,
I mean something like

diff --git i/fast-import.c w/fast-import.c
index 3db5b3d..d5f6e63 100644
--- i/fast-import.c
+++ w/fast-import.c
@@ -1735,21 +1735,28 @@ static void dump_tags(void)
 {
 	static const char *msg = "fast-import";
 	struct tag *t;
-	char ref_name[PATH_MAX];
+	struct strbuf ref_name = STRBUF_INIT;
 	struct strbuf err = STRBUF_INIT;
 	struct ref_transaction *transaction;
 
+	strbuf_addstr(&ref_name, "refs/tags/");
+
 	transaction = ref_transaction_begin(&err);
 	for (t = first_tag; t; t = t->next_tag) {
-		snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);
+		strbuf_setlen(&ref_name, strlen("refs/tags/"));
+		strbuf_addstr(&ref_name, t->name);
 
-		if (ref_transaction_update(transaction, ref_name, t->sha1,
-					   NULL, 0, 0, &err))
-			break;
+		if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
+					   NULL, 0, 0, &err)) {
+			failure |= error("%s", err.buf);
+			goto done;
+		}
 	}
 	if (ref_transaction_commit(transaction, msg, &err))
 		failure |= error("%s", err.buf);
+done:
 	ref_transaction_free(transaction);
+	strbuf_release(&ref_name);
 	strbuf_release(&err);
 }
 

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 26/41] walker.c: use ref transaction for ref updates
  2014-05-27 20:25 ` [PATCH v11 26/41] walker.c: use ref transaction for ref updates Ronnie Sahlberg
@ 2014-05-28 19:56   ` Jonathan Nieder
  2014-05-28 20:56     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 19:56 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Switch to using ref transactions in walker_fetch(). As part of the refactoring
> to use ref transactions we also fix a potential memory leak where in the
> original code if write_ref_sha1() would fail we would end up returning from
> the function without free()ing the msg string.

Sounds good.

> This changes the locking slightly for walker_fetch. Previously the code would
> lock all refs before writing them but now we do not lock the refs until the
> commit stage.

I don't think this actually changes locking in any significant way.
(Timing changes, but that's to be expected whenever code changes.)

The old contract was:

You run git http-fetch with refnames passed with "-w".  http-fetch
will overwrite the refs with their new values.

The new contract is:

You run git http-fetch with refnames passed with "-w".  http-fetch
will overwrite the refs with their new values.

What changed?

[...]
> Note that this function is only called when fetching from a remote HTTP
> repository onto the local (most of the time single-user) repository which
> likely means that the type of collissions that the previous locking would
> protect against and cause the fetch to fail for to be even more rare.

More to the point, while this function is used by 'git remote-http' (and
hence by "git fetch https://foo/bar/baz"), it is only used to fetch
objects by that code path.  The remote helper machinery handles the ref
updates on its own (and passes NULL as the write_ref argument to the
dumb walker).

[...]
> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  walker.c | 56 +++++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 33 insertions(+), 23 deletions(-)

The code change looks good from a quick glance.

gcc 4.6.3 doesn't seem to be smart enough to notice that 'transaction'
is always initialized before it is used, so it (optionally) could be
worth initializing 'transition = NULL' to work around that.

So I think this just needs a simpler commit message and then it would be
good to go.

Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 29/41] refs.c: remove the update_ref_write function
  2014-05-27 20:25 ` [PATCH v11 29/41] refs.c: remove the update_ref_write function Ronnie Sahlberg
@ 2014-05-28 20:39   ` Jonathan Nieder
  0 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 20:39 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  refs.c | 35 +++++++++--------------------------
>  1 file changed, 9 insertions(+), 26 deletions(-)

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 26/41] walker.c: use ref transaction for ref updates
  2014-05-28 19:56   ` Jonathan Nieder
@ 2014-05-28 20:56     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 20:56 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Wed, May 28, 2014 at 12:56 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> Switch to using ref transactions in walker_fetch(). As part of the refactoring
>> to use ref transactions we also fix a potential memory leak where in the
>> original code if write_ref_sha1() would fail we would end up returning from
>> the function without free()ing the msg string.
>
> Sounds good.
>
>> This changes the locking slightly for walker_fetch. Previously the code would
>> lock all refs before writing them but now we do not lock the refs until the
>> commit stage.
>
> I don't think this actually changes locking in any significant way.
> (Timing changes, but that's to be expected whenever code changes.)
>
> The old contract was:
>
> You run git http-fetch with refnames passed with "-w".  http-fetch
> will overwrite the refs with their new values.
>
> The new contract is:
>
> You run git http-fetch with refnames passed with "-w".  http-fetch
> will overwrite the refs with their new values.
>
> What changed?

Timing for the locks basically.
Which means the outcome for certain races could change slightly.
But not important.

>
> [...]
>> Note that this function is only called when fetching from a remote HTTP
>> repository onto the local (most of the time single-user) repository which
>> likely means that the type of collissions that the previous locking would
>> protect against and cause the fetch to fail for to be even more rare.
>
> More to the point, while this function is used by 'git remote-http' (and
> hence by "git fetch https://foo/bar/baz"), it is only used to fetch
> objects by that code path.  The remote helper machinery handles the ref
> updates on its own (and passes NULL as the write_ref argument to the
> dumb walker).
>
> [...]
>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
>> ---
>>  walker.c | 56 +++++++++++++++++++++++++++++++++-----------------------
>>  1 file changed, 33 insertions(+), 23 deletions(-)
>
> The code change looks good from a quick glance.
>
> gcc 4.6.3 doesn't seem to be smart enough to notice that 'transaction'
> is always initialized before it is used, so it (optionally) could be
> worth initializing 'transition = NULL' to work around that.
>
> So I think this just needs a simpler commit message and then it would be
> good to go.
>

Done, thanks.

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 23/41] refs.c: change update_ref to use a transaction
  2014-05-28 19:31   ` Jonathan Nieder
@ 2014-05-28 21:14     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 21:14 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

Done.
Thanks.

On Wed, May 28, 2014 at 12:31 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -3474,11 +3474,28 @@ int update_ref(const char *action, const char *refname,
>>              const unsigned char *sha1, const unsigned char *oldval,
>>              int flags, enum action_on_err onerr)
>>  {
>> -     struct ref_lock *lock;
>> -     lock = update_ref_lock(refname, oldval, flags, NULL, onerr);
>> -     if (!lock)
>> +     struct ref_transaction *t;
>> +     struct strbuf err = STRBUF_INIT;
>> +
>> +     t = ref_transaction_begin(&err);
>> +     if ((!t ||
>> +         ref_transaction_update(t, refname, sha1, oldval, flags,
>> +                                !!oldval, &err)) ||
>
> (style) Extra parens.
>
>> +         (ref_transaction_commit(t, action, &err) && !(t = NULL))) {
>
> No need for this assignment-in-if.
>
> With the following squashed in,
> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
>
> diff --git i/refs.c w/refs.c
> index 568b358..fb462a3 100644
> --- i/refs.c
> +++ w/refs.c
> @@ -3474,10 +3474,10 @@ int update_ref(const char *action, const char *refname,
>         struct strbuf err = STRBUF_INIT;
>
>         t = ref_transaction_begin(&err);
> -       if ((!t ||
> +       if (!t ||
>             ref_transaction_update(t, refname, sha1, oldval, flags,
> -                                  !!oldval, &err)) ||
> -           (ref_transaction_commit(t, action, &err) && !(t = NULL))) {
> +                                  !!oldval, &err) ||
> +           ref_transaction_commit(t, action, &err)) {
>                 const char *str = "update_ref failed for ref '%s': %s";
>
>                 ref_transaction_free(t);

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR
  2014-05-28 18:51   ` Jonathan Nieder
@ 2014-05-28 21:50     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 21:50 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Wed, May 28, 2014 at 11:51 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> --- a/refs.c
>> +++ b/refs.c
> [...]
>> @@ -3385,6 +3408,9 @@ int ref_transaction_update(struct ref_transaction *transaction,
>>  {
>>       struct ref_update *update;
>>
>> +     if (transaction->state != REF_TRANSACTION_OPEN)
>> +             return -1;
>
> I still think this is a step in the wrong direction.  It means that
> instead of being able to do
>
>         if (ref_transaction_update(..., &err))
>                 die("%s", err.buf);
>
> and be certain that err.buf has a meaningful message, now I have to
> worry that if the state were not REF_TRANSACTION_OPEN (e.g., due to
> someone else's code forgetting to handle an error) then the result
> would be a plain
>
>         fatal:
>
> The behavior would be much easier to debug if the code were
>
>         if (transaction->state != REF_TRANSACTION_OPEN)
>                 die("BUG: update on transaction that is not open");
>
> since then the symptom would be
>
>         fatal: BUG: update on transaction that is not open
>
> which is easier to work with.
>
> If a non-OPEN state were a normal, recoverable error, then it could
> append a message to the *err argument.  But there's no message that
> could be put there that would be meaningful to the end-user.  So I
> suspect it's not a normal, recoverable error.
>
> If we want to collect errors to make _commit() later fail with a
> message like '38 refs failed to update' or something (or a
> string_list, if the API is to change that way in the future), then
> _update() should not fail.

Agreed.
I have removed the if (transaction->state != REF_TRANSACTION_OPEN)
check from _update/_delete/_create and documented this usecase.

Thanks.!

  It can record information about what is
> wrong with this update in the transaction and succeed so the caller
> knows to keep going and collect other updates before the _commit()
> that will fail.
>
> Of course it's easily possible I'm missing something.  That's just how
> I see it now.
>
> Does that make sense?

Makes perfect sense.

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 31/41] refs.c: make prune_ref use a transaction to delete the ref
  2014-05-27 20:25 ` [PATCH v11 31/41] refs.c: make prune_ref use a transaction to delete the ref Ronnie Sahlberg
@ 2014-05-28 21:51   ` Jonathan Nieder
  2014-05-28 21:56     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 21:51 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Change prune_ref to delete the ref using a ref transaction. To do this we also
> need to add a new flag REF_ISPRUNING that will tell the transaction that we
> do not want to delete this ref from the packed refs.

s/from the packed refs/from packed-refs, nor delete the ref's reflog/

[...]
> --- a/refs.h
> +++ b/refs.h
> @@ -235,6 +235,11 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
>   * The following functions add a reference check or update to a
>   * ref_transaction.  In all of them, refname is the name of the
>   * reference to be affected.  The functions make internal copies of
>   * refname, so the caller retains ownership of the parameter.  flags
>   * can be REF_NODEREF; it is passed to update_ref_lock().
>   */
>  
>  /*
> + * ref_transaction_update ref_transaction_create and ref_transaction_delete
> + * all take a flag argument. Currently the only public flag is REF_NODEREF.
> + * Flag values >= 0x100 are reserved for internal use.
> + */
> +/*
>   * Add a reference update to transaction.  new_sha1 is the value that

The comment right before here already tries to explain the flag argument,
though it isn't in an obvious place so it's easy to miss.  Maybe the flag
argument should be explained in the overview documentation for the
ref_transaction API near the top of the file (but I haven't thought that
through, so leaving it alone).

How about this as a way to make the reserved flag values easier to
find when adding new flags?

diff --git i/refs.h w/refs.h
index 25ac4a9..dee7c8f 100644
--- i/refs.h
+++ w/refs.h
@@ -171,8 +171,17 @@ extern int ref_exists(const char *);
  */
 extern int peel_ref(const char *refname, unsigned char *sha1);
 
-/** Locks any ref (for 'HEAD' type refs). */
+/*
+ * Flags controlling lock_any_ref_for_update(), ref_transaction_update(),
+ * ref_transaction_create(), etc.
+ * REF_NODEREF: act on the ref directly, instead of dereferencing
+ *              symbolic references.
+ *
+ * Flags >= 0x100 are reserved for internal use.
+ */
 #define REF_NODEREF	0x01
+
+/** Locks any ref (for 'HEAD' type refs). */
 extern struct ref_lock *lock_any_ref_for_update(const char *refname,
 						const unsigned char *old_sha1,
 						int flags, int *type_p);
@@ -265,11 +274,6 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
  */
 
 /*
- * ref_transaction_update ref_transaction_create and ref_transaction_delete
- * all take a flag argument. Currently the only public flag is REF_NODEREF.
- * Flag values >= 0x100 are reserved for internal use.
- */
-/*
  * Add a reference update to transaction.  new_sha1 is the value that
  * the reference should have after the update, or zeros if it should
  * be deleted.  If have_old is true, then old_sha1 holds the value

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 31/41] refs.c: make prune_ref use a transaction to delete the ref
  2014-05-28 21:51   ` Jonathan Nieder
@ 2014-05-28 21:56     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 21:56 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Wed, May 28, 2014 at 2:51 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> Change prune_ref to delete the ref using a ref transaction. To do this we also
>> need to add a new flag REF_ISPRUNING that will tell the transaction that we
>> do not want to delete this ref from the packed refs.
>
> s/from the packed refs/from packed-refs, nor delete the ref's reflog/
>
> [...]
>> --- a/refs.h
>> +++ b/refs.h
>> @@ -235,6 +235,11 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
>>   * The following functions add a reference check or update to a
>>   * ref_transaction.  In all of them, refname is the name of the
>>   * reference to be affected.  The functions make internal copies of
>>   * refname, so the caller retains ownership of the parameter.  flags
>>   * can be REF_NODEREF; it is passed to update_ref_lock().
>>   */
>>
>>  /*
>> + * ref_transaction_update ref_transaction_create and ref_transaction_delete
>> + * all take a flag argument. Currently the only public flag is REF_NODEREF.
>> + * Flag values >= 0x100 are reserved for internal use.
>> + */
>> +/*
>>   * Add a reference update to transaction.  new_sha1 is the value that
>
> The comment right before here already tries to explain the flag argument,
> though it isn't in an obvious place so it's easy to miss.  Maybe the flag
> argument should be explained in the overview documentation for the
> ref_transaction API near the top of the file (but I haven't thought that
> through, so leaving it alone).
>
> How about this as a way to make the reserved flag values easier to
> find when adding new flags?
>
> diff --git i/refs.h w/refs.h
> index 25ac4a9..dee7c8f 100644
> --- i/refs.h
> +++ w/refs.h
> @@ -171,8 +171,17 @@ extern int ref_exists(const char *);
>   */
>  extern int peel_ref(const char *refname, unsigned char *sha1);
>
> -/** Locks any ref (for 'HEAD' type refs). */
> +/*
> + * Flags controlling lock_any_ref_for_update(), ref_transaction_update(),
> + * ref_transaction_create(), etc.
> + * REF_NODEREF: act on the ref directly, instead of dereferencing
> + *              symbolic references.
> + *
> + * Flags >= 0x100 are reserved for internal use.
> + */
>  #define REF_NODEREF    0x01
> +
> +/** Locks any ref (for 'HEAD' type refs). */
>  extern struct ref_lock *lock_any_ref_for_update(const char *refname,
>                                                 const unsigned char *old_sha1,
>                                                 int flags, int *type_p);
> @@ -265,11 +274,6 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);
>   */
>
>  /*
> - * ref_transaction_update ref_transaction_create and ref_transaction_delete
> - * all take a flag argument. Currently the only public flag is REF_NODEREF.
> - * Flag values >= 0x100 are reserved for internal use.
> - */
> -/*
>   * Add a reference update to transaction.  new_sha1 is the value that
>   * the reference should have after the update, or zeros if it should
>   * be deleted.  If have_old is true, then old_sha1 holds the value

Thanks. Changed.

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-28 19:47   ` Jonathan Nieder
@ 2014-05-28 22:06     ` Ronnie Sahlberg
  2014-05-28 22:17       ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 22:06 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Wed, May 28, 2014 at 12:47 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> --- a/fast-import.c
>> +++ b/fast-import.c
>> @@ -1735,15 +1735,22 @@ static void dump_tags(void)
>>  {
>>       static const char *msg = "fast-import";
>>       struct tag *t;
>> -     struct ref_lock *lock;
>>       char ref_name[PATH_MAX];
>> +     struct strbuf err = STRBUF_INIT;
>> +     struct ref_transaction *transaction;
>>
>> +     transaction = ref_transaction_begin(&err);
>>       for (t = first_tag; t; t = t->next_tag) {
>> -             sprintf(ref_name, "tags/%s", t->name);
>> +             snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);
>
> That ignores the error if the refname happens to go longer than
> PATH_MAX.
>
> This is the part of fast-import that doesn't need to be super
> fast ;-).  (The objects have been written to the pack, and now
> we just need to write some refs.)  Could this use a strbuf?  By that,
> I mean something like
>
> diff --git i/fast-import.c w/fast-import.c
> index 3db5b3d..d5f6e63 100644
> --- i/fast-import.c
> +++ w/fast-import.c
> @@ -1735,21 +1735,28 @@ static void dump_tags(void)
>  {
>         static const char *msg = "fast-import";
>         struct tag *t;
> -       char ref_name[PATH_MAX];
> +       struct strbuf ref_name = STRBUF_INIT;
>         struct strbuf err = STRBUF_INIT;
>         struct ref_transaction *transaction;
>
> +       strbuf_addstr(&ref_name, "refs/tags/");
> +
>         transaction = ref_transaction_begin(&err);
>         for (t = first_tag; t; t = t->next_tag) {
> -               snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);
> +               strbuf_setlen(&ref_name, strlen("refs/tags/"));
> +               strbuf_addstr(&ref_name, t->name);
>
> -               if (ref_transaction_update(transaction, ref_name, t->sha1,
> -                                          NULL, 0, 0, &err))
> -                       break;
> +               if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
> +                                          NULL, 0, 0, &err)) {
> +                       failure |= error("%s", err.buf);
> +                       goto done;
> +               }
>         }
>         if (ref_transaction_commit(transaction, msg, &err))
>                 failure |= error("%s", err.buf);
> +done:
>         ref_transaction_free(transaction);
> +       strbuf_release(&ref_name);
>         strbuf_release(&err);
>  }

Changed to strbuf.  Thanks.

>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-28 22:06     ` Ronnie Sahlberg
@ 2014-05-28 22:17       ` Jonathan Nieder
  2014-05-28 23:20         ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 22:17 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Ronnie Sahlberg wrote:
> On Wed, May 28, 2014 at 12:47 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:

>> --- i/fast-import.c
>> +++ w/fast-import.c
>> @@ -1735,21 +1735,28 @@ static void dump_tags(void)
>>  {
>>         static const char *msg = "fast-import";
>>         struct tag *t;
>> -       char ref_name[PATH_MAX];
>> +       struct strbuf ref_name = STRBUF_INIT;
>>         struct strbuf err = STRBUF_INIT;
>>         struct ref_transaction *transaction;
>>
>> +       strbuf_addstr(&ref_name, "refs/tags/");
>> +
>>         transaction = ref_transaction_begin(&err);
>>         for (t = first_tag; t; t = t->next_tag) {
>> -               snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);
>> +               strbuf_setlen(&ref_name, strlen("refs/tags/"));
>> +               strbuf_addstr(&ref_name, t->name);
>>
>> -               if (ref_transaction_update(transaction, ref_name, t->sha1,
>> -                                          NULL, 0, 0, &err))
>> -                       break;
>> +               if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
>> +                                          NULL, 0, 0, &err)) {
>> +                       failure |= error("%s", err.buf);
>> +                       goto done;
>> +               }
>>         }
>>         if (ref_transaction_commit(transaction, msg, &err))
>>                 failure |= error("%s", err.buf);
>> +done:
>>         ref_transaction_free(transaction);
>> +       strbuf_release(&ref_name);
>>         strbuf_release(&err);
>>  }
>
> Changed to strbuf.  Thanks.

Thanks.  The semantics when ref_transaction_update() fail seem weird ---
see above.  (refs.h tells me "A failure to update means that the
transaction as a whole has failed and will need to be rolled back", so I
assume that the function should be rolling back instead of calling
_commit at that point.)

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-28 22:17       ` Jonathan Nieder
@ 2014-05-28 23:20         ` Ronnie Sahlberg
  2014-05-28 23:39           ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-28 23:20 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Wed, May 28, 2014 at 3:17 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>> On Wed, May 28, 2014 at 12:47 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
>
>>> --- i/fast-import.c
>>> +++ w/fast-import.c
>>> @@ -1735,21 +1735,28 @@ static void dump_tags(void)
>>>  {
>>>         static const char *msg = "fast-import";
>>>         struct tag *t;
>>> -       char ref_name[PATH_MAX];
>>> +       struct strbuf ref_name = STRBUF_INIT;
>>>         struct strbuf err = STRBUF_INIT;
>>>         struct ref_transaction *transaction;
>>>
>>> +       strbuf_addstr(&ref_name, "refs/tags/");
>>> +
>>>         transaction = ref_transaction_begin(&err);
>>>         for (t = first_tag; t; t = t->next_tag) {
>>> -               snprintf(ref_name, PATH_MAX, "refs/tags/%s", t->name);
>>> +               strbuf_setlen(&ref_name, strlen("refs/tags/"));
>>> +               strbuf_addstr(&ref_name, t->name);
>>>
>>> -               if (ref_transaction_update(transaction, ref_name, t->sha1,
>>> -                                          NULL, 0, 0, &err))
>>> -                       break;
>>> +               if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
>>> +                                          NULL, 0, 0, &err)) {
>>> +                       failure |= error("%s", err.buf);
>>> +                       goto done;
>>> +               }
>>>         }
>>>         if (ref_transaction_commit(transaction, msg, &err))
>>>                 failure |= error("%s", err.buf);
>>> +done:
>>>         ref_transaction_free(transaction);
>>> +       strbuf_release(&ref_name);
>>>         strbuf_release(&err);
>>>  }
>>
>> Changed to strbuf.  Thanks.
>
> Thanks.  The semantics when ref_transaction_update() fail seem weird ---
> see above.  (refs.h tells me "A failure to update means that the
> transaction as a whole has failed and will need to be rolled back", so I
> assume that the function should be rolling back instead of calling
> _commit at that point.)


diff --git a/fast-import.c b/fast-import.c
index 4a7b196..e24c7e4 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1735,15 +1735,24 @@ static void dump_tags(void)
 {
        static const char *msg = "fast-import";
        struct tag *t;
-       struct ref_lock *lock;
-       char ref_name[PATH_MAX];
+       struct strbuf ref_name = STRBUF_INIT;
+       struct strbuf err = STRBUF_INIT;
+       struct ref_transaction *transaction;

+       transaction = ref_transaction_begin(&err);
        for (t = first_tag; t; t = t->next_tag) {
-               sprintf(ref_name, "tags/%s", t->name);
-               lock = lock_ref_sha1(ref_name, NULL);
-               if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
-                       failure |= error("Unable to update %s", ref_name);
+               strbuf_reset(&ref_name);
+               strbuf_addf(&ref_name, "refs/tags/%s", t->name);
+
+               if (ref_transaction_update(transaction, ref_name.buf, t->sha1,
+                                          NULL, 0, 0, &err))
+                       break;
        }
+       if (ref_transaction_commit(transaction, msg, &err))
+               failure |= error("%s", err.buf);
+       ref_transaction_free(transaction);
+       strbuf_release(&ref_name);
+       strbuf_release(&err);
 }


I rely on the fact that if the transaction has failed then it is safe
to call ref_transaction_commit since it is guaranteed to return an
error too.

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-28 23:20         ` Ronnie Sahlberg
@ 2014-05-28 23:39           ` Jonathan Nieder
  2014-05-29 16:10             ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-28 23:39 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Ronnie Sahlberg wrote:

> I rely on the fact that if the transaction has failed then it is safe
> to call ref_transaction_commit since it is guaranteed to return an
> error too.

Yes, I am saying that behavior for ref_transaction_commit is weird.

Usually when ref_transaction_commit is called I can do

	struct strbuf err = STRBUF_INIT;
	if (ref_transaction_commit(..., &err))
		die("%s", err.buf);

and I know that since ref_transaction_commit has returned a nonzero
result, err.buf is populated with a sensible message that will
describe what went wrong.

That's true even if there's a bug elsewhere in code I didn't write
(e.g., someone forgot to check the return value from
ref_transaction_update).

But the guarantee you are describing removes that property.  It
creates a case where ref_transaction_commit can return nonzero without
updating err.  So I get the following message:

	fatal:

I don't think that's a good outcome.

Sure, if I am well acquainted with the API, I can make sure to use the
same strbuf for all transaction API calls.  But that would result in
strange behavior, too: if multiple _update calls fail, then I get
concatenated messages.

Okay, I can make sure to do at most one failing _update, before
calling _commit and printing the error.  But at that point, what is
the advantage over normal exception handling, where the error gets
handled at the _update call site?

Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-28 23:39           ` Jonathan Nieder
@ 2014-05-29 16:10             ` Ronnie Sahlberg
  2014-05-29 17:41               ` Jonathan Nieder
  0 siblings, 1 reply; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-05-29 16:10 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Wed, May 28, 2014 at 4:39 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> I rely on the fact that if the transaction has failed then it is safe
>> to call ref_transaction_commit since it is guaranteed to return an
>> error too.
>
> Yes, I am saying that behavior for ref_transaction_commit is weird.
>
> Usually when ref_transaction_commit is called I can do
>
>         struct strbuf err = STRBUF_INIT;
>         if (ref_transaction_commit(..., &err))
>                 die("%s", err.buf);
>
> and I know that since ref_transaction_commit has returned a nonzero
> result, err.buf is populated with a sensible message that will
> describe what went wrong.
>
> That's true even if there's a bug elsewhere in code I didn't write
> (e.g., someone forgot to check the return value from
> ref_transaction_update).
>
> But the guarantee you are describing removes that property.  It
> creates a case where ref_transaction_commit can return nonzero without
> updating err.  So I get the following message:
>
>         fatal:
>
> I don't think that's a good outcome.

In this case "fatal:" can not happen.
This is no more subtle than most of the git core.


I have changed this function to explicitly abort on _update failing
but I think this is making the api too restrictive.


>
> Sure, if I am well acquainted with the API, I can make sure to use the
> same strbuf for all transaction API calls.  But that would result in
> strange behavior, too: if multiple _update calls fail, then I get
> concatenated messages.
>
> Okay, I can make sure to do at most one failing _update, before
> calling _commit and printing the error.  But at that point, what is
> the advantage over normal exception handling, where the error gets
> handled at the _update call site?
>
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-29 16:10             ` Ronnie Sahlberg
@ 2014-05-29 17:41               ` Jonathan Nieder
  2014-06-03 21:32                 ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-29 17:41 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git@vger.kernel.org, Michael Haggerty

Ronnie Sahlberg wrote:
> On Wed, May 28, 2014 at 4:39 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:

>> Usually when ref_transaction_commit is called I can do
>>
>>         struct strbuf err = STRBUF_INIT;
>>         if (ref_transaction_commit(..., &err))
>>                 die("%s", err.buf);
>>
>> and I know that since ref_transaction_commit has returned a nonzero
>> result, err.buf is populated with a sensible message that will
>> describe what went wrong.
[...]
>> But the guarantee you are describing removes that property.  It
>> creates a case where ref_transaction_commit can return nonzero without
>> updating err.  So I get the following message:
>>
>>         fatal:
>>
>> I don't think that's a good outcome.
>
> In this case "fatal:" can not happen.
> This is no more subtle than most of the git core.
>
> I have changed this function to explicitly abort on _update failing
> but I think this is making the api too restrictive.

I don't want to push you toward making a change you think is wrong.  I
certainly don't own the codebase, and there are lots of other people
(e.g., Michael, Junio, Jeff) to get advice from.  So I guess I should
try to address this.

I'm not quite sure what you mean by too restrictive.

 a. Having API constraints that aren't enforced by the function makes
    using the API too fussy.

    I agree with that.  That was something I liked about keeping track
    of the OPEN/CLOSED state of a transaction, which would let
    functions like _commit die() if someone is misusing the API so the
    problem gets detected early.

 b. Having to check the return value from _update() is too fussy.
 
    It certainly seems *possible* to have an API that doesn't require
    checking the return value, while still avoiding the usability
    problem I described in the quoted message above.  For example:

     * _update() returns void and has no strbuf parameter
     * error handling happens by checking the error from _commit()

    That would score well on the scale described at
    http://ozlabs.org/~rusty/index.cgi/tech/2008-03-30.html

    An API where checking the return value is optional would be
    doable, too.  For example:

     * _update() returns int and has a strbuf parameter
     * if the strbuf parameter is NULL, the caller is expected to
       wait for _commit() to check for errors, and a relevant
       message will be passed back then
     * if the strbuf parameter is non-NULL, then calling _commit()
       after an error is an API violation

I don't understand the comment about no more subtle than most of git.
Are you talking about the errno action at a distance you found in some
functions?  I thought we agreed that those were mistakes that accrue
when people aim for a quick fix without thinking about maintainability
and something git should have less of.

Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 32/41] refs.c: make delete_ref use a transaction
  2014-05-27 20:25 ` [PATCH v11 32/41] refs.c: make delete_ref use a transaction Ronnie Sahlberg
@ 2014-05-30 17:28   ` Jonathan Nieder
  2014-06-03 16:59     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-30 17:28 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  refs.c | 34 +++++++++++++---------------------
>  1 file changed, 13 insertions(+), 21 deletions(-)

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

[...]
> +++ b/refs.c
[...]
> @@ -2542,24 +2537,21 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
>  
>  int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
>  {
[...]
> +	if (!transaction ||
> +	    ref_transaction_delete(transaction, refname, sha1, delopt,
> +				   sha1 && !is_null_sha1(sha1), &err) ||
> +	    ref_transaction_commit(transaction, NULL, &err)) {
> +		error("%s", err.buf);
> +		ref_transaction_free(transaction);
> +		strbuf_release(&err);
>  		return 1;
> +	}
[...]
> -	ret |= repack_without_ref(lock->ref_name);

The old return value could be 1 or -1 depending on how the deletion
failed.  Now it's consistently 1.

The only callers I see that care are cmd_symbolic_ref and
cmd_update_ref, for which 1 is better (-1 would result in an exit
status of 255, which means something like "died with signal 127").

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit
  2014-05-27 20:25 ` [PATCH v11 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit Ronnie Sahlberg
@ 2014-05-30 17:38   ` Jonathan Nieder
  2014-06-03 17:01     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-30 17:38 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Change the reference transactions so that we pass the reflog message
> through to the create/delete/update function instead of the commit message.
> This allows for individual messages for each change in a multi ref
> transaction.

Nice.

That reminds me: in the future, do we want to have some way to figure
out what ref updates happened together?  E.g., cvsnt introduced commit
identifiers to answer a similar kind of question in CVS per-file
history.  If some backend wants to support that, the API this patch
introduces would handle it fine --- good.

[...]
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -673,10 +673,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
>  			}
>  		}
>  	}
> -
>  	if (rc & STORE_REF_ERROR_DF_CONFLICT)
>  		error(_("some local refs could not be updated; try running\n"
> -		      " 'git remote prune %s' to remove any old, conflicting "
> +		      "'git remote prune %s' to remove any old, conflicting "
>  		      "branches"), remote_name);

Unrelated change snuck in?

The rest of the patch is
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

diff --git a/builtin/fetch.c b/builtin/fetch.c
index faa1233..55f457c 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -673,9 +673,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 			}
 		}
 	}
+
 	if (rc & STORE_REF_ERROR_DF_CONFLICT)
 		error(_("some local refs could not be updated; try running\n"
-		      "'git remote prune %s' to remove any old, conflicting "
+		      " 'git remote prune %s' to remove any old, conflicting "
 		      "branches"), remote_name);
 
  abort:

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 35/41] refs.c: pack all refs before we start to rename a ref
  2014-05-27 20:25 ` [PATCH v11 35/41] refs.c: pack all refs before we start to rename a ref Ronnie Sahlberg
@ 2014-05-30 18:08   ` Jonathan Nieder
  2014-06-03 16:39     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-30 18:08 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> We want to do this to make it easier to handle atomic renames in rename_ref for
> the case 'git branch -m foo/bar foo'.

In an ideal world, a part of me would rather see "git branch -m" doing
something more targeted by only packing the two refs it is working
with, and only when it knows it has to so the normal "git branch -m
foo bar" case doesn't have to suffer.  But:

That would be more complicated.

Packing refs is not very slow and has some good benefits for
performance of later commands.  Once we've committed to writing out a
new pack-refs file, it's not so bad to enumerate all loose refs to get
more benefit from writing out the new packed-refs file.

Renaming refs is something usually done by humans and not by scripts
or remote clients.  I'm not too worried about "git branch -m" in a
tight loop getting slower.

So I think this is an okay thing to do.

>                                       If we can guarantee that foo/bar does not
> exist as a loose ref we can avoid locking foo/bar.lock during transaction
> commit

That is not quite true --- there's still value in locking foo/bar to
avoid clobbering a concurrent ref update.

If git performed the entire rename transaction in the packed-refs
file, we could avoid that race completely (for 'git branch -m foo/bar
foo': lock refs, make sure the loose refs are pruned, perform the
update in packed-refs, unlock refs.  for 'git branch -m foo foo/bar':
lock foo, prune foo, lock foo/bar, prune foo/bar, perform the update
in packed-refs, unlock refs).

Even without doing that, packing refs first has a benefit in terms of
ordering: if you do (1) delete loose foo/bar, (2) write loose foo, (3)
rewrite packed-refs without foo/bar, then because you've packed refs
first, no objects become unreferenced during step (1), which means
that a hypothetical version of "git gc" that used filesystem snapshots
would not see any relevant objects as safe to clean up no matter when
it runs.

> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> ---
>  refs.c            | 3 +++
>  t/t3200-branch.sh | 6 +++---
>  2 files changed, 6 insertions(+), 3 deletions(-)

With a clearer commit message,
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic
  2014-05-27 20:25 ` [PATCH v11 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic Ronnie Sahlberg
@ 2014-05-30 18:12   ` Jonathan Nieder
  2014-06-03 18:33     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-30 18:12 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Ronnie Sahlberg wrote:

> Move the check for check_refname_format from lock_any_ref_for_update
> to lock_ref_sha1_basic. At some later stage we will get rid of
> lock_any_ref_for_update completely.

Do you know if this will cause any functional change?

What is the potential impact?  Is that impact worth it?  (Given how
broken the recovery codepaths currently are, I suspect this change is
worth it, but it seems worth documenting in the log message.)

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers
  2014-05-27 20:25 ` [PATCH v11 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers Ronnie Sahlberg
@ 2014-05-31  0:22   ` Jonathan Nieder
  2014-06-03 18:21     ` Ronnie Sahlberg
  0 siblings, 1 reply; 89+ messages in thread
From: Jonathan Nieder @ 2014-05-31  0:22 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Hi,

Ronnie Sahlberg wrote:

> For these cases, save the errno value and abort and make sure that the caller
> can see errno==ENOTDIR.
>
> Also start defining specific return codes for _commit, assign -1 as a generic
> error and -2 as the error that refers to a name conflict. Callers can (and
> should) use that return value inspecting errno directly.

Heh.  Here's a patch on top to hopefully finish that part of the job.

Unfortunately, the value of errno after calling lock_ref_sha1_basic is
not reliable.
http://thread.gmane.org/gmane.comp.version-control.git/249159/focus=249407
lists the error paths that are broken (marked with "[!]" in that
message).

diff --git i/builtin/fetch.c w/builtin/fetch.c
index b13e8f9..0a01cda 100644
--- i/builtin/fetch.c
+++ w/builtin/fetch.c
@@ -377,6 +377,7 @@ static int s_update_ref(const char *action,
 	char *rla = getenv("GIT_REFLOG_ACTION");
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
+	int ret, df_conflict = 0;
 
 	if (dry_run)
 		return 0;
@@ -387,16 +388,22 @@ static int s_update_ref(const char *action,
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
 	    ref_transaction_update(transaction, ref->name, ref->new_sha1,
-				   ref->old_sha1, 0, check_old, msg, &err) ||
-	    ref_transaction_commit(transaction, &err)) {
-		ref_transaction_free(transaction);
-		error("%s", err.buf);
-		strbuf_release(&err);
-		return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
-					  STORE_REF_ERROR_OTHER;
-	}
+				   ref->old_sha1, 0, check_old, msg, &err))
+		goto fail;
+
+	ret = ref_transaction_commit(transaction, &err);
+	if (ret == UPDATE_REFS_NAME_CONFLICT)
+		df_conflict = 1;
+	if (ret)
+		goto fail;
 	ref_transaction_free(transaction);
 	return 0;
+fail:
+	ref_transaction_free(transaction);
+	error("%s", err.buf);
+	strbuf_release(&err);
+	return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
+			   : STORE_REF_ERROR_OTHER;
 }
 
 #define REFCOL_WIDTH  10
diff --git i/refs.c w/refs.c
index dbaabba..b22b99b 100644
--- i/refs.c
+++ w/refs.c
@@ -3499,7 +3499,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
-	int ret = 0, delnum = 0, i, save_errno = 0;
+	int ret = 0, delnum = 0, i, df_conflict = 0;
 	const char **delnames;
 	int n = transaction->nr;
 	struct ref_update **updates = transaction->updates;
@@ -3535,7 +3535,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 						   delnames, delnum);
 		if (!update->lock) {
 			if (errno == ENOTDIR)
-				save_errno = errno;
+				df_conflict = 1;
 			if (err)
 				strbuf_addf(err, "Cannot lock the ref '%s'.",
 					    update->refname);
@@ -3590,8 +3590,7 @@ cleanup:
 		if (updates[i]->lock)
 			unlock_ref(updates[i]->lock);
 	free(delnames);
-	errno = save_errno;
-	if (save_errno == ENOTDIR)
+	if (df_conflict)
 		ret = -2;
 	return ret;
 }
diff --git i/refs.h w/refs.h
index 88732a1..1583097 100644
--- i/refs.h
+++ w/refs.h
@@ -325,9 +325,11 @@ int ref_transaction_delete(struct ref_transaction *transaction,
  * problem.
  * If the transaction is already in failed state this function will return
  * an error.
- * Function returns 0 on success, -1 for generic failures and -2 if the
- * failure was due to a name collision (ENOTDIR).
+ * Function returns 0 on success, -1 for generic failures and
+ * UPDATE_REFS_NAME_CONFLICT (-2) if the failure was due to a name
+ * collision (ENOTDIR).
  */
+#define UPDATE_REFS_NAME_CONFLICT -2
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err);
 

^ permalink raw reply related	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 35/41] refs.c: pack all refs before we start to rename a ref
  2014-05-30 18:08   ` Jonathan Nieder
@ 2014-06-03 16:39     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-06-03 16:39 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

Thanks!

On Fri, May 30, 2014 at 11:08 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> We want to do this to make it easier to handle atomic renames in rename_ref for
>> the case 'git branch -m foo/bar foo'.
>
> In an ideal world, a part of me would rather see "git branch -m" doing
> something more targeted by only packing the two refs it is working
> with, and only when it knows it has to so the normal "git branch -m
> foo bar" case doesn't have to suffer.  But:
>
> That would be more complicated.
>
> Packing refs is not very slow and has some good benefits for
> performance of later commands.  Once we've committed to writing out a
> new pack-refs file, it's not so bad to enumerate all loose refs to get
> more benefit from writing out the new packed-refs file.
>
> Renaming refs is something usually done by humans and not by scripts
> or remote clients.  I'm not too worried about "git branch -m" in a
> tight loop getting slower.
>
> So I think this is an okay thing to do.
>
>>                                       If we can guarantee that foo/bar does not
>> exist as a loose ref we can avoid locking foo/bar.lock during transaction
>> commit
>
> That is not quite true --- there's still value in locking foo/bar to
> avoid clobbering a concurrent ref update.
>
> If git performed the entire rename transaction in the packed-refs
> file, we could avoid that race completely (for 'git branch -m foo/bar
> foo': lock refs, make sure the loose refs are pruned, perform the
> update in packed-refs, unlock refs.  for 'git branch -m foo foo/bar':
> lock foo, prune foo, lock foo/bar, prune foo/bar, perform the update
> in packed-refs, unlock refs).
>
> Even without doing that, packing refs first has a benefit in terms of
> ordering: if you do (1) delete loose foo/bar, (2) write loose foo, (3)
> rewrite packed-refs without foo/bar, then because you've packed refs
> first, no objects become unreferenced during step (1), which means
> that a hypothetical version of "git gc" that used filesystem snapshots
> would not see any relevant objects as safe to clean up no matter when
> it runs.
>
>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
>> ---
>>  refs.c            | 3 +++
>>  t/t3200-branch.sh | 6 +++---
>>  2 files changed, 6 insertions(+), 3 deletions(-)
>
> With a clearer commit message,
> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 32/41] refs.c: make delete_ref use a transaction
  2014-05-30 17:28   ` Jonathan Nieder
@ 2014-06-03 16:59     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-06-03 16:59 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

Thanks!

On Fri, May 30, 2014 at 10:28 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
>> ---
>>  refs.c | 34 +++++++++++++---------------------
>>  1 file changed, 13 insertions(+), 21 deletions(-)
>
> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
>
> [...]
>> +++ b/refs.c
> [...]
>> @@ -2542,24 +2537,21 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
>>
>>  int delete_ref(const char *refname, const unsigned char *sha1, int delopt)
>>  {
> [...]
>> +     if (!transaction ||
>> +         ref_transaction_delete(transaction, refname, sha1, delopt,
>> +                                sha1 && !is_null_sha1(sha1), &err) ||
>> +         ref_transaction_commit(transaction, NULL, &err)) {
>> +             error("%s", err.buf);
>> +             ref_transaction_free(transaction);
>> +             strbuf_release(&err);
>>               return 1;
>> +     }
> [...]
>> -     ret |= repack_without_ref(lock->ref_name);
>
> The old return value could be 1 or -1 depending on how the deletion
> failed.  Now it's consistently 1.
>
> The only callers I see that care are cmd_symbolic_ref and
> cmd_update_ref, for which 1 is better (-1 would result in an exit
> status of 255, which means something like "died with signal 127").
>
> Thanks,
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit
  2014-05-30 17:38   ` Jonathan Nieder
@ 2014-06-03 17:01     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-06-03 17:01 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Fri, May 30, 2014 at 10:38 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> Change the reference transactions so that we pass the reflog message
>> through to the create/delete/update function instead of the commit message.
>> This allows for individual messages for each change in a multi ref
>> transaction.
>
> Nice.
>
> That reminds me: in the future, do we want to have some way to figure
> out what ref updates happened together?  E.g., cvsnt introduced commit
> identifiers to answer a similar kind of question in CVS per-file
> history.  If some backend wants to support that, the API this patch
> introduces would handle it fine --- good.
>
> [...]
>> --- a/builtin/fetch.c
>> +++ b/builtin/fetch.c
>> @@ -673,10 +673,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
>>                       }
>>               }
>>       }
>> -
>>       if (rc & STORE_REF_ERROR_DF_CONFLICT)
>>               error(_("some local refs could not be updated; try running\n"
>> -                   " 'git remote prune %s' to remove any old, conflicting "
>> +                   "'git remote prune %s' to remove any old, conflicting "
>>                     "branches"), remote_name);
>
> Unrelated change snuck in?

Yeah, there shouldn't be a space there.

>
> The rest of the patch is
> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

Thanks!

>
> diff --git a/builtin/fetch.c b/builtin/fetch.c
> index faa1233..55f457c 100644
> --- a/builtin/fetch.c
> +++ b/builtin/fetch.c
> @@ -673,9 +673,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
>                         }
>                 }
>         }
> +
>         if (rc & STORE_REF_ERROR_DF_CONFLICT)
>                 error(_("some local refs could not be updated; try running\n"
> -                     "'git remote prune %s' to remove any old, conflicting "
> +                     " 'git remote prune %s' to remove any old, conflicting "
>                       "branches"), remote_name);
>
>   abort:

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers
  2014-05-31  0:22   ` Jonathan Nieder
@ 2014-06-03 18:21     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-06-03 18:21 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Fri, May 30, 2014 at 5:22 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Hi,
>
> Ronnie Sahlberg wrote:
>
>> For these cases, save the errno value and abort and make sure that the caller
>> can see errno==ENOTDIR.
>>
>> Also start defining specific return codes for _commit, assign -1 as a generic
>> error and -2 as the error that refers to a name conflict. Callers can (and
>> should) use that return value inspecting errno directly.
>
> Heh.  Here's a patch on top to hopefully finish that part of the job.

Thanks. Updated.

>
> Unfortunately, the value of errno after calling lock_ref_sha1_basic is
> not reliable.

I have changed the patch for lock_ref_sha1_basic so it now does :
  if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
    errno = EINVAL;
    return NULL;
  }

so this function should no longer fail without changing errno to a,
hopefully, sane value.


> http://thread.gmane.org/gmane.comp.version-control.git/249159/focus=249407
> lists the error paths that are broken (marked with "[!]" in that
> message).
>
> diff --git i/builtin/fetch.c w/builtin/fetch.c
> index b13e8f9..0a01cda 100644
> --- i/builtin/fetch.c
> +++ w/builtin/fetch.c
> @@ -377,6 +377,7 @@ static int s_update_ref(const char *action,
>         char *rla = getenv("GIT_REFLOG_ACTION");
>         struct ref_transaction *transaction;
>         struct strbuf err = STRBUF_INIT;
> +       int ret, df_conflict = 0;
>
>         if (dry_run)
>                 return 0;
> @@ -387,16 +388,22 @@ static int s_update_ref(const char *action,
>         transaction = ref_transaction_begin(&err);
>         if (!transaction ||
>             ref_transaction_update(transaction, ref->name, ref->new_sha1,
> -                                  ref->old_sha1, 0, check_old, msg, &err) ||
> -           ref_transaction_commit(transaction, &err)) {
> -               ref_transaction_free(transaction);
> -               error("%s", err.buf);
> -               strbuf_release(&err);
> -               return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
> -                                         STORE_REF_ERROR_OTHER;
> -       }
> +                                  ref->old_sha1, 0, check_old, msg, &err))
> +               goto fail;
> +
> +       ret = ref_transaction_commit(transaction, &err);
> +       if (ret == UPDATE_REFS_NAME_CONFLICT)
> +               df_conflict = 1;
> +       if (ret)
> +               goto fail;
>         ref_transaction_free(transaction);
>         return 0;
> +fail:
> +       ref_transaction_free(transaction);
> +       error("%s", err.buf);
> +       strbuf_release(&err);
> +       return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
> +                          : STORE_REF_ERROR_OTHER;
>  }
>
>  #define REFCOL_WIDTH  10
> diff --git i/refs.c w/refs.c
> index dbaabba..b22b99b 100644
> --- i/refs.c
> +++ w/refs.c
> @@ -3499,7 +3499,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
>  int ref_transaction_commit(struct ref_transaction *transaction,
>                            struct strbuf *err)
>  {
> -       int ret = 0, delnum = 0, i, save_errno = 0;
> +       int ret = 0, delnum = 0, i, df_conflict = 0;
>         const char **delnames;
>         int n = transaction->nr;
>         struct ref_update **updates = transaction->updates;
> @@ -3535,7 +3535,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
>                                                    delnames, delnum);
>                 if (!update->lock) {
>                         if (errno == ENOTDIR)
> -                               save_errno = errno;
> +                               df_conflict = 1;
>                         if (err)
>                                 strbuf_addf(err, "Cannot lock the ref '%s'.",
>                                             update->refname);
> @@ -3590,8 +3590,7 @@ cleanup:
>                 if (updates[i]->lock)
>                         unlock_ref(updates[i]->lock);
>         free(delnames);
> -       errno = save_errno;
> -       if (save_errno == ENOTDIR)
> +       if (df_conflict)
>                 ret = -2;
>         return ret;
>  }
> diff --git i/refs.h w/refs.h
> index 88732a1..1583097 100644
> --- i/refs.h
> +++ w/refs.h
> @@ -325,9 +325,11 @@ int ref_transaction_delete(struct ref_transaction *transaction,
>   * problem.
>   * If the transaction is already in failed state this function will return
>   * an error.
> - * Function returns 0 on success, -1 for generic failures and -2 if the
> - * failure was due to a name collision (ENOTDIR).
> + * Function returns 0 on success, -1 for generic failures and
> + * UPDATE_REFS_NAME_CONFLICT (-2) if the failure was due to a name
> + * collision (ENOTDIR).
>   */
> +#define UPDATE_REFS_NAME_CONFLICT -2
>  int ref_transaction_commit(struct ref_transaction *transaction,
>                            struct strbuf *err);
>

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 00/41] Use ref transactions
  2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
                   ` (40 preceding siblings ...)
  2014-05-27 20:26 ` [PATCH v11 41/41] refs.c: make write_ref_sha1 static Ronnie Sahlberg
@ 2014-06-03 18:31 ` Jonathan Nieder
  41 siblings, 0 replies; 89+ messages in thread
From: Jonathan Nieder @ 2014-06-03 18:31 UTC (permalink / raw
  To: Ronnie Sahlberg; +Cc: git, mhagger

Hi,

Ronnie Sahlberg wrote:

>                                                                        It
> converts all ref updates, inside refs.c as well as external, to use the
> transaction API for updates. This makes most of the ref updates to become
> atomic when there are failures locking or writing to a ref.

I'm still excited about this series.  Most of it looks ready.  The
remaining problems I see fall into a few categories:

 * The rename_ref codepath is strange.  You've convinced me that the
   approach you're taking there is not much of a regression, but it's
   confusing enough that I'd be happier if someone else takes a closer
   look (or I can try to find time to).

 * I think the approach taken in the patch "add transaction.status and
   track OPEN/CLOSED/ERROR" is a mistake and would make callers more
   error-prone.  The basic idea of checking that the caller is using
   the API right is valuable, so there is something in that patch I
   really like --- it's just the details (involving the same kind of
   easy-to-clobber error messages as errno provides with stdio) that
   seem broken.

   I suspect I'm just not communicating very well there.  Maybe
   mhagger or someone else could give it a sanity check.

 * Some commit messages (e.g., the one to "pack all refs before we
   start to rename a ref") are confusing.  That might be a sign that
   what those patches are trying to do is confusing.

 * The error handling in "add an err argument to repack_without_refs"
   is a thorny thicket.  It still has bugs.  I can completely
   understand not wanting to take that on but I think it is important
   to add a NEEDSWORK comment describing the known bugs to help people
   when they work with this code in the future.

I realize the process of addressing review comments in such a long
series has been a bit of a pain, and if there's anything I can do to
make it easier please let me know.  Hopefully adding some other
reviewers can help.

Thanks,
Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic
  2014-05-30 18:12   ` Jonathan Nieder
@ 2014-06-03 18:33     ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-06-03 18:33 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

On Fri, May 30, 2014 at 11:12 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>
>> Move the check for check_refname_format from lock_any_ref_for_update
>> to lock_ref_sha1_basic. At some later stage we will get rid of
>> lock_any_ref_for_update completely.
>
> Do you know if this will cause any functional change?
>
> What is the potential impact?  Is that impact worth it?  (Given how
> broken the recovery codepaths currently are, I suspect this change is
> worth it, but it seems worth documenting in the log message.)

Thanks.

Updated the commit message to mention this.

  This changes semantics for lock_ref_sha1_basic slightly. With this change
  it is no longer possible to open a ref that has a badly name which breaks
  any codepaths that tries to open and repair badly named refs. The normal refs
  API should not allow neither creating nor accessing refs with invalid names.
  If we need such recovery code we could add it as an option to git
fsck and have
  git fsck be the only sanctioned way of bypassing the normal API and checks.

>
> Thanks,
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

* Re: [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags
  2014-05-29 17:41               ` Jonathan Nieder
@ 2014-06-03 21:32                 ` Ronnie Sahlberg
  0 siblings, 0 replies; 89+ messages in thread
From: Ronnie Sahlberg @ 2014-06-03 21:32 UTC (permalink / raw
  To: Jonathan Nieder; +Cc: git@vger.kernel.org, Michael Haggerty

Thanks.

I have changed the transaction functions to die(BUG:) if the user
tries to call _update/_create/_delete on a failed transaction.



On Thu, May 29, 2014 at 10:41 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:
> Ronnie Sahlberg wrote:
>> On Wed, May 28, 2014 at 4:39 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
>
>>> Usually when ref_transaction_commit is called I can do
>>>
>>>         struct strbuf err = STRBUF_INIT;
>>>         if (ref_transaction_commit(..., &err))
>>>                 die("%s", err.buf);
>>>
>>> and I know that since ref_transaction_commit has returned a nonzero
>>> result, err.buf is populated with a sensible message that will
>>> describe what went wrong.
> [...]
>>> But the guarantee you are describing removes that property.  It
>>> creates a case where ref_transaction_commit can return nonzero without
>>> updating err.  So I get the following message:
>>>
>>>         fatal:
>>>
>>> I don't think that's a good outcome.
>>
>> In this case "fatal:" can not happen.
>> This is no more subtle than most of the git core.
>>
>> I have changed this function to explicitly abort on _update failing
>> but I think this is making the api too restrictive.
>
> I don't want to push you toward making a change you think is wrong.  I
> certainly don't own the codebase, and there are lots of other people
> (e.g., Michael, Junio, Jeff) to get advice from.  So I guess I should
> try to address this.
>
> I'm not quite sure what you mean by too restrictive.
>
>  a. Having API constraints that aren't enforced by the function makes
>     using the API too fussy.
>
>     I agree with that.  That was something I liked about keeping track
>     of the OPEN/CLOSED state of a transaction, which would let
>     functions like _commit die() if someone is misusing the API so the
>     problem gets detected early.
>
>  b. Having to check the return value from _update() is too fussy.
>
>     It certainly seems *possible* to have an API that doesn't require
>     checking the return value, while still avoiding the usability
>     problem I described in the quoted message above.  For example:
>
>      * _update() returns void and has no strbuf parameter
>      * error handling happens by checking the error from _commit()
>
>     That would score well on the scale described at
>     http://ozlabs.org/~rusty/index.cgi/tech/2008-03-30.html
>
>     An API where checking the return value is optional would be
>     doable, too.  For example:
>
>      * _update() returns int and has a strbuf parameter
>      * if the strbuf parameter is NULL, the caller is expected to
>        wait for _commit() to check for errors, and a relevant
>        message will be passed back then
>      * if the strbuf parameter is non-NULL, then calling _commit()
>        after an error is an API violation
>
> I don't understand the comment about no more subtle than most of git.
> Are you talking about the errno action at a distance you found in some
> functions?  I thought we agreed that those were mistakes that accrue
> when people aim for a quick fix without thinking about maintainability
> and something git should have less of.
>
> Jonathan

^ permalink raw reply	[flat|nested] 89+ messages in thread

end of thread, other threads:[~2014-06-03 21:32 UTC | newest]

Thread overview: 89+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-27 20:25 [PATCH v11 00/41] Use ref transactions Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 01/41] refs.c: remove ref_transaction_rollback Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 02/41] refs.c: ref_transaction_commit should not free the transaction Ronnie Sahlberg
2014-05-27 23:20   ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 03/41] refs.c: constify the sha arguments for ref_transaction_create|delete|update Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 04/41] refs.c: allow passing NULL to ref_transaction_free Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 05/41] refs.c: add a strbuf argument to ref_transaction_commit for error logging Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 06/41] refs.c: add an err argument to repack_without_refs Ronnie Sahlberg
2014-05-28  0:11   ` Jonathan Nieder
2014-05-28 15:31     ` Ronnie Sahlberg
2014-05-28 18:30       ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 07/41] refs.c: make ref_update_reject_duplicates take a strbuf argument for errors Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 08/41] refs.c: add an err argument to delete_ref_loose Ronnie Sahlberg
2014-05-28  0:25   ` Jonathan Nieder
2014-05-28 14:43     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 09/41] refs.c: make update_ref_write update a strbuf on failure Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 10/41] update-ref.c: log transaction error from the update_ref Ronnie Sahlberg
2014-05-28  0:27   ` Jonathan Nieder
2014-05-28 14:46     ` Ronnie Sahlberg
2014-05-28 16:49       ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 11/41] refs.c: remove the onerr argument to ref_transaction_commit Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 12/41] refs.c: change ref_transaction_update() to do error checking and return status Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 13/41] refs.c: change ref_transaction_create " Ronnie Sahlberg
2014-05-28  0:42   ` Jonathan Nieder
2014-05-28 14:55     ` Ronnie Sahlberg
2014-05-28 17:07       ` Jonathan Nieder
2014-05-28 17:15         ` Ronnie Sahlberg
2014-05-28 17:25           ` Jonathan Nieder
2014-05-28 18:01             ` Ronnie Sahlberg
2014-05-28 18:09               ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 14/41] refs.c: update ref_transaction_delete to check for error " Ronnie Sahlberg
2014-05-28 18:42   ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 15/41] refs.c: make ref_transaction_begin take an err argument Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR Ronnie Sahlberg
2014-05-28 18:51   ` Jonathan Nieder
2014-05-28 21:50     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 17/41] tag.c: use ref transactions when doing updates Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 18/41] replace.c: use the ref transaction functions for updates Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 19/41] commit.c: use ref transactions " Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 20/41] sequencer.c: use ref transactions for all ref updates Ronnie Sahlberg
2014-05-28 18:53   ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 21/41] fast-import.c: change update_branch to use ref transactions Ronnie Sahlberg
2014-05-28 18:54   ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 22/41] branch.c: use ref transaction for all ref updates Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 23/41] refs.c: change update_ref to use a transaction Ronnie Sahlberg
2014-05-28 19:31   ` Jonathan Nieder
2014-05-28 21:14     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 24/41] receive-pack.c: use a reference transaction for updating the refs Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 25/41] fast-import.c: use a ref transaction when dumping tags Ronnie Sahlberg
2014-05-28 19:47   ` Jonathan Nieder
2014-05-28 22:06     ` Ronnie Sahlberg
2014-05-28 22:17       ` Jonathan Nieder
2014-05-28 23:20         ` Ronnie Sahlberg
2014-05-28 23:39           ` Jonathan Nieder
2014-05-29 16:10             ` Ronnie Sahlberg
2014-05-29 17:41               ` Jonathan Nieder
2014-06-03 21:32                 ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 26/41] walker.c: use ref transaction for ref updates Ronnie Sahlberg
2014-05-28 19:56   ` Jonathan Nieder
2014-05-28 20:56     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 27/41] refs.c: make lock_ref_sha1 static Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 28/41] refs.c: remove the update_ref_lock function Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 29/41] refs.c: remove the update_ref_write function Ronnie Sahlberg
2014-05-28 20:39   ` Jonathan Nieder
2014-05-27 20:25 ` [PATCH v11 30/41] refs.c: remove lock_ref_sha1 Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 31/41] refs.c: make prune_ref use a transaction to delete the ref Ronnie Sahlberg
2014-05-28 21:51   ` Jonathan Nieder
2014-05-28 21:56     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 32/41] refs.c: make delete_ref use a transaction Ronnie Sahlberg
2014-05-30 17:28   ` Jonathan Nieder
2014-06-03 16:59     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit Ronnie Sahlberg
2014-05-30 17:38   ` Jonathan Nieder
2014-06-03 17:01     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 34/41] refs.c: pass NULL as *flags to read_ref_full Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 35/41] refs.c: pack all refs before we start to rename a ref Ronnie Sahlberg
2014-05-30 18:08   ` Jonathan Nieder
2014-06-03 16:39     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic Ronnie Sahlberg
2014-05-30 18:12   ` Jonathan Nieder
2014-06-03 18:33     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 37/41] refs.c: call lock_ref_sha1_basic directly from commit Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 38/41] refs.c: pass a skip list to name_conflict_fn Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers Ronnie Sahlberg
2014-05-31  0:22   ` Jonathan Nieder
2014-06-03 18:21     ` Ronnie Sahlberg
2014-05-27 20:25 ` [PATCH v11 40/41] fetch.c: change s_update_ref to use a ref transaction Ronnie Sahlberg
2014-05-27 20:26 ` [PATCH v11 41/41] refs.c: make write_ref_sha1 static Ronnie Sahlberg
2014-06-03 18:31 ` [PATCH v11 00/41] Use ref transactions Jonathan Nieder

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).