git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 00/16] LMDB refs backend atop pre-vtable
@ 2015-12-03  0:35 David Turner
  2015-12-03  0:35 ` [PATCH 01/16] refs: add a backend method structure with transaction functions David Turner
                   ` (16 more replies)
  0 siblings, 17 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

I'm starting the patchset numbering over from 1 here, because this
version of the patchset is a subset of the last version.

This version of the patch set applies on top of
dt/refs-backend-pre-vtable.  This required moving a bunch of stuff
that was in refs.h in previous versions into refs/refs-internal.h.

Since the last patchset, I added support for symlink HEAD refs, and
broke out the initdb stuff into a separate commit.

I also rearranged the order of the backend functions to make the vtable
easier to read.

I removed for_each_reftype_fullpath, which was at one point in next
but is not anymore.  And I did a bit more code cleanup/rearrangement
on the LMDB stuff: I removed memory leaks, improved style, and just
generally spruced things up.

As usual, the normal tests and the same set of hacked tests pass.

I've read over each of these patches a few times, but I've probably
still managed to miss things.  I look forward to your review.

David Turner (13):
  refs: add do_for_each_per_worktree_ref
  refs: add methods for reflog
  refs: add method for initial ref transaction commit
  refs: add method for delete_refs
  refs: add methods to init refs backend and db
  refs: add method to rename refs
  refs: make lock generic
  refs: move duplicate check to common code
  refs: always handle non-normal refs in files backend
  init: allow alternate backends to be set for new repos
  refs: allow ref backend to be set for clone
  refs: add LMDB refs backend
  refs: tests for lmdb backend

Ronnie Sahlberg (3):
  refs: add a backend method structure with transaction functions
  refs: add methods for misc ref operations
  refs: add methods for the ref iterators

 .gitignore                                     |    1 +
 Documentation/config.txt                       |    7 +
 Documentation/git-clone.txt                    |    4 +
 Documentation/git-init-db.txt                  |    2 +-
 Documentation/git-init.txt                     |    7 +-
 Documentation/technical/refs-lmdb-backend.txt  |   50 +
 Documentation/technical/repository-version.txt |    5 +
 Makefile                                       |   12 +
 builtin/clone.c                                |   27 +-
 builtin/init-db.c                              |   35 +-
 builtin/submodule--helper.c                    |    5 +-
 cache.h                                        |   10 +
 config.c                                       |   34 +
 configure.ac                                   |   33 +
 contrib/workdir/git-new-workdir                |    3 +
 environment.c                                  |    1 +
 path.c                                         |   32 +-
 refs.c                                         |  406 ++++-
 refs.h                                         |   17 +
 refs/files-backend.c                           |  257 +--
 refs/lmdb-backend.c                            | 2054 ++++++++++++++++++++++++
 refs/refs-internal.h                           |  118 +-
 setup.c                                        |   32 +-
 t/t1460-refs-lmdb-backend.sh                   | 1109 +++++++++++++
 t/t1470-refs-lmdb-backend-reflog.sh            |  359 +++++
 t/test-lib.sh                                  |    1 +
 test-refs-lmdb-backend.c                       |   68 +
 27 files changed, 4553 insertions(+), 136 deletions(-)
 create mode 100644 Documentation/technical/refs-lmdb-backend.txt
 create mode 100644 refs/lmdb-backend.c
 create mode 100755 t/t1460-refs-lmdb-backend.sh
 create mode 100755 t/t1470-refs-lmdb-backend-reflog.sh
 create mode 100644 test-refs-lmdb-backend.c

-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 01/16] refs: add a backend method structure with transaction functions
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-05  0:07   ` Junio C Hamano
  2015-12-03  0:35 ` [PATCH 02/16] refs: add methods for misc ref operations David Turner
                   ` (15 subsequent siblings)
  16 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: Ronnie Sahlberg, David Turner

From: Ronnie Sahlberg <sahlberg@google.com>

Add a ref structure for backend methods. Start by adding a method pointer
for the transaction commit function.

Add a function set_refs_backend to switch between backends. The files
based backend is the default.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 32 ++++++++++++++++++++++++++++++++
 refs.h               |  2 ++
 refs/files-backend.c | 10 ++++++++--
 refs/refs-internal.h | 10 ++++++++++
 4 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/refs.c b/refs.c
index 0f7628d..babba8a 100644
--- a/refs.c
+++ b/refs.c
@@ -10,6 +10,31 @@
 #include "tag.h"
 
 /*
+ * We always have a files backend and it is the default.
+ */
+extern struct ref_be refs_be_files;
+struct ref_be *the_refs_backend = &refs_be_files;
+/*
+ * List of all available backends
+ */
+struct ref_be *refs_backends = &refs_be_files;
+
+/*
+ * This function is used to switch to an alternate backend.
+ */
+int set_refs_backend(const char *name)
+{
+	struct ref_be *be;
+
+	for (be = refs_backends; be; be = be->next)
+		if (!strcmp(be->name, name)) {
+			the_refs_backend = be;
+			return 0;
+		}
+	return 1;
+}
+
+/*
  * How to handle various characters in refnames:
  * 0: An acceptable character for refs
  * 1: End-of-component
@@ -1082,3 +1107,10 @@ int rename_ref_available(const char *oldname, const char *newname)
 	strbuf_release(&err);
 	return ret;
 }
+
+/* backend functions */
+int ref_transaction_commit(struct ref_transaction *transaction,
+			   struct strbuf *err)
+{
+	return the_refs_backend->transaction_commit(transaction, err);
+}
diff --git a/refs.h b/refs.h
index 7a04077..4e5477d 100644
--- a/refs.h
+++ b/refs.h
@@ -508,4 +508,6 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,
 			 reflog_expiry_cleanup_fn cleanup_fn,
 			 void *policy_cb_data);
 
+int set_refs_backend(const char *name);
+
 #endif /* REFS_H */
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 4db3e36..be34772 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3123,8 +3123,8 @@ static int ref_update_reject_duplicates(struct string_list *refnames,
 	return 0;
 }
 
-int ref_transaction_commit(struct ref_transaction *transaction,
-			   struct strbuf *err)
+static int files_transaction_commit(struct ref_transaction *transaction,
+				    struct strbuf *err)
 {
 	int ret = 0, i;
 	int n = transaction->nr;
@@ -3510,3 +3510,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
 	unlock_ref(lock);
 	return -1;
 }
+
+struct ref_be refs_be_files = {
+	NULL,
+	"files",
+	files_transaction_commit,
+};
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index c7dded3..f2c74f3 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -197,4 +197,14 @@ const char *find_descendant_ref(const char *dirname,
 
 int rename_ref_available(const char *oldname, const char *newname);
 
+/* refs backends */
+typedef int ref_transaction_commit_fn(struct ref_transaction *transaction,
+				      struct strbuf *err);
+
+struct ref_be {
+	struct ref_be *next;
+	const char *name;
+	ref_transaction_commit_fn *transaction_commit;
+};
+
 #endif /* REFS_REFS_INTERNAL_H */
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
  2015-12-03  0:35 ` [PATCH 01/16] refs: add a backend method structure with transaction functions David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-11 23:33   ` Junio C Hamano
  2015-12-11 23:39   ` Junio C Hamano
  2015-12-03  0:35 ` [PATCH 03/16] refs: add methods for the ref iterators David Turner
                   ` (14 subsequent siblings)
  16 siblings, 2 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: Ronnie Sahlberg, David Turner

From: Ronnie Sahlberg <sahlberg@google.com>

Add ref backend methods for:
resolve_ref_unsafe, verify_refname_available, pack_refs, peel_ref,
create_symref, resolve_gitlink_ref.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twitter.com>
---
 builtin/init-db.c    |  1 +
 cache.h              |  7 +++++++
 refs.c               | 36 ++++++++++++++++++++++++++++++++++++
 refs/files-backend.c | 34 +++++++++++++++++++++++-----------
 refs/refs-internal.h | 23 +++++++++++++++++++++++
 5 files changed, 90 insertions(+), 11 deletions(-)

diff --git a/builtin/init-db.c b/builtin/init-db.c
index 07229d6..26e1cc3 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -8,6 +8,7 @@
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "parse-options.h"
+#include "refs.h"
 
 #ifndef DEFAULT_GIT_TEMPLATE_DIR
 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
diff --git a/cache.h b/cache.h
index 51c35c3..707455a 100644
--- a/cache.h
+++ b/cache.h
@@ -1111,6 +1111,13 @@ extern char *oid_to_hex(const struct object_id *oid);	/* same static buffer as s
 extern int interpret_branch_name(const char *str, int len, struct strbuf *);
 extern int get_sha1_mb(const char *str, unsigned char *sha1);
 
+/*
+ * Return true iff abbrev_name is a possible abbreviation for
+ * full_name according to the rules defined by ref_rev_parse_rules in
+ * refs.c.
+ */
+extern int refname_match(const char *abbrev_name, const char *full_name);
+
 extern int validate_headref(const char *ref);
 
 extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
diff --git a/refs.c b/refs.c
index babba8a..9562325 100644
--- a/refs.c
+++ b/refs.c
@@ -1114,3 +1114,39 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 {
 	return the_refs_backend->transaction_commit(transaction, err);
 }
+
+const char *resolve_ref_unsafe(const char *ref, int resolve_flags,
+			       unsigned char *sha1, int *flags)
+{
+	return the_refs_backend->resolve_ref_unsafe(ref, resolve_flags, sha1,
+						    flags);
+}
+
+int verify_refname_available(const char *refname, struct string_list *extra,
+			     struct string_list *skip, struct strbuf *err)
+{
+	return the_refs_backend->verify_refname_available(refname, extra, skip, err);
+}
+
+int pack_refs(unsigned int flags)
+{
+	return the_refs_backend->pack_refs(flags);
+}
+
+int peel_ref(const char *refname, unsigned char *sha1)
+{
+	return the_refs_backend->peel_ref(refname, sha1);
+}
+
+int create_symref(const char *ref_target, const char *refs_heads_master,
+		  const char *logmsg)
+{
+	return the_refs_backend->create_symref(ref_target, refs_heads_master,
+					       logmsg);
+}
+
+int resolve_gitlink_ref(const char *path, const char *refname,
+			unsigned char *sha1)
+{
+	return the_refs_backend->resolve_gitlink_ref(path, refname, sha1);
+}
diff --git a/refs/files-backend.c b/refs/files-backend.c
index be34772..25fba43 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1333,7 +1333,8 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
 	return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
 }
 
-int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
+static int files_resolve_gitlink_ref(const char *path, const char *refname,
+				     unsigned char *sha1)
 {
 	int len = strlen(path), retval;
 	char *submodule;
@@ -1565,8 +1566,10 @@ static const char *resolve_ref_1(const char *refname,
 	}
 }
 
-const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
-			       unsigned char *sha1, int *flags)
+static const char *files_resolve_ref_unsafe(const char *refname,
+					    int resolve_flags,
+					    unsigned char *sha1,
+					    int *flags)
 {
 	static struct strbuf sb_refname = STRBUF_INIT;
 	struct strbuf sb_contents = STRBUF_INIT;
@@ -1615,7 +1618,7 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
 	return status;
 }
 
-int peel_ref(const char *refname, unsigned char *sha1)
+static int files_peel_ref(const char *refname, unsigned char *sha1)
 {
 	int flag;
 	unsigned char base[20];
@@ -2246,7 +2249,7 @@ static void prune_refs(struct ref_to_prune *r)
 	}
 }
 
-int pack_refs(unsigned int flags)
+static int files_pack_refs(unsigned int flags)
 {
 	struct pack_refs_cb_data cbdata;
 
@@ -2437,10 +2440,10 @@ out:
 	return ret;
 }
 
-int verify_refname_available(const char *newname,
-			     struct string_list *extras,
-			     struct string_list *skip,
-			     struct strbuf *err)
+static int files_verify_refname_available(const char *newname,
+					  struct string_list *extras,
+					  struct string_list *skip,
+					  struct strbuf *err)
 {
 	struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
 	struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
@@ -2811,8 +2814,9 @@ static int commit_ref_update(struct ref_lock *lock,
 	return 0;
 }
 
-int create_symref(const char *ref_target, const char *refs_heads_master,
-		  const char *logmsg)
+static int files_create_symref(const char *ref_target,
+			       const char *refs_heads_master,
+			       const char *logmsg)
 {
 	char *lockpath = NULL;
 	char ref[1000];
@@ -3515,4 +3519,12 @@ struct ref_be refs_be_files = {
 	NULL,
 	"files",
 	files_transaction_commit,
+
+	files_pack_refs,
+	files_peel_ref,
+	files_create_symref,
+
+	files_resolve_ref_unsafe,
+	files_verify_refname_available,
+	files_resolve_gitlink_ref,
 };
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index f2c74f3..236bce9 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -201,10 +201,33 @@ int rename_ref_available(const char *oldname, const char *newname);
 typedef int ref_transaction_commit_fn(struct ref_transaction *transaction,
 				      struct strbuf *err);
 
+/* misc methods */
+typedef int pack_refs_fn(unsigned int flags);
+typedef int peel_ref_fn(const char *refname, unsigned char *sha1);
+typedef int create_symref_fn(const char *ref_target,
+			     const char *refs_heads_master,
+			     const char *logmsg);
+
+/* resolution methods */
+typedef const char *resolve_ref_unsafe_fn(const char *ref,
+					  int resolve_flags,
+					  unsigned char *sha1, int *flags);
+typedef int verify_refname_available_fn(const char *refname, struct string_list *extra, struct string_list *skip, struct strbuf *err);
+typedef int resolve_gitlink_ref_fn(const char *path, const char *refname,
+				   unsigned char *sha1);
+
 struct ref_be {
 	struct ref_be *next;
 	const char *name;
 	ref_transaction_commit_fn *transaction_commit;
+
+	pack_refs_fn *pack_refs;
+	peel_ref_fn *peel_ref;
+	create_symref_fn *create_symref;
+
+	resolve_ref_unsafe_fn *resolve_ref_unsafe;
+	verify_refname_available_fn *verify_refname_available;
+	resolve_gitlink_ref_fn *resolve_gitlink_ref;
 };
 
 #endif /* REFS_REFS_INTERNAL_H */
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 03/16] refs: add methods for the ref iterators
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
  2015-12-03  0:35 ` [PATCH 01/16] refs: add a backend method structure with transaction functions David Turner
  2015-12-03  0:35 ` [PATCH 02/16] refs: add methods for misc ref operations David Turner
@ 2015-12-03  0:35 ` David Turner
  2016-01-03  0:06   ` David Aguilar
  2015-12-03  0:35 ` [PATCH 04/16] refs: add do_for_each_per_worktree_ref David Turner
                   ` (13 subsequent siblings)
  16 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: Ronnie Sahlberg, David Turner

From: Ronnie Sahlberg <sahlberg@google.com>

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 refs/files-backend.c | 41 +++++++++++++++++++++++++++------------
 refs/refs-internal.h | 29 ++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+), 12 deletions(-)

diff --git a/refs.c b/refs.c
index 9562325..b9b0244 100644
--- a/refs.c
+++ b/refs.c
@@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname,
 {
 	return the_refs_backend->resolve_gitlink_ref(path, refname, sha1);
 }
+
+int head_ref(each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->head_ref(fn, cb_data);
+}
+
+int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->head_ref_submodule(submodule, fn, cb_data);
+}
+
+int for_each_ref(each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_ref(fn, cb_data);
+}
+
+int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_ref_submodule(submodule, fn, cb_data);
+}
+
+int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_ref_in(prefix, fn, cb_data);
+}
+
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data,
+			unsigned int broken)
+{
+	return the_refs_backend->for_each_fullref_in(prefix, fn, cb_data,
+						     broken);
+}
+
+int for_each_ref_in_submodule(const char *submodule, const char *prefix,
+			      each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_ref_in_submodule(submodule, prefix,
+							   fn, cb_data);
+}
+
+int for_each_rawref(each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_rawref(fn, cb_data);
+}
+
+int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_namespaced_ref(fn, cb_data);
+}
+
+int for_each_replace_ref(each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_replace_ref(fn, cb_data);
+}
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 25fba43..d4bd6cf 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1756,32 +1756,36 @@ static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
 	return 0;
 }
 
-int head_ref(each_ref_fn fn, void *cb_data)
+static int files_head_ref(each_ref_fn fn, void *cb_data)
 {
 	return do_head_ref(NULL, fn, cb_data);
 }
 
-int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+static int files_head_ref_submodule(const char *submodule, each_ref_fn fn,
+				    void *cb_data)
 {
 	return do_head_ref(submodule, fn, cb_data);
 }
 
-int for_each_ref(each_ref_fn fn, void *cb_data)
+static int files_for_each_ref(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data);
 }
 
-int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
+static int files_for_each_ref_submodule(const char *submodule, each_ref_fn fn,
+					void *cb_data)
 {
 	return do_for_each_ref(get_ref_cache(submodule), "", fn, 0, 0, cb_data);
 }
 
-int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
+static int files_for_each_ref_in(const char *prefix, each_ref_fn fn,
+				 void *cb_data)
 {
 	return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data);
 }
 
-int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
+static int files_for_each_fullref_in(const char *prefix, each_ref_fn fn,
+				     void *cb_data, unsigned int broken)
 {
 	unsigned int flag = 0;
 
@@ -1790,19 +1794,21 @@ int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsig
 	return do_for_each_ref(&ref_cache, prefix, fn, 0, flag, cb_data);
 }
 
-int for_each_ref_in_submodule(const char *submodule, const char *prefix,
-		each_ref_fn fn, void *cb_data)
+static int files_for_each_ref_in_submodule(const char *submodule,
+					   const char *prefix,
+					   each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(get_ref_cache(submodule), prefix, fn, strlen(prefix), 0, cb_data);
+	return do_for_each_ref(get_ref_cache(submodule), prefix, fn,
+			       strlen(prefix), 0, cb_data);
 }
 
-int for_each_replace_ref(each_ref_fn fn, void *cb_data)
+static int files_for_each_replace_ref(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_ref(&ref_cache, git_replace_ref_base, fn,
 			       strlen(git_replace_ref_base), 0, cb_data);
 }
 
-int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
+static int files_for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
 {
 	struct strbuf buf = STRBUF_INIT;
 	int ret;
@@ -1812,7 +1818,7 @@ int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
 	return ret;
 }
 
-int for_each_rawref(each_ref_fn fn, void *cb_data)
+static int files_for_each_rawref(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_ref(&ref_cache, "", fn, 0,
 			       DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
@@ -3527,4 +3533,15 @@ struct ref_be refs_be_files = {
 	files_resolve_ref_unsafe,
 	files_verify_refname_available,
 	files_resolve_gitlink_ref,
+
+	files_head_ref,
+	files_head_ref_submodule,
+	files_for_each_ref,
+	files_for_each_ref_submodule,
+	files_for_each_ref_in,
+	files_for_each_fullref_in,
+	files_for_each_ref_in_submodule,
+	files_for_each_rawref,
+	files_for_each_namespaced_ref,
+	files_for_each_replace_ref,
 };
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 236bce9..ad683df 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -216,6 +216,24 @@ typedef int verify_refname_available_fn(const char *refname, struct string_list
 typedef int resolve_gitlink_ref_fn(const char *path, const char *refname,
 				   unsigned char *sha1);
 
+/* iteration methods */
+typedef int head_ref_fn(each_ref_fn fn, void *cb_data);
+typedef int head_ref_submodule_fn(const char *submodule, each_ref_fn fn,
+				  void *cb_data);
+typedef int for_each_ref_fn(each_ref_fn fn, void *cb_data);
+typedef int for_each_ref_submodule_fn(const char *submodule, each_ref_fn fn,
+				      void *cb_data);
+typedef int for_each_ref_in_fn(const char *prefix, each_ref_fn fn,
+			       void *cb_data);
+typedef int for_each_fullref_in_fn(const char *prefix, each_ref_fn fn,
+				   void *cb_data, unsigned int broken);
+typedef int for_each_ref_in_submodule_fn(const char *submodule,
+					 const char *prefix,
+					 each_ref_fn fn, void *cb_data);
+typedef int for_each_rawref_fn(each_ref_fn fn, void *cb_data);
+typedef int for_each_namespaced_ref_fn(each_ref_fn fn, void *cb_data);
+typedef int for_each_replace_ref_fn(each_ref_fn fn, void *cb_data);
+
 struct ref_be {
 	struct ref_be *next;
 	const char *name;
@@ -228,6 +246,17 @@ struct ref_be {
 	resolve_ref_unsafe_fn *resolve_ref_unsafe;
 	verify_refname_available_fn *verify_refname_available;
 	resolve_gitlink_ref_fn *resolve_gitlink_ref;
+
+	head_ref_fn *head_ref;
+	head_ref_submodule_fn *head_ref_submodule;
+	for_each_ref_fn *for_each_ref;
+	for_each_ref_submodule_fn *for_each_ref_submodule;
+	for_each_ref_in_fn *for_each_ref_in;
+	for_each_fullref_in_fn *for_each_fullref_in;
+	for_each_ref_in_submodule_fn *for_each_ref_in_submodule;
+	for_each_rawref_fn *for_each_rawref;
+	for_each_namespaced_ref_fn *for_each_namespaced_ref;
+	for_each_replace_ref_fn *for_each_replace_ref;
 };
 
 #endif /* REFS_REFS_INTERNAL_H */
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 04/16] refs: add do_for_each_per_worktree_ref
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (2 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 03/16] refs: add methods for the ref iterators David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-11 23:52   ` Junio C Hamano
  2015-12-03  0:35 ` [PATCH 05/16] refs: add methods for reflog David Turner
                   ` (12 subsequent siblings)
  16 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Alternate refs backends might still use files to store per-worktree
refs.  So the files backend's ref-loading infrastructure should be
available to those backends, just for use on per-worktree refs.  Add
do_for_each_per_worktree_ref, which iterates over per-worktree refs.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs/files-backend.c | 15 ++++++++++++---
 refs/refs-internal.h | 10 ++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/refs/files-backend.c b/refs/files-backend.c
index d4bd6cf..bde4892 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -518,9 +518,6 @@ static void sort_ref_dir(struct ref_dir *dir)
 	dir->sorted = dir->nr = i;
 }
 
-/* Include broken references in a do_for_each_ref*() iteration: */
-#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
-
 /*
  * Return true iff the reference described by entry can be resolved to
  * an object in the database.  Emit a warning if the referred-to
@@ -568,6 +565,10 @@ static int do_one_ref(struct ref_entry *entry, void *cb_data)
 	struct ref_entry *old_current_ref;
 	int retval;
 
+	if (data->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
+	    ref_type(entry->name) != REF_TYPE_PER_WORKTREE)
+		return 0;
+
 	if (!starts_with(entry->name, data->base))
 		return 0;
 
@@ -1738,6 +1739,14 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,
 	return do_for_each_entry(refs, base, do_one_ref, &data);
 }
 
+int do_for_each_per_worktree_ref(const char *submodule, const char *base,
+				 each_ref_fn fn, int trim, int flags,
+				 void *cb_data)
+{
+	return do_for_each_ref(get_ref_cache(submodule), base, fn, trim,
+			       flags | DO_FOR_EACH_PER_WORKTREE_ONLY, cb_data);
+}
+
 static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
 {
 	struct object_id oid;
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index ad683df..433d0fe 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -42,6 +42,16 @@
  * value to ref_update::flags
  */
 
+/* Include broken references in a do_for_each_ref*() iteration */
+#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
+
+/* Only include per-worktree refs in a do_for_each_ref*() iteration */
+#define DO_FOR_EACH_PER_WORKTREE_ONLY 0x02
+
+int do_for_each_per_worktree_ref(const char *submodule, const char *base,
+				 each_ref_fn fn, int trim, int flags,
+				 void *cb_data);
+
 /*
  * Return true iff refname is minimally safe. "Safe" here means that
  * deleting a loose reference by this name will not do any damage, for
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 05/16] refs: add methods for reflog
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (3 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 04/16] refs: add do_for_each_per_worktree_ref David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-03  0:35 ` [PATCH 06/16] refs: add method for initial ref transaction commit David Turner
                   ` (11 subsequent siblings)
  16 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner, Ronnie Sahlberg

In the file-based backend, the reflog piggybacks on the ref lock.
Since other backends won't have the same sort of ref lock, ref backends
must also handle reflogs.

Signed-off-by: Ronnie Sahlberg <rsahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 refs/files-backend.c | 36 ++++++++++++++++++++++++------------
 refs/refs-internal.h | 27 +++++++++++++++++++++++++++
 3 files changed, 97 insertions(+), 12 deletions(-)

diff --git a/refs.c b/refs.c
index b9b0244..a8ed77d 100644
--- a/refs.c
+++ b/refs.c
@@ -1204,3 +1204,49 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data)
 {
 	return the_refs_backend->for_each_replace_ref(fn, cb_data);
 }
+
+int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
+				void *cb_data)
+{
+	return the_refs_backend->for_each_reflog_ent_reverse(refname, fn,
+							     cb_data);
+}
+
+int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
+			void *cb_data)
+{
+	return the_refs_backend->for_each_reflog_ent(refname, fn, cb_data);
+}
+
+int for_each_reflog(each_ref_fn fn, void *cb_data)
+{
+	return the_refs_backend->for_each_reflog(fn, cb_data);
+}
+
+int reflog_exists(const char *refname)
+{
+	return the_refs_backend->reflog_exists(refname);
+}
+
+int safe_create_reflog(const char *refname, int force_create,
+		       struct strbuf *err)
+{
+	return the_refs_backend->create_reflog(refname, force_create, err);
+}
+
+int delete_reflog(const char *refname)
+{
+	return the_refs_backend->delete_reflog(refname);
+}
+
+int reflog_expire(const char *refname, const unsigned char *sha1,
+		  unsigned int flags,
+		  reflog_expiry_prepare_fn prepare_fn,
+		  reflog_expiry_should_prune_fn should_prune_fn,
+		  reflog_expiry_cleanup_fn cleanup_fn,
+		  void *policy_cb_data)
+{
+	return the_refs_backend->reflog_expire(refname, sha1, flags,
+					       prepare_fn, should_prune_fn,
+					       cleanup_fn, policy_cb_data);
+}
diff --git a/refs/files-backend.c b/refs/files-backend.c
index bde4892..1f76e34 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2643,7 +2643,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
 }
 
 
-int safe_create_reflog(const char *refname, int force_create, struct strbuf *err)
+static int files_create_reflog(const char *refname, int force_create,
+			       struct strbuf *err)
 {
 	int ret;
 	struct strbuf sb = STRBUF_INIT;
@@ -2899,7 +2900,7 @@ static int files_create_symref(const char *ref_target,
 	return 0;
 }
 
-int reflog_exists(const char *refname)
+static int files_reflog_exists(const char *refname)
 {
 	struct stat st;
 
@@ -2907,7 +2908,7 @@ int reflog_exists(const char *refname)
 		S_ISREG(st.st_mode);
 }
 
-int delete_reflog(const char *refname)
+static int files_delete_reflog(const char *refname)
 {
 	return remove_path(git_path("logs/%s", refname));
 }
@@ -2951,7 +2952,9 @@ static char *find_beginning_of_line(char *bob, char *scan)
 	return scan;
 }
 
-int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data)
+static int files_for_each_reflog_ent_reverse(const char *refname,
+					     each_reflog_ent_fn fn,
+					     void *cb_data)
 {
 	struct strbuf sb = STRBUF_INIT;
 	FILE *logfp;
@@ -3053,7 +3056,8 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void
 	return ret;
 }
 
-int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)
+static int files_for_each_reflog_ent(const char *refname,
+				     each_reflog_ent_fn fn, void *cb_data)
 {
 	FILE *logfp;
 	struct strbuf sb = STRBUF_INIT;
@@ -3115,7 +3119,7 @@ static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data
 	return retval;
 }
 
-int for_each_reflog(each_ref_fn fn, void *cb_data)
+static int files_for_each_reflog(each_ref_fn fn, void *cb_data)
 {
 	int retval;
 	struct strbuf name;
@@ -3425,12 +3429,12 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 	return 0;
 }
 
-int reflog_expire(const char *refname, const unsigned char *sha1,
-		 unsigned int flags,
-		 reflog_expiry_prepare_fn prepare_fn,
-		 reflog_expiry_should_prune_fn should_prune_fn,
-		 reflog_expiry_cleanup_fn cleanup_fn,
-		 void *policy_cb_data)
+static int files_reflog_expire(const char *refname, const unsigned char *sha1,
+			       unsigned int flags,
+			       reflog_expiry_prepare_fn prepare_fn,
+			       reflog_expiry_should_prune_fn should_prune_fn,
+			       reflog_expiry_cleanup_fn cleanup_fn,
+			       void *policy_cb_data)
 {
 	static struct lock_file reflog_lock;
 	struct expire_reflog_cb cb;
@@ -3535,6 +3539,14 @@ struct ref_be refs_be_files = {
 	"files",
 	files_transaction_commit,
 
+	files_for_each_reflog_ent,
+	files_for_each_reflog_ent_reverse,
+	files_for_each_reflog,
+	files_reflog_exists,
+	files_create_reflog,
+	files_delete_reflog,
+	files_reflog_expire,
+
 	files_pack_refs,
 	files_peel_ref,
 	files_create_symref,
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 433d0fe..798dee9 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -211,6 +211,25 @@ int rename_ref_available(const char *oldname, const char *newname);
 typedef int ref_transaction_commit_fn(struct ref_transaction *transaction,
 				      struct strbuf *err);
 
+/* reflog functions */
+typedef int for_each_reflog_ent_fn(const char *refname,
+				   each_reflog_ent_fn fn,
+				   void *cb_data);
+typedef int for_each_reflog_ent_reverse_fn(const char *refname,
+					   each_reflog_ent_fn fn,
+					   void *cb_data);
+typedef int for_each_reflog_fn(each_ref_fn fn, void *cb_data);
+typedef int reflog_exists_fn(const char *refname);
+typedef int create_reflog_fn(const char *refname, int force_create,
+			     struct strbuf *err);
+typedef int delete_reflog_fn(const char *refname);
+typedef int reflog_expire_fn(const char *refname, const unsigned char *sha1,
+			     unsigned int flags,
+			     reflog_expiry_prepare_fn prepare_fn,
+			     reflog_expiry_should_prune_fn should_prune_fn,
+			     reflog_expiry_cleanup_fn cleanup_fn,
+			     void *policy_cb_data);
+
 /* misc methods */
 typedef int pack_refs_fn(unsigned int flags);
 typedef int peel_ref_fn(const char *refname, unsigned char *sha1);
@@ -249,6 +268,14 @@ struct ref_be {
 	const char *name;
 	ref_transaction_commit_fn *transaction_commit;
 
+	for_each_reflog_ent_fn *for_each_reflog_ent;
+	for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse;
+	for_each_reflog_fn *for_each_reflog;
+	reflog_exists_fn *reflog_exists;
+	create_reflog_fn *create_reflog;
+	delete_reflog_fn *delete_reflog;
+	reflog_expire_fn *reflog_expire;
+
 	pack_refs_fn *pack_refs;
 	peel_ref_fn *peel_ref;
 	create_symref_fn *create_symref;
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 06/16] refs: add method for initial ref transaction commit
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (4 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 05/16] refs: add methods for reflog David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-03  0:35 ` [PATCH 07/16] refs: add method for delete_refs David Turner
                   ` (10 subsequent siblings)
  16 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner, Ronnie Sahlberg

Signed-off-by: Ronnie Sahlberg <rsahlberg@google.com>
Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 6 ++++++
 refs/files-backend.c | 5 +++--
 refs/refs-internal.h | 1 +
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/refs.c b/refs.c
index a8ed77d..e50516c 100644
--- a/refs.c
+++ b/refs.c
@@ -1250,3 +1250,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
 					       prepare_fn, should_prune_fn,
 					       cleanup_fn, policy_cb_data);
 }
+
+int initial_ref_transaction_commit(struct ref_transaction *transaction,
+				   struct strbuf *err)
+{
+	return the_refs_backend->initial_transaction_commit(transaction, err);
+}
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 1f76e34..44ad632 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3313,8 +3313,8 @@ static int ref_present(const char *refname,
 	return string_list_has_string(affected_refnames, refname);
 }
 
-int initial_ref_transaction_commit(struct ref_transaction *transaction,
-				   struct strbuf *err)
+static int files_initial_transaction_commit(struct ref_transaction *transaction,
+					    struct strbuf *err)
 {
 	int ret = 0, i;
 	int n = transaction->nr;
@@ -3538,6 +3538,7 @@ struct ref_be refs_be_files = {
 	NULL,
 	"files",
 	files_transaction_commit,
+	files_initial_transaction_commit,
 
 	files_for_each_reflog_ent,
 	files_for_each_reflog_ent_reverse,
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 798dee9..74bd44b 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -267,6 +267,7 @@ struct ref_be {
 	struct ref_be *next;
 	const char *name;
 	ref_transaction_commit_fn *transaction_commit;
+	ref_transaction_commit_fn *initial_transaction_commit;
 
 	for_each_reflog_ent_fn *for_each_reflog_ent;
 	for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse;
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 07/16] refs: add method for delete_refs
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (5 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 06/16] refs: add method for initial ref transaction commit David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-03  0:35 ` [PATCH 08/16] refs: add methods to init refs backend and db David Turner
                   ` (9 subsequent siblings)
  16 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

In the file-based backend, delete_refs has some special optimization
to deal with packed refs.  In other backends, we might be able to make
ref deletion faster by putting all deletions into a single
transaction.  So we need a special backend function for this.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 5 +++++
 refs/files-backend.c | 3 ++-
 refs/refs-internal.h | 2 ++
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/refs.c b/refs.c
index e50516c..9a2fed7 100644
--- a/refs.c
+++ b/refs.c
@@ -1115,6 +1115,11 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	return the_refs_backend->transaction_commit(transaction, err);
 }
 
+int delete_refs(struct string_list *refnames)
+{
+	return the_refs_backend->delete_refs(refnames);
+}
+
 const char *resolve_ref_unsafe(const char *ref, int resolve_flags,
 			       unsigned char *sha1, int *flags)
 {
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 44ad632..e769242 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2356,7 +2356,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
 	return 0;
 }
 
-int delete_refs(struct string_list *refnames)
+static int files_delete_refs(struct string_list *refnames)
 {
 	struct strbuf err = STRBUF_INIT;
 	int i, result = 0;
@@ -3551,6 +3551,7 @@ struct ref_be refs_be_files = {
 	files_pack_refs,
 	files_peel_ref,
 	files_create_symref,
+	files_delete_refs,
 
 	files_resolve_ref_unsafe,
 	files_verify_refname_available,
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 74bd44b..478ad54 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -236,6 +236,7 @@ typedef int peel_ref_fn(const char *refname, unsigned char *sha1);
 typedef int create_symref_fn(const char *ref_target,
 			     const char *refs_heads_master,
 			     const char *logmsg);
+typedef int delete_refs_fn(struct string_list *refnames);
 
 /* resolution methods */
 typedef const char *resolve_ref_unsafe_fn(const char *ref,
@@ -280,6 +281,7 @@ struct ref_be {
 	pack_refs_fn *pack_refs;
 	peel_ref_fn *peel_ref;
 	create_symref_fn *create_symref;
+	delete_refs_fn *delete_refs;
 
 	resolve_ref_unsafe_fn *resolve_ref_unsafe;
 	verify_refname_available_fn *verify_refname_available;
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 08/16] refs: add methods to init refs backend and db
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (6 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 07/16] refs: add method for delete_refs David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-23  5:33   ` Michael Haggerty
  2015-12-03  0:35 ` [PATCH 09/16] refs: add method to rename refs David Turner
                   ` (8 subsequent siblings)
  16 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Alternate refs backends might not need the refs/heads directory and so
on, so we make ref db initialization part of the backend.  We also
might need to initialize ref backends themselves, so we'll add a
method for that as well.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 builtin/init-db.c    | 14 ++++----------
 refs.c               |  8 +++++++-
 refs.h               |  4 +++-
 refs/files-backend.c | 23 +++++++++++++++++++++++
 refs/refs-internal.h |  4 ++++
 5 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/builtin/init-db.c b/builtin/init-db.c
index 26e1cc3..4771e7e 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -178,13 +178,7 @@ static int create_default_files(const char *template_path)
 	char junk[2];
 	int reinit;
 	int filemode;
-
-	/*
-	 * Create .git/refs/{heads,tags}
-	 */
-	safe_create_dir(git_path_buf(&buf, "refs"), 1);
-	safe_create_dir(git_path_buf(&buf, "refs/heads"), 1);
-	safe_create_dir(git_path_buf(&buf, "refs/tags"), 1);
+	struct strbuf err = STRBUF_INIT;
 
 	/* Just look for `init.templatedir` */
 	git_config(git_init_db_config, NULL);
@@ -208,11 +202,11 @@ static int create_default_files(const char *template_path)
 	 */
 	if (shared_repository) {
 		adjust_shared_perm(get_git_dir());
-		adjust_shared_perm(git_path_buf(&buf, "refs"));
-		adjust_shared_perm(git_path_buf(&buf, "refs/heads"));
-		adjust_shared_perm(git_path_buf(&buf, "refs/tags"));
 	}
 
+	if (refs_init_db(&err, shared_repository))
+		die("failed to set up refs db: %s", err.buf);
+
 	/*
 	 * Create the default symlink from ".git/HEAD" to the "master"
 	 * branch, if it does not exist yet.
diff --git a/refs.c b/refs.c
index 9a2fed7..bdeb276 100644
--- a/refs.c
+++ b/refs.c
@@ -22,13 +22,14 @@ struct ref_be *refs_backends = &refs_be_files;
 /*
  * This function is used to switch to an alternate backend.
  */
-int set_refs_backend(const char *name)
+int set_refs_backend(const char *name, void *data)
 {
 	struct ref_be *be;
 
 	for (be = refs_backends; be; be = be->next)
 		if (!strcmp(be->name, name)) {
 			the_refs_backend = be;
+			be->init_backend(data);
 			return 0;
 		}
 	return 1;
@@ -1109,6 +1110,11 @@ int rename_ref_available(const char *oldname, const char *newname)
 }
 
 /* backend functions */
+int refs_init_db(struct strbuf *err, int shared)
+{
+	return the_refs_backend->init_db(err, shared);
+}
+
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
diff --git a/refs.h b/refs.h
index 4e5477d..c211b9e 100644
--- a/refs.h
+++ b/refs.h
@@ -66,6 +66,8 @@ extern int ref_exists(const char *refname);
 
 extern int is_branch(const char *refname);
 
+extern int refs_init_db(struct strbuf *err, int shared);
+
 /*
  * If refname is a non-symbolic reference that refers to a tag object,
  * and the tag can be (recursively) dereferenced to a non-tag object,
@@ -508,6 +510,6 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,
 			 reflog_expiry_cleanup_fn cleanup_fn,
 			 void *policy_cb_data);
 
-int set_refs_backend(const char *name);
+int set_refs_backend(const char *name, void *data);
 
 #endif /* REFS_H */
diff --git a/refs/files-backend.c b/refs/files-backend.c
index e769242..6600c02 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3313,6 +3313,11 @@ static int ref_present(const char *refname,
 	return string_list_has_string(affected_refnames, refname);
 }
 
+void files_init_backend(void *data)
+{
+	/* do nothing */
+}
+
 static int files_initial_transaction_commit(struct ref_transaction *transaction,
 					    struct strbuf *err)
 {
@@ -3534,9 +3539,27 @@ static int files_reflog_expire(const char *refname, const unsigned char *sha1,
 	return -1;
 }
 
+static int files_init_db(struct strbuf *err, int shared)
+{
+	/*
+	 * Create .git/refs/{heads,tags}
+	 */
+	safe_create_dir(git_path("refs"), 1);
+	safe_create_dir(git_path("refs/heads"), 1);
+	safe_create_dir(git_path("refs/tags"), 1);
+	if (shared) {
+		adjust_shared_perm(git_path("refs"));
+		adjust_shared_perm(git_path("refs/heads"));
+		adjust_shared_perm(git_path("refs/tags"));
+	}
+	return 0;
+}
+
 struct ref_be refs_be_files = {
 	NULL,
 	"files",
+	files_init_backend,
+	files_init_db,
 	files_transaction_commit,
 	files_initial_transaction_commit,
 
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 478ad54..85a0b91 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -208,6 +208,8 @@ const char *find_descendant_ref(const char *dirname,
 int rename_ref_available(const char *oldname, const char *newname);
 
 /* refs backends */
+typedef void ref_backend_init_fn(void *data);
+typedef int ref_backend_init_db_fn(struct strbuf *err, int shared);
 typedef int ref_transaction_commit_fn(struct ref_transaction *transaction,
 				      struct strbuf *err);
 
@@ -267,6 +269,8 @@ typedef int for_each_replace_ref_fn(each_ref_fn fn, void *cb_data);
 struct ref_be {
 	struct ref_be *next;
 	const char *name;
+	ref_backend_init_fn *init_backend;
+	ref_backend_init_db_fn *init_db;
 	ref_transaction_commit_fn *transaction_commit;
 	ref_transaction_commit_fn *initial_transaction_commit;
 
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 09/16] refs: add method to rename refs
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (7 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 08/16] refs: add methods to init refs backend and db David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-03  0:35 ` [PATCH 10/16] refs: make lock generic David Turner
                   ` (7 subsequent siblings)
  16 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 37 +++++++++++++++++++++----------------
 refs/files-backend.c |  4 +++-
 refs/refs-internal.h |  9 +++++++++
 3 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/refs.c b/refs.c
index bdeb276..1b79630 100644
--- a/refs.c
+++ b/refs.c
@@ -1093,22 +1093,6 @@ const char *find_descendant_ref(const char *dirname,
 	return NULL;
 }
 
-int rename_ref_available(const char *oldname, const char *newname)
-{
-	struct string_list skip = STRING_LIST_INIT_NODUP;
-	struct strbuf err = STRBUF_INIT;
-	int ret;
-
-	string_list_insert(&skip, oldname);
-	ret = !verify_refname_available(newname, NULL, &skip, &err);
-	if (!ret)
-		error("%s", err.buf);
-
-	string_list_clear(&skip, 0);
-	strbuf_release(&err);
-	return ret;
-}
-
 /* backend functions */
 int refs_init_db(struct strbuf *err, int shared)
 {
@@ -1126,6 +1110,11 @@ int delete_refs(struct string_list *refnames)
 	return the_refs_backend->delete_refs(refnames);
 }
 
+int rename_ref(const char *oldref, const char *newref, const char *logmsg)
+{
+	return the_refs_backend->rename_ref(oldref, newref, logmsg);
+}
+
 const char *resolve_ref_unsafe(const char *ref, int resolve_flags,
 			       unsigned char *sha1, int *flags)
 {
@@ -1139,6 +1128,22 @@ int verify_refname_available(const char *refname, struct string_list *extra,
 	return the_refs_backend->verify_refname_available(refname, extra, skip, err);
 }
 
+int rename_ref_available(const char *oldname, const char *newname)
+{
+	struct string_list skip = STRING_LIST_INIT_NODUP;
+	struct strbuf err = STRBUF_INIT;
+	int ret;
+
+	string_list_insert(&skip, oldname);
+	ret = !verify_refname_available(newname, NULL, &skip, &err);
+	if (!ret)
+		error("%s", err.buf);
+
+	string_list_clear(&skip, 0);
+	strbuf_release(&err);
+	return ret;
+}
+
 int pack_refs(unsigned int flags)
 {
 	return the_refs_backend->pack_refs(flags);
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 6600c02..0af0818 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2478,7 +2478,8 @@ static int commit_ref_update(struct ref_lock *lock,
 			     const unsigned char *sha1, const char *logmsg,
 			     int flags, struct strbuf *err);
 
-int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
+static int files_rename_ref(const char *oldrefname, const char *newrefname,
+			    const char *logmsg)
 {
 	unsigned char sha1[20], orig_sha1[20];
 	int flag = 0, logmoved = 0;
@@ -3575,6 +3576,7 @@ struct ref_be refs_be_files = {
 	files_peel_ref,
 	files_create_symref,
 	files_delete_refs,
+	files_rename_ref,
 
 	files_resolve_ref_unsafe,
 	files_verify_refname_available,
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 85a0b91..36a024f 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -53,6 +53,13 @@ int do_for_each_per_worktree_ref(const char *submodule, const char *base,
 				 void *cb_data);
 
 /*
+ * Check if the new name does not conflict with any existing refs
+ * (other than possibly the old ref).  Return 0 if the ref can be
+ * renamed to the new name.
+ */
+int rename_ref_available(const char *oldname, const char *newname);
+
+/*
  * Return true iff refname is minimally safe. "Safe" here means that
  * deleting a loose reference by this name will not do any damage, for
  * example by causing a file that is not a reference to be deleted.
@@ -247,6 +254,7 @@ typedef const char *resolve_ref_unsafe_fn(const char *ref,
 typedef int verify_refname_available_fn(const char *refname, struct string_list *extra, struct string_list *skip, struct strbuf *err);
 typedef int resolve_gitlink_ref_fn(const char *path, const char *refname,
 				   unsigned char *sha1);
+typedef int rename_ref_fn(const char *oldref, const char *newref, const char *logmsg);
 
 /* iteration methods */
 typedef int head_ref_fn(each_ref_fn fn, void *cb_data);
@@ -286,6 +294,7 @@ struct ref_be {
 	peel_ref_fn *peel_ref;
 	create_symref_fn *create_symref;
 	delete_refs_fn *delete_refs;
+	rename_ref_fn *rename_ref;
 
 	resolve_ref_unsafe_fn *resolve_ref_unsafe;
 	verify_refname_available_fn *verify_refname_available;
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 10/16] refs: make lock generic
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (8 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 09/16] refs: add method to rename refs David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-03  0:35 ` [PATCH 11/16] refs: move duplicate check to common code David Turner
                   ` (6 subsequent siblings)
  16 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Instead of using a files-backend-specific struct ref_lock, the generic
ref_transaction struct should provide a void pointer that backends can use
for their own lock data.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs/files-backend.c | 29 ++++++++++++++++-------------
 refs/refs-internal.h |  2 +-
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/refs/files-backend.c b/refs/files-backend.c
index 0af0818..22a6f24 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3184,11 +3184,12 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 	 */
 	for (i = 0; i < n; i++) {
 		struct ref_update *update = updates[i];
+		struct ref_lock *lock;
 
 		if ((update->flags & REF_HAVE_NEW) &&
 		    is_null_sha1(update->new_sha1))
 			update->flags |= REF_DELETING;
-		update->lock = lock_ref_sha1_basic(
+		lock = lock_ref_sha1_basic(
 				update->refname,
 				((update->flags & REF_HAVE_OLD) ?
 				 update->old_sha1 : NULL),
@@ -3196,7 +3197,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 				update->flags,
 				&update->type,
 				err);
-		if (!update->lock) {
+		update->backend_data = lock;
+		if (!lock) {
 			char *reason;
 
 			ret = (errno == ENOTDIR)
@@ -3214,12 +3216,12 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 						  (update->flags & REF_NODEREF));
 
 			if (!overwriting_symref &&
-			    !hashcmp(update->lock->old_oid.hash, update->new_sha1)) {
+			    !hashcmp(lock->old_oid.hash, update->new_sha1)) {
 				/*
 				 * The reference already has the desired
 				 * value, so we don't need to write it.
 				 */
-			} else if (write_ref_to_lockfile(update->lock,
+			} else if (write_ref_to_lockfile(lock,
 							 update->new_sha1,
 							 err)) {
 				char *write_err = strbuf_detach(err, NULL);
@@ -3228,7 +3230,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 				 * The lock was freed upon failure of
 				 * write_ref_to_lockfile():
 				 */
-				update->lock = NULL;
+				update->backend_data = NULL;
 				strbuf_addf(err,
 					    "cannot update the ref '%s': %s",
 					    update->refname, write_err);
@@ -3244,7 +3246,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 			 * We didn't have to write anything to the lockfile.
 			 * Close it to free up the file descriptor:
 			 */
-			if (close_ref(update->lock)) {
+			if (close_ref(lock)) {
 				strbuf_addf(err, "Couldn't close %s.lock",
 					    update->refname);
 				goto cleanup;
@@ -3257,16 +3259,16 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 		struct ref_update *update = updates[i];
 
 		if (update->flags & REF_NEEDS_COMMIT) {
-			if (commit_ref_update(update->lock,
+			if (commit_ref_update(update->backend_data,
 					      update->new_sha1, update->msg,
 					      update->flags, err)) {
 				/* freed by commit_ref_update(): */
-				update->lock = NULL;
+				update->backend_data = NULL;
 				ret = TRANSACTION_GENERIC_ERROR;
 				goto cleanup;
 			} else {
 				/* freed by commit_ref_update(): */
-				update->lock = NULL;
+				update->backend_data = NULL;
 			}
 		}
 	}
@@ -3274,16 +3276,17 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 	/* Perform deletes now that updates are safely completed */
 	for (i = 0; i < n; i++) {
 		struct ref_update *update = updates[i];
+		struct ref_lock *lock = update->backend_data;
 
 		if (update->flags & REF_DELETING) {
-			if (delete_ref_loose(update->lock, update->type, err)) {
+			if (delete_ref_loose(lock, update->type, err)) {
 				ret = TRANSACTION_GENERIC_ERROR;
 				goto cleanup;
 			}
 
 			if (!(update->flags & REF_ISPRUNING))
 				string_list_append(&refs_to_delete,
-						   update->lock->ref_name);
+						   lock->ref_name);
 		}
 	}
 
@@ -3299,8 +3302,8 @@ cleanup:
 	transaction->state = REF_TRANSACTION_CLOSED;
 
 	for (i = 0; i < n; i++)
-		if (updates[i]->lock)
-			unlock_ref(updates[i]->lock);
+		if (updates[i]->backend_data)
+			unlock_ref(updates[i]->backend_data);
 	string_list_clear(&refs_to_delete, 0);
 	string_list_clear(&affected_refnames, 0);
 	return ret;
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 36a024f..8322011 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -161,7 +161,7 @@ struct ref_update {
 	 * REF_DELETING, and REF_ISPRUNING:
 	 */
 	unsigned int flags;
-	struct ref_lock *lock;
+	void *backend_data;
 	int type;
 	char *msg;
 	const char refname[FLEX_ARRAY];
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 11/16] refs: move duplicate check to common code
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (9 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 10/16] refs: make lock generic David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-23  6:27   ` Michael Haggerty
  2015-12-03  0:35 ` [PATCH 12/16] refs: always handle non-normal refs in files backend David Turner
                   ` (5 subsequent siblings)
  16 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

The check for duplicate refnames in a transaction is needed for
all backends, so move it to the common code.

ref_transaction_commit_fn gains a new argument, the sorted
string_list of affected refnames.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 refs/files-backend.c | 57 ++++-------------------------------------
 refs/refs-internal.h |  1 +
 3 files changed, 75 insertions(+), 54 deletions(-)

diff --git a/refs.c b/refs.c
index 1b79630..808053f 100644
--- a/refs.c
+++ b/refs.c
@@ -1093,6 +1093,37 @@ const char *find_descendant_ref(const char *dirname,
 	return NULL;
 }
 
+/*
+ * Return 1 if there are any duplicate refnames in the updates in
+ * `transaction`, and fill in err with an appropriate error message.
+ * Fill in `refnames` with the refnames from the transaction.
+ */
+
+static int ref_update_reject_duplicates(struct ref_transaction *transaction,
+					struct string_list *refnames,
+					struct strbuf *err)
+{
+	int i, n = transaction->nr;
+	struct ref_update **updates;
+
+	assert(err);
+
+	updates = transaction->updates;
+	/* Fail if a refname appears more than once in the transaction: */
+	for (i = 0; i < n; i++)
+		string_list_append(refnames, updates[i]->refname);
+	string_list_sort(refnames);
+
+	for (i = 1; i < n; i++)
+		if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
+			strbuf_addf(err,
+				    "Multiple updates for ref '%s' not allowed.",
+				    refnames->items[i].string);
+			return 1;
+		}
+	return 0;
+}
+
 /* backend functions */
 int refs_init_db(struct strbuf *err, int shared)
 {
@@ -1102,7 +1133,29 @@ int refs_init_db(struct strbuf *err, int shared)
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
-	return the_refs_backend->transaction_commit(transaction, err);
+	int ret = -1;
+	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+
+	assert(err);
+
+	if (transaction->state != REF_TRANSACTION_OPEN)
+		die("BUG: commit called for transaction that is not open");
+
+	if (!transaction->nr) {
+		transaction->state = REF_TRANSACTION_CLOSED;
+		return 0;
+	}
+
+	if (ref_update_reject_duplicates(transaction, &affected_refnames, err)) {
+		ret = TRANSACTION_GENERIC_ERROR;
+		goto done;
+	}
+
+	ret = the_refs_backend->transaction_commit(transaction,
+						   &affected_refnames, err);
+done:
+	string_list_clear(&affected_refnames, 0);
+	return ret;
 }
 
 int delete_refs(struct string_list *refnames)
@@ -1270,5 +1323,19 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
 int initial_ref_transaction_commit(struct ref_transaction *transaction,
 				   struct strbuf *err)
 {
-	return the_refs_backend->initial_transaction_commit(transaction, err);
+
+	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+	int ret;
+
+	if (ref_update_reject_duplicates(transaction,
+					 &affected_refnames, err)) {
+		ret = TRANSACTION_GENERIC_ERROR;
+		goto done;
+	}
+	ret = the_refs_backend->initial_transaction_commit(transaction,
+							   &affected_refnames,
+							   err);
+done:
+	string_list_clear(&affected_refnames, 0);
+	return ret;
 }
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 22a6f24..59e2ec1 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3130,24 +3130,8 @@ static int files_for_each_reflog(each_ref_fn fn, void *cb_data)
 	return retval;
 }
 
-static int ref_update_reject_duplicates(struct string_list *refnames,
-					struct strbuf *err)
-{
-	int i, n = refnames->nr;
-
-	assert(err);
-
-	for (i = 1; i < n; i++)
-		if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
-			strbuf_addf(err,
-				    "Multiple updates for ref '%s' not allowed.",
-				    refnames->items[i].string);
-			return 1;
-		}
-	return 0;
-}
-
 static int files_transaction_commit(struct ref_transaction *transaction,
+				    struct string_list *affected_refnames,
 				    struct strbuf *err)
 {
 	int ret = 0, i;
@@ -3155,26 +3139,6 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 	struct ref_update **updates = transaction->updates;
 	struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
 	struct string_list_item *ref_to_delete;
-	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
-
-	assert(err);
-
-	if (transaction->state != REF_TRANSACTION_OPEN)
-		die("BUG: commit called for transaction that is not open");
-
-	if (!n) {
-		transaction->state = REF_TRANSACTION_CLOSED;
-		return 0;
-	}
-
-	/* Fail if a refname appears more than once in the transaction: */
-	for (i = 0; i < n; i++)
-		string_list_append(&affected_refnames, updates[i]->refname);
-	string_list_sort(&affected_refnames);
-	if (ref_update_reject_duplicates(&affected_refnames, err)) {
-		ret = TRANSACTION_GENERIC_ERROR;
-		goto cleanup;
-	}
 
 	/*
 	 * Acquire all locks, verify old values if provided, check
@@ -3193,7 +3157,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 				update->refname,
 				((update->flags & REF_HAVE_OLD) ?
 				 update->old_sha1 : NULL),
-				&affected_refnames, NULL,
+				affected_refnames, NULL,
 				update->flags,
 				&update->type,
 				err);
@@ -3305,7 +3269,6 @@ cleanup:
 		if (updates[i]->backend_data)
 			unlock_ref(updates[i]->backend_data);
 	string_list_clear(&refs_to_delete, 0);
-	string_list_clear(&affected_refnames, 0);
 	return ret;
 }
 
@@ -3323,27 +3286,18 @@ void files_init_backend(void *data)
 }
 
 static int files_initial_transaction_commit(struct ref_transaction *transaction,
+					    struct string_list *affected_refnames,
 					    struct strbuf *err)
 {
 	int ret = 0, i;
 	int n = transaction->nr;
 	struct ref_update **updates = transaction->updates;
-	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
 
 	assert(err);
 
 	if (transaction->state != REF_TRANSACTION_OPEN)
 		die("BUG: commit called for transaction that is not open");
 
-	/* Fail if a refname appears more than once in the transaction: */
-	for (i = 0; i < n; i++)
-		string_list_append(&affected_refnames, updates[i]->refname);
-	string_list_sort(&affected_refnames);
-	if (ref_update_reject_duplicates(&affected_refnames, err)) {
-		ret = TRANSACTION_GENERIC_ERROR;
-		goto cleanup;
-	}
-
 	/*
 	 * It's really undefined to call this function in an active
 	 * repository or when there are existing references: we are
@@ -3356,7 +3310,7 @@ static int files_initial_transaction_commit(struct ref_transaction *transaction,
 	 * so here we really only check that none of the references
 	 * that we are creating already exists.
 	 */
-	if (for_each_rawref(ref_present, &affected_refnames))
+	if (for_each_rawref(ref_present, affected_refnames))
 		die("BUG: initial ref transaction called with existing refs");
 
 	for (i = 0; i < n; i++) {
@@ -3366,7 +3320,7 @@ static int files_initial_transaction_commit(struct ref_transaction *transaction,
 		    !is_null_sha1(update->old_sha1))
 			die("BUG: initial ref transaction with old_sha1 set");
 		if (verify_refname_available(update->refname,
-					     &affected_refnames, NULL,
+					     affected_refnames, NULL,
 					     err)) {
 			ret = TRANSACTION_NAME_CONFLICT;
 			goto cleanup;
@@ -3397,7 +3351,6 @@ static int files_initial_transaction_commit(struct ref_transaction *transaction,
 
 cleanup:
 	transaction->state = REF_TRANSACTION_CLOSED;
-	string_list_clear(&affected_refnames, 0);
 	return ret;
 }
 
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 8322011..9c17fdf 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -218,6 +218,7 @@ int rename_ref_available(const char *oldname, const char *newname);
 typedef void ref_backend_init_fn(void *data);
 typedef int ref_backend_init_db_fn(struct strbuf *err, int shared);
 typedef int ref_transaction_commit_fn(struct ref_transaction *transaction,
+				      struct string_list *affected_refnames,
 				      struct strbuf *err);
 
 /* reflog functions */
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 12/16] refs: always handle non-normal refs in files backend
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (10 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 11/16] refs: move duplicate check to common code David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-23  8:02   ` Michael Haggerty
  2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
                   ` (4 subsequent siblings)
  16 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Always handle non-normal (per-worktree or pseudo) refs in the files
backend instead of alternate backends.

Sometimes a ref transaction will update both a per-worktree ref and a
normal ref.  For instance, an ordinary commit might update
refs/heads/master and HEAD (or at least HEAD's reflog).

We handle three cases here:

1. updates to normal refs continue to go through the chosen backend

2. updates to non-normal refs with REF_NODEREF or to non-symbolic refs
are moved to a separate files backend transaction.

3. updates to symbolic refs are dereferenced to their base ref.  The
update to the base ref then goes through the ordinary backend, while
the files backend is directly called to update the symref's reflog.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 139 insertions(+), 2 deletions(-)

diff --git a/refs.c b/refs.c
index 808053f..e48e43a 100644
--- a/refs.c
+++ b/refs.c
@@ -9,6 +9,11 @@
 #include "object.h"
 #include "tag.h"
 
+const char split_transaction_fail_warning[] =
+	"A ref transaction was split across two refs backends.  Part of the "
+	"transaction succeeded, but then the update to the per-worktree refs "
+	"failed.  Your repository may be in an inconsistent state.";
+
 /*
  * We always have a files backend and it is the default.
  */
@@ -784,6 +789,13 @@ void ref_transaction_free(struct ref_transaction *transaction)
 	free(transaction);
 }
 
+static void add_update_obj(struct ref_transaction *transaction,
+			   struct ref_update *update)
+{
+	ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
+	transaction->updates[transaction->nr++] = update;
+}
+
 static struct ref_update *add_update(struct ref_transaction *transaction,
 				     const char *refname)
 {
@@ -791,8 +803,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
 	struct ref_update *update = xcalloc(1, sizeof(*update) + len);
 
 	memcpy((char *)update->refname, refname, len); /* includes NUL */
-	ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
-	transaction->updates[transaction->nr++] = update;
+	add_update_obj(transaction, update);
 	return update;
 }
 
@@ -1130,11 +1141,87 @@ int refs_init_db(struct strbuf *err, int shared)
 	return the_refs_backend->init_db(err, shared);
 }
 
+/*
+ * Special case for non-normal refs.  For symbolic-refs when
+ * REF_NODEREF is not turned on, we dereference them here and replace
+ * updates to the symbolic refs with updates to the underlying ref.
+ * Then we do our own reflogging for the symbolic ref.
+ *
+ * We move other non-normal ref updates with into a specially-created
+ * files-backend transaction
+ */
+static int move_abnormal_ref_updates(struct ref_transaction *transaction,
+				     struct ref_transaction *files_transaction,
+				     struct string_list *symrefs)
+{
+	int i;
+
+	for (i = 0; i < transaction->nr; i++) {
+		struct ref_update *update = transaction->updates[i];
+		const char *resolved;
+		int flags = 0;
+		unsigned char sha1[20];
+
+		if (ref_type(update->refname) == REF_TYPE_NORMAL)
+			continue;
+
+		resolved = resolve_ref_unsafe(update->refname, 0, sha1, &flags);
+
+		if (update->flags & REF_NODEREF || !(flags & REF_ISSYMREF)) {
+			int last;
+
+			add_update_obj(files_transaction, update);
+			/*
+			 * Replace this transaction with the
+			 * last transaction, removing it from
+			 * the list of backend transactions
+			 */
+			last = --transaction->nr;
+			transaction->updates[i] = transaction->updates[last];
+			continue;
+		}
+
+		if (resolved) {
+			struct ref_update *new_update;
+			struct string_list_item *item;
+
+			if (ref_type(resolved) != REF_TYPE_NORMAL)
+				die("Non-normal symbolic ref `%s` points to non-normal ref `%s`", update->refname, resolved);
+
+			new_update = xmalloc(sizeof(*new_update) +
+					     strlen(resolved) + 1);
+			memcpy(new_update, update, sizeof(*update));
+
+			if (update->flags & REF_HAVE_OLD &&
+			    hashcmp(sha1, update->old_sha1)) {
+				/* consistency check failed */
+				free(new_update);
+				return -1;
+			} else {
+				hashcpy(update->old_sha1, sha1);
+			}
+
+			strcpy((char *)new_update->refname, resolved);
+			transaction->updates[i] = new_update;
+
+			item = string_list_append(symrefs, update->refname);
+			item->util = new_update;
+			free(update);
+		}
+	}
+
+	return 0;
+}
+
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
 	int ret = -1;
 	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+	struct string_list files_affected_refnames = STRING_LIST_INIT_NODUP;
+	struct string_list symrefs = STRING_LIST_INIT_DUP;
+	struct string_list_item *item;
+	struct ref_transaction *files_transaction = NULL;
 
 	assert(err);
 
@@ -1146,6 +1233,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 		return 0;
 	}
 
+	if (the_refs_backend != &refs_be_files) {
+		files_transaction = ref_transaction_begin(err);
+		if (!files_transaction)
+			die("%s", err->buf);
+
+		ret = move_abnormal_ref_updates(transaction, files_transaction,
+						&symrefs);
+		if (ret)
+			goto done;
+
+		/* files backend commit */
+		if (ref_update_reject_duplicates(files_transaction,
+						 &files_affected_refnames,
+						 err)) {
+			ret = TRANSACTION_GENERIC_ERROR;
+			goto done;
+		}
+	}
+
+	/* main backend commit */
 	if (ref_update_reject_duplicates(transaction, &affected_refnames, err)) {
 		ret = TRANSACTION_GENERIC_ERROR;
 		goto done;
@@ -1153,8 +1260,35 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 	ret = the_refs_backend->transaction_commit(transaction,
 						   &affected_refnames, err);
+	if (ret)
+		goto done;
+
+	if (the_refs_backend != &refs_be_files) {
+		ret = refs_be_files.transaction_commit(files_transaction,
+						       &files_affected_refnames,
+						       err);
+		if (ret) {
+			warning(split_transaction_fail_warning);
+			goto done;
+		}
+
+		/* reflogging for dereferenced symbolic refs */
+		for_each_string_list_item(item, &symrefs) {
+			struct ref_update *update = item->util;
+			if (files_log_ref_write(item->string, update->old_sha1,
+						update->new_sha1,
+						update->msg, update->flags, err))
+				warning("failed to log ref update for symref %s",
+					item->string);
+		}
+	}
+
 done:
 	string_list_clear(&affected_refnames, 0);
+	string_list_clear(&files_affected_refnames, 0);
+	if (files_transaction)
+		ref_transaction_free(files_transaction);
+	string_list_clear(&symrefs, 0);
 	return ret;
 }
 
@@ -1210,6 +1344,9 @@ int peel_ref(const char *refname, unsigned char *sha1)
 int create_symref(const char *ref_target, const char *refs_heads_master,
 		  const char *logmsg)
 {
+	if (ref_type(ref_target) != REF_TYPE_NORMAL)
+		return refs_be_files.create_symref(ref_target, refs_heads_master,
+						   logmsg);
 	return the_refs_backend->create_symref(ref_target, refs_heads_master,
 					       logmsg);
 }
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (11 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 12/16] refs: always handle non-normal refs in files backend David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-05  0:07   ` Junio C Hamano
                     ` (4 more replies)
  2015-12-03  0:35 ` [PATCH 14/16] refs: allow ref backend to be set for clone David Turner
                   ` (3 subsequent siblings)
  16 siblings, 5 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

git init learns a new argument --refs-backend-type.  Presently, only
"files" is supported, but later we will add other backends.

When this argument is used, the repository's core.refsBackendType
configuration value is set, and the refs backend's initdb function is
used to set up the ref database.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 Documentation/git-init-db.txt |  2 +-
 Documentation/git-init.txt    |  6 +++++-
 builtin/init-db.c             | 10 ++++++++++
 cache.h                       |  2 ++
 config.c                      | 20 ++++++++++++++++++++
 environment.c                 |  1 +
 path.c                        | 32 ++++++++++++++++++++++++++++++--
 refs.c                        |  8 ++++++++
 refs.h                        | 12 ++++++++++++
 setup.c                       | 10 ++++++++++
 10 files changed, 99 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-init-db.txt b/Documentation/git-init-db.txt
index 648a6cd..72fbd71 100644
--- a/Documentation/git-init-db.txt
+++ b/Documentation/git-init-db.txt
@@ -9,7 +9,7 @@ git-init-db - Creates an empty Git repository
 SYNOPSIS
 --------
 [verse]
-'git init-db' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]]
+'git init-db' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [--refs-backend-type=<name>]
 
 
 DESCRIPTION
diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
index 8174d27..9ea6753 100644
--- a/Documentation/git-init.txt
+++ b/Documentation/git-init.txt
@@ -12,7 +12,7 @@ SYNOPSIS
 'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
 	  [--separate-git-dir <git dir>]
 	  [--shared[=<permissions>]] [directory]
-
+	  [--refs-backend-type=<name>]
 
 DESCRIPTION
 -----------
@@ -113,6 +113,10 @@ does not exist, it will be created.
 
 --
 
+--refs-backend-type=<name>::
+Type of refs backend. Default is to use the original "files" backend,
+which stores ref data in files in .git/refs and .git/packed-refs.
+
 TEMPLATE DIRECTORY
 ------------------
 
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 4771e7e..44db591 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -204,6 +204,14 @@ static int create_default_files(const char *template_path)
 		adjust_shared_perm(get_git_dir());
 	}
 
+	if (refs_backend_type) {
+		struct refdb_config_data config_data = {NULL};
+		git_config_set("core.refsBackendType", refs_backend_type);
+		config_data.refs_backend_type = refs_backend_type;
+		config_data.refs_base = get_git_dir();
+		set_refs_backend(refs_backend_type, &config_data);
+	}
+
 	if (refs_init_db(&err, shared_repository))
 		die("failed to set up refs db: %s", err.buf);
 
@@ -469,6 +477,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 		OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
 		OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
 			   N_("separate git dir from working tree")),
+		OPT_STRING(0, "refs-backend-type", &refs_backend_type,
+			   N_("name"), N_("name of backend type to use")),
 		OPT_END()
 	};
 
diff --git a/cache.h b/cache.h
index 707455a..d1534db 100644
--- a/cache.h
+++ b/cache.h
@@ -696,6 +696,8 @@ extern enum object_creation_mode object_creation_mode;
 
 extern char *notes_ref_name;
 
+extern const char *refs_backend_type;
+
 extern int grafts_replace_parents;
 
 /*
diff --git a/config.c b/config.c
index 248a21a..210aa08 100644
--- a/config.c
+++ b/config.c
@@ -10,6 +10,7 @@
 #include "exec_cmd.h"
 #include "strbuf.h"
 #include "quote.h"
+#include "refs.h"
 #include "hashmap.h"
 #include "string-list.h"
 #include "utf8.h"
@@ -1207,6 +1208,25 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
 	}
 
 	if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
+		struct refdb_config_data refdb_data = {NULL};
+		char *repo_config_copy;
+
+		/*
+		 * make sure we always read the backend config from the
+		 * core section on startup
+		 */
+		ret += git_config_from_file(refdb_config, repo_config,
+					    &refdb_data);
+
+		repo_config_copy = xstrdup(repo_config);
+		refdb_data.refs_base = xstrdup(dirname(repo_config_copy));
+		free(repo_config_copy);
+
+		if (refdb_data.refs_backend_type &&
+		    strcmp(refdb_data.refs_backend_type, "files")) {
+			die("Unexpected backend %s", refdb_data.refs_backend_type);
+		}
+
 		ret += git_config_from_file(fn, repo_config, data);
 		found += 1;
 	}
diff --git a/environment.c b/environment.c
index 2da7fe2..8dbf0ab 100644
--- a/environment.c
+++ b/environment.c
@@ -66,6 +66,7 @@ int merge_log_config = -1;
 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
 struct startup_info *startup_info;
 unsigned long pack_size_limit_cfg;
+const char *refs_backend_type;
 
 #ifndef PROTECT_HFS_DEFAULT
 #define PROTECT_HFS_DEFAULT 0
diff --git a/path.c b/path.c
index 3cd155e..86a8035 100644
--- a/path.c
+++ b/path.c
@@ -2,6 +2,7 @@
  * Utilities for paths and pathnames
  */
 #include "cache.h"
+#include "refs.h"
 #include "strbuf.h"
 #include "string-list.h"
 #include "dir.h"
@@ -510,9 +511,36 @@ int validate_headref(const char *path)
 	unsigned char sha1[20];
 	int fd;
 	ssize_t len;
+	struct refdb_config_data refdb_data = {NULL, NULL};
+
+	if (lstat(path, &st) < 0) {
+		int backend_type_set;
+		struct strbuf config_path = STRBUF_INIT;
+		if (path) {
+			char *pathdup = xstrdup(path);
+			char *git_dir = dirname(pathdup);
+			strbuf_addf(&config_path, "%s/%s", git_dir, "config");
+			free(pathdup);
+		} else {
+			strbuf_addstr(&config_path, "config");
+		}
 
-	if (lstat(path, &st) < 0)
-		return -1;
+		if (git_config_from_file(refdb_config, config_path.buf, &refdb_data)) {
+			strbuf_release(&config_path);
+			return -1;
+		}
+
+		backend_type_set = !!refdb_data.refs_backend_type;
+		free((void *)refdb_data.refs_backend_type);
+		free((void *)refdb_data.refs_base);
+		strbuf_release(&config_path);
+		/*
+		 * Alternate backends are assumed to keep HEAD
+		 * in a valid state, so there's no need to do
+		 * further validation.
+		 */
+		return backend_type_set ? 0 : -1;
+	}
 
 	/* Make sure it is a "refs/.." symlink */
 	if (S_ISLNK(st.st_mode)) {
diff --git a/refs.c b/refs.c
index e48e43a..96e1673 100644
--- a/refs.c
+++ b/refs.c
@@ -24,6 +24,14 @@ struct ref_be *the_refs_backend = &refs_be_files;
  */
 struct ref_be *refs_backends = &refs_be_files;
 
+const char *refs_backend_type;
+
+void register_refs_backend(struct ref_be *be)
+{
+	be->next = refs_backends;
+	refs_backends = be;
+}
+
 /*
  * This function is used to switch to an alternate backend.
  */
diff --git a/refs.h b/refs.h
index c211b9e..c3670e8 100644
--- a/refs.h
+++ b/refs.h
@@ -510,6 +510,18 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,
 			 reflog_expiry_cleanup_fn cleanup_fn,
 			 void *policy_cb_data);
 
+struct refdb_config_data {
+	const char *refs_backend_type;
+	const char *refs_base;
+};
+/*
+ * Read the refdb configuration data out of the config file
+ */
+int refdb_config(const char *var, const char *value, void *ptr);
+
+struct ref_be;
 int set_refs_backend(const char *name, void *data);
 
+void register_refs_backend(struct ref_be *be);
+
 #endif /* REFS_H */
diff --git a/setup.c b/setup.c
index d343725..de6b8ac 100644
--- a/setup.c
+++ b/setup.c
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "dir.h"
+#include "refs.h"
 #include "string-list.h"
 
 static int inside_git_dir = -1;
@@ -263,6 +264,15 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
 	return ret;
 }
 
+int refdb_config(const char *var, const char *value, void *ptr)
+{
+       struct refdb_config_data *cdata = ptr;
+
+       if (!strcmp(var, "core.refsbackendtype"))
+	       cdata->refs_backend_type = xstrdup((char *)value);
+       return 0;
+}
+
 /*
  * Test if it looks like we're at a git directory.
  * We want to see:
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 14/16] refs: allow ref backend to be set for clone
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (12 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-23 13:51   ` Michael Haggerty
  2015-12-03  0:35 ` [PATCH 15/16] refs: add LMDB refs backend David Turner
                   ` (2 subsequent siblings)
  16 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Add a new option, --refs-backend-type, to allow the ref backend type to
be set on new clones.

Submodules must use the same ref backend as the parent repository, so
we also pass the --refs-backend-type option option when cloning
submodules.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 Documentation/git-clone.txt |  4 ++++
 builtin/clone.c             | 27 +++++++++++++++++++++++++--
 builtin/submodule--helper.c |  5 ++++-
 cache.h                     |  1 +
 4 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index 6bf000d..431575b 100644
--- a/Documentation/git-clone.txt
+++ b/Documentation/git-clone.txt
@@ -14,6 +14,7 @@ SYNOPSIS
 	  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
 	  [--dissociate] [--separate-git-dir <git dir>]
 	  [--depth <depth>] [--[no-]single-branch]
+	  [--refs-backend-type=<name>]
 	  [--recursive | --recurse-submodules] [--] <repository>
 	  [<directory>]
 
@@ -221,6 +222,9 @@ objects from the source repository into a pack in the cloned repository.
 	The result is Git repository can be separated from working
 	tree.
 
+--refs-backend-type=<name>::
+	Type of refs backend. Default is to use the original files based
+	backend.
 
 <repository>::
 	The (possibly remote) repository to clone from.  See the
diff --git a/builtin/clone.c b/builtin/clone.c
index caae43e..a53f341 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -92,11 +92,13 @@ static struct option builtin_clone_options[] = {
 		   N_("separate git dir from working tree")),
 	OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
 			N_("set config inside the new repository")),
+	OPT_STRING(0, "refs-backend-type", &refs_backend_type,
+		   N_("name"), N_("name of backend type to use")),
 	OPT_END()
 };
 
 static const char *argv_submodule[] = {
-	"submodule", "update", "--init", "--recursive", NULL
+	"submodule", "update", "--init", "--recursive", NULL, NULL
 };
 
 static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
@@ -724,8 +726,24 @@ static int checkout(void)
 	err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
 			   sha1_to_hex(sha1), "1", NULL);
 
-	if (!err && option_recursive)
+	if (!err && option_recursive) {
+		const char **backend_arg = argv_submodule;
+		char *new_backend_arg = NULL;
+		if (refs_backend_type) {
+			while (*backend_arg)
+				++backend_arg;
+
+			new_backend_arg = xmalloc(21 + strlen(refs_backend_type));
+			sprintf(new_backend_arg, "--refs-backend-type=%s",
+				refs_backend_type);
+			*backend_arg = new_backend_arg;
+		}
 		err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
+		if (refs_backend_type) {
+			free(new_backend_arg);
+			*backend_arg = NULL;
+		}
+	}
 
 	return err;
 }
@@ -744,6 +762,11 @@ static void write_config(struct string_list *config)
 					       write_one_config, NULL) < 0)
 			die("unable to write parameters to config file");
 	}
+
+	if (refs_backend_type &&
+	    write_one_config("core.refsBackendType",
+			     refs_backend_type, NULL) < 0)
+		die("unable to write backend parameter to config file");
 }
 
 static void write_refspec_config(const char *src_ref_prefix,
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index f4c3eff..5c9ca4e 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -140,7 +140,10 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
 		argv_array_pushl(&cp.args, "--reference", reference, NULL);
 	if (gitdir && *gitdir)
 		argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
-
+	if (refs_backend_type && *refs_backend_type) {
+		argv_array_push(&cp.args, "--refs-backend-type");
+		argv_array_push(&cp.args, refs_backend_type);
+	}
 	argv_array_push(&cp.args, url);
 	argv_array_push(&cp.args, path);
 
diff --git a/cache.h b/cache.h
index d1534db..8f2ca55 100644
--- a/cache.h
+++ b/cache.h
@@ -695,6 +695,7 @@ enum object_creation_mode {
 extern enum object_creation_mode object_creation_mode;
 
 extern char *notes_ref_name;
+extern const char *refs_backend_type;
 
 extern const char *refs_backend_type;
 
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 15/16] refs: add LMDB refs backend
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (13 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 14/16] refs: allow ref backend to be set for clone David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-05  0:08   ` Junio C Hamano
                     ` (2 more replies)
  2015-12-03  0:35 ` [PATCH 16/16] refs: tests for lmdb backend David Turner
  2015-12-22 23:56 ` [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
  16 siblings, 3 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Add a database backend for refs using LMDB.  This backend runs git
for-each-ref about 30% faster than the files backend with fully-packed
refs on a repo with ~120k refs.  It's also about 4x faster than using
fully-unpacked refs.  In addition, and perhaps more importantly, it
avoids case-conflict issues on OS X.

LMDB has a few features that make it suitable for usage in git:

1. It is relatively lightweight; it requires only one header file, and
the library code takes under 64k at runtime.

2. It is well-tested: it's been used in OpenLDAP for years.

3. It's very fast.  LMDB's benchmarks show that it is among
the fastest key-value stores.

4. It has a relatively simple concurrency story; readers don't
block writers and writers don't block readers.

Ronnie Sahlberg's original version of this patchset used tdb.  The
major disadvantage of tdb is that tdb is hard to build on OS X.  It's
also not in homebrew.  So lmdb seemed simpler.

To test this backend's correctness, I hacked test-lib.sh and
test-lib-functions.sh to run all tests under the refs backend. Dozens
of tests use manual ref/reflog reading/writing, or create submodules
without passing --refs-backend-type to git init.  If those tests are
changed to use the update-ref machinery or test-refs-lmdb-backend (or,
in the case of packed-refs, corrupt refs, and dumb fetch tests, are
skipped), the only remaining failing tests are the git-new-workdir
tests and the gitweb tests.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 .gitignore                                     |    1 +
 Documentation/config.txt                       |    7 +
 Documentation/git-clone.txt                    |    2 +-
 Documentation/git-init.txt                     |    3 +-
 Documentation/technical/refs-lmdb-backend.txt  |   50 +
 Documentation/technical/repository-version.txt |    5 +
 Makefile                                       |   12 +
 builtin/init-db.c                              |   10 +-
 config.c                                       |   20 +-
 configure.ac                                   |   33 +
 contrib/workdir/git-new-workdir                |    3 +
 refs.h                                         |    1 +
 refs/lmdb-backend.c                            | 2054 ++++++++++++++++++++++++
 setup.c                                        |   22 +-
 test-refs-lmdb-backend.c                       |   68 +
 15 files changed, 2279 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/technical/refs-lmdb-backend.txt
 create mode 100644 refs/lmdb-backend.c
 create mode 100644 test-refs-lmdb-backend.c

diff --git a/.gitignore b/.gitignore
index 1c2f832..87d45a2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -199,6 +199,7 @@
 /test-path-utils
 /test-prio-queue
 /test-read-cache
+/test-refs-lmdb-backend
 /test-regex
 /test-revision-walking
 /test-run-command
diff --git a/Documentation/config.txt b/Documentation/config.txt
index f617886..5fb25ed 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -496,6 +496,13 @@ core.repositoryFormatVersion::
 	Internal variable identifying the repository format and layout
 	version.
 
+core.refsBackendType::
+	Type of refs backend. Default is to use the original files
+	based backend. Set to 'lmdb' to activate the lmdb database
+	backend.  If you use the lmdb backend,
+	core.repositoryFormatVersion must be set to 1, and
+	extensions.refBackend must be set to 'lmdb'.
+
 core.sharedRepository::
 	When 'group' (or 'true'), the repository is made shareable between
 	several users in a group (making sure all the files and objects are
diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index 431575b..739c116 100644
--- a/Documentation/git-clone.txt
+++ b/Documentation/git-clone.txt
@@ -224,7 +224,7 @@ objects from the source repository into a pack in the cloned repository.
 
 --refs-backend-type=<name>::
 	Type of refs backend. Default is to use the original files based
-	backend.
+	backend. Set to "lmdb" to activate the lmdb database backend.
 
 <repository>::
 	The (possibly remote) repository to clone from.  See the
diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
index 9ea6753..bbe253f 100644
--- a/Documentation/git-init.txt
+++ b/Documentation/git-init.txt
@@ -115,7 +115,8 @@ does not exist, it will be created.
 
 --refs-backend-type=<name>::
 Type of refs backend. Default is to use the original "files" backend,
-which stores ref data in files in .git/refs and .git/packed-refs.
+which stores ref data in files in .git/refs and .git/packed-refs.  Set
+to "lmdb" to activate the lmdb database backend.
 
 TEMPLATE DIRECTORY
 ------------------
diff --git a/Documentation/technical/refs-lmdb-backend.txt b/Documentation/technical/refs-lmdb-backend.txt
new file mode 100644
index 0000000..c497ffc
--- /dev/null
+++ b/Documentation/technical/refs-lmdb-backend.txt
@@ -0,0 +1,50 @@
+Notes on the LMDB refs backend
+==============================
+
+Design:
+------
+
+Refs and reflogs are stored in a lmdb database in .git/refdb.  All
+keys and values are \0-terminated.
+
+Keys for refs are the name of the ref (e.g. refs/heads/master).
+Values are the value of the ref, in hex
+(e.g. 61f23eb0f81357c19fa91e2b8c6f3906c3a8f9b0).
+
+All per-worktree refs (refs/bisect/* and HEAD) are store using
+the traditional files-based backend.
+
+Reflogs are stored as a series of database entries.
+
+For non-empty reflogs, there is one entry per logged ref
+update.  The key format is logs/[refname]\0[timestamp].  The timestamp
+is a 64-bit unsigned integer number of nanoseconds since 1/1/1970.
+This means that reflog entries are chronologically ordered.  Because
+LMDB is a btree database, we can efficiently iterate over these keys.
+
+For an empty reflog, there is a "header" entry to show that a reflog
+exists.  The header has the same format as an ordinary reflog, but with
+a timeztamp of all zeros and an empty value.
+
+Reflog values are in the same format as the original files-based
+reflog.
+
+Weaknesses:
+-----------
+
+The reflog format is somewhat inefficient: a binary format could store
+reflog date/time information in somewhat less space.
+
+The rsync and file:// transports don't work yet, because they
+don't use the refs API.
+
+git new-workdir is incompatible with the lmdb backend.  Fortunately,
+git new-workdir is deprecated, and worktrees work fine.
+
+LMDB locks the entire database for write.  Any other writer waits
+until the first writer is done before beginning.  Readers do not wait
+for writers, and writers do not wait for readers.  The underlying
+scheme is approximately MVCC; each reader's queries see the state of
+the database as-of the time that the reader acquired its read lock.
+This is not too far off from the files backend, which loads all refs
+into memory when one is requested.
diff --git a/Documentation/technical/repository-version.txt b/Documentation/technical/repository-version.txt
index 00ad379..04c085d 100644
--- a/Documentation/technical/repository-version.txt
+++ b/Documentation/technical/repository-version.txt
@@ -86,3 +86,8 @@ for testing format-1 compatibility.
 When the config key `extensions.preciousObjects` is set to `true`,
 objects in the repository MUST NOT be deleted (e.g., by `git-prune` or
 `git repack -d`).
+
+`refBackend`
+~~~~~~~~~~~~
+This extension allows the user of alternate ref backends.  The only
+defined value is `lmdb`.
diff --git a/Makefile b/Makefile
index 5bd68e0..77b96d9 100644
--- a/Makefile
+++ b/Makefile
@@ -1037,6 +1037,17 @@ ifdef USE_LIBPCRE
 	EXTLIBS += -lpcre
 endif
 
+ifdef USE_LIBLMDB
+	BASIC_CFLAGS += -DUSE_LIBLMDB
+	ifdef LIBLMDBDIR
+		BASIC_CFLAGS += -I$(LIBLMDBDIR)/include
+		EXTLIBS += -L$(LIBLMDBDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBLMDBDIR)/$(lib)
+	endif
+	EXTLIBS += -llmdb
+	LIB_OBJS += refs/lmdb-backend.o
+	TEST_PROGRAMS_NEED_X += test-refs-lmdb-backend
+endif
+
 ifdef HAVE_ALLOCA_H
 	BASIC_CFLAGS += -DHAVE_ALLOCA_H
 endif
@@ -2124,6 +2135,7 @@ GIT-BUILD-OPTIONS: FORCE
 	@echo NO_CURL=\''$(subst ','\'',$(subst ','\'',$(NO_CURL)))'\' >>$@+
 	@echo NO_EXPAT=\''$(subst ','\'',$(subst ','\'',$(NO_EXPAT)))'\' >>$@+
 	@echo USE_LIBPCRE=\''$(subst ','\'',$(subst ','\'',$(USE_LIBPCRE)))'\' >>$@+
+	@echo USE_LIBLMDB=\''$(subst ','\'',$(subst ','\'',$(USE_LIBLMDB)))'\' >>$@+
 	@echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+
 	@echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+
 	@echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 44db591..1eb2feb 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -179,6 +179,7 @@ static int create_default_files(const char *template_path)
 	int reinit;
 	int filemode;
 	struct strbuf err = STRBUF_INIT;
+	int repo_version = 0;
 
 	/* Just look for `init.templatedir` */
 	git_config(git_init_db_config, NULL);
@@ -209,7 +210,14 @@ static int create_default_files(const char *template_path)
 		git_config_set("core.refsBackendType", refs_backend_type);
 		config_data.refs_backend_type = refs_backend_type;
 		config_data.refs_base = get_git_dir();
+#ifdef USE_LIBLMDB
+		register_refs_backend(&refs_be_lmdb);
+#endif
 		set_refs_backend(refs_backend_type, &config_data);
+		if (!strcmp(refs_backend_type, "lmdb")) {
+			git_config_set("extensions.refbackend", "lmdb");
+			repo_version = 1;
+		}
 	}
 
 	if (refs_init_db(&err, shared_repository))
@@ -229,7 +237,7 @@ static int create_default_files(const char *template_path)
 
 	/* This forces creation of new config file */
 	xsnprintf(repo_version_string, sizeof(repo_version_string),
-		  "%d", GIT_REPO_VERSION);
+		  "%d", repo_version);
 	git_config_set("core.repositoryformatversion", repo_version_string);
 
 	/* Check filemode trustability */
diff --git a/config.c b/config.c
index 210aa08..779bb73 100644
--- a/config.c
+++ b/config.c
@@ -1222,9 +1222,23 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
 		refdb_data.refs_base = xstrdup(dirname(repo_config_copy));
 		free(repo_config_copy);
 
-		if (refdb_data.refs_backend_type &&
-		    strcmp(refdb_data.refs_backend_type, "files")) {
-			die("Unexpected backend %s", refdb_data.refs_backend_type);
+		if (!refdb_data.refs_backend_type)
+			refdb_data.refs_backend_type = "";
+
+		if ((!*refdb_data.refs_backend_type) ||
+		    (!strcmp(refdb_data.refs_backend_type, "files"))) {
+			/* default backend, nothing to do */
+		} else if (!strcmp(refdb_data.refs_backend_type, "lmdb")) {
+
+#ifdef USE_LIBLMDB
+			refs_backend_type = refdb_data.refs_backend_type;
+			register_refs_backend(&refs_be_lmdb);
+			set_refs_backend(refs_backend_type, &refdb_data);
+#else
+			die("Git was not built with USE_LIBLMDB, so the db refs backend is not available");
+#endif
+		} else {
+			die("Unknown ref backend type '%s'", refdb_data.refs_backend_type);
 		}
 
 		ret += git_config_from_file(fn, repo_config, data);
diff --git a/configure.ac b/configure.ac
index 89e2590..3853bec 100644
--- a/configure.ac
+++ b/configure.ac
@@ -271,6 +271,24 @@ AS_HELP_STRING([],           [ARG can be also prefix for libpcre library and hea
         dnl it yet.
 	GIT_CONF_SUBST([LIBPCREDIR])
     fi)
+
+USE_LIBLMDB=YesPlease
+AC_ARG_WITH(liblmdb,
+AS_HELP_STRING([--with-liblmdb],[support lmdb (default is YES])
+AS_HELP_STRING([],           [ARG can be also prefix for liblmdb library and headers]),
+    if test "$withval" = "no"; then
+	USE_LIBLMDB=
+    elif test "$withval" = "yes"; then
+	USE_LIBLMDB=YesPlease
+    else
+	USE_LIBLMDB=YesPlease
+	LIBLMDBDIR=$withval
+	AC_MSG_NOTICE([Setting LIBLMDBDIR to $LIBLMDBDIR])
+        dnl USE_LIBLMDB can still be modified below, so don't substitute
+        dnl it yet.
+	GIT_CONF_SUBST([LIBLMDBDIR])
+    fi)
+
 #
 # Define HAVE_ALLOCA_H if you have working alloca(3) defined in that header.
 AC_FUNC_ALLOCA
@@ -510,6 +528,21 @@ GIT_CONF_SUBST([USE_LIBPCRE])
 
 fi
 
+if test -n "$USE_LIBLMDB"; then
+
+GIT_STASH_FLAGS($LIBLMDBDIR)
+
+AC_CHECK_LIB([lmdb], [mdb_env_open],
+[USE_LIBLMDB=YesPlease],
+[USE_LIBLMDB=])
+
+GIT_UNSTASH_FLAGS($LIBLMDBDIR)
+
+GIT_CONF_SUBST([USE_LIBLMDB])
+
+fi
+
+
 #
 # Define NO_CURL if you do not have libcurl installed.  git-http-pull and
 # git-http-push are not built, and you cannot use http:// and https://
diff --git a/contrib/workdir/git-new-workdir b/contrib/workdir/git-new-workdir
index 888c34a..66b7ecf 100755
--- a/contrib/workdir/git-new-workdir
+++ b/contrib/workdir/git-new-workdir
@@ -28,6 +28,9 @@ git_dir=$(cd "$orig_git" 2>/dev/null &&
   git rev-parse --git-dir 2>/dev/null) ||
   die "Not a git repository: \"$orig_git\""
 
+
+test "$(git config core.refsbackendtype)" = "lmdb" && die "git-new-workdir is incompatible with the refs lmdb backend"
+
 case "$git_dir" in
 .git)
 	git_dir="$orig_git/.git"
diff --git a/refs.h b/refs.h
index c3670e8..0cbfda9 100644
--- a/refs.h
+++ b/refs.h
@@ -520,6 +520,7 @@ struct refdb_config_data {
 int refdb_config(const char *var, const char *value, void *ptr);
 
 struct ref_be;
+extern struct ref_be refs_be_lmdb;
 int set_refs_backend(const char *name, void *data);
 
 void register_refs_backend(struct ref_be *be);
diff --git a/refs/lmdb-backend.c b/refs/lmdb-backend.c
new file mode 100644
index 0000000..654048b
--- /dev/null
+++ b/refs/lmdb-backend.c
@@ -0,0 +1,2054 @@
+/*
+ * This file implements a lmdb backend for refs.
+ *
+ * The design of this backend relies on lmdb's write lock -- that is, any
+ * write transaction blocks all other writers.  Thus, as soon as a ref
+ * transaction is opened, we know that any values we read won't
+ * change out from under us, and we have a fully-consistent view of the
+ * database.
+ *
+ * We store the content of refs including the trailing \0 so that
+ * standard C string functions can handle them.  Just like struct
+ * strbuf.
+ */
+#include "../cache.h"
+#include <lmdb.h>
+#include "../object.h"
+#include "../refs.h"
+#include "refs-internal.h"
+#include "../tag.h"
+#include "../lockfile.h"
+
+static struct trace_key db_trace = TRACE_KEY_INIT(LMDB);
+
+static MDB_env *env;
+
+static char *db_path;
+
+extern struct ref_be refs_be_files;
+
+struct lmdb_transaction {
+	MDB_txn *txn;
+	MDB_dbi dbi;
+	MDB_cursor *cursor;
+	const char *submodule;
+	int flags;
+};
+
+struct lmdb_transaction transaction;
+
+static char *get_refdb_path(const char *base)
+{
+	struct strbuf path_buf = STRBUF_INIT;
+	strbuf_addf(&path_buf, "%s/refdb", base);
+	return strbuf_detach(&path_buf, NULL);
+}
+
+static int in_write_transaction(void)
+{
+	return transaction.txn && !(transaction.flags & MDB_RDONLY);
+}
+
+static void init_env(MDB_env **env, const char *path)
+{
+	int ret;
+	if (*env)
+		return;
+
+	ret = mdb_env_create(env);
+	if (ret)
+		die("mdb_env_create failed: %s", mdb_strerror(ret));
+	ret = mdb_env_set_maxreaders(*env, 1000);
+	if (ret)
+		die("BUG: mdb_env_set_maxreaders failed: %s", mdb_strerror(ret));
+	ret = mdb_env_set_mapsize(*env, (1<<30));
+	if (ret)
+		die("BUG: mdb_set_mapsize failed: %s", mdb_strerror(ret));
+	ret = mdb_env_open(*env, path, 0 , 0664);
+	if (ret)
+		die("BUG: mdb_env_open (%s) failed: %s", path,
+		    mdb_strerror(ret));
+}
+
+static int lmdb_init_db(struct strbuf *err, int shared)
+{
+	/*
+	 * To create a db, all we need to do is make a directory for
+	 * it to live in; lmdb will do the rest.
+	 */
+
+	assert(db_path);
+	if (mkdir(db_path, 0775) && errno != EEXIST) {
+		strbuf_addf(err, "%s", strerror(errno));
+		return -1;
+	}
+
+	return 0;
+}
+
+static void lmdb_init_backend(void *cbdata)
+{
+	struct refdb_config_data *data = (struct refdb_config_data *)cbdata;
+
+	if (db_path)
+		return;
+
+	db_path = xstrdup(real_path(get_refdb_path(data->refs_base)));
+
+	refs_be_files.init_backend(NULL);
+	trace_printf_key(&db_trace, "Init backend\n");
+}
+
+static void mdb_cursor_open_or_die(struct lmdb_transaction *transaction,
+				   MDB_cursor **cursor)
+{
+	int ret = mdb_cursor_open(transaction->txn, transaction->dbi, cursor);
+	if (ret)
+		die("mdb_cursor_open failed: %s", mdb_strerror(ret));
+}
+
+static void submodule_path(struct strbuf *sb, const char *submodule,
+			   const char *refname)
+{
+	if (submodule)
+		strbuf_git_path_submodule(sb, submodule, "%s", refname);
+	else
+		strbuf_git_path(sb, "%s", refname);
+}
+
+static int read_per_worktree_ref(const char *submodule, const char *refname,
+				 struct MDB_val *val, int *needs_free)
+{
+	struct strbuf sb = STRBUF_INIT;
+	struct strbuf path = STRBUF_INIT;
+	struct stat st;
+	int ret = -1;
+
+	submodule_path(&path, submodule, refname);
+
+#ifndef NO_SYMLINK_HEAD
+	if (lstat(path.buf, &st)) {
+		if (errno == ENOENT)
+			ret = MDB_NOTFOUND;
+		goto done;
+	}
+	if (S_ISLNK(st.st_mode)) {
+		strbuf_readlink(&sb, path.buf, 0);
+		if (starts_with(sb.buf, "refs/") &&
+		    !check_refname_format(sb.buf, 0)) {
+			val->mv_data = xmalloc(sb.len + 6);
+			val->mv_size = sprintf(val->mv_data, "ref: %s",
+					       sb.buf) + 1;
+			ret = 0;
+		} else {
+			ret = MDB_NOTFOUND;
+		}
+		strbuf_release(&sb);
+		goto done;
+	}
+#endif
+
+	if (strbuf_read_file(&sb, path.buf, 200) < 0) {
+		strbuf_release(&sb);
+		if (errno == ENOENT)
+			ret = MDB_NOTFOUND;
+		goto done;
+	}
+	strbuf_rtrim(&sb);
+
+	val->mv_data = strbuf_detach(&sb, &val->mv_size);
+	val->mv_size++;
+
+	ret = 0;
+done:
+	strbuf_release(&path);
+	*needs_free = !ret;
+	return ret;
+}
+
+static void write_per_worktree_ref(const char *submodule, const char *refname,
+				   MDB_val *val)
+{
+	static struct lock_file lock;
+	int fd;
+	int len = val->mv_size - 1;
+	struct strbuf path = STRBUF_INIT;
+
+	submodule_path(&path, submodule, refname);
+	safe_create_leading_directories(path.buf);
+
+	fd = hold_lock_file_for_update(&lock, path.buf, LOCK_DIE_ON_ERROR);
+	strbuf_release(&path);
+
+	if (write_in_full(fd, val->mv_data, len) != len ||
+	    write_in_full(fd, "\n", 1) != 1)
+		die_errno("failed to write new HEAD");
+
+	if (commit_lock_file(&lock))
+		die_errno("failed to write new HEAD");
+}
+
+static int del_per_worktree_ref(const char *submodule, const char *refname,
+				MDB_val *val)
+{
+	struct strbuf path = STRBUF_INIT;
+	int result;
+
+	/*
+	 * Returning deleted ref data is not yet implemented, but no
+	 * callers need it.
+	 */
+	assert(val == NULL);
+
+	submodule_path(&path, submodule, refname);
+
+	result = unlink(path.buf);
+	strbuf_release(&path);
+	if (result && errno != ENOENT)
+		return 1;
+
+	return 0;
+}
+
+/*
+ * Read a ref.  If the ref is a per-worktree ref, read it from disk.
+ * Otherwise, read it from LMDB.  LMDB manages its own memory, so the
+ * data returned in *val will ordinarily not need to be freed.  But
+ * when a per-worktree ref is (successfully) read, non-LMDB memory is
+ * allocated.  In this case, *needs_free is set so that the caller can
+ * free the memory when it is done with it.
+ */
+static int mdb_get_or_die(struct lmdb_transaction *transaction, MDB_val *key,
+			  MDB_val *val, int *needs_free)
+{
+	int ret;
+
+	if (ref_type(key->mv_data) != REF_TYPE_NORMAL)
+		return read_per_worktree_ref(transaction->submodule,
+					     key->mv_data, val, needs_free);
+
+	*needs_free = 0;
+	ret = mdb_get(transaction->txn, transaction->dbi, key, val);
+	if (ret) {
+		if (ret != MDB_NOTFOUND)
+			die("mdb_get failed: %s", mdb_strerror(ret));
+		return ret;
+	}
+	return 0;
+}
+
+static int mdb_del_or_die(struct lmdb_transaction *transaction, MDB_val *key,
+			  MDB_val *val)
+{
+	int ret;
+
+	if (ref_type(key->mv_data) != REF_TYPE_NORMAL)
+		die("BUG: this backend should only try to delete normal refs");
+
+	ret = mdb_del(transaction->txn, transaction->dbi, key, val);
+	if (ret) {
+		if (ret != MDB_NOTFOUND)
+			die("mdb_del failed: %s", mdb_strerror(ret));
+		return ret;
+	}
+	return 0;
+}
+
+static void mdb_put_or_die(struct lmdb_transaction *transaction, MDB_val *key,
+			   MDB_val *val, int mode)
+{
+	int ret;
+
+	if (ref_type(key->mv_data) != REF_TYPE_NORMAL)
+		die("BUG: this backend should only try to write normal refs");
+
+	ret = mdb_put(transaction->txn, transaction->dbi, key, val, mode);
+	if (ret) {
+		if (ret == MDB_BAD_VALSIZE)
+			die("Ref name %s too long (max size is %d)",
+			    (const char *)key->mv_data,
+			    mdb_env_get_maxkeysize(env));
+		else
+			die("mdb_put failed: (%s -> %s) %s",
+			    (const char *)key->mv_data,
+			    (const char *)val->mv_data, mdb_strerror(ret));
+	}
+}
+
+static int mdb_cursor_get_or_die(MDB_cursor *cursor, MDB_val *key, MDB_val *val, int mode)
+{
+	int ret;
+
+	ret = mdb_cursor_get(cursor, key, val, mode);
+	if (ret) {
+		if (ret != MDB_NOTFOUND)
+			die("mdb_cursor_get failed: %s", mdb_strerror(ret));
+		return ret;
+	}
+	assert(((char *)val->mv_data)[val->mv_size - 1] == 0);
+	return 0;
+}
+
+static int mdb_cursor_del_or_die(MDB_cursor *cursor, int flags)
+{
+	int ret = mdb_cursor_del(cursor, flags);
+	if (ret) {
+		if (ret != MDB_NOTFOUND)
+			die("mdb_cursor_del failed: %s", mdb_strerror(ret));
+		return ret;
+	}
+	return 0;
+}
+
+/*
+ * Begin a transaction. Because only one transaction per thread is
+ * permitted, we use a global transaction object.  If a read-write
+ * transaction is presently already in-progress, and a read-only
+ * transaction is requested, the read-write transaction will be
+ * returned instead.  If a read-write transaction is requested and a
+ * read-only transaction is open, the read-only transaction will be
+ * closed.
+ *
+ * It is a bug to request a read-write transaction during another
+ * read-write transaction.
+ *
+ * As a result, it is unsafe to retain read-only transactions past the
+ * point where a read-write transaction might be needed.  For
+ * instance, any call that has callbacks outside this module must
+ * conclude all of its reads from the database before calling those
+ * callbacks, or must reacquire the transaction after its callbacks
+ * are completed.
+ */
+int lmdb_transaction_begin_flags(struct strbuf *err, unsigned int flags)
+{
+	int ret;
+	MDB_txn *txn;
+	static size_t last_txnid = 0;
+	int force_restart = 0;
+	MDB_envinfo stat;
+
+	init_env(&env, db_path);
+
+	/*
+	 * Since each transaction sees a consistent view of the db,
+	 * downstream processes that write the db won't be seen in
+	 * this transaction.  We can check if the last transaction id
+	 * has changed since this read transaction was started, and if
+	 * so, we want to reopen the transaction.
+	 */
+
+	mdb_env_info(env, &stat);
+	if (stat.me_last_txnid != last_txnid) {
+		force_restart = 1;
+		last_txnid = stat.me_last_txnid;
+	}
+
+	if (!transaction.txn) {
+		ret = mdb_txn_begin(env, NULL, flags, &txn);
+		if (ret) {
+			strbuf_addf(err, "mdb_txn_begin failed: %s",
+				    mdb_strerror(ret));
+			return -1;
+		}
+		ret = mdb_dbi_open(txn, NULL, 0, &transaction.dbi);
+		if (ret) {
+			strbuf_addf(err, "mdb_txn_open failed: %s",
+				    mdb_strerror(ret));
+			return -1;
+		}
+		transaction.txn = txn;
+		transaction.flags = flags;
+		return 0;
+	}
+
+	if (transaction.flags == flags && !(flags & MDB_RDONLY))
+		die("BUG: rw transaction started during another rw txn");
+
+	if (force_restart || (transaction.flags != flags && transaction.flags & MDB_RDONLY)) {
+		/*
+		 * RO -> RW, or forced restart due to possible changes
+		 * from downstream processes.
+		 */
+		mdb_txn_abort(transaction.txn);
+		ret = mdb_txn_begin(env, NULL, flags, &txn);
+		if (ret) {
+			strbuf_addf(err, "restarting txn: mdb_txn_begin failed: %s",
+				    mdb_strerror(ret));
+			return -1;
+		}
+		ret = mdb_dbi_open(txn, NULL, 0, &transaction.dbi);
+		if (ret) {
+			strbuf_addf(err, "mdb_txn_open failed: %s",
+				    mdb_strerror(ret));
+			return -1;
+		}
+		transaction.txn = txn;
+		transaction.flags = flags;
+	}
+	/* RW -> RO just keeps the RW txn */
+	return 0;
+}
+
+static struct lmdb_transaction *lmdb_transaction_begin_flags_or_die(int flags)
+{
+	struct strbuf err = STRBUF_INIT;
+	if (lmdb_transaction_begin_flags(&err, flags))
+		die("%s", err.buf);
+	return &transaction;
+}
+
+#define MAXDEPTH 5
+
+static const char *parse_ref_data(struct lmdb_transaction *transaction,
+				  const char *refname, const char *ref_data,
+				  unsigned char *sha1, int resolve_flags,
+				  int *flags, int bad_name)
+{
+	int depth = MAXDEPTH;
+	const char *buf;
+	static struct strbuf refname_buffer = STRBUF_INIT;
+	static struct strbuf refdata_buffer = STRBUF_INIT;
+	MDB_val key, val;
+	int needs_free = 0;
+
+	for (;;) {
+		if (--depth < 0)
+			return NULL;
+
+		if (!starts_with(ref_data, "ref:")) {
+			if (get_sha1_hex(ref_data, sha1) ||
+			    (ref_data[40] != '\0' && !isspace(ref_data[40]))) {
+				if (flags)
+					*flags |= REF_ISBROKEN;
+				errno = EINVAL;
+				return NULL;
+			}
+
+			if (bad_name) {
+				hashclr(sha1);
+				if (flags)
+					*flags |= REF_ISBROKEN;
+			} else if (is_null_sha1(sha1)) {
+				if (flags)
+					*flags |= REF_ISBROKEN;
+			}
+			return refname;
+		}
+		if (flags)
+			*flags |= REF_ISSYMREF;
+		buf = ref_data + 4;
+		while (isspace(*buf))
+			buf++;
+		strbuf_reset(&refname_buffer);
+		strbuf_addstr(&refname_buffer, buf);
+		refname = refname_buffer.buf;
+		if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
+			hashclr(sha1);
+			return refname;
+		}
+		if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
+			if (flags)
+				*flags |= REF_ISBROKEN;
+
+			if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
+			    !refname_is_safe(buf)) {
+				errno = EINVAL;
+				return NULL;
+			}
+			bad_name = 1;
+		}
+
+		key.mv_data = (char *)refname;
+		key.mv_size = strlen(refname) + 1;
+		if (mdb_get_or_die(transaction, &key, &val, &needs_free)) {
+			hashclr(sha1);
+			if (bad_name) {
+				if (flags)
+					*flags |= REF_ISBROKEN;
+			}
+			if (resolve_flags & RESOLVE_REF_READING)
+				return NULL;
+
+			return refname;
+		}
+		strbuf_reset(&refdata_buffer);
+		strbuf_add(&refdata_buffer, val.mv_data, val.mv_size);
+		if (needs_free)
+			free(val.mv_data);
+		ref_data = refdata_buffer.buf;
+	}
+	return refname;
+}
+
+static int verify_refname_available_txn(struct lmdb_transaction *transaction,
+					const char *refname,
+					struct string_list *extras,
+					struct string_list *skip,
+					struct strbuf *err)
+{
+	MDB_cursor *cursor;
+	MDB_val key;
+	MDB_val val;
+	int mdb_ret;
+	size_t refname_len;
+	char *search_key;
+	const char *extra_refname;
+	int ret = 1;
+	size_t i;
+
+	mdb_cursor_open_or_die(transaction, &cursor);
+
+	refname_len = strlen(refname) + 2;
+	key.mv_size = refname_len;
+	search_key = xmalloc(refname_len);
+	memcpy(search_key, refname, refname_len - 2);
+	search_key[refname_len - 2] = '/';
+	search_key[refname_len - 1] = 0;
+	key.mv_data = search_key;
+
+	/* Check for subdirs of refname: we start at refname/ */
+	mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET_RANGE);
+
+	while (!mdb_ret) {
+		if (starts_with(key.mv_data, refname) &&
+		    ((char*)key.mv_data)[refname_len - 2] == '/') {
+			if (skip && string_list_has_string(skip, key.mv_data))
+				goto next;
+
+			strbuf_addf(err, "'%s' exists; cannot create '%s'", (char *)key.mv_data, refname);
+			goto done;
+		}
+		break;
+	next:
+		mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_NEXT);
+	}
+
+	/* Check for parent dirs of refname. */
+	for (i = 0; i < refname_len - 2; i++) {
+		if (search_key[i] == '/') {
+			search_key[i] = 0;
+			if (skip && string_list_has_string(skip, search_key)) {
+				search_key[i] = '/';
+				continue;
+			}
+
+			if (extras && string_list_has_string(extras, search_key)) {
+				strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
+					    refname, search_key);
+				goto done;
+			}
+
+			key.mv_data = search_key;
+			key.mv_size = i + 1;
+			if (!mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET)) {
+				strbuf_addf(err, "'%s' exists; cannot create '%s'", (char *)key.mv_data, refname);
+				goto done;
+			}
+			search_key[i] = '/';
+		}
+	}
+
+	extra_refname = find_descendant_ref(refname, extras, skip);
+	if (extra_refname) {
+		strbuf_addf(err,
+			    "cannot process '%s' and '%s' at the same time",
+			    refname, extra_refname);
+		ret = 1;
+	} else {
+		ret = 0;
+	}
+done:
+	mdb_cursor_close(cursor);
+	free(search_key);
+	return ret;
+}
+
+static const char *resolve_ref_unsafe_txn(struct lmdb_transaction *transaction,
+					  const char *refname,
+					  int resolve_flags,
+					  unsigned char *sha1,
+					  int *flags)
+{
+	int bad_name = 0;
+	char *ref_data;
+	struct MDB_val key, val;
+	struct strbuf err = STRBUF_INIT;
+	int needs_free = 0;
+	const char *ret;
+
+	val.mv_size = 0;
+	val.mv_data = NULL;
+
+	if (flags)
+		*flags = 0;
+
+	if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
+		if (flags)
+			*flags |= REF_BAD_NAME;
+
+		if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
+		    !refname_is_safe(refname)) {
+			errno = EINVAL;
+			return NULL;
+		}
+		/*
+		 * dwim_ref() uses REF_ISBROKEN to distinguish between
+		 * missing refs and refs that were present but invalid,
+		 * to complain about the latter to stderr.
+		 *
+		 * We don't know whether the ref exists, so don't set
+		 * REF_ISBROKEN yet.
+		 */
+		bad_name = 1;
+	}
+
+	key.mv_data = (void *)refname;
+	key.mv_size = strlen(refname) + 1;
+	if (mdb_get_or_die(transaction, &key, &val, &needs_free)) {
+		if (bad_name) {
+			hashclr(sha1);
+			if (flags)
+				*flags |= REF_ISBROKEN;
+		}
+
+		if (resolve_flags & RESOLVE_REF_READING)
+			return NULL;
+
+		if (verify_refname_available_txn(transaction, refname, NULL, NULL, &err)) {
+			error("%s", err.buf);
+			strbuf_release(&err);
+			return NULL;
+		}
+
+		hashclr(sha1);
+		return refname;
+	}
+
+	ref_data = val.mv_data;
+	assert(ref_data[val.mv_size - 1] == 0);
+
+	ret = parse_ref_data(transaction, refname, ref_data, sha1,
+			     resolve_flags, flags, bad_name);
+	if (needs_free)
+		free(ref_data);
+	return ret;
+}
+
+static const char *lmdb_resolve_ref_unsafe(const char *refname, int resolve_flags,
+					   unsigned char *sha1, int *flags)
+{
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	return resolve_ref_unsafe_txn(&transaction, refname,
+				      resolve_flags, sha1, flags);
+}
+
+static void write_u64(char *buf, uint64_t number)
+{
+	int i;
+
+	for (i = 0; i < 8; i++)
+		buf[i] = (number >> (i * 8)) & 0xff;
+}
+
+static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
+{
+	unsigned char osha1[20], nsha1[20];
+	char *email_end, *message;
+	unsigned long timestamp;
+	int tz;
+
+	/* old (raw) new (raw) name <email> SP time TAB msg LF */
+	if (sb->len < 41 || sb->buf[sb->len - 1] != '\n' ||
+	    !(email_end = strchr(sb->buf + 40, '>')) ||
+	    email_end[1] != ' ' ||
+	    !(timestamp = strtoul(email_end + 2, &message, 10)) ||
+	    !message || message[0] != ' ' ||
+	    (message[1] != '+' && message[1] != '-') ||
+	    !isdigit(message[2]) || !isdigit(message[3]) ||
+	    !isdigit(message[4]) || !isdigit(message[5]))
+		return 0; /* corrupt? */
+
+	hashcpy(osha1, (const unsigned char *)sb->buf);
+	hashcpy(nsha1, (const unsigned char *)sb->buf + 20);
+
+	email_end[1] = '\0';
+	tz = strtol(message + 1, NULL, 10);
+	if (message[6] != '\t')
+		message += 6;
+	else
+		message += 7;
+	return fn(osha1, nsha1, sb->buf + 40, timestamp, tz, message, cb_data);
+}
+
+static void format_reflog_entry(struct strbuf *buf,
+				const unsigned char *old_sha1,
+				const unsigned char *new_sha1,
+				const char *committer, const char *msg)
+{
+	int len;
+	int msglen;
+
+	assert(buf->len == 0);
+	strbuf_add(buf, old_sha1, 20);
+	strbuf_add(buf, new_sha1, 20);
+	strbuf_addstr(buf, committer);
+	strbuf_addch(buf, '\n');
+
+	len = buf->len;
+	msglen = msg ? strlen(msg) : 0;
+	if (msglen) {
+		int copied;
+		strbuf_grow(buf, msglen + 1);
+		copied = copy_reflog_msg(buf->buf + 40 + strlen(committer), msg) - 1;
+		buf->len = len + copied;
+		buf->buf[buf->len] = 0;
+	}
+}
+
+static int log_ref_write(const char *refname,
+			 const unsigned char *old_sha1,
+			 const unsigned char *new_sha1,
+			 const char *msg,
+			 int flags,
+			 struct strbuf *err)
+{
+	MDB_val key, val;
+	uint64_t now = getnanotime();
+	int result;
+	char *log_key;
+	int refname_len;
+	MDB_cursor *cursor;
+	struct strbuf buf = STRBUF_INIT;
+	const char *timestamp;
+
+	if (log_all_ref_updates < 0)
+		log_all_ref_updates = !is_bare_repository();
+
+	/* it is assumed that we are in a ref transaction here */
+	assert(transaction.txn);
+
+	result = safe_create_reflog(refname, flags & REF_FORCE_CREATE_REFLOG, err);
+	if (result)
+		return result;
+
+	/* "logs/" + refname + \0 + 8-byte timestamp for sorting and expiry. */
+	refname_len = strlen(refname);
+	key.mv_size = refname_len + 14;
+	log_key = xcalloc(1, key.mv_size);
+	sprintf(log_key, "logs/%s", refname);
+	key.mv_data = log_key;
+
+	mdb_cursor_open_or_die(&transaction, &cursor);
+
+	/* if no reflog exists, we're done */
+	if (mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET_RANGE) ||
+	    strcmp(key.mv_data, log_key))
+		goto done;
+
+	/* Is this a header?  We only need the header for empty reflogs */
+	timestamp = (const char *)key.mv_data + refname_len + 6;
+	if (ntohll(*(uint64_t *)timestamp) == 0)
+		mdb_cursor_del_or_die(cursor, 0);
+
+	key.mv_data = log_key;
+
+	write_u64((char *)key.mv_data + refname_len + 6, htonll(now));
+
+	format_reflog_entry(&buf, old_sha1, new_sha1,
+			    git_committer_info(0), msg);
+	assert(buf.len >= 42);
+	val.mv_data = buf.buf;
+	val.mv_size = buf.len + 1;
+
+	mdb_put_or_die(&transaction, &key, &val, 0);
+	strbuf_release(&buf);
+
+done:
+	free(log_key);
+	mdb_cursor_close(cursor);
+	return 0;
+}
+
+static int lmdb_verify_refname_available(const char *refname,
+					 struct string_list *extras,
+					 struct string_list *skip,
+					 struct strbuf *err)
+{
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	return verify_refname_available_txn(&transaction, refname, extras, skip, err);
+}
+
+/*
+ * Attempt to resolve `refname` to `old_sha1` (if old_sha1 is
+ * non-null).  The return value is a pointer to a newly-allocated
+ * string containing the next ref name that this resolves to.  So if
+ * HEAD is a symbolic ref to refs/heads/example, which is itself a
+ * symbolic ref to refs/heads/foo, return refs/heads/example,
+ * and fill in resolved_sha1 with the sha of refs/heads/foo.
+ */
+static char *check_ref(MDB_txn *txn, const char *refname,
+		       const unsigned char *old_sha1,
+		       unsigned char *resolved_sha1, int flags,
+		       int *type_p)
+{
+	int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
+	int resolve_flags = 0;
+	int type;
+	char *resolved_refname;
+
+	if (mustexist)
+		resolve_flags |= RESOLVE_REF_READING;
+	if (flags & REF_DELETING) {
+		resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
+		if (flags & REF_NODEREF)
+			resolve_flags |= RESOLVE_REF_NO_RECURSE;
+	}
+
+	/*
+	 * The first time we resolve the refname, we're just trying to
+	 * see if there is any ref at all by this name, even if it is
+	 * a broken symref.
+	 */
+	refname = resolve_ref_unsafe(refname, resolve_flags,
+				     resolved_sha1, &type);
+	if (type_p)
+	    *type_p = type;
+
+	if (!refname)
+		return NULL;
+
+	/*
+	 * Need to copy refname here because the resolve_ref_unsafe
+	 * returns a pointer to a static buffer that could get mangled
+	 * by the second call.
+	 */
+	resolved_refname = xstrdup(refname);
+
+	if (old_sha1) {
+		if (flags & REF_NODEREF) {
+			resolve_flags &= ~RESOLVE_REF_NO_RECURSE;
+
+			resolve_ref_unsafe(refname, resolve_flags,
+					   resolved_sha1, &type);
+		}
+		if (hashcmp(old_sha1, resolved_sha1)) {
+			error("ref %s is at %s but expected %s", refname,
+			      sha1_to_hex(resolved_sha1), sha1_to_hex(old_sha1));
+
+			return NULL;
+		}
+	}
+	return resolved_refname;
+}
+
+static int mdb_transaction_commit(struct lmdb_transaction *transaction,
+				  struct strbuf *err)
+{
+	int result;
+
+	result = mdb_txn_commit(transaction->txn);
+	if (result && err)
+		strbuf_addstr(err, mdb_strerror(result));
+
+	transaction->txn = NULL;
+	return result;
+}
+
+static int lmdb_delete_reflog(const char *refname)
+{
+	MDB_val key, val;
+	char *log_path;
+	int len;
+	MDB_cursor *cursor;
+	int ret = 0;
+	int mdb_ret;
+	struct strbuf err = STRBUF_INIT;
+	int in_transaction;
+
+	if (ref_type(refname) != REF_TYPE_NORMAL)
+		return refs_be_files.delete_reflog(refname);
+
+	in_transaction = in_write_transaction();
+
+	len = strlen(refname) + 6;
+	log_path = xmalloc(len);
+	sprintf(log_path, "logs/%s", refname);
+
+	key.mv_data = log_path;
+	key.mv_size = len;
+
+	if (!in_transaction)
+		lmdb_transaction_begin_flags_or_die(0);
+
+	mdb_cursor_open_or_die(&transaction, &cursor);
+
+	mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET_RANGE);
+
+	while (!mdb_ret) {
+		if (key.mv_size < len)
+			break;
+
+		if (!starts_with(key.mv_data, log_path) || ((char*)key.mv_data)[len - 1] != 0)
+			break;
+
+		mdb_cursor_del_or_die(cursor, 0);
+		mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_NEXT);
+	}
+
+	free(log_path);
+	mdb_cursor_close(cursor);
+	transaction.cursor = NULL;
+
+	if (!in_transaction && mdb_transaction_commit(&transaction, &err)) {
+		warning("%s", err.buf);
+		ret = 01;
+	}
+	strbuf_release(&err);
+	return ret;
+}
+
+#define REF_NO_REFLOG 0x8000
+
+static int lmdb_transaction_update(const char *refname,
+				   const unsigned char *new_sha1,
+				   const unsigned char *old_sha1,
+				   unsigned int flags, const char *msg,
+				   struct strbuf *err)
+{
+	const char *orig_refname = refname;
+	MDB_val key, val;
+	unsigned char resolved_sha1[20];
+	int type;
+	int ret = -1;
+
+	if ((flags & REF_HAVE_NEW) && is_null_sha1(new_sha1))
+		flags |= REF_DELETING;
+
+	if (new_sha1 && !is_null_sha1(new_sha1) &&
+	    check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
+		strbuf_addf(err, "refusing to update ref with bad name %s",
+			    refname);
+		return TRANSACTION_GENERIC_ERROR;
+	}
+
+	refname = check_ref(transaction.txn, orig_refname, old_sha1,
+			    resolved_sha1, flags, &type);
+	if (refname == NULL) {
+		strbuf_addf(err, "cannot lock the ref '%s'", orig_refname);
+		return TRANSACTION_GENERIC_ERROR;
+	}
+
+	if (!(flags & REF_DELETING) && is_null_sha1(resolved_sha1) &&
+	    verify_refname_available_txn(&transaction, refname, NULL, NULL, err))
+		return TRANSACTION_NAME_CONFLICT;
+
+	if (flags & REF_NODEREF) {
+		free((void *)refname);
+		refname = orig_refname;
+	}
+
+	key.mv_size = strlen(refname) + 1;
+	key.mv_data = (void *)refname;
+
+	if ((flags & REF_HAVE_NEW) && !is_null_sha1(new_sha1)) {
+		int overwriting_symref = ((type & REF_ISSYMREF) &&
+					  (flags & REF_NODEREF));
+
+		struct object *o = parse_object(new_sha1);
+		if (!o) {
+			strbuf_addf(err,
+				    "Trying to write ref %s with nonexistent object %s",
+				    refname, sha1_to_hex(new_sha1));
+			goto done;
+		}
+		if (o->type != OBJ_COMMIT && is_branch(refname)) {
+			strbuf_addf(err,
+				    "Trying to write non-commit object %s to branch %s",
+				    sha1_to_hex(new_sha1), refname);
+			goto done;
+		}
+
+		if (!overwriting_symref
+		    && !hashcmp(resolved_sha1, new_sha1)) {
+			/*
+			 * The reference already has the desired
+			 * value, so we don't need to write it.
+			 */
+			flags |= REF_NO_REFLOG;
+		} else {
+			val.mv_size = 41;
+			if (new_sha1)
+				val.mv_data = sha1_to_hex(new_sha1);
+			else
+				val.mv_data = sha1_to_hex(null_sha1);
+			mdb_put_or_die(&transaction, &key, &val, 0);
+		}
+	}
+
+	if (flags & REF_DELETING) {
+		if (mdb_del_or_die(&transaction, &key, NULL)) {
+			if (old_sha1 && !is_null_sha1(old_sha1)) {
+				strbuf_addf(err, "No such ref %s", refname);
+				ret = TRANSACTION_GENERIC_ERROR;
+				goto done;
+			}
+		}
+		lmdb_delete_reflog(orig_refname);
+	} else if (!(flags & REF_NO_REFLOG)) {
+		if (!new_sha1)
+			new_sha1 = null_sha1;
+		if (log_ref_write(orig_refname, resolved_sha1,
+				  new_sha1, msg, flags, err) < 0)
+			goto done;
+		if (strcmp (refname, orig_refname) &&
+		    log_ref_write(refname, resolved_sha1,
+				  new_sha1, msg, flags, err) < 0)
+			goto done;
+	}
+
+	ret = 0;
+done:
+	if (refname != orig_refname)
+		free((void *) refname);
+	return ret;
+}
+
+static int lmdb_transaction_commit(struct ref_transaction *ref_transaction,
+				   struct string_list *affected_refnames,
+				   struct strbuf *err)
+{
+	int ret = 0, i;
+	int n = ref_transaction->nr;
+	struct ref_update **updates = ref_transaction->updates;
+
+	/*
+	 * We might already be in a write transaction, because some
+	 * lmdb backend functionality is implemented in terms of
+	 * (other stuff) + ref_transaction_commit
+	 */
+	if (!in_write_transaction())
+		lmdb_transaction_begin_flags_or_die(0);
+
+	for (i = 0; i < n; i++) {
+		struct ref_update *update = updates[i];
+
+		if (lmdb_transaction_update(update->refname,
+					    update->new_sha1,
+					    (update->flags & REF_HAVE_OLD) ?
+					     update->old_sha1 : NULL,
+					    update->flags,
+					    update->msg,
+					    err)) {
+			mdb_txn_abort(transaction.txn);
+			ret = -1;
+			goto cleanup;
+		}
+
+	}
+	ret = mdb_transaction_commit(&transaction, err);
+
+cleanup:
+	ref_transaction->state = REF_TRANSACTION_CLOSED;
+	return ret;
+}
+
+static int rename_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
+			     const char *email, unsigned long timestamp, int tz,
+			     const char *message, void *cb_data)
+{
+
+	const char *newrefname = cb_data;
+	MDB_val key, new_key, val;
+
+	assert(transaction.cursor);
+
+	if (mdb_cursor_get_or_die(transaction.cursor, &key, &val, MDB_GET_CURRENT))
+		die("renaming ref: mdb_cursor_get failed to get current");
+
+	new_key.mv_size = strlen(newrefname) + 5 + 1 + 8;
+	new_key.mv_data = xmalloc(new_key.mv_size);
+	strcpy(new_key.mv_data, "logs/");
+	strcpy((char *)new_key.mv_data + 5, newrefname);
+	memcpy((char *)new_key.mv_data + new_key.mv_size - 8,
+	       (const char *)key.mv_data + key.mv_size - 8, 8);
+	mdb_put_or_die(&transaction, &new_key, &val, 0);
+	mdb_cursor_del_or_die(transaction.cursor, 0);
+	free(new_key.mv_data);
+	return 0;
+}
+
+static int lmdb_rename_ref(const char *oldref, const char *newref, const char *logmsg)
+{
+	unsigned char orig_sha1[20];
+	int flag = 0;
+	int log = reflog_exists(oldref);
+	const char *symref = NULL;
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *ref_transaction;
+
+	if (!strcmp(oldref, newref))
+		return 0;
+
+	lmdb_transaction_begin_flags_or_die(0);
+
+	ref_transaction = ref_transaction_begin(&err);
+	if (!ref_transaction)
+		die("%s", err.buf);
+
+	symref = resolve_ref_unsafe(oldref, RESOLVE_REF_READING,
+				    orig_sha1, &flag);
+	if (flag & REF_ISSYMREF) {
+		error("refname %s is a symbolic ref, renaming it is not supported",
+		      oldref);
+		goto fail;
+	}
+	if (!symref) {
+		mdb_txn_abort(transaction.txn);
+		error("refname %s not found", oldref);
+		goto fail;
+	}
+	if (!rename_ref_available(oldref, newref))
+		goto fail;
+
+	/* Copy the reflog from the old to the new */
+	if (log) {
+		struct strbuf old_log_sentinel = STRBUF_INIT;
+		MDB_val key;
+		int log_all;
+
+		log_all = log_all_ref_updates;
+		log_all_ref_updates = 1;
+		if (safe_create_reflog(newref, 0, &err)) {
+			error("can't create reflog for %s: %s", newref, err.buf);
+			strbuf_release(&err);
+			goto fail;
+		}
+		log_all_ref_updates = log_all;
+
+		for_each_reflog_ent(oldref, rename_reflog_ent, (void *)newref);
+		strbuf_addf(&old_log_sentinel, "logs/%sxxxxxxxx", oldref);
+		memset(old_log_sentinel.buf + old_log_sentinel.len - 8, 0, 8);
+
+		key.mv_size = old_log_sentinel.len;
+		key.mv_data = old_log_sentinel.buf;
+
+		/* It's OK if the old reflog is missing */
+		mdb_del_or_die(&transaction, &key, NULL);
+		strbuf_release(&old_log_sentinel);
+	}
+
+	if (ref_transaction_delete(ref_transaction, oldref,
+				   orig_sha1, REF_NODEREF, NULL, &err)) {
+		error("unable to delete old %s", oldref);
+		goto fail;
+	}
+
+	if (ref_transaction_update(ref_transaction, newref, orig_sha1, NULL,
+				    0, logmsg, &err)) {
+		error("%s", err.buf);
+		goto fail;
+	}
+
+	if (ref_transaction_commit(ref_transaction, &err)) {
+		error("%s", err.buf);
+		goto fail;
+	}
+
+	return 0;
+
+fail:
+	ref_transaction_free(ref_transaction);
+	strbuf_release(&err);
+	mdb_txn_abort(transaction.txn);
+	return 1;
+}
+
+static int lmdb_delete_refs(struct string_list *refnames)
+{
+	int i;
+	struct strbuf err = STRBUF_INIT;
+	int result = 0;
+
+	if (!refnames->nr)
+		return 0;
+
+	lmdb_transaction_begin_flags_or_die(0);
+
+	for (i = 0; i < refnames->nr; i++) {
+		const char *refname = refnames->items[0].string;
+
+		if (lmdb_transaction_update(refname, null_sha1, NULL,
+					    0, NULL, &err))
+			result |= error(_("could not remove reference %s: %s"),
+					refname, err.buf);
+	}
+
+	result |= mdb_transaction_commit(&transaction, &err);
+	strbuf_release(&err);
+	return 0;
+}
+
+static int lmdb_for_each_reflog_ent_order(const char *refname,
+					  each_reflog_ent_fn fn,
+					  void *cb_data, int reverse)
+{
+	MDB_val key, val;
+	char *search_key;
+	char *log_path;
+	int len;
+	MDB_cursor *cursor;
+	int ret = 0;
+	struct strbuf sb = STRBUF_INIT;
+	enum MDB_cursor_op direction = reverse ? MDB_PREV : MDB_NEXT;
+	uint64_t zero = 0ULL;
+
+	len = strlen(refname) + 6;
+	log_path = xmalloc(len);
+	search_key = xmalloc(len + 1);
+	sprintf(log_path, "logs/%s", refname);
+	strcpy(search_key, log_path);
+
+	if (reverse) {
+		/*
+		 * For a reverse search, start at the key
+		 * lexicographically after the searched-for key.
+		 * That's the one with \001 appended to the key.
+		 */
+
+		search_key[len - 1] = 1;
+		search_key[len] = 0;
+		key.mv_size = len + 1;
+	} else {
+		key.mv_size = len;
+	}
+
+	key.mv_data = search_key;
+
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+
+	mdb_cursor_open_or_die(&transaction, &cursor);
+
+	transaction.cursor = cursor;
+
+	/*
+	 * MDB's cursor API requires that the first mdb_cursor_get be
+	 * called with MDB_SET_RANGE.  For reverse searches, this will
+	 * give us the entry one-past the entry we're looking for, so
+	 * we should jump back using MDB_PREV.
+	 */
+	mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET_RANGE);
+	if (direction == MDB_PREV)
+		mdb_cursor_get_or_die(cursor, &key, &val, direction);
+
+	do {
+		if (key.mv_size < len)
+			break;
+
+		if (!starts_with(key.mv_data, log_path) || ((char *)key.mv_data)[len - 1] != 0)
+			break;
+
+		if (!memcmp(&zero, ((char *)key.mv_data) + key.mv_size - 8, 8))
+			continue;
+
+		assert(val.mv_size != 0);
+
+		strbuf_add(&sb, val.mv_data, val.mv_size - 1);
+		ret = show_one_reflog_ent(&sb, fn, cb_data);
+		if (ret)
+			break;
+
+		strbuf_reset(&sb);
+	} while (!mdb_cursor_get_or_die(cursor, &key, &val, direction));
+
+	strbuf_release(&sb);
+	free(log_path);
+	free(search_key);
+	mdb_cursor_close(cursor);
+	return ret;
+}
+
+static int lmdb_for_each_reflog_ent(const char *refname,
+				    each_reflog_ent_fn fn,
+				    void *cb_data)
+{
+	if (ref_type(refname) != REF_TYPE_NORMAL)
+		return refs_be_files.for_each_reflog_ent(refname, fn, cb_data);
+	return lmdb_for_each_reflog_ent_order(refname, fn, cb_data, 0);
+}
+
+static int lmdb_for_each_reflog_ent_reverse(const char *refname,
+					    each_reflog_ent_fn fn,
+					    void *cb_data)
+{
+	if (ref_type(refname) != REF_TYPE_NORMAL)
+		return refs_be_files.for_each_reflog_ent_reverse(refname, fn, cb_data);
+	return lmdb_for_each_reflog_ent_order(refname, fn, cb_data, 1);
+}
+
+static int lmdb_reflog_exists(const char *refname)
+{
+	MDB_val key, val;
+	char *log_path;
+	int len;
+	MDB_cursor *cursor;
+	int ret = 1;
+
+	if (ref_type(refname) != REF_TYPE_NORMAL)
+		return refs_be_files.reflog_exists(refname);
+
+	len = strlen(refname) + 6;
+	log_path = xmalloc(len);
+	sprintf(log_path, "logs/%s", refname);
+
+	key.mv_data = log_path;
+	key.mv_size = len;
+
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	mdb_cursor_open_or_die(&transaction, &cursor);
+
+	if (mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET_RANGE) ||
+	    !starts_with(key.mv_data, log_path))
+		ret = 0;
+
+	free(log_path);
+	mdb_cursor_close(cursor);
+
+	return ret;
+}
+
+struct wrapped_each_ref_fn {
+	each_ref_fn *fn;
+	void *cb_data;
+};
+
+static int check_reflog(const char *refname,
+			const struct object_id *oid, int flags, void *cb_data)
+{
+	struct wrapped_each_ref_fn *wrapped = cb_data;
+
+	if (reflog_exists(refname))
+		return wrapped->fn(refname, oid, 0, wrapped->cb_data);
+
+	return 0;
+}
+
+static int lmdb_for_each_reflog(each_ref_fn fn, void *cb_data)
+{
+	struct wrapped_each_ref_fn wrapped = {fn, cb_data};
+	int result = head_ref(fn, cb_data);
+	if (result)
+		return result;
+	return for_each_ref(check_reflog, &wrapped);
+}
+
+static int lmdb_create_reflog(const char *refname, int force_create, struct strbuf *err)
+{
+	/*
+	 * We mark that there is a reflog by creating a key of the
+	 * form logs/$refname followed by nine \0 (one for
+	 * string-termination, 8 in lieu of a timestamp), with an empty
+	 * value.
+	 */
+
+	int in_transaction = in_write_transaction();
+	MDB_val key, val;
+
+	if (!force_create && !should_autocreate_reflog(refname))
+		return 0;
+
+	if (!in_transaction)
+		lmdb_transaction_begin_flags_or_die(0);
+
+	key.mv_size = strlen(refname) + 5 + 1 + 8;
+	key.mv_data = xcalloc(1, key.mv_size);
+	sprintf((char *)key.mv_data, "logs/%s", refname);
+	val.mv_size = 0;
+	val.mv_data = NULL;
+	mdb_put_or_die(&transaction, &key, &val, 0);
+
+	free(key.mv_data);
+	if (!in_transaction)
+		return mdb_transaction_commit(&transaction, err);
+	return 0;
+}
+
+struct expire_reflog_cb {
+	unsigned int flags;
+	reflog_expiry_should_prune_fn *should_prune_fn;
+	void *policy_cb;
+	unsigned char last_kept_sha1[20];
+};
+
+static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
+			     const char *email, unsigned long timestamp, int tz,
+			     const char *message, void *cb_data)
+{
+	struct expire_reflog_cb *cb = cb_data;
+	struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
+
+	if (cb->flags & EXPIRE_REFLOGS_REWRITE)
+		osha1 = cb->last_kept_sha1;
+
+	if ((*cb->should_prune_fn)(osha1, nsha1, email, timestamp, tz,
+				   message, policy_cb)) {
+		if (cb->flags & EXPIRE_REFLOGS_DRY_RUN)
+			printf("would prune %s", message);
+		else {
+			if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
+				printf("prune %s", message);
+
+			mdb_cursor_del_or_die(transaction.cursor, 0);
+		}
+	} else {
+		hashcpy(cb->last_kept_sha1, nsha1);
+		if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
+			printf("keep %s", message);
+	}
+	return 0;
+}
+
+static int write_ref(const char *refname, const unsigned char *sha1)
+{
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *transaction;
+
+	transaction = ref_transaction_begin(&err);
+	if (!transaction) {
+		error("%s", err.buf);
+		strbuf_release(&err);
+		return -1;
+	}
+
+	if (ref_transaction_update(transaction, refname, sha1, NULL,
+				   REF_NO_REFLOG, NULL, &err)) {
+		error("%s", err.buf);
+		strbuf_release(&err);
+		return -1;
+	}
+
+	if (ref_transaction_commit(transaction, &err)) {
+		error("%s", err.buf);
+		strbuf_release(&err);
+		return -1;
+	}
+
+	return 0;
+}
+
+int lmdb_reflog_expire(const char *refname, const unsigned char *sha1,
+		       unsigned int flags,
+		       reflog_expiry_prepare_fn prepare_fn,
+		       reflog_expiry_should_prune_fn should_prune_fn,
+		       reflog_expiry_cleanup_fn cleanup_fn,
+		       void *policy_cb_data)
+{
+	struct expire_reflog_cb cb;
+	int dry_run = flags & EXPIRE_REFLOGS_DRY_RUN;
+	int status = 0;
+	struct strbuf err = STRBUF_INIT;
+	unsigned char resolved_sha1[20];
+	int type;
+	char *resolved;
+
+	if (ref_type(refname) != REF_TYPE_NORMAL)
+		return refs_be_files.reflog_expire(refname, sha1, flags, prepare_fn,
+					       should_prune_fn, cleanup_fn,
+					       policy_cb_data);
+
+	memset(&cb, 0, sizeof(cb));
+	cb.flags = flags;
+	cb.policy_cb = policy_cb_data;
+	cb.should_prune_fn = should_prune_fn;
+
+	lmdb_transaction_begin_flags_or_die(dry_run ? MDB_RDONLY : 0);
+
+	resolved = check_ref(transaction.txn, refname, sha1,
+			     resolved_sha1, 0, &type);
+	if (!resolved)
+		die("Failed to resolve %s", refname);
+	free(resolved);
+
+	(*prepare_fn)(refname, sha1, cb.policy_cb);
+	lmdb_for_each_reflog_ent(refname, expire_reflog_ent, &cb);
+	(*cleanup_fn)(cb.policy_cb);
+
+	if (!dry_run) {
+		/*
+		 * It doesn't make sense to adjust a reference pointed
+		 * to by a symbolic ref based on expiring entries in
+		 * the symbolic reference's reflog. Nor can we update
+		 * a reference if there are no remaining reflog
+		 * entries.
+		 */
+		int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
+			!(type & REF_ISSYMREF) &&
+			!is_null_sha1(cb.last_kept_sha1);
+
+		if (mdb_transaction_commit(&transaction, &err)) {
+			status |= error("couldn't write logs/%s: %s", refname,
+					err.buf);
+			strbuf_release(&err);
+		} else if (update &&
+			   write_ref(refname, cb.last_kept_sha1)) {
+			status |= error("couldn't set %s",
+					refname);
+		}
+	}
+	return status;
+}
+
+static int lmdb_pack_refs(unsigned int flags)
+{
+	/* This concept does not exist in this backend. */
+	return 0;
+}
+
+static int lmdb_peel_ref(const char *refname, unsigned char *sha1)
+{
+	int flag;
+	unsigned char base[20];
+
+	if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag))
+		return -1;
+
+	return peel_object(base, sha1);
+}
+
+static int lmdb_create_symref(const char *ref_target,
+			      const char *refs_heads_master,
+			      const char *logmsg)
+{
+
+	struct strbuf err = STRBUF_INIT;
+	unsigned char old_sha1[20], new_sha1[20];
+	MDB_val key, val;
+	char *valdata;
+	int ret = 0;
+	int in_transaction;
+
+	in_transaction = in_write_transaction();
+
+	if (logmsg && read_ref(ref_target, old_sha1))
+		hashclr(old_sha1);
+
+	key.mv_size = strlen(ref_target) + 1;
+	key.mv_data = xstrdup(ref_target);
+
+	val.mv_size = strlen(refs_heads_master) + 1 + 5;
+	valdata = xmalloc(val.mv_size);
+	sprintf(valdata, "ref: %s", refs_heads_master);
+	val.mv_data = valdata;
+
+	if (!in_transaction)
+		lmdb_transaction_begin_flags_or_die(0);
+
+	mdb_put_or_die(&transaction, &key, &val, 0);
+
+	if (logmsg && !read_ref(refs_heads_master, new_sha1) &&
+	    log_ref_write(ref_target, old_sha1, new_sha1, logmsg, 0, &err)) {
+		error("create_symref: log_ref_write failed: %s", err.buf);
+		ret = -1;
+		goto done;
+	}
+
+	if (!in_transaction && mdb_transaction_commit(&transaction, &err)) {
+		error("create_symref: commit failed: %s", err.buf);
+		ret = -1;
+	}
+
+done:
+	strbuf_release(&err);
+	free(key.mv_data);
+	free(valdata);
+
+	return ret;
+}
+
+MDB_env *submodule_txn_begin(struct lmdb_transaction *transaction)
+{
+	int ret;
+	MDB_env *submodule_env = NULL;
+	struct strbuf path = STRBUF_INIT;
+
+	strbuf_git_path_submodule(&path, transaction->submodule, "refdb");
+
+	if (!is_directory(path.buf))
+		goto done;
+
+	mkdir(path.buf, 0775);
+
+	init_env(&submodule_env, path.buf);
+
+	ret = mdb_txn_begin(submodule_env, NULL, MDB_RDONLY, &transaction->txn);
+	if (ret)
+		die("mdb_txn_begin failed: %s", mdb_strerror(ret));
+
+	ret = mdb_dbi_open(transaction->txn, NULL, 0, &transaction->dbi);
+	if (ret)
+		die("mdb_txn_open failed: %s", mdb_strerror(ret));
+
+done:
+	strbuf_release(&path);
+	return submodule_env;
+}
+
+static int lmdb_resolve_gitlink_ref(const char *submodule, const char *refname,
+				    unsigned char *sha1)
+{
+	struct lmdb_transaction transaction;
+	MDB_env *submodule_env;
+	int result;
+
+	transaction.txn = NULL;
+	transaction.submodule = submodule;
+	submodule_env = submodule_txn_begin(&transaction);
+	if (!submodule_env)
+		return -1;
+	result = !resolve_ref_unsafe_txn(&transaction, refname,
+					 RESOLVE_REF_READING, sha1, NULL);
+
+	mdb_txn_abort(transaction.txn);
+	mdb_env_close(submodule_env);
+	return result ? -1 : 0;
+}
+
+static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
+{
+	struct object_id oid;
+	int flag;
+
+	if (submodule) {
+		if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
+			return fn("HEAD", &oid, 0, cb_data);
+
+		return 0;
+	}
+
+	if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
+		return fn("HEAD", &oid, flag, cb_data);
+
+	return 0;
+}
+
+static int lmdb_head_ref(each_ref_fn fn, void *cb_data)
+{
+	return do_head_ref(NULL, fn, cb_data);
+}
+
+static int lmdb_head_ref_submodule(const char *submodule, each_ref_fn fn,
+				   void *cb_data)
+{
+	return do_head_ref(submodule, fn, cb_data);
+}
+
+/*
+ * Call fn for each reference for which the refname begins with base.
+ * If trim is non-zero, then trim that many characters off the
+ * beginning of each refname before passing the refname to fn.  flags
+ * can be DO_FOR_EACH_INCLUDE_BROKEN to include broken references in
+ * the iteration.  If fn ever returns a non-zero value, stop the
+ * iteration and return that value; otherwise, return 0.
+ */
+static int do_for_each_ref(struct lmdb_transaction *transaction,
+			   const char *base, each_ref_fn fn, int trim,
+			   int flags, void *cb_data)
+{
+
+	MDB_val key, val;
+	MDB_cursor *cursor;
+	int baselen;
+	char *search_key;
+	int retval;
+	int mdb_ret;
+
+	retval = do_for_each_per_worktree_ref(transaction->submodule, base, fn,
+					      trim, flags, cb_data);
+	if (retval)
+		return retval;
+
+	if (ref_paranoia < 0)
+		ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
+	if (ref_paranoia)
+		flags |= DO_FOR_EACH_INCLUDE_BROKEN;
+
+	if (!base || !*base) {
+		base = "refs/";
+		trim = 0;
+	}
+
+	baselen = strlen(base);
+	search_key = xmalloc(baselen + 1);
+	strcpy(search_key, base);
+	key.mv_size = baselen + 1;
+	key.mv_data = search_key;
+
+	mdb_cursor_open_or_die(transaction, &cursor);
+
+	mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET_RANGE);
+
+	while (!mdb_ret) {
+		struct object_id oid;
+		int parsed_flags = 0;
+
+		if (memcmp(key.mv_data, base, baselen))
+			break;
+
+		parse_ref_data(transaction, (const char *)key.mv_data + (trim ? baselen : 0),
+			       val.mv_data, oid.hash, 0, &parsed_flags, 0);
+
+		if (flags & DO_FOR_EACH_INCLUDE_BROKEN ||
+		    (!(parsed_flags & REF_ISBROKEN) &&
+		     has_sha1_file(oid.hash))) {
+			retval = fn((const char *)key.mv_data + (trim ? baselen : 0), &oid, parsed_flags, cb_data);
+			if (retval)
+				break;
+		}
+
+		mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_NEXT);
+	}
+
+	mdb_cursor_close(cursor);
+	free(search_key);
+
+	return retval;
+}
+
+static int lmdb_for_each_ref(each_ref_fn fn, void *cb_data)
+{
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	return do_for_each_ref(&transaction, "", fn, 0, 0, cb_data);
+}
+
+static int lmdb_for_each_ref_submodule(const char *submodule, each_ref_fn fn,
+				       void *cb_data)
+{
+	struct lmdb_transaction transaction;
+	MDB_env *submodule_env;
+	int result;
+
+	if (!submodule)
+		return for_each_ref(fn, cb_data);
+
+	transaction.txn = NULL;
+	transaction.submodule = submodule;
+
+	submodule_env = submodule_txn_begin(&transaction);
+	if (!submodule_env)
+		return 0;
+	result = do_for_each_ref(&transaction, "", fn, 0, 0, cb_data);
+	mdb_txn_abort(transaction.txn);
+	mdb_env_close(submodule_env);
+	return result;
+}
+
+static int lmdb_for_each_ref_in(const char *prefix, each_ref_fn fn,
+				 void *cb_data)
+{
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	return do_for_each_ref(&transaction, prefix, fn, strlen(prefix),
+			       0, cb_data);
+}
+
+static int lmdb_for_each_fullref_in(const char *prefix, each_ref_fn fn,
+				    void *cb_data, unsigned int broken)
+{
+	unsigned int flag = 0;
+
+	if (broken)
+		flag = DO_FOR_EACH_INCLUDE_BROKEN;
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	return do_for_each_ref(&transaction, prefix, fn, 0, flag, cb_data);
+}
+
+static int lmdb_for_each_ref_in_submodule(const char *submodule,
+					  const char *prefix,
+					  each_ref_fn fn, void *cb_data)
+{
+	struct lmdb_transaction transaction = {NULL};
+	MDB_env *submodule_env;
+	int result;
+
+	if (!submodule)
+		return for_each_ref_in(prefix, fn, cb_data);
+
+	transaction.submodule = submodule;
+	submodule_env = submodule_txn_begin(&transaction);
+	if (!submodule_env)
+		return 0;
+	result = do_for_each_ref(&transaction, prefix, fn,
+				 strlen(prefix), 0, cb_data);
+	mdb_txn_abort(transaction.txn);
+	mdb_env_close(submodule_env);
+	return result;
+}
+
+static int lmdb_for_each_replace_ref(each_ref_fn fn, void *cb_data)
+{
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	return do_for_each_ref(&transaction, git_replace_ref_base, fn,
+			       strlen(git_replace_ref_base), 0, cb_data);
+}
+
+static int lmdb_for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
+{
+	struct strbuf buf = STRBUF_INIT;
+	int ret;
+
+	strbuf_addf(&buf, "%srefs/", get_git_namespace());
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	ret = do_for_each_ref(&transaction, buf.buf, fn, 0, 0, cb_data);
+	strbuf_release(&buf);
+	return ret;
+}
+
+static int lmdb_for_each_rawref(each_ref_fn fn, void *cb_data)
+{
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	return do_for_each_ref(&transaction, "", fn, 0,
+			       DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
+}
+
+/* For testing only! */
+int test_refdb_raw_read(const char *key)
+{
+	MDB_val key_val, val;
+	char *keydup;
+	int ret;
+	int needs_free = 0;
+
+	lmdb_transaction_begin_flags_or_die(MDB_RDONLY);
+	keydup = xstrdup(key);
+	key_val.mv_data = keydup;
+	key_val.mv_size = strlen(key) + 1;
+
+	ret = mdb_get_or_die(&transaction, &key_val, &val, &needs_free);
+	free(keydup);
+	switch (ret) {
+	case 0:
+		printf("%s\n", (char *)val.mv_data);
+		return 0;
+	case MDB_NOTFOUND:
+		fprintf(stderr, "%s not found\n", key);
+		return 1;
+	default:
+		return 2;
+	}
+	if (needs_free)
+		free(val.mv_data);
+}
+
+/* For testing only! */
+void test_refdb_raw_write(const char *key, const char *value)
+{
+	MDB_val key_val, val;
+	char *keydup, *valdup;
+
+	if (ref_type(key) != REF_TYPE_NORMAL) {
+		val.mv_data = (void *)value;
+		val.mv_size = strlen(value) + 1;
+		write_per_worktree_ref(NULL, key, &val);
+		return;
+	}
+
+	lmdb_transaction_begin_flags_or_die(0);
+
+	keydup = xstrdup(key);
+	key_val.mv_data = keydup;
+	key_val.mv_size = strlen(key) + 1;
+
+	valdup = xstrdup(value);
+	val.mv_data = valdup;
+	val.mv_size = strlen(value) + 1;
+
+	mdb_put_or_die(&transaction, &key_val, &val, 0);
+	assert(mdb_transaction_commit(&transaction, NULL) == 0);
+
+	free(keydup);
+	free(valdup);
+}
+
+/* For testing only! */
+int test_refdb_raw_delete(const char *key)
+{
+	MDB_val key_val;
+	char *keydup;
+	int ret;
+
+	if (ref_type(key) != REF_TYPE_NORMAL)
+		return del_per_worktree_ref(NULL, key, NULL);
+
+	lmdb_transaction_begin_flags_or_die(0);
+	keydup = xstrdup(key);
+	key_val.mv_data = keydup;
+	key_val.mv_size = strlen(key) + 1;
+
+	ret = mdb_del_or_die(&transaction, &key_val, NULL);
+
+	assert(mdb_transaction_commit(&transaction, NULL) == 0);
+
+	free(keydup);
+	return ret;
+}
+
+static int print_raw_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
+				const char *email, unsigned long timestamp,
+				int tz, const char *message, void *cb_data)
+{
+	int *any = cb_data;
+	*any = 1;
+
+	if (*message != '\n')
+		printf("%s %s %s %lu %+05d\t%s", sha1_to_hex(osha1),
+		       sha1_to_hex(nsha1),
+		       email, timestamp, tz, message);
+	else
+		printf("%s %s %s %lu %+05d\n", sha1_to_hex(osha1),
+		       sha1_to_hex(nsha1),
+		       email, timestamp, tz);
+	return 0;
+}
+
+/* For testing only! */
+int test_refdb_raw_reflog(const char *refname)
+{
+	int any = 0;
+
+	for_each_reflog_ent(refname, print_raw_reflog_ent, &any);
+
+	return !any;
+}
+
+/* For testing only! */
+void test_refdb_raw_delete_reflog(char *refname)
+{
+	MDB_val key, val;
+	int mdb_ret;
+	char *search_key;
+	MDB_cursor *cursor;
+	int len;
+
+	if (refname) {
+		len = strlen(refname) + 5 + 1; /* logs/ + 0*/
+		search_key = xmalloc(len);
+		sprintf(search_key, "logs/%s", refname);
+	} else {
+		len = 6; /* logs/ + 0*/
+		search_key = xstrdup("logs/");
+	}
+	key.mv_data = search_key;
+	key.mv_size = len;
+
+	lmdb_transaction_begin_flags_or_die(0);
+
+	mdb_cursor_open_or_die(&transaction, &cursor);
+
+	mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_SET_RANGE);
+	while (!mdb_ret) {
+		if (!starts_with(key.mv_data, search_key))
+			break;
+		if (refname && ((char *)val.mv_data)[len - 1] == 0)
+			break;
+
+		mdb_cursor_del_or_die(cursor, 0);
+		mdb_ret = mdb_cursor_get_or_die(cursor, &key, &val, MDB_NEXT);
+	}
+
+	free(search_key);
+	mdb_cursor_close(cursor);
+
+	assert(mdb_transaction_commit(&transaction, NULL) == 0);
+	return;
+}
+
+static void format_lmdb_reflog_ent(struct strbuf *dst, struct strbuf *src)
+{
+	unsigned char osha1[20], nsha1[20];
+	const char *msg;
+
+	get_sha1_hex(src->buf, osha1);
+	get_sha1_hex(src->buf + 41, nsha1);
+
+	msg = strchr(src->buf + 82, '\n');
+	assert(msg);
+	msg += 1;
+
+	format_reflog_entry(dst, osha1, nsha1, src->buf + 82, msg);
+}
+
+/* For testing only! */
+void test_refdb_raw_append_reflog(const char *refname)
+{
+	struct strbuf input = STRBUF_INIT;
+	struct strbuf sb = STRBUF_INIT;
+	uint64_t now = getnanotime();
+	MDB_val key, val;
+
+	key.mv_size = strlen(refname) + 14;
+	key.mv_data = xcalloc(1, key.mv_size);
+	sprintf(key.mv_data, "logs/%s", refname);
+
+	lmdb_transaction_begin_flags_or_die(0);
+
+	/* We do not remove the header here, because this is just for
+	 * tests, so it's OK to be a bit inefficient */
+
+	while (strbuf_getwholeline(&input, stdin, '\n') != EOF) {
+		/* "logs/" + \0 + 8-byte timestamp for sorting and expiry */
+		write_u64((char *)key.mv_data + key.mv_size - 8, htonll(now++));
+
+		/*
+		 * Convert the input from files-reflog format to
+		 * lmdb-reflog-format
+		 */
+
+		format_lmdb_reflog_ent(&sb, &input);
+		val.mv_data = sb.buf;
+		val.mv_size = sb.len + 1;
+		mdb_put_or_die(&transaction, &key, &val, 0);
+		strbuf_reset(&sb);
+		input.len = 0;
+	}
+
+	strbuf_release(&input);
+	strbuf_release(&sb);
+	assert(mdb_transaction_commit(&transaction, NULL) == 0);
+	free(key.mv_data);
+}
+
+struct ref_be refs_be_lmdb = {
+	NULL,
+	"lmdb",
+	lmdb_init_backend,
+	lmdb_init_db,
+	lmdb_transaction_commit,
+	lmdb_transaction_commit, /* initial commit */
+
+	lmdb_for_each_reflog_ent,
+	lmdb_for_each_reflog_ent_reverse,
+	lmdb_for_each_reflog,
+	lmdb_reflog_exists,
+	lmdb_create_reflog,
+	lmdb_delete_reflog,
+	lmdb_reflog_expire,
+
+	lmdb_pack_refs,
+	lmdb_peel_ref,
+	lmdb_create_symref,
+	lmdb_delete_refs,
+	lmdb_rename_ref,
+
+	lmdb_resolve_ref_unsafe,
+	lmdb_verify_refname_available,
+	lmdb_resolve_gitlink_ref,
+
+	lmdb_head_ref,
+	lmdb_head_ref_submodule,
+	lmdb_for_each_ref,
+	lmdb_for_each_ref_submodule,
+	lmdb_for_each_ref_in,
+	lmdb_for_each_fullref_in,
+	lmdb_for_each_ref_in_submodule,
+	lmdb_for_each_rawref,
+	lmdb_for_each_namespaced_ref,
+	lmdb_for_each_replace_ref,
+};
diff --git a/setup.c b/setup.c
index de6b8ac..9724c0b 100644
--- a/setup.c
+++ b/setup.c
@@ -279,10 +279,11 @@ int refdb_config(const char *var, const char *value, void *ptr)
  *
  *  - either an objects/ directory _or_ the proper
  *    GIT_OBJECT_DIRECTORY environment variable
- *  - a refs/ directory
- *  - either a HEAD symlink or a HEAD file that is formatted as
- *    a proper "ref:", or a regular file HEAD that has a properly
- *    formatted sha1 object name.
+ *  - a refdb/ directory or
+ *    - a refs/ directory
+ *    - either a HEAD symlink or a HEAD file that is formatted as
+ *      a proper "ref:", or a regular file HEAD that has a properly
+ *      formatted sha1 object name.
  */
 int is_git_directory(const char *suspect)
 {
@@ -313,8 +314,13 @@ int is_git_directory(const char *suspect)
 
 	strbuf_setlen(&path, len);
 	strbuf_addstr(&path, "/refs");
-	if (access(path.buf, X_OK))
-		goto done;
+
+	if (access(path.buf, X_OK)) {
+		strbuf_setlen(&path, len);
+		strbuf_addstr(&path, "/refdb");
+		if (access(path.buf, X_OK))
+			goto done;
+	}
 
 	ret = 1;
 done:
@@ -383,6 +389,10 @@ static int check_repo_format(const char *var, const char *value, void *cb)
 			;
 		else if (!strcmp(ext, "preciousobjects"))
 			repository_format_precious_objects = git_config_bool(var, value);
+#ifdef USE_LIBLMDB
+		else if (!strcmp(ext, "refbackend") && !strcmp(value, "lmdb"))
+			;
+#endif
 		else
 			string_list_append(&unknown_extensions, ext);
 	}
diff --git a/test-refs-lmdb-backend.c b/test-refs-lmdb-backend.c
new file mode 100644
index 0000000..022cd21
--- /dev/null
+++ b/test-refs-lmdb-backend.c
@@ -0,0 +1,68 @@
+#include "cache.h"
+#include "string-list.h"
+#include "parse-options.h"
+#include "refs.h"
+
+static const char * const test_refs_be_lmdb_usage[] = {
+	"git test-refs-lmdb-backend <key>",
+	"git test-refs-lmdb-backend <key> <value>",
+	NULL,
+};
+
+int test_refdb_raw_read(const char *key);
+void test_refdb_raw_write(const char *key, const char *value);
+int test_refdb_raw_reflog(const char *refname);
+int test_refdb_raw_delete(const char *key);
+void test_refdb_raw_delete_reflog(const char *refname);
+void test_refdb_raw_append_reflog(const char *refname);
+
+int main(int argc, const char **argv)
+{
+	const char *delete = NULL;
+	const char *reflog = NULL;
+	const char *append_reflog = NULL;
+	int delete_missing_error = 0;
+	int clear_reflog = 0;
+	struct refdb_config_data config_data = {NULL};
+
+	struct option options[] = {
+		OPT_STRING('d', NULL, &delete, "branch", "delete refdb entry"),
+		OPT_STRING('l', NULL, &reflog, "branch", "show reflog"),
+		OPT_STRING('a', NULL, &append_reflog, "branch", "append to reflog"),
+		OPT_BOOL('c', NULL, &clear_reflog, "delete reflog. If a branch is provided, the reflog for that branch will be deleted; else all reflogs will be deleted."),
+		OPT_BOOL('x', NULL, &delete_missing_error,
+			 "deleting a missing key is an error"),
+		OPT_END(),
+	};
+
+	argc = parse_options(argc, argv, "", options, test_refs_be_lmdb_usage,
+			     0);
+
+	if (!append_reflog && !clear_reflog && !delete && !reflog && argc != 1 && argc != 2)
+		usage_with_options(test_refs_be_lmdb_usage,
+				   options);
+
+	git_config(git_default_config, NULL);
+
+	config_data.refs_backend_type = "lmdb";
+	config_data.refs_base = get_git_dir();
+
+	register_refs_backend(&refs_be_lmdb);
+	set_refs_backend("lmdb", &config_data);
+
+	if (clear_reflog) {
+		test_refdb_raw_delete_reflog(argv[0]);
+	} else if (append_reflog) {
+		test_refdb_raw_append_reflog(append_reflog);
+	} else if (reflog) {
+		return test_refdb_raw_reflog(reflog);
+	} else if (delete) {
+		if (test_refdb_raw_delete(delete) && delete_missing_error)
+			return 1;
+	} else if (argc == 1) {
+		return test_refdb_raw_read(argv[0]);
+	} else {
+		test_refdb_raw_write(argv[0], argv[1]);
+	}
+	return 0;
+}
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* [PATCH 16/16] refs: tests for lmdb backend
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (14 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 15/16] refs: add LMDB refs backend David Turner
@ 2015-12-03  0:35 ` David Turner
  2015-12-22 23:56 ` [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
  16 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-03  0:35 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Add tests for the database backend.

Signed-off-by: David Turner <dturner@twopensource.com>
Helped-by: Dennis Kaarsemaker <dennis@kaarsemaker.net>
---
 t/t1460-refs-lmdb-backend.sh        | 1109 +++++++++++++++++++++++++++++++++++
 t/t1470-refs-lmdb-backend-reflog.sh |  359 ++++++++++++
 t/test-lib.sh                       |    1 +
 3 files changed, 1469 insertions(+)
 create mode 100755 t/t1460-refs-lmdb-backend.sh
 create mode 100755 t/t1470-refs-lmdb-backend-reflog.sh

diff --git a/t/t1460-refs-lmdb-backend.sh b/t/t1460-refs-lmdb-backend.sh
new file mode 100755
index 0000000..390e148
--- /dev/null
+++ b/t/t1460-refs-lmdb-backend.sh
@@ -0,0 +1,1109 @@
+#!/bin/sh
+#
+# Copyright (c) 2015 Twitter, Inc
+# Copyright (c) 2006 Shawn Pearce
+# This test is based on t1400-update-ref.sh
+#
+
+test_description='Test lmdb refs backend'
+TEST_NO_CREATE_REPO=1
+. ./test-lib.sh
+
+if ! test_have_prereq LMDB
+then
+	skip_all="Skipping lmdb refs backend tests, lmdb backend not built"
+	test_done
+fi
+
+raw_ref() {
+	test-refs-lmdb-backend "$1"
+}
+
+delete_ref() {
+	test-refs-lmdb-backend -d "$1"
+}
+
+write_ref() {
+	test-refs-lmdb-backend "$1" "$2"
+}
+
+raw_reflog() {
+	test-refs-lmdb-backend -l "$1"
+}
+
+delete_all_reflogs() {
+	test-refs-lmdb-backend -c
+}
+
+append_reflog() {
+	test-refs-lmdb-backend -a "$1"
+}
+
+Z=$_z40
+
+test_expect_success setup '
+	git init --refs-backend-type=lmdb &&
+	for name in A B C D E F
+	do
+		test_tick &&
+		T=$(git write-tree) &&
+		sha1=$(echo $name | git commit-tree $T) &&
+		eval $name=$sha1
+	done
+'
+
+m=refs/heads/master
+n_dir=refs/heads/gu
+n=$n_dir/fixes
+
+test_expect_success \
+	"create $m" \
+	"git update-ref $m $A &&
+	 test $A"' = $(raw_ref '"$m"')'
+test_expect_success \
+	"create $m" \
+	"git update-ref $m $B $A &&
+	 test $B"' = $(raw_ref '"$m"')'
+test_expect_success "fail to delete $m with stale ref" '
+	test_must_fail git update-ref -d $m $A &&
+	test $B = "$(raw_ref $m)"
+'
+test_expect_success "delete $m" '
+	git update-ref -d $m $B &&
+	! raw_ref $m
+'
+delete_ref $m
+
+test_expect_success "delete $m without oldvalue verification" "
+	git update-ref $m $A &&
+	test $A = \$(raw_ref $m) &&
+	git update-ref -d $m &&
+	! raw_ref $m
+"
+delete_ref $m
+
+test_expect_success \
+	"fail to create $n" \
+	"git update-ref $n_dir $A &&
+	 test_must_fail git update-ref $n $A >out 2>err"
+
+delete_ref $n_dir
+rm -f out err
+
+test_expect_success \
+	"create $m (by HEAD)" \
+	"git update-ref HEAD $A &&
+	 test $A"' = $(raw_ref '"$m"')'
+test_expect_success \
+	"create $m (by HEAD)" \
+	"git update-ref HEAD $B $A &&
+	 test $B"' = $(raw_ref '"$m"')'
+test_expect_success "fail to delete $m (by HEAD) with stale ref" '
+	test_must_fail git update-ref -d HEAD $A &&
+	test $B = $(raw_ref '"$m"')
+'
+test_expect_success "delete $m (by HEAD)" '
+	git update-ref -d HEAD $B &&
+	! raw_ref $m
+'
+delete_ref $m
+
+test_expect_success \
+	"create $m (by HEAD)" \
+	"git update-ref HEAD $A &&
+	 test $A"' = $(raw_ref '"$m"')'
+test_expect_success \
+	"pack refs" \
+	"git pack-refs --all"
+test_expect_success \
+	"move $m (by HEAD)" \
+	"git update-ref HEAD $B $A &&
+	 test $B"' = $(raw_ref '"$m"')'
+test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
+	git update-ref -d HEAD $B &&
+	! raw_ref $m
+'
+delete_ref $m
+
+OLD_HEAD=$(raw_ref HEAD)
+test_expect_success "delete symref without dereference" '
+	git update-ref --no-deref -d HEAD &&
+	! raw_ref HEAD
+'
+write_ref HEAD "$OLD_HEAD"
+
+test_expect_success "delete symref without dereference when the referred ref is packed" '
+	echo foo >foo.c &&
+	git add foo.c &&
+	git commit -m foo &&
+	git pack-refs --all &&
+	git update-ref --no-deref -d HEAD &&
+	! raw_ref HEAD
+'
+write_ref HEAD "$OLD_HEAD"
+delete_ref $m
+
+test_expect_success 'update-ref -d is not confused by self-reference' '
+	git symbolic-ref refs/heads/self refs/heads/self &&
+	test_when_finished "delete_ref refs/heads/self" &&
+	test_must_fail git update-ref -d refs/heads/self
+'
+
+test_expect_success 'update-ref --no-deref -d can delete self-reference' '
+	git symbolic-ref refs/heads/self refs/heads/self &&
+	test_when_finished "delete_ref refs/heads/self" &&
+	git update-ref --no-deref -d refs/heads/self
+'
+
+test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' '
+	test-refs-lmdb-backend refs/heads/bad "" &&
+	test_when_finished "delete_ref refs/heads/bad" &&
+	git symbolic-ref refs/heads/ref-to-bad refs/heads/bad &&
+	test_when_finished "delete_ref refs/heads/ref-to-bad" &&
+	raw_ref refs/heads/ref-to-bad &&
+	git update-ref --no-deref -d refs/heads/ref-to-bad &&
+	! raw_ref refs/heads/ref-to-bad
+'
+
+test_expect_success '(not) create HEAD with old sha1' "
+	test_must_fail git update-ref HEAD $A $B
+"
+test_expect_success "(not) prior created .git/$m" "
+	! raw_ref $m
+"
+delete_ref $m
+
+test_expect_success \
+	"create HEAD" \
+	"git update-ref HEAD $A"
+test_expect_success '(not) change HEAD with wrong SHA1' "
+	test_must_fail git update-ref HEAD $B $Z
+"
+test_expect_success "(not) changed .git/$m" "
+	! test $B"' = $(raw_ref '"$m"')
+'
+
+: a repository with working tree always has reflog these days...
+delete_all_reflogs
+: | append_reflog $m
+delete_ref $m
+
+test_expect_success \
+	"create $m (logged by touch)" \
+	'GIT_COMMITTER_DATE="2005-05-26 23:30" \
+	 git update-ref HEAD '"$A"' -m "Initial Creation" &&
+	 test '"$A"' = $(raw_ref '"$m"')'
+test_expect_success \
+	"update $m (logged by touch)" \
+	'GIT_COMMITTER_DATE="2005-05-26 23:31" \
+	 git update-ref HEAD'" $B $A "'-m "Switch" &&
+	 test '"$B"' = $(raw_ref '"$m"')'
+test_expect_success \
+	"set $m (logged by touch)" \
+	'GIT_COMMITTER_DATE="2005-05-26 23:41" \
+	 git update-ref HEAD'" $A &&
+	 test $A"' = $(raw_ref '"$m"')'
+
+cat >expect <<EOF
+$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000	Initial Creation
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000	Switch
+$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
+EOF
+test_expect_success \
+	"verifying $m's log" \
+	"raw_reflog $m >actual &&
+	 test_cmp expect actual"
+delete_ref $m
+delete_all_reflogs
+: | append_reflog $m
+rm -f actual expect
+
+test_expect_success \
+	'enable core.logAllRefUpdates' \
+	'git config core.logAllRefUpdates true &&
+	 test true = $(git config --bool --get core.logAllRefUpdates)'
+
+test_expect_success \
+	"create $m (logged by config)" \
+	'GIT_COMMITTER_DATE="2005-05-26 23:32" \
+	 git update-ref HEAD'" $A "'-m "Initial Creation" &&
+	 test '"$A"' = $(raw_ref '"$m"')'
+test_expect_success \
+	"update $m (logged by config)" \
+	'GIT_COMMITTER_DATE="2005-05-26 23:33" \
+	 git update-ref HEAD'" $B $A "'-m "Switch" &&
+	 test '"$B"' = $(raw_ref '"$m"')'
+test_expect_success \
+	"set $m (logged by config)" \
+	'GIT_COMMITTER_DATE="2005-05-26 23:43" \
+	 git update-ref HEAD '"$A &&
+	 test $A"' = $(raw_ref '"$m"')'
+
+cat >expect <<EOF
+$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000	Initial Creation
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000	Switch
+$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
+EOF
+test_expect_success \
+	"verifying $m's log" \
+	'raw_reflog $m >actual &&
+	test_cmp expect actual'
+delete_ref $m
+rm -f expect
+
+git update-ref $m $D
+git reflog expire --expire=all $m
+
+append_reflog $m <<EOF
+0000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
+$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
+$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
+$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
+EOF
+
+ed="Thu, 26 May 2005 18:32:00 -0500"
+gd="Thu, 26 May 2005 18:33:00 -0500"
+ld="Thu, 26 May 2005 18:43:00 -0500"
+test_expect_success \
+	'Query "master@{May 25 2005}" (before history)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{May 25 2005}" >o 2>e &&
+	 test '"$C"' = $(cat o) &&
+	 test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
+test_expect_success \
+	"Query master@{2005-05-25} (before history)" \
+	'rm -f o e &&
+	 git rev-parse --verify master@{2005-05-25} >o 2>e &&
+	 test '"$C"' = $(cat o) &&
+	 echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
+test_expect_success \
+	'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
+	 test '"$C"' = $(cat o) &&
+	 test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"'
+test_expect_success \
+	'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
+	 test '"$C"' = $(cat o) &&
+	 test "" = "$(cat e)"'
+test_expect_success \
+	'Query "master@{May 26 2005 23:32:30}" (first non-creation change)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{May 26 2005 23:32:30}" >o 2>e &&
+	 test '"$A"' = $(cat o) &&
+	 test "" = "$(cat e)"'
+test_expect_success \
+	'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
+	 test '"$B"' = $(cat o) &&
+	 test "warning: Log for ref '"$m has gap after $gd"'." = "$(cat e)"'
+test_expect_success \
+	'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
+	 test '"$Z"' = $(cat o) &&
+	 test "" = "$(cat e)"'
+test_expect_success \
+	'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
+	 test '"$E"' = $(cat o) &&
+	 test "" = "$(cat e)"'
+test_expect_success \
+	'Query "master@{2005-05-28}" (past end of history)' \
+	'rm -f o e &&
+	 git rev-parse --verify "master@{2005-05-28}" >o 2>e &&
+	 test '"$D"' = $(cat o) &&
+	 test "warning: Log for ref '"$m unexpectedly ended on $ld"'." = "$(cat e)"'
+
+
+git reflog expire --expire=all $m
+delete_ref $m
+
+test_expect_success \
+    'creating initial files' \
+    'echo TEST >F &&
+     git add F &&
+	 GIT_AUTHOR_DATE="2005-05-26 23:30" \
+	 GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
+	 h_TEST=$(git rev-parse --verify HEAD) &&
+	 echo The other day this did not work. >M &&
+	 echo And then Bob told me how to fix it. >>M &&
+	 echo OTHER >F &&
+	 GIT_AUTHOR_DATE="2005-05-26 23:41" \
+	 GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
+	 h_OTHER=$(git rev-parse --verify HEAD) &&
+	 GIT_AUTHOR_DATE="2005-05-26 23:44" \
+	 GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
+	 h_FIXED=$(git rev-parse --verify HEAD) &&
+	 echo Merged initial commit and a later commit. >M &&
+	 echo $h_TEST >.git/MERGE_HEAD &&
+	 GIT_AUTHOR_DATE="2005-05-26 23:45" \
+	 GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
+	 h_MERGED=$(git rev-parse --verify HEAD) &&
+	 rm -f M'
+
+cat >expect <<EOF
+$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000	commit (initial): add
+$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000	commit: The other day this did not work.
+$h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000	commit (amend): The other day this did not work.
+$h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000	commit (merge): Merged initial commit and a later commit.
+EOF
+test_expect_success \
+	'git commit logged updates' \
+	"raw_reflog $m >actual &&
+	test_cmp expect actual"
+unset h_TEST h_OTHER h_FIXED h_MERGED
+
+test_expect_success \
+	'git cat-file blob master:F (expect OTHER)' \
+	'test OTHER = $(git cat-file blob master:F)'
+test_expect_success \
+	'git cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \
+	'test TEST = $(git cat-file blob "master@{2005-05-26 23:30}:F")'
+test_expect_success \
+	'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \
+	'test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")'
+
+a=refs/heads/a
+b=refs/heads/b
+c=refs/heads/c
+E='""'
+F='%s\0'
+pws='path with space'
+
+test_expect_success 'stdin test setup' '
+	echo "$pws" >"$pws" &&
+	git add -- "$pws" &&
+	git commit -m "$pws"
+'
+
+test_expect_success '-z fails without --stdin' '
+	test_must_fail git update-ref -z $m $m $m 2>err &&
+	grep "usage: git update-ref" err
+'
+
+test_expect_success 'stdin works with no input' '
+	>stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse --verify -q $m
+'
+
+test_expect_success 'stdin fails on empty line' '
+	echo "" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: empty command in input" err
+'
+
+test_expect_success 'stdin fails on only whitespace' '
+	echo " " >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: whitespace before command:  " err
+'
+
+test_expect_success 'stdin fails on leading whitespace' '
+	echo " create $a $m" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: whitespace before command:  create $a $m" err
+'
+
+test_expect_success 'stdin fails on unknown command' '
+	echo "unknown $a" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: unknown command: unknown $a" err
+'
+
+test_expect_success 'stdin fails on unbalanced quotes' '
+	echo "create $a \"master" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: badly quoted argument: \\\"master" err
+'
+
+test_expect_success 'stdin fails on invalid escape' '
+	echo "create $a \"ma\zter\"" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: badly quoted argument: \\\"ma\\\\zter\\\"" err
+'
+
+test_expect_success 'stdin fails on junk after quoted argument' '
+	echo "create \"$a\"master" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: unexpected character after quoted argument: \\\"$a\\\"master" err
+'
+
+test_expect_success 'stdin fails create with no ref' '
+	echo "create " >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: create: missing <ref>" err
+'
+
+test_expect_success 'stdin fails create with no new value' '
+	echo "create $a" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: create $a: missing <newvalue>" err
+'
+
+test_expect_success 'stdin fails create with too many arguments' '
+	echo "create $a $m $m" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: create $a: extra input:  $m" err
+'
+
+test_expect_success 'stdin fails update with no ref' '
+	echo "update " >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: update: missing <ref>" err
+'
+
+test_expect_success 'stdin fails update with no new value' '
+	echo "update $a" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: update $a: missing <newvalue>" err
+'
+
+test_expect_success 'stdin fails update with too many arguments' '
+	echo "update $a $m $m $m" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: update $a: extra input:  $m" err
+'
+
+test_expect_success 'stdin fails delete with no ref' '
+	echo "delete " >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: delete: missing <ref>" err
+'
+
+test_expect_success 'stdin fails delete with too many arguments' '
+	echo "delete $a $m $m" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: delete $a: extra input:  $m" err
+'
+
+test_expect_success 'stdin fails verify with too many arguments' '
+	echo "verify $a $m $m" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: verify $a: extra input:  $m" err
+'
+
+test_expect_success 'stdin fails option with unknown name' '
+	echo "option unknown" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: option unknown: unknown" err
+'
+
+test_expect_success 'stdin fails with duplicate refs' '
+	cat >stdin <<-EOF &&
+	create $a $m
+	create $b $m
+	create $a $m
+	EOF
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err
+'
+
+test_expect_success 'stdin create ref works' '
+	echo "create $a $m" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin succeeds with quoted argument' '
+	git update-ref -d $a &&
+	echo "create $a \"$m\"" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin succeeds with escaped character' '
+	git update-ref -d $a &&
+	echo "create $a \"ma\\163ter\"" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin update ref creates with zero old value' '
+	echo "update $b $m $Z" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	git update-ref -d $b
+'
+
+test_expect_success 'stdin update ref creates with empty old value' '
+	echo "update $b $m $E" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin create ref works with path with space to blob' '
+	echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse "$m:$pws" >expect &&
+	git rev-parse refs/blobs/pws >actual &&
+	test_cmp expect actual &&
+	git update-ref -d refs/blobs/pws
+'
+
+test_expect_success 'stdin update ref fails with wrong old value' '
+	echo "update $c $m $m~1" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: cannot lock the ref '"'"'$c'"'"'" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin update ref fails with bad old value' '
+	echo "update $c $m does-not-exist" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin create ref fails with bad new value' '
+	echo "create $c does-not-exist" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin create ref fails with zero new value' '
+	echo "create $c " >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: create $c: zero <newvalue>" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin update ref works with right old value' '
+	echo "update $b $m~1 $m" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m~1 >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin delete ref fails with wrong old value' '
+	echo "delete $a $m~1" >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: cannot lock the ref '"'"'$a'"'"'" err &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin delete ref fails with zero old value' '
+	echo "delete $a " >stdin &&
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: delete $a: zero <oldvalue>" err &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin update symref works option no-deref' '
+	git symbolic-ref refs/TESTSYMREF $b &&
+	cat >stdin <<-EOF &&
+	option no-deref
+	update refs/TESTSYMREF $a $b
+	EOF
+	git update-ref --stdin <stdin &&
+	git rev-parse refs/TESTSYMREF >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $m~1 >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin delete symref works option no-deref' '
+	git symbolic-ref refs/TESTSYMREF $b &&
+	cat >stdin <<-EOF &&
+	option no-deref
+	delete refs/TESTSYMREF $b
+	EOF
+	git update-ref --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q refs/TESTSYMREF &&
+	git rev-parse $m~1 >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin delete ref works with right old value' '
+	echo "delete $b $m~1" >stdin &&
+	git update-ref --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q $b
+'
+
+test_expect_success 'stdin update/create/verify combination works' '
+	cat >stdin <<-EOF &&
+	update $a $m
+	create $b $m
+	verify $c
+	EOF
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin verify succeeds for correct value' '
+	git rev-parse $m >expect &&
+	echo "verify $m $m" >stdin &&
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin verify succeeds for missing reference' '
+	echo "verify refs/heads/missing $Z" >stdin &&
+	git update-ref --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin verify treats no value as missing' '
+	echo "verify refs/heads/missing" >stdin &&
+	git update-ref --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin verify fails for wrong value' '
+	git rev-parse $m >expect &&
+	echo "verify $m $m~1" >stdin &&
+	test_must_fail git update-ref --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin verify fails for mistaken null value' '
+	git rev-parse $m >expect &&
+	echo "verify $m $Z" >stdin &&
+	test_must_fail git update-ref --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin verify fails for mistaken empty value' '
+	M=$(git rev-parse $m) &&
+	test_when_finished "git update-ref $m $M" &&
+	git rev-parse $m >expect &&
+	echo "verify $m" >stdin &&
+	test_must_fail git update-ref --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin update refs works with identity updates' '
+	cat >stdin <<-EOF &&
+	update $a $m $m
+	update $b $m $m
+	update $c $Z $E
+	EOF
+	git update-ref --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin update refs fails with wrong old value' '
+	git update-ref $c $m &&
+	cat >stdin <<-EOF &&
+	update $a $m $m
+	update $b $m $m
+	update $c  ''
+	EOF
+	test_must_fail git update-ref --stdin <stdin 2>err &&
+	grep "fatal: cannot lock the ref '"'"'$c'"'"'" err &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	git rev-parse $c >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin delete refs works with packed and loose refs' '
+	git pack-refs --all &&
+	git update-ref $c $m~1 &&
+	cat >stdin <<-EOF &&
+	delete $a $m
+	update $b $Z $m
+	update $c $E $m~1
+	EOF
+	git update-ref --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q $a &&
+	test_must_fail git rev-parse --verify -q $b &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z works on empty input' '
+	>stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse --verify -q $m
+'
+
+test_expect_success 'stdin -z fails on empty line' '
+	echo "" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: whitespace before command: " err
+'
+
+test_expect_success 'stdin -z fails on empty command' '
+	printf $F "" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: empty command in input" err
+'
+
+test_expect_success 'stdin -z fails on only whitespace' '
+	printf $F " " >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: whitespace before command:  " err
+'
+
+test_expect_success 'stdin -z fails on leading whitespace' '
+	printf $F " create $a" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: whitespace before command:  create $a" err
+'
+
+test_expect_success 'stdin -z fails on unknown command' '
+	printf $F "unknown $a" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: unknown command: unknown $a" err
+'
+
+test_expect_success 'stdin -z fails create with no ref' '
+	printf $F "create " >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: create: missing <ref>" err
+'
+
+test_expect_success 'stdin -z fails create with no new value' '
+	printf $F "create $a" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
+'
+
+test_expect_success 'stdin -z fails create with too many arguments' '
+	printf $F "create $a" "$m" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails update with no ref' '
+	printf $F "update " >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: update: missing <ref>" err
+'
+
+test_expect_success 'stdin -z fails update with too few args' '
+	printf $F "update $a" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z emits warning with empty new value' '
+	git update-ref $a $m &&
+	printf $F "update $a" "" "" >stdin &&
+	git update-ref -z --stdin <stdin 2>err &&
+	grep "warning: update $a: missing <newvalue>, treating as zero" err &&
+	test_must_fail git rev-parse --verify -q $a
+'
+
+test_expect_success 'stdin -z fails update with no new value' '
+	printf $F "update $a" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
+'
+
+test_expect_success 'stdin -z fails update with no old value' '
+	printf $F "update $a" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z fails update with too many arguments' '
+	printf $F "update $m" "$m" "$m" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails delete with no ref' '
+	printf $F "delete " >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: delete: missing <ref>" err
+'
+
+test_expect_success 'stdin -z fails delete with no old value' '
+	printf $F "delete $a" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z fails delete with too many arguments' '
+	printf $F "delete $m" "$m" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails verify with too many arguments' '
+	printf $F "verify $m" "$m" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails verify with no old value' '
+	printf $F "verify $a" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z fails option with unknown name' '
+	printf $F "option unknown" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: option unknown: unknown" err
+'
+
+test_expect_success 'stdin -z fails with duplicate refs' '
+	printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err
+'
+
+test_expect_success 'stdin -z create ref works' '
+	printf $F "create $a" "$m" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z update ref creates with zero old value' '
+	printf $F "update $b" "$m" "$Z" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	git update-ref -d $b
+'
+
+test_expect_success 'stdin -z update ref creates with empty old value' '
+	printf $F "update $b" "$m" "" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z create ref works with path with space to blob' '
+	printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse "$m:$pws" >expect &&
+	git rev-parse refs/blobs/pws >actual &&
+	test_cmp expect actual &&
+	git update-ref -d refs/blobs/pws
+'
+
+test_expect_success 'stdin -z update ref fails with wrong old value' '
+	printf $F "update $c" "$m" "$m~1" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: cannot lock the ref '"'"'$c'"'"'" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z update ref fails with bad old value' '
+	printf $F "update $c" "$m" "does-not-exist" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z create ref fails when ref exists' '
+	git update-ref $c $m &&
+	git rev-parse "$c" >expect &&
+	printf $F "create $c" "$m~1" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: cannot lock the ref '"'"'$c'"'"'" err &&
+	git rev-parse "$c" >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z create ref fails with bad new value' '
+	git update-ref -d "$c" &&
+	printf $F "create $c" "does-not-exist" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z create ref fails with empty new value' '
+	printf $F "create $c" "" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: create $c: missing <newvalue>" err &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z update ref works with right old value' '
+	printf $F "update $b" "$m~1" "$m" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse $m~1 >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete ref fails with wrong old value' '
+	printf $F "delete $a" "$m~1" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: cannot lock the ref '"'"'$a'"'"'" err &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete ref fails with zero old value' '
+	printf $F "delete $a" "$Z" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: delete $a: zero <oldvalue>" err &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z update symref works option no-deref' '
+	git symbolic-ref refs/TESTSYMREF $b &&
+	printf $F "option no-deref" "update refs/TESTSYMREF" "$a" "$b" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse refs/TESTSYMREF >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $m~1 >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete symref works option no-deref' '
+	git symbolic-ref refs/TESTSYMREF $b &&
+	printf $F "option no-deref" "delete refs/TESTSYMREF" "$b" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q refs/TESTSYMREF &&
+	git rev-parse $m~1 >expect &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete ref works with right old value' '
+	printf $F "delete $b" "$m~1" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q $b
+'
+
+test_expect_success 'stdin -z update/create/verify combination works' '
+	printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z verify succeeds for correct value' '
+	git rev-parse $m >expect &&
+	printf $F "verify $m" "$m" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z verify succeeds for missing reference' '
+	printf $F "verify refs/heads/missing" "$Z" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin -z verify treats no value as missing' '
+	printf $F "verify refs/heads/missing" "" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin -z verify fails for wrong value' '
+	git rev-parse $m >expect &&
+	printf $F "verify $m" "$m~1" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z verify fails for mistaken null value' '
+	git rev-parse $m >expect &&
+	printf $F "verify $m" "$Z" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z verify fails for mistaken empty value' '
+	M=$(git rev-parse $m) &&
+	test_when_finished "git update-ref $m $M" &&
+	git rev-parse $m >expect &&
+	printf $F "verify $m" "" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin &&
+	git rev-parse $m >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z update refs works with identity updates' '
+	printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z update refs fails with wrong old value' '
+	git update-ref $c $m &&
+	printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
+	test_must_fail git update-ref -z --stdin <stdin 2>err &&
+	grep "fatal: cannot lock the ref '"'"'$c'"'"'" err &&
+	git rev-parse $m >expect &&
+	git rev-parse $a >actual &&
+	test_cmp expect actual &&
+	git rev-parse $b >actual &&
+	test_cmp expect actual &&
+	git rev-parse $c >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete refs works with packed and loose refs' '
+	git pack-refs --all &&
+	git update-ref $c $m~1 &&
+	printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
+	git update-ref -z --stdin <stdin &&
+	test_must_fail git rev-parse --verify -q $a &&
+	test_must_fail git rev-parse --verify -q $b &&
+	test_must_fail git rev-parse --verify -q $c
+'
+
+test_done
diff --git a/t/t1470-refs-lmdb-backend-reflog.sh b/t/t1470-refs-lmdb-backend-reflog.sh
new file mode 100755
index 0000000..35586c2
--- /dev/null
+++ b/t/t1470-refs-lmdb-backend-reflog.sh
@@ -0,0 +1,359 @@
+#!/bin/sh
+#
+# Copyright (c) 2015 Twitter, Inc
+# Copyright (c) 2007 Junio C Hamano
+#
+
+test_description='Test prune and reflog expiration'
+TEST_NO_CREATE_REPO=1
+. ./test-lib.sh
+
+if ! test_have_prereq LMDB
+then
+	skip_all="Skipping lmdb refs backend tests, lmdb backend not built"
+	test_done
+fi
+
+raw_reflog() {
+	cat .git/logs/$1 2>/dev/null || test-refs-lmdb-backend -l "$1"
+}
+
+append_reflog() {
+	test-refs-lmdb-backend -a "$1"
+}
+
+check_have () {
+	gaah= &&
+	for N in "$@"
+	do
+		eval "o=\$$N" && git cat-file -t $o || {
+			echo Gaah $N
+			gaah=$N
+			break
+		}
+	done &&
+	test -z "$gaah"
+}
+
+check_fsck () {
+	output=$(git fsck --full)
+	case "$1" in
+	'')
+		test -z "$output" ;;
+	*)
+		echo "$output" | grep "$1" ;;
+	esac
+}
+
+corrupt () {
+	aa=${1%??????????????????????????????????????} zz=${1#??}
+	mv .git/objects/$aa/$zz .git/$aa$zz
+}
+
+recover () {
+	aa=${1%??????????????????????????????????????} zz=${1#??}
+	mkdir -p .git/objects/$aa
+	mv .git/$aa$zz .git/objects/$aa/$zz
+}
+
+check_dont_have () {
+	gaah= &&
+	for N in "$@"
+	do
+		eval "o=\$$N"
+		git cat-file -t $o && {
+			echo Gaah $N
+			gaah=$N
+			break
+		}
+	done
+	test -z "$gaah"
+}
+
+test_expect_success setup '
+	git init --refs-backend-type=lmdb &&
+	mkdir -p A/B &&
+	echo rat >C &&
+	echo ox >A/D &&
+	echo tiger >A/B/E &&
+	git add . &&
+
+	test_tick && git commit -m rabbit &&
+	H=`git rev-parse --verify HEAD` &&
+	A=`git rev-parse --verify HEAD:A` &&
+	B=`git rev-parse --verify HEAD:A/B` &&
+	C=`git rev-parse --verify HEAD:C` &&
+	D=`git rev-parse --verify HEAD:A/D` &&
+	E=`git rev-parse --verify HEAD:A/B/E` &&
+	check_fsck &&
+
+	test_chmod +x C &&
+	git add C &&
+	test_tick && git commit -m dragon &&
+	L=`git rev-parse --verify HEAD` &&
+	check_fsck &&
+
+	rm -f C A/B/E &&
+	echo snake >F &&
+	echo horse >A/G &&
+	git add F A/G &&
+	test_tick && git commit -a -m sheep &&
+	F=`git rev-parse --verify HEAD:F` &&
+	G=`git rev-parse --verify HEAD:A/G` &&
+	I=`git rev-parse --verify HEAD:A` &&
+	J=`git rev-parse --verify HEAD` &&
+	check_fsck &&
+
+	rm -f A/G &&
+	test_tick && git commit -a -m monkey &&
+	K=`git rev-parse --verify HEAD` &&
+	check_fsck &&
+
+	check_have A B C D E F G H I J K L &&
+
+	git prune &&
+
+	check_have A B C D E F G H I J K L &&
+
+	check_fsck &&
+
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 4 reflog
+'
+
+test_expect_success rewind '
+	test_tick && git reset --hard HEAD~2 &&
+	test -f C &&
+	test -f A/B/E &&
+	! test -f F &&
+	! test -f A/G &&
+
+	check_have A B C D E F G H I J K L &&
+
+	git prune &&
+
+	check_have A B C D E F G H I J K L &&
+
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 5 reflog
+'
+
+test_expect_success 'corrupt and check' '
+
+	corrupt $F &&
+	check_fsck "missing blob $F"
+
+'
+
+test_expect_success 'reflog expire --dry-run should not touch reflog' '
+
+	git reflog expire --dry-run \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all &&
+
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 5 reflog &&
+
+	check_fsck "missing blob $F"
+'
+
+test_expect_success 'reflog expire' '
+
+	git reflog expire --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all &&
+
+	echo git reflog expire --verbose \
+		--expire=$(($test_tick - 10000)) \
+		--expire-unreachable=$(($test_tick - 10000)) \
+		--stale-fix \
+		--all &&
+
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 2 reflog &&
+
+	check_fsck "dangling commit $K"
+'
+
+test_expect_success 'prune and fsck' '
+
+	git prune &&
+	check_fsck &&
+
+	check_have A B C D E H L &&
+	check_dont_have F G I J K
+
+'
+
+test_expect_success 'recover and check' '
+
+	recover $F &&
+	check_fsck "dangling blob $F"
+
+'
+
+test_expect_success 'delete' '
+	echo 1 > C &&
+	test_tick &&
+	git commit -m rat C &&
+
+	echo 2 > C &&
+	test_tick &&
+	git commit -m ox C &&
+
+	echo 3 > C &&
+	test_tick &&
+	git commit -m tiger C &&
+
+	HEAD_entry_count=$(git reflog | wc -l) &&
+	master_entry_count=$(git reflog show master | wc -l) &&
+
+	test $HEAD_entry_count = 5 &&
+	test $master_entry_count = 5 &&
+
+
+	git reflog delete master@{1} &&
+	git reflog show master > output &&
+	test $(($master_entry_count - 1)) = $(wc -l < output) &&
+	test $HEAD_entry_count = $(git reflog | wc -l) &&
+	! grep ox < output &&
+
+	master_entry_count=$(wc -l < output) &&
+
+	git reflog delete HEAD@{1} &&
+	test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
+	test $master_entry_count = $(git reflog show master | wc -l) &&
+
+	HEAD_entry_count=$(git reflog | wc -l) &&
+
+	git reflog delete master@{07.04.2005.15:15:00.-0700} &&
+	git reflog show master > output &&
+	test $(($master_entry_count - 1)) = $(wc -l < output) &&
+	! grep dragon < output
+
+'
+
+test_expect_success 'rewind2' '
+
+	test_tick && git reset --hard HEAD~2 &&
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 4 reflog
+'
+
+test_expect_success '--expire=never' '
+
+	git reflog expire --verbose \
+		--expire=never \
+		--expire-unreachable=never \
+		--all &&
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 4 reflog
+'
+
+test_expect_success 'gc.reflogexpire=never' '
+
+	git config gc.reflogexpire never &&
+	git config gc.reflogexpireunreachable never &&
+	git reflog expire --verbose --all &&
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 4 reflog
+'
+
+test_expect_success 'gc.reflogexpire=false' '
+
+	git config gc.reflogexpire false &&
+	git config gc.reflogexpireunreachable false &&
+	git reflog expire --verbose --all &&
+	raw_reflog refs/heads/master >reflog &&
+	test_when_finished rm -f reflog &&
+	test_line_count = 4 reflog &&
+
+	git config --unset gc.reflogexpire &&
+	git config --unset gc.reflogexpireunreachable
+
+'
+
+test_expect_success 'checkout should not delete log for packed ref' '
+	test $(git reflog master | wc -l) = 4 &&
+	git branch foo &&
+	git pack-refs --all &&
+	git checkout foo &&
+	test $(git reflog master | wc -l) = 4
+'
+
+test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' '
+	test_when_finished "git branch -d one || git branch -d one/two" &&
+
+	git branch one/two master &&
+	echo "one/two@{0} branch: Created from master" >expect &&
+	git log -g --format="%gd %gs" one/two >actual &&
+	test_cmp expect actual &&
+	git branch -d one/two &&
+
+	# now logs/refs/heads/one is a stale directory, but
+	# we should move it out of the way to create "one" reflog
+	git branch one master &&
+	echo "one@{0} branch: Created from master" >expect &&
+	git log -g --format="%gd %gs" one >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' '
+	test_when_finished "git branch -d one || git branch -d one/two" &&
+
+	git branch one/two master &&
+	echo "one/two@{0} branch: Created from master" >expect &&
+	git log -g --format="%gd %gs" one/two >actual &&
+	test_cmp expect actual &&
+	git branch -d one/two &&
+
+	# same as before, but we only create a reflog for "one" if
+	# it already exists, which it does not
+	git -c core.logallrefupdates=false branch one master &&
+	: >expect &&
+	git log -g --format="%gd %gs" one >actual &&
+	test_cmp expect actual
+'
+
+# Triggering the bug detected by this test requires a newline to fall
+# exactly BUFSIZ-1 bytes from the end of the file. We don't know
+# what that value is, since it's platform dependent. However, if
+# we choose some value N, we also catch any D which divides N evenly
+# (since we will read backwards in chunks of D). So we choose 8K,
+# which catches glibc (with an 8K BUFSIZ) and *BSD (1K).
+#
+# Each line is 114 characters, so we need 75 to still have a few before the
+# last 8K. The 89-character padding on the final entry lines up our
+# newline exactly.
+test_expect_success 'parsing reverse reflogs at BUFSIZ boundaries' '
+	git checkout -b reflogskip &&
+	z38=00000000000000000000000000000000000000 &&
+	ident="abc <xyz> 0000000001 +0000" &&
+	for i in $(test_seq 1 75); do
+		printf "$z38%02d $z38%02d %s\t" $i $(($i+1)) "$ident" &&
+		if test $i = 75; then
+			for j in $(test_seq 1 89); do
+				printf X
+			done
+		else
+			printf X
+		fi &&
+		printf "\n"
+	done | append_reflog refs/heads/reflogskip &&
+	git rev-parse reflogskip@{73} >actual &&
+	echo ${z38}03 >expect &&
+	test_cmp expect actual
+'
+
+test_done
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 16c4d7b..c6ba541 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -958,6 +958,7 @@ test -z "$NO_PERL" && test_set_prereq PERL
 test -z "$NO_PYTHON" && test_set_prereq PYTHON
 test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
 test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
+test -n "$USE_LIBLMDB" && test_set_prereq LMDB
 
 # Can we rely on git's output in the C locale?
 if test -n "$GETTEXT_POISON"
-- 
2.4.2.749.g0ed01d8-twtrsrc

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

* Re: [PATCH 01/16] refs: add a backend method structure with transaction functions
  2015-12-03  0:35 ` [PATCH 01/16] refs: add a backend method structure with transaction functions David Turner
@ 2015-12-05  0:07   ` Junio C Hamano
  0 siblings, 0 replies; 73+ messages in thread
From: Junio C Hamano @ 2015-12-05  0:07 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger, Ronnie Sahlberg

David Turner <dturner@twopensource.com> writes:

> diff --git a/refs.c b/refs.c
> index 0f7628d..babba8a 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -10,6 +10,31 @@
>  #include "tag.h"
>  
>  /*
> + * We always have a files backend and it is the default.
> + */
> +extern struct ref_be refs_be_files;

It is customary to s/extern //; in C sources.

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
@ 2015-12-05  0:07   ` Junio C Hamano
  2015-12-05  6:30   ` Duy Nguyen
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 73+ messages in thread
From: Junio C Hamano @ 2015-12-05  0:07 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger

David Turner <dturner@twopensource.com> writes:

> diff --git a/setup.c b/setup.c
> index d343725..de6b8ac 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -1,5 +1,6 @@
>  #include "cache.h"
>  #include "dir.h"
> +#include "refs.h"
>  #include "string-list.h"
>  
>  static int inside_git_dir = -1;
> @@ -263,6 +264,15 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
>  	return ret;
>  }
>  
> +int refdb_config(const char *var, const char *value, void *ptr)
> +{
> +       struct refdb_config_data *cdata = ptr;
> +
> +       if (!strcmp(var, "core.refsbackendtype"))
> +	       cdata->refs_backend_type = xstrdup((char *)value);
> +       return 0;
> +}
> +

These lines seem to be indented with SPs not HTs.

>  /*
>   * Test if it looks like we're at a git directory.
>   * We want to see:

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

* Re: [PATCH 15/16] refs: add LMDB refs backend
  2015-12-03  0:35 ` [PATCH 15/16] refs: add LMDB refs backend David Turner
@ 2015-12-05  0:08   ` Junio C Hamano
  2015-12-05  0:25     ` David Turner
  2015-12-17  1:00   ` Jonathan Nieder
  2015-12-23 14:32   ` Michael Haggerty
  2 siblings, 1 reply; 73+ messages in thread
From: Junio C Hamano @ 2015-12-05  0:08 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger

David Turner <dturner@twopensource.com> writes:

> +	while (!mdb_ret) {
> +		if (starts_with(key.mv_data, refname) &&
> +		    ((char*)key.mv_data)[refname_len - 2] == '/') {

ERROR: "(foo*)" should be "(foo *)"
#877: FILE: refs/lmdb-backend.c:514:
+                   ((char*)key.mv_data)[refname_len - 2] == '/') {

> +static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
> +{
> +	unsigned char osha1[20], nsha1[20];
> +	char *email_end, *message;
> +	unsigned long timestamp;
> +	int tz;
> +
> +	/* old (raw) new (raw) name <email> SP time TAB msg LF */
> +	if (sb->len < 41 || sb->buf[sb->len - 1] != '\n' ||
> +	    !(email_end = strchr(sb->buf + 40, '>')) ||
> +	    email_end[1] != ' ' ||
> +	    !(timestamp = strtoul(email_end + 2, &message, 10)) ||
> +	    !message || message[0] != ' ' ||
> +	    (message[1] != '+' && message[1] != '-') ||
> +	    !isdigit(message[2]) || !isdigit(message[3]) ||
> +	    !isdigit(message[4]) || !isdigit(message[5]))
> +		return 0; /* corrupt? */

ERROR: do not use assignment in if condition
#1024: FILE: refs/lmdb-backend.c:661:
+       if (sb->len < 41 || sb->buf[sb->len - 1] != '\n' ||

> +static char *check_ref(MDB_txn *txn, const char *refname,
> +		       const unsigned char *old_sha1,
> +		       unsigned char *resolved_sha1, int flags,
> +		       int *type_p)
> +{
> +	int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
> +	int resolve_flags = 0;
> +	int type;
> +	char *resolved_refname;
> +
> +	if (mustexist)
> +		resolve_flags |= RESOLVE_REF_READING;
> +	if (flags & REF_DELETING) {
> +		resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
> +		if (flags & REF_NODEREF)
> +			resolve_flags |= RESOLVE_REF_NO_RECURSE;
> +	}
> +
> +	/*
> +	 * The first time we resolve the refname, we're just trying to
> +	 * see if there is any ref at all by this name, even if it is
> +	 * a broken symref.
> +	 */
> +	refname = resolve_ref_unsafe(refname, resolve_flags,
> +				     resolved_sha1, &type);
> +	if (type_p)
> +	    *type_p = type;

WARNING: suspect code indent for conditional statements (8, 12)
#1177: FILE: refs/lmdb-backend.c:814:
+       if (type_p)
+           *type_p = type;

Indeed, this line should be indented with two HTs.

> +	while (!mdb_ret) {
> +		if (key.mv_size < len)
> +			break;
> +
> +		if (!starts_with(key.mv_data, log_path) || ((char*)key.mv_data)[len - 1] != 0)

ERROR: "(foo*)" should be "(foo *)"
#1254: FILE: refs/lmdb-backend.c:891:
+               if (!starts_with(key.mv_data, log_path) || ((char*)key.mv_data)[len - 1] != 0)

> +		if (strcmp (refname, orig_refname) &&

WARNING: space prohibited between function name and open parenthesis '('
#1366: FILE: refs/lmdb-backend.c:1003:
+               if (strcmp (refname, orig_refname) &&


This concludes my first pass, mechanical "lint" (with help from
checkpatch.pl).  I'll hopefully have time to do the more meaningful
design and implementation review later.

Thanks.

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

* Re: [PATCH 15/16] refs: add LMDB refs backend
  2015-12-05  0:08   ` Junio C Hamano
@ 2015-12-05  0:25     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-05  0:25 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git mailing list, mhagger

On Fri, 2015-12-04 at 16:08 -0800, Junio C Hamano wrote:
> David Turner <dturner@twopensource.com> writes:
> 
> > +	while (!mdb_ret) {
> > +		if (starts_with(key.mv_data, refname) &&
> > +		    ((char*)key.mv_data)[refname_len - 2] == '/') {
> 
> ERROR: "(foo*)" should be "(foo *)"
> #877: FILE: refs/lmdb-backend.c:514:
> +                   ((char*)key.mv_data)[refname_len - 2] == '/') {
> 
> > +static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
> > +{
> > +	unsigned char osha1[20], nsha1[20];
> > +	char *email_end, *message;
> > +	unsigned long timestamp;
> > +	int tz;
> > +
> > +	/* old (raw) new (raw) name <email> SP time TAB msg LF */
> > +	if (sb->len < 41 || sb->buf[sb->len - 1] != '\n' ||
> > +	    !(email_end = strchr(sb->buf + 40, '>')) ||
> > +	    email_end[1] != ' ' ||
> > +	    !(timestamp = strtoul(email_end + 2, &message, 10)) ||
> > +	    !message || message[0] != ' ' ||
> > +	    (message[1] != '+' && message[1] != '-') ||
> > +	    !isdigit(message[2]) || !isdigit(message[3]) ||
> > +	    !isdigit(message[4]) || !isdigit(message[5]))
> > +		return 0; /* corrupt? */
> 
> ERROR: do not use assignment in if condition
> #1024: FILE: refs/lmdb-backend.c:661:
> +       if (sb->len < 41 || sb->buf[sb->len - 1] != '\n' ||

This code is based on code from files-backend.c.  But I can change it
anyway.

I'll also fix the rest, but wait for further review to post.

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
  2015-12-05  0:07   ` Junio C Hamano
@ 2015-12-05  6:30   ` Duy Nguyen
  2015-12-05  7:44     ` Jeff King
  2015-12-10 18:02   ` Jeff King
                     ` (2 subsequent siblings)
  4 siblings, 1 reply; 73+ messages in thread
From: Duy Nguyen @ 2015-12-05  6:30 UTC (permalink / raw)
  To: David Turner; +Cc: Git Mailing List, Michael Haggerty

On Thu, Dec 3, 2015 at 1:35 AM, David Turner <dturner@twopensource.com> wrote:
> git init learns a new argument --refs-backend-type.  Presently, only
> "files" is supported, but later we will add other backends.
>
> When this argument is used, the repository's core.refsBackendType
> configuration value is set, and the refs backend's initdb function is
> used to set up the ref database.

git-init can also be used on existing repos. What happens in that case
if we use --refs-backend-type. Will existing  refs be migrated to the
new backend or hidden away?
-- 
Duy

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-05  6:30   ` Duy Nguyen
@ 2015-12-05  7:44     ` Jeff King
  2015-12-08  0:38       ` David Turner
  2015-12-23  9:52       ` Michael Haggerty
  0 siblings, 2 replies; 73+ messages in thread
From: Jeff King @ 2015-12-05  7:44 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: David Turner, Git Mailing List, Michael Haggerty

On Sat, Dec 05, 2015 at 07:30:13AM +0100, Duy Nguyen wrote:

> On Thu, Dec 3, 2015 at 1:35 AM, David Turner <dturner@twopensource.com> wrote:
> > git init learns a new argument --refs-backend-type.  Presently, only
> > "files" is supported, but later we will add other backends.
> >
> > When this argument is used, the repository's core.refsBackendType
> > configuration value is set, and the refs backend's initdb function is
> > used to set up the ref database.
> 
> git-init can also be used on existing repos. What happens in that case
> if we use --refs-backend-type. Will existing  refs be migrated to the
> new backend or hidden away?

It would be neat if it migrated, but I suspect that may introduce
complications. It's probably OK in the initial implementation to bail if
the option is used in an existing repo.

I think the config option needs to be extensions.refsBackendType, too,
per the logic in 00a09d5 (introduce "extensions" form of
core.repositoryformatversion, 2015-06-23). And I guess it needs to bump
core.repositoryformatversion to "1".

-Peff

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-05  7:44     ` Jeff King
@ 2015-12-08  0:38       ` David Turner
  2015-12-23  9:52       ` Michael Haggerty
  1 sibling, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-08  0:38 UTC (permalink / raw)
  To: Jeff King; +Cc: Duy Nguyen, Git Mailing List, Michael Haggerty

On Sat, 2015-12-05 at 02:44 -0500, Jeff King wrote:
> On Sat, Dec 05, 2015 at 07:30:13AM +0100, Duy Nguyen wrote:
> 
> > On Thu, Dec 3, 2015 at 1:35 AM, David Turner <dturner@twopensource.com> wrote:
> > > git init learns a new argument --refs-backend-type.  Presently, only
> > > "files" is supported, but later we will add other backends.
> > >
> > > When this argument is used, the repository's core.refsBackendType
> > > configuration value is set, and the refs backend's initdb function is
> > > used to set up the ref database.
> > 
> > git-init can also be used on existing repos. What happens in that case
> > if we use --refs-backend-type. Will existing  refs be migrated to the
> > new backend or hidden away?

I'd like to migrate, but I don't want to write that code prematurely.  I
figure we can get everything else right first.  

> It would be neat if it migrated, but I suspect that may introduce
> complications. It's probably OK in the initial implementation to bail if
> the option is used in an existing repo.

Will fix.

> I think the config option needs to be extensions.refsBackendType, too,
> per the logic in 00a09d5 (introduce "extensions" form of
> core.repositoryformatversion, 2015-06-23). And I guess it needs to bump
> core.repositoryformatversion to "1".

I did that in a later patch, but now is a better time; will fix.

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
  2015-12-05  0:07   ` Junio C Hamano
  2015-12-05  6:30   ` Duy Nguyen
@ 2015-12-10 18:02   ` Jeff King
  2015-12-10 19:36     ` David Turner
  2015-12-23 11:30   ` [PATCH] clone: use child_process for recursive checkouts Michael Haggerty
  2015-12-23 13:34   ` [PATCH 13/16] init: allow alternate backends to be set for new repos Michael Haggerty
  4 siblings, 1 reply; 73+ messages in thread
From: Jeff King @ 2015-12-10 18:02 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger

On Wed, Dec 02, 2015 at 07:35:18PM -0500, David Turner wrote:

> @@ -510,9 +511,36 @@ int validate_headref(const char *path)
>  	unsigned char sha1[20];
>  	int fd;
>  	ssize_t len;
> +	struct refdb_config_data refdb_data = {NULL, NULL};
> +
> +	if (lstat(path, &st) < 0) {
> +		int backend_type_set;
> +		struct strbuf config_path = STRBUF_INIT;
> +		if (path) {
> +			char *pathdup = xstrdup(path);
> +			char *git_dir = dirname(pathdup);
> +			strbuf_addf(&config_path, "%s/%s", git_dir, "config");
> +			free(pathdup);
> +		} else {
> +			strbuf_addstr(&config_path, "config");
> +		}

Can we ever follow this "else" branch? It would mean feeding NULL to
lstat.

-Peff

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-10 18:02   ` Jeff King
@ 2015-12-10 19:36     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-10 19:36 UTC (permalink / raw)
  To: Jeff King; +Cc: git, mhagger

On Thu, 2015-12-10 at 13:02 -0500, Jeff King wrote:
> On Wed, Dec 02, 2015 at 07:35:18PM -0500, David Turner wrote:
> 
> > @@ -510,9 +511,36 @@ int validate_headref(const char *path)
> >  	unsigned char sha1[20];
> >  	int fd;
> >  	ssize_t len;
> > +	struct refdb_config_data refdb_data = {NULL, NULL};
> > +
> > +	if (lstat(path, &st) < 0) {
> > +		int backend_type_set;
> > +		struct strbuf config_path = STRBUF_INIT;
> > +		if (path) {
> > +			char *pathdup = xstrdup(path);
> > +			char *git_dir = dirname(pathdup);
> > +			strbuf_addf(&config_path, "%s/%s", git_dir, "config");
> > +			free(pathdup);
> > +		} else {
> > +			strbuf_addstr(&config_path, "config");
> > +		}
> 
> Can we ever follow this "else" branch? It would mean feeding NULL to
> lstat.

Probably cruft from an old version of the patch.  Removed if/else.
Thanks.

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

* Re: [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-03  0:35 ` [PATCH 02/16] refs: add methods for misc ref operations David Turner
@ 2015-12-11 23:33   ` Junio C Hamano
  2015-12-11 23:49     ` David Turner
  2015-12-11 23:39   ` Junio C Hamano
  1 sibling, 1 reply; 73+ messages in thread
From: Junio C Hamano @ 2015-12-11 23:33 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger, Ronnie Sahlberg, David Turner

David Turner <dturner@twopensource.com> writes:

> diff --git a/cache.h b/cache.h
> index 51c35c3..707455a 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -1111,6 +1111,13 @@ extern char *oid_to_hex(const struct object_id *oid);	/* same static buffer as s
>  extern int interpret_branch_name(const char *str, int len, struct strbuf *);
>  extern int get_sha1_mb(const char *str, unsigned char *sha1);
>  
> +/*
> + * Return true iff abbrev_name is a possible abbreviation for
> + * full_name according to the rules defined by ref_rev_parse_rules in
> + * refs.c.
> + */
> +extern int refname_match(const char *abbrev_name, const char *full_name);
> +
>  extern int validate_headref(const char *ref);

This hunk is strange.  It duplicates what is in refs.h, risking the
two copies to drift away from each other over time.

As the only callsites are in remote.c that includes refs.h, this
probably should be dropped.

Other changes that give redirection to various operations via vtable
looked all sensible.

>  struct ref_be {
>  	struct ref_be *next;
>  	const char *name;
>  	ref_transaction_commit_fn *transaction_commit;
> +
> +	pack_refs_fn *pack_refs;
> +	peel_ref_fn *peel_ref;
> +	create_symref_fn *create_symref;
> +
> +	resolve_ref_unsafe_fn *resolve_ref_unsafe;
> +	verify_refname_available_fn *verify_refname_available;
> +	resolve_gitlink_ref_fn *resolve_gitlink_ref;
>  };
>  
>  #endif /* REFS_REFS_INTERNAL_H */

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

* Re: [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-03  0:35 ` [PATCH 02/16] refs: add methods for misc ref operations David Turner
  2015-12-11 23:33   ` Junio C Hamano
@ 2015-12-11 23:39   ` Junio C Hamano
  2015-12-11 23:49     ` David Turner
  2015-12-18  4:06     ` Howard Chu
  1 sibling, 2 replies; 73+ messages in thread
From: Junio C Hamano @ 2015-12-11 23:39 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger, Ronnie Sahlberg, David Turner

David Turner <dturner@twopensource.com> writes:

>  struct ref_be {
>  	struct ref_be *next;
>  	const char *name;
>  	ref_transaction_commit_fn *transaction_commit;
> +
> +	pack_refs_fn *pack_refs;
> +	peel_ref_fn *peel_ref;
> +	create_symref_fn *create_symref;
> +
> +	resolve_ref_unsafe_fn *resolve_ref_unsafe;
> +	verify_refname_available_fn *verify_refname_available;
> +	resolve_gitlink_ref_fn *resolve_gitlink_ref;
>  };

This may have been pointed out in the previous reviews by somebody
else, but I think it is more customary to declare a struct member
that is a pointer to a customization function without leading '*',
i.e.

	typedef TYPE (*customize_fn)(ARGS);

        struct vtable {
		...
        	cutomize_fn fn;
		...
	};

in our codebase (cf. string_list::cmp, prio_queue::compare).

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

* Re: [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-11 23:39   ` Junio C Hamano
@ 2015-12-11 23:49     ` David Turner
  2015-12-12  0:23       ` Junio C Hamano
  2015-12-18  4:06     ` Howard Chu
  1 sibling, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-11 23:49 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, mhagger, Ronnie Sahlberg, David Turner

On Fri, 2015-12-11 at 15:39 -0800, Junio C Hamano wrote:
> David Turner <dturner@twopensource.com> writes:
> 
> >  struct ref_be {
> >  	struct ref_be *next;
> >  	const char *name;
> >  	ref_transaction_commit_fn *transaction_commit;
> > +
> > +	pack_refs_fn *pack_refs;
> > +	peel_ref_fn *peel_ref;
> > +	create_symref_fn *create_symref;
> > +
> > +	resolve_ref_unsafe_fn *resolve_ref_unsafe;
> > +	verify_refname_available_fn *verify_refname_available;
> > +	resolve_gitlink_ref_fn *resolve_gitlink_ref;
> >  };
> 
> This may have been pointed out in the previous reviews by somebody
> else, but I think it is more customary to declare a struct member
> that is a pointer to a customization function without leading '*',
> i.e.
> 
> 	typedef TYPE (*customize_fn)(ARGS);
> 
>         struct vtable {
> 		...
>         	cutomize_fn fn;
> 		...
> 	};
> 
> in our codebase (cf. string_list::cmp, prio_queue::compare).

The previous review was here:
http://permalink.gmane.org/gmane.comp.version-control.git/279062

Michael wrote:
> Hmmm, I thought our convention was to define typedefs for functions
> themselves, not for the pointer-to-function; e.g.,
>
>     typedef struct ref_transaction *ref_transaction_begin_fn(struct
> strbuf *err);
>
> (which would require `struct ref_be` to be changed to
>
>         ref_transaction_begin_fn *transaction_begin;

And you agreed.  So I changed it.  Do you want me to change it back?  

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

* Re: [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-11 23:33   ` Junio C Hamano
@ 2015-12-11 23:49     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-11 23:49 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, mhagger, Ronnie Sahlberg, David Turner

On Fri, 2015-12-11 at 15:33 -0800, Junio C Hamano wrote:
> David Turner <dturner@twopensource.com> writes:
> 
> > diff --git a/cache.h b/cache.h
> > index 51c35c3..707455a 100644
> > --- a/cache.h
> > +++ b/cache.h
> > @@ -1111,6 +1111,13 @@ extern char *oid_to_hex(const struct object_id *oid);	/* same static buffer as s
> >  extern int interpret_branch_name(const char *str, int len, struct strbuf *);
> >  extern int get_sha1_mb(const char *str, unsigned char *sha1);
> >  
> > +/*
> > + * Return true iff abbrev_name is a possible abbreviation for
> > + * full_name according to the rules defined by ref_rev_parse_rules in
> > + * refs.c.
> > + */
> > +extern int refname_match(const char *abbrev_name, const char *full_name);
> > +
> >  extern int validate_headref(const char *ref);
> 
> This hunk is strange.  It duplicates what is in refs.h, risking the
> two copies to drift away from each other over time.
> 
> As the only callsites are in remote.c that includes refs.h, this
> probably should be dropped.

Will fix, thanks.

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

* Re: [PATCH 04/16] refs: add do_for_each_per_worktree_ref
  2015-12-03  0:35 ` [PATCH 04/16] refs: add do_for_each_per_worktree_ref David Turner
@ 2015-12-11 23:52   ` Junio C Hamano
  2015-12-12  0:01     ` David Turner
  0 siblings, 1 reply; 73+ messages in thread
From: Junio C Hamano @ 2015-12-11 23:52 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger

David Turner <dturner@twopensource.com> writes:

> Alternate refs backends might still use files to store per-worktree
> refs.  So the files backend's ref-loading infrastructure should be
> available to those backends, just for use on per-worktree refs.  Add
> do_for_each_per_worktree_ref, which iterates over per-worktree refs.

Is this "might still use"?  I am instead getting an impression that,
we are declaring that the recommended way to store per-worktree refs
is with filesystem backend.

Not complaining against either such a design decision (if that is
what is made here) or the above description in the log message--just
want to understand the intention better.

I also wonder if it is cleaner to have a single interface that lets
anybody ask a pointer to the backend struct by name.  With such an
interface, a new backend that wants to delegate per-worktree refs to
files backend could

	static int DB_for_each_ref(...)
        {
		struct ref_be *files = locate_ref_backend("files");
		files->for_each_ref(... | WORKTREE_ONLY, ...);
		... do its own enumeration of non-per-worktree refs ...
	}

and such a delegation does not need to be limited to per-worktree
iteration.

Or is that a road to expose too much implementation details of
a backend to other backends?

> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  refs/files-backend.c | 15 ++++++++++++---
>  refs/refs-internal.h | 10 ++++++++++
>  2 files changed, 22 insertions(+), 3 deletions(-)
>
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index d4bd6cf..bde4892 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -518,9 +518,6 @@ static void sort_ref_dir(struct ref_dir *dir)
>  	dir->sorted = dir->nr = i;
>  }
>  
> -/* Include broken references in a do_for_each_ref*() iteration: */
> -#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
> -
>  /*
>   * Return true iff the reference described by entry can be resolved to
>   * an object in the database.  Emit a warning if the referred-to
> @@ -568,6 +565,10 @@ static int do_one_ref(struct ref_entry *entry, void *cb_data)
>  	struct ref_entry *old_current_ref;
>  	int retval;
>  
> +	if (data->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
> +	    ref_type(entry->name) != REF_TYPE_PER_WORKTREE)
> +		return 0;
> +
>  	if (!starts_with(entry->name, data->base))
>  		return 0;
>  
> @@ -1738,6 +1739,14 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,
>  	return do_for_each_entry(refs, base, do_one_ref, &data);
>  }
>  
> +int do_for_each_per_worktree_ref(const char *submodule, const char *base,
> +				 each_ref_fn fn, int trim, int flags,
> +				 void *cb_data)
> +{
> +	return do_for_each_ref(get_ref_cache(submodule), base, fn, trim,
> +			       flags | DO_FOR_EACH_PER_WORKTREE_ONLY, cb_data);
> +}
> +
>  static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
>  {
>  	struct object_id oid;
> diff --git a/refs/refs-internal.h b/refs/refs-internal.h
> index ad683df..433d0fe 100644
> --- a/refs/refs-internal.h
> +++ b/refs/refs-internal.h
> @@ -42,6 +42,16 @@
>   * value to ref_update::flags
>   */
>  
> +/* Include broken references in a do_for_each_ref*() iteration */
> +#define DO_FOR_EACH_INCLUDE_BROKEN 0x01
> +
> +/* Only include per-worktree refs in a do_for_each_ref*() iteration */
> +#define DO_FOR_EACH_PER_WORKTREE_ONLY 0x02
> +
> +int do_for_each_per_worktree_ref(const char *submodule, const char *base,
> +				 each_ref_fn fn, int trim, int flags,
> +				 void *cb_data);
> +
>  /*
>   * Return true iff refname is minimally safe. "Safe" here means that
>   * deleting a loose reference by this name will not do any damage, for

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

* Re: [PATCH 04/16] refs: add do_for_each_per_worktree_ref
  2015-12-11 23:52   ` Junio C Hamano
@ 2015-12-12  0:01     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-12  0:01 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, mhagger

On Fri, 2015-12-11 at 15:52 -0800, Junio C Hamano wrote:
> David Turner <dturner@twopensource.com> writes:
> 
> > Alternate refs backends might still use files to store per-worktree
> > refs.  So the files backend's ref-loading infrastructure should be
> > available to those backends, just for use on per-worktree refs.  Add
> > do_for_each_per_worktree_ref, which iterates over per-worktree refs.
> 
> Is this "might still use"?  I am instead getting an impression that,
> we are declaring that the recommended way to store per-worktree refs
> is with filesystem backend.
> 
> Not complaining against either such a design decision (if that is
> what is made here) or the above description in the log message--just
> want to understand the intention better.

I'm not super-happy with the notion of splitting data across backends;
it's a bit messy.  I've resigned myself to it because it seems like the
simplest way forward in the presence of per-worktree refs.  But I
figured the message could leave it open in case we change our minds.

> I also wonder if it is cleaner to have a single interface that lets
> anybody ask a pointer to the backend struct by name.  With such an
> interface, a new backend that wants to delegate per-worktree refs to
> files backend could
> 
> 	static int DB_for_each_ref(...)
>         {
> 		struct ref_be *files = locate_ref_backend("files");
> 		files->for_each_ref(... | WORKTREE_ONLY, ...);
> 		... do its own enumeration of non-per-worktree refs ...
> 	}
> 
> and such a delegation does not need to be limited to per-worktree
> iteration.
> 
> Or is that a road to expose too much implementation details of
> a backend to other backends?

Given that the files backend is special in terms of its rights and
responsibilities, I think it's OK to just refer to it directly.  I think
allowing backends to generally pick and choose bits from other backends
would be confusing.

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

* Re: [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-11 23:49     ` David Turner
@ 2015-12-12  0:23       ` Junio C Hamano
  2015-12-12  0:48         ` David Turner
  0 siblings, 1 reply; 73+ messages in thread
From: Junio C Hamano @ 2015-12-12  0:23 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger, Ronnie Sahlberg, David Turner

David Turner <dturner@twopensource.com> writes:

> The previous review was here:
> http://permalink.gmane.org/gmane.comp.version-control.git/279062
>
> Michael wrote:
>> Hmmm, I thought our convention was to define typedefs for functions
>> themselves, not for the pointer-to-function; e.g.,
>>
>>     typedef struct ref_transaction *ref_transaction_begin_fn(struct
>> strbuf *err);
>>
>> (which would require `struct ref_be` to be changed to
>>
>>         ref_transaction_begin_fn *transaction_begin;
>
> And you agreed.  So I changed it.  Do you want me to change it back?  

Sorry about that. I was agreeing to my mistaken understanding of
what was pointed out X-<.

We do prefer fn(args) over (*fn)(args) and somehow I mixed that up
with the declaration style.

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

* Re: [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-12  0:23       ` Junio C Hamano
@ 2015-12-12  0:48         ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-12  0:48 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, mhagger, Ronnie Sahlberg, David Turner

On Fri, 2015-12-11 at 16:23 -0800, Junio C Hamano wrote:
> David Turner <dturner@twopensource.com> writes:
> 
> > The previous review was here:
> > http://permalink.gmane.org/gmane.comp.version-control.git/279062
> >
> > Michael wrote:
> >> Hmmm, I thought our convention was to define typedefs for functions
> >> themselves, not for the pointer-to-function; e.g.,
> >>
> >>     typedef struct ref_transaction *ref_transaction_begin_fn(struct
> >> strbuf *err);
> >>
> >> (which would require `struct ref_be` to be changed to
> >>
> >>         ref_transaction_begin_fn *transaction_begin;
> >
> > And you agreed.  So I changed it.  Do you want me to change it back?  
> 
> Sorry about that. I was agreeing to my mistaken understanding of
> what was pointed out X-<.
> 
> We do prefer fn(args) over (*fn)(args) and somehow I mixed that up
> with the declaration style.

So I should change it back?

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

* Re: [PATCH 15/16] refs: add LMDB refs backend
  2015-12-03  0:35 ` [PATCH 15/16] refs: add LMDB refs backend David Turner
  2015-12-05  0:08   ` Junio C Hamano
@ 2015-12-17  1:00   ` Jonathan Nieder
  2015-12-17  2:31     ` David Turner
  2015-12-23 14:32   ` Michael Haggerty
  2 siblings, 1 reply; 73+ messages in thread
From: Jonathan Nieder @ 2015-12-17  1:00 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger, Jeff King, Shawn Pearce

David Turner wrote:

> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -496,6 +496,13 @@ core.repositoryFormatVersion::
>  	Internal variable identifying the repository format and layout
>  	version.
>  
> +core.refsBackendType::
> +	Type of refs backend. Default is to use the original files
> +	based backend. Set to 'lmdb' to activate the lmdb database
> +	backend.  If you use the lmdb backend,
> +	core.repositoryFormatVersion must be set to 1, and
> +	extensions.refBackend must be set to 'lmdb'.

If core.refsBackendType names one backend and extensions.refBackend
names another, which wins?

Thanks,
Jonathan

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

* Re: [PATCH 15/16] refs: add LMDB refs backend
  2015-12-17  1:00   ` Jonathan Nieder
@ 2015-12-17  2:31     ` David Turner
  2015-12-17 20:49       ` Jonathan Nieder
  0 siblings, 1 reply; 73+ messages in thread
From: David Turner @ 2015-12-17  2:31 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: git, mhagger, Jeff King, Shawn Pearce

On Wed, 2015-12-16 at 17:00 -0800, Jonathan Nieder wrote:
> David Turner wrote:
> 
> > --- a/Documentation/config.txt
> > +++ b/Documentation/config.txt
> > @@ -496,6 +496,13 @@ core.repositoryFormatVersion::
> >  	Internal variable identifying the repository format and
> > layout
> >  	version.
> >  
> > +core.refsBackendType::
> > +	Type of refs backend. Default is to use the original files
> > +	based backend. Set to 'lmdb' to activate the lmdb database
> > +	backend.  If you use the lmdb backend,
> > +	core.repositoryFormatVersion must be set to 1, and
> > +	extensions.refBackend must be set to 'lmdb'.
> 
> If core.refsBackendType names one backend and extensions.refBackend
> names another, which wins?

I've simplified this to just use extensions.refsBackendType for
everything.  Will re-roll once I finish getting comments on the rest of
this series.

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

* Re: [PATCH 15/16] refs: add LMDB refs backend
  2015-12-17  2:31     ` David Turner
@ 2015-12-17 20:49       ` Jonathan Nieder
  0 siblings, 0 replies; 73+ messages in thread
From: Jonathan Nieder @ 2015-12-17 20:49 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger, Jeff King, Shawn Pearce

David Turner wrote:
> On Wed, 2015-12-16 at 17:00 -0800, Jonathan Nieder wrote:
>> David Turner wrote:

>>> +core.refsBackendType::
>>> +	Type of refs backend. Default is to use the original files
>>> +	based backend. Set to 'lmdb' to activate the lmdb database
>>> +	backend.  If you use the lmdb backend,
>>> +	core.repositoryFormatVersion must be set to 1, and
>>> +	extensions.refBackend must be set to 'lmdb'.
>>
>> If core.refsBackendType names one backend and extensions.refBackend
>> names another, which wins?
>
> I've simplified this to just use extensions.refsBackendType for
> everything.  Will re-roll once I finish getting comments on the rest of
> this series.

Perfect.  Thanks for the quick reply.

Jonathan

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

* Re: [PATCH 02/16] refs: add methods for misc ref operations
  2015-12-11 23:39   ` Junio C Hamano
  2015-12-11 23:49     ` David Turner
@ 2015-12-18  4:06     ` Howard Chu
  1 sibling, 0 replies; 73+ messages in thread
From: Howard Chu @ 2015-12-18  4:06 UTC (permalink / raw)
  To: git

Junio C Hamano <gitster <at> pobox.com> writes:

> 
> David Turner <dturner <at> twopensource.com> writes:
> 
> >  struct ref_be {
> >  	struct ref_be *next;
> >  	const char *name;
> >  	ref_transaction_commit_fn *transaction_commit;
> > +
> > +	pack_refs_fn *pack_refs;
> > +	peel_ref_fn *peel_ref;
> > +	create_symref_fn *create_symref;
> > +
> > +	resolve_ref_unsafe_fn *resolve_ref_unsafe;
> > +	verify_refname_available_fn *verify_refname_available;
> > +	resolve_gitlink_ref_fn *resolve_gitlink_ref;
> >  };
> 
> This may have been pointed out in the previous reviews by somebody
> else, but I think it is more customary to declare a struct member
> that is a pointer to a customization function without leading '*',
> i.e.
> 
> 	typedef TYPE (*customize_fn)(ARGS);
> 
>         struct vtable {
> 		...
>         	cutomize_fn fn;
> 		...
> 	};
> 
> in our codebase (cf. string_list::cmp, prio_queue::compare).

(LMDB author here, just passing by)

IMO you're making a mistake. You should always typedef the thing itself, not
a pointer to the thing. If you only typedef the pointer, you can only use
the typedef to declare pointers and never the thing itself. If you typedef
the actual thing, you can use the typedef to declare both the thing and
pointer to thing.

It's particularly useful to have function typedefs because later on you can
use the actual typedef to declare instances of that function type in header
files, and guarantee that your function definitions match what they're
intended to match. Otherwise, assigning a bare (*func) to something that
mismatches only generates a warning, instead of an error.

-- 
  -- Howard Chu
  CTO, Symas Corp.           http://www.symas.com
  Director, Highland Sun     http://highlandsun.com/hyc/
  Chief Architect, OpenLDAP  http://www.openldap.org/project/ 

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

* Re: [PATCH 00/16] LMDB refs backend atop pre-vtable
  2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
                   ` (15 preceding siblings ...)
  2015-12-03  0:35 ` [PATCH 16/16] refs: tests for lmdb backend David Turner
@ 2015-12-22 23:56 ` David Turner
  16 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-22 23:56 UTC (permalink / raw)
  To: git mailing list

On Wed, 2015-12-02 at 19:35 -0500, David Turner wrote:
> I'm starting the patchset numbering over from 1 here, because this
> version of the patchset is a subset of the last version.

I'm going out of town on 12/24 and will return 1/5.  I won't be reading
this email address.  When I get back, I'll read any reviews.

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

* Re: [PATCH 08/16] refs: add methods to init refs backend and db
  2015-12-03  0:35 ` [PATCH 08/16] refs: add methods to init refs backend and db David Turner
@ 2015-12-23  5:33   ` Michael Haggerty
  2015-12-23  6:54     ` David Turner
  0 siblings, 1 reply; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23  5:33 UTC (permalink / raw)
  To: David Turner, git

On 12/03/2015 01:35 AM, David Turner wrote:
> Alternate refs backends might not need the refs/heads directory and so
> on, so we make ref db initialization part of the backend.  We also
> might need to initialize ref backends themselves, so we'll add a
> method for that as well.
> 
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  builtin/init-db.c    | 14 ++++----------
>  refs.c               |  8 +++++++-
>  refs.h               |  4 +++-
>  refs/files-backend.c | 23 +++++++++++++++++++++++
>  refs/refs-internal.h |  4 ++++
>  5 files changed, 41 insertions(+), 12 deletions(-)
> 
> [...]
> diff --git a/refs.c b/refs.c
> index 9a2fed7..bdeb276 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -22,13 +22,14 @@ struct ref_be *refs_backends = &refs_be_files;
>  /*
>   * This function is used to switch to an alternate backend.
>   */
> -int set_refs_backend(const char *name)
> +int set_refs_backend(const char *name, void *data)

The purpose of the data argument is rather mysterious at this point,
especially since set_refs_backend() still doesn't have any callers. I
hope that will become clearer later in the series. Nevertheless, it
would be nice for its use to be described in the docstring (which should
preferably be moved to the header file).

> [...]
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index e769242..6600c02 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -3313,6 +3313,11 @@ static int ref_present(const char *refname,
>  	return string_list_has_string(affected_refnames, refname);
>  }
>  
> +void files_init_backend(void *data)
> +{
> +	/* do nothing */
> +}
> +
>  static int files_initial_transaction_commit(struct ref_transaction *transaction,
>  					    struct strbuf *err)
>  {
> @@ -3534,9 +3539,27 @@ static int files_reflog_expire(const char *refname, const unsigned char *sha1,
>  	return -1;
>  }
>  
> +static int files_init_db(struct strbuf *err, int shared)
> +{
> +	/*
> +	 * Create .git/refs/{heads,tags}
> +	 */
> +	safe_create_dir(git_path("refs"), 1);
> +	safe_create_dir(git_path("refs/heads"), 1);
> +	safe_create_dir(git_path("refs/tags"), 1);
> +	if (shared) {
> +		adjust_shared_perm(git_path("refs"));
> +		adjust_shared_perm(git_path("refs/heads"));
> +		adjust_shared_perm(git_path("refs/tags"));
> +	}
> +	return 0;
> +}
> +
>  struct ref_be refs_be_files = {
>  	NULL,
>  	"files",
> +	files_init_backend,
> +	files_init_db,
>  	files_transaction_commit,
>  	files_initial_transaction_commit,
>  
> diff --git a/refs/refs-internal.h b/refs/refs-internal.h
> index 478ad54..85a0b91 100644
> --- a/refs/refs-internal.h
> +++ b/refs/refs-internal.h
> @@ -208,6 +208,8 @@ const char *find_descendant_ref(const char *dirname,
>  int rename_ref_available(const char *oldname, const char *newname);
>  
>  /* refs backends */
> +typedef void ref_backend_init_fn(void *data);
> +typedef int ref_backend_init_db_fn(struct strbuf *err, int shared);
>  typedef int ref_transaction_commit_fn(struct ref_transaction *transaction,
>  				      struct strbuf *err);
>  
> @@ -267,6 +269,8 @@ typedef int for_each_replace_ref_fn(each_ref_fn fn, void *cb_data);
>  struct ref_be {
>  	struct ref_be *next;
>  	const char *name;
> +	ref_backend_init_fn *init_backend;
> +	ref_backend_init_db_fn *init_db;
>  	ref_transaction_commit_fn *transaction_commit;
>  	ref_transaction_commit_fn *initial_transaction_commit;
>  
> 

Your naming seems inconsistent here. I would have expected a
"files_init_backend()" function to satisfy the typedef
"ref_backend_init_backend_fn" or "ref_init_backend_fn", not
"ref_backend_init_fn". Or, conversely, for the function implementing
"ref_backend_init_fn" to be called "files_init()".

More generally, it would be nice to have a consistent naming pattern
between (1) the name of the typedef, (2) the name of the member in
"struct ref_be", (3) the names of concrete, backend-specific
implementations of the functions.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 11/16] refs: move duplicate check to common code
  2015-12-03  0:35 ` [PATCH 11/16] refs: move duplicate check to common code David Turner
@ 2015-12-23  6:27   ` Michael Haggerty
  2016-01-05 16:42     ` David Turner
  0 siblings, 1 reply; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23  6:27 UTC (permalink / raw)
  To: David Turner, git

On 12/03/2015 01:35 AM, David Turner wrote:
> The check for duplicate refnames in a transaction is needed for
> all backends, so move it to the common code.
> 
> ref_transaction_commit_fn gains a new argument, the sorted
> string_list of affected refnames.
> 
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  refs.c               | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++--
>  refs/files-backend.c | 57 ++++-------------------------------------
>  refs/refs-internal.h |  1 +
>  3 files changed, 75 insertions(+), 54 deletions(-)
> 
> diff --git a/refs.c b/refs.c
> index 1b79630..808053f 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -1093,6 +1093,37 @@ const char *find_descendant_ref(const char *dirname,
>  	return NULL;
>  }
>  
> +/*
> + * Return 1 if there are any duplicate refnames in the updates in
> + * `transaction`, and fill in err with an appropriate error message.
> + * Fill in `refnames` with the refnames from the transaction.
> + */
> +
> +static int ref_update_reject_duplicates(struct ref_transaction *transaction,
> +					struct string_list *refnames,
> +					struct strbuf *err)

I like that you extract this code into a function. Though it feels
awkward to have a function called "ref_update_reject_duplicates()" that
has a side effect of filling the names into a string list. I think it
would feel more natural to call the function get_affected_refnames(),
and treat the duplicate check as an extra bonus.

You could go even farther and split it into two functions,

void get_affected_refnames(struct ref_transaction *transaction,
                           struct string_list *refnames);
int ref_update_reject_duplicates(struct string_list *refnames,
                                 struct strbuf *err);

> +{
> +	int i, n = transaction->nr;
> +	struct ref_update **updates;
> +
> +	assert(err);
> +
> +	updates = transaction->updates;
> +	/* Fail if a refname appears more than once in the transaction: */
> +	for (i = 0; i < n; i++)
> +		string_list_append(refnames, updates[i]->refname);
> +	string_list_sort(refnames);
> +
> +	for (i = 1; i < n; i++)
> +		if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
> +			strbuf_addf(err,
> +				    "Multiple updates for ref '%s' not allowed.",
> +				    refnames->items[i].string);
> +			return 1;
> +		}
> +	return 0;
> +}
> +
>  /* backend functions */
>  int refs_init_db(struct strbuf *err, int shared)
>  {
> @@ -1102,7 +1133,29 @@ int refs_init_db(struct strbuf *err, int shared)
>  int ref_transaction_commit(struct ref_transaction *transaction,
>  			   struct strbuf *err)
>  {
> -	return the_refs_backend->transaction_commit(transaction, err);
> +	int ret = -1;
> +	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
> +
> +	assert(err);
> +
> +	if (transaction->state != REF_TRANSACTION_OPEN)
> +		die("BUG: commit called for transaction that is not open");
> +
> +	if (!transaction->nr) {
> +		transaction->state = REF_TRANSACTION_CLOSED;
> +		return 0;
> +	}
> +
> +	if (ref_update_reject_duplicates(transaction, &affected_refnames, err)) {
> +		ret = TRANSACTION_GENERIC_ERROR;
> +		goto done;
> +	}
> +
> +	ret = the_refs_backend->transaction_commit(transaction,
> +						   &affected_refnames, err);
> +done:
> +	string_list_clear(&affected_refnames, 0);
> +	return ret;
>  }

Here you are avoiding a small amount of code duplication by calling
ref_update_reject_duplicates() here rather than in the backend-specific
code. But you are doing so at the cost of having to compute
affected_refnames here and pass it (redundantly) to the backend's
transaction_commit function. This increases the coupling between these
functions, which in my opinion is worse than the small amount of code
duplication. But maybe it's just me.

The check of transaction->state, on the other hand, makes sense here.

> [...]

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 08/16] refs: add methods to init refs backend and db
  2015-12-23  5:33   ` Michael Haggerty
@ 2015-12-23  6:54     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2015-12-23  6:54 UTC (permalink / raw)
  To: Michael Haggerty, git

On Wed, 2015-12-23 at 06:33 +0100, Michael Haggerty wrote:
> On 12/03/2015 01:35 AM, David Turner wrote:
> > Alternate refs backends might not need the refs/heads directory and
> > so
> > on, so we make ref db initialization part of the backend.  We also
> > might need to initialize ref backends themselves, so we'll add a
> > method for that as well.
> > 
> > Signed-off-by: David Turner <dturner@twopensource.com>
> > ---
> >  builtin/init-db.c    | 14 ++++----------
> >  refs.c               |  8 +++++++-
> >  refs.h               |  4 +++-
> >  refs/files-backend.c | 23 +++++++++++++++++++++++
> >  refs/refs-internal.h |  4 ++++
> >  5 files changed, 41 insertions(+), 12 deletions(-)
> > 
> > [...]
> > diff --git a/refs.c b/refs.c
> > index 9a2fed7..bdeb276 100644
> > --- a/refs.c
> > +++ b/refs.c
> > @@ -22,13 +22,14 @@ struct ref_be *refs_backends = &refs_be_files;
> >  /*
> >   * This function is used to switch to an alternate backend.
> >   */
> > -int set_refs_backend(const char *name)
> > +int set_refs_backend(const char *name, void *data)
> 
> The purpose of the data argument is rather mysterious at this point,
> especially since set_refs_backend() still doesn't have any callers. I
> hope that will become clearer later in the series. Nevertheless, it
> would be nice for its use to be described in the docstring (which
> should
> preferably be moved to the header file).

Will fix.

> >  struct ref_be {
> >  	struct ref_be *next;
> >  	const char *name;
> > +	ref_backend_init_fn *init_backend;
> > +	ref_backend_init_db_fn *init_db;
> >  	ref_transaction_commit_fn *transaction_commit;
> >  	ref_transaction_commit_fn *initial_transaction_commit;
> >  
> > 
> 
> Your naming seems inconsistent here. I would have expected a
> "files_init_backend()" function to satisfy the typedef
> "ref_backend_init_backend_fn" or "ref_init_backend_fn", not
> "ref_backend_init_fn". Or, conversely, for the function implementing
> "ref_backend_init_fn" to be called "files_init()".
> 
> More generally, it would be nice to have a consistent naming pattern
> between (1) the name of the typedef, (2) the name of the member in
> "struct ref_be", (3) the names of concrete, backend-specific
> implementations of the functions.

I'll change this to "refs_init_backend_fn *init_backend" and
"refs_init_db_fn *init_db" (since we already have an init_db function),
and make any other similar changes I happen to notice.

On the naming in general, I am somewhat at the mercy of history here. 
 For example, git presently has functions named ref_transaction_commit
(object_verb) and create_reflog (verb_object).  So nothing I do will be
consistent with everything.  I could, of course, do some initial
commits to clean up the naming, but that would create code churn.

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

* Re: [PATCH 12/16] refs: always handle non-normal refs in files backend
  2015-12-03  0:35 ` [PATCH 12/16] refs: always handle non-normal refs in files backend David Turner
@ 2015-12-23  8:02   ` Michael Haggerty
  2016-01-06  0:13     ` David Turner
                       ` (2 more replies)
  0 siblings, 3 replies; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23  8:02 UTC (permalink / raw)
  To: David Turner, git

On 12/03/2015 01:35 AM, David Turner wrote:
> Always handle non-normal (per-worktree or pseudo) refs in the files
> backend instead of alternate backends.
> 
> Sometimes a ref transaction will update both a per-worktree ref and a
> normal ref.  For instance, an ordinary commit might update
> refs/heads/master and HEAD (or at least HEAD's reflog).
> 
> We handle three cases here:
> 
> 1. updates to normal refs continue to go through the chosen backend
> 
> 2. updates to non-normal refs with REF_NODEREF or to non-symbolic refs
> are moved to a separate files backend transaction.
> 
> 3. updates to symbolic refs are dereferenced to their base ref.  The
> update to the base ref then goes through the ordinary backend, while
> the files backend is directly called to update the symref's reflog.
> 
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  refs.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 139 insertions(+), 2 deletions(-)
> 
> diff --git a/refs.c b/refs.c
> index 808053f..e48e43a 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -9,6 +9,11 @@
>  #include "object.h"
>  #include "tag.h"
>  
> +const char split_transaction_fail_warning[] =
> +	"A ref transaction was split across two refs backends.  Part of the "
> +	"transaction succeeded, but then the update to the per-worktree refs "
> +	"failed.  Your repository may be in an inconsistent state.";
> +
>  /*
>   * We always have a files backend and it is the default.
>   */
> @@ -784,6 +789,13 @@ void ref_transaction_free(struct ref_transaction *transaction)
>  	free(transaction);
>  }
>  
> +static void add_update_obj(struct ref_transaction *transaction,
> +			   struct ref_update *update)
> +{
> +	ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
> +	transaction->updates[transaction->nr++] = update;
> +}
> +
>  static struct ref_update *add_update(struct ref_transaction *transaction,
>  				     const char *refname)
>  {
> @@ -791,8 +803,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
>  	struct ref_update *update = xcalloc(1, sizeof(*update) + len);
>  
>  	memcpy((char *)update->refname, refname, len); /* includes NUL */
> -	ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
> -	transaction->updates[transaction->nr++] = update;
> +	add_update_obj(transaction, update);
>  	return update;
>  }
>  
> @@ -1130,11 +1141,87 @@ int refs_init_db(struct strbuf *err, int shared)
>  	return the_refs_backend->init_db(err, shared);
>  }
>  
> +/*
> + * Special case for non-normal refs.  For symbolic-refs when
> + * REF_NODEREF is not turned on, we dereference them here and replace
> + * updates to the symbolic refs with updates to the underlying ref.
> + * Then we do our own reflogging for the symbolic ref.
> + *
> + * We move other non-normal ref updates with into a specially-created
> + * files-backend transaction
> + */

Extra word? s/with//?

> +static int move_abnormal_ref_updates(struct ref_transaction *transaction,
> +				     struct ref_transaction *files_transaction,
> +				     struct string_list *symrefs)
> +{
> +	int i;
> +
> +	for (i = 0; i < transaction->nr; i++) {
> +		struct ref_update *update = transaction->updates[i];
> +		const char *resolved;
> +		int flags = 0;
> +		unsigned char sha1[20];
> +
> +		if (ref_type(update->refname) == REF_TYPE_NORMAL)
> +			continue;
> +
> +		resolved = resolve_ref_unsafe(update->refname, 0, sha1, &flags);
> +
> +		if (update->flags & REF_NODEREF || !(flags & REF_ISSYMREF)) {
> +			int last;
> +
> +			add_update_obj(files_transaction, update);
> +			/*
> +			 * Replace this transaction with the
> +			 * last transaction, removing it from
> +			 * the list of backend transactions
> +			 */
> +			last = --transaction->nr;
> +			transaction->updates[i] = transaction->updates[last];

The "last" temporary variable could be trivially inlined.

> +			continue;
> +		}
> +
> +		if (resolved) {
> +			struct ref_update *new_update;
> +			struct string_list_item *item;
> +
> +			if (ref_type(resolved) != REF_TYPE_NORMAL)
> +				die("Non-normal symbolic ref `%s` points to non-normal ref `%s`", update->refname, resolved);

We don't usually use backticks in error messages. Please use "'" instead.

Also, please store this error message into a "strbuf *err" and report it
via the usual mechanism.

> +			new_update = xmalloc(sizeof(*new_update) +
> +					     strlen(resolved) + 1);
> +			memcpy(new_update, update, sizeof(*update));

Wouldn't it be preferable to replace this messy replacement code
(including the memcpy(), which can't be checked by the type system) with
a call to ref_transaction_update() followed by moving the new update to
this position in the list and possibly tweaking some of its fields?

> +			if (update->flags & REF_HAVE_OLD &&
> +			    hashcmp(sha1, update->old_sha1)) {
> +				/* consistency check failed */
> +				free(new_update);
> +				return -1;

We need an error message to be reported in this case; i.e., via a
"struct strbuf *err" argument.

But actually, I don't understand why this check is needed here at all.
Isn't it redundant with a similar check that will be done later (and
properly, under lock) as part of the main ref_transaction_commit()?

> +			} else {
> +				hashcpy(update->old_sha1, sha1);
> +			}
> +
> +			strcpy((char *)new_update->refname, resolved);
> +			transaction->updates[i] = new_update;
> +
> +			item = string_list_append(symrefs, update->refname);
> +			item->util = new_update;
> +			free(update);
> +		}
> +	}
> +
> +	return 0;
> +}
> +
>  int ref_transaction_commit(struct ref_transaction *transaction,
>  			   struct strbuf *err)
>  {
>  	int ret = -1;
>  	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
> +	struct string_list files_affected_refnames = STRING_LIST_INIT_NODUP;
> +	struct string_list symrefs = STRING_LIST_INIT_DUP;
> +	struct string_list_item *item;
> +	struct ref_transaction *files_transaction = NULL;
>  
>  	assert(err);
>  
> @@ -1146,6 +1233,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,
>  		return 0;
>  	}
>  
> +	if (the_refs_backend != &refs_be_files) {
> +		files_transaction = ref_transaction_begin(err);
> +		if (!files_transaction)
> +			die("%s", err->buf);

I think dying here is too abrupt. Some callers try to recover from a
failed ref_transaction_commit(). Couldn't you "goto done" and let the
caller deal with err?

> +		ret = move_abnormal_ref_updates(transaction, files_transaction,
> +						&symrefs);
> +		if (ret)
> +			goto done;
> +
> +		/* files backend commit */
> +		if (ref_update_reject_duplicates(files_transaction,
> +						 &files_affected_refnames,
> +						 err)) {
> +			ret = TRANSACTION_GENERIC_ERROR;
> +			goto done;
> +		}

Is it correct to reject_duplicates among "abnormal" references and
"normal" references separately? ***

> +	}
> +
> +	/* main backend commit */
>  	if (ref_update_reject_duplicates(transaction, &affected_refnames, err)) {
>  		ret = TRANSACTION_GENERIC_ERROR;
>  		goto done;
> @@ -1153,8 +1260,35 @@ int ref_transaction_commit(struct ref_transaction *transaction,
>  
>  	ret = the_refs_backend->transaction_commit(transaction,
>  						   &affected_refnames, err);
> +	if (ret)
> +		goto done;
> +
> +	if (the_refs_backend != &refs_be_files) {

This conditional would perhaps be more to the point if expressed as "if
(files_transaction)".

> +		ret = refs_be_files.transaction_commit(files_transaction,
> +						       &files_affected_refnames,
> +						       err);
> +		if (ret) {
> +			warning(split_transaction_fail_warning);
> +			goto done;
> +		}
> +
> +		/* reflogging for dereferenced symbolic refs */
> +		for_each_string_list_item(item, &symrefs) {
> +			struct ref_update *update = item->util;
> +			if (files_log_ref_write(item->string, update->old_sha1,
> +						update->new_sha1,
> +						update->msg, update->flags, err))
> +				warning("failed to log ref update for symref %s",
> +					item->string);
> +		}

I think this code is incorrect because it doesn't lock the symbolic
reference before modifying its reflog (though I seem to recall that the
old code was buggy in this respect, too).

I wonder whether it would be simpler overall to leave the ref_update for
the symbolic ref in the files_transaction, but set a special internal
internal flag like REF_LOG_ONLY which tells the usual transaction_commit
code to add a reflog entry for update->old_sha1 to update->new_sha1,
without actually changing the reference.

> +	}
> +
>  done:
>  	string_list_clear(&affected_refnames, 0);
> +	string_list_clear(&files_affected_refnames, 0);
> +	if (files_transaction)
> +		ref_transaction_free(files_transaction);
> +	string_list_clear(&symrefs, 0);
>  	return ret;
>  }
>  
> @@ -1210,6 +1344,9 @@ int peel_ref(const char *refname, unsigned char *sha1)
>  int create_symref(const char *ref_target, const char *refs_heads_master,
>  		  const char *logmsg)
>  {
> +	if (ref_type(ref_target) != REF_TYPE_NORMAL)
> +		return refs_be_files.create_symref(ref_target, refs_heads_master,
> +						   logmsg);
>  	return the_refs_backend->create_symref(ref_target, refs_heads_master,
>  					       logmsg);
>  }
> 

I very much like the idea of introducing special handling for symbolic
reference updates within a transaction. In fact, I think I would go even
farther:

Let's take the example of an update to HEAD, which currently points at
refs/heads/master. I think it would *always* be a good idea (i.e., even
when only the files backend is in use) to split that ref_update into two
ref_updates:

1. One to update refs/heads/master and add a reflog entry for that
   reference.

2. One to add a reflog entry for HEAD (i.e. using the new REF_LOG_ONLY
   flag suggested above).

Why?

* It ensures that both references are locked correctly while their
  reflogs are updated. (I believe the current code gets this wrong.)

* It improves the reject_duplicates coverage, which (I think) currently
  wouldn't detect the conflict between a direct update of
  refs/heads/master with a simultaneous update of the same reference
  via HEAD.

* It could later be generalized to an update that goes through multiple
  layers of symref indirection (though this would be a very low
  priority).

This might benefit the split-backend situation that you are implementing
here. You could first do the symref-splitting step I just described, and
*then* separate the non-normal from the normal refs. I think the net
result would be simpler.

This patch is a lot to digest. I'm not yet confident that I have thought
through all of the ramifications of this patch. I guess a few iterations
will be needed in any case.

By the way, all of the patches preceding this one that I haven't
commented on look OK to me.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-05  7:44     ` Jeff King
  2015-12-08  0:38       ` David Turner
@ 2015-12-23  9:52       ` Michael Haggerty
  2015-12-23 20:01         ` Jeff King
  1 sibling, 1 reply; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23  9:52 UTC (permalink / raw)
  To: Jeff King, Duy Nguyen; +Cc: David Turner, Git Mailing List

On 12/05/2015 08:44 AM, Jeff King wrote:
> [...]
> I think the config option needs to be extensions.refsBackendType, too,
> per the logic in 00a09d5 (introduce "extensions" form of
> core.repositoryformatversion, 2015-06-23). And I guess it needs to bump
> core.repositoryformatversion to "1".

I think also, strictly speaking, the extensions.refsBackendType option
should be ignored if core.repositoryFormatVersion is not "1". In
practice, it probably makes more sense for the code to error out in that
case because it is likely the result of a bug.

Note that if the user explicitly chooses the "files" backend, it would
be preferable to leave "core.repositoryFormatVersion" at "0" (assuming
that no other extension is being used) and leave
"extensions.refsBackendType" unset. This approach creates a repository
that is compatible with older clients that don't know about
refsBackendTypes.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* [PATCH] clone: use child_process for recursive checkouts
  2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
                     ` (2 preceding siblings ...)
  2015-12-10 18:02   ` Jeff King
@ 2015-12-23 11:30   ` Michael Haggerty
  2016-01-06 23:41     ` David Turner
  2015-12-23 13:34   ` [PATCH 13/16] init: allow alternate backends to be set for new repos Michael Haggerty
  4 siblings, 1 reply; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23 11:30 UTC (permalink / raw)
  To: David Turner; +Cc: Junio C Hamano, Duy Nguyen, Jeff King, git, Michael Haggerty

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
David, I think if you insert this patch before your

  13/16 refs: allow ref backend to be set for clone

, then the hunk in builtin/clone.c:checkout() of your patch becomes
trivial:

	if (refs_backend_type)
		argv_array_pushf(&cmd.args, "--refs-backend-type=%s",
				 refs_backend_type);

 builtin/clone.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index caae43e..53c2834 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -95,10 +95,6 @@ static struct option builtin_clone_options[] = {
 	OPT_END()
 };
 
-static const char *argv_submodule[] = {
-	"submodule", "update", "--init", "--recursive", NULL
-};
-
 static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
 {
 	static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
@@ -724,8 +720,14 @@ static int checkout(void)
 	err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
 			   sha1_to_hex(sha1), "1", NULL);
 
-	if (!err && option_recursive)
-		err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
+	if (!err && option_recursive) {
+		struct child_process cmd = CHILD_PROCESS_INIT;
+
+		cmd.git_cmd = 1;
+		argv_array_pushl(&cmd.args, "submodule", "update",
+				 "--init", "--recursive", NULL);
+		err = run_command(&cmd);
+	}
 
 	return err;
 }
-- 
2.6.4

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
                     ` (3 preceding siblings ...)
  2015-12-23 11:30   ` [PATCH] clone: use child_process for recursive checkouts Michael Haggerty
@ 2015-12-23 13:34   ` Michael Haggerty
  2016-01-05 17:26     ` David Turner
  2016-01-06 12:52     ` Duy Nguyen
  4 siblings, 2 replies; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23 13:34 UTC (permalink / raw)
  To: David Turner, git

On 12/03/2015 01:35 AM, David Turner wrote:
> git init learns a new argument --refs-backend-type.  Presently, only
> "files" is supported, but later we will add other backends.
> 
> When this argument is used, the repository's core.refsBackendType
> configuration value is set, and the refs backend's initdb function is
> used to set up the ref database.
> 
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  Documentation/git-init-db.txt |  2 +-
>  Documentation/git-init.txt    |  6 +++++-
>  builtin/init-db.c             | 10 ++++++++++
>  cache.h                       |  2 ++
>  config.c                      | 20 ++++++++++++++++++++
>  environment.c                 |  1 +
>  path.c                        | 32 ++++++++++++++++++++++++++++++--
>  refs.c                        |  8 ++++++++
>  refs.h                        | 12 ++++++++++++
>  setup.c                       | 10 ++++++++++
>  10 files changed, 99 insertions(+), 4 deletions(-)
> 
> diff --git a/Documentation/git-init-db.txt b/Documentation/git-init-db.txt
> index 648a6cd..72fbd71 100644
> --- a/Documentation/git-init-db.txt
> +++ b/Documentation/git-init-db.txt
> @@ -9,7 +9,7 @@ git-init-db - Creates an empty Git repository
>  SYNOPSIS
>  --------
>  [verse]
> -'git init-db' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]]
> +'git init-db' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [--refs-backend-type=<name>]
>  
>  
>  DESCRIPTION
> diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
> index 8174d27..9ea6753 100644
> --- a/Documentation/git-init.txt
> +++ b/Documentation/git-init.txt
> @@ -12,7 +12,7 @@ SYNOPSIS
>  'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
>  	  [--separate-git-dir <git dir>]
>  	  [--shared[=<permissions>]] [directory]
> -
> +	  [--refs-backend-type=<name>]

ISTM that "backend" (used here in this option name, and in the manpage)
is not such a meaningful term to users. Could we pick a less obscure
term? E.g., maybe "--ref-storage=<name>"?


>  DESCRIPTION
>  -----------
> @@ -113,6 +113,10 @@ does not exist, it will be created.
>  
>  --
>  
> +--refs-backend-type=<name>::
> +Type of refs backend. Default is to use the original "files" backend,
> +which stores ref data in files in .git/refs and .git/packed-refs.
> +
>  TEMPLATE DIRECTORY
>  ------------------
>  
> diff --git a/builtin/init-db.c b/builtin/init-db.c
> index 4771e7e..44db591 100644
> --- a/builtin/init-db.c
> +++ b/builtin/init-db.c
> @@ -204,6 +204,14 @@ static int create_default_files(const char *template_path)
>  		adjust_shared_perm(get_git_dir());
>  	}
>  
> +	if (refs_backend_type) {
> +		struct refdb_config_data config_data = {NULL};
> +		git_config_set("core.refsBackendType", refs_backend_type);
> +		config_data.refs_backend_type = refs_backend_type;
> +		config_data.refs_base = get_git_dir();
> +		set_refs_backend(refs_backend_type, &config_data);
> +	}
> +

Here is the source of the "void *data" argument that puzzled me in patch
08/16. I'm still puzzled. This code, which is later also used for the
LMDB backend, *always* passes that function a "struct refdb_config_data
*". So why is the argument declared to be "void *"?

If, on the other hand, you want to preserve a backend's freedom to
require different extra data in this parameter, then this code in
init-db.c, and code like it elsewhere, would have to know about the
reference backends so that it knows what data to pass to each one. In
that case, there would be no reason to make this function virtual; this
code could just as well call a different function (with a different
signature) depending on the backend that is in use.

Either way, something seems strange here.

(Remember that another alternative is to let the refs backend read
whatever specialized information it needs from the config by itself.)

>  	if (refs_init_db(&err, shared_repository))
>  		die("failed to set up refs db: %s", err.buf);
>  
> @@ -469,6 +477,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
>  		OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
>  		OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
>  			   N_("separate git dir from working tree")),
> +		OPT_STRING(0, "refs-backend-type", &refs_backend_type,
> +			   N_("name"), N_("name of backend type to use")),
>  		OPT_END()
>  	};
>  
> diff --git a/cache.h b/cache.h
> index 707455a..d1534db 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -696,6 +696,8 @@ extern enum object_creation_mode object_creation_mode;
>  
>  extern char *notes_ref_name;
>  
> +extern const char *refs_backend_type;
> +
>  extern int grafts_replace_parents;
>  
>  /*
> diff --git a/config.c b/config.c
> index 248a21a..210aa08 100644
> --- a/config.c
> +++ b/config.c
> @@ -10,6 +10,7 @@
>  #include "exec_cmd.h"
>  #include "strbuf.h"
>  #include "quote.h"
> +#include "refs.h"
>  #include "hashmap.h"
>  #include "string-list.h"
>  #include "utf8.h"
> @@ -1207,6 +1208,25 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
>  	}
>  
>  	if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
> +		struct refdb_config_data refdb_data = {NULL};
> +		char *repo_config_copy;
> +
> +		/*
> +		 * make sure we always read the backend config from the
> +		 * core section on startup
> +		 */
> +		ret += git_config_from_file(refdb_config, repo_config,
> +					    &refdb_data);
> +
> +		repo_config_copy = xstrdup(repo_config);
> +		refdb_data.refs_base = xstrdup(dirname(repo_config_copy));
> +		free(repo_config_copy);
> +
> +		if (refdb_data.refs_backend_type &&
> +		    strcmp(refdb_data.refs_backend_type, "files")) {
> +			die("Unexpected backend %s", refdb_data.refs_backend_type);
> +		}

The refs code already maintains a list of valid backend names. It would
be better to ask it to validate this parameter than to maintain a second
list here. Or just call set_refs_backend() and let *it* fail.

> +
>  		ret += git_config_from_file(fn, repo_config, data);
>  		found += 1;
>  	}
> diff --git a/environment.c b/environment.c
> index 2da7fe2..8dbf0ab 100644
> --- a/environment.c
> +++ b/environment.c
> @@ -66,6 +66,7 @@ int merge_log_config = -1;
>  int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
>  struct startup_info *startup_info;
>  unsigned long pack_size_limit_cfg;
> +const char *refs_backend_type;
>  
>  #ifndef PROTECT_HFS_DEFAULT
>  #define PROTECT_HFS_DEFAULT 0
> diff --git a/path.c b/path.c
> index 3cd155e..86a8035 100644
> --- a/path.c
> +++ b/path.c
> @@ -2,6 +2,7 @@
>   * Utilities for paths and pathnames
>   */
>  #include "cache.h"
> +#include "refs.h"
>  #include "strbuf.h"
>  #include "string-list.h"
>  #include "dir.h"
> @@ -510,9 +511,36 @@ int validate_headref(const char *path)
>  	unsigned char sha1[20];
>  	int fd;
>  	ssize_t len;
> +	struct refdb_config_data refdb_data = {NULL, NULL};
> +
> +	if (lstat(path, &st) < 0) {
> +		int backend_type_set;
> +		struct strbuf config_path = STRBUF_INIT;
> +		if (path) {
> +			char *pathdup = xstrdup(path);
> +			char *git_dir = dirname(pathdup);
> +			strbuf_addf(&config_path, "%s/%s", git_dir, "config");
> +			free(pathdup);
> +		} else {
> +			strbuf_addstr(&config_path, "config");
> +		}
>  
> -	if (lstat(path, &st) < 0)
> -		return -1;
> +		if (git_config_from_file(refdb_config, config_path.buf, &refdb_data)) {
> +			strbuf_release(&config_path);
> +			return -1;
> +		}
> +
> +		backend_type_set = !!refdb_data.refs_backend_type;
> +		free((void *)refdb_data.refs_backend_type);
> +		free((void *)refdb_data.refs_base);
> +		strbuf_release(&config_path);
> +		/*
> +		 * Alternate backends are assumed to keep HEAD
> +		 * in a valid state, so there's no need to do
> +		 * further validation.
> +		 */
> +		return backend_type_set ? 0 : -1;
> +	}

I'm not sure what problem you are trying to solve with this particular
change. Won't a lmdb-backed repository still have a "HEAD" file with the
usual contents?

In any case, it seems like you are doing work that belongs at a higher
level.

validate_headref() has only one caller, is_git_directory(). The purpose
of that function is to check whether a specified directory is a
plausible Git directory, which it does by looking for some required files:

* "objects/" (unless GIT_OBJECT_DIRECTORY is set)
* "refs/"
* "HEAD" (which must also have appropriate contents)

NB: it doesn't care whether there is a "config" file. Presumably that
was invented later.

It seems to me that whether a repository should have a "refs/" and/or
"HEAD", and if so what contents "HEAD" should have, or whether (for
example) it should have a "refdb" file, depends on what reference
backend is in use. But *that* depends on the contents of "config" or its
absence.

You are reading "config" down here in validate_headref(). It feels to me
more like is_git_directory() should read "config", determine which
reference backend seems to be in use, and then ask *that* reference
backend whether the directory is a plausible Git directory. The files
backend would check for "HEAD" and "refs/" and the LMDB backend would
check for "HEAD" and "refdb/".

Note that this approach would require "config" to be read and parsed an
extra time. That might be considered too expensive to do so early in the
startup code. I don't know.

By the way, this code ends up reading "config" files a bit more
promiscuously than the old code. I don't think this raises any security
considerations, but I wanted to point it out anyway. If somebody can put
a hostile "config" file in a directory where you are running git, then
they can probably put an "objects/", "refs/", and "HEAD" file there,
which is all that is needed to get even the old code to read the
"config" file.

>  	/* Make sure it is a "refs/.." symlink */
>  	if (S_ISLNK(st.st_mode)) {
> diff --git a/refs.c b/refs.c
> index e48e43a..96e1673 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -24,6 +24,14 @@ struct ref_be *the_refs_backend = &refs_be_files;
>   */
>  struct ref_be *refs_backends = &refs_be_files;
>  
> +const char *refs_backend_type;
> +
> +void register_refs_backend(struct ref_be *be)
> +{
> +	be->next = refs_backends;
> +	refs_backends = be;
> +}
> +
>  /*
>   * This function is used to switch to an alternate backend.
>   */
> diff --git a/refs.h b/refs.h
> index c211b9e..c3670e8 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -510,6 +510,18 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,
>  			 reflog_expiry_cleanup_fn cleanup_fn,
>  			 void *policy_cb_data);
>  
> +struct refdb_config_data {
> +	const char *refs_backend_type;
> +	const char *refs_base;
> +};
> +/*
> + * Read the refdb configuration data out of the config file
> + */
> +int refdb_config(const char *var, const char *value, void *ptr);
> +
> +struct ref_be;
>  int set_refs_backend(const char *name, void *data);
>  
> +void register_refs_backend(struct ref_be *be);
> +
>  #endif /* REFS_H */
> diff --git a/setup.c b/setup.c
> index d343725..de6b8ac 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -1,5 +1,6 @@
>  #include "cache.h"
>  #include "dir.h"
> +#include "refs.h"
>  #include "string-list.h"
>  
>  static int inside_git_dir = -1;
> @@ -263,6 +264,15 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
>  	return ret;
>  }
>  
> +int refdb_config(const char *var, const char *value, void *ptr)
> +{
> +       struct refdb_config_data *cdata = ptr;
> +
> +       if (!strcmp(var, "core.refsbackendtype"))
> +	       cdata->refs_backend_type = xstrdup((char *)value);

Unnecessary cast.

> +       return 0;
> +}
> +
>  /*
>   * Test if it looks like we're at a git directory.
>   * We want to see:

I can guarantee that before long somebody will want a config option that
can be put in a global .gitconfig to choose the default refs backend
type for new repositories when it is not specified explicitly via the
command line. But that can wait :-)

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 14/16] refs: allow ref backend to be set for clone
  2015-12-03  0:35 ` [PATCH 14/16] refs: allow ref backend to be set for clone David Turner
@ 2015-12-23 13:51   ` Michael Haggerty
  2015-12-23 20:23     ` Eric Sunshine
  0 siblings, 1 reply; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23 13:51 UTC (permalink / raw)
  To: David Turner, git; +Cc: Jens Lehmann, Heiko Voigt

On 12/03/2015 01:35 AM, David Turner wrote:
> Add a new option, --refs-backend-type, to allow the ref backend type to
> be set on new clones.
> 
> Submodules must use the same ref backend as the parent repository, so

^^^ is this verified anywhere? Hint: if not, it probably should be. For
example, imagine a future where an existing repository can be migrated
in place from files -> lmdb. If somebody runs that command in a
submodule, this invariant would be broken and *this* version of git
would have to at least detect the problem and refuse to corrupt things.

I added CC to a couple submodules experts in case they have any
feedback. Are there any other code paths that can lead to submodules
being newly cloned, where this option would also have to be passed?

> we also pass the --refs-backend-type option option when cloning
> submodules.
> 
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  Documentation/git-clone.txt |  4 ++++
>  builtin/clone.c             | 27 +++++++++++++++++++++++++--
>  builtin/submodule--helper.c |  5 ++++-
>  cache.h                     |  1 +
>  4 files changed, 34 insertions(+), 3 deletions(-)
> 
> diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
> index 6bf000d..431575b 100644
> --- a/Documentation/git-clone.txt
> +++ b/Documentation/git-clone.txt
> @@ -14,6 +14,7 @@ SYNOPSIS
>  	  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
>  	  [--dissociate] [--separate-git-dir <git dir>]
>  	  [--depth <depth>] [--[no-]single-branch]
> +	  [--refs-backend-type=<name>]
>  	  [--recursive | --recurse-submodules] [--] <repository>
>  	  [<directory>]
>  
> @@ -221,6 +222,9 @@ objects from the source repository into a pack in the cloned repository.
>  	The result is Git repository can be separated from working
>  	tree.
>  
> +--refs-backend-type=<name>::
> +	Type of refs backend. Default is to use the original files based
> +	backend.
>  
>  <repository>::
>  	The (possibly remote) repository to clone from.  See the
> diff --git a/builtin/clone.c b/builtin/clone.c
> index caae43e..a53f341 100644
> --- a/builtin/clone.c
> +++ b/builtin/clone.c
> @@ -92,11 +92,13 @@ static struct option builtin_clone_options[] = {
>  		   N_("separate git dir from working tree")),
>  	OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
>  			N_("set config inside the new repository")),
> +	OPT_STRING(0, "refs-backend-type", &refs_backend_type,
> +		   N_("name"), N_("name of backend type to use")),
>  	OPT_END()
>  };
>  
>  static const char *argv_submodule[] = {
> -	"submodule", "update", "--init", "--recursive", NULL
> +	"submodule", "update", "--init", "--recursive", NULL, NULL
>  };
>  
>  static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
> @@ -724,8 +726,24 @@ static int checkout(void)
>  	err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
>  			   sha1_to_hex(sha1), "1", NULL);
>  
> -	if (!err && option_recursive)
> +	if (!err && option_recursive) {
> +		const char **backend_arg = argv_submodule;
> +		char *new_backend_arg = NULL;
> +		if (refs_backend_type) {
> +			while (*backend_arg)
> +				++backend_arg;
> +
> +			new_backend_arg = xmalloc(21 + strlen(refs_backend_type));
> +			sprintf(new_backend_arg, "--refs-backend-type=%s",
> +				refs_backend_type);
> +			*backend_arg = new_backend_arg;
> +		}
>  		err = run_command_v_opt(argv_submodule, RUN_GIT_CMD);
> +		if (refs_backend_type) {
> +			free(new_backend_arg);
> +			*backend_arg = NULL;
> +		}
> +	}

I sent a separate email about this hunk:


http://mid.gmane.org/c21eb4a5d3a3a4886c45da0abe307fe1772e932e.1450869637.git.mhagger@alum.mit.edu

>  
>  	return err;
>  }
> @@ -744,6 +762,11 @@ static void write_config(struct string_list *config)
>  					       write_one_config, NULL) < 0)
>  			die("unable to write parameters to config file");
>  	}
> +
> +	if (refs_backend_type &&
> +	    write_one_config("core.refsBackendType",
> +			     refs_backend_type, NULL) < 0)
> +		die("unable to write backend parameter to config file");
>  }
>  
>  static void write_refspec_config(const char *src_ref_prefix,
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index f4c3eff..5c9ca4e 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -140,7 +140,10 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
>  		argv_array_pushl(&cp.args, "--reference", reference, NULL);
>  	if (gitdir && *gitdir)
>  		argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
> -
> +	if (refs_backend_type && *refs_backend_type) {
> +		argv_array_push(&cp.args, "--refs-backend-type");
> +		argv_array_push(&cp.args, refs_backend_type);
> +	}

This could be shortened to

    argv_array_pushf(&cp.args, "--refs-backend-type=%s", refs_backend_type);

>  	argv_array_push(&cp.args, url);
>  	argv_array_push(&cp.args, path);
>  
> diff --git a/cache.h b/cache.h
> index d1534db..8f2ca55 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -695,6 +695,7 @@ enum object_creation_mode {
>  extern enum object_creation_mode object_creation_mode;
>  
>  extern char *notes_ref_name;
> +extern const char *refs_backend_type;
>  
>  extern const char *refs_backend_type;

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 15/16] refs: add LMDB refs backend
  2015-12-03  0:35 ` [PATCH 15/16] refs: add LMDB refs backend David Turner
  2015-12-05  0:08   ` Junio C Hamano
  2015-12-17  1:00   ` Jonathan Nieder
@ 2015-12-23 14:32   ` Michael Haggerty
  2016-01-08 16:05     ` David Turner
  2 siblings, 1 reply; 73+ messages in thread
From: Michael Haggerty @ 2015-12-23 14:32 UTC (permalink / raw)
  To: David Turner, git

On 12/03/2015 01:35 AM, David Turner wrote:
> Add a database backend for refs using LMDB.  This backend runs git
> for-each-ref about 30% faster than the files backend with fully-packed
> refs on a repo with ~120k refs.  It's also about 4x faster than using
> fully-unpacked refs.  In addition, and perhaps more importantly, it
> avoids case-conflict issues on OS X.
> 
> LMDB has a few features that make it suitable for usage in git:
> 
> 1. It is relatively lightweight; it requires only one header file, and
> the library code takes under 64k at runtime.
> 
> 2. It is well-tested: it's been used in OpenLDAP for years.
> 
> 3. It's very fast.  LMDB's benchmarks show that it is among
> the fastest key-value stores.
> 
> 4. It has a relatively simple concurrency story; readers don't
> block writers and writers don't block readers.
> 
> Ronnie Sahlberg's original version of this patchset used tdb.  The
> major disadvantage of tdb is that tdb is hard to build on OS X.  It's
> also not in homebrew.  So lmdb seemed simpler.
> 
> To test this backend's correctness, I hacked test-lib.sh and
> test-lib-functions.sh to run all tests under the refs backend. Dozens
> of tests use manual ref/reflog reading/writing, or create submodules
> without passing --refs-backend-type to git init.  If those tests are
> changed to use the update-ref machinery or test-refs-lmdb-backend (or,
> in the case of packed-refs, corrupt refs, and dumb fetch tests, are
> skipped), the only remaining failing tests are the git-new-workdir
> tests and the gitweb tests.
> 
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  .gitignore                                     |    1 +
>  Documentation/config.txt                       |    7 +
>  Documentation/git-clone.txt                    |    2 +-
>  Documentation/git-init.txt                     |    3 +-
>  Documentation/technical/refs-lmdb-backend.txt  |   50 +
>  Documentation/technical/repository-version.txt |    5 +
>  Makefile                                       |   12 +
>  builtin/init-db.c                              |   10 +-
>  config.c                                       |   20 +-
>  configure.ac                                   |   33 +
>  contrib/workdir/git-new-workdir                |    3 +
>  refs.h                                         |    1 +
>  refs/lmdb-backend.c                            | 2054 ++++++++++++++++++++++++
>  setup.c                                        |   22 +-
>  test-refs-lmdb-backend.c                       |   68 +
>  15 files changed, 2279 insertions(+), 12 deletions(-)
>  create mode 100644 Documentation/technical/refs-lmdb-backend.txt
>  create mode 100644 refs/lmdb-backend.c
>  create mode 100644 test-refs-lmdb-backend.c
> 
> diff --git a/.gitignore b/.gitignore
> index 1c2f832..87d45a2 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -199,6 +199,7 @@
>  /test-path-utils
>  /test-prio-queue
>  /test-read-cache
> +/test-refs-lmdb-backend
>  /test-regex
>  /test-revision-walking
>  /test-run-command
> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index f617886..5fb25ed 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -496,6 +496,13 @@ core.repositoryFormatVersion::
>  	Internal variable identifying the repository format and layout
>  	version.
>  
> +core.refsBackendType::
> +	Type of refs backend. Default is to use the original files
> +	based backend. Set to 'lmdb' to activate the lmdb database
> +	backend.  If you use the lmdb backend,
> +	core.repositoryFormatVersion must be set to 1, and
> +	extensions.refBackend must be set to 'lmdb'.

This phrasing makes it sound like I can go into an existing repository,
change this repository setting, and *presto* I will have a LMDB-backed
repository. I suggest rewording it more along the lines of "this setting
reflects the refs backend that is currently in use".

Also please see my earlier question about whether users should see the
term "backend" or whether other terms would be easier to understand.

> +
>  core.sharedRepository::
>  	When 'group' (or 'true'), the repository is made shareable between
>  	several users in a group (making sure all the files and objects are
> diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
> index 431575b..739c116 100644
> --- a/Documentation/git-clone.txt
> +++ b/Documentation/git-clone.txt
> @@ -224,7 +224,7 @@ objects from the source repository into a pack in the cloned repository.
>  
>  --refs-backend-type=<name>::
>  	Type of refs backend. Default is to use the original files based
> -	backend.
> +	backend. Set to "lmdb" to activate the lmdb database backend.
>  
>  <repository>::
>  	The (possibly remote) repository to clone from.  See the
> diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
> index 9ea6753..bbe253f 100644
> --- a/Documentation/git-init.txt
> +++ b/Documentation/git-init.txt
> @@ -115,7 +115,8 @@ does not exist, it will be created.
>  
>  --refs-backend-type=<name>::
>  Type of refs backend. Default is to use the original "files" backend,
> -which stores ref data in files in .git/refs and .git/packed-refs.
> +which stores ref data in files in .git/refs and .git/packed-refs.  Set
> +to "lmdb" to activate the lmdb database backend.
>  
>  TEMPLATE DIRECTORY
>  ------------------
> diff --git a/Documentation/technical/refs-lmdb-backend.txt b/Documentation/technical/refs-lmdb-backend.txt
> new file mode 100644
> index 0000000..c497ffc
> --- /dev/null
> +++ b/Documentation/technical/refs-lmdb-backend.txt
> @@ -0,0 +1,50 @@
> +Notes on the LMDB refs backend
> +==============================
> +
> +Design:
> +------
> +
> +Refs and reflogs are stored in a lmdb database in .git/refdb.  All
> +keys and values are \0-terminated.
> +
> +Keys for refs are the name of the ref (e.g. refs/heads/master).
> +Values are the value of the ref, in hex
> +(e.g. 61f23eb0f81357c19fa91e2b8c6f3906c3a8f9b0).
> +
> +All per-worktree refs (refs/bisect/* and HEAD) are store using

s/store/stored/

> +the traditional files-based backend.
> +
> +Reflogs are stored as a series of database entries.
> +
> +For non-empty reflogs, there is one entry per logged ref
> +update.  The key format is logs/[refname]\0[timestamp].  The timestamp
> +is a 64-bit unsigned integer number of nanoseconds since 1/1/1970.
> +This means that reflog entries are chronologically ordered.  Because
> +LMDB is a btree database, we can efficiently iterate over these keys.

Is there a guarantee that the reflog entries for all references updated
in a single transaction have the same timestamp? Is there a guarantee
that updates that happened in *different* transactions have different
timestamps? These might be useful properties if they are easy to
implement, because then one could deduce the scope of transactions from
the reflog. But of course, they admittedly go beyond what the files
backend offers and so are not a requirement.

Is the timestamp in ASCII base 10, binary format, or or something else?

> +For an empty reflog, there is a "header" entry to show that a reflog
> +exists.  The header has the same format as an ordinary reflog, but with
> +a timeztamp of all zeros and an empty value.
> +
> +Reflog values are in the same format as the original files-based
> +reflog.

I assume that means that each entry has the same contents as one line
from a reflog file. Does the value include a trailing LF? (From above I
guess it does have a trailing NUL.)

Is the timestamp within the reflog entry guaranteed to agree with the
timestamp in the key? This might be a convenient property for debugging.

> +Weaknesses:
> +-----------
> +
> +The reflog format is somewhat inefficient: a binary format could store
> +reflog date/time information in somewhat less space.
> +
> +The rsync and file:// transports don't work yet, because they
> +don't use the refs API.
> +
> +git new-workdir is incompatible with the lmdb backend.  Fortunately,
> +git new-workdir is deprecated, and worktrees work fine.
> +
> +LMDB locks the entire database for write.  Any other writer waits
> +until the first writer is done before beginning.  Readers do not wait
> +for writers, and writers do not wait for readers.  The underlying
> +scheme is approximately MVCC; each reader's queries see the state of
> +the database as-of the time that the reader acquired its read lock.
> +This is not too far off from the files backend, which loads all refs
> +into memory when one is requested.
> diff --git a/Documentation/technical/repository-version.txt b/Documentation/technical/repository-version.txt
> index 00ad379..04c085d 100644
> --- a/Documentation/technical/repository-version.txt
> +++ b/Documentation/technical/repository-version.txt
> @@ -86,3 +86,8 @@ for testing format-1 compatibility.
>  When the config key `extensions.preciousObjects` is set to `true`,
>  objects in the repository MUST NOT be deleted (e.g., by `git-prune` or
>  `git repack -d`).
> +
> +`refBackend`
> +~~~~~~~~~~~~
> +This extension allows the user of alternate ref backends.  The only
> +defined value is `lmdb`.

s/user/use/

> diff --git a/Makefile b/Makefile
> index 5bd68e0..77b96d9 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1037,6 +1037,17 @@ ifdef USE_LIBPCRE
>  	EXTLIBS += -lpcre
>  endif
>  
> +ifdef USE_LIBLMDB
> +	BASIC_CFLAGS += -DUSE_LIBLMDB
> +	ifdef LIBLMDBDIR
> +		BASIC_CFLAGS += -I$(LIBLMDBDIR)/include
> +		EXTLIBS += -L$(LIBLMDBDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBLMDBDIR)/$(lib)
> +	endif
> +	EXTLIBS += -llmdb
> +	LIB_OBJS += refs/lmdb-backend.o
> +	TEST_PROGRAMS_NEED_X += test-refs-lmdb-backend
> +endif
> +
>  ifdef HAVE_ALLOCA_H
>  	BASIC_CFLAGS += -DHAVE_ALLOCA_H
>  endif
> @@ -2124,6 +2135,7 @@ GIT-BUILD-OPTIONS: FORCE
>  	@echo NO_CURL=\''$(subst ','\'',$(subst ','\'',$(NO_CURL)))'\' >>$@+
>  	@echo NO_EXPAT=\''$(subst ','\'',$(subst ','\'',$(NO_EXPAT)))'\' >>$@+
>  	@echo USE_LIBPCRE=\''$(subst ','\'',$(subst ','\'',$(USE_LIBPCRE)))'\' >>$@+
> +	@echo USE_LIBLMDB=\''$(subst ','\'',$(subst ','\'',$(USE_LIBLMDB)))'\' >>$@+
>  	@echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+
>  	@echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+
>  	@echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+
> diff --git a/builtin/init-db.c b/builtin/init-db.c
> index 44db591..1eb2feb 100644
> --- a/builtin/init-db.c
> +++ b/builtin/init-db.c
> @@ -179,6 +179,7 @@ static int create_default_files(const char *template_path)
>  	int reinit;
>  	int filemode;
>  	struct strbuf err = STRBUF_INIT;
> +	int repo_version = 0;
>  
>  	/* Just look for `init.templatedir` */
>  	git_config(git_init_db_config, NULL);
> @@ -209,7 +210,14 @@ static int create_default_files(const char *template_path)
>  		git_config_set("core.refsBackendType", refs_backend_type);
>  		config_data.refs_backend_type = refs_backend_type;
>  		config_data.refs_base = get_git_dir();
> +#ifdef USE_LIBLMDB
> +		register_refs_backend(&refs_be_lmdb);
> +#endif
>  		set_refs_backend(refs_backend_type, &config_data);
> +		if (!strcmp(refs_backend_type, "lmdb")) {

Wouldn't expressing this condition as

    if (strcmp(refs_backend_type, "files"))

better reflect the reason that we might need to set
"extensions.refBackend" and use repo_version "1"? (You might also need a
"refs_backend_type &&" and/or a "*refs_backend_type &&" in there.)

> +			git_config_set("extensions.refbackend", "lmdb");
> +			repo_version = 1;
> +		}
>  	}
>  
>  	if (refs_init_db(&err, shared_repository))
> @@ -229,7 +237,7 @@ static int create_default_files(const char *template_path)
>  
>  	/* This forces creation of new config file */
>  	xsnprintf(repo_version_string, sizeof(repo_version_string),
> -		  "%d", GIT_REPO_VERSION);
> +		  "%d", repo_version);
>  	git_config_set("core.repositoryformatversion", repo_version_string);
>  
>  	/* Check filemode trustability */
> diff --git a/config.c b/config.c
> index 210aa08..779bb73 100644
> --- a/config.c
> +++ b/config.c
> @@ -1222,9 +1222,23 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
>  		refdb_data.refs_base = xstrdup(dirname(repo_config_copy));
>  		free(repo_config_copy);
>  
> -		if (refdb_data.refs_backend_type &&
> -		    strcmp(refdb_data.refs_backend_type, "files")) {
> -			die("Unexpected backend %s", refdb_data.refs_backend_type);
> +		if (!refdb_data.refs_backend_type)
> +			refdb_data.refs_backend_type = "";
> +
> +		if ((!*refdb_data.refs_backend_type) ||
> +		    (!strcmp(refdb_data.refs_backend_type, "files"))) {
> +			/* default backend, nothing to do */
> +		} else if (!strcmp(refdb_data.refs_backend_type, "lmdb")) {
> +
> +#ifdef USE_LIBLMDB
> +			refs_backend_type = refdb_data.refs_backend_type;
> +			register_refs_backend(&refs_be_lmdb);
> +			set_refs_backend(refs_backend_type, &refdb_data);
> +#else
> +			die("Git was not built with USE_LIBLMDB, so the db refs backend is not available");
> +#endif
> +		} else {
> +			die("Unknown ref backend type '%s'", refdb_data.refs_backend_type);
>  		}
>  
>  		ret += git_config_from_file(fn, repo_config, data);
> diff --git a/configure.ac b/configure.ac
> index 89e2590..3853bec 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -271,6 +271,24 @@ AS_HELP_STRING([],           [ARG can be also prefix for libpcre library and hea
>          dnl it yet.
>  	GIT_CONF_SUBST([LIBPCREDIR])
>      fi)
> +
> +USE_LIBLMDB=YesPlease
> +AC_ARG_WITH(liblmdb,
> +AS_HELP_STRING([--with-liblmdb],[support lmdb (default is YES])
> +AS_HELP_STRING([],           [ARG can be also prefix for liblmdb library and headers]),
> +    if test "$withval" = "no"; then
> +	USE_LIBLMDB=
> +    elif test "$withval" = "yes"; then
> +	USE_LIBLMDB=YesPlease
> +    else
> +	USE_LIBLMDB=YesPlease
> +	LIBLMDBDIR=$withval
> +	AC_MSG_NOTICE([Setting LIBLMDBDIR to $LIBLMDBDIR])
> +        dnl USE_LIBLMDB can still be modified below, so don't substitute
> +        dnl it yet.
> +	GIT_CONF_SUBST([LIBLMDBDIR])
> +    fi)
> +
>  #
>  # Define HAVE_ALLOCA_H if you have working alloca(3) defined in that header.
>  AC_FUNC_ALLOCA
> @@ -510,6 +528,21 @@ GIT_CONF_SUBST([USE_LIBPCRE])
>  
>  fi
>  
> +if test -n "$USE_LIBLMDB"; then
> +
> +GIT_STASH_FLAGS($LIBLMDBDIR)
> +
> +AC_CHECK_LIB([lmdb], [mdb_env_open],
> +[USE_LIBLMDB=YesPlease],
> +[USE_LIBLMDB=])
> +
> +GIT_UNSTASH_FLAGS($LIBLMDBDIR)
> +
> +GIT_CONF_SUBST([USE_LIBLMDB])
> +
> +fi
> +
> +
>  #
>  # Define NO_CURL if you do not have libcurl installed.  git-http-pull and
>  # git-http-push are not built, and you cannot use http:// and https://
> diff --git a/contrib/workdir/git-new-workdir b/contrib/workdir/git-new-workdir
> index 888c34a..66b7ecf 100755
> --- a/contrib/workdir/git-new-workdir
> +++ b/contrib/workdir/git-new-workdir
> @@ -28,6 +28,9 @@ git_dir=$(cd "$orig_git" 2>/dev/null &&
>    git rev-parse --git-dir 2>/dev/null) ||
>    die "Not a git repository: \"$orig_git\""
>  
> +
> +test "$(git config core.refsbackendtype)" = "lmdb" && die "git-new-workdir is incompatible with the refs lmdb backend"
> +
>  case "$git_dir" in
>  .git)
>  	git_dir="$orig_git/.git"
> diff --git a/refs.h b/refs.h
> index c3670e8..0cbfda9 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -520,6 +520,7 @@ struct refdb_config_data {
>  int refdb_config(const char *var, const char *value, void *ptr);
>  
>  struct ref_be;
> +extern struct ref_be refs_be_lmdb;
>  int set_refs_backend(const char *name, void *data);
>  
>  void register_refs_backend(struct ref_be *be);
> diff --git a/refs/lmdb-backend.c b/refs/lmdb-backend.c
> [...]
> [...]
> [...]
> [...]
> [...]
> diff --git a/setup.c b/setup.c
> index de6b8ac..9724c0b 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -279,10 +279,11 @@ int refdb_config(const char *var, const char *value, void *ptr)
>   *
>   *  - either an objects/ directory _or_ the proper
>   *    GIT_OBJECT_DIRECTORY environment variable
> - *  - a refs/ directory
> - *  - either a HEAD symlink or a HEAD file that is formatted as
> - *    a proper "ref:", or a regular file HEAD that has a properly
> - *    formatted sha1 object name.
> + *  - a refdb/ directory or
> + *    - a refs/ directory
> + *    - either a HEAD symlink or a HEAD file that is formatted as
> + *      a proper "ref:", or a regular file HEAD that has a properly
> + *      formatted sha1 object name.

The indentation looks wrong here. I think the requirement is

*  - either a refdb/ directory or a refs/ directory
*  - either a HEAD symlink or a HEAD file that is formatted as
*    a proper "ref:", or a regular file HEAD that has a properly
*    formatted sha1 object name.

though the test of the *contents* of HEAD is currently skipped for
non-files backends.

>   */
>  int is_git_directory(const char *suspect)
>  {
> @@ -313,8 +314,13 @@ int is_git_directory(const char *suspect)
>  
>  	strbuf_setlen(&path, len);
>  	strbuf_addstr(&path, "/refs");
> -	if (access(path.buf, X_OK))
> -		goto done;
> +
> +	if (access(path.buf, X_OK)) {
> +		strbuf_setlen(&path, len);
> +		strbuf_addstr(&path, "/refdb");
> +		if (access(path.buf, X_OK))
> +			goto done;
> +	}
>  
>  	ret = 1;
>  done:
> @@ -383,6 +389,10 @@ static int check_repo_format(const char *var, const char *value, void *cb)
>  			;
>  		else if (!strcmp(ext, "preciousobjects"))
>  			repository_format_precious_objects = git_config_bool(var, value);
> +#ifdef USE_LIBLMDB
> +		else if (!strcmp(ext, "refbackend") && !strcmp(value, "lmdb"))
> +			;
> +#endif
>  		else
>  			string_list_append(&unknown_extensions, ext);
>  	}
> diff --git a/test-refs-lmdb-backend.c b/test-refs-lmdb-backend.c
> [...]

I haven't actually reviewed the LMDB-specific code, just the glue code.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-23  9:52       ` Michael Haggerty
@ 2015-12-23 20:01         ` Jeff King
  0 siblings, 0 replies; 73+ messages in thread
From: Jeff King @ 2015-12-23 20:01 UTC (permalink / raw)
  To: Michael Haggerty; +Cc: Duy Nguyen, David Turner, Git Mailing List

On Wed, Dec 23, 2015 at 10:52:41AM +0100, Michael Haggerty wrote:

> On 12/05/2015 08:44 AM, Jeff King wrote:
> > [...]
> > I think the config option needs to be extensions.refsBackendType, too,
> > per the logic in 00a09d5 (introduce "extensions" form of
> > core.repositoryformatversion, 2015-06-23). And I guess it needs to bump
> > core.repositoryformatversion to "1".
> 
> I think also, strictly speaking, the extensions.refsBackendType option
> should be ignored if core.repositoryFormatVersion is not "1". In
> practice, it probably makes more sense for the code to error out in that
> case because it is likely the result of a bug.

Yeah, I agree. It's not wrong, but it may be a good indication the user
is confused. Perhaps issuing a warning would be appropriate.

> Note that if the user explicitly chooses the "files" backend, it would
> be preferable to leave "core.repositoryFormatVersion" at "0" (assuming
> that no other extension is being used) and leave
> "extensions.refsBackendType" unset. This approach creates a repository
> that is compatible with older clients that don't know about
> refsBackendTypes.

Also agreed. This is going to be the case for basically every
"extension" option. If the extension config key isn't present, there
will be some default backwards-compatible behavior, and for
compatibility, you are always better off turning off the extension than
setting turning it "on" with that default value. This is the case for
the "preciousObjects" extension: there is no point in setting it to
"false" as opposed to removing it.

So I think the rules for setting an extension value should be:

  1. If you're setting it to the default value, then remove it
     completely.

  2. If you're removing it, and there are no other extensions, set
     repositoryFormatVersion back to 0.

Of course there's currently no code that sets extension values. And
doing step 1 is going to be specific to each extension. So this a
general philosophy to follow, not something we would write in a
function.

-Peff

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

* Re: [PATCH 14/16] refs: allow ref backend to be set for clone
  2015-12-23 13:51   ` Michael Haggerty
@ 2015-12-23 20:23     ` Eric Sunshine
  0 siblings, 0 replies; 73+ messages in thread
From: Eric Sunshine @ 2015-12-23 20:23 UTC (permalink / raw)
  To: Michael Haggerty; +Cc: David Turner, Git List, Jens Lehmann, Heiko Voigt

On Wed, Dec 23, 2015 at 8:51 AM, Michael Haggerty <mhagger@alum.mit.edu> wrote:
> On 12/03/2015 01:35 AM, David Turner wrote:
>> +     if (refs_backend_type && *refs_backend_type) {
>> +             argv_array_push(&cp.args, "--refs-backend-type");
>> +             argv_array_push(&cp.args, refs_backend_type);
>> +     }
>
> This could be shortened to
>
>     argv_array_pushf(&cp.args, "--refs-backend-type=%s", refs_backend_type);

Or, without interpolation:

    argv_array_pushl(&cp.args, "--refs-backend-type", refs_backend_type, NULL);

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2015-12-03  0:35 ` [PATCH 03/16] refs: add methods for the ref iterators David Turner
@ 2016-01-03  0:06   ` David Aguilar
  2016-01-04 19:01     ` Junio C Hamano
  2016-01-04 19:12     ` Ronnie Sahlberg
  0 siblings, 2 replies; 73+ messages in thread
From: David Aguilar @ 2016-01-03  0:06 UTC (permalink / raw)
  To: David Turner; +Cc: git, mhagger, Ronnie Sahlberg

Apologies for the late review, and this review should probably
go on patch 01 or 02 but I don't have it in my mbox atm...

On Wed, Dec 02, 2015 at 07:35:08PM -0500, David Turner wrote:
> From: Ronnie Sahlberg <sahlberg@google.com>
> 
> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
>  refs.c               | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  refs/files-backend.c | 41 +++++++++++++++++++++++++++------------
>  refs/refs-internal.h | 29 ++++++++++++++++++++++++++++
>  3 files changed, 112 insertions(+), 12 deletions(-)
> 
> diff --git a/refs.c b/refs.c
> index 9562325..b9b0244 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname,
>  {
>  	return the_refs_backend->resolve_gitlink_ref(path, refname, sha1);
>  }
> +
> +int head_ref(each_ref_fn fn, void *cb_data)
> +{
> +	return the_refs_backend->head_ref(fn, cb_data);
> +}

My only comment is that it seems like having a single static global
the_refs_backend seems like it should be avoided.

It seems like the intention was to keep the existing interface
as-is, which explains why this is using globals, but it seems
like the refs backend should be part of some "application
context" struct on the stack or allocated during main().  It can
then be passed around in the API so that we do not need to have
a global.

That way the code will not be tied to a specific single
the_refs_backend and could in theory have multiple backends
working at the same time.  If submodule were ever rewritten in C
then this could potentially be a useful feature.

That said, the refs backend is not the only global static data
in git that would need to be factored out, but it wouldn't hurt
to make this part a little more tidy.

Thoughts?
-- 
David

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2016-01-03  0:06   ` David Aguilar
@ 2016-01-04 19:01     ` Junio C Hamano
  2016-01-05 13:43       ` Michael Haggerty
  2016-01-04 19:12     ` Ronnie Sahlberg
  1 sibling, 1 reply; 73+ messages in thread
From: Junio C Hamano @ 2016-01-04 19:01 UTC (permalink / raw)
  To: David Aguilar; +Cc: David Turner, git, mhagger, Ronnie Sahlberg

David Aguilar <davvid@gmail.com> writes:

>> diff --git a/refs.c b/refs.c
>> index 9562325..b9b0244 100644
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname,
>>  {
>>  	return the_refs_backend->resolve_gitlink_ref(path, refname, sha1);
>>  }
>> +
>> +int head_ref(each_ref_fn fn, void *cb_data)
>> +{
>> +	return the_refs_backend->head_ref(fn, cb_data);
>> +}
>
> My only comment is that it seems like having a single static global
> the_refs_backend seems like it should be avoided.
>
> It seems like the intention was to keep the existing interface
> as-is, which explains why this is using globals, but it seems
> like the refs backend should be part of some "application
> context" struct on the stack or allocated during main().  It can
> then be passed around in the API so that we do not need to have
> a global.

I think the ship is sailing in a different direction.  The API to
deal with refs, possibly in multiple repositories, is that you use a
single ref backend, and that backend is expected to handle refs in
submodule repositories.  I.e. refs.c::for_each_ref_in_submodule()
calls into the backend implementation of the same method.  So from
that point of view, you cannot say "the top level repository uses
LMDB backend but this and that submodule refs are looked up by
consulting files backend".

If we want to go the opposite direction, so that we can keep more
than one in-core representations of "repository" (what you called
"application context") and optionally have different refs backend
handling different repositories, we most likely would want to
rethink the part of the refs API that special cases the submodule
refs from within the current repository.  Their implementation
should be excised from the API set, and instead made to be a set
of thin wrapper functions that explicitly refer to a repository
instance that represents the submodule.

The refs and the object database go hand in hand (i.e. the former
gives the anchoring points to keep objects in the latter alive), so
an instance of "repository" would hold "the_refs_backend", "objects[]",
and possibly "the_index" for that repository.

The caching of already read refs is a responsiblity of each ref
backend in the current codebase.  I am not sure if that is a good
placement, or a single implementation of the caching layer should
instead sit on top of any and ll ref backends.

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2016-01-03  0:06   ` David Aguilar
  2016-01-04 19:01     ` Junio C Hamano
@ 2016-01-04 19:12     ` Ronnie Sahlberg
  2016-01-04 20:26       ` Junio C Hamano
  1 sibling, 1 reply; 73+ messages in thread
From: Ronnie Sahlberg @ 2016-01-04 19:12 UTC (permalink / raw)
  To: David Aguilar; +Cc: David Turner, git@vger.kernel.org, Michael Haggerty

On Sat, Jan 2, 2016 at 4:06 PM, David Aguilar <davvid@gmail.com> wrote:
> Apologies for the late review, and this review should probably
> go on patch 01 or 02 but I don't have it in my mbox atm...
>
> On Wed, Dec 02, 2015 at 07:35:08PM -0500, David Turner wrote:
>> From: Ronnie Sahlberg <sahlberg@google.com>
>>
>> Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
>> Signed-off-by: David Turner <dturner@twopensource.com>
>> ---
>>  refs.c               | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>>  refs/files-backend.c | 41 +++++++++++++++++++++++++++------------
>>  refs/refs-internal.h | 29 ++++++++++++++++++++++++++++
>>  3 files changed, 112 insertions(+), 12 deletions(-)
>>
>> diff --git a/refs.c b/refs.c
>> index 9562325..b9b0244 100644
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname,
>>  {
>>       return the_refs_backend->resolve_gitlink_ref(path, refname, sha1);
>>  }
>> +
>> +int head_ref(each_ref_fn fn, void *cb_data)
>> +{
>> +     return the_refs_backend->head_ref(fn, cb_data);
>> +}
>
> My only comment is that it seems like having a single static global
> the_refs_backend seems like it should be avoided.
>
> It seems like the intention was to keep the existing interface
> as-is, which explains why this is using globals, but it seems
> like the refs backend should be part of some "application
> context" struct on the stack or allocated during main().  It can
> then be passed around in the API so that we do not need to have
> a global.

Not commenting on whether this is the right direction or not. A global
variable holding a methods table might not be most aesthetic design,
but there are practicalities.

However, that kind of change would change the function signatures for
all public refs functions and probably most private refs functions as
well and will likely have massive conflicts with almost any other
patch that touches the refs code.

If doing this API change is desired it is probably best to do that as
a separate patch later that ONLY does this signature change and
nothing else to make review easier and to possibly make merge conflict
changes easier to manage.



>
> That way the code will not be tied to a specific single
> the_refs_backend and could in theory have multiple backends
> working at the same time.  If submodule were ever rewritten in C
> then this could potentially be a useful feature.
>
> That said, the refs backend is not the only global static data
> in git that would need to be factored out, but it wouldn't hurt
>ice,  to make this part a little more tidy.
>
> Thoughts?
> --
> David

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2016-01-04 19:12     ` Ronnie Sahlberg
@ 2016-01-04 20:26       ` Junio C Hamano
  2016-01-05  1:17         ` Jeff King
  0 siblings, 1 reply; 73+ messages in thread
From: Junio C Hamano @ 2016-01-04 20:26 UTC (permalink / raw)
  To: Ronnie Sahlberg
  Cc: David Aguilar, David Turner, git@vger.kernel.org,
	Michael Haggerty

Ronnie Sahlberg <sahlberg@google.com> writes:

> Not commenting on whether this is the right direction or not. A global
> variable holding a methods table might not be most aesthetic design,
> but there are practicalities.
>
> However, that kind of change would change the function signatures for
> all public refs functions and probably most private refs functions as
> well and will likely have massive conflicts with almost any other
> patch that touches the refs code.

I do not think "the_backend" that is the default is wrong.  Most
callers want to interact with the single default backend that shows
refs from the single default repository (i.e. ours), and having a
set of functions that interact with that single default instance of
the ref data without having to explicitly pass it as a parameter
makes sense.

It is just like how we handle the in-core index.  Most callers want
to interact with the_index, the single default one, and we have a
set of "cache" functions written back in Linus's days to access it
without having to pass &the_index as the parameter.  Later in April
2007 when I invented the support for multiple in-core index, we kept
the traditional "works on the default instance" API in place but
made them thin wrappers to the "works on the given instance", i.e. a
set of "index" functions that was introduced at da94faf6 (Merge
branch 'jc/the-index', 2007-04-24).  Even after that change, most
callers that work on the in-core index intract with "the_index", the
single default instance, via the "cache" functions.

An API enhancement to allow us to handle refs in multiple
repositories separately would be a very welcome move (it would get
rid of the hacky interface for_each_ref_in_submodule(), for one
thing), but even after that happens, I'd expect we would have most
callers asking to interact with "the_backend", the single default
instance.

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2016-01-04 20:26       ` Junio C Hamano
@ 2016-01-05  1:17         ` Jeff King
  2016-01-05  3:29           ` Junio C Hamano
  0 siblings, 1 reply; 73+ messages in thread
From: Jeff King @ 2016-01-05  1:17 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Ronnie Sahlberg, David Aguilar, David Turner, git@vger.kernel.org,
	Michael Haggerty

On Mon, Jan 04, 2016 at 12:26:01PM -0800, Junio C Hamano wrote:

> An API enhancement to allow us to handle refs in multiple
> repositories separately would be a very welcome move (it would get
> rid of the hacky interface for_each_ref_in_submodule(), for one
> thing), but even after that happens, I'd expect we would have most
> callers asking to interact with "the_backend", the single default
> instance.

I am just a bystander here, but I think I agree. I do not know how we
will handle migrations from one format to another, but if we can
instantiate two separate ref backends at once, it seems like we should
be able to just for_each_ref() in the "old" backend, and update_ref() in
the new one.

I did not dig in the code, but I think that is similar to why we have
the multiple-index code. It is not about juggling a bunch of independent
instances, but about moving data between them (though not specifically
for migration in the case of the index).

As much as it would be nice to clean this up before moving to multiple
backends, though, I don't think we should make it a pre-requisite. This
is a difficult topic as it is, and I'd rather see us make incremental
improvement (backends, then hopefully more flexible mixing of backends)
than get bogged down and get neither.

-Peff

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2016-01-05  1:17         ` Jeff King
@ 2016-01-05  3:29           ` Junio C Hamano
  0 siblings, 0 replies; 73+ messages in thread
From: Junio C Hamano @ 2016-01-05  3:29 UTC (permalink / raw)
  To: Jeff King
  Cc: Ronnie Sahlberg, David Aguilar, David Turner, git@vger.kernel.org,
	Michael Haggerty

Jeff King <peff@peff.net> writes:

> As much as it would be nice to clean this up before moving to multiple
> backends, though, I don't think we should make it a pre-requisite. This
> is a difficult topic as it is, and I'd rather see us make incremental
> improvement (backends, then hopefully more flexible mixing of backends)
> than get bogged down and get neither.

Oh, yes, if I sounded like this needs to be cleaned up before it can
go in, that wasn't what I meant.  After all, the single in-core
index support was adequate for our use even before the multiple
in-core index support was introduced, and we still use the API to
access the single default instance in most of the codepaths.  I'd
expect that the ref API will evolve in the same way.

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2016-01-04 19:01     ` Junio C Hamano
@ 2016-01-05 13:43       ` Michael Haggerty
  2016-01-05 18:56         ` Junio C Hamano
  0 siblings, 1 reply; 73+ messages in thread
From: Michael Haggerty @ 2016-01-05 13:43 UTC (permalink / raw)
  To: Junio C Hamano, David Aguilar; +Cc: David Turner, git, Ronnie Sahlberg

On 01/04/2016 08:01 PM, Junio C Hamano wrote:
> David Aguilar <davvid@gmail.com> writes:
> [...]
>> My only comment is that it seems like having a single static global
>> the_refs_backend seems like it should be avoided.
>> [...]
> 
> I think the ship is sailing in a different direction.  The API to
> deal with refs, possibly in multiple repositories, is that you use a
> single ref backend, and that backend is expected to handle refs in
> submodule repositories.  I.e. refs.c::for_each_ref_in_submodule()
> calls into the backend implementation of the same method.  So from
> that point of view, you cannot say "the top level repository uses
> LMDB backend but this and that submodule refs are looked up by
> consulting files backend".
> 
> If we want to go the opposite direction, so that we can keep more
> than one in-core representations of "repository" (what you called
> "application context") and optionally have different refs backend
> handling different repositories, we most likely would want to
> rethink the part of the refs API that special cases the submodule
> refs from within the current repository.  Their implementation
> should be excised from the API set, and instead made to be a set
> of thin wrapper functions that explicitly refer to a repository
> instance that represents the submodule.

I definitely think that the "one and only one refs backend per process"
model is not a good long-term approach. For example, submodules are
relatively independent but are nevertheless sometimes accessed from a
single process. I don't think insisting that they all use the same
references backend is tenable over the long term.

But I haven't insisted on this flexibility yet because I don't think it
will be much extra work in total to retrofit it later. What we mostly
want to avoid is the need to rewrite all call sites more than once. The
current patch series hasn't had to rewrite callers at all, so we still
have a rewrite in reserve :-)

Actually, maybe we will never have to rewrite all callers. I rather
think that we will retain one simplified API for dealing with "*this*
repository's references" and a second for dealing with other repos' refs.

By the way, I think the next step towards the ability to work with
multiple backends at the same time would be to store a pointer to the
refs backend inside the ref_cache instance (possibly renaming the
latter) and allowing them both to be looked up by submodule name as is
already done by get_ref_cache(). I think this change could also be done
without rewriting callers.

> [...]
> The caching of already read refs is a responsiblity of each ref
> backend in the current codebase.  I am not sure if that is a good
> placement, or a single implementation of the caching layer should
> instead sit on top of any and ll ref backends.

I still dream about having compoundable reference backends, in which
case, ultimately, a "CacheReferenceStore" instance would optionally wrap
on top of an arbitrary other "ReferenceStore" instance (so to speak) to
give it caching functionality.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 11/16] refs: move duplicate check to common code
  2015-12-23  6:27   ` Michael Haggerty
@ 2016-01-05 16:42     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-05 16:42 UTC (permalink / raw)
  To: Michael Haggerty, git

On Wed, 2015-12-23 at 07:27 +0100, Michael Haggerty wrote:
> > +static int ref_update_reject_duplicates(struct ref_transaction
> > *transaction,
> > +					struct string_list
> > *refnames,
> > +					struct strbuf *err)
> 
> I like that you extract this code into a function. Though it feels
> awkward to have a function called "ref_update_reject_duplicates()"
> that
> has a side effect of filling the names into a string list. I think it
> would feel more natural to call the function get_affected_refnames(),
> and treat the duplicate check as an extra bonus.


Renamed, thanks.

> >  int ref_transaction_commit(struct ref_transaction *transaction,
> >  			   struct strbuf *err)
> >  {
> > -	return the_refs_backend->transaction_commit(transaction,
> > err);
> > +	int ret = -1;
> > +	struct string_list affected_refnames =
> > STRING_LIST_INIT_NODUP;
> > +
> > +	assert(err);
> > +
> > +	if (transaction->state != REF_TRANSACTION_OPEN)
> > +		die("BUG: commit called for transaction that is
> > not open");
> > +
> > +	if (!transaction->nr) {
> > +		transaction->state = REF_TRANSACTION_CLOSED;
> > +		return 0;
> > +	}
> > +
> > +	if (ref_update_reject_duplicates(transaction,
> > &affected_refnames, err)) {
> > +		ret = TRANSACTION_GENERIC_ERROR;
> > +		goto done;
> > +	}
> > +
> > +	ret = the_refs_backend->transaction_commit(transaction,
> > +						  
> >  &affected_refnames, err);
> > +done:
> > +	string_list_clear(&affected_refnames, 0);
> > +	return ret;
> >  }
> 
> Here you are avoiding a small amount of code duplication by calling
> ref_update_reject_duplicates() here rather than in the backend
> -specific
> code. But you are doing so at the cost of having to compute
> affected_refnames here and pass it (redundantly) to the backend's
> transaction_commit function. This increases the coupling between
> these
> functions, which in my opinion is worse than the small amount of code
> duplication. But maybe it's just me.

Hm.  I'm trying to follow the principle that the backends should do as
little as possible (and that the common code should do as much as
possible).  This reduces the cognitive load when writing new backends -
- a new backend author need not know that the list of ref updates must
be unique.

What do others think? 

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-23 13:34   ` [PATCH 13/16] init: allow alternate backends to be set for new repos Michael Haggerty
@ 2016-01-05 17:26     ` David Turner
  2016-01-05 18:03       ` Junio C Hamano
  2016-01-06 12:52     ` Duy Nguyen
  1 sibling, 1 reply; 73+ messages in thread
From: David Turner @ 2016-01-05 17:26 UTC (permalink / raw)
  To: Michael Haggerty, git

On Wed, 2015-12-23 at 14:34 +0100, Michael Haggerty wrote:
> On 12/03/2015 01:35 AM, David Turner wrote:
> > git init learns a new argument --refs-backend-type.  Presently,
> > only
> > "files" is supported, but later we will add other backends.
> > 
> > When this argument is used, the repository's core.refsBackendType
> > configuration value is set, and the refs backend's initdb function
> > is
> > used to set up the ref database.
> > 
> > Signed-off-by: David Turner <dturner@twopensource.com>
> > ---
> >  Documentation/git-init-db.txt |  2 +-
> >  Documentation/git-init.txt    |  6 +++++-
> >  builtin/init-db.c             | 10 ++++++++++
> >  cache.h                       |  2 ++
> >  config.c                      | 20 ++++++++++++++++++++
> >  environment.c                 |  1 +
> >  path.c                        | 32 ++++++++++++++++++++++++++++++-
> > -
> >  refs.c                        |  8 ++++++++
> >  refs.h                        | 12 ++++++++++++
> >  setup.c                       | 10 ++++++++++
> >  10 files changed, 99 insertions(+), 4 deletions(-)
> > 
> > diff --git a/Documentation/git-init-db.txt b/Documentation/git-init
> > -db.txt
> > index 648a6cd..72fbd71 100644
> > --- a/Documentation/git-init-db.txt
> > +++ b/Documentation/git-init-db.txt
> > @@ -9,7 +9,7 @@ git-init-db - Creates an empty Git repository
> >  SYNOPSIS
> >  --------
> >  [verse]
> > -'git init-db' [-q | --quiet] [--bare] [-
> > -template=<template_directory>] [--separate-git-dir <git dir>] [-
> > -shared[=<permissions>]]
> > +'git init-db' [-q | --quiet] [--bare] [-
> > -template=<template_directory>] [--separate-git-dir <git dir>] [-
> > -shared[=<permissions>]] [--refs-backend-type=<name>]
> >  
> >  
> >  DESCRIPTION
> > diff --git a/Documentation/git-init.txt b/Documentation/git
> > -init.txt
> > index 8174d27..9ea6753 100644
> > --- a/Documentation/git-init.txt
> > +++ b/Documentation/git-init.txt
> > @@ -12,7 +12,7 @@ SYNOPSIS
> >  'git init' [-q | --quiet] [--bare] [-
> > -template=<template_directory>]
> >  	  [--separate-git-dir <git dir>]
> >  	  [--shared[=<permissions>]] [directory]
> > -
> > +	  [--refs-backend-type=<name>]
> 
> ISTM that "backend" (used here in this option name, and in the
> manpage)
> is not such a meaningful term to users. Could we pick a less obscure
> term? E.g., maybe "--ref-storage=<name>"?

I'm working on the rest now, but wanted to comment on this first.  I
went ahead and made this change, but I'm not sure I like it.  In the
git codebase, the concept will continue to be called "backend"; there
are already-accepted patches using that terminology.  Having two
separate names for the same thing seems confusing to me.  We have this
problem already with the index ("cache" or "staging area"), which is a
little annoying when grepping the code. 

(For refs-backend-type, the name of the config variable is a third
separate thing, where we'll have to choose between the internal and the
external name).  

So I think maybe we should just go with refs-backend here.  What do
others think?

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2016-01-05 17:26     ` David Turner
@ 2016-01-05 18:03       ` Junio C Hamano
  2016-01-05 18:24         ` David Turner
  2016-01-06 12:02         ` Michael Haggerty
  0 siblings, 2 replies; 73+ messages in thread
From: Junio C Hamano @ 2016-01-05 18:03 UTC (permalink / raw)
  To: David Turner; +Cc: Michael Haggerty, git

David Turner <dturner@twopensource.com> writes:

> I'm working on the rest now, but wanted to comment on this first.  I
> went ahead and made this change, but I'm not sure I like it.  In the
> git codebase, the concept will continue to be called "backend"; there
> are already-accepted patches using that terminology.  Having two
> separate names for the same thing seems confusing to me.

We have the option to update whatever "are already-accepted" [*1*].
That would allow us to uniformly call it "ref storage", if we wanted
to.

In any case, we shouldn't be using an unqualified "backend" (or
"storage" for that matter); we should always say "ref", i.e. either
"ref backend" or "ref storage", in the name.

Between "backend" and "storage", I am slightly in favor of the
latter, but I am not good at naming things so...


[Footnote]

*1* Output from

    $ git grep backend master --

seems to show me only 

    master:refs.c: * The backend-independent part of the reference module.

and all others are other kinds of backends, e.g. "merge backend",
"http-backend", etc. so that may not be too bad.

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2016-01-05 18:03       ` Junio C Hamano
@ 2016-01-05 18:24         ` David Turner
  2016-01-06 12:02         ` Michael Haggerty
  1 sibling, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-05 18:24 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Michael Haggerty, git

On Tue, 2016-01-05 at 10:03 -0800, Junio C Hamano wrote:
> David Turner <dturner@twopensource.com> writes:
> 
> > I'm working on the rest now, but wanted to comment on this first. 
> >  I
> > went ahead and made this change, but I'm not sure I like it.  In
> > the
> > git codebase, the concept will continue to be called "backend";
> > there
> > are already-accepted patches using that terminology.  Having two
> > separate names for the same thing seems confusing to me.
> 
> We have the option to update whatever "are already-accepted" [*1*].
> That would allow us to uniformly call it "ref storage", if we wanted
> to.
> 
> In any case, we shouldn't be using an unqualified "backend" (or
> "storage" for that matter); we should always say "ref", i.e. either
> "ref backend" or "ref storage", in the name.
> 
> Between "backend" and "storage", I am slightly in favor of the
> latter, but I am not good at naming things so...
> 
> 
> [Footnote]
> 
> *1* Output from
> 
>     $ git grep backend master --
> 
> seems to show me only 
> 
>     master:refs.c: * The backend-independent part of the reference
> module.
> 
> and all others are other kinds of backends, e.g. "merge backend",
> "http-backend", etc. so that may not be too bad.

There's refs/files-backend.c in master.

I guess the argument for "backend" is that it is a better description
of the struct.  That is, "a storage" sounds funny.  Usually "storage"
is a mass noun.  I guess we could call them "storage backends" (with
"ref-storage" in the UI), which would split the difference.  I guess
I'll go with that, and we can decide later whether to rename those
files.

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

* Re: [PATCH 03/16] refs: add methods for the ref iterators
  2016-01-05 13:43       ` Michael Haggerty
@ 2016-01-05 18:56         ` Junio C Hamano
  0 siblings, 0 replies; 73+ messages in thread
From: Junio C Hamano @ 2016-01-05 18:56 UTC (permalink / raw)
  To: Michael Haggerty; +Cc: David Aguilar, David Turner, git, Ronnie Sahlberg

Michael Haggerty <mhagger@alum.mit.edu> writes:

> Actually, maybe we will never have to rewrite all callers. I rather
> think that we will retain one simplified API for dealing with "*this*
> repository's references" and a second for dealing with other repos' refs.

Yeah, I think the similarity with the multiple in-core index support
Peff brought up holds true here as well.

>> [...]
>> The caching of already read refs is a responsiblity of each ref
>> backend in the current codebase.  I am not sure if that is a good
>> placement, or a single implementation of the caching layer should
>> instead sit on top of any and ll ref backends.
>
> I still dream about having compoundable reference backends, in which
> case, ultimately, a "CacheReferenceStore" instance would optionally wrap
> on top of an arbitrary other "ReferenceStore" instance (so to speak) to
> give it caching functionality.

I am not sure if we need or want the fully stackable backends, but I
can see that the implementation of the API could be structured like
so:

 (1) Users of API would interact solely with the in-core caching
     layer when creating, reading, modifying, enumerating and
     deleting refs and contents of their logs.  The caching layer
     has a handle for each in-core representation of a "repository",
     and there is a single default one, i.e. our repository.  Also
     there is a way to ask for the in-core represention of a
     "repository" for submodules.

 (2) An instance of an in-core "repository" caching layer knows what
     "storage backend" the repository uses and this is used to
     dispatch the requests to suitable backends.  The caching layer
     would be responsible for maintaining the validity of in-core
     cache it keeps while it relays the requests.

 (3) The implementation of for_each_ref_in_submodule() family of
     functions may need to be restructured; having to have the
     backend method for them would force storage backends to be
     aware of "other" repositories.  Other "only in this repository"
     methods David and Ronnie's topics abstracts out of the files
     backend would remain the same.

which may be clean and flexible enough for our purpose.

Just to reiterate to avoid misunderstanding, I do not think that
kind of restructuring has to be a prerequisite for the current
effort to allow multiple backends, though.

Thanks.

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

* Re: [PATCH 12/16] refs: always handle non-normal refs in files backend
  2015-12-23  8:02   ` Michael Haggerty
@ 2016-01-06  0:13     ` David Turner
  2016-01-06 23:41     ` [PATCH/RFC v2 1/3] refs: allow log-only updates David Turner
  2016-01-06 23:42     ` [PATCH 12/16] " David Turner
  2 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-06  0:13 UTC (permalink / raw)
  To: Michael Haggerty, git

On Wed, 2015-12-23 at 09:02 +0100, Michael Haggerty wrote:
> + * We move other non-normal ref updates with into a specially
> > -created
> > + * files-backend transaction
> > + */
> 
> Extra word? s/with//?

Fixed, thanks.

> > +			add_update_obj(files_transaction, update);
> > +			/*
> > +			 * Replace this transaction with the
> > +			 * last transaction, removing it from
> > +			 * the list of backend transactions
> > +			 */
> > +			last = --transaction->nr;
> > +			transaction->updates[i] = transaction
> > ->updates[last];
> 
> The "last" temporary variable could be trivially inlined.

Yes, but then the line would be > 80 characters.

> > +			continue;
> > +		}
> > +
> > +		if (resolved) {
> > +			struct ref_update *new_update;
> > +			struct string_list_item *item;
> > +
> > +			if (ref_type(resolved) != REF_TYPE_NORMAL)
> > +				die("Non-normal symbolic ref `%s`
> > points to non-normal ref `%s`", update->refname, resolved);
> 
> We don't usually use backticks in error messages. Please use "'"
> instead.

Fixed.

> Also, please store this error message into a "strbuf *err" and report
> it
> via the usual mechanism.

Fixed.

> > +			new_update = xmalloc(sizeof(*new_update) +
> > +					     strlen(resolved) +
> > 1);
> > +			memcpy(new_update, update,sizeof(*update));
> z
> Wouldn't it be preferable to replace this messy replacement code
> (including the memcpy(), which can't be checked by the type system)
> with
> a call to ref_transaction_update() followed by moving the new update
> to
> this position in the list and possibly tweaking some of its fields?
>
> > +			if (update->flags & REF_HAVE_OLD &&
> > +			    hashcmp(sha1, update->old_sha1)) {
> > +				/* consistency check failed */
> > +				free(new_update);
> > +				return -1;
> 
> We need an error message to be reported in this case; i.e., via a
> "struct strbuf *err" argument.
>
> But actually, I don't understand why this check is needed here at
> all.
> Isn't it redundant with a similar check that will be done later (and
> properly, under lock) as part of the main ref_transaction_commit(

Looks like probably yes.

> > +			} else {
> > +				hashcpy(update->old_sha1, sha1);
> > +			}
> > +
> > +			strcpy((char *)new_update->refname,
> > resolved);
> > +			transaction->updates[i] = new_update;
> > +
> > +			item = string_list_append(symrefs, update
> > ->refname);
> > +			item->util = new_update;
> > +			free(update);
> > +		}
> > +	}
> > +
> > +	return 0;
> > +}
> > +
> >  int ref_transaction_commit(struct ref_transaction *transaction,
> >  			   struct strbuf *err)
> >  {
> >  	int ret = -1;
> >  	struct string_list affected_refnames =
> > STRING_LIST_INIT_NODUP;
> > +	struct string_list files_affected_refnames =
> > STRING_LIST_INIT_NODUP;
> > +	struct string_list symrefs = STRING_LIST_INIT_DUP;
> > +	struct string_list_item *item;
> > +	struct ref_transaction *files_transaction = NULL;
> >  
> >  	assert(err);
> >  
> > @@ -1146,6 +1233,26 @@ int ref_transaction_commit(struct
> > ref_transaction *transaction,
> >  		return 0;
> >  	}
> >  
> > +	if (the_refs_backend != &refs_be_files) {
> > +		files_transaction = ref_transaction_begin(err);
> > +		if (!files_transaction)
> > +			die("%s", err->buf);
> 
> I think dying here is too abrupt. Some callers try to recover from a
> failed ref_transaction_commit(). Couldn't you "goto done" and let the
> caller deal with err?

Sure.

> > +		ret = move_abnormal_ref_updates(transaction,
> > files_transaction,
> > +						&symrefs);
> > +		if (ret)
> > +			goto done;
> > +
> > +		/* files backend commit */
> > +		if
> > (ref_update_reject_duplicates(files_transaction,
> > +						 &files_affected_r
> > efnames,
> > +						 err)) {
> > +			ret = TRANSACTION_GENERIC_ERROR;
> > +			goto done;
> > +		}
> 
> Is it correct to reject_duplicates among "abnormal" references and
> "normal" references separately? ***

Yes.  No reference is both normal and abnormal.  

> > +	}
> > +
> > +	/* main backend commit */
> >  	if (ref_update_reject_duplicates(transaction,
> > &affected_refnames, err)) {
> >  		ret = TRANSACTION_GENERIC_ERROR;
> >  		goto done;
> > @@ -1153,8 +1260,35 @@ int ref_transaction_commit(struct
> > ref_transaction *transaction,
> >  
> >  	ret = the_refs_backend->transaction_commit(transaction,
> >  						  
> >  &affected_refnames, err);
> > +	if (ret)
> > +		goto done;
> > +
> > +	if (the_refs_backend != &refs_be_files) {
> 
> This conditional would perhaps be more to the point if expressed as
> "if
> (files_transaction)".

OK.

> > +		ret =
> > refs_be_files.transaction_commit(files_transaction,
> > +						      
> >  &files_affected_refnames,
> > +						       err);
> > +		if (ret) {
> > +			warning(split_transaction_fail_warning);
> > +			goto done;
> > +		}
> > +
> > +		/* reflogging for dereferenced symbolic refs */
> > +		for_each_string_list_item(item, &symrefs) {
> > +			struct ref_update *update = item->util;
> > +			if (files_log_ref_write(item->string,
> > update->old_sha1,
> > +						update->new_sha1,
> > +						update->msg,
> > update->flags, err))
> > +				warning("failed to log ref update
> > for symref %s",
> > +					item->string);
> > +		}
> 
> I think this code is incorrect because it doesn't lock the symbolic
> reference before modifying its reflog (though I seem to recall that
> the
> old code was buggy in this respect, too).

> I wonder whether it would be simpler overall to leave the ref_update
> for
> the symbolic ref in the files_transaction, but set a special internal
> internal flag like REF_LOG_ONLY which tells the usual
> transaction_commit
> code to add a reflog entry for update->old_sha1 to update->new_sha1,
> without actually changing the reference.

Hm.  This seems to work -- see below for more details.

> > +	}
> > +
> >  done:
> >  	string_list_clear(&affected_refnames, 0);
> > +	string_list_clear(&files_affected_refnames, 0);
> > +	if (files_transaction)
> > +		ref_transaction_free(files_transaction);
> > +	string_list_clear(&symrefs, 0);
> >  	return ret;
> >  }
> >  
> > @@ -1210,6 +1344,9 @@ int peel_ref(const char *refname, unsigned
> > char *sha1)
> >  int create_symref(const char *ref_target, const char
> > *refs_heads_master,
> >  		  const char *logmsg)
> >  {
> > +	if (ref_type(ref_target) != REF_TYPE_NORMAL)
> > +		return refs_be_files.create_symref(ref_target,
> > refs_heads_master,
> > +						   logmsg);
> >  	return the_refs_backend->create_symref(ref_target,
> > refs_heads_master,
> >  					       logmsg);
> >  }
> > 
> 
> I very much like the idea of introducing special handling for 
> symbolic reference updates within a transaction. In fact, I think I 
> would go even farther:
> 
> Let's take the example of an update to HEAD, which currently points
> at
> refs/heads/master. I think it would *always* be a good idea (i.e., 
> even when only the files backend is in use) to split that ref_update 
> into two ref_updates:
> 
> 1. One to update refs/heads/master and add a reflog entry for that
>    reference.
> 
> 2. One to add a reflog entry for HEAD (i.e. using the new 
> REF_LOG_ONLY flag suggested above).
> 
> Why?
> 
> * It ensures that both references are locked correctly while their
>   reflogs are updated. (I believe the current code gets this wrong.)

It's true that both refs are locked while their reflogs are updated.
However, the symbolic ref isn't locked while the update to its referent
happens, which is still bad.  I guess we could fix that later, since I
don't think this would make the situation any worse.

One thing to note is that there's a bunch of dereferencing code in
files-backend.c which we could eliminate by doing the split outside the
backend.  I've made the basic version of this change, but haven't yet
looked at whether and how we can eliminate that code.  Tomorrow (I
hope), I'll do some more work on that.

> * It improves the reject_duplicates coverage, which (I think)
> currently
>   wouldn't detect the conflict between a direct update of
>   refs/heads/master with a simultaneous update of the same reference
>   via HEAD.

As it happens, the current code handles this case indirectly; the ref
is locked by the first update, so the update through the second can't
do get the lock, causing the whole transaction to fail.  After this
patch, it is handled directly (since the main transaction now contains
the referent of HEAD).

> * It could later be generalized to an update that goes through
> multiple
>   layers of symref indirection (though this would be a very low
>   priority).
> 
> This might benefit the split-backend situation that you are
> implementing
> here. You could first do the symref-splitting step I just described,
> and
> *then* separate the non-normal from the normal refs. I think the net
> result would be simpler.
> 
> This patch is a lot to digest. I'm not yet confident that I have
> thought
> through all of the ramifications of this patch. I guess a few
> iterations
> will be needed in any case.

I'm sure that's true.

> By the way, all of the patches preceding this one that I haven't
> commented on look OK to me.

Thanks.

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2016-01-05 18:03       ` Junio C Hamano
  2016-01-05 18:24         ` David Turner
@ 2016-01-06 12:02         ` Michael Haggerty
  1 sibling, 0 replies; 73+ messages in thread
From: Michael Haggerty @ 2016-01-06 12:02 UTC (permalink / raw)
  To: Junio C Hamano, David Turner; +Cc: git

On 01/05/2016 07:03 PM, Junio C Hamano wrote:
> David Turner <dturner@twopensource.com> writes:
> 
>> I'm working on the rest now, but wanted to comment on this first.  I
>> went ahead and made this change, but I'm not sure I like it.  In the
>> git codebase, the concept will continue to be called "backend"; there
>> are already-accepted patches using that terminology.  Having two
>> separate names for the same thing seems confusing to me.
> 
> We have the option to update whatever "are already-accepted" [*1*].
> That would allow us to uniformly call it "ref storage", if we wanted
> to.

...whereas whatever we name the option, we have to live with forever
because it is user-facing. It's more important to get the option name
correct (though I agree that it would be nice for the nomenclature used
in the code to be reminiscent of the option name).

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2015-12-23 13:34   ` [PATCH 13/16] init: allow alternate backends to be set for new repos Michael Haggerty
  2016-01-05 17:26     ` David Turner
@ 2016-01-06 12:52     ` Duy Nguyen
  2016-01-07  3:31       ` Shawn Pearce
  1 sibling, 1 reply; 73+ messages in thread
From: Duy Nguyen @ 2016-01-06 12:52 UTC (permalink / raw)
  To: Michael Haggerty; +Cc: David Turner, Git Mailing List

On Wed, Dec 23, 2015 at 8:34 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:
> On 12/03/2015 01:35 AM, David Turner wrote:
>> diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
>> index 8174d27..9ea6753 100644
>> --- a/Documentation/git-init.txt
>> +++ b/Documentation/git-init.txt
>> @@ -12,7 +12,7 @@ SYNOPSIS
>>  'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
>>         [--separate-git-dir <git dir>]
>>         [--shared[=<permissions>]] [directory]
>> -
>> +       [--refs-backend-type=<name>]
>
> ISTM that "backend" (used here in this option name, and in the manpage)
> is not such a meaningful term to users. Could we pick a less obscure
> term? E.g., maybe "--ref-storage=<name>"?

>From an (ex-)translator point of view, storage is also easier to
translate than the technical term "backend". I know we do not
translate option names, but whatever term you use usually show up in
some user-facing messages that need translating. But I do prefer
backend in source code, I think it expresses the idea much better than
storage.
-- 
Duy

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

* Re: [PATCH] clone: use child_process for recursive checkouts
  2015-12-23 11:30   ` [PATCH] clone: use child_process for recursive checkouts Michael Haggerty
@ 2016-01-06 23:41     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-06 23:41 UTC (permalink / raw)
  To: Michael Haggerty; +Cc: Junio C Hamano, Duy Nguyen, Jeff King, git

On Wed, 2015-12-23 at 12:30 +0100, Michael Haggerty wrote:
> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> ---
> David, I think if you insert this patch before your
> 
>   13/16 refs: allow ref backend to be set for clone
> 
> , then the hunk in builtin/clone.c:checkout() of your patch becomes
> trivial:
> 
> 	if (refs_backend_type)
> 		argv_array_pushf(&cmd.args, "--refs-backend-type=%s",
> 				 refs_backend_type);

Inserted, thanks.

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

* [PATCH/RFC v2 1/3] refs: allow log-only updates
  2015-12-23  8:02   ` Michael Haggerty
  2016-01-06  0:13     ` David Turner
@ 2016-01-06 23:41     ` David Turner
  2016-01-06 23:41       ` [PATCH/RFC v2 2/3] refs: resolve symbolic refs first David Turner
  2016-01-06 23:41       ` [PATCH/RFC v2 3/3] refs: always handle non-normal refs in files backend David Turner
  2016-01-06 23:42     ` [PATCH 12/16] " David Turner
  2 siblings, 2 replies; 73+ messages in thread
From: David Turner @ 2016-01-06 23:41 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

The refs infrastructure learns about log-only ref updates, which only
update the reflog.  Later, we will use this to separate symbolic
reference resolution from ref updating.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs/files-backend.c | 15 ++++++++++-----
 refs/refs-internal.h |  2 ++
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/refs/files-backend.c b/refs/files-backend.c
index 176cf65..0800a57 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2821,7 +2821,7 @@ static int commit_ref_update(struct ref_lock *lock,
 			}
 		}
 	}
-	if (commit_ref(lock)) {
+	if (!(flags & REF_LOG_ONLY) && commit_ref(lock)) {
 		error("Couldn't set %s", lock->ref_name);
 		unlock_ref(lock);
 		return -1;
@@ -3175,7 +3175,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 			goto cleanup;
 		}
 		if ((update->flags & REF_HAVE_NEW) &&
-		    !(update->flags & REF_DELETING)) {
+		    !(update->flags & REF_DELETING) &&
+		    !(update->flags & REF_LOG_ONLY)) {
 			int overwriting_symref = ((update->type & REF_ISSYMREF) &&
 						  (update->flags & REF_NODEREF));
 
@@ -3205,7 +3206,9 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 				update->flags |= REF_NEEDS_COMMIT;
 			}
 		}
-		if (!(update->flags & REF_NEEDS_COMMIT)) {
+
+		if (!(update->flags & REF_LOG_ONLY) &&
+		    !(update->flags & REF_NEEDS_COMMIT)) {
 			/*
 			 * We didn't have to write anything to the lockfile.
 			 * Close it to free up the file descriptor:
@@ -3222,7 +3225,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 	for (i = 0; i < n; i++) {
 		struct ref_update *update = updates[i];
 
-		if (update->flags & REF_NEEDS_COMMIT) {
+		if (update->flags & REF_NEEDS_COMMIT ||
+		    update->flags & REF_LOG_ONLY) {
 			if (commit_ref_update(update->backend_data,
 					      update->new_sha1, update->msg,
 					      update->flags, err)) {
@@ -3242,7 +3246,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 		struct ref_update *update = updates[i];
 		struct ref_lock *lock = update->backend_data;
 
-		if (update->flags & REF_DELETING) {
+		if (update->flags & REF_DELETING &&
+		    !(update->flags & REF_LOG_ONLY)) {
 			if (delete_ref_loose(lock, update->type, err)) {
 				ret = TRANSACTION_GENERIC_ERROR;
 				goto cleanup;
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 21ac680..649ba63 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -42,6 +42,8 @@
  * value to ref_update::flags
  */
 
+#define REF_LOG_ONLY 0x80
+
 /* Include broken references in a do_for_each_ref*() iteration */
 #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
 
-- 
2.4.2.749.g730654d-twtrsrc

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

* [PATCH/RFC v2 2/3] refs: resolve symbolic refs first
  2016-01-06 23:41     ` [PATCH/RFC v2 1/3] refs: allow log-only updates David Turner
@ 2016-01-06 23:41       ` David Turner
  2016-01-06 23:41       ` [PATCH/RFC v2 3/3] refs: always handle non-normal refs in files backend David Turner
  1 sibling, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-06 23:41 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Before committing ref updates, split symbolic ref updates into two
parts: an update to the underlying ref, and a log-only update to the
symbolic ref.  This ensures that both references are locked correctly
while their reflogs are updated.

It is still possible to confuse git by concurrent updates, since the
splitting of symbolic refs does not happen under lock. So a symbolic ref
could be replaced by a plain ref in the middle of this operation, which
would lead to reflog discontinuities and missed old-ref checks.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               |  70 +++++++++++++++++++++++++++++
 refs/files-backend.c | 123 +++++++++++++++++++++++++--------------------------
 refs/refs-internal.h |   8 ++++
 3 files changed, 139 insertions(+), 62 deletions(-)

diff --git a/refs.c b/refs.c
index 38f29b3..2496b91 100644
--- a/refs.c
+++ b/refs.c
@@ -1126,6 +1126,72 @@ int refs_init_db(struct strbuf *err, int shared)
 	return the_refs_backend->init_db(err, shared);
 }
 
+/*
+ * Special case for symbolic refs when REF_NODEREF is not turned on.
+ * Dereference them here, mark them REF_LOG_ONLY, and add an update
+ * for the underlying ref.
+ */
+static int dereference_symrefs(struct ref_transaction *transaction,
+			       struct strbuf *err)
+{
+	int i;
+	int nr = transaction->nr;
+
+	for (i = 0; i < nr; i++) {
+		struct ref_update *update = transaction->updates[i];
+		const char *resolved;
+		unsigned char sha1[20];
+		int resolve_flags = 0;
+		int mustexist = (update->old_sha1 &&
+				 !is_null_sha1(update->old_sha1));
+		int deleting = (update->flags & REF_HAVE_NEW) &&
+			is_null_sha1(update->new_sha1);
+		struct ref_update *new_update;
+
+		if (mustexist)
+			resolve_flags |= RESOLVE_REF_READING;
+		if (deleting)
+			resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME |
+				RESOLVE_REF_NO_RECURSE;
+
+		if (strcmp(update->refname, "HEAD"))
+			update->flags |= REF_IS_NOT_HEAD;
+
+		resolved = resolve_ref_unsafe(update->refname, resolve_flags,
+					      sha1, &update->type);
+		if (!resolved) {
+			/*
+			 * We'll try again to resolve this during
+			 * commit and give a better error message
+			 * then, but we know it's not a symbolic ref
+			 * (or, indeed, any sort of ref).
+			 */
+			continue;
+		}
+
+		hashcpy(update->read_sha1, sha1);
+
+		if (update->flags & REF_NODEREF ||
+		    !(update->type & REF_ISSYMREF))
+			continue;
+
+		/* Create a new transaction for the underlying ref */
+		if (ref_transaction_update(transaction,
+					   resolved,
+					   update->new_sha1,
+					   (update->flags & REF_HAVE_OLD) ?
+					   update->old_sha1 : NULL,
+					   update->flags & ~REF_IS_NOT_HEAD,
+					   update->msg, err))
+			return -1;
+
+		/* Make the symbolic ref update log-only */
+		update->flags |= REF_LOG_ONLY | REF_NODEREF;
+	}
+
+	return 0;
+}
+
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
@@ -1142,6 +1208,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 		return 0;
 	}
 
+	ret = dereference_symrefs(transaction, err);
+	if (ret)
+		goto done;
+
 	if (get_affected_refnames(transaction, &affected_refnames, err)) {
 		ret = TRANSACTION_GENERIC_ERROR;
 		goto done;
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 0800a57..ce73bc4 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -7,7 +7,6 @@
 
 struct ref_lock {
 	char *ref_name;
-	char *orig_ref_name;
 	struct lock_file *lk;
 	struct object_id old_oid;
 };
@@ -1839,7 +1838,6 @@ static void unlock_ref(struct ref_lock *lock)
 	if (lock->lk)
 		rollback_lock_file(lock->lk);
 	free(lock->ref_name);
-	free(lock->orig_ref_name);
 	free(lock);
 }
 
@@ -1890,6 +1888,7 @@ static int remove_empty_directories(struct strbuf *path)
  */
 static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 					    const unsigned char *old_sha1,
+					    const unsigned char *read_sha1,
 					    const struct string_list *extras,
 					    const struct string_list *skip,
 					    unsigned int flags, int *type_p,
@@ -1897,13 +1896,13 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 {
 	struct strbuf ref_file = STRBUF_INIT;
 	struct strbuf orig_ref_file = STRBUF_INIT;
-	const char *orig_refname = refname;
 	struct ref_lock *lock;
 	int last_errno = 0;
 	int type, lflags;
 	int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
-	int resolve_flags = 0;
+	int resolve_flags = RESOLVE_REF_NO_RECURSE;
 	int attempts_remaining = 3;
+	int resolved;
 
 	assert(err);
 
@@ -1911,67 +1910,68 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 
 	if (mustexist)
 		resolve_flags |= RESOLVE_REF_READING;
-	if (flags & REF_DELETING) {
-		resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
-		if (flags & REF_NODEREF)
-			resolve_flags |= RESOLVE_REF_NO_RECURSE;
-	}
 
-	refname = resolve_ref_unsafe(refname, resolve_flags,
-				     lock->old_oid.hash, &type);
-	if (!refname && errno == EISDIR) {
-		/*
-		 * we are trying to lock foo but we used to
-		 * have foo/bar which now does not exist;
-		 * it is normal for the empty directory 'foo'
-		 * to remain.
-		 */
-		strbuf_git_path(&orig_ref_file, "%s", orig_refname);
-		if (remove_empty_directories(&orig_ref_file)) {
+	if (type_p && *type_p & REF_ISSYMREF) {
+		hashcpy(lock->old_oid.hash, read_sha1);
+	} else {
+
+		if (flags & REF_DELETING)
+			resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
+
+		resolved = !!resolve_ref_unsafe(refname, resolve_flags,
+						lock->old_oid.hash, &type);
+		if (!resolved && errno == EISDIR) {
+			/*
+			 * we are trying to lock foo but we used to
+			 * have foo/bar which now does not exist;
+			 * it is normal for the empty directory 'foo'
+			 * to remain.
+			 */
+			strbuf_git_path(&orig_ref_file, "%s", refname);
+			if (remove_empty_directories(&orig_ref_file)) {
+				last_errno = errno;
+				if (!verify_refname_available_dir(refname, extras, skip,
+								  get_loose_refs(&ref_cache), err))
+					strbuf_addf(err, "there are still refs under '%s'",
+						    refname);
+				goto error_return;
+			}
+			resolved = !!resolve_ref_unsafe(refname, resolve_flags,
+							lock->old_oid.hash, &type);
+		}
+
+		if (type_p)
+			*type_p = type;
+		if (!resolved) {
 			last_errno = errno;
-			if (!verify_refname_available_dir(orig_refname, extras, skip,
+			if (last_errno != ENOTDIR ||
+			    !verify_refname_available_dir(refname, extras, skip,
 							  get_loose_refs(&ref_cache), err))
-				strbuf_addf(err, "there are still refs under '%s'",
-					    orig_refname);
+				strbuf_addf(err,
+					    "unable to resolve reference %s: %s",
+					    refname, strerror(last_errno));
+
+			goto error_return;
+		}
+		/*
+		 * If the ref did not exist and we are creating it, make sure
+		 * there is no existing packed ref whose name begins with our
+		 * refname, nor a packed ref whose name is a proper prefix of
+		 * our refname.
+		 */
+		if (is_null_oid(&lock->old_oid) &&
+		    verify_refname_available_dir(refname, extras, skip,
+						 get_packed_refs(&ref_cache), err)) {
+			last_errno = ENOTDIR;
 			goto error_return;
 		}
-		refname = resolve_ref_unsafe(orig_refname, resolve_flags,
-					     lock->old_oid.hash, &type);
-	}
-	if (type_p)
-	    *type_p = type;
-	if (!refname) {
-		last_errno = errno;
-		if (last_errno != ENOTDIR ||
-		    !verify_refname_available_dir(orig_refname, extras, skip,
-						  get_loose_refs(&ref_cache), err))
-			strbuf_addf(err, "unable to resolve reference %s: %s",
-				    orig_refname, strerror(last_errno));
-
-		goto error_return;
-	}
-	/*
-	 * If the ref did not exist and we are creating it, make sure
-	 * there is no existing packed ref whose name begins with our
-	 * refname, nor a packed ref whose name is a proper prefix of
-	 * our refname.
-	 */
-	if (is_null_oid(&lock->old_oid) &&
-	    verify_refname_available_dir(refname, extras, skip,
-					 get_packed_refs(&ref_cache), err)) {
-		last_errno = ENOTDIR;
-		goto error_return;
 	}
-
 	lock->lk = xcalloc(1, sizeof(struct lock_file));
 
 	lflags = 0;
-	if (flags & REF_NODEREF) {
-		refname = orig_refname;
+	if (flags & REF_NODEREF)
 		lflags |= LOCK_NO_DEREF;
-	}
 	lock->ref_name = xstrdup(refname);
-	lock->orig_ref_name = xstrdup(orig_refname);
 	strbuf_git_path(&ref_file, "%s", refname);
 
  retry:
@@ -2537,7 +2537,7 @@ static int files_rename_ref(const char *oldrefname, const char *newrefname,
 
 	logmoved = log;
 
-	lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL, &err);
+	lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, NULL, 0, NULL, &err);
 	if (!lock) {
 		error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
 		strbuf_release(&err);
@@ -2555,7 +2555,7 @@ static int files_rename_ref(const char *oldrefname, const char *newrefname,
 	return 0;
 
  rollback:
-	lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL, &err);
+	lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, NULL, 0, NULL, &err);
 	if (!lock) {
 		error("unable to lock %s for rollback: %s", oldrefname, err.buf);
 		strbuf_release(&err);
@@ -2783,9 +2783,7 @@ static int commit_ref_update(struct ref_lock *lock,
 			     int flags, struct strbuf *err)
 {
 	clear_loose_ref_cache(&ref_cache);
-	if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0 ||
-	    (strcmp(lock->ref_name, lock->orig_ref_name) &&
-	     log_ref_write(lock->orig_ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0)) {
+	if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0) {
 		char *old_msg = strbuf_detach(err, NULL);
 		strbuf_addf(err, "Cannot update the ref '%s': %s",
 			    lock->ref_name, old_msg);
@@ -2793,7 +2791,7 @@ static int commit_ref_update(struct ref_lock *lock,
 		unlock_ref(lock);
 		return -1;
 	}
-	if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
+	if (flags & REF_IS_NOT_HEAD) {
 		/*
 		 * Special hack: If a branch is updated directly and HEAD
 		 * points to it (may happen on the remote side of a push
@@ -3157,6 +3155,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,
 				update->refname,
 				((update->flags & REF_HAVE_OLD) ?
 				 update->old_sha1 : NULL),
+				update->read_sha1,
 				affected_refnames, NULL,
 				update->flags,
 				&update->type,
@@ -3408,7 +3407,7 @@ static int files_reflog_expire(const char *refname, const unsigned char *sha1,
 	struct ref_lock *lock;
 	char *log_file;
 	int status = 0;
-	int type;
+	int type = 0;
 	struct strbuf err = STRBUF_INIT;
 
 	memset(&cb, 0, sizeof(cb));
@@ -3421,7 +3420,7 @@ static int files_reflog_expire(const char *refname, const unsigned char *sha1,
 	 * reference itself, plus we might need to update the
 	 * reference if --updateref was specified:
 	 */
-	lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type, &err);
+	lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, NULL, 0, &type, &err);
 	if (!lock) {
 		error("cannot lock ref '%s': %s", refname, err.buf);
 		strbuf_release(&err);
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 649ba63..971f450 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -44,6 +44,8 @@
 
 #define REF_LOG_ONLY 0x80
 
+#define REF_IS_NOT_HEAD 0x100
+
 /* Include broken references in a do_for_each_ref*() iteration */
 #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
 
@@ -159,6 +161,12 @@ struct ref_update {
 	 */
 	unsigned char old_sha1[20];
 	/*
+	 * During the symbolic ref split stage, we resolve refs.
+	 * We'll re-resolve non-symbolic refs once they are locked,
+	 * but we store this to avoid re-resolving symbolic refs.
+	 */
+	unsigned char read_sha1[20];
+	/*
 	 * One or more of REF_HAVE_NEW, REF_HAVE_OLD, REF_NODEREF,
 	 * REF_DELETING, and REF_ISPRUNING:
 	 */
-- 
2.4.2.749.g730654d-twtrsrc

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

* [PATCH/RFC v2 3/3] refs: always handle non-normal refs in files backend
  2016-01-06 23:41     ` [PATCH/RFC v2 1/3] refs: allow log-only updates David Turner
  2016-01-06 23:41       ` [PATCH/RFC v2 2/3] refs: resolve symbolic refs first David Turner
@ 2016-01-06 23:41       ` David Turner
  2016-01-08 12:52         ` David Turner
  1 sibling, 1 reply; 73+ messages in thread
From: David Turner @ 2016-01-06 23:41 UTC (permalink / raw)
  To: git, mhagger; +Cc: David Turner

Always handle non-normal (per-worktree or pseudo) refs in the files
backend instead of alternate backends.

Sometimes a ref transaction will update both a per-worktree ref and a
normal ref.  For instance, an ordinary commit might update
refs/heads/master and HEAD (or at least HEAD's reflog).

Updates to normal refs continue to go through the chosen backend.

Updates to non-normal refs are moved to a separate files backend
transaction.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 refs.c               | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 refs/refs-internal.h |  2 ++
 2 files changed, 82 insertions(+), 2 deletions(-)

diff --git a/refs.c b/refs.c
index 2496b91..67b0280 100644
--- a/refs.c
+++ b/refs.c
@@ -9,6 +9,11 @@
 #include "object.h"
 #include "tag.h"
 
+static const char split_transaction_fail_warning[] =
+	"A ref transaction was split across two refs backends.  Part of the "
+	"transaction succeeded, but then the update to the per-worktree refs "
+	"failed.  Your repository may be in an inconsistent state.";
+
 /*
  * We always have a files backend and it is the default.
  */
@@ -781,6 +786,13 @@ void ref_transaction_free(struct ref_transaction *transaction)
 	free(transaction);
 }
 
+static void add_update_obj(struct ref_transaction *transaction,
+			   struct ref_update *update)
+{
+	ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
+	transaction->updates[transaction->nr++] = update;
+}
+
 static struct ref_update *add_update(struct ref_transaction *transaction,
 				     const char *refname)
 {
@@ -788,8 +800,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,
 	struct ref_update *update = xcalloc(1, sizeof(*update) + len);
 
 	memcpy((char *)update->refname, refname, len); /* includes NUL */
-	ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
-	transaction->updates[transaction->nr++] = update;
+	add_update_obj(transaction, update);
 	return update;
 }
 
@@ -1192,11 +1203,39 @@ static int dereference_symrefs(struct ref_transaction *transaction,
 	return 0;
 }
 
+/*
+ * Move all non-normal ref updates into a specially-created
+ * files-backend transaction
+ */
+static int move_abnormal_ref_updates(struct ref_transaction *transaction,
+				     struct ref_transaction *files_transaction,
+				     struct strbuf *err)
+{
+	int i;
+
+	for (i = 0; i < transaction->nr; i++) {
+		int last;
+		struct ref_update *update = transaction->updates[i];
+
+		if (ref_type(update->refname) == REF_TYPE_NORMAL)
+			continue;
+
+		last = --transaction->nr;
+		transaction->updates[i] = transaction->updates[last];
+		files_transaction->updates[files_transaction->nr++] = update;
+	}
+
+	return 0;
+}
+
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
 	int ret = -1;
 	struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
+	struct string_list files_affected_refnames = STRING_LIST_INIT_NODUP;
+	struct string_list_item *item;
+	struct ref_transaction *files_transaction = NULL;
 
 	assert(err);
 
@@ -1212,6 +1251,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	if (ret)
 		goto done;
 
+	if (the_refs_backend != &refs_be_files) {
+		files_transaction = ref_transaction_begin(err);
+		if (!files_transaction)
+			goto done;
+
+		ret = move_abnormal_ref_updates(transaction, files_transaction,
+						err);
+		if (ret)
+			goto done;
+
+		/* files backend commit */
+		if (get_affected_refnames(files_transaction,
+						 &files_affected_refnames,
+						 err)) {
+			ret = TRANSACTION_GENERIC_ERROR;
+			goto done;
+		}
+	}
+
+	/* main backend commit */
 	if (get_affected_refnames(transaction, &affected_refnames, err)) {
 		ret = TRANSACTION_GENERIC_ERROR;
 		goto done;
@@ -1219,8 +1278,24 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 	ret = the_refs_backend->transaction_commit(transaction,
 						   &affected_refnames, err);
+	if (ret)
+		goto done;
+
+	if (files_transaction) {
+		ret = refs_be_files.transaction_commit(files_transaction,
+						       &files_affected_refnames,
+						       err);
+		if (ret) {
+			warning(split_transaction_fail_warning);
+			goto done;
+		}
+	}
+
 done:
 	string_list_clear(&affected_refnames, 0);
+	string_list_clear(&files_affected_refnames, 0);
+	if (files_transaction)
+		ref_transaction_free(files_transaction);
 	return ret;
 }
 
@@ -1276,6 +1351,9 @@ int peel_ref(const char *refname, unsigned char *sha1)
 int create_symref(const char *ref_target, const char *refs_heads_master,
 		  const char *logmsg)
 {
+	if (ref_type(ref_target) != REF_TYPE_NORMAL)
+		return refs_be_files.create_symref(ref_target, refs_heads_master,
+						   logmsg);
 	return the_refs_backend->create_symref(ref_target, refs_heads_master,
 					       logmsg);
 }
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 971f450..dd035f5 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -323,4 +323,6 @@ struct ref_storage_be {
 	for_each_replace_ref_fn *for_each_replace_ref;
 };
 
+extern struct ref_storage_be refs_be_files;
+
 #endif /* REFS_REFS_INTERNAL_H */
-- 
2.4.2.749.g730654d-twtrsrc

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

* Re: [PATCH 12/16] refs: always handle non-normal refs in files backend
  2015-12-23  8:02   ` Michael Haggerty
  2016-01-06  0:13     ` David Turner
  2016-01-06 23:41     ` [PATCH/RFC v2 1/3] refs: allow log-only updates David Turner
@ 2016-01-06 23:42     ` David Turner
  2 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-06 23:42 UTC (permalink / raw)
  To: Michael Haggerty, git

On Wed, 2015-12-23 at 09:02 +0100, Michael Haggerty wrote:
> I very much like the idea of introducing special handling for
> symbolic
> reference updates within a transaction. In fact, I think I would go
> even
> farther:
> 
> Let's take the example of an update to HEAD, which currently points
> at
> refs/heads/master. I think it would *always* be a good idea (i.e.,
> even
> when only the files backend is in use) to split that ref_update into
> two
> ref_updates:
> 
> 1. One to update refs/heads/master and add a reflog entry for that
>    reference.
> 
> 2. One to add a reflog entry for HEAD (i.e. using the new
> REF_LOG_ONLY
>    flag suggested above).

Having now implemented this, I think it's ugly.

While committing a ref_update, we read the ref's old value.  We need
this value for two things: 1. confirming that it matches old_sha1 in
the ref_update and 2. writing to the reflog.  In the case of a symbolic
ref, we need to resolve it in order to get its old value.  But we've
already resolved it during the splitting step (in order to know that it
was a symbolic ref).  So we're duplicating effort.  We can store the
resolved SHA from the split phase (along with the resolved ref type),
but that's just leaking more weird details.  

In addition, this doesn't actually solve the problem that this patch is
intended to solve; I still have to split refs into normal and non
-normal (files-backend).  It's true that this splitting is simplified,
since we don't need to do weird things for the reflogs, but I'm not
sure it's a net win.

I'm going to send just these patches in reply to your mail (I hope),
and see what you think before continuing down this path.  

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

* Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
  2016-01-06 12:52     ` Duy Nguyen
@ 2016-01-07  3:31       ` Shawn Pearce
  0 siblings, 0 replies; 73+ messages in thread
From: Shawn Pearce @ 2016-01-07  3:31 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Michael Haggerty, David Turner, Git Mailing List

On Wed, Jan 6, 2016 at 4:52 AM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Wed, Dec 23, 2015 at 8:34 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:
>> On 12/03/2015 01:35 AM, David Turner wrote:
>>> diff --git a/Documentation/git-init.txt b/Documentation/git-init.txt
>>> index 8174d27..9ea6753 100644
>>> --- a/Documentation/git-init.txt
>>> +++ b/Documentation/git-init.txt
>>> @@ -12,7 +12,7 @@ SYNOPSIS
>>>  'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
>>>         [--separate-git-dir <git dir>]
>>>         [--shared[=<permissions>]] [directory]
>>> -
>>> +       [--refs-backend-type=<name>]
>>
>> ISTM that "backend" (used here in this option name, and in the manpage)
>> is not such a meaningful term to users. Could we pick a less obscure
>> term? E.g., maybe "--ref-storage=<name>"?
>
> From an (ex-)translator point of view, storage is also easier to
> translate than the technical term "backend". I know we do not
> translate option names, but whatever term you use usually show up in
> some user-facing messages that need translating. But I do prefer
> backend in source code, I think it expresses the idea much better than
> storage.

And JGit calls these things RefDatabases. Because Java.

Also, its a table of ref -> SHA-1.
And you put tables into databases.
Or something.

</two-cents>

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

* Re: [PATCH/RFC v2 3/3] refs: always handle non-normal refs in files backend
  2016-01-06 23:41       ` [PATCH/RFC v2 3/3] refs: always handle non-normal refs in files backend David Turner
@ 2016-01-08 12:52         ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-08 12:52 UTC (permalink / raw)
  To: git, mhagger

On Wed, 2016-01-06 at 18:41 -0500, David Turner wrote:
+		files_transaction->updates[files_transaction->nr++]
> = update;

oops, need to allocate this (fixed on my branch).

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

* Re: [PATCH 15/16] refs: add LMDB refs backend
  2015-12-23 14:32   ` Michael Haggerty
@ 2016-01-08 16:05     ` David Turner
  0 siblings, 0 replies; 73+ messages in thread
From: David Turner @ 2016-01-08 16:05 UTC (permalink / raw)
  To: Michael Haggerty, git mailing list

On Wed, 2015-12-23 at 15:32 +0100, Michael Haggerty wrote:
> +core.refsBackendType::
> > +	Type of refs backend. Default is to use the original files
> > +	based backend. Set to 'lmdb' to activate the lmdb database
> > +	backend.  If you use the lmdb backend,
> > +	core.repositoryFormatVersion must be set to 1, and
> > +	extensions.refBackend must be set to 'lmdb'.
> 
> This phrasing makes it sound like I can go into an existing
> repository,
> change this repository setting, and *presto* I will have a LMDB
> -backed
> repository. I suggest rewording it more along the lines of "this
> setting
> reflects the refs backend that is currently in use".
> 
> Also please see my earlier question about whether users should see
> the
> term "backend" or whether other terms would be easier to understand.

Adjusted, thanks.

> > +All per-worktree refs (refs/bisect/* and HEAD) are store using
> 
> s/store/stored/

Fixed.

> > +the traditional files-based backend.
> > +
> > +Reflogs are stored as a series of database entries.
> > +
> > +For non-empty reflogs, there is one entry per logged ref
> > +update.  The key format is logs/[refname]\0[timestamp].  The
> > timestamp
> > +is a 64-bit unsigned integer number of nanoseconds since 1/1/1970.
> > +This means that reflog entries are chronologically ordered. 
> >  Because
> > +LMDB is a btree database, we can efficiently iterate over these
> > keys.
> 
> Is there a guarantee that the reflog entries for all references
> updated
> in a single transaction have the same timestamp? Is there a guarantee
> that updates that happened in *different* transactions have different
> timestamps? These might be useful properties if they are easy to
> implement, because then one could deduce the scope of transactions
> from
> the reflog. But of course, they admittedly go beyond what the files
> backend offers and so are not a requirement.

All of this stuff is handled outside of the storage backend, so I don't
want to address it here.  That is, this offers the same guarantees as
the files-based backend (neither of those, I think).

> Is the timestamp in ASCII base 10, binary format, or or something
> else?

I've added the words "network byte order" to make it clear that this is
binary.

> > +For an empty reflog, there is a "header" entry to show that a
> > reflog
> > +exists.  The header has the same format as an ordinary reflog, but
> > with
> > +a timeztamp of all zeros and an empty value.
> > +
> > +Reflog values are in the same format as the original files-based
> > +reflog.
> 
> I assume that means that each entry has the same contents as one line
> from a reflog file. Does the value include a trailing LF? (From above
> I
> guess it does have a trailing NUL.)

Trailing LF, yes; will fix.

> Is the timestamp within the reflog entry guaranteed to agree with the
> timestamp in the key? This might be a convenient property for
> debugging.

Yes; edited.
diff --git a/Makefile b/Makefile

> > index 5bd68e0..77b96d9 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -1037,6 +1037,17 @@ ifdef USE_LIBPCRE
> >  	EXTLIBS += -lpcre
> >  endif
> >  
> > +ifdef USE_LIBLMDB
> > +	BASIC_CFLAGS += -DUSE_LIBLMDB
> > +	ifdef LIBLMDBDIR
> > +		BASIC_CFLAGS += -I$(LIBLMDBDIR)/include
> > +		EXTLIBS += -L$(LIBLMDBDIR)/$(lib)
> > $(CC_LD_DYNPATH)$(LIBLMDBDIR)/$(lib)
> > +	endif
> > +	EXTLIBS += -llmdb
> > +	LIB_OBJS += refs/lmdb-backend.o
> > +	TEST_PROGRAMS_NEED_X += test-refs-lmdb-backend
> > +endif
> > +
> >  ifdef HAVE_ALLOCA_H
> >  	BASIC_CFLAGS += -DHAVE_ALLOCA_H
> >  endif
> > @@ -2124,6 +2135,7 @@ GIT-BUILD-OPTIONS: FORCE
> >  	@echo NO_CURL=\''$(subst ','\'',$(subst
> > ','\'',$(NO_CURL)))'\' >>$@+
> >  	@echo NO_EXPAT=\''$(subst ','\'',$(subst
> > ','\'',$(NO_EXPAT)))'\' >>$@+
> >  	@echo USE_LIBPCRE=\''$(subst ','\'',$(subst
> > ','\'',$(USE_LIBPCRE)))'\' >>$@+
> > +	@echo USE_LIBLMDB=\''$(subst ','\'',$(subst
> > ','\'',$(USE_LIBLMDB)))'\' >>$@+
> >  	@echo NO_PERL=\''$(subst ','\'',$(subst
> > ','\'',$(NO_PERL)))'\' >>$@+
> >  	@echo NO_PYTHON=\''$(subst ','\'',$(subst
> > ','\'',$(NO_PYTHON)))'\' >>$@+
> >  	@echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst
> > ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+
> > diff --git a/builtin/init-db.c b/builtin/init-db.c
> > index 44db591..1eb2feb 100644
> > --- a/builtin/init-db.c
> > +++ b/builtin/init-db.c
> > @@ -179,6 +179,7 @@ static int create_default_files(const char
> > *template_path)
> >  	int reinit;
> >  	int filemode;
> >  	struct strbuf err = STRBUF_INIT;
> > +	int repo_version = 0;
> >  
> >  	/* Just look for `init.templatedir` */
> >  	git_config(git_init_db_config, NULL);
> > @@ -209,7 +210,14 @@ static int create_default_files(const char
> > *template_path)
> >  		git_config_set("core.refsBackendType",
> > refs_backend_type);
> >  		config_data.refs_backend_type = refs_backend_type;
> >  		config_data.refs_base = get_git_dir();
> > +#ifdef USE_LIBLMDB
> > +		register_refs_backend(&refs_be_lmdb);
> > +#endif
> >  		set_refs_backend(refs_backend_type, &config_data);
> > +		if (!strcmp(refs_backend_type, "lmdb")) {
> 
> Wouldn't expressing this condition as
> 
>     if (strcmp(refs_backend_type, "files"))
> 
> better reflect the reason that we might need to set
> "extensions.refBackend" and use repo_version "1"?

Yeah.

>  (You might also need a
> "refs_backend_type &&" and/or a "*refs_backend_type &&" in there.)

I've just decided to set this var to be "files" by default, to simplify
a lot of this code.

> >   *  - either an objects/ directory _or_ the proper
> >   *    GIT_OBJECT_DIRECTORY environment variable
> > - *  - a refs/ directory
> > - *  - either a HEAD symlink or a HEAD file that is formatted as
> > - *    a proper "ref:", or a regular file HEAD that has a properly
> > - *    formatted sha1 object name.
> > + *  - a refdb/ directory or
> > + *    - a refs/ directory
> > + *    - either a HEAD symlink or a HEAD file that is formatted as
> > + *      a proper "ref:", or a regular file HEAD that has a
> > properly
> > + *      formatted sha1 object name.
> 
> The indentation looks wrong here. I think the requirement is
> *  - either a refdb/ directory or a refs/ directory
> *  - either a HEAD symlink or a HEAD file that is formatted as
> *    a proper "ref:", or a regular file HEAD that has a properly
> *    formatted sha1 object name.
> 
> though the test of the *contents* of HEAD is currently skipped for
> non-files backends.

Yes.

Thanks for the review. Hopefully another version by EOD, but we'll see.

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

end of thread, other threads:[~2016-01-08 16:06 UTC | newest]

Thread overview: 73+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-03  0:35 [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner
2015-12-03  0:35 ` [PATCH 01/16] refs: add a backend method structure with transaction functions David Turner
2015-12-05  0:07   ` Junio C Hamano
2015-12-03  0:35 ` [PATCH 02/16] refs: add methods for misc ref operations David Turner
2015-12-11 23:33   ` Junio C Hamano
2015-12-11 23:49     ` David Turner
2015-12-11 23:39   ` Junio C Hamano
2015-12-11 23:49     ` David Turner
2015-12-12  0:23       ` Junio C Hamano
2015-12-12  0:48         ` David Turner
2015-12-18  4:06     ` Howard Chu
2015-12-03  0:35 ` [PATCH 03/16] refs: add methods for the ref iterators David Turner
2016-01-03  0:06   ` David Aguilar
2016-01-04 19:01     ` Junio C Hamano
2016-01-05 13:43       ` Michael Haggerty
2016-01-05 18:56         ` Junio C Hamano
2016-01-04 19:12     ` Ronnie Sahlberg
2016-01-04 20:26       ` Junio C Hamano
2016-01-05  1:17         ` Jeff King
2016-01-05  3:29           ` Junio C Hamano
2015-12-03  0:35 ` [PATCH 04/16] refs: add do_for_each_per_worktree_ref David Turner
2015-12-11 23:52   ` Junio C Hamano
2015-12-12  0:01     ` David Turner
2015-12-03  0:35 ` [PATCH 05/16] refs: add methods for reflog David Turner
2015-12-03  0:35 ` [PATCH 06/16] refs: add method for initial ref transaction commit David Turner
2015-12-03  0:35 ` [PATCH 07/16] refs: add method for delete_refs David Turner
2015-12-03  0:35 ` [PATCH 08/16] refs: add methods to init refs backend and db David Turner
2015-12-23  5:33   ` Michael Haggerty
2015-12-23  6:54     ` David Turner
2015-12-03  0:35 ` [PATCH 09/16] refs: add method to rename refs David Turner
2015-12-03  0:35 ` [PATCH 10/16] refs: make lock generic David Turner
2015-12-03  0:35 ` [PATCH 11/16] refs: move duplicate check to common code David Turner
2015-12-23  6:27   ` Michael Haggerty
2016-01-05 16:42     ` David Turner
2015-12-03  0:35 ` [PATCH 12/16] refs: always handle non-normal refs in files backend David Turner
2015-12-23  8:02   ` Michael Haggerty
2016-01-06  0:13     ` David Turner
2016-01-06 23:41     ` [PATCH/RFC v2 1/3] refs: allow log-only updates David Turner
2016-01-06 23:41       ` [PATCH/RFC v2 2/3] refs: resolve symbolic refs first David Turner
2016-01-06 23:41       ` [PATCH/RFC v2 3/3] refs: always handle non-normal refs in files backend David Turner
2016-01-08 12:52         ` David Turner
2016-01-06 23:42     ` [PATCH 12/16] " David Turner
2015-12-03  0:35 ` [PATCH 13/16] init: allow alternate backends to be set for new repos David Turner
2015-12-05  0:07   ` Junio C Hamano
2015-12-05  6:30   ` Duy Nguyen
2015-12-05  7:44     ` Jeff King
2015-12-08  0:38       ` David Turner
2015-12-23  9:52       ` Michael Haggerty
2015-12-23 20:01         ` Jeff King
2015-12-10 18:02   ` Jeff King
2015-12-10 19:36     ` David Turner
2015-12-23 11:30   ` [PATCH] clone: use child_process for recursive checkouts Michael Haggerty
2016-01-06 23:41     ` David Turner
2015-12-23 13:34   ` [PATCH 13/16] init: allow alternate backends to be set for new repos Michael Haggerty
2016-01-05 17:26     ` David Turner
2016-01-05 18:03       ` Junio C Hamano
2016-01-05 18:24         ` David Turner
2016-01-06 12:02         ` Michael Haggerty
2016-01-06 12:52     ` Duy Nguyen
2016-01-07  3:31       ` Shawn Pearce
2015-12-03  0:35 ` [PATCH 14/16] refs: allow ref backend to be set for clone David Turner
2015-12-23 13:51   ` Michael Haggerty
2015-12-23 20:23     ` Eric Sunshine
2015-12-03  0:35 ` [PATCH 15/16] refs: add LMDB refs backend David Turner
2015-12-05  0:08   ` Junio C Hamano
2015-12-05  0:25     ` David Turner
2015-12-17  1:00   ` Jonathan Nieder
2015-12-17  2:31     ` David Turner
2015-12-17 20:49       ` Jonathan Nieder
2015-12-23 14:32   ` Michael Haggerty
2016-01-08 16:05     ` David Turner
2015-12-03  0:35 ` [PATCH 16/16] refs: tests for lmdb backend David Turner
2015-12-22 23:56 ` [PATCH 00/16] LMDB refs backend atop pre-vtable David Turner

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).