{"thread":{"id":"42524","subject":"[PATCH 01/38] resolve_gitlink_ref(): eliminate temporary variable","startedAt":"2016-06-03T21:03:35Z","lastAt":"2016-07-13T15:30:33Z","messageCount":67,"participants":["Michael Haggerty","Junio C Hamano","René Scharfe","Eric Sunshine","Duy Nguyen"],"isPatch":true,"patchVersion":1,"patchTotal":38},"messages":[{"id":"288330","messageId":"cover.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":null,"subject":"[PATCH 00/38] Virtualization of the refs API","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:35Z","receivedAt":"2016-06-03T21:03:35Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Since the that ref-iterator [1] changes seem to have gotten a positive\nreception, let's try to keep up the momentum. I hope I'm not\noverloading the review pipeline...\n\nI think all of the groundwork is in place now to virtualize the refs\nAPI. This will open the way to storing refs in ways other than the\nfamiliar loose refs / packed refs format, such as David Turner's\nproposed LMDB-based storage [2].\n\nThis is a long patch series, but most of the patches are pretty simple\nand formulaic. The goal is to implement a `ref_store`. In the language\nof object-oriented programming, `ref_store` is an abstract base class\nrepresenting a reference storage backend. It provides methods to read,\nwrite, and delete references and symrefs, and to iterate over\nreferences, reflogs, and reflog entries, plus a number of other\nthings—19 methods in all.\n\nThe one concrete implementation of this class is files_ref_store,\nwhich implements the traditional loose/packed refs scheme with\ncaching. After this patch series, about the only things left in\n`refs/files-backend.c` with external linkage are vtables.\n\nOn these 19 methods are built the refs API as used by the rest of Git.\nThe OO interface is not exposed; instead, the OO implementation is\nwrapped in traditional C functions. In fact, this patch series doesn't\nchange the existing public interface at all, and adds only two new\nfunctions!\n\nSomebody who wants to implement a new way to store references needs to\nimplement a new class derived from `ref_store`, including its 19\nmethods, plus one or two supporting ref_iterator classes, and wire it\nup to be instantiated when needed.\n\nThis patch series opens up a lot of possibilities for improving the\ninternal design of the files backend; I've described some of my plans\nin recent emails [3,4]. But that can be done separately; for now, the\nmain point of this series is to clear the way for LMDB-based reference\nstorage.\n\nThis patch series owes a lot to Ronnie Sahlberg and David Turner, who\nwrote earlier drafts along the same basic lines. The current patch\nseries differs from theirs in many details of where exactly to draw\nthe line of abstraction. But the most important difference in my\nopinion is that this series allows multiple ref_stores of different\ntypes to coexist. I think this is important for the UI (for example,\nif somebody converts a submodule to use a different reference storage\nscheme than the umbrella repository uses), but also because it allows\nref_stores to be compounded together internally to decrease the\ncoupling between different parts of the system. I've tried to retain\ntheir authorship of any patches that are more or less recognizable\nfrom their versions (hopefully without introducing any bugs!) But the\nspirit of their versions permeates this patch series. Thanks a lot to\nboth of you!\n\nThis series applies on top of the \"ref-iterator v2\" series that I just\nsubmitted [1]. It can also be obtained from my GitHub repo [5] as\nbranch \"ref-store\".\n\nMichael\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/296322\n[2] http://thread.gmane.org/gmane.comp.version-control.git/286572\n[3] http://thread.gmane.org/gmane.comp.version-control.git/295961/focus=296096\n[4] http://thread.gmane.org/gmane.comp.version-control.git/295961/focus=296186\n[5] https://github.com/mhagger/git\n\nDavid Turner (8):\n  rename_ref_available(): add docstring\n  refs: add methods for reflog\n  refs: add method for initial ref transaction commit\n  refs: add method for delete_refs\n  refs: add methods to init refs db\n  refs: add method to rename refs\n  refs: make lock generic\n  refs: implement iteration over only per-worktree refs\n\nMichael Haggerty (28):\n  resolve_gitlink_ref(): eliminate temporary variable\n  refs: rename struct ref_cache to files_ref_store\n  refs: create a base class \"ref_store\" for files_ref_store\n  add_packed_ref(): add a files_ref_store argument\n  get_packed_ref(): add a files_ref_store argument\n  resolve_missing_loose_ref(): add a files_ref_store argument\n  {lock,commit,rollback}_packed_refs(): add files_ref_store arguments\n  refs: reorder definitions\n  resolve_packed_ref(): rename function from resolve_missing_loose_ref()\n  resolve_gitlink_packed_ref(): remove function\n  read_raw_ref(): take a (struct ref_store *) argument\n  resolve_ref_recursively(): new function\n  resolve_gitlink_ref(): implement using resolve_ref_recursively()\n  resolve_gitlink_ref(): avoid memory allocation in many cases\n  resolve_gitlink_ref(): rename path parameter to submodule\n  refs: make read_raw_ref() virtual\n  refs: make verify_refname_available() virtual\n  refs: make pack_refs() virtual\n  refs: make create_symref() virtual\n  refs: make peel_ref() virtual\n  repack_without_refs(): add a files_ref_store argument\n  lock_raw_ref(): add a files_ref_store argument\n  commit_ref_update(): add a files_ref_store argument\n  lock_ref_for_update(): add a files_ref_store argument\n  lock_ref_sha1_basic(): add a files_ref_store argument\n  split_symref_update(): add a files_ref_store argument\n  files_ref_iterator_begin(): take a ref_store argument\n  refs: add method iterator_begin\n\nRonnie Sahlberg (2):\n  refs: add a backend method structure\n  refs: add a transaction_commit() method\n\n builtin/init-db.c    |  21 +-\n refs.c               | 294 +++++++++++++++++++++++++-\n refs.h               |  13 +-\n refs/files-backend.c | 567 +++++++++++++++++++++++++++------------------------\n refs/refs-internal.h | 213 +++++++++++++++++--\n 5 files changed, 799 insertions(+), 309 deletions(-)\n\n-- \n2.8.1\n"},{"id":"288294","messageId":"c1efb665029d679bdeb3865dffd02e5e8f45db97.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 01/38] resolve_gitlink_ref(): eliminate temporary variable","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:36Z","receivedAt":"2016-06-03T21:03:36Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 5 ++---\n 1 file changed, 2 insertions(+), 3 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 36bf128..2f9d79a 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1387,7 +1387,7 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,\n \n int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n {\n-\tint len = strlen(path), retval;\n+\tint len = strlen(path);\n \tstruct strbuf submodule = STRBUF_INIT;\n \tstruct ref_cache *refs;\n \n@@ -1404,8 +1404,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n \t}\n \tstrbuf_release(&submodule);\n \n-\tretval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0);\n-\treturn retval;\n+\treturn resolve_gitlink_ref_recursive(refs, refname, sha1, 0);\n }\n \n /*\n-- \n2.8.1\n"},{"id":"288331","messageId":"5eb96cc20c0f76aff39d1556803ebd4f9f422cea.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 02/38] rename_ref_available(): add docstring","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:37Z","receivedAt":"2016-06-03T21:03:37Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/refs-internal.h | 5 +++++\n 1 file changed, 5 insertions(+)\n\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex efe5847..d8a2606 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -240,6 +240,11 @@ const char *find_descendant_ref(const char *dirname,\n \t\t\t\tconst struct string_list *extras,\n \t\t\t\tconst struct string_list *skip);\n \n+/*\n+ * Check if the new name does not conflict with any existing refs\n+ * (other than possibly the old ref).  Return 0 if the ref can be\n+ * renamed to the new name.\n+ */\n int rename_ref_available(const char *oldname, const char *newname);\n \n /* We allow \"recursive\" symbolic refs. Only within reason, though */\n-- \n2.8.1\n"},{"id":"288295","messageId":"5d30b5d674b5b30d783f9d7ec0c1f7490f5e151e.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 03/38] refs: rename struct ref_cache to files_ref_store","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:38Z","receivedAt":"2016-06-03T21:03:38Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"The greater goal of this patch series is to develop the concept of a\nreference store, which is a place that references, their values, and\ntheir reflogs are stored, and to virtualize the reference interface so\nthat different types of ref_stores can be implemented. We will then, for\nexample, use ref_store instances to access submodule references and\nworktree references.\n\nCurrently, we keep a ref_cache for each submodule that has had its\nreferences iterated over. It is a far cry from a ref_store, but they are\nstored the way we will want to store ref_stores, and ref_stores will\neventually have to hold the reference caches. So let's treat ref_caches\nas embryo ref_stores, and build them out from there.\n\nAs the first step, simply rename `ref_cache` to `files_ref_store`, and\nrename some functions and attributes correspondingly.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 126 +++++++++++++++++++++++++--------------------------\n 1 file changed, 63 insertions(+), 63 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 2f9d79a..b94aad2 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -39,7 +39,7 @@ struct ref_value {\n \tstruct object_id peeled;\n };\n \n-struct ref_cache;\n+struct files_ref_store;\n \n /*\n  * Information used (along with the information in ref_entry) to\n@@ -78,8 +78,8 @@ struct ref_dir {\n \t */\n \tint sorted;\n \n-\t/* A pointer to the ref_cache that contains this ref_dir. */\n-\tstruct ref_cache *ref_cache;\n+\t/* A pointer to the files_ref_store that contains this ref_dir. */\n+\tstruct files_ref_store *ref_store;\n \n \tstruct ref_entry **entries;\n };\n@@ -161,7 +161,7 @@ struct ref_entry {\n \n static void read_loose_refs(const char *dirname, struct ref_dir *dir);\n static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len);\n-static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,\n+static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,\n \t\t\t\t\t  const char *dirname, size_t len,\n \t\t\t\t\t  int incomplete);\n static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);\n@@ -183,7 +183,7 @@ static struct ref_dir *get_ref_dir(struct ref_entry *entry)\n \t\t\tint pos = search_ref_dir(dir, \"refs/bisect/\", 12);\n \t\t\tif (pos < 0) {\n \t\t\t\tstruct ref_entry *child_entry;\n-\t\t\t\tchild_entry = create_dir_entry(dir->ref_cache,\n+\t\t\t\tchild_entry = create_dir_entry(dir->ref_store,\n \t\t\t\t\t\t\t       \"refs/bisect/\",\n \t\t\t\t\t\t\t       12, 1);\n \t\t\t\tadd_entry_to_dir(dir, child_entry);\n@@ -261,13 +261,13 @@ static void clear_ref_dir(struct ref_dir *dir)\n  * dirname is the name of the directory with a trailing slash (e.g.,\n  * \"refs/heads/\") or \"\" for the top-level directory.\n  */\n-static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,\n+static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,\n \t\t\t\t\t  const char *dirname, size_t len,\n \t\t\t\t\t  int incomplete)\n {\n \tstruct ref_entry *direntry;\n \tFLEX_ALLOC_MEM(direntry, name, dirname, len);\n-\tdirentry->u.subdir.ref_cache = ref_cache;\n+\tdirentry->u.subdir.ref_store = ref_store;\n \tdirentry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);\n \treturn direntry;\n }\n@@ -343,7 +343,7 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,\n \t\t * therefore, create an empty record for it but mark\n \t\t * the record complete.\n \t\t */\n-\t\tentry = create_dir_entry(dir->ref_cache, subdirname, len, 0);\n+\t\tentry = create_dir_entry(dir->ref_store, subdirname, len, 0);\n \t\tadd_entry_to_dir(dir, entry);\n \t} else {\n \t\tentry = dir->entries[entry_index];\n@@ -887,9 +887,9 @@ struct packed_ref_cache {\n \n \t/*\n \t * Count of references to the data structure in this instance,\n-\t * including the pointer from ref_cache::packed if any.  The\n-\t * data will not be freed as long as the reference count is\n-\t * nonzero.\n+\t * including the pointer from files_ref_store::packed if any.\n+\t * The data will not be freed as long as the reference count\n+\t * is nonzero.\n \t */\n \tunsigned int referrers;\n \n@@ -910,17 +910,17 @@ struct packed_ref_cache {\n  * Future: need to be in \"struct repository\"\n  * when doing a full libification.\n  */\n-static struct ref_cache {\n-\tstruct ref_cache *next;\n+static struct files_ref_store {\n+\tstruct files_ref_store *next;\n \tstruct ref_entry *loose;\n \tstruct packed_ref_cache *packed;\n \t/*\n-\t * The submodule name, or \"\" for the main repo.  We allocate\n-\t * length 1 rather than FLEX_ARRAY so that the main ref_cache\n-\t * is initialized correctly.\n+\t * The submodule name, or \"\" for the main repo. We allocate\n+\t * length 1 rather than FLEX_ARRAY so that the main\n+\t * files_ref_store is initialized correctly.\n \t */\n \tchar name[1];\n-} ref_cache, *submodule_ref_caches;\n+} ref_store, *submodule_ref_stores;\n \n /* Lock used for the main packed-refs file: */\n static struct lock_file packlock;\n@@ -949,7 +949,7 @@ static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)\n \t}\n }\n \n-static void clear_packed_ref_cache(struct ref_cache *refs)\n+static void clear_packed_ref_cache(struct files_ref_store *refs)\n {\n \tif (refs->packed) {\n \t\tstruct packed_ref_cache *packed_refs = refs->packed;\n@@ -961,7 +961,7 @@ static void clear_packed_ref_cache(struct ref_cache *refs)\n \t}\n }\n \n-static void clear_loose_ref_cache(struct ref_cache *refs)\n+static void clear_loose_ref_cache(struct files_ref_store *refs)\n {\n \tif (refs->loose) {\n \t\tfree_ref_entry(refs->loose);\n@@ -973,32 +973,32 @@ static void clear_loose_ref_cache(struct ref_cache *refs)\n  * Create a new submodule ref cache and add it to the internal\n  * set of caches.\n  */\n-static struct ref_cache *create_ref_cache(const char *submodule)\n+static struct files_ref_store *create_ref_store(const char *submodule)\n {\n-\tstruct ref_cache *refs;\n+\tstruct files_ref_store *refs;\n \tif (!submodule)\n \t\tsubmodule = \"\";\n \tFLEX_ALLOC_STR(refs, name, submodule);\n-\trefs->next = submodule_ref_caches;\n-\tsubmodule_ref_caches = refs;\n+\trefs->next = submodule_ref_stores;\n+\tsubmodule_ref_stores = refs;\n \treturn refs;\n }\n \n-static struct ref_cache *lookup_ref_cache(const char *submodule)\n+static struct files_ref_store *lookup_ref_store(const char *submodule)\n {\n-\tstruct ref_cache *refs;\n+\tstruct files_ref_store *refs;\n \n \tif (!submodule || !*submodule)\n-\t\treturn &ref_cache;\n+\t\treturn &ref_store;\n \n-\tfor (refs = submodule_ref_caches; refs; refs = refs->next)\n+\tfor (refs = submodule_ref_stores; refs; refs = refs->next)\n \t\tif (!strcmp(submodule, refs->name))\n \t\t\treturn refs;\n \treturn NULL;\n }\n \n /*\n- * Return a pointer to a ref_cache for the specified submodule. For\n+ * Return a pointer to a files_ref_store for the specified submodule. For\n  * the main repository, use submodule==NULL; such a call cannot fail.\n  * For a submodule, the submodule must exist and be a nonbare\n  * repository, otherwise return NULL.\n@@ -1006,16 +1006,16 @@ static struct ref_cache *lookup_ref_cache(const char *submodule)\n  * The returned structure will be allocated and initialized but not\n  * necessarily populated; it should not be freed.\n  */\n-static struct ref_cache *get_ref_cache(const char *submodule)\n+static struct files_ref_store *get_ref_store(const char *submodule)\n {\n-\tstruct ref_cache *refs = lookup_ref_cache(submodule);\n+\tstruct files_ref_store *refs = lookup_ref_store(submodule);\n \n \tif (!refs) {\n \t\tstruct strbuf submodule_sb = STRBUF_INIT;\n \n \t\tstrbuf_addstr(&submodule_sb, submodule);\n \t\tif (is_nonbare_repository_dir(&submodule_sb))\n-\t\t\trefs = create_ref_cache(submodule);\n+\t\t\trefs = create_ref_store(submodule);\n \t\tstrbuf_release(&submodule_sb);\n \t}\n \n@@ -1151,10 +1151,10 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)\n }\n \n /*\n- * Get the packed_ref_cache for the specified ref_cache, creating it\n- * if necessary.\n+ * Get the packed_ref_cache for the specified files_ref_store,\n+ * creating it if necessary.\n  */\n-static struct packed_ref_cache *get_packed_ref_cache(struct ref_cache *refs)\n+static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)\n {\n \tchar *packed_refs_file;\n \n@@ -1189,7 +1189,7 @@ static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_ca\n \treturn get_ref_dir(packed_ref_cache->root);\n }\n \n-static struct ref_dir *get_packed_refs(struct ref_cache *refs)\n+static struct ref_dir *get_packed_refs(struct files_ref_store *refs)\n {\n \treturn get_packed_ref_dir(get_packed_ref_cache(refs));\n }\n@@ -1203,7 +1203,7 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)\n static void add_packed_ref(const char *refname, const unsigned char *sha1)\n {\n \tstruct packed_ref_cache *packed_ref_cache =\n-\t\tget_packed_ref_cache(&ref_cache);\n+\t\tget_packed_ref_cache(&ref_store);\n \n \tif (!packed_ref_cache->lock)\n \t\tdie(\"internal error: packed refs not locked\");\n@@ -1218,7 +1218,7 @@ static void add_packed_ref(const char *refname, const unsigned char *sha1)\n  */\n static void read_loose_refs(const char *dirname, struct ref_dir *dir)\n {\n-\tstruct ref_cache *refs = dir->ref_cache;\n+\tstruct files_ref_store *refs = dir->ref_store;\n \tDIR *d;\n \tstruct dirent *de;\n \tint dirnamelen = strlen(dirname);\n@@ -1306,7 +1306,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)\n \tclosedir(d);\n }\n \n-static struct ref_dir *get_loose_refs(struct ref_cache *refs)\n+static struct ref_dir *get_loose_refs(struct files_ref_store *refs)\n {\n \tif (!refs->loose) {\n \t\t/*\n@@ -1328,10 +1328,10 @@ static struct ref_dir *get_loose_refs(struct ref_cache *refs)\n \n /*\n  * Called by resolve_gitlink_ref_recursive() after it failed to read\n- * from the loose refs in ref_cache refs. Find <refname> in the\n- * packed-refs file for the submodule.\n+ * from the loose refs in refs. Find <refname> in the packed-refs file\n+ * for the submodule.\n  */\n-static int resolve_gitlink_packed_ref(struct ref_cache *refs,\n+static int resolve_gitlink_packed_ref(struct files_ref_store *refs,\n \t\t\t\t      const char *refname, unsigned char *sha1)\n {\n \tstruct ref_entry *ref;\n@@ -1345,7 +1345,7 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,\n \treturn 0;\n }\n \n-static int resolve_gitlink_ref_recursive(struct ref_cache *refs,\n+static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n \t\t\t\t\t const char *refname, unsigned char *sha1,\n \t\t\t\t\t int recursion)\n {\n@@ -1389,7 +1389,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n {\n \tint len = strlen(path);\n \tstruct strbuf submodule = STRBUF_INIT;\n-\tstruct ref_cache *refs;\n+\tstruct files_ref_store *refs;\n \n \twhile (len && path[len-1] == '/')\n \t\tlen--;\n@@ -1397,7 +1397,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n \t\treturn -1;\n \n \tstrbuf_add(&submodule, path, len);\n-\trefs = get_ref_cache(submodule.buf);\n+\trefs = get_ref_store(submodule.buf);\n \tif (!refs) {\n \t\tstrbuf_release(&submodule);\n \t\treturn -1;\n@@ -1413,7 +1413,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n  */\n static struct ref_entry *get_packed_ref(const char *refname)\n {\n-\treturn find_ref(get_packed_refs(&ref_cache), refname);\n+\treturn find_ref(get_packed_refs(&ref_store), refname);\n }\n \n /*\n@@ -1745,7 +1745,7 @@ retry:\n \t\t\t\t\t\t\t  REMOVE_DIR_EMPTY_ONLY)) {\n \t\t\t\tif (verify_refname_available_dir(\n \t\t\t\t\t\t    refname, extras, skip,\n-\t\t\t\t\t\t    get_loose_refs(&ref_cache),\n+\t\t\t\t\t\t    get_loose_refs(&ref_store),\n \t\t\t\t\t\t    err)) {\n \t\t\t\t\t/*\n \t\t\t\t\t * The error message set by\n@@ -1784,7 +1784,7 @@ retry:\n \t\t */\n \t\tif (verify_refname_available_dir(\n \t\t\t\t    refname, extras, skip,\n-\t\t\t\t    get_packed_refs(&ref_cache),\n+\t\t\t\t    get_packed_refs(&ref_store),\n \t\t\t\t    err)) {\n \t\t\tgoto error_return;\n \t\t}\n@@ -1942,7 +1942,7 @@ struct ref_iterator *files_ref_iterator_begin(\n \t\tconst char *submodule,\n \t\tconst char *prefix, unsigned int flags)\n {\n-\tstruct ref_cache *refs = get_ref_cache(submodule);\n+\tstruct files_ref_store *refs = get_ref_store(submodule);\n \tstruct ref_dir *loose_dir, *packed_dir;\n \tstruct ref_iterator *loose_iter, *packed_iter;\n \tstruct files_ref_iterator *iter;\n@@ -2096,7 +2096,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t\tif (remove_empty_directories(&ref_file)) {\n \t\t\tlast_errno = errno;\n \t\t\tif (!verify_refname_available_dir(refname, extras, skip,\n-\t\t\t\t\t\t\t  get_loose_refs(&ref_cache), err))\n+\t\t\t\t\t\t\t  get_loose_refs(&ref_store), err))\n \t\t\t\tstrbuf_addf(err, \"there are still refs under '%s'\",\n \t\t\t\t\t    refname);\n \t\t\tgoto error_return;\n@@ -2108,7 +2108,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t\tlast_errno = errno;\n \t\tif (last_errno != ENOTDIR ||\n \t\t    !verify_refname_available_dir(refname, extras, skip,\n-\t\t\t\t\t\t  get_loose_refs(&ref_cache), err))\n+\t\t\t\t\t\t  get_loose_refs(&ref_store), err))\n \t\t\tstrbuf_addf(err, \"unable to resolve reference '%s': %s\",\n \t\t\t\t    refname, strerror(last_errno));\n \n@@ -2123,7 +2123,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t */\n \tif (is_null_oid(&lock->old_oid) &&\n \t    verify_refname_available_dir(refname, extras, skip,\n-\t\t\t\t\t get_packed_refs(&ref_cache), err)) {\n+\t\t\t\t\t get_packed_refs(&ref_store), err)) {\n \t\tlast_errno = ENOTDIR;\n \t\tgoto error_return;\n \t}\n@@ -2232,7 +2232,7 @@ static int lock_packed_refs(int flags)\n \t * this will automatically invalidate the cache and re-read\n \t * the packed-refs file.\n \t */\n-\tpacked_ref_cache = get_packed_ref_cache(&ref_cache);\n+\tpacked_ref_cache = get_packed_ref_cache(&ref_store);\n \tpacked_ref_cache->lock = &packlock;\n \t/* Increment the reference count to prevent it from being freed: */\n \tacquire_packed_ref_cache(packed_ref_cache);\n@@ -2248,7 +2248,7 @@ static int lock_packed_refs(int flags)\n static int commit_packed_refs(void)\n {\n \tstruct packed_ref_cache *packed_ref_cache =\n-\t\tget_packed_ref_cache(&ref_cache);\n+\t\tget_packed_ref_cache(&ref_store);\n \tint error = 0;\n \tint save_errno = 0;\n \tFILE *out;\n@@ -2282,14 +2282,14 @@ static int commit_packed_refs(void)\n static void rollback_packed_refs(void)\n {\n \tstruct packed_ref_cache *packed_ref_cache =\n-\t\tget_packed_ref_cache(&ref_cache);\n+\t\tget_packed_ref_cache(&ref_store);\n \n \tif (!packed_ref_cache->lock)\n \t\tdie(\"internal error: packed-refs not locked\");\n \trollback_lock_file(packed_ref_cache->lock);\n \tpacked_ref_cache->lock = NULL;\n \trelease_packed_ref_cache(packed_ref_cache);\n-\tclear_packed_ref_cache(&ref_cache);\n+\tclear_packed_ref_cache(&ref_store);\n }\n \n struct ref_to_prune {\n@@ -2428,9 +2428,9 @@ int pack_refs(unsigned int flags)\n \tcbdata.flags = flags;\n \n \tlock_packed_refs(LOCK_DIE_ON_ERROR);\n-\tcbdata.packed_refs = get_packed_refs(&ref_cache);\n+\tcbdata.packed_refs = get_packed_refs(&ref_store);\n \n-\tdo_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0,\n+\tdo_for_each_entry_in_dir(get_loose_refs(&ref_store), 0,\n \t\t\t\t pack_if_possible_fn, &cbdata);\n \n \tif (commit_packed_refs())\n@@ -2471,7 +2471,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n \t\tunable_to_lock_message(git_path(\"packed-refs\"), errno, err);\n \t\treturn -1;\n \t}\n-\tpacked = get_packed_refs(&ref_cache);\n+\tpacked = get_packed_refs(&ref_store);\n \n \t/* Remove refnames from the cache */\n \tfor_each_string_list_item(refname, refnames)\n@@ -2616,8 +2616,8 @@ int verify_refname_available(const char *newname,\n \t\t\t     const struct string_list *skip,\n \t\t\t     struct strbuf *err)\n {\n-\tstruct ref_dir *packed_refs = get_packed_refs(&ref_cache);\n-\tstruct ref_dir *loose_refs = get_loose_refs(&ref_cache);\n+\tstruct ref_dir *packed_refs = get_packed_refs(&ref_store);\n+\tstruct ref_dir *loose_refs = get_loose_refs(&ref_store);\n \n \tif (verify_refname_available_dir(newname, extras, skip,\n \t\t\t\t\t packed_refs, err) ||\n@@ -2968,7 +2968,7 @@ static int commit_ref_update(struct ref_lock *lock,\n \t\t\t     const unsigned char *sha1, const char *logmsg,\n \t\t\t     struct strbuf *err)\n {\n-\tclear_loose_ref_cache(&ref_cache);\n+\tclear_loose_ref_cache(&ref_store);\n \tif (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {\n \t\tchar *old_msg = strbuf_detach(err, NULL);\n \t\tstrbuf_addf(err, \"cannot update the ref '%s': %s\",\n@@ -3790,7 +3790,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t}\n \t\t}\n \t\tif (update->flags & REF_NEEDS_COMMIT) {\n-\t\t\tclear_loose_ref_cache(&ref_cache);\n+\t\t\tclear_loose_ref_cache(&ref_store);\n \t\t\tif (commit_ref(lock)) {\n \t\t\t\tstrbuf_addf(err, \"couldn't set '%s'\", lock->ref_name);\n \t\t\t\tunlock_ref(lock);\n@@ -3823,7 +3823,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t}\n \tfor_each_string_list_item(ref_to_delete, &refs_to_delete)\n \t\tunlink_or_warn(git_path(\"logs/%s\", ref_to_delete->string));\n-\tclear_loose_ref_cache(&ref_cache);\n+\tclear_loose_ref_cache(&ref_store);\n \n cleanup:\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n-- \n2.8.1\n"},{"id":"288323","messageId":"151f90bea3b22cc1f0818b08c1505aad2db891d4.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 04/38] refs: add a backend method structure","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:39Z","receivedAt":"2016-06-03T21:03:39Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: Ronnie Sahlberg <sahlberg@google.com>\n\nAdd a `struct ref_storage_be` to represent types of reference stores. In\nOO notation, this is the class, and will soon hold some class\nmethods (e.g., a factory to create new ref_store instances) and will\nalso serve as the vtable for ref_store instances of that type.\n\nAs yet, the backends cannot do anything.\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Jeff King <peff@peff.net>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 19 +++++++++++++++++++\n refs.h               |  2 ++\n refs/files-backend.c |  5 +++++\n refs/refs-internal.h |  8 ++++++++\n 4 files changed, 34 insertions(+)\n\ndiff --git a/refs.c b/refs.c\nindex 814cad3..f57a93e 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -10,6 +10,25 @@\n #include \"tag.h\"\n \n /*\n+ * List of all available backends\n+ */\n+static struct ref_storage_be *refs_backends = &refs_be_files;\n+\n+static struct ref_storage_be *find_ref_storage_backend(const char *name)\n+{\n+\tstruct ref_storage_be *be;\n+\tfor (be = refs_backends; be; be = be->next)\n+\t\tif (!strcmp(be->name, name))\n+\t\t\treturn be;\n+\treturn NULL;\n+}\n+\n+int ref_storage_backend_exists(const char *name)\n+{\n+\treturn find_ref_storage_backend(name) != NULL;\n+}\n+\n+/*\n  * How to handle various characters in refnames:\n  * 0: An acceptable character for refs\n  * 1: End-of-component\ndiff --git a/refs.h b/refs.h\nindex 442c1a5..52ea93b 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -544,4 +544,6 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n \t\t  reflog_expiry_cleanup_fn cleanup_fn,\n \t\t  void *policy_cb_data);\n \n+int ref_storage_backend_exists(const char *name);\n+\n #endif /* REFS_H */\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex b94aad2..bde6f0e 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -4066,3 +4066,8 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n \tunlock_ref(lock);\n \treturn -1;\n }\n+\n+struct ref_storage_be refs_be_files = {\n+\tNULL,\n+\t\"files\"\n+};\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex d8a2606..4d88849 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -520,4 +520,12 @@ int do_for_each_ref_iterator(struct ref_iterator *iter,\n int read_raw_ref(const char *refname, unsigned char *sha1,\n \t\t struct strbuf *referent, unsigned int *type);\n \n+/* refs backends */\n+struct ref_storage_be {\n+\tstruct ref_storage_be *next;\n+\tconst char *name;\n+};\n+\n+extern struct ref_storage_be refs_be_files;\n+\n #endif /* REFS_REFS_INTERNAL_H */\n-- \n2.8.1\n"},{"id":"288322","messageId":"8ee7d7359f6763cba29dc788aba2f236204bb76e.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:40Z","receivedAt":"2016-06-03T21:03:40Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"We want ref_stores to be polymorphic, so invent a base class of which\nfiles_ref_store is a derived class. For now there is a one-to-one\nrelationship between ref_stores and submodules.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               |  91 ++++++++++++++++++++++++++\n refs/files-backend.c | 179 +++++++++++++++++++++++++++++----------------------\n refs/refs-internal.h |  78 ++++++++++++++++++++++\n 3 files changed, 271 insertions(+), 77 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex f57a93e..5e20ae0 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1151,8 +1151,12 @@ int head_ref(each_ref_fn fn, void *cb_data)\n static int do_for_each_ref(const char *submodule, const char *prefix,\n \t\t\t   each_ref_fn fn, int trim, int flags, void *cb_data)\n {\n+\tstruct ref_store *refs = get_ref_store(submodule);\n \tstruct ref_iterator *iter;\n \n+\tif (!refs)\n+\t\treturn 0;\n+\n \titer = files_ref_iterator_begin(submodule, prefix, flags);\n \titer = prefix_ref_iterator_begin(iter, prefix, trim);\n \n@@ -1284,3 +1288,90 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \terrno = ELOOP;\n \treturn NULL;\n }\n+\n+static struct ref_store *main_ref_store = NULL;\n+\n+static struct ref_store *submodule_ref_stores = NULL;\n+\n+void base_ref_store_init(struct ref_store *refs,\n+\t\t\t const struct ref_storage_be *be,\n+\t\t\t const char *submodule)\n+{\n+\trefs->be = be;\n+\tif (!submodule) {\n+\t\tif (main_ref_store)\n+\t\t\tdie(\"BUG: main_ref_store initialized twice\");\n+\n+\t\trefs->submodule = \"\";\n+\t\trefs->next = NULL;\n+\t\tmain_ref_store = refs;\n+\t} else {\n+\t\tif (lookup_ref_store(submodule))\n+\t\t\tdie(\"BUG: ref_store for submodule '%s' initialized twice\",\n+\t\t\t    submodule);\n+\n+\t\trefs->submodule = xstrdup(submodule);\n+\t\trefs->next = submodule_ref_stores;\n+\t\tsubmodule_ref_stores = refs;\n+\t}\n+}\n+\n+struct ref_store *ref_store_init(const char *submodule)\n+{\n+\tconst char *be_name = \"files\";\n+\tstruct ref_storage_be *be = find_ref_storage_backend(be_name);\n+\n+\tif (!be)\n+\t\tdie(\"BUG: reference backend %s is unknown\", be_name);\n+\n+\tif (!submodule || !*submodule)\n+\t\treturn be->init(NULL);\n+\telse\n+\t\treturn be->init(submodule);\n+}\n+\n+struct ref_store *lookup_ref_store(const char *submodule)\n+{\n+\tstruct ref_store *refs;\n+\n+\tif (!submodule || !*submodule)\n+\t\treturn main_ref_store;\n+\n+\tfor (refs = submodule_ref_stores; refs; refs = refs->next) {\n+\t\tif (!strcmp(submodule, refs->submodule))\n+\t\t\treturn refs;\n+\t}\n+\n+\treturn NULL;\n+}\n+\n+struct ref_store *get_ref_store(const char *submodule)\n+{\n+\tstruct ref_store *refs;\n+\n+\tif (!submodule || !*submodule) {\n+\t\trefs = lookup_ref_store(NULL);\n+\n+\t\tif (!refs)\n+\t\t\trefs = ref_store_init(NULL);\n+\t} else {\n+\t\trefs = lookup_ref_store(submodule);\n+\n+\t\tif (!refs) {\n+\t\t\tstruct strbuf submodule_sb = STRBUF_INIT;\n+\n+\t\t\tstrbuf_addstr(&submodule_sb, submodule);\n+\t\t\tif (is_nonbare_repository_dir(&submodule_sb))\n+\t\t\t\trefs = ref_store_init(submodule);\n+\t\t\tstrbuf_release(&submodule_sb);\n+\t\t}\n+\t}\n+\n+\treturn refs;\n+}\n+\n+void assert_main_repository(struct ref_store *refs, const char *caller)\n+{\n+\tif (*refs->submodule)\n+\t\tdie(\"BUG: %s called for a submodule\", caller);\n+}\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex bde6f0e..8ef79c2 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -910,17 +910,11 @@ struct packed_ref_cache {\n  * Future: need to be in \"struct repository\"\n  * when doing a full libification.\n  */\n-static struct files_ref_store {\n-\tstruct files_ref_store *next;\n+struct files_ref_store {\n+\tstruct ref_store base;\n \tstruct ref_entry *loose;\n \tstruct packed_ref_cache *packed;\n-\t/*\n-\t * The submodule name, or \"\" for the main repo. We allocate\n-\t * length 1 rather than FLEX_ARRAY so that the main\n-\t * files_ref_store is initialized correctly.\n-\t */\n-\tchar name[1];\n-} ref_store, *submodule_ref_stores;\n+};\n \n /* Lock used for the main packed-refs file: */\n static struct lock_file packlock;\n@@ -973,53 +967,54 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)\n  * Create a new submodule ref cache and add it to the internal\n  * set of caches.\n  */\n-static struct files_ref_store *create_ref_store(const char *submodule)\n+static struct ref_store *files_ref_store_create(const char *submodule)\n {\n-\tstruct files_ref_store *refs;\n-\tif (!submodule)\n-\t\tsubmodule = \"\";\n-\tFLEX_ALLOC_STR(refs, name, submodule);\n-\trefs->next = submodule_ref_stores;\n-\tsubmodule_ref_stores = refs;\n-\treturn refs;\n+\tstruct files_ref_store *refs = xcalloc(1, sizeof(*refs));\n+\tstruct ref_store *ref_store = (struct ref_store *)refs;\n+\n+\tbase_ref_store_init(ref_store, &refs_be_files, submodule);\n+\n+\treturn ref_store;\n }\n \n-static struct files_ref_store *lookup_ref_store(const char *submodule)\n+/*\n+ * Downcast ref_store to files_ref_store. Die if ref_store is not a\n+ * files_ref_store. If submodule_allowed is not true, then also die if\n+ * files_ref_store is for a submodule (i.e., not for the main\n+ * repository). caller is used in any necessary error messages.\n+ */\n+static struct files_ref_store *files_downcast(\n+\t\tstruct ref_store *ref_store, int submodule_allowed,\n+\t\tconst char *caller)\n {\n \tstruct files_ref_store *refs;\n \n-\tif (!submodule || !*submodule)\n-\t\treturn &ref_store;\n+\tif (ref_store->be != &refs_be_files)\n+\t\tdie(\"BUG: ref_store is type \\\"%s\\\" not \\\"files\\\" in %s\",\n+\t\t    ref_store->be->name, caller);\n \n-\tfor (refs = submodule_ref_stores; refs; refs = refs->next)\n-\t\tif (!strcmp(submodule, refs->name))\n-\t\t\treturn refs;\n-\treturn NULL;\n+\trefs = (struct files_ref_store *)ref_store;\n+\n+\tif (!submodule_allowed)\n+\t\tassert_main_repository(ref_store, caller);\n+\n+\treturn refs;\n }\n \n /*\n- * Return a pointer to a files_ref_store for the specified submodule. For\n- * the main repository, use submodule==NULL; such a call cannot fail.\n- * For a submodule, the submodule must exist and be a nonbare\n- * repository, otherwise return NULL.\n- *\n- * The returned structure will be allocated and initialized but not\n- * necessarily populated; it should not be freed.\n+ * Return a pointer to the reference store for the specified\n+ * submodule. For the main repository, use submodule==NULL; such a\n+ * call cannot fail. For a submodule, the submodule must exist and be\n+ * a nonbare repository, otherwise return NULL. Verify that the\n+ * reference store is a files_ref_store, and cast it to that type\n+ * before returning it.\n  */\n-static struct files_ref_store *get_ref_store(const char *submodule)\n+static struct files_ref_store *get_files_ref_store(const char *submodule,\n+\t\t\t\t\t\t   const char *caller)\n {\n-\tstruct files_ref_store *refs = lookup_ref_store(submodule);\n+\tstruct ref_store *refs = get_ref_store(submodule);\n \n-\tif (!refs) {\n-\t\tstruct strbuf submodule_sb = STRBUF_INIT;\n-\n-\t\tstrbuf_addstr(&submodule_sb, submodule);\n-\t\tif (is_nonbare_repository_dir(&submodule_sb))\n-\t\t\trefs = create_ref_store(submodule);\n-\t\tstrbuf_release(&submodule_sb);\n-\t}\n-\n-\treturn refs;\n+\treturn refs ? files_downcast(refs, 1, caller) : NULL;\n }\n \n /* The length of a peeled reference line in packed-refs, including EOL: */\n@@ -1158,8 +1153,9 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref\n {\n \tchar *packed_refs_file;\n \n-\tif (*refs->name)\n-\t\tpacked_refs_file = git_pathdup_submodule(refs->name, \"packed-refs\");\n+\tif (*refs->base.submodule)\n+\t\tpacked_refs_file = git_pathdup_submodule(refs->base.submodule,\n+\t\t\t\t\t\t\t \"packed-refs\");\n \telse\n \t\tpacked_refs_file = git_pathdup(\"packed-refs\");\n \n@@ -1202,8 +1198,9 @@ static struct ref_dir *get_packed_refs(struct files_ref_store *refs)\n  */\n static void add_packed_ref(const char *refname, const unsigned char *sha1)\n {\n-\tstruct packed_ref_cache *packed_ref_cache =\n-\t\tget_packed_ref_cache(&ref_store);\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"add_packed_ref\");\n+\tstruct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);\n \n \tif (!packed_ref_cache->lock)\n \t\tdie(\"internal error: packed refs not locked\");\n@@ -1226,8 +1223,8 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)\n \tstruct strbuf path = STRBUF_INIT;\n \tsize_t path_baselen;\n \n-\tif (*refs->name)\n-\t\tstrbuf_git_path_submodule(&path, refs->name, \"%s\", dirname);\n+\tif (*refs->base.submodule)\n+\t\tstrbuf_git_path_submodule(&path, refs->base.submodule, \"%s\", dirname);\n \telse\n \t\tstrbuf_git_path(&path, \"%s\", dirname);\n \tpath_baselen = path.len;\n@@ -1262,10 +1259,10 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)\n \t\t} else {\n \t\t\tint read_ok;\n \n-\t\t\tif (*refs->name) {\n+\t\t\tif (*refs->base.submodule) {\n \t\t\t\thashclr(sha1);\n \t\t\t\tflag = 0;\n-\t\t\t\tread_ok = !resolve_gitlink_ref(refs->name,\n+\t\t\t\tread_ok = !resolve_gitlink_ref(refs->base.submodule,\n \t\t\t\t\t\t\t       refname.buf, sha1);\n \t\t\t} else {\n \t\t\t\tread_ok = !read_ref_full(refname.buf,\n@@ -1355,8 +1352,8 @@ static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n \n \tif (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)\n \t\treturn -1;\n-\tpath = *refs->name\n-\t\t? git_pathdup_submodule(refs->name, \"%s\", refname)\n+\tpath = *refs->base.submodule\n+\t\t? git_pathdup_submodule(refs->base.submodule, \"%s\", refname)\n \t\t: git_pathdup(\"%s\", refname);\n \tfd = open(path, O_RDONLY);\n \tfree(path);\n@@ -1397,7 +1394,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n \t\treturn -1;\n \n \tstrbuf_add(&submodule, path, len);\n-\trefs = get_ref_store(submodule.buf);\n+\trefs = get_files_ref_store(submodule.buf, \"resolve_gitlink_ref\");\n \tif (!refs) {\n \t\tstrbuf_release(&submodule);\n \t\treturn -1;\n@@ -1413,7 +1410,10 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n  */\n static struct ref_entry *get_packed_ref(const char *refname)\n {\n-\treturn find_ref(get_packed_refs(&ref_store), refname);\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"get_packed_ref\");\n+\n+\treturn find_ref(get_packed_refs(refs), refname);\n }\n \n /*\n@@ -1613,6 +1613,8 @@ static int lock_raw_ref(const char *refname, int mustexist,\n \t\t\tunsigned int *type,\n \t\t\tstruct strbuf *err)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"lock_raw_ref\");\n \tstruct ref_lock *lock;\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tint attempts_remaining = 3;\n@@ -1745,7 +1747,7 @@ retry:\n \t\t\t\t\t\t\t  REMOVE_DIR_EMPTY_ONLY)) {\n \t\t\t\tif (verify_refname_available_dir(\n \t\t\t\t\t\t    refname, extras, skip,\n-\t\t\t\t\t\t    get_loose_refs(&ref_store),\n+\t\t\t\t\t\t    get_loose_refs(refs),\n \t\t\t\t\t\t    err)) {\n \t\t\t\t\t/*\n \t\t\t\t\t * The error message set by\n@@ -1784,7 +1786,7 @@ retry:\n \t\t */\n \t\tif (verify_refname_available_dir(\n \t\t\t\t    refname, extras, skip,\n-\t\t\t\t    get_packed_refs(&ref_store),\n+\t\t\t\t    get_packed_refs(refs),\n \t\t\t\t    err)) {\n \t\t\tgoto error_return;\n \t\t}\n@@ -1942,7 +1944,8 @@ struct ref_iterator *files_ref_iterator_begin(\n \t\tconst char *submodule,\n \t\tconst char *prefix, unsigned int flags)\n {\n-\tstruct files_ref_store *refs = get_ref_store(submodule);\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(submodule, \"ref_iterator_begin\");\n \tstruct ref_dir *loose_dir, *packed_dir;\n \tstruct ref_iterator *loose_iter, *packed_iter;\n \tstruct files_ref_iterator *iter;\n@@ -2065,6 +2068,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t\t\t\t\t    unsigned int flags, int *type,\n \t\t\t\t\t    struct strbuf *err)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"lock_ref_sha1_basic\");\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tstruct ref_lock *lock;\n \tint last_errno = 0;\n@@ -2095,8 +2100,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t\t */\n \t\tif (remove_empty_directories(&ref_file)) {\n \t\t\tlast_errno = errno;\n-\t\t\tif (!verify_refname_available_dir(refname, extras, skip,\n-\t\t\t\t\t\t\t  get_loose_refs(&ref_store), err))\n+\t\t\tif (!verify_refname_available_dir(\n+\t\t\t\t\t    refname, extras, skip,\n+\t\t\t\t\t    get_loose_refs(refs), err))\n \t\t\t\tstrbuf_addf(err, \"there are still refs under '%s'\",\n \t\t\t\t\t    refname);\n \t\t\tgoto error_return;\n@@ -2107,8 +2113,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \tif (!resolved) {\n \t\tlast_errno = errno;\n \t\tif (last_errno != ENOTDIR ||\n-\t\t    !verify_refname_available_dir(refname, extras, skip,\n-\t\t\t\t\t\t  get_loose_refs(&ref_store), err))\n+\t\t    !verify_refname_available_dir(\n+\t\t\t\t    refname, extras, skip,\n+\t\t\t\t    get_loose_refs(refs), err))\n \t\t\tstrbuf_addf(err, \"unable to resolve reference '%s': %s\",\n \t\t\t\t    refname, strerror(last_errno));\n \n@@ -2123,7 +2130,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \t */\n \tif (is_null_oid(&lock->old_oid) &&\n \t    verify_refname_available_dir(refname, extras, skip,\n-\t\t\t\t\t get_packed_refs(&ref_store), err)) {\n+\t\t\t\t\t get_packed_refs(refs),\n+\t\t\t\t\t err)) {\n \t\tlast_errno = ENOTDIR;\n \t\tgoto error_return;\n \t}\n@@ -2212,9 +2220,10 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)\n  */\n static int lock_packed_refs(int flags)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"lock_packed_refs\");\n \tstatic int timeout_configured = 0;\n \tstatic int timeout_value = 1000;\n-\n \tstruct packed_ref_cache *packed_ref_cache;\n \n \tif (!timeout_configured) {\n@@ -2232,7 +2241,7 @@ static int lock_packed_refs(int flags)\n \t * this will automatically invalidate the cache and re-read\n \t * the packed-refs file.\n \t */\n-\tpacked_ref_cache = get_packed_ref_cache(&ref_store);\n+\tpacked_ref_cache = get_packed_ref_cache(refs);\n \tpacked_ref_cache->lock = &packlock;\n \t/* Increment the reference count to prevent it from being freed: */\n \tacquire_packed_ref_cache(packed_ref_cache);\n@@ -2247,8 +2256,10 @@ static int lock_packed_refs(int flags)\n  */\n static int commit_packed_refs(void)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"commit_packed_refs\");\n \tstruct packed_ref_cache *packed_ref_cache =\n-\t\tget_packed_ref_cache(&ref_store);\n+\t\tget_packed_ref_cache(refs);\n \tint error = 0;\n \tint save_errno = 0;\n \tFILE *out;\n@@ -2281,15 +2292,17 @@ static int commit_packed_refs(void)\n  */\n static void rollback_packed_refs(void)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"rollback_packed_refs\");\n \tstruct packed_ref_cache *packed_ref_cache =\n-\t\tget_packed_ref_cache(&ref_store);\n+\t\tget_packed_ref_cache(refs);\n \n \tif (!packed_ref_cache->lock)\n \t\tdie(\"internal error: packed-refs not locked\");\n \trollback_lock_file(packed_ref_cache->lock);\n \tpacked_ref_cache->lock = NULL;\n \trelease_packed_ref_cache(packed_ref_cache);\n-\tclear_packed_ref_cache(&ref_store);\n+\tclear_packed_ref_cache(refs);\n }\n \n struct ref_to_prune {\n@@ -2422,15 +2435,17 @@ static void prune_refs(struct ref_to_prune *r)\n \n int pack_refs(unsigned int flags)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"pack_refs\");\n \tstruct pack_refs_cb_data cbdata;\n \n \tmemset(&cbdata, 0, sizeof(cbdata));\n \tcbdata.flags = flags;\n \n \tlock_packed_refs(LOCK_DIE_ON_ERROR);\n-\tcbdata.packed_refs = get_packed_refs(&ref_store);\n+\tcbdata.packed_refs = get_packed_refs(refs);\n \n-\tdo_for_each_entry_in_dir(get_loose_refs(&ref_store), 0,\n+\tdo_for_each_entry_in_dir(get_loose_refs(refs), 0,\n \t\t\t\t pack_if_possible_fn, &cbdata);\n \n \tif (commit_packed_refs())\n@@ -2449,6 +2464,8 @@ int pack_refs(unsigned int flags)\n  */\n static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"repack_without_refs\");\n \tstruct ref_dir *packed;\n \tstruct string_list_item *refname;\n \tint ret, needs_repacking = 0, removed = 0;\n@@ -2471,7 +2488,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n \t\tunable_to_lock_message(git_path(\"packed-refs\"), errno, err);\n \t\treturn -1;\n \t}\n-\tpacked = get_packed_refs(&ref_store);\n+\tpacked = get_packed_refs(refs);\n \n \t/* Remove refnames from the cache */\n \tfor_each_string_list_item(refname, refnames)\n@@ -2616,8 +2633,10 @@ int verify_refname_available(const char *newname,\n \t\t\t     const struct string_list *skip,\n \t\t\t     struct strbuf *err)\n {\n-\tstruct ref_dir *packed_refs = get_packed_refs(&ref_store);\n-\tstruct ref_dir *loose_refs = get_loose_refs(&ref_store);\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"verify_refname_available\");\n+\tstruct ref_dir *packed_refs = get_packed_refs(refs);\n+\tstruct ref_dir *loose_refs = get_loose_refs(refs);\n \n \tif (verify_refname_available_dir(newname, extras, skip,\n \t\t\t\t\t packed_refs, err) ||\n@@ -2968,7 +2987,10 @@ static int commit_ref_update(struct ref_lock *lock,\n \t\t\t     const unsigned char *sha1, const char *logmsg,\n \t\t\t     struct strbuf *err)\n {\n-\tclear_loose_ref_cache(&ref_store);\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"commit_ref_update\");\n+\n+\tclear_loose_ref_cache(refs);\n \tif (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {\n \t\tchar *old_msg = strbuf_detach(err, NULL);\n \t\tstrbuf_addf(err, \"cannot update the ref '%s': %s\",\n@@ -3683,6 +3705,8 @@ static int lock_ref_for_update(struct ref_update *update,\n int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t   struct strbuf *err)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"ref_transaction_commit\");\n \tint ret = 0, i;\n \tstruct ref_update **updates = transaction->updates;\n \tstruct string_list refs_to_delete = STRING_LIST_INIT_NODUP;\n@@ -3790,7 +3814,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t}\n \t\t}\n \t\tif (update->flags & REF_NEEDS_COMMIT) {\n-\t\t\tclear_loose_ref_cache(&ref_store);\n+\t\t\tclear_loose_ref_cache(refs);\n \t\t\tif (commit_ref(lock)) {\n \t\t\t\tstrbuf_addf(err, \"couldn't set '%s'\", lock->ref_name);\n \t\t\t\tunlock_ref(lock);\n@@ -3823,7 +3847,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t}\n \tfor_each_string_list_item(ref_to_delete, &refs_to_delete)\n \t\tunlink_or_warn(git_path(\"logs/%s\", ref_to_delete->string));\n-\tclear_loose_ref_cache(&ref_store);\n+\tclear_loose_ref_cache(refs);\n \n cleanup:\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n@@ -4069,5 +4093,6 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n \n struct ref_storage_be refs_be_files = {\n \tNULL,\n-\t\"files\"\n+\t\"files\",\n+\tfiles_ref_store_create\n };\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 4d88849..d6c74cf 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -521,11 +521,89 @@ int read_raw_ref(const char *refname, unsigned char *sha1,\n \t\t struct strbuf *referent, unsigned int *type);\n \n /* refs backends */\n+\n+/*\n+ * Initialize the ref_store for the specified submodule, or for the\n+ * main repository if submodule == NULL. These functions should call\n+ * base_ref_store_init() to initialize the shared part of the\n+ * ref_store and to record the ref_store for later lookup.\n+ */\n+typedef struct ref_store *ref_store_init_fn(const char *submodule);\n+\n struct ref_storage_be {\n \tstruct ref_storage_be *next;\n \tconst char *name;\n+\tref_store_init_fn *init;\n };\n \n extern struct ref_storage_be refs_be_files;\n \n+/*\n+ * A representation of the reference store for the main repository or\n+ * a submodule. The ref_store instances for submodules are kept in a\n+ * linked list.\n+ */\n+struct ref_store {\n+\t/* The backend describing this ref_store's storage scheme: */\n+\tconst struct ref_storage_be *be;\n+\n+\t/*\n+\t * The name of the submodule represented by this object, or\n+\t * the empty string if it represents the main repository's\n+\t * reference store:\n+\t */\n+\tconst char *submodule;\n+\n+\t/*\n+\t * Submodule reference store instances are stored in a linked\n+\t * list using this pointer.\n+\t */\n+\tstruct ref_store *next;\n+};\n+\n+/*\n+ * Fill in the generic part of refs for the specified submodule and\n+ * add it to our collection of reference stores.\n+ */\n+void base_ref_store_init(struct ref_store *refs,\n+\t\t\t const struct ref_storage_be *be,\n+\t\t\t const char *submodule);\n+\n+/*\n+ * Create, record, and return a ref_store instance for the specified\n+ * submodule (or the main repository if submodule is NULL).\n+ *\n+ * For backwards compatibility, submodule==\"\" is treated the same as\n+ * submodule==NULL.\n+ */\n+struct ref_store *ref_store_init(const char *submodule);\n+\n+/*\n+ * Return the ref_store instance for the specified submodule (or the\n+ * main repository if submodule is NULL). If that ref_store hasn't\n+ * been initialized yet, return NULL.\n+ *\n+ * For backwards compatibility, submodule==\"\" is treated the same as\n+ * submodule==NULL.\n+ */\n+struct ref_store *lookup_ref_store(const char *submodule);\n+\n+/*\n+ * Return the ref_store instance for the specified submodule. For the\n+ * main repository, use submodule==NULL; such a call cannot fail. For\n+ * a submodule, the submodule must exist and be a nonbare repository,\n+ * otherwise return NULL. If the requested reference store has not yet\n+ * been initialized, initialize it first.\n+ *\n+ * For backwards compatibility, submodule==\"\" is treated the same as\n+ * submodule==NULL.\n+ */\n+struct ref_store *get_ref_store(const char *submodule);\n+\n+/*\n+ * Die if refs is for a submodule (i.e., not for the main repository).\n+ * caller is used in any necessary error messages.\n+ */\n+void assert_main_repository(struct ref_store *refs, const char *caller);\n+\n #endif /* REFS_REFS_INTERNAL_H */\n-- \n2.8.1\n"},{"id":"288320","messageId":"9f629d0d60f77414961b966ddbd00eff32014037.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 06/38] add_packed_ref(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:41Z","receivedAt":"2016-06-03T21:03:41Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 9 +++++----\n 1 file changed, 5 insertions(+), 4 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 8ef79c2..5bfa912 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1196,10 +1196,9 @@ static struct ref_dir *get_packed_refs(struct files_ref_store *refs)\n  * lock_packed_refs()).  To actually write the packed-refs file, call\n  * commit_packed_refs().\n  */\n-static void add_packed_ref(const char *refname, const unsigned char *sha1)\n+static void add_packed_ref(struct files_ref_store *refs,\n+\t\t\t   const char *refname, const unsigned char *sha1)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"add_packed_ref\");\n \tstruct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);\n \n \tif (!packed_ref_cache->lock)\n@@ -3873,6 +3872,8 @@ static int ref_present(const char *refname,\n int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t\t   struct strbuf *err)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"initial_ref_transaction_commit\");\n \tint ret = 0, i;\n \tstruct ref_update **updates = transaction->updates;\n \tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n@@ -3932,7 +3933,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \n \t\tif ((update->flags & REF_HAVE_NEW) &&\n \t\t    !is_null_sha1(update->new_sha1))\n-\t\t\tadd_packed_ref(update->refname, update->new_sha1);\n+\t\t\tadd_packed_ref(refs, update->refname, update->new_sha1);\n \t}\n \n \tif (commit_packed_refs()) {\n-- \n2.8.1\n"},{"id":"288325","messageId":"bf2601a0a5c940b5c9f81f917c1935d3d3144c93.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 07/38] get_packed_ref(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:42Z","receivedAt":"2016-06-03T21:03:42Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 16 +++++++++-------\n 1 file changed, 9 insertions(+), 7 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 5bfa912..57f1965 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1407,11 +1407,9 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n  * Return the ref_entry for the given refname from the packed\n  * references.  If it does not exist, return NULL.\n  */\n-static struct ref_entry *get_packed_ref(const char *refname)\n+static struct ref_entry *get_packed_ref(struct files_ref_store *refs,\n+\t\t\t\t\tconst char *refname)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"get_packed_ref\");\n-\n \treturn find_ref(get_packed_refs(refs), refname);\n }\n \n@@ -1422,13 +1420,16 @@ static int resolve_missing_loose_ref(const char *refname,\n \t\t\t\t     unsigned char *sha1,\n \t\t\t\t     unsigned int *flags)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"resolve_missing_loose_ref\");\n+\n \tstruct ref_entry *entry;\n \n \t/*\n \t * The loose reference file does not exist; check for a packed\n \t * reference.\n \t */\n-\tentry = get_packed_ref(refname);\n+\tentry = get_packed_ref(refs, refname);\n \tif (entry) {\n \t\thashcpy(sha1, entry->u.value.oid.hash);\n \t\t*flags |= REF_ISPACKED;\n@@ -1840,6 +1841,7 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)\n \n int peel_ref(const char *refname, unsigned char *sha1)\n {\n+\tstruct files_ref_store *refs = get_files_ref_store(NULL, \"peel_ref\");\n \tint flag;\n \tunsigned char base[20];\n \n@@ -1864,7 +1866,7 @@ int peel_ref(const char *refname, unsigned char *sha1)\n \t * have REF_KNOWS_PEELED.\n \t */\n \tif (flag & REF_ISPACKED) {\n-\t\tstruct ref_entry *r = get_packed_ref(refname);\n+\t\tstruct ref_entry *r = get_packed_ref(refs, refname);\n \t\tif (r) {\n \t\t\tif (peel_entry(r, 0))\n \t\t\t\treturn -1;\n@@ -2473,7 +2475,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n \n \t/* Look for a packed ref */\n \tfor_each_string_list_item(refname, refnames) {\n-\t\tif (get_packed_ref(refname->string)) {\n+\t\tif (get_packed_ref(refs, refname->string)) {\n \t\t\tneeds_repacking = 1;\n \t\t\tbreak;\n \t\t}\n-- \n2.8.1\n"},{"id":"288318","messageId":"6caa9cba0e57dd8fd84e2c37b7ad4e5fcd83098c.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 08/38] resolve_missing_loose_ref(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:43Z","receivedAt":"2016-06-03T21:03:43Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 12 ++++++------\n 1 file changed, 6 insertions(+), 6 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 57f1965..9307fa0 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1416,13 +1416,11 @@ static struct ref_entry *get_packed_ref(struct files_ref_store *refs,\n /*\n  * A loose ref file doesn't exist; check for a packed ref.\n  */\n-static int resolve_missing_loose_ref(const char *refname,\n+static int resolve_missing_loose_ref(struct files_ref_store *refs,\n+\t\t\t\t     const char *refname,\n \t\t\t\t     unsigned char *sha1,\n \t\t\t\t     unsigned int *flags)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"resolve_missing_loose_ref\");\n-\n \tstruct ref_entry *entry;\n \n \t/*\n@@ -1442,6 +1440,8 @@ static int resolve_missing_loose_ref(const char *refname,\n int read_raw_ref(const char *refname, unsigned char *sha1,\n \t\t struct strbuf *referent, unsigned int *type)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"read_raw_ref\");\n \tstruct strbuf sb_contents = STRBUF_INIT;\n \tstruct strbuf sb_path = STRBUF_INIT;\n \tconst char *path;\n@@ -1470,7 +1470,7 @@ stat_ref:\n \tif (lstat(path, &st) < 0) {\n \t\tif (errno != ENOENT)\n \t\t\tgoto out;\n-\t\tif (resolve_missing_loose_ref(refname, sha1, type)) {\n+\t\tif (resolve_missing_loose_ref(refs, refname, sha1, type)) {\n \t\t\terrno = ENOENT;\n \t\t\tgoto out;\n \t\t}\n@@ -1504,7 +1504,7 @@ stat_ref:\n \t\t * ref is supposed to be, there could still be a\n \t\t * packed ref:\n \t\t */\n-\t\tif (resolve_missing_loose_ref(refname, sha1, type)) {\n+\t\tif (resolve_missing_loose_ref(refs, refname, sha1, type)) {\n \t\t\terrno = EISDIR;\n \t\t\tgoto out;\n \t\t}\n-- \n2.8.1\n"},{"id":"288302","messageId":"54d13545f73cf6a00a2e3a5f89c3e4568b5c836b.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 09/38] {lock,commit,rollback}_packed_refs(): add files_ref_store arguments","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:44Z","receivedAt":"2016-06-03T21:03:44Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"These functions currently only work in the main repository, so add an\nassert_main_repository() check to each function.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 32 ++++++++++++++++----------------\n 1 file changed, 16 insertions(+), 16 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 9307fa0..0d64a3d 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2219,14 +2219,14 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)\n  * hold_lock_file_for_update(). Return 0 on success. On errors, set\n  * errno appropriately and return a nonzero value.\n  */\n-static int lock_packed_refs(int flags)\n+static int lock_packed_refs(struct files_ref_store *refs, int flags)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"lock_packed_refs\");\n \tstatic int timeout_configured = 0;\n \tstatic int timeout_value = 1000;\n \tstruct packed_ref_cache *packed_ref_cache;\n \n+\tassert_main_repository(&refs->base, \"lock_packed_refs\");\n+\n \tif (!timeout_configured) {\n \t\tgit_config_get_int(\"core.packedrefstimeout\", &timeout_value);\n \t\ttimeout_configured = 1;\n@@ -2255,16 +2255,16 @@ static int lock_packed_refs(int flags)\n  * lock_packed_refs()). Return zero on success. On errors, set errno\n  * and return a nonzero value\n  */\n-static int commit_packed_refs(void)\n+static int commit_packed_refs(struct files_ref_store *refs)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"commit_packed_refs\");\n \tstruct packed_ref_cache *packed_ref_cache =\n \t\tget_packed_ref_cache(refs);\n \tint error = 0;\n \tint save_errno = 0;\n \tFILE *out;\n \n+\tassert_main_repository(&refs->base, \"commit_packed_refs\");\n+\n \tif (!packed_ref_cache->lock)\n \t\tdie(\"internal error: packed-refs not locked\");\n \n@@ -2291,13 +2291,13 @@ static int commit_packed_refs(void)\n  * in-memory packed reference cache.  (The packed-refs file will be\n  * read anew if it is needed again after this function is called.)\n  */\n-static void rollback_packed_refs(void)\n+static void rollback_packed_refs(struct files_ref_store *refs)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"rollback_packed_refs\");\n \tstruct packed_ref_cache *packed_ref_cache =\n \t\tget_packed_ref_cache(refs);\n \n+\tassert_main_repository(&refs->base, \"rollback_packed_refs\");\n+\n \tif (!packed_ref_cache->lock)\n \t\tdie(\"internal error: packed-refs not locked\");\n \trollback_lock_file(packed_ref_cache->lock);\n@@ -2443,13 +2443,13 @@ int pack_refs(unsigned int flags)\n \tmemset(&cbdata, 0, sizeof(cbdata));\n \tcbdata.flags = flags;\n \n-\tlock_packed_refs(LOCK_DIE_ON_ERROR);\n+\tlock_packed_refs(refs, LOCK_DIE_ON_ERROR);\n \tcbdata.packed_refs = get_packed_refs(refs);\n \n \tdo_for_each_entry_in_dir(get_loose_refs(refs), 0,\n \t\t\t\t pack_if_possible_fn, &cbdata);\n \n-\tif (commit_packed_refs())\n+\tif (commit_packed_refs(refs))\n \t\tdie_errno(\"unable to overwrite old ref-pack file\");\n \n \tprune_refs(cbdata.ref_to_prune);\n@@ -2485,7 +2485,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n \tif (!needs_repacking)\n \t\treturn 0; /* no refname exists in packed refs */\n \n-\tif (lock_packed_refs(0)) {\n+\tif (lock_packed_refs(refs, 0)) {\n \t\tunable_to_lock_message(git_path(\"packed-refs\"), errno, err);\n \t\treturn -1;\n \t}\n@@ -2500,12 +2500,12 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n \t\t * All packed entries disappeared while we were\n \t\t * acquiring the lock.\n \t\t */\n-\t\trollback_packed_refs();\n+\t\trollback_packed_refs(refs);\n \t\treturn 0;\n \t}\n \n \t/* Write what remains */\n-\tret = commit_packed_refs();\n+\tret = commit_packed_refs(refs);\n \tif (ret)\n \t\tstrbuf_addf(err, \"unable to overwrite old ref-pack file: %s\",\n \t\t\t    strerror(errno));\n@@ -3923,7 +3923,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \t\t}\n \t}\n \n-\tif (lock_packed_refs(0)) {\n+\tif (lock_packed_refs(refs, 0)) {\n \t\tstrbuf_addf(err, \"unable to lock packed-refs file: %s\",\n \t\t\t    strerror(errno));\n \t\tret = TRANSACTION_GENERIC_ERROR;\n@@ -3938,7 +3938,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\tadd_packed_ref(refs, update->refname, update->new_sha1);\n \t}\n \n-\tif (commit_packed_refs()) {\n+\tif (commit_packed_refs(refs)) {\n \t\tstrbuf_addf(err, \"unable to commit packed-refs file: %s\",\n \t\t\t    strerror(errno));\n \t\tret = TRANSACTION_GENERIC_ERROR;\n-- \n2.8.1\n"},{"id":"288298","messageId":"5ae6c74b12f7dbbaf6bf72c2a0deec7e6c6167f4.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 10/38] refs: add a transaction_commit() method","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:45Z","receivedAt":"2016-06-03T21:03:45Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: Ronnie Sahlberg <sahlberg@google.com>\n\nSigned-off-by: Ronnie Sahlberg <sahlberg@google.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Jeff King <peff@peff.net>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               |  9 +++++++++\n refs/files-backend.c | 10 ++++++----\n refs/refs-internal.h |  5 +++++\n 3 files changed, 20 insertions(+), 4 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 5e20ae0..904e758 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1375,3 +1375,12 @@ void assert_main_repository(struct ref_store *refs, const char *caller)\n \tif (*refs->submodule)\n \t\tdie(\"BUG: %s called for a submodule\", caller);\n }\n+\n+/* backend functions */\n+int ref_transaction_commit(struct ref_transaction *transaction,\n+\t\t\t   struct strbuf *err)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->transaction_commit(refs, transaction, err);\n+}\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 0d64a3d..fea4487 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3703,11 +3703,12 @@ static int lock_ref_for_update(struct ref_update *update,\n \treturn 0;\n }\n \n-int ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t   struct strbuf *err)\n+static int files_transaction_commit(struct ref_store *ref_store,\n+\t\t\t\t    struct ref_transaction *transaction,\n+\t\t\t\t    struct strbuf *err)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"ref_transaction_commit\");\n+\t\tfiles_downcast(ref_store, 0, \"ref_transaction_commit\");\n \tint ret = 0, i;\n \tstruct ref_update **updates = transaction->updates;\n \tstruct string_list refs_to_delete = STRING_LIST_INIT_NODUP;\n@@ -4097,5 +4098,6 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n struct ref_storage_be refs_be_files = {\n \tNULL,\n \t\"files\",\n-\tfiles_ref_store_create\n+\tfiles_ref_store_create,\n+\tfiles_transaction_commit\n };\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex d6c74cf..e24366b 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -530,10 +530,15 @@ int read_raw_ref(const char *refname, unsigned char *sha1,\n  */\n typedef struct ref_store *ref_store_init_fn(const char *submodule);\n \n+typedef int ref_transaction_commit_fn(struct ref_store *refs,\n+\t\t\t\t      struct ref_transaction *transaction,\n+\t\t\t\t      struct strbuf *err);\n+\n struct ref_storage_be {\n \tstruct ref_storage_be *next;\n \tconst char *name;\n \tref_store_init_fn *init;\n+\tref_transaction_commit_fn *transaction_commit;\n };\n \n extern struct ref_storage_be refs_be_files;\n-- \n2.8.1\n"},{"id":"288297","messageId":"b67dc4918007a8b556ff12fed9e712535951ac8f.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 11/38] refs: reorder definitions","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:46Z","receivedAt":"2016-06-03T21:03:46Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Move resolve_gitlink_ref() and related functions lower in the file to\navoid the need for forward declarations in the next step.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 166 +++++++++++++++++++++++++--------------------------\n 1 file changed, 83 insertions(+), 83 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex fea4487..50bfe97 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1320,89 +1320,6 @@ static struct ref_dir *get_loose_refs(struct files_ref_store *refs)\n \treturn get_ref_dir(refs->loose);\n }\n \n-#define MAXREFLEN (1024)\n-\n-/*\n- * Called by resolve_gitlink_ref_recursive() after it failed to read\n- * from the loose refs in refs. Find <refname> in the packed-refs file\n- * for the submodule.\n- */\n-static int resolve_gitlink_packed_ref(struct files_ref_store *refs,\n-\t\t\t\t      const char *refname, unsigned char *sha1)\n-{\n-\tstruct ref_entry *ref;\n-\tstruct ref_dir *dir = get_packed_refs(refs);\n-\n-\tref = find_ref(dir, refname);\n-\tif (ref == NULL)\n-\t\treturn -1;\n-\n-\thashcpy(sha1, ref->u.value.oid.hash);\n-\treturn 0;\n-}\n-\n-static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n-\t\t\t\t\t const char *refname, unsigned char *sha1,\n-\t\t\t\t\t int recursion)\n-{\n-\tint fd, len;\n-\tchar buffer[128], *p;\n-\tchar *path;\n-\n-\tif (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)\n-\t\treturn -1;\n-\tpath = *refs->base.submodule\n-\t\t? git_pathdup_submodule(refs->base.submodule, \"%s\", refname)\n-\t\t: git_pathdup(\"%s\", refname);\n-\tfd = open(path, O_RDONLY);\n-\tfree(path);\n-\tif (fd < 0)\n-\t\treturn resolve_gitlink_packed_ref(refs, refname, sha1);\n-\n-\tlen = read(fd, buffer, sizeof(buffer)-1);\n-\tclose(fd);\n-\tif (len < 0)\n-\t\treturn -1;\n-\twhile (len && isspace(buffer[len-1]))\n-\t\tlen--;\n-\tbuffer[len] = 0;\n-\n-\t/* Was it a detached head or an old-fashioned symlink? */\n-\tif (!get_sha1_hex(buffer, sha1))\n-\t\treturn 0;\n-\n-\t/* Symref? */\n-\tif (strncmp(buffer, \"ref:\", 4))\n-\t\treturn -1;\n-\tp = buffer + 4;\n-\twhile (isspace(*p))\n-\t\tp++;\n-\n-\treturn resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);\n-}\n-\n-int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n-{\n-\tint len = strlen(path);\n-\tstruct strbuf submodule = STRBUF_INIT;\n-\tstruct files_ref_store *refs;\n-\n-\twhile (len && path[len-1] == '/')\n-\t\tlen--;\n-\tif (!len)\n-\t\treturn -1;\n-\n-\tstrbuf_add(&submodule, path, len);\n-\trefs = get_files_ref_store(submodule.buf, \"resolve_gitlink_ref\");\n-\tif (!refs) {\n-\t\tstrbuf_release(&submodule);\n-\t\treturn -1;\n-\t}\n-\tstrbuf_release(&submodule);\n-\n-\treturn resolve_gitlink_ref_recursive(refs, refname, sha1, 0);\n-}\n-\n /*\n  * Return the ref_entry for the given refname from the packed\n  * references.  If it does not exist, return NULL.\n@@ -1576,6 +1493,89 @@ static void unlock_ref(struct ref_lock *lock)\n \tfree(lock);\n }\n \n+#define MAXREFLEN (1024)\n+\n+/*\n+ * Called by resolve_gitlink_ref_recursive() after it failed to read\n+ * from the loose refs in refs. Find <refname> in the packed-refs file\n+ * for the submodule.\n+ */\n+static int resolve_gitlink_packed_ref(struct files_ref_store *refs,\n+\t\t\t\t      const char *refname, unsigned char *sha1)\n+{\n+\tstruct ref_entry *ref;\n+\tstruct ref_dir *dir = get_packed_refs(refs);\n+\n+\tref = find_ref(dir, refname);\n+\tif (ref == NULL)\n+\t\treturn -1;\n+\n+\thashcpy(sha1, ref->u.value.oid.hash);\n+\treturn 0;\n+}\n+\n+static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n+\t\t\t\t\t const char *refname, unsigned char *sha1,\n+\t\t\t\t\t int recursion)\n+{\n+\tint fd, len;\n+\tchar buffer[128], *p;\n+\tchar *path;\n+\n+\tif (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)\n+\t\treturn -1;\n+\tpath = *refs->base.submodule\n+\t\t? git_pathdup_submodule(refs->base.submodule, \"%s\", refname)\n+\t\t: git_pathdup(\"%s\", refname);\n+\tfd = open(path, O_RDONLY);\n+\tfree(path);\n+\tif (fd < 0)\n+\t\treturn resolve_gitlink_packed_ref(refs, refname, sha1);\n+\n+\tlen = read(fd, buffer, sizeof(buffer)-1);\n+\tclose(fd);\n+\tif (len < 0)\n+\t\treturn -1;\n+\twhile (len && isspace(buffer[len-1]))\n+\t\tlen--;\n+\tbuffer[len] = 0;\n+\n+\t/* Was it a detached head or an old-fashioned symlink? */\n+\tif (!get_sha1_hex(buffer, sha1))\n+\t\treturn 0;\n+\n+\t/* Symref? */\n+\tif (strncmp(buffer, \"ref:\", 4))\n+\t\treturn -1;\n+\tp = buffer + 4;\n+\twhile (isspace(*p))\n+\t\tp++;\n+\n+\treturn resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);\n+}\n+\n+int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n+{\n+\tint len = strlen(path);\n+\tstruct strbuf submodule = STRBUF_INIT;\n+\tstruct files_ref_store *refs;\n+\n+\twhile (len && path[len-1] == '/')\n+\t\tlen--;\n+\tif (!len)\n+\t\treturn -1;\n+\n+\tstrbuf_add(&submodule, path, len);\n+\trefs = get_files_ref_store(submodule.buf, \"resolve_gitlink_ref\");\n+\tif (!refs) {\n+\t\tstrbuf_release(&submodule);\n+\t\treturn -1;\n+\t}\n+\tstrbuf_release(&submodule);\n+\n+\treturn resolve_gitlink_ref_recursive(refs, refname, sha1, 0);\n+}\n+\n /*\n  * Lock refname, without following symrefs, and set *lock_p to point\n  * at a newly-allocated lock object. Fill in lock->old_oid, referent,\n-- \n2.8.1\n"},{"id":"288300","messageId":"977a81b2d2ac82516bbbd8494052ccaaed5d495a.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 12/38] resolve_packed_ref(): rename function from resolve_missing_loose_ref()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:47Z","receivedAt":"2016-06-03T21:03:47Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 11 +++++------\n 1 file changed, 5 insertions(+), 6 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 50bfe97..b54355d 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1333,10 +1333,9 @@ static struct ref_entry *get_packed_ref(struct files_ref_store *refs,\n /*\n  * A loose ref file doesn't exist; check for a packed ref.\n  */\n-static int resolve_missing_loose_ref(struct files_ref_store *refs,\n-\t\t\t\t     const char *refname,\n-\t\t\t\t     unsigned char *sha1,\n-\t\t\t\t     unsigned int *flags)\n+static int resolve_packed_ref(struct files_ref_store *refs,\n+\t\t\t      const char *refname,\n+\t\t\t      unsigned char *sha1, unsigned int *flags)\n {\n \tstruct ref_entry *entry;\n \n@@ -1387,7 +1386,7 @@ stat_ref:\n \tif (lstat(path, &st) < 0) {\n \t\tif (errno != ENOENT)\n \t\t\tgoto out;\n-\t\tif (resolve_missing_loose_ref(refs, refname, sha1, type)) {\n+\t\tif (resolve_packed_ref(refs, refname, sha1, type)) {\n \t\t\terrno = ENOENT;\n \t\t\tgoto out;\n \t\t}\n@@ -1421,7 +1420,7 @@ stat_ref:\n \t\t * ref is supposed to be, there could still be a\n \t\t * packed ref:\n \t\t */\n-\t\tif (resolve_missing_loose_ref(refs, refname, sha1, type)) {\n+\t\tif (resolve_packed_ref(refs, refname, sha1, type)) {\n \t\t\terrno = EISDIR;\n \t\t\tgoto out;\n \t\t}\n-- \n2.8.1\n"},{"id":"288307","messageId":"ebfdfb0bb15f16418f9155c529a486feacc776c4.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 13/38] resolve_gitlink_packed_ref(): remove function","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:48Z","receivedAt":"2016-06-03T21:03:48Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Now that resolve_packed_ref() can work with an arbitrary\nfiles_ref_store, there is no need to have a separate\nresolve_gitlink_packed_ref() function.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 26 +++++---------------------\n 1 file changed, 5 insertions(+), 21 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex b54355d..5f14227 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1494,25 +1494,6 @@ static void unlock_ref(struct ref_lock *lock)\n \n #define MAXREFLEN (1024)\n \n-/*\n- * Called by resolve_gitlink_ref_recursive() after it failed to read\n- * from the loose refs in refs. Find <refname> in the packed-refs file\n- * for the submodule.\n- */\n-static int resolve_gitlink_packed_ref(struct files_ref_store *refs,\n-\t\t\t\t      const char *refname, unsigned char *sha1)\n-{\n-\tstruct ref_entry *ref;\n-\tstruct ref_dir *dir = get_packed_refs(refs);\n-\n-\tref = find_ref(dir, refname);\n-\tif (ref == NULL)\n-\t\treturn -1;\n-\n-\thashcpy(sha1, ref->u.value.oid.hash);\n-\treturn 0;\n-}\n-\n static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n \t\t\t\t\t const char *refname, unsigned char *sha1,\n \t\t\t\t\t int recursion)\n@@ -1528,8 +1509,11 @@ static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n \t\t: git_pathdup(\"%s\", refname);\n \tfd = open(path, O_RDONLY);\n \tfree(path);\n-\tif (fd < 0)\n-\t\treturn resolve_gitlink_packed_ref(refs, refname, sha1);\n+\tif (fd < 0) {\n+\t\tunsigned int flags;\n+\n+\t\treturn resolve_packed_ref(refs, refname, sha1, &flags);\n+\t}\n \n \tlen = read(fd, buffer, sizeof(buffer)-1);\n \tclose(fd);\n-- \n2.8.1\n"},{"id":"288299","messageId":"14f4d208f5fde3d758a65f388749b01b79698cce.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 14/38] read_raw_ref(): take a (struct ref_store *) argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:49Z","receivedAt":"2016-06-03T21:03:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"And make the function work for submodules.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               |  4 +++-\n refs/files-backend.c | 18 +++++++++++++-----\n refs/refs-internal.h |  9 ++++++---\n 3 files changed, 22 insertions(+), 9 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 904e758..9d36dfe 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1222,6 +1222,7 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \tstatic struct strbuf sb_refname = STRBUF_INIT;\n \tint unused_flags;\n \tint symref_count;\n+\tstruct ref_store *refs = get_ref_store(NULL);\n \n \tif (!flags)\n \t\tflags = &unused_flags;\n@@ -1249,7 +1250,8 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \tfor (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {\n \t\tunsigned int read_flags = 0;\n \n-\t\tif (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {\n+\t\tif (read_raw_ref(refs, refname,\n+\t\t\t\t sha1, &sb_refname, &read_flags)) {\n \t\t\t*flags |= read_flags;\n \t\t\tif (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))\n \t\t\t\treturn NULL;\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 5f14227..ff7ee7d 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1353,11 +1353,12 @@ static int resolve_packed_ref(struct files_ref_store *refs,\n \treturn -1;\n }\n \n-int read_raw_ref(const char *refname, unsigned char *sha1,\n+int read_raw_ref(struct ref_store *ref_store,\n+\t\t const char *refname, unsigned char *sha1,\n \t\t struct strbuf *referent, unsigned int *type)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"read_raw_ref\");\n+\t\tfiles_downcast(ref_store, 1, \"read_raw_ref\");\n \tstruct strbuf sb_contents = STRBUF_INIT;\n \tstruct strbuf sb_path = STRBUF_INIT;\n \tconst char *path;\n@@ -1369,7 +1370,12 @@ int read_raw_ref(const char *refname, unsigned char *sha1,\n \n \t*type = 0;\n \tstrbuf_reset(&sb_path);\n-\tstrbuf_git_path(&sb_path, \"%s\", refname);\n+\n+\tif (*refs->base.submodule)\n+\t\tstrbuf_git_path_submodule(&sb_path, refs->base.submodule, \"%s\", refname);\n+\telse\n+\t\tstrbuf_git_path(&sb_path, \"%s\", refname);\n+\n \tpath = sb_path.buf;\n \n stat_ref:\n@@ -1596,8 +1602,9 @@ static int lock_raw_ref(const char *refname, int mustexist,\n \t\t\tunsigned int *type,\n \t\t\tstruct strbuf *err)\n {\n+\tstruct ref_store *ref_store = get_ref_store(NULL);\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"lock_raw_ref\");\n+\t\tfiles_downcast(ref_store, 0, \"lock_raw_ref\");\n \tstruct ref_lock *lock;\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tint attempts_remaining = 3;\n@@ -1687,7 +1694,8 @@ retry:\n \t * fear that its value will change.\n \t */\n \n-\tif (read_raw_ref(refname, lock->old_oid.hash, referent, type)) {\n+\tif (read_raw_ref(ref_store, refname,\n+\t\t\t lock->old_oid.hash, referent, type)) {\n \t\tif (errno == ENOENT) {\n \t\t\tif (mustexist) {\n \t\t\t\t/* Garden variety missing reference. */\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex e24366b..8c45ee3 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -479,9 +479,11 @@ extern struct ref_iterator *current_ref_iter;\n int do_for_each_ref_iterator(struct ref_iterator *iter,\n \t\t\t     each_ref_fn fn, void *cb_data);\n \n+struct ref_store;\n+\n /*\n- * Read the specified reference from the filesystem or packed refs\n- * file, non-recursively. Set type to describe the reference, and:\n+ * Read a reference from the specified reference store, non-recursively.\n+ * Set type to describe the reference, and:\n  *\n  * - If refname is the name of a normal reference, fill in sha1\n  *   (leaving referent unchanged).\n@@ -517,7 +519,8 @@ int do_for_each_ref_iterator(struct ref_iterator *iter,\n  * - in all other cases, referent will be untouched, and therefore\n  *   refname will still be valid and unchanged.\n  */\n-int read_raw_ref(const char *refname, unsigned char *sha1,\n+int read_raw_ref(struct ref_store *ref_store,\n+\t\t const char *refname, unsigned char *sha1,\n \t\t struct strbuf *referent, unsigned int *type);\n \n /* refs backends */\n-- \n2.8.1\n"},{"id":"288296","messageId":"b4f93e572ad6567fd57841c18778c608a6eeaa7a.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 15/38] resolve_ref_recursively(): new function","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:50Z","receivedAt":"2016-06-03T21:03:50Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Add a new function, resolve_ref_recursively(), which is basically like\nthe old resolve_ref_unsafe() except that it takes a (ref_store *)\nargument and also works for submodules.\n\nRe-implement resolve_ref_unsafe() as a thin wrapper around\nresolve_ref_recursively().\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 14 +++++++++++---\n 1 file changed, 11 insertions(+), 3 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 9d36dfe..93f2e4d 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1216,13 +1216,14 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)\n }\n \n /* This function needs to return a meaningful errno on failure */\n-const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n-\t\t\t       unsigned char *sha1, int *flags)\n+static const char *resolve_ref_recursively(struct ref_store *refs,\n+\t\t\t\t\t   const char *refname,\n+\t\t\t\t\t   int resolve_flags,\n+\t\t\t\t\t   unsigned char *sha1, int *flags)\n {\n \tstatic struct strbuf sb_refname = STRBUF_INIT;\n \tint unused_flags;\n \tint symref_count;\n-\tstruct ref_store *refs = get_ref_store(NULL);\n \n \tif (!flags)\n \t\tflags = &unused_flags;\n@@ -1291,6 +1292,13 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \treturn NULL;\n }\n \n+const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n+\t\t\t       unsigned char *sha1, int *flags)\n+{\n+\treturn resolve_ref_recursively(get_ref_store(NULL), refname,\n+\t\t\t\t       resolve_flags, sha1, flags);\n+}\n+\n static struct ref_store *main_ref_store = NULL;\n \n static struct ref_store *submodule_ref_stores = NULL;\n-- \n2.8.1\n"},{"id":"288304","messageId":"a9787723bcd990413661b8cb2e5542d618ab6321.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 16/38] resolve_gitlink_ref(): implement using resolve_ref_recursively()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:51Z","receivedAt":"2016-06-03T21:03:51Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"resolve_ref_recursively() can handle references in arbitrary files\nreference stores, so use it to resolve \"gitlink\" (i.e., submodule)\nreferences. Aside from removing redundant code, this allows submodule\nlookups to benefit from the much more robust code that we use for\nreading non-submodule references. And, since the code is now agnostic\nabout reference backends, it will work for any future references\nbackend (so move its definition to refs.c).\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 24 +++++++++++++++++++\n refs/files-backend.c | 67 ----------------------------------------------------\n 2 files changed, 24 insertions(+), 67 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 93f2e4d..5e0777a 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1299,6 +1299,30 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \t\t\t\t       resolve_flags, sha1, flags);\n }\n \n+int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n+{\n+\tint len = strlen(path);\n+\tstruct strbuf submodule = STRBUF_INIT;\n+\tstruct ref_store *refs;\n+\tint flags;\n+\n+\twhile (len && path[len-1] == '/')\n+\t\tlen--;\n+\tif (!len)\n+\t\treturn -1;\n+\n+\tstrbuf_add(&submodule, path, len);\n+\trefs = get_ref_store(submodule.buf);\n+\tstrbuf_release(&submodule);\n+\tif (!refs)\n+\t\treturn -1;\n+\n+\tif (!resolve_ref_recursively(refs, refname, 0, sha1, &flags) ||\n+\t    is_null_sha1(sha1))\n+\t\treturn -1;\n+\treturn 0;\n+}\n+\n static struct ref_store *main_ref_store = NULL;\n \n static struct ref_store *submodule_ref_stores = NULL;\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex ff7ee7d..d6d83d2 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1498,73 +1498,6 @@ static void unlock_ref(struct ref_lock *lock)\n \tfree(lock);\n }\n \n-#define MAXREFLEN (1024)\n-\n-static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n-\t\t\t\t\t const char *refname, unsigned char *sha1,\n-\t\t\t\t\t int recursion)\n-{\n-\tint fd, len;\n-\tchar buffer[128], *p;\n-\tchar *path;\n-\n-\tif (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)\n-\t\treturn -1;\n-\tpath = *refs->base.submodule\n-\t\t? git_pathdup_submodule(refs->base.submodule, \"%s\", refname)\n-\t\t: git_pathdup(\"%s\", refname);\n-\tfd = open(path, O_RDONLY);\n-\tfree(path);\n-\tif (fd < 0) {\n-\t\tunsigned int flags;\n-\n-\t\treturn resolve_packed_ref(refs, refname, sha1, &flags);\n-\t}\n-\n-\tlen = read(fd, buffer, sizeof(buffer)-1);\n-\tclose(fd);\n-\tif (len < 0)\n-\t\treturn -1;\n-\twhile (len && isspace(buffer[len-1]))\n-\t\tlen--;\n-\tbuffer[len] = 0;\n-\n-\t/* Was it a detached head or an old-fashioned symlink? */\n-\tif (!get_sha1_hex(buffer, sha1))\n-\t\treturn 0;\n-\n-\t/* Symref? */\n-\tif (strncmp(buffer, \"ref:\", 4))\n-\t\treturn -1;\n-\tp = buffer + 4;\n-\twhile (isspace(*p))\n-\t\tp++;\n-\n-\treturn resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);\n-}\n-\n-int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n-{\n-\tint len = strlen(path);\n-\tstruct strbuf submodule = STRBUF_INIT;\n-\tstruct files_ref_store *refs;\n-\n-\twhile (len && path[len-1] == '/')\n-\t\tlen--;\n-\tif (!len)\n-\t\treturn -1;\n-\n-\tstrbuf_add(&submodule, path, len);\n-\trefs = get_files_ref_store(submodule.buf, \"resolve_gitlink_ref\");\n-\tif (!refs) {\n-\t\tstrbuf_release(&submodule);\n-\t\treturn -1;\n-\t}\n-\tstrbuf_release(&submodule);\n-\n-\treturn resolve_gitlink_ref_recursive(refs, refname, sha1, 0);\n-}\n-\n /*\n  * Lock refname, without following symrefs, and set *lock_p to point\n  * at a newly-allocated lock object. Fill in lock->old_oid, referent,\n-- \n2.8.1\n"},{"id":"288301","messageId":"c126a2bf5e1f48faf48b3b4ee7cc599313a76b99.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 17/38] resolve_gitlink_ref(): avoid memory allocation in many cases","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:52Z","receivedAt":"2016-06-03T21:03:52Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"If we don't have to strip trailing '/' from the submodule path, then\ndon't allocate and copy the submodule name.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 20 +++++++++++++-------\n 1 file changed, 13 insertions(+), 7 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 5e0777a..7a8ef6d 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1301,19 +1301,25 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \n int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n {\n-\tint len = strlen(path);\n-\tstruct strbuf submodule = STRBUF_INIT;\n+\tsize_t len, orig_len = strlen(path);\n \tstruct ref_store *refs;\n \tint flags;\n \n-\twhile (len && path[len-1] == '/')\n-\t\tlen--;\n+\tfor (len = orig_len; len && path[len - 1] == '/'; len--)\n+\t\t;\n+\n \tif (!len)\n \t\treturn -1;\n \n-\tstrbuf_add(&submodule, path, len);\n-\trefs = get_ref_store(submodule.buf);\n-\tstrbuf_release(&submodule);\n+\tif (len == orig_len) {\n+\t\trefs = get_ref_store(path);\n+\t} else {\n+\t\tchar *stripped = xmemdupz(path, len);\n+\n+\t\trefs = get_ref_store(stripped);\n+\t\tfree(stripped);\n+\t}\n+\n \tif (!refs)\n \t\treturn -1;\n \n-- \n2.8.1\n"},{"id":"288315","messageId":"2c2adb2b2ffdfd2f644d6345dbe8b08b8e08d829.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 18/38] resolve_gitlink_ref(): rename path parameter to submodule","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:53Z","receivedAt":"2016-06-03T21:03:53Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c | 11 ++++++-----\n refs.h |  9 +++++----\n 2 files changed, 11 insertions(+), 9 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 7a8ef6d..c39f85a 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1299,22 +1299,23 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \t\t\t\t       resolve_flags, sha1, flags);\n }\n \n-int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n+int resolve_gitlink_ref(const char *submodule, const char *refname,\n+\t\t\tunsigned char *sha1)\n {\n-\tsize_t len, orig_len = strlen(path);\n+\tsize_t len, orig_len = strlen(submodule);\n \tstruct ref_store *refs;\n \tint flags;\n \n-\tfor (len = orig_len; len && path[len - 1] == '/'; len--)\n+\tfor (len = orig_len; len && submodule[len - 1] == '/'; len--)\n \t\t;\n \n \tif (!len)\n \t\treturn -1;\n \n \tif (len == orig_len) {\n-\t\trefs = get_ref_store(path);\n+\t\trefs = get_ref_store(submodule);\n \t} else {\n-\t\tchar *stripped = xmemdupz(path, len);\n+\t\tchar *stripped = xmemdupz(submodule, len);\n \n \t\trefs = get_ref_store(stripped);\n \t\tfree(stripped);\ndiff --git a/refs.h b/refs.h\nindex 52ea93b..132dcef 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -77,11 +77,12 @@ int is_branch(const char *refname);\n int peel_ref(const char *refname, unsigned char *sha1);\n \n /**\n- * Resolve refname in the nested \"gitlink\" repository that is located\n- * at path.  If the resolution is successful, return 0 and set sha1 to\n- * the name of the object; otherwise, return a non-zero value.\n+ * Resolve refname in the nested \"gitlink\" repository in the specified\n+ * submodule (which must be non-NULL). If the resolution is\n+ * successful, return 0 and set sha1 to the name of the object;\n+ * otherwise, return a non-zero value.\n  */\n-int resolve_gitlink_ref(const char *path, const char *refname,\n+int resolve_gitlink_ref(const char *submodule, const char *refname,\n \t\t\tunsigned char *sha1);\n \n /*\n-- \n2.8.1\n"},{"id":"288303","messageId":"ca7ba1938c725ae936a9f5f82935674d1d582366.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 19/38] refs: make read_raw_ref() virtual","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:54Z","receivedAt":"2016-06-03T21:03:54Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Reference backends will be able to customize this function to implement\nreference reading.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               |  4 ++--\n refs/files-backend.c | 14 ++++++++------\n refs/refs-internal.h | 36 +++++++++++++++++++-----------------\n 3 files changed, 29 insertions(+), 25 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex c39f85a..1798f66 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1251,8 +1251,8 @@ static const char *resolve_ref_recursively(struct ref_store *refs,\n \tfor (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {\n \t\tunsigned int read_flags = 0;\n \n-\t\tif (read_raw_ref(refs, refname,\n-\t\t\t\t sha1, &sb_refname, &read_flags)) {\n+\t\tif (refs->be->read_raw_ref(refs, refname,\n+\t\t\t\t\t   sha1, &sb_refname, &read_flags)) {\n \t\t\t*flags |= read_flags;\n \t\t\tif (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))\n \t\t\t\treturn NULL;\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex d6d83d2..5b6d388 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1353,9 +1353,9 @@ static int resolve_packed_ref(struct files_ref_store *refs,\n \treturn -1;\n }\n \n-int read_raw_ref(struct ref_store *ref_store,\n-\t\t const char *refname, unsigned char *sha1,\n-\t\t struct strbuf *referent, unsigned int *type)\n+static int files_read_raw_ref(struct ref_store *ref_store,\n+\t\t\t      const char *refname, unsigned char *sha1,\n+\t\t\t      struct strbuf *referent, unsigned int *type)\n {\n \tstruct files_ref_store *refs =\n \t\tfiles_downcast(ref_store, 1, \"read_raw_ref\");\n@@ -1627,8 +1627,8 @@ retry:\n \t * fear that its value will change.\n \t */\n \n-\tif (read_raw_ref(ref_store, refname,\n-\t\t\t lock->old_oid.hash, referent, type)) {\n+\tif (files_read_raw_ref(ref_store, refname,\n+\t\t\t       lock->old_oid.hash, referent, type)) {\n \t\tif (errno == ENOENT) {\n \t\t\tif (mustexist) {\n \t\t\t\t/* Garden variety missing reference. */\n@@ -4023,5 +4023,7 @@ struct ref_storage_be refs_be_files = {\n \tNULL,\n \t\"files\",\n \tfiles_ref_store_create,\n-\tfiles_transaction_commit\n+\tfiles_transaction_commit,\n+\n+\tfiles_read_raw_ref\n };\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 8c45ee3..8d4287c 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -481,6 +481,20 @@ int do_for_each_ref_iterator(struct ref_iterator *iter,\n \n struct ref_store;\n \n+/* refs backends */\n+\n+/*\n+ * Initialize the ref_store for the specified submodule, or for the\n+ * main repository if submodule == NULL. These functions should call\n+ * base_ref_store_init() to initialize the shared part of the\n+ * ref_store and to record the ref_store for later lookup.\n+ */\n+typedef struct ref_store *ref_store_init_fn(const char *submodule);\n+\n+typedef int ref_transaction_commit_fn(struct ref_store *refs,\n+\t\t\t\t      struct ref_transaction *transaction,\n+\t\t\t\t      struct strbuf *err);\n+\n /*\n  * Read a reference from the specified reference store, non-recursively.\n  * Set type to describe the reference, and:\n@@ -519,29 +533,17 @@ struct ref_store;\n  * - in all other cases, referent will be untouched, and therefore\n  *   refname will still be valid and unchanged.\n  */\n-int read_raw_ref(struct ref_store *ref_store,\n-\t\t const char *refname, unsigned char *sha1,\n-\t\t struct strbuf *referent, unsigned int *type);\n-\n-/* refs backends */\n-\n-/*\n- * Initialize the ref_store for the specified submodule, or for the\n- * main repository if submodule == NULL. These functions should call\n- * base_ref_store_init() to initialize the shared part of the\n- * ref_store and to record the ref_store for later lookup.\n- */\n-typedef struct ref_store *ref_store_init_fn(const char *submodule);\n-\n-typedef int ref_transaction_commit_fn(struct ref_store *refs,\n-\t\t\t\t      struct ref_transaction *transaction,\n-\t\t\t\t      struct strbuf *err);\n+typedef int read_raw_ref_fn(struct ref_store *ref_store,\n+\t\t\t    const char *refname, unsigned char *sha1,\n+\t\t\t    struct strbuf *referent, unsigned int *type);\n \n struct ref_storage_be {\n \tstruct ref_storage_be *next;\n \tconst char *name;\n \tref_store_init_fn *init;\n \tref_transaction_commit_fn *transaction_commit;\n+\n+\tread_raw_ref_fn *read_raw_ref;\n };\n \n extern struct ref_storage_be refs_be_files;\n-- \n2.8.1\n"},{"id":"288306","messageId":"6781e0075884167b1ea6d1ca10ff07e235052d72.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 20/38] refs: make verify_refname_available() virtual","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:55Z","receivedAt":"2016-06-03T21:03:55Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 10 ++++++++++\n refs/files-backend.c | 14 ++++++++------\n refs/refs-internal.h |  7 +++++++\n 3 files changed, 25 insertions(+), 6 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 1798f66..79ef443 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1425,3 +1425,13 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \n \treturn refs->be->transaction_commit(refs, transaction, err);\n }\n+\n+int verify_refname_available(const char *refname,\n+\t\t\t     const struct string_list *extra,\n+\t\t\t     const struct string_list *skip,\n+\t\t\t     struct strbuf *err)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->verify_refname_available(refs, refname, extra, skip, err);\n+}\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 5b6d388..af8de85 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2553,13 +2553,14 @@ out:\n \treturn ret;\n }\n \n-int verify_refname_available(const char *newname,\n-\t\t\t     const struct string_list *extras,\n-\t\t\t     const struct string_list *skip,\n-\t\t\t     struct strbuf *err)\n+static int files_verify_refname_available(struct ref_store *ref_store,\n+\t\t\t\t\t  const char *newname,\n+\t\t\t\t\t  const struct string_list *extras,\n+\t\t\t\t\t  const struct string_list *skip,\n+\t\t\t\t\t  struct strbuf *err)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"verify_refname_available\");\n+\t\tfiles_downcast(ref_store, 1, \"verify_refname_available\");\n \tstruct ref_dir *packed_refs = get_packed_refs(refs);\n \tstruct ref_dir *loose_refs = get_loose_refs(refs);\n \n@@ -4025,5 +4026,6 @@ struct ref_storage_be refs_be_files = {\n \tfiles_ref_store_create,\n \tfiles_transaction_commit,\n \n-\tfiles_read_raw_ref\n+\tfiles_read_raw_ref,\n+\tfiles_verify_refname_available\n };\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 8d4287c..18e01d0 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -537,6 +537,12 @@ typedef int read_raw_ref_fn(struct ref_store *ref_store,\n \t\t\t    const char *refname, unsigned char *sha1,\n \t\t\t    struct strbuf *referent, unsigned int *type);\n \n+typedef int verify_refname_available_fn(struct ref_store *ref_store,\n+\t\t\t\t\tconst char *newname,\n+\t\t\t\t\tconst struct string_list *extras,\n+\t\t\t\t\tconst struct string_list *skip,\n+\t\t\t\t\tstruct strbuf *err);\n+\n struct ref_storage_be {\n \tstruct ref_storage_be *next;\n \tconst char *name;\n@@ -544,6 +550,7 @@ struct ref_storage_be {\n \tref_transaction_commit_fn *transaction_commit;\n \n \tread_raw_ref_fn *read_raw_ref;\n+\tverify_refname_available_fn *verify_refname_available;\n };\n \n extern struct ref_storage_be refs_be_files;\n-- \n2.8.1\n"},{"id":"288314","messageId":"22e49ca0f50831538d07d115d56b4af4e457a80c.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 21/38] refs: make pack_refs() virtual","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:56Z","receivedAt":"2016-06-03T21:03:56Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 7 +++++++\n refs/files-backend.c | 6 ++++--\n refs/refs-internal.h | 4 ++++\n 3 files changed, 15 insertions(+), 2 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 79ef443..f4f5f32 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1418,6 +1418,13 @@ void assert_main_repository(struct ref_store *refs, const char *caller)\n }\n \n /* backend functions */\n+int pack_refs(unsigned int flags)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->pack_refs(refs, flags);\n+}\n+\n int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t   struct strbuf *err)\n {\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex af8de85..e5a8799 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2358,10 +2358,10 @@ static void prune_refs(struct ref_to_prune *r)\n \t}\n }\n \n-int pack_refs(unsigned int flags)\n+static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"pack_refs\");\n+\t\tfiles_downcast(ref_store, 0, \"pack_refs\");\n \tstruct pack_refs_cb_data cbdata;\n \n \tmemset(&cbdata, 0, sizeof(cbdata));\n@@ -4026,6 +4026,8 @@ struct ref_storage_be refs_be_files = {\n \tfiles_ref_store_create,\n \tfiles_transaction_commit,\n \n+\tfiles_pack_refs,\n+\n \tfiles_read_raw_ref,\n \tfiles_verify_refname_available\n };\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 18e01d0..578c125 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -495,6 +495,8 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,\n \t\t\t\t      struct ref_transaction *transaction,\n \t\t\t\t      struct strbuf *err);\n \n+typedef int pack_refs_fn(struct ref_store *ref_store, unsigned int flags);\n+\n /*\n  * Read a reference from the specified reference store, non-recursively.\n  * Set type to describe the reference, and:\n@@ -549,6 +551,8 @@ struct ref_storage_be {\n \tref_store_init_fn *init;\n \tref_transaction_commit_fn *transaction_commit;\n \n+\tpack_refs_fn *pack_refs;\n+\n \tread_raw_ref_fn *read_raw_ref;\n \tverify_refname_available_fn *verify_refname_available;\n };\n-- \n2.8.1\n"},{"id":"288305","messageId":"a56b10941416c5d0ff229ac397cd16f2d9a795b7.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 22/38] refs: make create_symref() virtual","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:57Z","receivedAt":"2016-06-03T21:03:57Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 9 +++++++++\n refs/files-backend.c | 7 ++++++-\n refs/refs-internal.h | 5 +++++\n 3 files changed, 20 insertions(+), 1 deletion(-)\n\ndiff --git a/refs.c b/refs.c\nindex f4f5f32..22837f4 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1425,6 +1425,15 @@ int pack_refs(unsigned int flags)\n \treturn refs->be->pack_refs(refs, flags);\n }\n \n+int create_symref(const char *ref_target, const char *refs_heads_master,\n+\t\t  const char *logmsg)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->create_symref(refs, ref_target, refs_heads_master,\n+\t\t\t\t       logmsg);\n+}\n+\n int ref_transaction_commit(struct ref_transaction *transaction,\n \t\t\t   struct strbuf *err)\n {\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex e5a8799..f82a1be 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3015,12 +3015,16 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,\n \treturn 0;\n }\n \n-int create_symref(const char *refname, const char *target, const char *logmsg)\n+static int files_create_symref(struct ref_store *ref_store,\n+\t\t\t       const char *refname, const char *target,\n+\t\t\t       const char *logmsg)\n {\n \tstruct strbuf err = STRBUF_INIT;\n \tstruct ref_lock *lock;\n \tint ret;\n \n+\tfiles_downcast(ref_store, 0, \"create_symref\");\n+\n \tlock = lock_ref_sha1_basic(refname, NULL, NULL, NULL, REF_NODEREF, NULL,\n \t\t\t\t   &err);\n \tif (!lock) {\n@@ -4027,6 +4031,7 @@ struct ref_storage_be refs_be_files = {\n \tfiles_transaction_commit,\n \n \tfiles_pack_refs,\n+\tfiles_create_symref,\n \n \tfiles_read_raw_ref,\n \tfiles_verify_refname_available\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 578c125..c342e57 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -496,6 +496,10 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,\n \t\t\t\t      struct strbuf *err);\n \n typedef int pack_refs_fn(struct ref_store *ref_store, unsigned int flags);\n+typedef int create_symref_fn(struct ref_store *ref_store,\n+\t\t\t     const char *ref_target,\n+\t\t\t     const char *refs_heads_master,\n+\t\t\t     const char *logmsg);\n \n /*\n  * Read a reference from the specified reference store, non-recursively.\n@@ -552,6 +556,7 @@ struct ref_storage_be {\n \tref_transaction_commit_fn *transaction_commit;\n \n \tpack_refs_fn *pack_refs;\n+\tcreate_symref_fn *create_symref;\n \n \tread_raw_ref_fn *read_raw_ref;\n \tverify_refname_available_fn *verify_refname_available;\n-- \n2.8.1\n"},{"id":"288310","messageId":"316f0fed796fd2ed17ea5df4c264a98028d6b98a.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 23/38] refs: make peel_ref() virtual","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:58Z","receivedAt":"2016-06-03T21:03:58Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"For now it only supports the main reference store.\n\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 7 +++++++\n refs/files-backend.c | 6 ++++--\n refs/refs-internal.h | 3 +++\n 3 files changed, 14 insertions(+), 2 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 22837f4..2d84c5c 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1425,6 +1425,13 @@ int pack_refs(unsigned int flags)\n \treturn refs->be->pack_refs(refs, flags);\n }\n \n+int peel_ref(const char *refname, unsigned char *sha1)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->peel_ref(refs, refname, sha1);\n+}\n+\n int create_symref(const char *ref_target, const char *refs_heads_master,\n \t\t  const char *logmsg)\n {\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex f82a1be..91dcfcb 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1763,9 +1763,10 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)\n \treturn status;\n }\n \n-int peel_ref(const char *refname, unsigned char *sha1)\n+static int files_peel_ref(struct ref_store *ref_store,\n+\t\t\t  const char *refname, unsigned char *sha1)\n {\n-\tstruct files_ref_store *refs = get_files_ref_store(NULL, \"peel_ref\");\n+\tstruct files_ref_store *refs = files_downcast(ref_store, 0, \"peel_ref\");\n \tint flag;\n \tunsigned char base[20];\n \n@@ -4031,6 +4032,7 @@ struct ref_storage_be refs_be_files = {\n \tfiles_transaction_commit,\n \n \tfiles_pack_refs,\n+\tfiles_peel_ref,\n \tfiles_create_symref,\n \n \tfiles_read_raw_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex c342e57..ae67b49 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -496,6 +496,8 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,\n \t\t\t\t      struct strbuf *err);\n \n typedef int pack_refs_fn(struct ref_store *ref_store, unsigned int flags);\n+typedef int peel_ref_fn(struct ref_store *ref_store,\n+\t\t\tconst char *refname, unsigned char *sha1);\n typedef int create_symref_fn(struct ref_store *ref_store,\n \t\t\t     const char *ref_target,\n \t\t\t     const char *refs_heads_master,\n@@ -556,6 +558,7 @@ struct ref_storage_be {\n \tref_transaction_commit_fn *transaction_commit;\n \n \tpack_refs_fn *pack_refs;\n+\tpeel_ref_fn *peel_ref;\n \tcreate_symref_fn *create_symref;\n \n \tread_raw_ref_fn *read_raw_ref;\n-- \n2.8.1\n"},{"id":"288308","messageId":"7ee1796e8cdfe95c043663f2c1446944012467d6.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 24/38] repack_without_refs(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:03:59Z","receivedAt":"2016-06-03T21:03:59Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 12 +++++++-----\n 1 file changed, 7 insertions(+), 5 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 91dcfcb..48b37fa 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2388,14 +2388,14 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)\n  *\n  * The refs in 'refnames' needn't be sorted. `err` must not be NULL.\n  */\n-static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n+static int repack_without_refs(struct files_ref_store *refs,\n+\t\t\t       struct string_list *refnames, struct strbuf *err)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"repack_without_refs\");\n \tstruct ref_dir *packed;\n \tstruct string_list_item *refname;\n \tint ret, needs_repacking = 0, removed = 0;\n \n+\tassert_main_repository(&refs->base, \"repack_without_refs\");\n \tassert(err);\n \n \t/* Look for a packed ref */\n@@ -2457,13 +2457,15 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)\n \n int delete_refs(struct string_list *refnames, unsigned int flags)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"delete_refs\");\n \tstruct strbuf err = STRBUF_INIT;\n \tint i, result = 0;\n \n \tif (!refnames->nr)\n \t\treturn 0;\n \n-\tresult = repack_without_refs(refnames, &err);\n+\tresult = repack_without_refs(refs, refnames, &err);\n \tif (result) {\n \t\t/*\n \t\t * If we failed to rewrite the packed-refs file, then\n@@ -3773,7 +3775,7 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \t\t}\n \t}\n \n-\tif (repack_without_refs(&refs_to_delete, err)) {\n+\tif (repack_without_refs(refs, &refs_to_delete, err)) {\n \t\tret = TRANSACTION_GENERIC_ERROR;\n \t\tgoto cleanup;\n \t}\n-- \n2.8.1\n"},{"id":"288309","messageId":"e06f5aae56ae8d83812effa7c359dc0b6796740b.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 25/38] lock_raw_ref(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:00Z","receivedAt":"2016-06-03T21:04:00Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 14 ++++++++------\n 1 file changed, 8 insertions(+), 6 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 48b37fa..dfce4d1 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1527,7 +1527,8 @@ static void unlock_ref(struct ref_lock *lock)\n  *   avoided, namely if we were successfully able to read the ref\n  * - Generate informative error messages in the case of failure\n  */\n-static int lock_raw_ref(const char *refname, int mustexist,\n+static int lock_raw_ref(struct files_ref_store *refs,\n+\t\t\tconst char *refname, int mustexist,\n \t\t\tconst struct string_list *extras,\n \t\t\tconst struct string_list *skip,\n \t\t\tstruct ref_lock **lock_p,\n@@ -1535,15 +1536,14 @@ static int lock_raw_ref(const char *refname, int mustexist,\n \t\t\tunsigned int *type,\n \t\t\tstruct strbuf *err)\n {\n-\tstruct ref_store *ref_store = get_ref_store(NULL);\n-\tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, 0, \"lock_raw_ref\");\n \tstruct ref_lock *lock;\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tint attempts_remaining = 3;\n \tint ret = TRANSACTION_GENERIC_ERROR;\n \n \tassert(err);\n+\tassert_main_repository(&refs->base, \"lock_raw_ref\");\n+\n \t*type = 0;\n \n \t/* First lock the file so it can't change out from under us. */\n@@ -1627,7 +1627,7 @@ retry:\n \t * fear that its value will change.\n \t */\n \n-\tif (files_read_raw_ref(ref_store, refname,\n+\tif (files_read_raw_ref(&refs->base, refname,\n \t\t\t       lock->old_oid.hash, referent, type)) {\n \t\tif (errno == ENOENT) {\n \t\t\tif (mustexist) {\n@@ -3489,6 +3489,8 @@ static int lock_ref_for_update(struct ref_update *update,\n \t\t\t       struct string_list *affected_refnames,\n \t\t\t       struct strbuf *err)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"lock_ref_for_update\");\n \tstruct strbuf referent = STRBUF_INIT;\n \tint mustexist = (update->flags & REF_HAVE_OLD) &&\n \t\t!is_null_sha1(update->old_sha1);\n@@ -3505,7 +3507,7 @@ static int lock_ref_for_update(struct ref_update *update,\n \t\t\treturn ret;\n \t}\n \n-\tret = lock_raw_ref(update->refname, mustexist,\n+\tret = lock_raw_ref(refs, update->refname, mustexist,\n \t\t\t   affected_refnames, NULL,\n \t\t\t   &update->lock, &referent,\n \t\t\t   &update->type, err);\n-- \n2.8.1\n"},{"id":"288312","messageId":"f987509203a2ff8c3ac7c3855f85ad229252d2ae.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 26/38] commit_ref_update(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:01Z","receivedAt":"2016-06-03T21:04:01Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 14 ++++++++------\n 1 file changed, 8 insertions(+), 6 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex dfce4d1..bc7d250 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2578,12 +2578,14 @@ static int files_verify_refname_available(struct ref_store *ref_store,\n \n static int write_ref_to_lockfile(struct ref_lock *lock,\n \t\t\t\t const unsigned char *sha1, struct strbuf *err);\n-static int commit_ref_update(struct ref_lock *lock,\n+static int commit_ref_update(struct files_ref_store *refs,\n+\t\t\t     struct ref_lock *lock,\n \t\t\t     const unsigned char *sha1, const char *logmsg,\n \t\t\t     struct strbuf *err);\n \n int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)\n {\n+\tstruct files_ref_store *refs = get_files_ref_store(NULL, \"rename_ref\");\n \tunsigned char sha1[20], orig_sha1[20];\n \tint flag = 0, logmoved = 0;\n \tstruct ref_lock *lock;\n@@ -2656,7 +2658,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \thashcpy(lock->old_oid.hash, orig_sha1);\n \n \tif (write_ref_to_lockfile(lock, orig_sha1, &err) ||\n-\t    commit_ref_update(lock, orig_sha1, logmsg, &err)) {\n+\t    commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {\n \t\terror(\"unable to write current sha1 into %s: %s\", newrefname, err.buf);\n \t\tstrbuf_release(&err);\n \t\tgoto rollback;\n@@ -2676,7 +2678,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \tflag = log_all_ref_updates;\n \tlog_all_ref_updates = 0;\n \tif (write_ref_to_lockfile(lock, orig_sha1, &err) ||\n-\t    commit_ref_update(lock, orig_sha1, NULL, &err)) {\n+\t    commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {\n \t\terror(\"unable to write current sha1 into %s: %s\", oldrefname, err.buf);\n \t\tstrbuf_release(&err);\n \t}\n@@ -2912,12 +2914,12 @@ static int write_ref_to_lockfile(struct ref_lock *lock,\n  * to the loose reference lockfile. Also update the reflogs if\n  * necessary, using the specified lockmsg (which can be NULL).\n  */\n-static int commit_ref_update(struct ref_lock *lock,\n+static int commit_ref_update(struct files_ref_store *refs,\n+\t\t\t     struct ref_lock *lock,\n \t\t\t     const unsigned char *sha1, const char *logmsg,\n \t\t\t     struct strbuf *err)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"commit_ref_update\");\n+\tassert_main_repository(&refs->base, \"commit_ref_update\");\n \n \tclear_loose_ref_cache(refs);\n \tif (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {\n-- \n2.8.1\n"},{"id":"288313","messageId":"f9b7e6a1979e91115a549e94b365a3196ff5e3f5.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 27/38] lock_ref_for_update(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:02Z","receivedAt":"2016-06-03T21:04:02Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 11 ++++++-----\n 1 file changed, 6 insertions(+), 5 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex bc7d250..225e0af 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3485,20 +3485,21 @@ static const char *original_update_refname(struct ref_update *update)\n  * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY\n  *   update of HEAD.\n  */\n-static int lock_ref_for_update(struct ref_update *update,\n+static int lock_ref_for_update(struct files_ref_store *refs,\n+\t\t\t       struct ref_update *update,\n \t\t\t       struct ref_transaction *transaction,\n \t\t\t       const char *head_ref,\n \t\t\t       struct string_list *affected_refnames,\n \t\t\t       struct strbuf *err)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"lock_ref_for_update\");\n \tstruct strbuf referent = STRBUF_INIT;\n \tint mustexist = (update->flags & REF_HAVE_OLD) &&\n \t\t!is_null_sha1(update->old_sha1);\n \tint ret;\n \tstruct ref_lock *lock;\n \n+\tassert_main_repository(&refs->base, \"lock_ref_for_update\");\n+\n \tif ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))\n \t\tupdate->flags |= REF_DELETING;\n \n@@ -3724,8 +3725,8 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = updates[i];\n \n-\t\tret = lock_ref_for_update(update, transaction, head_ref,\n-\t\t\t\t\t  &affected_refnames, err);\n+\t\tret = lock_ref_for_update(refs, update, transaction,\n+\t\t\t\t\t  head_ref, &affected_refnames, err);\n \t\tif (ret)\n \t\t\tgoto cleanup;\n \t}\n-- \n2.8.1\n"},{"id":"288311","messageId":"f754a38a5b0c6c3fa0873e3c460f9b0deb183e47.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 28/38] lock_ref_sha1_basic(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:03Z","receivedAt":"2016-06-03T21:04:03Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 26 +++++++++++++++-----------\n 1 file changed, 15 insertions(+), 11 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 225e0af..24b7c60 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1987,15 +1987,14 @@ static int remove_empty_directories(struct strbuf *path)\n  * Locks a ref returning the lock on success and NULL on failure.\n  * On failure errno is set to something meaningful.\n  */\n-static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n+static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,\n+\t\t\t\t\t    const char *refname,\n \t\t\t\t\t    const unsigned char *old_sha1,\n \t\t\t\t\t    const struct string_list *extras,\n \t\t\t\t\t    const struct string_list *skip,\n \t\t\t\t\t    unsigned int flags, int *type,\n \t\t\t\t\t    struct strbuf *err)\n {\n-\tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"lock_ref_sha1_basic\");\n \tstruct strbuf ref_file = STRBUF_INIT;\n \tstruct ref_lock *lock;\n \tint last_errno = 0;\n@@ -2005,6 +2004,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n \tint attempts_remaining = 3;\n \tint resolved;\n \n+\tassert_main_repository(&refs->base, \"lock_ref_sha1_basic\");\n \tassert(err);\n \n \tlock = xcalloc(1, sizeof(struct ref_lock));\n@@ -2648,8 +2648,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \n \tlogmoved = log;\n \n-\tlock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, REF_NODEREF,\n-\t\t\t\t   NULL, &err);\n+\tlock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,\n+\t\t\t\t   REF_NODEREF, NULL, &err);\n \tif (!lock) {\n \t\terror(\"unable to rename '%s' to '%s': %s\", oldrefname, newrefname, err.buf);\n \t\tstrbuf_release(&err);\n@@ -2667,8 +2667,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms\n \treturn 0;\n \n  rollback:\n-\tlock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, REF_NODEREF,\n-\t\t\t\t   NULL, &err);\n+\tlock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,\n+\t\t\t\t   REF_NODEREF, NULL, &err);\n \tif (!lock) {\n \t\terror(\"unable to lock %s for rollback: %s\", oldrefname, err.buf);\n \t\tstrbuf_release(&err);\n@@ -3024,13 +3024,14 @@ static int files_create_symref(struct ref_store *ref_store,\n \t\t\t       const char *refname, const char *target,\n \t\t\t       const char *logmsg)\n {\n+\tstruct files_ref_store *refs =\n+\t\tfiles_downcast(ref_store, 0, \"create_symref\");\n \tstruct strbuf err = STRBUF_INIT;\n \tstruct ref_lock *lock;\n \tint ret;\n \n-\tfiles_downcast(ref_store, 0, \"create_symref\");\n-\n-\tlock = lock_ref_sha1_basic(refname, NULL, NULL, NULL, REF_NODEREF, NULL,\n+\tlock = lock_ref_sha1_basic(refs, refname, NULL,\n+\t\t\t\t   NULL, NULL, REF_NODEREF, NULL,\n \t\t\t\t   &err);\n \tif (!lock) {\n \t\terror(\"%s\", err.buf);\n@@ -3933,6 +3934,8 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n \t\t reflog_expiry_cleanup_fn cleanup_fn,\n \t\t void *policy_cb_data)\n {\n+\tstruct files_ref_store *refs =\n+\t\tget_files_ref_store(NULL, \"reflog_expire\");\n \tstatic struct lock_file reflog_lock;\n \tstruct expire_reflog_cb cb;\n \tstruct ref_lock *lock;\n@@ -3951,7 +3954,8 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n \t * reference itself, plus we might need to update the\n \t * reference if --updateref was specified:\n \t */\n-\tlock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, REF_NODEREF,\n+\tlock = lock_ref_sha1_basic(refs, refname, sha1,\n+\t\t\t\t   NULL, NULL, REF_NODEREF,\n \t\t\t\t   &type, &err);\n \tif (!lock) {\n \t\terror(\"cannot lock ref '%s': %s\", refname, err.buf);\n-- \n2.8.1\n"},{"id":"288319","messageId":"68c836148e4c8655161fba54cd1fd56a0e48dbbb.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 29/38] split_symref_update(): add a files_ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:04Z","receivedAt":"2016-06-03T21:04:04Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 7 ++++---\n 1 file changed, 4 insertions(+), 3 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 24b7c60..8d3cf96 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3405,7 +3405,8 @@ static int split_head_update(struct ref_update *update,\n  * Note that the new update will itself be subject to splitting when\n  * the iteration gets to it.\n  */\n-static int split_symref_update(struct ref_update *update,\n+static int split_symref_update(struct files_ref_store *refs,\n+\t\t\t       struct ref_update *update,\n \t\t\t       const char *referent,\n \t\t\t       struct ref_transaction *transaction,\n \t\t\t       struct string_list *affected_refnames,\n@@ -3556,7 +3557,6 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t\t\t    sha1_to_hex(update->old_sha1));\n \t\t\t\treturn TRANSACTION_GENERIC_ERROR;\n \t\t\t}\n-\n \t\t} else {\n \t\t\t/*\n \t\t\t * Create a new update for the reference this\n@@ -3565,7 +3565,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t * of processing the split-off update, so we\n \t\t\t * don't have to do it here.\n \t\t\t */\n-\t\t\tret = split_symref_update(update, referent.buf, transaction,\n+\t\t\tret = split_symref_update(refs, update,\n+\t\t\t\t\t\t  referent.buf, transaction,\n \t\t\t\t\t\t  affected_refnames, err);\n \t\t\tif (ret)\n \t\t\t\treturn ret;\n-- \n2.8.1\n"},{"id":"288324","messageId":"74aa9408e6ff1f0e26cd424d27e43c3606eb5a80.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 30/38] files_ref_iterator_begin(): take a ref_store argument","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:05Z","receivedAt":"2016-06-03T21:04:05Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 2 +-\n refs/files-backend.c | 4 ++--\n refs/refs-internal.h | 8 ++++----\n 3 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 2d84c5c..bda8fc1 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1157,7 +1157,7 @@ static int do_for_each_ref(const char *submodule, const char *prefix,\n \tif (!refs)\n \t\treturn 0;\n \n-\titer = files_ref_iterator_begin(submodule, prefix, flags);\n+\titer = files_ref_iterator_begin(refs, prefix, flags);\n \titer = prefix_ref_iterator_begin(iter, prefix, trim);\n \n \treturn do_for_each_ref_iterator(iter, fn, cb_data);\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 8d3cf96..6699ad3 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1867,11 +1867,11 @@ struct ref_iterator_vtable files_ref_iterator_vtable = {\n };\n \n struct ref_iterator *files_ref_iterator_begin(\n-\t\tconst char *submodule,\n+\t\tstruct ref_store *ref_store,\n \t\tconst char *prefix, unsigned int flags)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(submodule, \"ref_iterator_begin\");\n+\t\tfiles_downcast(ref_store, 1, \"ref_iterator_begin\");\n \tstruct ref_dir *loose_dir, *packed_dir;\n \tstruct ref_iterator *loose_iter, *packed_iter;\n \tstruct files_ref_iterator *iter;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex ae67b49..a5caecc 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -399,13 +399,15 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,\n \t\t\t\t\t       const char *prefix,\n \t\t\t\t\t       int trim);\n \n+struct ref_store;\n+\n /*\n  * Iterate over the packed and loose references in the specified\n- * submodule that are within find_containing_dir(prefix). If prefix is\n+ * ref_store that are within find_containing_dir(prefix). If prefix is\n  * NULL or the empty string, iterate over all references in the\n  * submodule.\n  */\n-struct ref_iterator *files_ref_iterator_begin(const char *submodule,\n+struct ref_iterator *files_ref_iterator_begin(struct ref_store *ref_store,\n \t\t\t\t\t      const char *prefix,\n \t\t\t\t\t      unsigned int flags);\n \n@@ -479,8 +481,6 @@ extern struct ref_iterator *current_ref_iter;\n int do_for_each_ref_iterator(struct ref_iterator *iter,\n \t\t\t     each_ref_fn fn, void *cb_data);\n \n-struct ref_store;\n-\n /* refs backends */\n \n /*\n-- \n2.8.1\n"},{"id":"288316","messageId":"3775f2713c0a8070b2a8424ae3d4176585748479.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 31/38] refs: add method iterator_begin","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:06Z","receivedAt":"2016-06-03T21:04:06Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               |  2 +-\n refs/files-backend.c |  3 ++-\n refs/refs-internal.h | 24 ++++++++++++------------\n 3 files changed, 15 insertions(+), 14 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex bda8fc1..d1ab20b 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1157,7 +1157,7 @@ static int do_for_each_ref(const char *submodule, const char *prefix,\n \tif (!refs)\n \t\treturn 0;\n \n-\titer = files_ref_iterator_begin(refs, prefix, flags);\n+\titer = refs->be->iterator_begin(refs, prefix, flags);\n \titer = prefix_ref_iterator_begin(iter, prefix, trim);\n \n \treturn do_for_each_ref_iterator(iter, fn, cb_data);\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 6699ad3..40c05b2 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1866,7 +1866,7 @@ struct ref_iterator_vtable files_ref_iterator_vtable = {\n \tfiles_ref_iterator_abort\n };\n \n-struct ref_iterator *files_ref_iterator_begin(\n+static struct ref_iterator *files_ref_iterator_begin(\n \t\tstruct ref_store *ref_store,\n \t\tconst char *prefix, unsigned int flags)\n {\n@@ -4047,6 +4047,7 @@ struct ref_storage_be refs_be_files = {\n \tfiles_peel_ref,\n \tfiles_create_symref,\n \n+\tfiles_ref_iterator_begin,\n \tfiles_read_raw_ref,\n \tfiles_verify_refname_available\n };\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex a5caecc..797d44f 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -399,18 +399,6 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,\n \t\t\t\t\t       const char *prefix,\n \t\t\t\t\t       int trim);\n \n-struct ref_store;\n-\n-/*\n- * Iterate over the packed and loose references in the specified\n- * ref_store that are within find_containing_dir(prefix). If prefix is\n- * NULL or the empty string, iterate over all references in the\n- * submodule.\n- */\n-struct ref_iterator *files_ref_iterator_begin(struct ref_store *ref_store,\n-\t\t\t\t\t      const char *prefix,\n-\t\t\t\t\t      unsigned int flags);\n-\n /*\n  * Iterate over the references in the main ref_store that have a\n  * reflog. The paths within a directory are iterated over in arbitrary\n@@ -483,6 +471,8 @@ int do_for_each_ref_iterator(struct ref_iterator *iter,\n \n /* refs backends */\n \n+struct ref_store;\n+\n /*\n  * Initialize the ref_store for the specified submodule, or for the\n  * main repository if submodule == NULL. These functions should call\n@@ -504,6 +494,15 @@ typedef int create_symref_fn(struct ref_store *ref_store,\n \t\t\t     const char *logmsg);\n \n /*\n+ * Iterate over the references in the specified ref_store that are\n+ * within find_containing_dir(prefix). If prefix is NULL or the empty\n+ * string, iterate over all references in the submodule.\n+ */\n+typedef struct ref_iterator *ref_iterator_begin_fn(\n+\t\tstruct ref_store *ref_store,\n+\t\tconst char *prefix, unsigned int flags);\n+\n+/*\n  * Read a reference from the specified reference store, non-recursively.\n  * Set type to describe the reference, and:\n  *\n@@ -561,6 +560,7 @@ struct ref_storage_be {\n \tpeel_ref_fn *peel_ref;\n \tcreate_symref_fn *create_symref;\n \n+\tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\n \tverify_refname_available_fn *verify_refname_available;\n };\n-- \n2.8.1\n"},{"id":"288317","messageId":"a2875a32847a605a95896646aae9ca88812b81f7.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 32/38] refs: add methods for reflog","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:07Z","receivedAt":"2016-06-03T21:04:07Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nIn the file-based backend, the reflog piggybacks on the ref lock.\nSince other backends won't have the same sort of ref lock, ref backends\nmust also handle reflogs.\n\nSigned-off-by: Ronnie Sahlberg <rsahlberg@google.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\nThis patch doesn't fix the known problem [1] that worktree reflogs are\nnot considered when considering reachability, but I think this whole\npatch series will make the problem easier to fix in a relatively\nelegant way.\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/295961/focus=296068\n\n refs.c               | 63 ++++++++++++++++++++++++++++++++++++++++++++++\n refs/files-backend.c | 70 +++++++++++++++++++++++++++++++++++++---------------\n refs/refs-internal.h | 44 +++++++++++++++++++++++++++------\n 3 files changed, 150 insertions(+), 27 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex d1ab20b..77d3986 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1458,3 +1458,66 @@ int verify_refname_available(const char *refname,\n \n \treturn refs->be->verify_refname_available(refs, refname, extra, skip, err);\n }\n+\n+int for_each_reflog(each_ref_fn fn, void *cb_data)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\tstruct ref_iterator *iter;\n+\n+\titer = refs->be->reflog_iterator_begin(refs);\n+\n+\treturn do_for_each_ref_iterator(iter, fn, cb_data);\n+}\n+\n+int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,\n+\t\t\t\tvoid *cb_data)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->for_each_reflog_ent_reverse(refs, refname,\n+\t\t\t\t\t\t     fn, cb_data);\n+}\n+\n+int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,\n+\t\t\tvoid *cb_data)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);\n+}\n+\n+int reflog_exists(const char *refname)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->reflog_exists(refs, refname);\n+}\n+\n+int safe_create_reflog(const char *refname, int force_create,\n+\t\t       struct strbuf *err)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->create_reflog(refs, refname, force_create, err);\n+}\n+\n+int delete_reflog(const char *refname)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->delete_reflog(refs, refname);\n+}\n+\n+int reflog_expire(const char *refname, const unsigned char *sha1,\n+\t\t  unsigned int flags,\n+\t\t  reflog_expiry_prepare_fn prepare_fn,\n+\t\t  reflog_expiry_should_prune_fn should_prune_fn,\n+\t\t  reflog_expiry_cleanup_fn cleanup_fn,\n+\t\t  void *policy_cb_data)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->reflog_expire(refs, refname, sha1, flags,\n+\t\t\t\t       prepare_fn, should_prune_fn,\n+\t\t\t\t       cleanup_fn, policy_cb_data);\n+}\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 40c05b2..b19ea02 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2781,11 +2781,16 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str\n }\n \n \n-int safe_create_reflog(const char *refname, int force_create, struct strbuf *err)\n+static int files_create_reflog(struct ref_store *ref_store,\n+\t\t\t       const char *refname, int force_create,\n+\t\t\t       struct strbuf *err)\n {\n \tint ret;\n \tstruct strbuf sb = STRBUF_INIT;\n \n+\t/* Check validity (but we don't need the result): */\n+\tfiles_downcast(ref_store, 0, \"create_reflog\");\n+\n \tret = log_ref_setup(refname, &sb, err, force_create);\n \tstrbuf_release(&sb);\n \treturn ret;\n@@ -3079,16 +3084,24 @@ int set_worktree_head_symref(const char *gitdir, const char *target)\n \treturn ret;\n }\n \n-int reflog_exists(const char *refname)\n+static int files_reflog_exists(struct ref_store *ref_store,\n+\t\t\t       const char *refname)\n {\n \tstruct stat st;\n \n+\t/* Check validity (but we don't need the result): */\n+\tfiles_downcast(ref_store, 0, \"reflog_exists\");\n+\n \treturn !lstat(git_path(\"logs/%s\", refname), &st) &&\n \t\tS_ISREG(st.st_mode);\n }\n \n-int delete_reflog(const char *refname)\n+static int files_delete_reflog(struct ref_store *ref_store,\n+\t\t\t       const char *refname)\n {\n+\t/* Check validity (but we don't need the result): */\n+\tfiles_downcast(ref_store, 0, \"delete_reflog\");\n+\n \treturn remove_path(git_path(\"logs/%s\", refname));\n }\n \n@@ -3131,13 +3144,19 @@ static char *find_beginning_of_line(char *bob, char *scan)\n \treturn scan;\n }\n \n-int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data)\n+static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,\n+\t\t\t\t\t     const char *refname,\n+\t\t\t\t\t     each_reflog_ent_fn fn,\n+\t\t\t\t\t     void *cb_data)\n {\n \tstruct strbuf sb = STRBUF_INIT;\n \tFILE *logfp;\n \tlong pos;\n \tint ret = 0, at_tail = 1;\n \n+\t/* Check validity (but we don't need the result): */\n+\tfiles_downcast(ref_store, 0, \"for_each_reflog_ent_reverse\");\n+\n \tlogfp = fopen(git_path(\"logs/%s\", refname), \"r\");\n \tif (!logfp)\n \t\treturn -1;\n@@ -3233,12 +3252,17 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void\n \treturn ret;\n }\n \n-int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)\n+static int files_for_each_reflog_ent(struct ref_store *ref_store,\n+\t\t\t\t     const char *refname,\n+\t\t\t\t     each_reflog_ent_fn fn, void *cb_data)\n {\n \tFILE *logfp;\n \tstruct strbuf sb = STRBUF_INIT;\n \tint ret = 0;\n \n+\t/* Check validity (but we don't need the result): */\n+\tfiles_downcast(ref_store, 0, \"for_each_reflog_ent\");\n+\n \tlogfp = fopen(git_path(\"logs/%s\", refname), \"r\");\n \tif (!logfp)\n \t\treturn -1;\n@@ -3317,22 +3341,19 @@ struct ref_iterator_vtable files_reflog_iterator_vtable = {\n \tfiles_reflog_iterator_abort\n };\n \n-struct ref_iterator *files_reflog_iterator_begin(void)\n+struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)\n {\n \tstruct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));\n \tstruct ref_iterator *ref_iterator = &iter->base;\n \n+\t/* Check validity (but we don't need the result): */\n+\tfiles_downcast(ref_store, 0, \"reflog_iterator_begin\");\n+\n \tbase_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);\n \titer->dir_iterator = dir_iterator_begin(git_path(\"logs\"));\n \treturn ref_iterator;\n }\n \n-int for_each_reflog(each_ref_fn fn, void *cb_data)\n-{\n-\treturn do_for_each_ref_iterator(files_reflog_iterator_begin(),\n-\t\t\t\t\tfn, cb_data);\n-}\n-\n static int ref_update_reject_duplicates(struct string_list *refnames,\n \t\t\t\t\tstruct strbuf *err)\n {\n@@ -3928,15 +3949,16 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,\n \treturn 0;\n }\n \n-int reflog_expire(const char *refname, const unsigned char *sha1,\n-\t\t unsigned int flags,\n-\t\t reflog_expiry_prepare_fn prepare_fn,\n-\t\t reflog_expiry_should_prune_fn should_prune_fn,\n-\t\t reflog_expiry_cleanup_fn cleanup_fn,\n-\t\t void *policy_cb_data)\n+static int files_reflog_expire(struct ref_store *ref_store,\n+\t\t\t       const char *refname, const unsigned char *sha1,\n+\t\t\t       unsigned int flags,\n+\t\t\t       reflog_expiry_prepare_fn prepare_fn,\n+\t\t\t       reflog_expiry_should_prune_fn should_prune_fn,\n+\t\t\t       reflog_expiry_cleanup_fn cleanup_fn,\n+\t\t\t       void *policy_cb_data)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"reflog_expire\");\n+\t\tfiles_downcast(ref_store, 0, \"reflog_expire\");\n \tstatic struct lock_file reflog_lock;\n \tstruct expire_reflog_cb cb;\n \tstruct ref_lock *lock;\n@@ -4049,5 +4071,13 @@ struct ref_storage_be refs_be_files = {\n \n \tfiles_ref_iterator_begin,\n \tfiles_read_raw_ref,\n-\tfiles_verify_refname_available\n+\tfiles_verify_refname_available,\n+\n+\tfiles_reflog_iterator_begin,\n+\tfiles_for_each_reflog_ent,\n+\tfiles_for_each_reflog_ent_reverse,\n+\tfiles_reflog_exists,\n+\tfiles_create_reflog,\n+\tfiles_delete_reflog,\n+\tfiles_reflog_expire\n };\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 797d44f..7d84984 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -399,13 +399,6 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,\n \t\t\t\t\t       const char *prefix,\n \t\t\t\t\t       int trim);\n \n-/*\n- * Iterate over the references in the main ref_store that have a\n- * reflog. The paths within a directory are iterated over in arbitrary\n- * order.\n- */\n-struct ref_iterator *files_reflog_iterator_begin(void);\n-\n /* Internal implementation of reference iteration: */\n \n /*\n@@ -502,6 +495,35 @@ typedef struct ref_iterator *ref_iterator_begin_fn(\n \t\tstruct ref_store *ref_store,\n \t\tconst char *prefix, unsigned int flags);\n \n+/* reflog functions */\n+\n+/*\n+ * Iterate over the references in the specified ref_store that have a\n+ * reflog. The refs are iterated over in arbitrary order.\n+ */\n+typedef struct ref_iterator *reflog_iterator_begin_fn(\n+\t\tstruct ref_store *ref_store);\n+\n+typedef int for_each_reflog_ent_fn(struct ref_store *ref_store,\n+\t\t\t\t   const char *refname,\n+\t\t\t\t   each_reflog_ent_fn fn,\n+\t\t\t\t   void *cb_data);\n+typedef int for_each_reflog_ent_reverse_fn(struct ref_store *ref_store,\n+\t\t\t\t\t   const char *refname,\n+\t\t\t\t\t   each_reflog_ent_fn fn,\n+\t\t\t\t\t   void *cb_data);\n+typedef int reflog_exists_fn(struct ref_store *ref_store, const char *refname);\n+typedef int create_reflog_fn(struct ref_store *ref_store, const char *refname,\n+\t\t\t     int force_create, struct strbuf *err);\n+typedef int delete_reflog_fn(struct ref_store *ref_store, const char *refname);\n+typedef int reflog_expire_fn(struct ref_store *ref_store,\n+\t\t\t     const char *refname, const unsigned char *sha1,\n+\t\t\t     unsigned int flags,\n+\t\t\t     reflog_expiry_prepare_fn prepare_fn,\n+\t\t\t     reflog_expiry_should_prune_fn should_prune_fn,\n+\t\t\t     reflog_expiry_cleanup_fn cleanup_fn,\n+\t\t\t     void *policy_cb_data);\n+\n /*\n  * Read a reference from the specified reference store, non-recursively.\n  * Set type to describe the reference, and:\n@@ -563,6 +585,14 @@ struct ref_storage_be {\n \tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\n \tverify_refname_available_fn *verify_refname_available;\n+\n+\treflog_iterator_begin_fn *reflog_iterator_begin;\n+\tfor_each_reflog_ent_fn *for_each_reflog_ent;\n+\tfor_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse;\n+\treflog_exists_fn *reflog_exists;\n+\tcreate_reflog_fn *create_reflog;\n+\tdelete_reflog_fn *delete_reflog;\n+\treflog_expire_fn *reflog_expire;\n };\n \n extern struct ref_storage_be refs_be_files;\n-- \n2.8.1\n"},{"id":"288321","messageId":"51a0083346b1107333fd2bf032a0b87f9ab86896.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 33/38] refs: add method for initial ref transaction commit","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:08Z","receivedAt":"2016-06-03T21:04:08Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nSigned-off-by: Ronnie Sahlberg <rsahlberg@google.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               | 8 ++++++++\n refs/files-backend.c | 8 +++++---\n refs/refs-internal.h | 1 +\n 3 files changed, 14 insertions(+), 3 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 77d3986..6c1e899 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1521,3 +1521,11 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n \t\t\t\t       prepare_fn, should_prune_fn,\n \t\t\t\t       cleanup_fn, policy_cb_data);\n }\n+\n+int initial_ref_transaction_commit(struct ref_transaction *transaction,\n+\t\t\t\t   struct strbuf *err)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->initial_transaction_commit(refs, transaction, err);\n+}\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex b19ea02..253899f 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3832,11 +3832,12 @@ static int ref_present(const char *refname,\n \treturn string_list_has_string(affected_refnames, refname);\n }\n \n-int initial_ref_transaction_commit(struct ref_transaction *transaction,\n-\t\t\t\t   struct strbuf *err)\n+static int files_initial_transaction_commit(struct ref_store *ref_store,\n+\t\t\t\t\t    struct ref_transaction *transaction,\n+\t\t\t\t\t    struct strbuf *err)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"initial_ref_transaction_commit\");\n+\t\tfiles_downcast(ref_store, 0, \"initial_ref_transaction_commit\");\n \tint ret = 0, i;\n \tstruct ref_update **updates = transaction->updates;\n \tstruct string_list affected_refnames = STRING_LIST_INIT_NODUP;\n@@ -4064,6 +4065,7 @@ struct ref_storage_be refs_be_files = {\n \t\"files\",\n \tfiles_ref_store_create,\n \tfiles_transaction_commit,\n+\tfiles_initial_transaction_commit,\n \n \tfiles_pack_refs,\n \tfiles_peel_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 7d84984..e462b54 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -577,6 +577,7 @@ struct ref_storage_be {\n \tconst char *name;\n \tref_store_init_fn *init;\n \tref_transaction_commit_fn *transaction_commit;\n+\tref_transaction_commit_fn *initial_transaction_commit;\n \n \tpack_refs_fn *pack_refs;\n \tpeel_ref_fn *peel_ref;\n-- \n2.8.1\n"},{"id":"288329","messageId":"17da5760ac671c98811cc5a3ec46a61f8261d8d9.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 34/38] refs: add method for delete_refs","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:09Z","receivedAt":"2016-06-03T21:04:09Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nIn the file-based backend, delete_refs has some special optimization\nto deal with packed refs.  In other backends, we might be able to make\nref deletion faster by putting all deletions into a single\ntransaction.  So we need a special backend function for this.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\nI think that we could get away without this method if we make\nref_transactions a bit smarter (for example, by supporting best-effort\nupdates that can fail without causing the entire transaction to be\naborted). But that would be a significant detour, so let's leave it\nhere for now.\n\n refs.c               | 7 +++++++\n refs/files-backend.c | 6 ++++--\n refs/refs-internal.h | 3 +++\n 3 files changed, 14 insertions(+), 2 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 6c1e899..8ab9862 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1529,3 +1529,10 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n \n \treturn refs->be->initial_transaction_commit(refs, transaction, err);\n }\n+\n+int delete_refs(struct string_list *refnames, unsigned int flags)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->delete_refs(refs, refnames, flags);\n+}\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 253899f..5681141 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -2455,10 +2455,11 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)\n \treturn 0;\n }\n \n-int delete_refs(struct string_list *refnames, unsigned int flags)\n+static int files_delete_refs(struct ref_store *ref_store,\n+\t\t\t     struct string_list *refnames, unsigned int flags)\n {\n \tstruct files_ref_store *refs =\n-\t\tget_files_ref_store(NULL, \"delete_refs\");\n+\t\tfiles_downcast(ref_store, 0, \"delete_refs\");\n \tstruct strbuf err = STRBUF_INIT;\n \tint i, result = 0;\n \n@@ -4070,6 +4071,7 @@ struct ref_storage_be refs_be_files = {\n \tfiles_pack_refs,\n \tfiles_peel_ref,\n \tfiles_create_symref,\n+\tfiles_delete_refs,\n \n \tfiles_ref_iterator_begin,\n \tfiles_read_raw_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex e462b54..f944b7a 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -485,6 +485,8 @@ typedef int create_symref_fn(struct ref_store *ref_store,\n \t\t\t     const char *ref_target,\n \t\t\t     const char *refs_heads_master,\n \t\t\t     const char *logmsg);\n+typedef int delete_refs_fn(struct ref_store *ref_store,\n+\t\t\t   struct string_list *refnames, unsigned int flags);\n \n /*\n  * Iterate over the references in the specified ref_store that are\n@@ -582,6 +584,7 @@ struct ref_storage_be {\n \tpack_refs_fn *pack_refs;\n \tpeel_ref_fn *peel_ref;\n \tcreate_symref_fn *create_symref;\n+\tdelete_refs_fn *delete_refs;\n \n \tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\n-- \n2.8.1\n"},{"id":"288326","messageId":"5e50cbd4240bd341612254a8a290e7fc008d5326.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 35/38] refs: add methods to init refs db","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:10Z","receivedAt":"2016-06-03T21:04:10Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nAlternate refs backends might not need the refs/heads directory and so\non, so we make ref db initialization part of the backend.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n builtin/init-db.c    | 21 +++++++++++----------\n refs.c               |  8 ++++++++\n refs.h               |  2 ++\n refs/files-backend.c | 18 ++++++++++++++++++\n refs/refs-internal.h |  3 +++\n 5 files changed, 42 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/init-db.c b/builtin/init-db.c\nindex b2d8d40..082fa9f 100644\n--- a/builtin/init-db.c\n+++ b/builtin/init-db.c\n@@ -180,13 +180,7 @@ static int create_default_files(const char *template_path)\n \tchar junk[2];\n \tint reinit;\n \tint filemode;\n-\n-\t/*\n-\t * Create .git/refs/{heads,tags}\n-\t */\n-\tsafe_create_dir(git_path_buf(&buf, \"refs\"), 1);\n-\tsafe_create_dir(git_path_buf(&buf, \"refs/heads\"), 1);\n-\tsafe_create_dir(git_path_buf(&buf, \"refs/tags\"), 1);\n+\tstruct strbuf err = STRBUF_INIT;\n \n \t/* Just look for `init.templatedir` */\n \tgit_config(git_init_db_config, NULL);\n@@ -210,12 +204,19 @@ static int create_default_files(const char *template_path)\n \t */\n \tif (get_shared_repository()) {\n \t\tadjust_shared_perm(get_git_dir());\n-\t\tadjust_shared_perm(git_path_buf(&buf, \"refs\"));\n-\t\tadjust_shared_perm(git_path_buf(&buf, \"refs/heads\"));\n-\t\tadjust_shared_perm(git_path_buf(&buf, \"refs/tags\"));\n \t}\n \n \t/*\n+\t * We need to create a \"refs\" dir in any case so that older\n+\t * versions of git can tell that this is a repository.\n+\t */\n+\tsafe_create_dir(git_path(\"refs\"), 1);\n+\tadjust_shared_perm(git_path(\"refs\"));\n+\n+\tif (refs_init_db(&err))\n+\t\tdie(\"failed to set up refs db: %s\", err.buf);\n+\n+\t/*\n \t * Create the default symlink from \".git/HEAD\" to the \"master\"\n \t * branch, if it does not exist yet.\n \t */\ndiff --git a/refs.c b/refs.c\nindex 8ab9862..7dc67a6 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1292,6 +1292,14 @@ static const char *resolve_ref_recursively(struct ref_store *refs,\n \treturn NULL;\n }\n \n+/* backend functions */\n+int refs_init_db(struct strbuf *err)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->init_db(refs, err);\n+}\n+\n const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n \t\t\t       unsigned char *sha1, int *flags)\n {\ndiff --git a/refs.h b/refs.h\nindex 132dcef..20fae94 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -66,6 +66,8 @@ int ref_exists(const char *refname);\n \n int is_branch(const char *refname);\n \n+extern int refs_init_db(struct strbuf *err);\n+\n /*\n  * If refname is a non-symbolic reference that refers to a tag object,\n  * and the tag can be (recursively) dereferenced to a non-tag object,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 5681141..b2d677e 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -4061,10 +4061,28 @@ static int files_reflog_expire(struct ref_store *ref_store,\n \treturn -1;\n }\n \n+static int files_init_db(struct ref_store *ref_store, struct strbuf *err)\n+{\n+\t/* Check validity (but we don't need the result): */\n+\tfiles_downcast(ref_store, 0, \"init_db\");\n+\n+\t/*\n+\t * Create .git/refs/{heads,tags}\n+\t */\n+\tsafe_create_dir(git_path(\"refs/heads\"), 1);\n+\tsafe_create_dir(git_path(\"refs/tags\"), 1);\n+\tif (get_shared_repository()) {\n+\t\tadjust_shared_perm(git_path(\"refs/heads\"));\n+\t\tadjust_shared_perm(git_path(\"refs/tags\"));\n+\t}\n+\treturn 0;\n+}\n+\n struct ref_storage_be refs_be_files = {\n \tNULL,\n \t\"files\",\n \tfiles_ref_store_create,\n+\tfiles_init_db,\n \tfiles_transaction_commit,\n \tfiles_initial_transaction_commit,\n \ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex f944b7a..6c46353 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -474,6 +474,8 @@ struct ref_store;\n  */\n typedef struct ref_store *ref_store_init_fn(const char *submodule);\n \n+typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);\n+\n typedef int ref_transaction_commit_fn(struct ref_store *refs,\n \t\t\t\t      struct ref_transaction *transaction,\n \t\t\t\t      struct strbuf *err);\n@@ -578,6 +580,7 @@ struct ref_storage_be {\n \tstruct ref_storage_be *next;\n \tconst char *name;\n \tref_store_init_fn *init;\n+\tref_init_db_fn *init_db;\n \tref_transaction_commit_fn *transaction_commit;\n \tref_transaction_commit_fn *initial_transaction_commit;\n \n-- \n2.8.1\n"},{"id":"288327","messageId":"5b60488598626c2230db96e951dbc4ff6201e5f0.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 36/38] refs: add method to rename refs","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:11Z","receivedAt":"2016-06-03T21:04:11Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nThis removes the last caller of function get_files_ref_store(), so\nremove it.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs.c               |  7 +++++++\n refs/files-backend.c | 24 ++++++------------------\n refs/refs-internal.h |  4 ++++\n 3 files changed, 17 insertions(+), 18 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 7dc67a6..17a36fb 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1544,3 +1544,10 @@ int delete_refs(struct string_list *refnames, unsigned int flags)\n \n \treturn refs->be->delete_refs(refs, refnames, flags);\n }\n+\n+int rename_ref(const char *oldref, const char *newref, const char *logmsg)\n+{\n+\tstruct ref_store *refs = get_ref_store(NULL);\n+\n+\treturn refs->be->rename_ref(refs, oldref, newref, logmsg);\n+}\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex b2d677e..d098c7d 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1001,22 +1001,6 @@ static struct files_ref_store *files_downcast(\n \treturn refs;\n }\n \n-/*\n- * Return a pointer to the reference store for the specified\n- * submodule. For the main repository, use submodule==NULL; such a\n- * call cannot fail. For a submodule, the submodule must exist and be\n- * a nonbare repository, otherwise return NULL. Verify that the\n- * reference store is a files_ref_store, and cast it to that type\n- * before returning it.\n- */\n-static struct files_ref_store *get_files_ref_store(const char *submodule,\n-\t\t\t\t\t\t   const char *caller)\n-{\n-\tstruct ref_store *refs = get_ref_store(submodule);\n-\n-\treturn refs ? files_downcast(refs, 1, caller) : NULL;\n-}\n-\n /* The length of a peeled reference line in packed-refs, including EOL: */\n #define PEELED_LINE_LENGTH 42\n \n@@ -2584,9 +2568,12 @@ static int commit_ref_update(struct files_ref_store *refs,\n \t\t\t     const unsigned char *sha1, const char *logmsg,\n \t\t\t     struct strbuf *err);\n \n-int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)\n+static int files_rename_ref(struct ref_store *ref_store,\n+\t\t\t    const char *oldrefname, const char *newrefname,\n+\t\t\t    const char *logmsg)\n {\n-\tstruct files_ref_store *refs = get_files_ref_store(NULL, \"rename_ref\");\n+\tstruct files_ref_store *refs =\n+\t\tfiles_downcast(ref_store, 0, \"rename_ref\");\n \tunsigned char sha1[20], orig_sha1[20];\n \tint flag = 0, logmoved = 0;\n \tstruct ref_lock *lock;\n@@ -4090,6 +4077,7 @@ struct ref_storage_be refs_be_files = {\n \tfiles_peel_ref,\n \tfiles_create_symref,\n \tfiles_delete_refs,\n+\tfiles_rename_ref,\n \n \tfiles_ref_iterator_begin,\n \tfiles_read_raw_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 6c46353..4bfa5ce 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -489,6 +489,9 @@ typedef int create_symref_fn(struct ref_store *ref_store,\n \t\t\t     const char *logmsg);\n typedef int delete_refs_fn(struct ref_store *ref_store,\n \t\t\t   struct string_list *refnames, unsigned int flags);\n+typedef int rename_ref_fn(struct ref_store *ref_store,\n+\t\t\t  const char *oldref, const char *newref,\n+\t\t\t  const char *logmsg);\n \n /*\n  * Iterate over the references in the specified ref_store that are\n@@ -588,6 +591,7 @@ struct ref_storage_be {\n \tpeel_ref_fn *peel_ref;\n \tcreate_symref_fn *create_symref;\n \tdelete_refs_fn *delete_refs;\n+\trename_ref_fn *rename_ref;\n \n \tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\n-- \n2.8.1\n"},{"id":"288328","messageId":"d2a3cb56d7276c00174a08fe609d63e00755b205.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 37/38] refs: make lock generic","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:12Z","receivedAt":"2016-06-03T21:04:12Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nInstead of including a files-backend-specific struct ref_lock, change\nthe generic ref_update struct to include a void pointer that backends\ncan use for their own arbitrary data.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c | 25 +++++++++++++------------\n refs/refs-internal.h |  2 +-\n 2 files changed, 14 insertions(+), 13 deletions(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex d098c7d..53d0ba1 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3523,9 +3523,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \n \tret = lock_raw_ref(refs, update->refname, mustexist,\n \t\t\t   affected_refnames, NULL,\n-\t\t\t   &update->lock, &referent,\n+\t\t\t   &lock, &referent,\n \t\t\t   &update->type, err);\n-\n \tif (ret) {\n \t\tchar *reason;\n \n@@ -3536,7 +3535,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\treturn ret;\n \t}\n \n-\tlock = update->lock;\n+\tupdate->backend_data = lock;\n \n \tif (update->type & REF_ISSYMREF) {\n \t\tif (update->flags & REF_NODEREF) {\n@@ -3591,7 +3590,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\tfor (parent_update = update->parent_update;\n \t\t     parent_update;\n \t\t     parent_update = parent_update->parent_update) {\n-\t\t\toidcpy(&parent_update->lock->old_oid, &lock->old_oid);\n+\t\t\tstruct ref_lock *parent_lock = parent_update->backend_data;\n+\t\t\toidcpy(&parent_lock->old_oid, &lock->old_oid);\n \t\t}\n \n \t\tif ((update->flags & REF_HAVE_OLD) &&\n@@ -3626,7 +3626,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n \t\t\t * The lock was freed upon failure of\n \t\t\t * write_ref_to_lockfile():\n \t\t\t */\n-\t\t\tupdate->lock = NULL;\n+\t\t\tupdate->backend_data = NULL;\n \t\t\tstrbuf_addf(err,\n \t\t\t\t    \"cannot update the ref '%s': %s\",\n \t\t\t\t    update->refname, write_err);\n@@ -3745,7 +3745,7 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \t/* Perform updates first so live commits remain referenced */\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = updates[i];\n-\t\tstruct ref_lock *lock = update->lock;\n+\t\tstruct ref_lock *lock = update->backend_data;\n \n \t\tif (update->flags & REF_NEEDS_COMMIT ||\n \t\t    update->flags & REF_LOG_ONLY) {\n@@ -3758,7 +3758,7 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \t\t\t\t\t    lock->ref_name, old_msg);\n \t\t\t\tfree(old_msg);\n \t\t\t\tunlock_ref(lock);\n-\t\t\t\tupdate->lock = NULL;\n+\t\t\t\tupdate->backend_data = NULL;\n \t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n \t\t\t\tgoto cleanup;\n \t\t\t}\n@@ -3768,7 +3768,7 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \t\t\tif (commit_ref(lock)) {\n \t\t\t\tstrbuf_addf(err, \"couldn't set '%s'\", lock->ref_name);\n \t\t\t\tunlock_ref(lock);\n-\t\t\t\tupdate->lock = NULL;\n+\t\t\t\tupdate->backend_data = NULL;\n \t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n \t\t\t\tgoto cleanup;\n \t\t\t}\n@@ -3777,17 +3777,18 @@ static int files_transaction_commit(struct ref_store *ref_store,\n \t/* Perform deletes now that updates are safely completed */\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = updates[i];\n+\t\tstruct ref_lock *lock = update->backend_data;\n \n \t\tif (update->flags & REF_DELETING &&\n \t\t    !(update->flags & REF_LOG_ONLY)) {\n-\t\t\tif (delete_ref_loose(update->lock, update->type, err)) {\n+\t\t\tif (delete_ref_loose(lock, update->type, err)) {\n \t\t\t\tret = TRANSACTION_GENERIC_ERROR;\n \t\t\t\tgoto cleanup;\n \t\t\t}\n \n \t\t\tif (!(update->flags & REF_ISPRUNING))\n \t\t\t\tstring_list_append(&refs_to_delete,\n-\t\t\t\t\t\t   update->lock->ref_name);\n+\t\t\t\t\t\t   lock->ref_name);\n \t\t}\n \t}\n \n@@ -3803,8 +3804,8 @@ cleanup:\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n \n \tfor (i = 0; i < transaction->nr; i++)\n-\t\tif (updates[i]->lock)\n-\t\t\tunlock_ref(updates[i]->lock);\n+\t\tif (updates[i]->backend_data)\n+\t\t\tunlock_ref(updates[i]->backend_data);\n \tstring_list_clear(&refs_to_delete, 0);\n \tfree(head_ref);\n \tstring_list_clear(&affected_refnames, 0);\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 4bfa5ce..db891e6 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -162,7 +162,7 @@ struct ref_update {\n \t */\n \tunsigned int flags;\n \n-\tstruct ref_lock *lock;\n+\tvoid *backend_data;\n \tunsigned int type;\n \tchar *msg;\n \n-- \n2.8.1\n"},{"id":"288332","messageId":"adf0c96e684d56943f6b4a62a91de549facad0e7.1464983301.git.mhagger@alum.mit.edu","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"[PATCH 38/38] refs: implement iteration over only per-worktree refs","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-03T21:04:13Z","receivedAt":"2016-06-03T21:04:13Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"From: David Turner <dturner@twopensource.com>\n\nAlternate refs backends might still use files to store per-worktree\nrefs. So provide a way to iterate over only the per-worktree references\nin a ref_store. The other backend can set up a files ref_store and\niterate using the new DO_FOR_EACH_PER_WORKTREE_ONLY flag when iterating.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n---\n refs/files-backend.c |  4 ++++\n refs/refs-internal.h | 10 +++++++++-\n 2 files changed, 13 insertions(+), 1 deletion(-)\n\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 53d0ba1..b5ad21c 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1802,6 +1802,10 @@ static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)\n \tint ok;\n \n \twhile ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {\n+\t\tif (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&\n+\t\t    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)\n+\t\t\tcontinue;\n+\n \t\tif (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&\n \t\t    !ref_resolves_to_object(iter->iter0->refname,\n \t\t\t\t\t    iter->iter0->oid,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex db891e6..6d97ed9 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -462,10 +462,18 @@ extern struct ref_iterator *current_ref_iter;\n int do_for_each_ref_iterator(struct ref_iterator *iter,\n \t\t\t     each_ref_fn fn, void *cb_data);\n \n-/* refs backends */\n+/*\n+ * Only include per-worktree refs in a do_for_each_ref*() iteration.\n+ * Normally this will be used with a files ref_store, since that's\n+ * where all reference backends will presumably store their\n+ * per-worktree refs.\n+ */\n+#define DO_FOR_EACH_PER_WORKTREE_ONLY 0x02\n \n struct ref_store;\n \n+/* refs backends */\n+\n /*\n  * Initialize the ref_store for the specified submodule, or for the\n  * main repository if submodule == NULL. These functions should call\n-- \n2.8.1\n"},{"id":"288617","messageId":"xmqqfusprmeq.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"8ee7d7359f6763cba29dc788aba2f236204bb76e.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T16:31:09Z","receivedAt":"2016-06-07T16:31:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This is a tangent, but your series that ends at 4aa2c475 (grep: -W:\ndon't extend context to trailing empty lines, 2016-05-28) does not\nseem to have much effect when viewing the change to refs.c this\npatch makes (it modifies a function in an early part, and then adds\nbunch of new functions at the end) with \"git show -W\".\n\nThanks.\n\nMichael Haggerty <mhagger@alum.mit.edu> writes:\n\n> We want ref_stores to be polymorphic, so invent a base class of which\n> files_ref_store is a derived class. For now there is a one-to-one\n> relationship between ref_stores and submodules.\n>\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n>  refs.c               |  91 ++++++++++++++++++++++++++\n>  refs/files-backend.c | 179 +++++++++++++++++++++++++++++----------------------\n>  refs/refs-internal.h |  78 ++++++++++++++++++++++\n>  3 files changed, 271 insertions(+), 77 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index f57a93e..5e20ae0 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1151,8 +1151,12 @@ int head_ref(each_ref_fn fn, void *cb_data)\n>  static int do_for_each_ref(const char *submodule, const char *prefix,\n>  \t\t\t   each_ref_fn fn, int trim, int flags, void *cb_data)\n>  {\n> +\tstruct ref_store *refs = get_ref_store(submodule);\n>  \tstruct ref_iterator *iter;\n>  \n> +\tif (!refs)\n> +\t\treturn 0;\n> +\n>  \titer = files_ref_iterator_begin(submodule, prefix, flags);\n>  \titer = prefix_ref_iterator_begin(iter, prefix, trim);\n>  \n> @@ -1284,3 +1288,90 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n>  \terrno = ELOOP;\n>  \treturn NULL;\n>  }\n> +\n> +static struct ref_store *main_ref_store = NULL;\n> +\n> +static struct ref_store *submodule_ref_stores = NULL;\n> +\n> +void base_ref_store_init(struct ref_store *refs,\n> +\t\t\t const struct ref_storage_be *be,\n> +\t\t\t const char *submodule)\n> +{\n> +\trefs->be = be;\n> +\tif (!submodule) {\n> +\t\tif (main_ref_store)\n> +\t\t\tdie(\"BUG: main_ref_store initialized twice\");\n> +\n> +\t\trefs->submodule = \"\";\n> +\t\trefs->next = NULL;\n> +\t\tmain_ref_store = refs;\n> +\t} else {\n> +\t\tif (lookup_ref_store(submodule))\n> +\t\t\tdie(\"BUG: ref_store for submodule '%s' initialized twice\",\n> +\t\t\t    submodule);\n> +\n> +\t\trefs->submodule = xstrdup(submodule);\n> +\t\trefs->next = submodule_ref_stores;\n> +\t\tsubmodule_ref_stores = refs;\n> +\t}\n> +}\n> +\n> +struct ref_store *ref_store_init(const char *submodule)\n> +{\n> +\tconst char *be_name = \"files\";\n> +\tstruct ref_storage_be *be = find_ref_storage_backend(be_name);\n> +\n> +\tif (!be)\n> +\t\tdie(\"BUG: reference backend %s is unknown\", be_name);\n> +\n> +\tif (!submodule || !*submodule)\n> +\t\treturn be->init(NULL);\n> +\telse\n> +\t\treturn be->init(submodule);\n> +}\n> +\n> +struct ref_store *lookup_ref_store(const char *submodule)\n> +{\n> +\tstruct ref_store *refs;\n> +\n> +\tif (!submodule || !*submodule)\n> +\t\treturn main_ref_store;\n> +\n> +\tfor (refs = submodule_ref_stores; refs; refs = refs->next) {\n> +\t\tif (!strcmp(submodule, refs->submodule))\n> +\t\t\treturn refs;\n> +\t}\n> +\n> +\treturn NULL;\n> +}\n> +\n> +struct ref_store *get_ref_store(const char *submodule)\n> +{\n> +\tstruct ref_store *refs;\n> +\n> +\tif (!submodule || !*submodule) {\n> +\t\trefs = lookup_ref_store(NULL);\n> +\n> +\t\tif (!refs)\n> +\t\t\trefs = ref_store_init(NULL);\n> +\t} else {\n> +\t\trefs = lookup_ref_store(submodule);\n> +\n> +\t\tif (!refs) {\n> +\t\t\tstruct strbuf submodule_sb = STRBUF_INIT;\n> +\n> +\t\t\tstrbuf_addstr(&submodule_sb, submodule);\n> +\t\t\tif (is_nonbare_repository_dir(&submodule_sb))\n> +\t\t\t\trefs = ref_store_init(submodule);\n> +\t\t\tstrbuf_release(&submodule_sb);\n> +\t\t}\n> +\t}\n> +\n> +\treturn refs;\n> +}\n> +\n> +void assert_main_repository(struct ref_store *refs, const char *caller)\n> +{\n> +\tif (*refs->submodule)\n> +\t\tdie(\"BUG: %s called for a submodule\", caller);\n> +}\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index bde6f0e..8ef79c2 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -910,17 +910,11 @@ struct packed_ref_cache {\n>   * Future: need to be in \"struct repository\"\n>   * when doing a full libification.\n>   */\n> -static struct files_ref_store {\n> -\tstruct files_ref_store *next;\n> +struct files_ref_store {\n> +\tstruct ref_store base;\n>  \tstruct ref_entry *loose;\n>  \tstruct packed_ref_cache *packed;\n> -\t/*\n> -\t * The submodule name, or \"\" for the main repo. We allocate\n> -\t * length 1 rather than FLEX_ARRAY so that the main\n> -\t * files_ref_store is initialized correctly.\n> -\t */\n> -\tchar name[1];\n> -} ref_store, *submodule_ref_stores;\n> +};\n>  \n>  /* Lock used for the main packed-refs file: */\n>  static struct lock_file packlock;\n> @@ -973,53 +967,54 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)\n>   * Create a new submodule ref cache and add it to the internal\n>   * set of caches.\n>   */\n> -static struct files_ref_store *create_ref_store(const char *submodule)\n> +static struct ref_store *files_ref_store_create(const char *submodule)\n>  {\n> -\tstruct files_ref_store *refs;\n> -\tif (!submodule)\n> -\t\tsubmodule = \"\";\n> -\tFLEX_ALLOC_STR(refs, name, submodule);\n> -\trefs->next = submodule_ref_stores;\n> -\tsubmodule_ref_stores = refs;\n> -\treturn refs;\n> +\tstruct files_ref_store *refs = xcalloc(1, sizeof(*refs));\n> +\tstruct ref_store *ref_store = (struct ref_store *)refs;\n> +\n> +\tbase_ref_store_init(ref_store, &refs_be_files, submodule);\n> +\n> +\treturn ref_store;\n>  }\n>  \n> -static struct files_ref_store *lookup_ref_store(const char *submodule)\n> +/*\n> + * Downcast ref_store to files_ref_store. Die if ref_store is not a\n> + * files_ref_store. If submodule_allowed is not true, then also die if\n> + * files_ref_store is for a submodule (i.e., not for the main\n> + * repository). caller is used in any necessary error messages.\n> + */\n> +static struct files_ref_store *files_downcast(\n> +\t\tstruct ref_store *ref_store, int submodule_allowed,\n> +\t\tconst char *caller)\n>  {\n>  \tstruct files_ref_store *refs;\n>  \n> -\tif (!submodule || !*submodule)\n> -\t\treturn &ref_store;\n> +\tif (ref_store->be != &refs_be_files)\n> +\t\tdie(\"BUG: ref_store is type \\\"%s\\\" not \\\"files\\\" in %s\",\n> +\t\t    ref_store->be->name, caller);\n>  \n> -\tfor (refs = submodule_ref_stores; refs; refs = refs->next)\n> -\t\tif (!strcmp(submodule, refs->name))\n> -\t\t\treturn refs;\n> -\treturn NULL;\n> +\trefs = (struct files_ref_store *)ref_store;\n> +\n> +\tif (!submodule_allowed)\n> +\t\tassert_main_repository(ref_store, caller);\n> +\n> +\treturn refs;\n>  }\n>  \n>  /*\n> - * Return a pointer to a files_ref_store for the specified submodule. For\n> - * the main repository, use submodule==NULL; such a call cannot fail.\n> - * For a submodule, the submodule must exist and be a nonbare\n> - * repository, otherwise return NULL.\n> - *\n> - * The returned structure will be allocated and initialized but not\n> - * necessarily populated; it should not be freed.\n> + * Return a pointer to the reference store for the specified\n> + * submodule. For the main repository, use submodule==NULL; such a\n> + * call cannot fail. For a submodule, the submodule must exist and be\n> + * a nonbare repository, otherwise return NULL. Verify that the\n> + * reference store is a files_ref_store, and cast it to that type\n> + * before returning it.\n>   */\n> -static struct files_ref_store *get_ref_store(const char *submodule)\n> +static struct files_ref_store *get_files_ref_store(const char *submodule,\n> +\t\t\t\t\t\t   const char *caller)\n>  {\n> -\tstruct files_ref_store *refs = lookup_ref_store(submodule);\n> +\tstruct ref_store *refs = get_ref_store(submodule);\n>  \n> -\tif (!refs) {\n> -\t\tstruct strbuf submodule_sb = STRBUF_INIT;\n> -\n> -\t\tstrbuf_addstr(&submodule_sb, submodule);\n> -\t\tif (is_nonbare_repository_dir(&submodule_sb))\n> -\t\t\trefs = create_ref_store(submodule);\n> -\t\tstrbuf_release(&submodule_sb);\n> -\t}\n> -\n> -\treturn refs;\n> +\treturn refs ? files_downcast(refs, 1, caller) : NULL;\n>  }\n>  \n>  /* The length of a peeled reference line in packed-refs, including EOL: */\n> @@ -1158,8 +1153,9 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref\n>  {\n>  \tchar *packed_refs_file;\n>  \n> -\tif (*refs->name)\n> -\t\tpacked_refs_file = git_pathdup_submodule(refs->name, \"packed-refs\");\n> +\tif (*refs->base.submodule)\n> +\t\tpacked_refs_file = git_pathdup_submodule(refs->base.submodule,\n> +\t\t\t\t\t\t\t \"packed-refs\");\n>  \telse\n>  \t\tpacked_refs_file = git_pathdup(\"packed-refs\");\n>  \n> @@ -1202,8 +1198,9 @@ static struct ref_dir *get_packed_refs(struct files_ref_store *refs)\n>   */\n>  static void add_packed_ref(const char *refname, const unsigned char *sha1)\n>  {\n> -\tstruct packed_ref_cache *packed_ref_cache =\n> -\t\tget_packed_ref_cache(&ref_store);\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"add_packed_ref\");\n> +\tstruct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);\n>  \n>  \tif (!packed_ref_cache->lock)\n>  \t\tdie(\"internal error: packed refs not locked\");\n> @@ -1226,8 +1223,8 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)\n>  \tstruct strbuf path = STRBUF_INIT;\n>  \tsize_t path_baselen;\n>  \n> -\tif (*refs->name)\n> -\t\tstrbuf_git_path_submodule(&path, refs->name, \"%s\", dirname);\n> +\tif (*refs->base.submodule)\n> +\t\tstrbuf_git_path_submodule(&path, refs->base.submodule, \"%s\", dirname);\n>  \telse\n>  \t\tstrbuf_git_path(&path, \"%s\", dirname);\n>  \tpath_baselen = path.len;\n> @@ -1262,10 +1259,10 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)\n>  \t\t} else {\n>  \t\t\tint read_ok;\n>  \n> -\t\t\tif (*refs->name) {\n> +\t\t\tif (*refs->base.submodule) {\n>  \t\t\t\thashclr(sha1);\n>  \t\t\t\tflag = 0;\n> -\t\t\t\tread_ok = !resolve_gitlink_ref(refs->name,\n> +\t\t\t\tread_ok = !resolve_gitlink_ref(refs->base.submodule,\n>  \t\t\t\t\t\t\t       refname.buf, sha1);\n>  \t\t\t} else {\n>  \t\t\t\tread_ok = !read_ref_full(refname.buf,\n> @@ -1355,8 +1352,8 @@ static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,\n>  \n>  \tif (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)\n>  \t\treturn -1;\n> -\tpath = *refs->name\n> -\t\t? git_pathdup_submodule(refs->name, \"%s\", refname)\n> +\tpath = *refs->base.submodule\n> +\t\t? git_pathdup_submodule(refs->base.submodule, \"%s\", refname)\n>  \t\t: git_pathdup(\"%s\", refname);\n>  \tfd = open(path, O_RDONLY);\n>  \tfree(path);\n> @@ -1397,7 +1394,7 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n>  \t\treturn -1;\n>  \n>  \tstrbuf_add(&submodule, path, len);\n> -\trefs = get_ref_store(submodule.buf);\n> +\trefs = get_files_ref_store(submodule.buf, \"resolve_gitlink_ref\");\n>  \tif (!refs) {\n>  \t\tstrbuf_release(&submodule);\n>  \t\treturn -1;\n> @@ -1413,7 +1410,10 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh\n>   */\n>  static struct ref_entry *get_packed_ref(const char *refname)\n>  {\n> -\treturn find_ref(get_packed_refs(&ref_store), refname);\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"get_packed_ref\");\n> +\n> +\treturn find_ref(get_packed_refs(refs), refname);\n>  }\n>  \n>  /*\n> @@ -1613,6 +1613,8 @@ static int lock_raw_ref(const char *refname, int mustexist,\n>  \t\t\tunsigned int *type,\n>  \t\t\tstruct strbuf *err)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"lock_raw_ref\");\n>  \tstruct ref_lock *lock;\n>  \tstruct strbuf ref_file = STRBUF_INIT;\n>  \tint attempts_remaining = 3;\n> @@ -1745,7 +1747,7 @@ retry:\n>  \t\t\t\t\t\t\t  REMOVE_DIR_EMPTY_ONLY)) {\n>  \t\t\t\tif (verify_refname_available_dir(\n>  \t\t\t\t\t\t    refname, extras, skip,\n> -\t\t\t\t\t\t    get_loose_refs(&ref_store),\n> +\t\t\t\t\t\t    get_loose_refs(refs),\n>  \t\t\t\t\t\t    err)) {\n>  \t\t\t\t\t/*\n>  \t\t\t\t\t * The error message set by\n> @@ -1784,7 +1786,7 @@ retry:\n>  \t\t */\n>  \t\tif (verify_refname_available_dir(\n>  \t\t\t\t    refname, extras, skip,\n> -\t\t\t\t    get_packed_refs(&ref_store),\n> +\t\t\t\t    get_packed_refs(refs),\n>  \t\t\t\t    err)) {\n>  \t\t\tgoto error_return;\n>  \t\t}\n> @@ -1942,7 +1944,8 @@ struct ref_iterator *files_ref_iterator_begin(\n>  \t\tconst char *submodule,\n>  \t\tconst char *prefix, unsigned int flags)\n>  {\n> -\tstruct files_ref_store *refs = get_ref_store(submodule);\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(submodule, \"ref_iterator_begin\");\n>  \tstruct ref_dir *loose_dir, *packed_dir;\n>  \tstruct ref_iterator *loose_iter, *packed_iter;\n>  \tstruct files_ref_iterator *iter;\n> @@ -2065,6 +2068,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>  \t\t\t\t\t    unsigned int flags, int *type,\n>  \t\t\t\t\t    struct strbuf *err)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"lock_ref_sha1_basic\");\n>  \tstruct strbuf ref_file = STRBUF_INIT;\n>  \tstruct ref_lock *lock;\n>  \tint last_errno = 0;\n> @@ -2095,8 +2100,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>  \t\t */\n>  \t\tif (remove_empty_directories(&ref_file)) {\n>  \t\t\tlast_errno = errno;\n> -\t\t\tif (!verify_refname_available_dir(refname, extras, skip,\n> -\t\t\t\t\t\t\t  get_loose_refs(&ref_store), err))\n> +\t\t\tif (!verify_refname_available_dir(\n> +\t\t\t\t\t    refname, extras, skip,\n> +\t\t\t\t\t    get_loose_refs(refs), err))\n>  \t\t\t\tstrbuf_addf(err, \"there are still refs under '%s'\",\n>  \t\t\t\t\t    refname);\n>  \t\t\tgoto error_return;\n> @@ -2107,8 +2113,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>  \tif (!resolved) {\n>  \t\tlast_errno = errno;\n>  \t\tif (last_errno != ENOTDIR ||\n> -\t\t    !verify_refname_available_dir(refname, extras, skip,\n> -\t\t\t\t\t\t  get_loose_refs(&ref_store), err))\n> +\t\t    !verify_refname_available_dir(\n> +\t\t\t\t    refname, extras, skip,\n> +\t\t\t\t    get_loose_refs(refs), err))\n>  \t\t\tstrbuf_addf(err, \"unable to resolve reference '%s': %s\",\n>  \t\t\t\t    refname, strerror(last_errno));\n>  \n> @@ -2123,7 +2130,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,\n>  \t */\n>  \tif (is_null_oid(&lock->old_oid) &&\n>  \t    verify_refname_available_dir(refname, extras, skip,\n> -\t\t\t\t\t get_packed_refs(&ref_store), err)) {\n> +\t\t\t\t\t get_packed_refs(refs),\n> +\t\t\t\t\t err)) {\n>  \t\tlast_errno = ENOTDIR;\n>  \t\tgoto error_return;\n>  \t}\n> @@ -2212,9 +2220,10 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)\n>   */\n>  static int lock_packed_refs(int flags)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"lock_packed_refs\");\n>  \tstatic int timeout_configured = 0;\n>  \tstatic int timeout_value = 1000;\n> -\n>  \tstruct packed_ref_cache *packed_ref_cache;\n>  \n>  \tif (!timeout_configured) {\n> @@ -2232,7 +2241,7 @@ static int lock_packed_refs(int flags)\n>  \t * this will automatically invalidate the cache and re-read\n>  \t * the packed-refs file.\n>  \t */\n> -\tpacked_ref_cache = get_packed_ref_cache(&ref_store);\n> +\tpacked_ref_cache = get_packed_ref_cache(refs);\n>  \tpacked_ref_cache->lock = &packlock;\n>  \t/* Increment the reference count to prevent it from being freed: */\n>  \tacquire_packed_ref_cache(packed_ref_cache);\n> @@ -2247,8 +2256,10 @@ static int lock_packed_refs(int flags)\n>   */\n>  static int commit_packed_refs(void)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"commit_packed_refs\");\n>  \tstruct packed_ref_cache *packed_ref_cache =\n> -\t\tget_packed_ref_cache(&ref_store);\n> +\t\tget_packed_ref_cache(refs);\n>  \tint error = 0;\n>  \tint save_errno = 0;\n>  \tFILE *out;\n> @@ -2281,15 +2292,17 @@ static int commit_packed_refs(void)\n>   */\n>  static void rollback_packed_refs(void)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"rollback_packed_refs\");\n>  \tstruct packed_ref_cache *packed_ref_cache =\n> -\t\tget_packed_ref_cache(&ref_store);\n> +\t\tget_packed_ref_cache(refs);\n>  \n>  \tif (!packed_ref_cache->lock)\n>  \t\tdie(\"internal error: packed-refs not locked\");\n>  \trollback_lock_file(packed_ref_cache->lock);\n>  \tpacked_ref_cache->lock = NULL;\n>  \trelease_packed_ref_cache(packed_ref_cache);\n> -\tclear_packed_ref_cache(&ref_store);\n> +\tclear_packed_ref_cache(refs);\n>  }\n>  \n>  struct ref_to_prune {\n> @@ -2422,15 +2435,17 @@ static void prune_refs(struct ref_to_prune *r)\n>  \n>  int pack_refs(unsigned int flags)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"pack_refs\");\n>  \tstruct pack_refs_cb_data cbdata;\n>  \n>  \tmemset(&cbdata, 0, sizeof(cbdata));\n>  \tcbdata.flags = flags;\n>  \n>  \tlock_packed_refs(LOCK_DIE_ON_ERROR);\n> -\tcbdata.packed_refs = get_packed_refs(&ref_store);\n> +\tcbdata.packed_refs = get_packed_refs(refs);\n>  \n> -\tdo_for_each_entry_in_dir(get_loose_refs(&ref_store), 0,\n> +\tdo_for_each_entry_in_dir(get_loose_refs(refs), 0,\n>  \t\t\t\t pack_if_possible_fn, &cbdata);\n>  \n>  \tif (commit_packed_refs())\n> @@ -2449,6 +2464,8 @@ int pack_refs(unsigned int flags)\n>   */\n>  static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"repack_without_refs\");\n>  \tstruct ref_dir *packed;\n>  \tstruct string_list_item *refname;\n>  \tint ret, needs_repacking = 0, removed = 0;\n> @@ -2471,7 +2488,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)\n>  \t\tunable_to_lock_message(git_path(\"packed-refs\"), errno, err);\n>  \t\treturn -1;\n>  \t}\n> -\tpacked = get_packed_refs(&ref_store);\n> +\tpacked = get_packed_refs(refs);\n>  \n>  \t/* Remove refnames from the cache */\n>  \tfor_each_string_list_item(refname, refnames)\n> @@ -2616,8 +2633,10 @@ int verify_refname_available(const char *newname,\n>  \t\t\t     const struct string_list *skip,\n>  \t\t\t     struct strbuf *err)\n>  {\n> -\tstruct ref_dir *packed_refs = get_packed_refs(&ref_store);\n> -\tstruct ref_dir *loose_refs = get_loose_refs(&ref_store);\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"verify_refname_available\");\n> +\tstruct ref_dir *packed_refs = get_packed_refs(refs);\n> +\tstruct ref_dir *loose_refs = get_loose_refs(refs);\n>  \n>  \tif (verify_refname_available_dir(newname, extras, skip,\n>  \t\t\t\t\t packed_refs, err) ||\n> @@ -2968,7 +2987,10 @@ static int commit_ref_update(struct ref_lock *lock,\n>  \t\t\t     const unsigned char *sha1, const char *logmsg,\n>  \t\t\t     struct strbuf *err)\n>  {\n> -\tclear_loose_ref_cache(&ref_store);\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"commit_ref_update\");\n> +\n> +\tclear_loose_ref_cache(refs);\n>  \tif (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {\n>  \t\tchar *old_msg = strbuf_detach(err, NULL);\n>  \t\tstrbuf_addf(err, \"cannot update the ref '%s': %s\",\n> @@ -3683,6 +3705,8 @@ static int lock_ref_for_update(struct ref_update *update,\n>  int ref_transaction_commit(struct ref_transaction *transaction,\n>  \t\t\t   struct strbuf *err)\n>  {\n> +\tstruct files_ref_store *refs =\n> +\t\tget_files_ref_store(NULL, \"ref_transaction_commit\");\n>  \tint ret = 0, i;\n>  \tstruct ref_update **updates = transaction->updates;\n>  \tstruct string_list refs_to_delete = STRING_LIST_INIT_NODUP;\n> @@ -3790,7 +3814,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>  \t\t\t}\n>  \t\t}\n>  \t\tif (update->flags & REF_NEEDS_COMMIT) {\n> -\t\t\tclear_loose_ref_cache(&ref_store);\n> +\t\t\tclear_loose_ref_cache(refs);\n>  \t\t\tif (commit_ref(lock)) {\n>  \t\t\t\tstrbuf_addf(err, \"couldn't set '%s'\", lock->ref_name);\n>  \t\t\t\tunlock_ref(lock);\n> @@ -3823,7 +3847,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>  \t}\n>  \tfor_each_string_list_item(ref_to_delete, &refs_to_delete)\n>  \t\tunlink_or_warn(git_path(\"logs/%s\", ref_to_delete->string));\n> -\tclear_loose_ref_cache(&ref_store);\n> +\tclear_loose_ref_cache(refs);\n>  \n>  cleanup:\n>  \ttransaction->state = REF_TRANSACTION_CLOSED;\n> @@ -4069,5 +4093,6 @@ int reflog_expire(const char *refname, const unsigned char *sha1,\n>  \n>  struct ref_storage_be refs_be_files = {\n>  \tNULL,\n> -\t\"files\"\n> +\t\"files\",\n> +\tfiles_ref_store_create\n>  };\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> index 4d88849..d6c74cf 100644\n> --- a/refs/refs-internal.h\n> +++ b/refs/refs-internal.h\n> @@ -521,11 +521,89 @@ int read_raw_ref(const char *refname, unsigned char *sha1,\n>  \t\t struct strbuf *referent, unsigned int *type);\n>  \n>  /* refs backends */\n> +\n> +/*\n> + * Initialize the ref_store for the specified submodule, or for the\n> + * main repository if submodule == NULL. These functions should call\n> + * base_ref_store_init() to initialize the shared part of the\n> + * ref_store and to record the ref_store for later lookup.\n> + */\n> +typedef struct ref_store *ref_store_init_fn(const char *submodule);\n> +\n>  struct ref_storage_be {\n>  \tstruct ref_storage_be *next;\n>  \tconst char *name;\n> +\tref_store_init_fn *init;\n>  };\n>  \n>  extern struct ref_storage_be refs_be_files;\n>  \n> +/*\n> + * A representation of the reference store for the main repository or\n> + * a submodule. The ref_store instances for submodules are kept in a\n> + * linked list.\n> + */\n> +struct ref_store {\n> +\t/* The backend describing this ref_store's storage scheme: */\n> +\tconst struct ref_storage_be *be;\n> +\n> +\t/*\n> +\t * The name of the submodule represented by this object, or\n> +\t * the empty string if it represents the main repository's\n> +\t * reference store:\n> +\t */\n> +\tconst char *submodule;\n> +\n> +\t/*\n> +\t * Submodule reference store instances are stored in a linked\n> +\t * list using this pointer.\n> +\t */\n> +\tstruct ref_store *next;\n> +};\n> +\n> +/*\n> + * Fill in the generic part of refs for the specified submodule and\n> + * add it to our collection of reference stores.\n> + */\n> +void base_ref_store_init(struct ref_store *refs,\n> +\t\t\t const struct ref_storage_be *be,\n> +\t\t\t const char *submodule);\n> +\n> +/*\n> + * Create, record, and return a ref_store instance for the specified\n> + * submodule (or the main repository if submodule is NULL).\n> + *\n> + * For backwards compatibility, submodule==\"\" is treated the same as\n> + * submodule==NULL.\n> + */\n> +struct ref_store *ref_store_init(const char *submodule);\n> +\n> +/*\n> + * Return the ref_store instance for the specified submodule (or the\n> + * main repository if submodule is NULL). If that ref_store hasn't\n> + * been initialized yet, return NULL.\n> + *\n> + * For backwards compatibility, submodule==\"\" is treated the same as\n> + * submodule==NULL.\n> + */\n> +struct ref_store *lookup_ref_store(const char *submodule);\n> +\n> +/*\n> + * Return the ref_store instance for the specified submodule. For the\n> + * main repository, use submodule==NULL; such a call cannot fail. For\n> + * a submodule, the submodule must exist and be a nonbare repository,\n> + * otherwise return NULL. If the requested reference store has not yet\n> + * been initialized, initialize it first.\n> + *\n> + * For backwards compatibility, submodule==\"\" is treated the same as\n> + * submodule==NULL.\n> + */\n> +struct ref_store *get_ref_store(const char *submodule);\n> +\n> +/*\n> + * Die if refs is for a submodule (i.e., not for the main repository).\n> + * caller is used in any necessary error messages.\n> + */\n> +void assert_main_repository(struct ref_store *refs, const char *caller);\n> +\n>  #endif /* REFS_REFS_INTERNAL_H */\n"},{"id":"288619","messageId":"xmqqbn3drkwd.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"8ee7d7359f6763cba29dc788aba2f236204bb76e.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T17:03:46Z","receivedAt":"2016-06-07T17:03:46Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> We want ref_stores to be polymorphic, so invent a base class of which\n> files_ref_store is a derived class. For now there is a one-to-one\n> relationship between ref_stores and submodules.\n\nThe mention of \"submodules\" made me go \"Huh?\" but thinking about it\nfor a second it is clear and obvious.  We often peek into refs in a\ndifferent repository that is a submodule, and we do not mix them with\nour own refs.  Logically that is what a \"ref store\" is, and one-to-one\nrelationship is expected.\n\n> @@ -1284,3 +1288,90 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n>  \terrno = ELOOP;\n>  \treturn NULL;\n>  }\n> +\n> +static struct ref_store *main_ref_store = NULL;\n> +\n> +static struct ref_store *submodule_ref_stores = NULL;\n\nLet's let BSS take care of these initialization.\n\n> +/*\n> + * Downcast ref_store to files_ref_store. Die if ref_store is not a\n> + * files_ref_store. If submodule_allowed is not true, then also die if\n> + * files_ref_store is for a submodule (i.e., not for the main\n> + * repository). caller is used in any necessary error messages.\n> + */\n> +static struct files_ref_store *files_downcast(\n> +\t\tstruct ref_store *ref_store, int submodule_allowed,\n> +\t\tconst char *caller)\n>  {\n>  \tstruct files_ref_store *refs;\n>  \n> +\tif (ref_store->be != &refs_be_files)\n> +\t\tdie(\"BUG: ref_store is type \\\"%s\\\" not \\\"files\\\" in %s\",\n> +\t\t    ref_store->be->name, caller);\n>  \n> +\trefs = (struct files_ref_store *)ref_store;\n> +\n> +\tif (!submodule_allowed)\n> +\t\tassert_main_repository(ref_store, caller);\n> +\n> +\treturn refs;\n>  }\n>\n>  /*\n> + * Return a pointer to the reference store for the specified\n> + * submodule. For the main repository, use submodule==NULL; such a\n> + * call cannot fail. For a submodule, the submodule must exist and be\n> + * a nonbare repository, otherwise return NULL. Verify that the\n> + * reference store is a files_ref_store, and cast it to that type\n> + * before returning it.\n>   */\n> +static struct files_ref_store *get_files_ref_store(const char *submodule,\n> +\t\t\t\t\t\t   const char *caller)\n>  {\n> +\tstruct ref_store *refs = get_ref_store(submodule);\n>  \n> +\treturn refs ? files_downcast(refs, 1, caller) : NULL;\n>  }\n\nThis comment may be barking up a wrong tree, but the support for\nclass inheritance makes me wonder if I can do something along this\nline:\n\n * implement a filesystem based ref_store, that is very similar to\n   what you have as files_ref_store, but \n\n   - when storing a ref as a loose ref, or when checking if a ref\n     exists as a loose ref, quote them somehow (e.g. a branch\n     \"Branch\" is not stored as a file \"$GIT_DIR/refs/heads/branch\"\n     but by using \"^\" as a single shift marker, i.e. as\n     \"$GIT_DIR/refs/heads/^branch\");\n\n   - when enumerating what refs we have as loose refs, unquote what\n     readdir(3) gave us, e.g. seeing \"$GIT_DIR/refs/heads/^branch\",\n     I say \"there is a branch whose name is 'Branch'\".\n\n * as locking and other 'methods' are likely to be very similar to\n   your files_ref_store, make this new backend as a subclass of it,\n   i.e. create a new class but function pointers to many methods are\n   copied from files ref_store vtable.\n\nWould the strict \"when downcasting to 'files', we make sure vtable\nis that of 'files' backend\" interfere with such an approach?\n"},{"id":"288620","messageId":"xmqq7fe0syq7.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"a9787723bcd990413661b8cb2e5542d618ab6321.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 16/38] resolve_gitlink_ref(): implement using resolve_ref_recursively()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T17:19:44Z","receivedAt":"2016-06-07T17:19:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> resolve_ref_recursively() can handle references in arbitrary files\n> reference stores, so use it to resolve \"gitlink\" (i.e., submodule)\n> references. Aside from removing redundant code, this allows submodule\n> lookups to benefit from the much more robust code that we use for\n> reading non-submodule references. And, since the code is now agnostic\n> about reference backends, it will work for any future references\n> backend (so move its definition to refs.c).\n\nNice.\n"},{"id":"288621","messageId":"xmqq37oosya6.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"c126a2bf5e1f48faf48b3b4ee7cc599313a76b99.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 17/38] resolve_gitlink_ref(): avoid memory allocation in many cases","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T17:29:21Z","receivedAt":"2016-06-07T17:29:21Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> If we don't have to strip trailing '/' from the submodule path, then\n> don't allocate and copy the submodule name.\n\nMakes sense.\n\n>  int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n>  {\n> -\tint len = strlen(path);\n> -\tstruct strbuf submodule = STRBUF_INIT;\n> +\tsize_t len, orig_len = strlen(path);\n>  \tstruct ref_store *refs;\n>  \tint flags;\n>  \n> -\twhile (len && path[len-1] == '/')\n> -\t\tlen--;\n> +\tfor (len = orig_len; len && path[len - 1] == '/'; len--)\n> +\t\t;\n> +\n>  \tif (!len)\n>  \t\treturn -1;\n>  \n> -\tstrbuf_add(&submodule, path, len);\n> -\trefs = get_ref_store(submodule.buf);\n> -\tstrbuf_release(&submodule);\n> +\tif (len == orig_len) {\n\nYou can keep the original while (), without introducing orig_len,\nand check if path[len] is NUL, which would probably be an end result\nthat is easier to read.\n\n> +\t\trefs = get_ref_store(path);\n> +\t} else {\n> +\t\tchar *stripped = xmemdupz(path, len);\n> +\n> +\t\trefs = get_ref_store(stripped);\n> +\t\tfree(stripped);\n\nAn alternative might be to add get_ref_store_counted() that takes\n(path, len) instead of NUL-terminated path, which does not look too\nbad looking at the state after applying all 38 patches.\n\n> +\t}\n> +\n>  \tif (!refs)\n>  \t\treturn -1;\n"},{"id":"288622","messageId":"xmqqy46grjg5.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"22e49ca0f50831538d07d115d56b4af4e457a80c.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 21/38] refs: make pack_refs() virtual","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T17:35:06Z","receivedAt":"2016-06-07T17:35:06Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n>  refs.c               | 7 +++++++\n>  refs/files-backend.c | 6 ++++--\n>  refs/refs-internal.h | 4 ++++\n>  3 files changed, 15 insertions(+), 2 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index 79ef443..f4f5f32 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1418,6 +1418,13 @@ void assert_main_repository(struct ref_store *refs, const char *caller)\n>  }\n>  \n>  /* backend functions */\n> +int pack_refs(unsigned int flags)\n> +{\n> +\tstruct ref_store *refs = get_ref_store(NULL);\n> +\n> +\treturn refs->be->pack_refs(refs, flags);\n> +}\n> +\n\nMakes me wonder what it even means to \"pack_refs\" in the context of\nother possible backends (e.g. lmdb), but higher level API users\n(e.g. \"gc\") needs something to call to give the backend \"here is a\nchance for you to optimize yourself\" cue, so perhaps it is OK.\n\n>  int ref_transaction_commit(struct ref_transaction *transaction,\n>  \t\t\t   struct strbuf *err)\n>  {\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index af8de85..e5a8799 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -2358,10 +2358,10 @@ static void prune_refs(struct ref_to_prune *r)\n>  \t}\n>  }\n>  \n> -int pack_refs(unsigned int flags)\n> +static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)\n>  {\n>  \tstruct files_ref_store *refs =\n> -\t\tget_files_ref_store(NULL, \"pack_refs\");\n> +\t\tfiles_downcast(ref_store, 0, \"pack_refs\");\n>  \tstruct pack_refs_cb_data cbdata;\n>  \n>  \tmemset(&cbdata, 0, sizeof(cbdata));\n> @@ -4026,6 +4026,8 @@ struct ref_storage_be refs_be_files = {\n>  \tfiles_ref_store_create,\n>  \tfiles_transaction_commit,\n>  \n> +\tfiles_pack_refs,\n> +\n>  \tfiles_read_raw_ref,\n>  \tfiles_verify_refname_available\n>  };\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> index 18e01d0..578c125 100644\n> --- a/refs/refs-internal.h\n> +++ b/refs/refs-internal.h\n> @@ -495,6 +495,8 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,\n>  \t\t\t\t      struct ref_transaction *transaction,\n>  \t\t\t\t      struct strbuf *err);\n>  \n> +typedef int pack_refs_fn(struct ref_store *ref_store, unsigned int flags);\n> +\n>  /*\n>   * Read a reference from the specified reference store, non-recursively.\n>   * Set type to describe the reference, and:\n> @@ -549,6 +551,8 @@ struct ref_storage_be {\n>  \tref_store_init_fn *init;\n>  \tref_transaction_commit_fn *transaction_commit;\n>  \n> +\tpack_refs_fn *pack_refs;\n> +\n>  \tread_raw_ref_fn *read_raw_ref;\n>  \tverify_refname_available_fn *verify_refname_available;\n>  };\n"},{"id":"288623","messageId":"xmqqtwh4rjdh.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"316f0fed796fd2ed17ea5df4c264a98028d6b98a.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 23/38] refs: make peel_ref() virtual","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T17:36:42Z","receivedAt":"2016-06-07T17:36:42Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> For now it only supports the main reference store.\n\nIsn't this comment applicable to a handful of recent changes that\nmade other things virtual, too?  Just wondering if I am missing\nsomething very special with the peel_ref() thing to single it out.\n>\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n>  refs.c               | 7 +++++++\n>  refs/files-backend.c | 6 ++++--\n>  refs/refs-internal.h | 3 +++\n>  3 files changed, 14 insertions(+), 2 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index 22837f4..2d84c5c 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1425,6 +1425,13 @@ int pack_refs(unsigned int flags)\n>  \treturn refs->be->pack_refs(refs, flags);\n>  }\n>  \n> +int peel_ref(const char *refname, unsigned char *sha1)\n> +{\n> +\tstruct ref_store *refs = get_ref_store(NULL);\n> +\n> +\treturn refs->be->peel_ref(refs, refname, sha1);\n> +}\n> +\n>  int create_symref(const char *ref_target, const char *refs_heads_master,\n>  \t\t  const char *logmsg)\n>  {\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index f82a1be..91dcfcb 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -1763,9 +1763,10 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)\n>  \treturn status;\n>  }\n>  \n> -int peel_ref(const char *refname, unsigned char *sha1)\n> +static int files_peel_ref(struct ref_store *ref_store,\n> +\t\t\t  const char *refname, unsigned char *sha1)\n>  {\n> -\tstruct files_ref_store *refs = get_files_ref_store(NULL, \"peel_ref\");\n> +\tstruct files_ref_store *refs = files_downcast(ref_store, 0, \"peel_ref\");\n>  \tint flag;\n>  \tunsigned char base[20];\n>  \n> @@ -4031,6 +4032,7 @@ struct ref_storage_be refs_be_files = {\n>  \tfiles_transaction_commit,\n>  \n>  \tfiles_pack_refs,\n> +\tfiles_peel_ref,\n>  \tfiles_create_symref,\n>  \n>  \tfiles_read_raw_ref,\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> index c342e57..ae67b49 100644\n> --- a/refs/refs-internal.h\n> +++ b/refs/refs-internal.h\n> @@ -496,6 +496,8 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs,\n>  \t\t\t\t      struct strbuf *err);\n>  \n>  typedef int pack_refs_fn(struct ref_store *ref_store, unsigned int flags);\n> +typedef int peel_ref_fn(struct ref_store *ref_store,\n> +\t\t\tconst char *refname, unsigned char *sha1);\n>  typedef int create_symref_fn(struct ref_store *ref_store,\n>  \t\t\t     const char *ref_target,\n>  \t\t\t     const char *refs_heads_master,\n> @@ -556,6 +558,7 @@ struct ref_storage_be {\n>  \tref_transaction_commit_fn *transaction_commit;\n>  \n>  \tpack_refs_fn *pack_refs;\n> +\tpeel_ref_fn *peel_ref;\n>  \tcreate_symref_fn *create_symref;\n>  \n>  \tread_raw_ref_fn *read_raw_ref;\n"},{"id":"288624","messageId":"xmqqporsrj21.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"17da5760ac671c98811cc5a3ec46a61f8261d8d9.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 34/38] refs: add method for delete_refs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T17:43:34Z","receivedAt":"2016-06-07T17:43:34Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> From: David Turner <dturner@twopensource.com>\n>\n> In the file-based backend, delete_refs has some special optimization\n> to deal with packed refs.  In other backends, we might be able to make\n> ref deletion faster by putting all deletions into a single\n> transaction.  So we need a special backend function for this.\n>\n> Signed-off-by: David Turner <dturner@twopensource.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n> I think that we could get away without this method if we make\n> ref_transactions a bit smarter (for example, by supporting best-effort\n> updates that can fail without causing the entire transaction to be\n> aborted). But that would be a significant detour, so let's leave it\n> here for now.\n\nHmm, I actually was wondering why 'pack without' was there while\nreading 24/38; IIUC, that is one of the \"special optimization\" that\nis very much tied to the files backend, and it may make sense to\nhide it behind delete_refs() as its implementation detail.\n\nWhich is exactly what this step is about, so I am happy ;-)\n\nUnlike other changes like the ones that did read_raw_ref(),\nverify_refname_available(), etc., the title does not follow the\npattern \"refs: make X() virtual\", even though as far as I can see\nthe intent is the same as others.  Perhaps a minor retitle is in\norder?\n\n>  refs.c               | 7 +++++++\n>  refs/files-backend.c | 6 ++++--\n>  refs/refs-internal.h | 3 +++\n>  3 files changed, 14 insertions(+), 2 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index 6c1e899..8ab9862 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1529,3 +1529,10 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,\n>  \n>  \treturn refs->be->initial_transaction_commit(refs, transaction, err);\n>  }\n> +\n> +int delete_refs(struct string_list *refnames, unsigned int flags)\n> +{\n> +\tstruct ref_store *refs = get_ref_store(NULL);\n> +\n> +\treturn refs->be->delete_refs(refs, refnames, flags);\n> +}\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index 253899f..5681141 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -2455,10 +2455,11 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)\n>  \treturn 0;\n>  }\n>  \n> -int delete_refs(struct string_list *refnames, unsigned int flags)\n> +static int files_delete_refs(struct ref_store *ref_store,\n> +\t\t\t     struct string_list *refnames, unsigned int flags)\n>  {\n>  \tstruct files_ref_store *refs =\n> -\t\tget_files_ref_store(NULL, \"delete_refs\");\n> +\t\tfiles_downcast(ref_store, 0, \"delete_refs\");\n>  \tstruct strbuf err = STRBUF_INIT;\n>  \tint i, result = 0;\n>  \n> @@ -4070,6 +4071,7 @@ struct ref_storage_be refs_be_files = {\n>  \tfiles_pack_refs,\n>  \tfiles_peel_ref,\n>  \tfiles_create_symref,\n> +\tfiles_delete_refs,\n>  \n>  \tfiles_ref_iterator_begin,\n>  \tfiles_read_raw_ref,\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> index e462b54..f944b7a 100644\n> --- a/refs/refs-internal.h\n> +++ b/refs/refs-internal.h\n> @@ -485,6 +485,8 @@ typedef int create_symref_fn(struct ref_store *ref_store,\n>  \t\t\t     const char *ref_target,\n>  \t\t\t     const char *refs_heads_master,\n>  \t\t\t     const char *logmsg);\n> +typedef int delete_refs_fn(struct ref_store *ref_store,\n> +\t\t\t   struct string_list *refnames, unsigned int flags);\n>  \n>  /*\n>   * Iterate over the references in the specified ref_store that are\n> @@ -582,6 +584,7 @@ struct ref_storage_be {\n>  \tpack_refs_fn *pack_refs;\n>  \tpeel_ref_fn *peel_ref;\n>  \tcreate_symref_fn *create_symref;\n> +\tdelete_refs_fn *delete_refs;\n>  \n>  \tref_iterator_begin_fn *iterator_begin;\n>  \tread_raw_ref_fn *read_raw_ref;\n"},{"id":"288625","messageId":"xmqqlh2griq3.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"d2a3cb56d7276c00174a08fe609d63e00755b205.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 37/38] refs: make lock generic","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T17:50:44Z","receivedAt":"2016-06-07T17:50:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> From: David Turner <dturner@twopensource.com>\n>\n> Instead of including a files-backend-specific struct ref_lock, change\n> the generic ref_update struct to include a void pointer that backends\n> can use for their own arbitrary data.\n\nHmph.\n\n> @@ -3591,7 +3590,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n>  \t\tfor (parent_update = update->parent_update;\n>  \t\t     parent_update;\n>  \t\t     parent_update = parent_update->parent_update) {\n> -\t\t\toidcpy(&parent_update->lock->old_oid, &lock->old_oid);\n> +\t\t\tstruct ref_lock *parent_lock = parent_update->backend_data;\n> +\t\t\toidcpy(&parent_lock->old_oid, &lock->old_oid);\n>  \t\t}\n> ...\n> @@ -3745,7 +3745,7 @@ static int files_transaction_commit(struct ref_store *ref_store,\n>  \t/* Perform updates first so live commits remain referenced */\n>  \tfor (i = 0; i < transaction->nr; i++) {\n>  \t\tstruct ref_update *update = updates[i];\n> -\t\tstruct ref_lock *lock = update->lock;\n> +\t\tstruct ref_lock *lock = update->backend_data;\n\nOK, and files_* backend method downcasts it to what it wants, which\nis good.\n"},{"id":"288626","messageId":"xmqq7fe0rhts.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"5eb96cc20c0f76aff39d1556803ebd4f9f422cea.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 02/38] rename_ref_available(): add docstring","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-07T18:10:07Z","receivedAt":"2016-06-07T18:10:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> From: David Turner <dturner@twopensource.com>\n>\n> Signed-off-by: David Turner <dturner@twopensource.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n>  refs/refs-internal.h | 5 +++++\n>  1 file changed, 5 insertions(+)\n>\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> index efe5847..d8a2606 100644\n> --- a/refs/refs-internal.h\n> +++ b/refs/refs-internal.h\n> @@ -240,6 +240,11 @@ const char *find_descendant_ref(const char *dirname,\n>  \t\t\t\tconst struct string_list *extras,\n>  \t\t\t\tconst struct string_list *skip);\n>  \n> +/*\n> + * Check if the new name does not conflict with any existing refs\n> + * (other than possibly the old ref).  Return 0 if the ref can be\n> + * renamed to the new name.\n> + */\n>  int rename_ref_available(const char *oldname, const char *newname);\n\nI do not quite understand the comment.  Partly because it is unclear\nwhat \"conflict\" means here, but I guess it means a D/F conflict that\nis explained near verify_refname_available()?\n\nA new name can conflict with an existing, possibly old ref?  Are you\nreferring to this condition?\n\n    You are trying to rename \"refs/a/b\" to \"refs/a\", which would\n    conflict, but as long as there is no other ref that share the\n    prefix \"refs/a/\", e.g. \"refs/a/c\", the new name \"refs/a\" is\n    available.\n\nI wonder if it is necessary to document that this function is not\nmeant to protect us from others racing with us.  That is, when you\nare renaming something to \"refs/a\", you call this function and it\nfinds, by calling verify_refname_available(), that the repository\nhas nothing that conflicts with the name and says \"OK\", but before\nyou actually do the rename, somebody may push from sideways to\ncreate \"refs/a/b\", making the result of an earlier check with this\nfunction invalid.\n\nOr is this to be called only under a lock that protects us from such\na race?\n"},{"id":"288807","messageId":"57596A7E.9020407@alum.mit.edu","threadId":"42524","inReplyTo":"xmqq7fe0rhts.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 02/38] rename_ref_available(): add docstring","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T13:09:18Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 08:10 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> From: David Turner <dturner@twopensource.com>\n>>\n>> Signed-off-by: David Turner <dturner@twopensource.com>\n>> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n>> ---\n>>  refs/refs-internal.h | 5 +++++\n>>  1 file changed, 5 insertions(+)\n>>\n>> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n>> index efe5847..d8a2606 100644\n>> --- a/refs/refs-internal.h\n>> +++ b/refs/refs-internal.h\n>> @@ -240,6 +240,11 @@ const char *find_descendant_ref(const char *dirname,\n>>  \t\t\t\tconst struct string_list *extras,\n>>  \t\t\t\tconst struct string_list *skip);\n>>  \n>> +/*\n>> + * Check if the new name does not conflict with any existing refs\n>> + * (other than possibly the old ref).  Return 0 if the ref can be\n>> + * renamed to the new name.\n>> + */\n>>  int rename_ref_available(const char *oldname, const char *newname);\n> \n> I do not quite understand the comment.  Partly because it is unclear\n> what \"conflict\" means here, but I guess it means a D/F conflict that\n> is explained near verify_refname_available()?\n\nYes.\n\n> A new name can conflict with an existing, possibly old ref?  Are you\n> referring to this condition?\n> \n>     You are trying to rename \"refs/a/b\" to \"refs/a\", which would\n>     conflict, but as long as there is no other ref that share the\n>     prefix \"refs/a/\", e.g. \"refs/a/c\", the new name \"refs/a\" is\n>     available.\n\nThat is correct.\n\n> I wonder if it is necessary to document that this function is not\n> meant to protect us from others racing with us.  That is, when you\n> are renaming something to \"refs/a\", you call this function and it\n> finds, by calling verify_refname_available(), that the repository\n> has nothing that conflicts with the name and says \"OK\", but before\n> you actually do the rename, somebody may push from sideways to\n> create \"refs/a/b\", making the result of an earlier check with this\n> function invalid.\n> \n> Or is this to be called only under a lock that protects us from such\n> a race?\n\nIt would be really awkward (maybe impossible?) to guard against all such\nraces even using locks. One problem is that the lockfiles for the old\nand new refnames would themselves, in some cases, not be able to coexist\ndue to D/F conflicts. Also, there is no way to prevent the creation of\n\"any reference in `refs/a/*`\" except by creating the reference `refs/a`\n(the presence of `refs/a.lock` is not enough), but by that time it is\ntoo late.\n\nIn the end, this function mostly exists as a pre-check that\n`rename_ref()` is *likely* to succeed, so that the latter function is\nless likely to detect a problem after it has started moving things\naround. But `rename_ref()` is the final arbiter and is a bit more robust\nthan this check.\n\nI also noticed that the docstring in this patch got the polarity of the\nreturn value backwards.\n\nI propose to change the parameter names to `old_refname` and\n`new_refname` and to change the docstring to\n\n/*\n * Check whether an attempt to rename old_refname to new_refname would\n * cause a D/F conflict with any existing reference (other than\n * possibly old_refname). If there would be a conflict, emit an error\n * message and return false; otherwise, return true.\n *\n * Note that this function is not safe against all races with other\n * processes (though rename_ref() catches some races that might get by\n * this check).\n */\n\nDoes that sound good?\n\nMichael\n"},{"id":"288808","messageId":"575978DA.30608@alum.mit.edu","threadId":"42524","inReplyTo":"xmqqbn3drkwd.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T14:10:34Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 07:03 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> We want ref_stores to be polymorphic, so invent a base class of which\n>> files_ref_store is a derived class. For now there is a one-to-one\n>> relationship between ref_stores and submodules.\n> \n> The mention of \"submodules\" made me go \"Huh?\" but thinking about it\n> for a second it is clear and obvious.  We often peek into refs in a\n> different repository that is a submodule, and we do not mix them with\n> our own refs.  Logically that is what a \"ref store\" is, and one-to-one\n> relationship is expected.\n> \n>> @@ -1284,3 +1288,90 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n>>  \terrno = ELOOP;\n>>  \treturn NULL;\n>>  }\n>> +\n>> +static struct ref_store *main_ref_store = NULL;\n>> +\n>> +static struct ref_store *submodule_ref_stores = NULL;\n> \n> Let's let BSS take care of these initialization.\n\nI like the `= NULL` because it expresses \"yes, I care about the initial\nvalues of these variables\", which to me is more valuable than saving a\nfew bytes in the size of the executable. But in fact, GCC does the\nobvious optimization: it detects that these variables are being\ninitialized to zero, and puts them in BSS anyway. I'd be surprised if\nother compilers don't do the same. So I'd prefer to leave this as-is, if\nit's OK with you.\n\n>> [...]\n>>  /*\n>> + * Return a pointer to the reference store for the specified\n>> + * submodule. For the main repository, use submodule==NULL; such a\n>> + * call cannot fail. For a submodule, the submodule must exist and be\n>> + * a nonbare repository, otherwise return NULL. Verify that the\n>> + * reference store is a files_ref_store, and cast it to that type\n>> + * before returning it.\n>>   */\n>> +static struct files_ref_store *get_files_ref_store(const char *submodule,\n>> +\t\t\t\t\t\t   const char *caller)\n>>  {\n>> +\tstruct ref_store *refs = get_ref_store(submodule);\n>>  \n>> +\treturn refs ? files_downcast(refs, 1, caller) : NULL;\n>>  }\n> \n> This comment may be barking up a wrong tree, but the support for\n> class inheritance makes me wonder if I can do something along this\n> line:\n> \n>  * implement a filesystem based ref_store, that is very similar to\n>    what you have as files_ref_store, but \n> \n>    - when storing a ref as a loose ref, or when checking if a ref\n>      exists as a loose ref, quote them somehow (e.g. a branch\n>      \"Branch\" is not stored as a file \"$GIT_DIR/refs/heads/branch\"\n>      but by using \"^\" as a single shift marker, i.e. as\n>      \"$GIT_DIR/refs/heads/^branch\");\n> \n>    - when enumerating what refs we have as loose refs, unquote what\n>      readdir(3) gave us, e.g. seeing \"$GIT_DIR/refs/heads/^branch\",\n>      I say \"there is a branch whose name is 'Branch'\".\n> \n>  * as locking and other 'methods' are likely to be very similar to\n>    your files_ref_store, make this new backend as a subclass of it,\n>    i.e. create a new class but function pointers to many methods are\n>    copied from files ref_store vtable.\n\nThat is definitely something we could implement in the future. If I were\ngoing to design an extension like this, I think I'd go straight to\nsomething more expressive; maybe something like URL-encoding. For\nexample, we might like a system that can record refnames with a Unicode\nencoding that is determined by us rather than by the filesystem.\n\nDepending on the details, it might be preferable to implement the new\nref-store as an encoding layer that delegates to an old-fashioned files\nbackend rather than using inheritance. In fact, you'd only want to do\nthe translation for the loose storage part, so in the end the\nimplemention might look something like\n\n    overlay_ref_store(\n        encoding_ref_store(loose_ref_store()),\n        packed_ref_store())\n\n> Would the strict \"when downcasting to 'files', we make sure vtable\n> is that of 'files' backend\" interfere with such an approach?\n\nTrue, the simple approach that I use above doesn't generalize to\nimplementation inheritance. But I'd rather cross that bridge when we\ncome to it. Implementing an RTTI system in C is a bit ambitious and, I\nthink, comes with runtime costs.\n\nMichael\n"},{"id":"288810","messageId":"57597F1A.3060400@alum.mit.edu","threadId":"42524","inReplyTo":"xmqq37oosya6.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 17/38] resolve_gitlink_ref(): avoid memory allocation in many cases","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T14:37:14Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 07:29 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> If we don't have to strip trailing '/' from the submodule path, then\n>> don't allocate and copy the submodule name.\n> \n> Makes sense.\n> \n>>  int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n>>  {\n>> -\tint len = strlen(path);\n>> -\tstruct strbuf submodule = STRBUF_INIT;\n>> +\tsize_t len, orig_len = strlen(path);\n>>  \tstruct ref_store *refs;\n>>  \tint flags;\n>>  \n>> -\twhile (len && path[len-1] == '/')\n>> -\t\tlen--;\n>> +\tfor (len = orig_len; len && path[len - 1] == '/'; len--)\n>> +\t\t;\n>> +\n>>  \tif (!len)\n>>  \t\treturn -1;\n>>  \n>> -\tstrbuf_add(&submodule, path, len);\n>> -\trefs = get_ref_store(submodule.buf);\n>> -\tstrbuf_release(&submodule);\n>> +\tif (len == orig_len) {\n> \n> You can keep the original while (), without introducing orig_len,\n> and check if path[len] is NUL, which would probably be an end result\n> that is easier to read.\n\nOK, I'll change it.\n\n>> +\t\trefs = get_ref_store(path);\n>> +\t} else {\n>> +\t\tchar *stripped = xmemdupz(path, len);\n>> +\n>> +\t\trefs = get_ref_store(stripped);\n>> +\t\tfree(stripped);\n> \n> An alternative might be to add get_ref_store_counted() that takes\n> (path, len) instead of NUL-terminated path, which does not look too\n> bad looking at the state after applying all 38 patches.\n\nThis slash-stripping code was introduced in 2007 (0ebde32c87) and it's\nnot my priority to improve it as part of this patch series.\n\nMichael\n"},{"id":"288811","messageId":"57598155.3040101@alum.mit.edu","threadId":"42524","inReplyTo":"xmqqy46grjg5.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 21/38] refs: make pack_refs() virtual","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T14:46:45Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 07:35 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n>> ---\n>>  refs.c               | 7 +++++++\n>>  refs/files-backend.c | 6 ++++--\n>>  refs/refs-internal.h | 4 ++++\n>>  3 files changed, 15 insertions(+), 2 deletions(-)\n>>\n>> diff --git a/refs.c b/refs.c\n>> index 79ef443..f4f5f32 100644\n>> --- a/refs.c\n>> +++ b/refs.c\n>> @@ -1418,6 +1418,13 @@ void assert_main_repository(struct ref_store *refs, const char *caller)\n>>  }\n>>  \n>>  /* backend functions */\n>> +int pack_refs(unsigned int flags)\n>> +{\n>> +\tstruct ref_store *refs = get_ref_store(NULL);\n>> +\n>> +\treturn refs->be->pack_refs(refs, flags);\n>> +}\n>> +\n> \n> Makes me wonder what it even means to \"pack_refs\" in the context of\n> other possible backends (e.g. lmdb), but higher level API users\n> (e.g. \"gc\") needs something to call to give the backend \"here is a\n> chance for you to optimize yourself\" cue, so perhaps it is OK.\n\nMy thinking exactly. This might end up being renamed to optimize_refs()\nand be made an optional part of the ref_store interface. But it seemed\npremature to worry about that.\n\nMichael\n"},{"id":"288812","messageId":"575985C8.9040108@alum.mit.edu","threadId":"42524","inReplyTo":"xmqqtwh4rjdh.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 23/38] refs: make peel_ref() virtual","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T15:05:44Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 07:36 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> For now it only supports the main reference store.\n> \n> Isn't this comment applicable to a handful of recent changes that\n> made other things virtual, too?  Just wondering if I am missing\n> something very special with the peel_ref() thing to single it out.\n\nIt is true that many (most?) virtual functions can currently only be\nused with the main reference store. That is likely to stay the case for\nreference-writing functions, because currently there isn't any code that\nwants to write references in other submodule. (Or maybe there is, but\nit's currently done by invoking a subprocess? I haven't actually looked.)\n\nBut peel_ref() is a read-only function, and it seems more plausible to\nimplement it for other submodules. It just seems like work that can be\nput off. And there's the kindof ugly current_ref_iter hack that might go\naway if callers are rewritten to use ref_iterators, in which case\nimplementing this method for other submodules would become even easier.\n\nIn other words, the main point of the comment is not \"it only supports\nthe main reference store\", but rather \"for now\".\n\nMichael\n"},{"id":"288813","messageId":"xmqqh9d2jt6x.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"57596A7E.9020407@alum.mit.edu","subject":"Re: [PATCH 02/38] rename_ref_available(): add docstring","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-09T15:08:38Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> I propose to change the parameter names to `old_refname` and\n> `new_refname` and to change the docstring to\n> ...\n> Does that sound good?\n\nYes.\n"},{"id":"288814","messageId":"575987E1.9060200@alum.mit.edu","threadId":"42524","inReplyTo":"xmqqporsrj21.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 34/38] refs: add method for delete_refs","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T15:14:41Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 07:43 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> From: David Turner <dturner@twopensource.com>\n>>\n>> In the file-based backend, delete_refs has some special optimization\n>> to deal with packed refs.  In other backends, we might be able to make\n>> ref deletion faster by putting all deletions into a single\n>> transaction.  So we need a special backend function for this.\n>>\n>> Signed-off-by: David Turner <dturner@twopensource.com>\n>> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n>> ---\n>> I think that we could get away without this method if we make\n>> ref_transactions a bit smarter (for example, by supporting best-effort\n>> updates that can fail without causing the entire transaction to be\n>> aborted). But that would be a significant detour, so let's leave it\n>> here for now.\n> \n> Hmm, I actually was wondering why 'pack without' was there while\n> reading 24/38; IIUC, that is one of the \"special optimization\" that\n> is very much tied to the files backend, and it may make sense to\n> hide it behind delete_refs() as its implementation detail.\n> \n> Which is exactly what this step is about, so I am happy ;-)\n\nIn the future I think this optimization will be even better hidden,\nnamely within the transaction_commit method of a packed_refs_store class :-)\n\n> Unlike other changes like the ones that did read_raw_ref(),\n> verify_refname_available(), etc., the title does not follow the\n> pattern \"refs: make X() virtual\", even though as far as I can see\n> the intent is the same as others.  Perhaps a minor retitle is in\n> order?\n\nOK.\n\nMichael\n"},{"id":"288815","messageId":"5759897A.7040502@alum.mit.edu","threadId":"42524","inReplyTo":"xmqqlh2griq3.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 37/38] refs: make lock generic","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T15:21:30Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 07:50 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>> From: David Turner <dturner@twopensource.com>\n>>\n>> Instead of including a files-backend-specific struct ref_lock, change\n>> the generic ref_update struct to include a void pointer that backends\n>> can use for their own arbitrary data.\n> \n> Hmph.\n\nI don't know what your comment means. This step is a consequence of the\ndesign decision to stick with a single ref_transaction class that is\nused by all ref_stores, which was nice because it avoided the need to\nvirtualize the functions\nref_transaction_{begin,update,create,delete,verify}.\n\n>> @@ -3591,7 +3590,8 @@ static int lock_ref_for_update(struct files_ref_store *refs,\n>>  \t\tfor (parent_update = update->parent_update;\n>>  \t\t     parent_update;\n>>  \t\t     parent_update = parent_update->parent_update) {\n>> -\t\t\toidcpy(&parent_update->lock->old_oid, &lock->old_oid);\n>> +\t\t\tstruct ref_lock *parent_lock = parent_update->backend_data;\n>> +\t\t\toidcpy(&parent_lock->old_oid, &lock->old_oid);\n>>  \t\t}\n>> ...\n>> @@ -3745,7 +3745,7 @@ static int files_transaction_commit(struct ref_store *ref_store,\n>>  \t/* Perform updates first so live commits remain referenced */\n>>  \tfor (i = 0; i < transaction->nr; i++) {\n>>  \t\tstruct ref_update *update = updates[i];\n>> -\t\tstruct ref_lock *lock = update->lock;\n>> +\t\tstruct ref_lock *lock = update->backend_data;\n> \n> OK, and files_* backend method downcasts it to what it wants, which\n> is good.\n\nMichael\n"},{"id":"288817","messageId":"575990FB.5000908@alum.mit.edu","threadId":"42524","inReplyTo":"xmqqlh2griq3.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 37/38] refs: make lock generic","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-09T15:53:31Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/07/2016 07:50 PM, Junio C Hamano wrote:\n> [...]\n\nThanks for all your comments, Junio. I've pushed a revised version of\nthe patch series to my GitHub fork [1] as branch \"ref-store\", but I'll\nwait for a little longer to see if there are more comments on the list\nbefore sending a re-roll.\n\nMichael\n\n[1] https://github.com/mhagger/git\n"},{"id":"288819","messageId":"xmqq60tijq4g.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"575978DA.30608@alum.mit.edu","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-09T16:14:55Z","receivedAt":"2016-06-16T02:19:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n>>> +\n>>> +static struct ref_store *main_ref_store = NULL;\n>>> +\n>>> +static struct ref_store *submodule_ref_stores = NULL;\n>> \n>> Let's let BSS take care of these initialization.\n>\n> I like the `= NULL` because it expresses \"yes, I care about the initial\n> values of these variables\", which to me is more valuable than saving a\n> few bytes in the size of the executable. But in fact, GCC does the\n> obvious optimization: it detects that these variables are being\n> initialized to zero, and puts them in BSS anyway. I'd be surprised if\n> other compilers don't do the same. So I'd prefer to leave this as-is, if\n> it's OK with you.\n\nSorry; I shouldn't have phrased it with BSS, because the main point\nof the convention is not about \"saving bytes by placing it in BSS\".\n\nLack of \"= ...\" is a clear-enough clue that the code wants these\npointers initialized to NULL.  And in this snippet:\n\n\tstatic struct foo the_foo;\n\n        static struct foo *default_foo = &the_foo;\n        static struct foo *other_foo;\n\nwe want the presence of \"=\" as a clue that something special is\ngoing on only for default_foo (it is not initialied to the nul value\nfor the type like usual static variables).  If you wrote it this way,\n\n        static struct foo *default_foo = &the_foo;\n        static struct foo *other_foo = NULL;\n\nthe reader has to say \"something funny going on about other_foo?\"\nwhen scanning up to \"other_foo =\" and then \"... ah, no that is just\nwasted typing and reading, it is left to NULL after all\".\n\nSo, no, it is not OK with me.\n\nOtherwise I would have said \"I wonder if ...\" ;-)\n"},{"id":"288854","messageId":"5759E5A8.9060000@web.de","threadId":"42524","inReplyTo":"xmqqfusprmeq.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2016-06-09T21:54:48Z","receivedAt":"2016-06-16T02:19:50Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 07.06.2016 um 18:31 schrieb Junio C Hamano:\n> This is a tangent, but your series that ends at 4aa2c475 (grep: -W:\n> don't extend context to trailing empty lines, 2016-05-28) does not\n> seem to have much effect when viewing the change to refs.c this\n> patch makes (it modifies a function in an early part, and then adds\n> bunch of new functions at the end) with \"git show -W\".\n> \n> Thanks.\n\nGood catch, thanks!\n\n-- >8 --\nSubject: [PATCH] xdiff: fix merging of appended hunk with -W\n\nWhen -W is given we search the lines between the end of the current\ncontext and the next change for a function line.  If there is none then\nwe merge those two hunks as they must be part of the same function.\n\nIf the next change is an appended chunk we abort the search early in\nget_func_line(), however, because its line number is out of range.  Fix\nthat by searching from the end of the pre-image in that case instead.\n\nReported-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n t/t4051-diff-function-context.sh | 24 +++++++++++++++++++++++-\n xdiff/xemit.c                    |  3 ++-\n 2 files changed, 25 insertions(+), 2 deletions(-)\n\ndiff --git a/t/t4051-diff-function-context.sh b/t/t4051-diff-function-context.sh\nindex b6bb04a..b79b877 100755\n--- a/t/t4051-diff-function-context.sh\n+++ b/t/t4051-diff-function-context.sh\n@@ -64,7 +64,13 @@ test_expect_success 'setup' '\n \n \tgrep -v \"Begin of second part\" <file.c >file.c.new &&\n \tmv file.c.new file.c &&\n-\tcommit_and_tag long_common_tail file.c\n+\tcommit_and_tag long_common_tail file.c &&\n+\n+\tgit checkout initial &&\n+\tgrep -v \"delete me from hello\" <file.c >file.c.new &&\n+\tmv file.c.new file.c &&\n+\tcat \"$dir/appended1.c\" >>file.c &&\n+\tcommit_and_tag changed_hello_appended file.c\n '\n \n check_diff changed_hello 'changed function'\n@@ -157,4 +163,20 @@ test_expect_success ' context does not include preceding empty lines' '\n \ttest \"$(first_context_line <long_common_tail.diff.diff)\" != \" \"\n '\n \n+check_diff changed_hello_appended 'changed function plus appended function'\n+\n+test_expect_success ' context includes begin' '\n+\tgrep \"^ .*Begin of hello\" changed_hello_appended.diff &&\n+\tgrep \"^[+].*Begin of first part\" changed_hello_appended.diff\n+'\n+\n+test_expect_success ' context includes end' '\n+\tgrep \"^ .*End of hello\" changed_hello_appended.diff &&\n+\tgrep \"^[+].*End of first part\" changed_hello_appended.diff\n+'\n+\n+test_expect_success ' context does not include other functions' '\n+\ttest $(grep -c \"^[ +-].*Begin\" changed_hello_appended.diff) -le 2\n+'\n+\n test_done\ndiff --git a/xdiff/xemit.c b/xdiff/xemit.c\nindex bfa53d3..49aa16f 100644\n--- a/xdiff/xemit.c\n+++ b/xdiff/xemit.c\n@@ -246,7 +246,8 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,\n \t\t\t * its new end.\n \t\t\t */\n \t\t\tif (xche->next) {\n-\t\t\t\tlong l = xche->next->i1;\n+\t\t\t\tlong l = XDL_MIN(xche->next->i1,\n+\t\t\t\t\t\t xe->xdf1.nrec - 1);\n \t\t\t\tif (l <= e1 ||\n \t\t\t\t    get_func_line(xe, xecfg, NULL, l, e1) < 0) {\n \t\t\t\t\txche = xche->next;\n-- \n2.8.3\n"},{"id":"288864","messageId":"575A5BCF.20409@alum.mit.edu","threadId":"42524","inReplyTo":"xmqq60tijq4g.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-10T06:18:55Z","receivedAt":"2016-06-16T02:19:50Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/09/2016 06:14 PM, Junio C Hamano wrote:\n> Michael Haggerty <mhagger@alum.mit.edu> writes:\n> \n>>>> +static struct ref_store *main_ref_store = NULL;\n>>>> +\n>>>> +static struct ref_store *submodule_ref_stores = NULL;\n>>>\n>>> Let's let BSS take care of these initialization.\n> [...]\n> Lack of \"= ...\" is a clear-enough clue that the code wants these\n> pointers initialized to NULL.\n> [...]\n\nOK. While I'm at it I'll add docstrings for these variables.\n\nMichael\n"},{"id":"288870","messageId":"CAPig+cQqnDFQ1=ydQReJimVhL7WrjH9CD1RZ63ddCnNm=YVenA@mail.gmail.com","threadId":"42524","inReplyTo":"8ee7d7359f6763cba29dc788aba2f236204bb76e.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-06-10T08:08:42Z","receivedAt":"2016-06-16T02:19:50Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Jun 3, 2016 at 5:03 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n> We want ref_stores to be polymorphic, so invent a base class of which\n> files_ref_store is a derived class. For now there is a one-to-one\n> relationship between ref_stores and submodules.\n>\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> @@ -973,53 +967,54 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)\n> +/*\n> + * Downcast ref_store to files_ref_store. Die if ref_store is not a\n> + * files_ref_store. If submodule_allowed is not true, then also die if\n> + * files_ref_store is for a submodule (i.e., not for the main\n> + * repository). caller is used in any necessary error messages.\n> + */\n> +static struct files_ref_store *files_downcast(\n> +               struct ref_store *ref_store, int submodule_allowed,\n> +               const char *caller)\n>  {\n>         struct files_ref_store *refs;\n>\n> +       if (ref_store->be != &refs_be_files)\n> +               die(\"BUG: ref_store is type \\\"%s\\\" not \\\"files\\\" in %s\",\n> +                   ref_store->be->name, caller);\n>\n> +       refs = (struct files_ref_store *)ref_store;\n> +\n> +       if (!submodule_allowed)\n> +               assert_main_repository(ref_store, caller);\n> +\n> +       return refs;\n>  }\n\nAside from returning the downcasted value, 'refs' doesn't seem to be\nused for anything, thus could be dropped and  the downcasted value\nreturned directly:\n\n    return (struct files_ref_store *)ref_store;\n\nNot worth a re-roll.\n\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> @@ -521,11 +521,89 @@ int read_raw_ref(const char *refname, unsigned char *sha1,\n> +/*\n> + * A representation of the reference store for the main repository or\n> + * a submodule. The ref_store instances for submodules are kept in a\n> + * linked list.\n> + */\n> +struct ref_store {\n> +       /* The backend describing this ref_store's storage scheme: */\n> +       const struct ref_storage_be *be;\n> +\n> +       /*\n> +        * The name of the submodule represented by this object, or\n> +        * the empty string if it represents the main repository's\n> +        * reference store:\n> +        */\n> +       const char *submodule;\n\nTangent: Apart from backward compatibility due to all the existing\ncode which tests *submodule to distinguish between the main repository\nand a submodule, is there a technical reason that this ought to store\nan empty string rather than (the more idiomatic) NULL to signify the\nmain repository?\n"},{"id":"288892","messageId":"575AAC68.5000806@alum.mit.edu","threadId":"42524","inReplyTo":"CAPig+cQqnDFQ1=ydQReJimVhL7WrjH9CD1RZ63ddCnNm=YVenA@mail.gmail.com","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-10T12:02:48Z","receivedAt":"2016-06-16T02:19:50Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/10/2016 10:08 AM, Eric Sunshine wrote:\n> On Fri, Jun 3, 2016 at 5:03 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n>> We want ref_stores to be polymorphic, so invent a base class of which\n>> files_ref_store is a derived class. For now there is a one-to-one\n>> relationship between ref_stores and submodules.\n>>\n>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n>> ---\n>> diff --git a/refs/files-backend.c b/refs/files-backend.c\n>> @@ -973,53 +967,54 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)\n>> +/*\n>> + * Downcast ref_store to files_ref_store. Die if ref_store is not a\n>> + * files_ref_store. If submodule_allowed is not true, then also die if\n>> + * files_ref_store is for a submodule (i.e., not for the main\n>> + * repository). caller is used in any necessary error messages.\n>> + */\n>> +static struct files_ref_store *files_downcast(\n>> +               struct ref_store *ref_store, int submodule_allowed,\n>> +               const char *caller)\n>>  {\n>>         struct files_ref_store *refs;\n>>\n>> +       if (ref_store->be != &refs_be_files)\n>> +               die(\"BUG: ref_store is type \\\"%s\\\" not \\\"files\\\" in %s\",\n>> +                   ref_store->be->name, caller);\n>>\n>> +       refs = (struct files_ref_store *)ref_store;\n>> +\n>> +       if (!submodule_allowed)\n>> +               assert_main_repository(ref_store, caller);\n>> +\n>> +       return refs;\n>>  }\n> \n> Aside from returning the downcasted value, 'refs' doesn't seem to be\n> used for anything, thus could be dropped and  the downcasted value\n> returned directly:\n> \n>     return (struct files_ref_store *)ref_store;\n> \n> Not worth a re-roll.\n\nGood point. I'll fix it.\n\n>> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n>> @@ -521,11 +521,89 @@ int read_raw_ref(const char *refname, unsigned char *sha1,\n>> +/*\n>> + * A representation of the reference store for the main repository or\n>> + * a submodule. The ref_store instances for submodules are kept in a\n>> + * linked list.\n>> + */\n>> +struct ref_store {\n>> +       /* The backend describing this ref_store's storage scheme: */\n>> +       const struct ref_storage_be *be;\n>> +\n>> +       /*\n>> +        * The name of the submodule represented by this object, or\n>> +        * the empty string if it represents the main repository's\n>> +        * reference store:\n>> +        */\n>> +       const char *submodule;\n> \n> Tangent: Apart from backward compatibility due to all the existing\n> code which tests *submodule to distinguish between the main repository\n> and a submodule, is there a technical reason that this ought to store\n> an empty string rather than (the more idiomatic) NULL to signify the\n> main repository?\n\nNo, this was just how the old code worked and I just haven't gotten\naround to changing it. I actually started doing the conversion once, but\nit was turning into too much of a distraction, so I added the item to my\nTODO list instead.\n\nMichael\n"},{"id":"288929","messageId":"xmqqziqtf3c3.fsf@gitster.mtv.corp.google.com","threadId":"42524","inReplyTo":"575A5BCF.20409@alum.mit.edu","subject":"Re: [PATCH 05/38] refs: create a base class \"ref_store\" for files_ref_store","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-10T15:53:00Z","receivedAt":"2016-06-16T02:19:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> On 06/09/2016 06:14 PM, Junio C Hamano wrote:\n>> Michael Haggerty <mhagger@alum.mit.edu> writes:\n>> \n>>>>> +static struct ref_store *main_ref_store = NULL;\n>>>>> +\n>>>>> +static struct ref_store *submodule_ref_stores = NULL;\n>>>>\n>>>> Let's let BSS take care of these initialization.\n>> [...]\n>> Lack of \"= ...\" is a clear-enough clue that the code wants these\n>> pointers initialized to NULL.\n>> [...]\n>\n> OK. While I'm at it I'll add docstrings for these variables.\n\nYeah, if you explain what they are used for, it would become obvious\nwhy their natural initial state is NULL.\n"},{"id":"289183","messageId":"CAPig+cQTDrUw63qWknZQxmuDBXkoYr6gLyp5Bu=JopeT394puw@mail.gmail.com","threadId":"42524","inReplyTo":"a9787723bcd990413661b8cb2e5542d618ab6321.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 16/38] resolve_gitlink_ref(): implement using resolve_ref_recursively()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-06-14T05:03:52Z","receivedAt":"2016-06-16T06:55:52Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Jun 3, 2016 at 5:03 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n> resolve_ref_recursively() can handle references in arbitrary files\n> reference stores, so use it to resolve \"gitlink\" (i.e., submodule)\n> references. Aside from removing redundant code, this allows submodule\n> lookups to benefit from the much more robust code that we use for\n> reading non-submodule references. And, since the code is now agnostic\n> about reference backends, it will work for any future references\n> backend (so move its definition to refs.c).\n>\n> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n> ---\n> diff --git a/refs.c b/refs.c\n> @@ -1299,6 +1299,30 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n> +int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n> +{\n> +       int len = strlen(path);\n> +       struct strbuf submodule = STRBUF_INIT;\n> +       struct ref_store *refs;\n> +       int flags;\n> +\n> +       while (len && path[len-1] == '/')\n> +               len--;\n> +       if (!len)\n> +               return -1;\n> +\n> +       strbuf_add(&submodule, path, len);\n\nIt took me a moment to figure out that you're using the strbuf only\nfor its side-effect of giving you a NUL-terminated string needed by\nget_ref_store(), and not because you need any fancy functionality of\nstrbuf. I wonder if xstrndup() would have made this clearer.\n\nNot worth a re-roll.\n\n> +       refs = get_ref_store(submodule.buf);\n> +       strbuf_release(&submodule);\n> +       if (!refs)\n> +               return -1;\n> +\n> +       if (!resolve_ref_recursively(refs, refname, 0, sha1, &flags) ||\n> +           is_null_sha1(sha1))\n> +               return -1;\n> +       return 0;\n> +}\n"},{"id":"289328","messageId":"5762250E.6020200@alum.mit.edu","threadId":"42524","inReplyTo":"CAPig+cQTDrUw63qWknZQxmuDBXkoYr6gLyp5Bu=JopeT394puw@mail.gmail.com","subject":"Re: [PATCH 16/38] resolve_gitlink_ref(): implement using resolve_ref_recursively()","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-06-16T04:03:26Z","receivedAt":"2016-06-16T06:55:53Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 06/14/2016 07:03 AM, Eric Sunshine wrote:\n> On Fri, Jun 3, 2016 at 5:03 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n>> resolve_ref_recursively() can handle references in arbitrary files\n>> reference stores, so use it to resolve \"gitlink\" (i.e., submodule)\n>> references. Aside from removing redundant code, this allows submodule\n>> lookups to benefit from the much more robust code that we use for\n>> reading non-submodule references. And, since the code is now agnostic\n>> about reference backends, it will work for any future references\n>> backend (so move its definition to refs.c).\n>>\n>> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>\n>> ---\n>> diff --git a/refs.c b/refs.c\n>> @@ -1299,6 +1299,30 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,\n>> +int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)\n>> +{\n>> +       int len = strlen(path);\n>> +       struct strbuf submodule = STRBUF_INIT;\n>> +       struct ref_store *refs;\n>> +       int flags;\n>> +\n>> +       while (len && path[len-1] == '/')\n>> +               len--;\n>> +       if (!len)\n>> +               return -1;\n>> +\n>> +       strbuf_add(&submodule, path, len);\n> \n> It took me a moment to figure out that you're using the strbuf only\n> for its side-effect of giving you a NUL-terminated string needed by\n> get_ref_store(), and not because you need any fancy functionality of\n> strbuf. I wonder if xstrndup() would have made this clearer.\n> \n> Not worth a re-roll.\n\nI agree both that xstrndup() (or, in this case, xmemdupz()) would have\nbeen clearer, and also that it's not worth a re-roll. I'll keep it in\nmind if I touch this code again.\n\nThanks for your comment.\n\nMichael\n"},{"id":"291139","messageId":"CACsJy8CNJfQxnn94b=+mjfPVvNzJu3sD3w_g8yCUHY0UQCbfLQ@mail.gmail.com","threadId":"42524","inReplyTo":"cover.1464983301.git.mhagger@alum.mit.edu","subject":"Re: [PATCH 00/38] Virtualization of the refs API","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-07-10T15:09:06Z","receivedAt":"2016-07-10T15:09:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Fri, Jun 3, 2016 at 11:03 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n> Since the that ref-iterator [1] changes seem to have gotten a positive\n> reception, let's try to keep up the momentum. I hope I'm not\n> overloading the review pipeline...\n>\n> I think all of the groundwork is in place now to virtualize the refs\n> API. This will open the way to storing refs in ways other than the\n> familiar loose refs / packed refs format, such as David Turner's\n> proposed LMDB-based storage [2].\n>\n> This is a long patch series, but most of the patches are pretty simple\n> and formulaic. The goal is to implement a `ref_store`. In the language\n> of object-oriented programming, `ref_store` is an abstract base class\n> representing a reference storage backend. It provides methods to read,\n> write, and delete references and symrefs, and to iterate over\n> references, reflogs, and reflog entries, plus a number of other\n> things—19 methods in all.\n\nI probably don't know what I'm talking about because I don't follow\nyour work closely enough. Please ignore if this is nonsense. But if we\nextend/change API, we might need to update git-for-each-ref too, to\nexpose it to shell scripts and external commands. I guess for\niteration there's nothing else more needed, but we may need to\nintroduction new options for the storage thing, e.g. to select\nstorage...\n-- \nDuy\n"},{"id":"291346","messageId":"6c123315-ba5c-ac15-8314-b6a71c325d9f@alum.mit.edu","threadId":"42524","inReplyTo":"CACsJy8CNJfQxnn94b=+mjfPVvNzJu3sD3w_g8yCUHY0UQCbfLQ@mail.gmail.com","subject":"Re: [PATCH 00/38] Virtualization of the refs API","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2016-07-13T15:26:38Z","receivedAt":"2016-07-13T15:30:33Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 07/10/2016 05:09 PM, Duy Nguyen wrote:\n> On Fri, Jun 3, 2016 at 11:03 PM, Michael Haggerty <mhagger@alum.mit.edu> wrote:\n>> Since the that ref-iterator [1] changes seem to have gotten a positive\n>> reception, let's try to keep up the momentum. I hope I'm not\n>> overloading the review pipeline...\n>>\n>> I think all of the groundwork is in place now to virtualize the refs\n>> API. This will open the way to storing refs in ways other than the\n>> familiar loose refs / packed refs format, such as David Turner's\n>> proposed LMDB-based storage [2].\n>>\n>> This is a long patch series, but most of the patches are pretty simple\n>> and formulaic. The goal is to implement a `ref_store`. In the language\n>> of object-oriented programming, `ref_store` is an abstract base class\n>> representing a reference storage backend. It provides methods to read,\n>> write, and delete references and symrefs, and to iterate over\n>> references, reflogs, and reflog entries, plus a number of other\n>> things—19 methods in all.\n> \n> I probably don't know what I'm talking about because I don't follow\n> your work closely enough. Please ignore if this is nonsense. But if we\n> extend/change API, we might need to update git-for-each-ref too, to\n> expose it to shell scripts and external commands. I guess for\n> iteration there's nothing else more needed, but we may need to\n> introduction new options for the storage thing, e.g. to select\n> storage...\n\nThis patch series doesn't change the external API in any significant\nway. It only wraps it up on a virtualization layer so that a different\nreference storage backends can be plugged in.\n\nSo as long as people are using plumbing commands to work with references\n(rather than reading/writing files under $GIT_DIR directly), they should\nnotice no difference.\n\nThere are only two exceptions that I know of:\n\n1. Users will need to be able to request that a repository use a\nnon-default reference backend, and (less importantly) inquire about\nwhich reference backend a particular repository is using. Those\nfacilities will be added when the first non-files reference backend is\nadded.\n\n2. At least one command (`git pack-refs`) is particular to the files\nbackend, and won't be needed (at least not in its current form) for\nother backends. Conversely, it is conceivable that future reference\nbackends will require their own \"maintenance\" commands. Such commands\nwould be added as they are needed.\n\nIf there were operations that other reference backends could do much\nmore efficiently than the files backend (like, hypothetically, return\nall references matching a regular expression without having to iterate\nthrough all references), then it might make sense for performance\nreasons to provide commands to access that functionality. But at the\nmoment I don't know of any such cases.\n\nMichael\n\n"}]}