refs backend
Subsystems (2)
v4 cover refs backend
Changes to this version:
re-rolled on top of pu as-of 9db66d9f1aa.
Bug fixes include:
For submodules: memory leaks; segfault on bad config. (thanks to Peff) In symref splitting: check that would always succeed (thanks to Peff) A bogus double-declaration of a var (thanks to Ramsay Jones) Two memory leaks (thanks to Thomas Gummerer)
An unused var (thanks to Duy Nguyen)
Other improvements are:
Strings prepped for 18n (thanks to Duy Nguyen)
Cleaner submodule handling (thanks to Peff)
Whitelisting instead of blacklisting in git-new-workdir (thanks to
Thomas Gummerer)
Allow older gits to recognize lmdb-backend git repos (thanks to Duy Nguyen) Tab completion and cleaner commit messages (thanks SZEDER Gábor) Removed some #ifdefs, moving all backend setup to one place (thanks to Duy Nguyen)
Thanks to all for reviews.
David Turner (18):
refs: add do_for_each_per_worktree_ref
refs: add methods for reflog
refs: add method for initial ref transaction commit
refs: add method for delete_refs
refs: add methods to init refs db
refs: add method to rename refs
refs: make lock generic
refs: move duplicate check to common code
refs: allow log-only updates
refs: resolve symbolic refs first
refs: always handle non-normal refs in files backend
init: allow alternate ref strorage to be set for new repos
refs: check submodules ref storage config
clone: allow ref storage backend to be set for clone
svn: learn ref-storage argument
refs: add register_ref_storage_backends()
refs: add LMDB refs storage backend
refs: tests for lmdb backend
Ronnie Sahlberg (3):
refs: add a backend method structure with transaction functions
refs: add methods for misc ref operations
refs: add methods for the ref iterators
.gitignore | 1 + Documentation/config.txt | 7 + Documentation/git-clone.txt | 6 + Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 7 +- Documentation/technical/refs-lmdb-backend.txt | 52 + Documentation/technical/repository-version.txt | 5 + Makefile | 12 + builtin/clone.c | 5 + builtin/init-db.c | 57 +- builtin/submodule--helper.c | 2 +- cache.h | 2 + config.c | 25 + configure.ac | 33 + contrib/completion/git-completion.bash | 6 +- contrib/workdir/git-new-workdir | 3 + git-submodule.sh | 13 + git-svn.perl | 6 +- path.c | 29 +- refs.c | 486 +++++- refs.h | 21 + refs/files-backend.c | 404 ++--- refs/lmdb-backend.c | 2052 ++++++++++++++++++++++++ refs/refs-internal.h | 128 +- setup.c | 23 +- t/t0001-init.sh | 25 + t/t1460-refs-lmdb-backend.sh | 1109 +++++++++++++ t/t1470-refs-lmdb-backend-reflog.sh | 359 +++++ t/t1480-refs-lmdb-submodule.sh | 85 + t/test-lib.sh | 1 + test-refs-lmdb-backend.c | 64 + transport.c | 7 +- 32 files changed, 4825 insertions(+), 212 deletions(-) create mode 100644 Documentation/technical/refs-lmdb-backend.txt create mode 100644 refs/lmdb-backend.c create mode 100755 t/t1460-refs-lmdb-backend.sh create mode 100755 t/t1470-refs-lmdb-backend-reflog.sh create mode 100755 t/t1480-refs-lmdb-submodule.sh create mode 100644 test-refs-lmdb-backend.c
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 1/21 refs: add a backend method structure with transaction functions
From: Ronnie Sahlberg <[email protected]>
Add a ref structure for storage backend methods. Start by adding a method pointer for the transaction commit function.
Add a function set_refs_backend to switch between storage backends. The files based storage backend is the default.
--- refs.c | 40 ++++++++++++++++++++++++++++++++++++++++ refs.h | 7 +++++++ refs/files-backend.c | 10 ++++++++-- refs/refs-internal.h | 12 ++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-)
refs.c +40 -0
@@ -10,6 +10,39 @@#include "tag.h" /* +* We always have a files backend and it is the default. +*/ +static struct ref_storage_be *the_refs_backend = &refs_be_files; +/* +* List of all available backends +*/ +static struct ref_storage_be *refs_backends = &refs_be_files; + +int set_ref_storage_backend (const char *name) +{ +struct ref_storage_be *be; + +for (be = refs_backends; be; be = be->next) +if (!strcmp (be->name, name)) { + the_refs_backend = be; +return 0 ; + } +return 1 ; +} + +int ref_storage_backend_exists (const char *name) +{ +struct ref_storage_be *be; + +for (be = refs_backends; be; be = be->next) +if (!strcmp (be->name, name)) { + the_refs_backend = be; +return 1 ; + } +return 0 ; +} + +/* * How to handle various characters in refnames: * 0: An acceptable character for refs * 1: End-of-component
@@ -1082,3 +1115,10 @@ int rename_ref_available(const char *oldname, const char *newname)strbuf_release (&err);return ret; } + +/* backend functions */ +int ref_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) +{ +return the_refs_backend->transaction_commit (transaction, err); +}
refs.h +7 -0
@@ -508,4 +508,11 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data); +/* +* Switch to an alternate ref storage backend. +*/ +int set_ref_storage_backend (const char *name); + +int ref_storage_backend_exists (const char *name); +#endif /* REFS_H */
refs/files-backend.c +8 -2
@@ -3146,8 +3146,8 @@ static int ref_update_reject_duplicates(struct string_list *refnames,return 0 ; } -int ref_transaction_commit (struct ref_transaction *transaction, -struct strbuf *err) +static int files_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) {int ret =0 , i;int n = transaction->nr;
@@ -3533,3 +3533,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1,unlock_ref (lock);return -1 ; } + +struct ref_storage_be refs_be_files = { +NULL , +"files" , + files_transaction_commit, +};
refs/refs-internal.h +12 -0
@@ -197,4 +197,16 @@ const char *find_descendant_ref(const char *dirname,int rename_ref_available (const char *oldname,const char *newname); +/* refs backends */ +typedef int ref_transaction_commit_fn (struct ref_transaction *transaction, +struct strbuf *err); + +struct ref_storage_be { +struct ref_storage_be *next; +const char *name; +ref_transaction_commit_fn *transaction_commit; +}; + +extern struct ref_storage_be refs_be_files; +#endif /* REFS_REFS_INTERNAL_H */
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 2/21 refs: add methods for misc ref operations
In reply to David TurnerFrom: Ronnie Sahlberg <[email protected]>
Add ref backend methods for:resolve_ref_unsafe, verify_refname_available, pack_refs, peel_ref, create_symref, resolve_gitlink_ref.
--- builtin/init-db.c | 1 + refs.c | 36 ++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 33 +++++++++++++++++++++++---------- refs/refs-internal.h | 23 +++++++++++++++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-)
builtin/init-db.c +1 -0
@@ -8,6 +8,7 @@#include "builtin.h" #include "exec_cmd.h" #include "parse-options.h" +#include "refs.h" #ifndef DEFAULT_GIT_TEMPLATE_DIR #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
refs.c +36 -0
@@ -1122,3 +1122,39 @@ int ref_transaction_commit(struct ref_transaction *transaction, {return the_refs_backend->transaction_commit (transaction, err); } + +const char *resolve_ref_unsafe (const char *ref,int resolve_flags, +unsigned char *sha1,int *flags) +{ +return the_refs_backend->resolve_ref_unsafe (ref, resolve_flags, sha1, + flags); +} + +int verify_refname_available (const char *refname,struct string_list *extra, +struct string_list *skip,struct strbuf *err) +{ +return the_refs_backend->verify_refname_available (refname, extra, skip, err); +} + +int pack_refs (unsigned int flags) +{ +return the_refs_backend->pack_refs (flags); +} + +int peel_ref (const char *refname,unsigned char *sha1) +{ +return the_refs_backend->peel_ref (refname, sha1); +} + +int create_symref (const char *ref_target,const char *refs_heads_master, +const char *logmsg) +{ +return the_refs_backend->create_symref (ref_target, refs_heads_master, + logmsg); +} + +int resolve_gitlink_ref (const char *path,const char *refname, +unsigned char *sha1) +{ +return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); +}
refs/files-backend.c +23 -10
@@ -1343,7 +1343,8 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,return resolve_gitlink_ref_recursive (refs, p, sha1, recursion+1 ); } -int resolve_gitlink_ref (const char *path,const char *refname,unsigned char *sha1) +static int files_resolve_gitlink_ref (const char *path,const char *refname, +unsigned char *sha1) {int len =strlen (path), retval;struct strbuf submodule =STRBUF_INIT ;
@@ -1583,8 +1584,10 @@ static const char *resolve_ref_1(const char *refname, } } -const char *resolve_ref_unsafe (const char *refname,int resolve_flags, -unsigned char *sha1,int *flags) +static const char *files_resolve_ref_unsafe (const char *refname, +int resolve_flags, +unsigned char *sha1, +int *flags) {static struct strbuf sb_refname =STRBUF_INIT ;struct strbuf sb_contents =STRBUF_INIT ;
@@ -1633,7 +1636,7 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)return status; } -int peel_ref (const char *refname,unsigned char *sha1) +static int files_peel_ref (const char *refname,unsigned char *sha1) {int flag;unsigned char base[20 ];
@@ -2270,7 +2273,7 @@ static void prune_refs(struct ref_to_prune *r) } } -int pack_refs (unsigned int flags) +static int files_pack_refs (unsigned int flags) {struct pack_refs_cb_data cbdata;
@@ -2461,10 +2464,10 @@ out:return ret; } -int verify_refname_available (const char *newname, -struct string_list *extras, -struct string_list *skip, -struct strbuf *err) +static int files_verify_refname_available (const char *newname, +struct string_list *extras, +struct string_list *skip, +struct strbuf *err) {struct ref_dir *packed_refs =get_packed_refs (&ref_cache);struct ref_dir *loose_refs =get_loose_refs (&ref_cache);
@@ -2884,7 +2887,9 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,return 0 ; } -int create_symref (const char *refname,const char *target,const char *logmsg) +static int files_create_symref (const char *refname, +const char *target, +const char *logmsg) {struct strbuf err =STRBUF_INIT ;struct ref_lock *lock;
@@ -3538,4 +3543,12 @@ struct ref_storage_be refs_be_files = {NULL ,"files" , files_transaction_commit, + + files_pack_refs, + files_peel_ref, + files_create_symref, + + files_resolve_ref_unsafe, + files_verify_refname_available, + files_resolve_gitlink_ref, };
refs/refs-internal.h +23 -0
@@ -201,10 +201,33 @@ int rename_ref_available(const char *oldname, const char *newname);typedef int ref_transaction_commit_fn (struct ref_transaction *transaction,struct strbuf *err); +/* misc methods */ +typedef int pack_refs_fn (unsigned int flags); +typedef int peel_ref_fn (const char *refname,unsigned char *sha1); +typedef int create_symref_fn (const char *ref_target, +const char *refs_heads_master, +const char *logmsg); + +/* resolution methods */ +typedef const char *resolve_ref_unsafe_fn (const char *ref, +int resolve_flags, +unsigned char *sha1,int *flags); +typedef int verify_refname_available_fn (const char *refname,struct string_list *extra,struct string_list *skip,struct strbuf *err); +typedef int resolve_gitlink_ref_fn (const char *path,const char *refname, +unsigned char *sha1); +struct ref_storage_be {struct ref_storage_be *next;const char *name;ref_transaction_commit_fn *transaction_commit; + +pack_refs_fn *pack_refs; +peel_ref_fn *peel_ref; +create_symref_fn *create_symref; + +resolve_ref_unsafe_fn *resolve_ref_unsafe; +verify_refname_available_fn *verify_refname_available; +resolve_gitlink_ref_fn *resolve_gitlink_ref; };extern struct ref_storage_be refs_be_files;
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 3/21 refs: add methods for the ref iterators
In reply to David TurnerFrom: Ronnie Sahlberg <[email protected]>
--- refs.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 41 +++++++++++++++++++++++++++------------ refs/refs-internal.h | 29 ++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 12 deletions(-)
refs.c +54 -0
@@ -1158,3 +1158,57 @@ int resolve_gitlink_ref(const char *path, const char *refname, {return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); } + +int head_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref (fn, cb_data); +} + +int head_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref_submodule (submodule, fn, cb_data); +} + +int for_each_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref (fn, cb_data); +} + +int for_each_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref_submodule (submodule, fn, cb_data); +} + +int for_each_ref_in (const char *prefix,each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref_in (prefix, fn, cb_data); +} + +int for_each_fullref_in (const char *prefix,each_ref_fn fn,void *cb_data, +unsigned int broken) +{ +return the_refs_backend->for_each_fullref_in (prefix, fn, cb_data, + broken); +} + +int for_each_ref_in_submodule (const char *submodule,const char *prefix, +each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref_in_submodule (submodule, prefix, + fn, cb_data); +} + +int for_each_rawref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_rawref (fn, cb_data); +} + +int for_each_namespaced_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_namespaced_ref (fn, cb_data); +} + +int for_each_replace_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_replace_ref (fn, cb_data); +}
refs/files-backend.c +29 -12
91 lines not on this page · raw message
refs/refs-internal.h +29 -0
@@ -216,6 +216,24 @@ typedef int verify_refname_available_fn(const char *refname, struct string_listtypedef int resolve_gitlink_ref_fn (const char *path,const char *refname,unsigned char *sha1); +/* iteration methods */ +typedef int head_ref_fn (each_ref_fn fn,void *cb_data); +typedef int head_ref_submodule_fn (const char *submodule,each_ref_fn fn, +void *cb_data); +typedef int for_each_ref_fn (each_ref_fn fn,void *cb_data); +typedef int for_each_ref_submodule_fn (const char *submodule,each_ref_fn fn, +void *cb_data); +typedef int for_each_ref_in_fn (const char *prefix,each_ref_fn fn, +void *cb_data); +typedef int for_each_fullref_in_fn (const char *prefix,each_ref_fn fn, +void *cb_data,unsigned int broken); +typedef int for_each_ref_in_submodule_fn (const char *submodule, +const char *prefix, +each_ref_fn fn,void *cb_data); +typedef int for_each_rawref_fn (each_ref_fn fn,void *cb_data); +typedef int for_each_namespaced_ref_fn (each_ref_fn fn,void *cb_data); +typedef int for_each_replace_ref_fn (each_ref_fn fn,void *cb_data); +struct ref_storage_be {struct ref_storage_be *next;const char *name;
@@ -228,6 +246,17 @@ struct ref_storage_be {resolve_ref_unsafe_fn *resolve_ref_unsafe;verify_refname_available_fn *verify_refname_available;resolve_gitlink_ref_fn *resolve_gitlink_ref; + +head_ref_fn *head_ref; +head_ref_submodule_fn *head_ref_submodule; +for_each_ref_fn *for_each_ref; +for_each_ref_submodule_fn *for_each_ref_submodule; +for_each_ref_in_fn *for_each_ref_in; +for_each_fullref_in_fn *for_each_fullref_in; +for_each_ref_in_submodule_fn *for_each_ref_in_submodule; +for_each_rawref_fn *for_each_rawref; +for_each_namespaced_ref_fn *for_each_namespaced_ref; +for_each_replace_ref_fn *for_each_replace_ref; };extern struct ref_storage_be refs_be_files;
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 4/21 refs: add do_for_each_per_worktree_ref
In reply to David TurnerAlternate refs backends might still use files to store per-worktree refs. So the files backend's ref-loading infrastructure should be available to those backends, just for use on per-worktree refs. Add do_for_each_per_worktree_ref, which iterates over per-worktree refs.
--- refs/files-backend.c | 15 ++++++++++++--- refs/refs-internal.h | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-)
refs/files-backend.c +12 -3
@@ -518,9 +518,6 @@ static void sort_ref_dir(struct ref_dir *dir) dir->sorted = dir->nr = i; } -/* Include broken references in a do_for_each_ref*() iteration: */ -#define DO_FOR_EACH_INCLUDE_BROKEN 0x01 -/* * Return true iff the reference described by entry can be resolved to * an object in the database. Emit a warning if the referred-to
@@ -568,6 +565,10 @@ static int do_one_ref(struct ref_entry *entry, void *cb_data)struct ref_entry *old_current_ref;int retval; +if (data->flags &DO_FOR_EACH_PER_WORKTREE_ONLY && +ref_type (entry->name) !=REF_TYPE_PER_WORKTREE ) +return 0 ; +if (!starts_with (entry->name, data->base))return 0 ;
@@ -1756,6 +1757,14 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,return do_for_each_entry (refs, base, do_one_ref, &data); } +int do_for_each_per_worktree_ref (const char *submodule,const char *base, +each_ref_fn fn,int trim,int flags, +void *cb_data) +{ +return do_for_each_ref (get_ref_cache (submodule), base, fn, trim, + flags |DO_FOR_EACH_PER_WORKTREE_ONLY , cb_data); +} +static int do_head_ref (const char *submodule,each_ref_fn fn,void *cb_data) {struct object_id oid;
refs/refs-internal.h +10 -0
@@ -57,6 +57,16 @@*/ int refname_is_safe (const char *refname); +/* Include broken references in a do_for_each_ref*() iteration */ +#define DO_FOR_EACH_INCLUDE_BROKEN 0x01 + +/* Only include per-worktree refs in a do_for_each_ref*() iteration */ +#define DO_FOR_EACH_PER_WORKTREE_ONLY 0x02 + +int do_for_each_per_worktree_ref (const char *submodule,const char *base, +each_ref_fn fn,int trim,int flags, +void *cb_data); +enum peel_status {/* object was peeled successfully: */ PEEL_PEELED =0 ,
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 5/21 refs: add methods for reflog
In reply to David TurnerIn the file-based backend, the reflog piggybacks on the ref lock. Since other backends won't have the same sort of ref lock, ref backends must also handle reflogs.
--- refs.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 36 ++++++++++++++++++++++++------------ refs/refs-internal.h | 27 +++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 12 deletions(-)
refs.c +46 -0
@@ -1212,3 +1212,49 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data) {return the_refs_backend->for_each_replace_ref (fn, cb_data); } + +int for_each_reflog_ent_reverse (const char *refname,each_reflog_ent_fn fn, +void *cb_data) +{ +return the_refs_backend->for_each_reflog_ent_reverse (refname, fn, + cb_data); +} + +int for_each_reflog_ent (const char *refname,each_reflog_ent_fn fn, +void *cb_data) +{ +return the_refs_backend->for_each_reflog_ent (refname, fn, cb_data); +} + +int for_each_reflog (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_reflog (fn, cb_data); +} + +int reflog_exists (const char *refname) +{ +return the_refs_backend->reflog_exists (refname); +} + +int safe_create_reflog (const char *refname,int force_create, +struct strbuf *err) +{ +return the_refs_backend->create_reflog (refname, force_create, err); +} + +int delete_reflog (const char *refname) +{ +return the_refs_backend->delete_reflog (refname); +} + +int reflog_expire (const char *refname,const unsigned char *sha1, +unsigned int flags, +reflog_expiry_prepare_fn prepare_fn, +reflog_expiry_should_prune_fn should_prune_fn, +reflog_expiry_cleanup_fn cleanup_fn, +void *policy_cb_data) +{ +return the_refs_backend->reflog_expire (refname, sha1, flags, + prepare_fn, should_prune_fn, + cleanup_fn, policy_cb_data); +}
refs/files-backend.c +24 -12
@@ -2667,7 +2667,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str } -int safe_create_reflog (const char *refname,int force_create,struct strbuf *err) +static int files_create_reflog (const char *refname,int force_create, +struct strbuf *err) {int ret;struct strbuf sb =STRBUF_INIT ;
@@ -2923,7 +2924,7 @@ static int files_create_symref(const char *refname,return ret; } -int reflog_exists (const char *refname) +static int files_reflog_exists (const char *refname) {struct stat st;
@@ -2931,7 +2932,7 @@ int reflog_exists(const char *refname)S_ISREG (st.st_mode); } -int delete_reflog (const char *refname) +static int files_delete_reflog (const char *refname) {return remove_path (git_path ("logs/%s" , refname)); }
@@ -2975,7 +2976,9 @@ static char *find_beginning_of_line(char *bob, char *scan)return scan; } -int for_each_reflog_ent_reverse (const char *refname,each_reflog_ent_fn fn,void *cb_data) +static int files_for_each_reflog_ent_reverse (const char *refname, +each_reflog_ent_fn fn, +void *cb_data) {struct strbuf sb =STRBUF_INIT ;FILE *logfp;
@@ -3077,7 +3080,8 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, voidreturn ret; } -int for_each_reflog_ent (const char *refname,each_reflog_ent_fn fn,void *cb_data) +static int files_for_each_reflog_ent (const char *refname, +each_reflog_ent_fn fn,void *cb_data) {FILE *logfp;struct strbuf sb =STRBUF_INIT ;
@@ -3139,7 +3143,7 @@ static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_datareturn retval; } -int for_each_reflog (each_ref_fn fn,void *cb_data) +static int files_for_each_reflog (each_ref_fn fn,void *cb_data) {int retval;struct strbuf name;
@@ -3449,12 +3453,12 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,return 0 ; } -int reflog_expire (const char *refname,const unsigned char *sha1, -unsigned int flags, -reflog_expiry_prepare_fn prepare_fn, -reflog_expiry_should_prune_fn should_prune_fn, -reflog_expiry_cleanup_fn cleanup_fn, -void *policy_cb_data) +static int files_reflog_expire (const char *refname,const unsigned char *sha1, +unsigned int flags, +reflog_expiry_prepare_fn prepare_fn, +reflog_expiry_should_prune_fn should_prune_fn, +reflog_expiry_cleanup_fn cleanup_fn, +void *policy_cb_data) {static struct lock_file reflog_lock;struct expire_reflog_cb cb;
@@ -3559,6 +3563,14 @@ struct ref_storage_be refs_be_files = {"files" , files_transaction_commit, + files_for_each_reflog_ent, + files_for_each_reflog_ent_reverse, + files_for_each_reflog, + files_reflog_exists, + files_create_reflog, + files_delete_reflog, + files_reflog_expire, + files_pack_refs, files_peel_ref, files_create_symref,
refs/refs-internal.h +27 -0
@@ -211,6 +211,25 @@ int rename_ref_available(const char *oldname, const char *newname);typedef int ref_transaction_commit_fn (struct ref_transaction *transaction,struct strbuf *err); +/* reflog functions */ +typedef int for_each_reflog_ent_fn (const char *refname, +each_reflog_ent_fn fn, +void *cb_data); +typedef int for_each_reflog_ent_reverse_fn (const char *refname, +each_reflog_ent_fn fn, +void *cb_data); +typedef int for_each_reflog_fn (each_ref_fn fn,void *cb_data); +typedef int reflog_exists_fn (const char *refname); +typedef int create_reflog_fn (const char *refname,int force_create, +struct strbuf *err); +typedef int delete_reflog_fn (const char *refname); +typedef int reflog_expire_fn (const char *refname,const unsigned char *sha1, +unsigned int flags, +reflog_expiry_prepare_fn prepare_fn, +reflog_expiry_should_prune_fn should_prune_fn, +reflog_expiry_cleanup_fn cleanup_fn, +void *policy_cb_data); +/* misc methods */ typedef int pack_refs_fn (unsigned int flags);typedef int peel_ref_fn (const char *refname,unsigned char *sha1);
@@ -249,6 +268,14 @@ struct ref_storage_be {const char *name;ref_transaction_commit_fn *transaction_commit; +for_each_reflog_ent_fn *for_each_reflog_ent; +for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse; +for_each_reflog_fn *for_each_reflog; +reflog_exists_fn *reflog_exists; +create_reflog_fn *create_reflog; +delete_reflog_fn *delete_reflog; +reflog_expire_fn *reflog_expire; +pack_refs_fn *pack_refs;peel_ref_fn *peel_ref;create_symref_fn *create_symref;
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 6/21 refs: add method for initial ref transaction commit
In reply to David Turner--- refs.c | 6 ++++++ refs/files-backend.c | 5 +++-- refs/refs-internal.h | 1 + 3 files changed, 10 insertions(+), 2 deletions(-)
refs.c +6 -0
@@ -1258,3 +1258,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1, prepare_fn, should_prune_fn, cleanup_fn, policy_cb_data); } + +int initial_ref_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) +{ +return the_refs_backend->initial_transaction_commit (transaction, err); +}
refs/files-backend.c +3 -2
@@ -3337,8 +3337,8 @@ static int ref_present(const char *refname,return string_list_has_string (affected_refnames, refname); } -int initial_ref_transaction_commit (struct ref_transaction *transaction, -struct strbuf *err) +static int files_initial_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) {int ret =0 , i;int n = transaction->nr;
@@ -3562,6 +3562,7 @@ struct ref_storage_be refs_be_files = {NULL ,"files" , files_transaction_commit, + files_initial_transaction_commit, files_for_each_reflog_ent, files_for_each_reflog_ent_reverse,
refs/refs-internal.h +1 -0
@@ -267,6 +267,7 @@ struct ref_storage_be {struct ref_storage_be *next;const char *name;ref_transaction_commit_fn *transaction_commit; +ref_transaction_commit_fn *initial_transaction_commit;for_each_reflog_ent_fn *for_each_reflog_ent;for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse;
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 7/21 refs: add method for delete_refs
In reply to David TurnerIn the file-based backend, delete_refs has some special optimization to deal with packed refs. In other backends, we might be able to make ref deletion faster by putting all deletions into a single transaction. So we need a special backend function for this.
--- refs.c | 5 +++++ refs/files-backend.c | 3 ++- refs/refs-internal.h | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-)
refs.c +5 -0
@@ -1123,6 +1123,11 @@ int ref_transaction_commit(struct ref_transaction *transaction,return the_refs_backend->transaction_commit (transaction, err); } +int delete_refs (struct string_list *refnames) +{ +return the_refs_backend->delete_refs (refnames); +} +const char *resolve_ref_unsafe (const char *ref,int resolve_flags,unsigned char *sha1,int *flags) {
refs/files-backend.c +2 -1
@@ -2380,7 +2380,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)return 0 ; } -int delete_refs (struct string_list *refnames) +static int files_delete_refs (struct string_list *refnames) {struct strbuf err =STRBUF_INIT ;int i, result =0 ;
@@ -3575,6 +3575,7 @@ struct ref_storage_be refs_be_files = { files_pack_refs, files_peel_ref, files_create_symref, + files_delete_refs, files_resolve_ref_unsafe, files_verify_refname_available,
refs/refs-internal.h +2 -0
@@ -236,6 +236,7 @@ typedef int peel_ref_fn(const char *refname, unsigned char *sha1);typedef int create_symref_fn (const char *ref_target,const char *refs_heads_master,const char *logmsg); +typedef int delete_refs_fn (struct string_list *refnames);/* resolution methods */ typedef const char *resolve_ref_unsafe_fn(const char *ref,
@@ -280,6 +281,7 @@ struct ref_storage_be {pack_refs_fn *pack_refs;peel_ref_fn *peel_ref;create_symref_fn *create_symref; +delete_refs_fn *delete_refs;resolve_ref_unsafe_fn *resolve_ref_unsafe;verify_refname_available_fn *verify_refname_available;
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 8/21 refs: add methods to init refs db
In reply to David TurnerAlternate refs backends might not need the refs/heads directory and so on, so we make ref db initialization part of the backend.
--- builtin/init-db.c | 20 ++++++++++---------- refs.c | 5 +++++ refs.h | 2 ++ refs/files-backend.c | 16 ++++++++++++++++ refs/refs-internal.h | 2 ++ 5 files changed, 35 insertions(+), 10 deletions(-)
builtin/init-db.c +10 -10
@@ -178,13 +178,7 @@ static int create_default_files(const char *template_path)char junk[2 ];int reinit;int filemode; - -/* -* Create .git/refs/{heads,tags} -*/ -safe_create_dir (git_path_buf (&buf,"refs" ),1 ); -safe_create_dir (git_path_buf (&buf,"refs/heads" ),1 ); -safe_create_dir (git_path_buf (&buf,"refs/tags" ),1 ); +struct strbuf err =STRBUF_INIT ;/* Just look for `init.templatedir` */ git_config (git_init_db_config,NULL );
@@ -208,12 +202,18 @@ static int create_default_files(const char *template_path)*/ if (shared_repository) {adjust_shared_perm (get_git_dir ()); -adjust_shared_perm (git_path_buf (&buf,"refs" )); -adjust_shared_perm (git_path_buf (&buf,"refs/heads" )); -adjust_shared_perm (git_path_buf (&buf,"refs/tags" )); }/* +* We need to create a "refs" dir in any case so that older +* versions of git can tell that this is a repository. +*/ +safe_create_dir (git_path ("refs" ),1 ); + +if (refs_init_db (&err, shared_repository)) +die ("failed to set up refs db: %s" , err.buf); + +/* * Create the default symlink from ".git/HEAD" to the "master" * branch, if it does not exist yet. */
refs.c +5 -0
@@ -1117,6 +1117,11 @@ int rename_ref_available(const char *oldname, const char *newname) }/* backend functions */ +int refs_init_db (struct strbuf *err,int shared) +{ +return the_refs_backend->init_db (err, shared); +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {
refs.h +2 -0
@@ -66,6 +66,8 @@ extern int ref_exists(const char *refname);extern int is_branch (const char *refname); +extern int refs_init_db (struct strbuf *err,int shared); +/* * If refname is a non-symbolic reference that refers to a tag object, * and the tag can be (recursively) dereferenced to a non-tag object,
refs/files-backend.c +16 -0
@@ -3558,9 +3558,25 @@ static int files_reflog_expire(const char *refname, const unsigned char *sha1,return -1 ; } +static int files_init_db (struct strbuf *err,int shared) +{ +/* +* Create .git/refs/{heads,tags} +*/ +safe_create_dir (git_path ("refs/heads" ),1 ); +safe_create_dir (git_path ("refs/tags" ),1 ); +if (shared) { +adjust_shared_perm (git_path ("refs" )); +adjust_shared_perm (git_path ("refs/heads" )); +adjust_shared_perm (git_path ("refs/tags" )); + } +return 0 ; +} +struct ref_storage_be refs_be_files = {NULL ,"files" , + files_init_db, files_transaction_commit, files_initial_transaction_commit,
refs/refs-internal.h +2 -0
@@ -208,6 +208,7 @@ const char *find_descendant_ref(const char *dirname,int rename_ref_available (const char *oldname,const char *newname);/* refs backends */ +typedef int ref_init_db_fn (struct strbuf *err,int shared);typedef int ref_transaction_commit_fn (struct ref_transaction *transaction,struct strbuf *err);
@@ -267,6 +268,7 @@ typedef int for_each_replace_ref_fn(each_ref_fn fn, void *cb_data);struct ref_storage_be {struct ref_storage_be *next;const char *name; +ref_init_db_fn *init_db;ref_transaction_commit_fn *transaction_commit;ref_transaction_commit_fn *initial_transaction_commit;
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 9/21 refs: add method to rename refs
In reply to David Turner--- refs.c | 5 +++++ refs/files-backend.c | 4 +++- refs/refs-internal.h | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-)
refs.c +5 -0
@@ -1133,6 +1133,11 @@ int delete_refs(struct string_list *refnames)return the_refs_backend->delete_refs (refnames); } +int rename_ref (const char *oldref,const char *newref,const char *logmsg) +{ +return the_refs_backend->rename_ref (oldref, newref, logmsg); +} +const char *resolve_ref_unsafe (const char *ref,int resolve_flags,unsigned char *sha1,int *flags) {
refs/files-backend.c +3 -1
@@ -2502,7 +2502,8 @@ static int commit_ref_update(struct ref_lock *lock, const unsigned char *sha1, const char *logmsg, int flags, struct strbuf *err); -int rename_ref (const char *oldrefname,const char *newrefname,const char *logmsg) +static int files_rename_ref (const char *oldrefname,const char *newrefname, +const char *logmsg) {unsigned char sha1[20 ], orig_sha1[20 ];int flag =0 , logmoved =0 ;
@@ -3592,6 +3593,7 @@ struct ref_storage_be refs_be_files = { files_peel_ref, files_create_symref, files_delete_refs, + files_rename_ref, files_resolve_ref_unsafe, files_verify_refname_available,
refs/refs-internal.h +9 -0
@@ -67,6 +67,13 @@ int do_for_each_per_worktree_ref(const char *submodule, const char *base, each_ref_fn fn, int trim, int flags, void *cb_data); +/* +* Check if the new name does not conflict with any existing refs +* (other than possibly the old ref). Return 0 if the ref can be +* renamed to the new name. +*/ +int rename_ref_available (const char *oldname,const char *newname); +enum peel_status {/* object was peeled successfully: */ PEEL_PEELED =0 ,
@@ -246,6 +253,7 @@ typedef const char *resolve_ref_unsafe_fn(const char *ref,typedef int verify_refname_available_fn (const char *refname,struct string_list *extra,struct string_list *skip,struct strbuf *err);typedef int resolve_gitlink_ref_fn (const char *path,const char *refname,unsigned char *sha1); +typedef int rename_ref_fn (const char *oldref,const char *newref,const char *logmsg);/* iteration methods */ typedef int head_ref_fn (each_ref_fn fn,void *cb_data);
@@ -284,6 +292,7 @@ struct ref_storage_be {peel_ref_fn *peel_ref;create_symref_fn *create_symref;delete_refs_fn *delete_refs; +rename_ref_fn *rename_ref;resolve_ref_unsafe_fn *resolve_ref_unsafe;verify_refname_available_fn *verify_refname_available;
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 10/21 refs: make lock generic
In reply to David TurnerInstead of using a files-backend-specific struct ref_lock, the generic ref_transaction struct should provide a void pointer that backends can use for their own lock data.
--- refs/files-backend.c | 29 ++++++++++++++++------------- refs/refs-internal.h | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-)
refs/files-backend.c +16 -13
@@ -3208,11 +3208,12 @@ static int files_transaction_commit(struct ref_transaction *transaction,*/ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; +struct ref_lock *lock;if ((update->flags &REF_HAVE_NEW ) &&is_null_sha1 (update->new_sha1)) update->flags |=REF_DELETING ; - update->lock = lock_ref_sha1_basic( + lock = lock_ref_sha1_basic( update->refname, ((update->flags & REF_HAVE_OLD) ? update->old_sha1 : NULL),
@@ -3220,7 +3221,8 @@ static int files_transaction_commit(struct ref_transaction *transaction, update->flags, &update->type, err); -if (!update->lock) { + update->backend_data = lock; +if (!lock) {char *reason; ret = (errno ==ENOTDIR )
@@ -3238,12 +3240,12 @@ static int files_transaction_commit(struct ref_transaction *transaction, (update->flags & REF_NODEREF));if (!overwriting_symref && - !hashcmp (update->lock->old_oid.hash, update->new_sha1)) { + !hashcmp (lock->old_oid.hash, update->new_sha1)) {/* * The reference already has the desired * value, so we don't need to write it. */ - }else if (write_ref_to_lockfile (update->lock, + }else if (write_ref_to_lockfile (lock, update->new_sha1, err)) {char *write_err =strbuf_detach (err,NULL );
@@ -3252,7 +3254,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,* The lock was freed upon failure of * write_ref_to_lockfile(): */ - update->lock =NULL ; + update->backend_data =NULL ;strbuf_addf (err,"cannot update the ref '%s': %s" , update->refname, write_err);
@@ -3268,7 +3270,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,* We didn't have to write anything to the lockfile. * Close it to free up the file descriptor: */ -if (close_ref (update->lock)) { +if (close_ref (lock)) {strbuf_addf (err,"Couldn't close %s.lock" , update->refname); goto cleanup;
@@ -3281,16 +3283,16 @@ static int files_transaction_commit(struct ref_transaction *transaction,struct ref_update *update = updates[i];if (update->flags &REF_NEEDS_COMMIT ) { -if (commit_ref_update (update->lock, +if (commit_ref_update (update->backend_data, update->new_sha1, update->msg, update->flags, err)) {/* freed by commit_ref_update(): */ - update->lock =NULL ; + update->backend_data =NULL ; ret =TRANSACTION_GENERIC_ERROR ; goto cleanup; }else {/* freed by commit_ref_update(): */ - update->lock =NULL ; + update->backend_data =NULL ; } } }
@@ -3298,16 +3300,17 @@ static int files_transaction_commit(struct ref_transaction *transaction,/* Perform deletes now that updates are safely completed */ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; +struct ref_lock *lock = update->backend_data;if (update->flags &REF_DELETING ) { -if (delete_ref_loose (update->lock, update->type, err)) { +if (delete_ref_loose (lock, update->type, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto cleanup; }if (!(update->flags &REF_ISPRUNING ))string_list_append (&refs_to_delete, - update->lock->ref_name); + lock->ref_name); } }
@@ -3323,8 +3326,8 @@ cleanup: transaction->state =REF_TRANSACTION_CLOSED ;for (i =0 ; i < n; i++) -if (updates[i]->lock) -unlock_ref (updates[i]->lock); +if (updates[i]->backend_data) +unlock_ref (updates[i]->backend_data);string_list_clear (&refs_to_delete,0 );string_list_clear (&affected_refnames,0 );return ret;
refs/refs-internal.h +1 -1
@@ -161,7 +161,7 @@ struct ref_update {* REF_DELETING, and REF_ISPRUNING: */ unsigned int flags; -struct ref_lock *lock; +void *backend_data;int type;char *msg;const char refname[FLEX_ARRAY ];
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 11/21 refs: move duplicate check to common code
In reply to David TurnerThe check for duplicate refnames in a transaction is needed for all backends, so move it to the common code.
ref_transaction_commit_fn gains a new argument, the sorted string_list of affected refnames.
--- refs.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++-- refs/files-backend.c | 57 ++++--------------------------------------- refs/refs-internal.h | 1 + 3 files changed, 73 insertions(+), 54 deletions(-)
refs.c +67 -2
@@ -1116,6 +1116,36 @@ int rename_ref_available(const char *oldname, const char *newname)return ret; } +/* +* Return 1 if there are any duplicate refnames in the updates in +* `transaction`, and fill in err with an appropriate error message. +* Fill in `refnames` with the refnames from the transaction. +*/ +static int get_affected_refnames (struct ref_transaction *transaction, +struct string_list *refnames, +struct strbuf *err) +{ +int i, n = transaction->nr; +struct ref_update **updates; + +assert (err); + + updates = transaction->updates; +/* Fail if a refname appears more than once in the transaction: */ +for (i =0 ; i < n; i++) +string_list_append (refnames, updates[i]->refname); +string_list_sort (refnames); + +for (i =1 ; i < n; i++) +if (!strcmp (refnames->items[i -1 ].string, refnames->items[i].string)) { +strbuf_addf (err, +"Multiple updates for ref '%s' not allowed." , + refnames->items[i].string); +return 1 ; + } +return 0 ; +} +/* backend functions */ int refs_init_db (struct strbuf *err,int shared) {
@@ -1125,7 +1155,29 @@ int refs_init_db(struct strbuf *err, int shared)int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) { -return the_refs_backend->transaction_commit (transaction, err); +int ret =-1 ; +struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; + +assert (err); + +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: commit called for transaction that is not open" ); + +if (!transaction->nr) { + transaction->state =REF_TRANSACTION_CLOSED ; +return 0 ; + } + +if (get_affected_refnames (transaction, &affected_refnames, err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + + ret = the_refs_backend->transaction_commit (transaction, + &affected_refnames, err); +done: +string_list_clear (&affected_refnames,0 ); +return ret; }int delete_refs (struct string_list *refnames)
@@ -1277,5 +1329,18 @@ int reflog_expire(const char *refname, const unsigned char *sha1,int initial_ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) { -return the_refs_backend->initial_transaction_commit (transaction, err); +struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; +int ret; + +if (get_affected_refnames (transaction, + &affected_refnames, err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + ret = the_refs_backend->initial_transaction_commit (transaction, + &affected_refnames, + err); +done: +string_list_clear (&affected_refnames,0 ); +return ret; }
refs/files-backend.c +5 -52
117 lines not on this page · raw message
refs/refs-internal.h +1 -0
@@ -217,6 +217,7 @@ int rename_ref_available(const char *oldname, const char *newname);/* refs backends */ typedef int ref_init_db_fn (struct strbuf *err,int shared);typedef int ref_transaction_commit_fn (struct ref_transaction *transaction, +struct string_list *affected_refnames,struct strbuf *err);/* reflog functions */
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 12/21 refs: allow log-only updates
In reply to David TurnerThe refs infrastructure learns about log-only ref updates, which only update the reflog. Later, we will use this to separate symbolic reference resolution from ref updating.
--- refs/files-backend.c | 15 ++++++++++----- refs/refs-internal.h | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-)
refs/files-backend.c +10 -5
@@ -2845,7 +2845,7 @@ static int commit_ref_update(struct ref_lock *lock, } } } -if (commit_ref (lock)) { +if (!(flags &REF_LOG_ONLY ) &&commit_ref (lock)) {error ("Couldn't set %s" , lock->ref_name);unlock_ref (lock);return -1 ;
@@ -3199,7 +3199,8 @@ static int files_transaction_commit(struct ref_transaction *transaction, goto cleanup; }if ((update->flags &REF_HAVE_NEW ) && - !(update->flags &REF_DELETING )) { + !(update->flags &REF_DELETING ) && + !(update->flags &REF_LOG_ONLY )) {int overwriting_symref = ((update->type &REF_ISSYMREF ) && (update->flags &REF_NODEREF ));
@@ -3229,7 +3230,9 @@ static int files_transaction_commit(struct ref_transaction *transaction, update->flags |=REF_NEEDS_COMMIT ; } } -if (!(update->flags &REF_NEEDS_COMMIT )) { + +if (!(update->flags &REF_LOG_ONLY ) && + !(update->flags &REF_NEEDS_COMMIT )) {/* * We didn't have to write anything to the lockfile. * Close it to free up the file descriptor:
@@ -3246,7 +3249,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; -if (update->flags &REF_NEEDS_COMMIT ) { +if (update->flags &REF_NEEDS_COMMIT || + update->flags &REF_LOG_ONLY ) {if (commit_ref_update (update->backend_data, update->new_sha1, update->msg, update->flags, err)) {
@@ -3266,7 +3270,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,struct ref_update *update = updates[i];struct ref_lock *lock = update->backend_data; -if (update->flags &REF_DELETING ) { +if (update->flags &REF_DELETING && + !(update->flags &REF_LOG_ONLY )) {if (delete_ref_loose (lock, update->type, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto cleanup;
refs/refs-internal.h +2 -0
@@ -42,6 +42,8 @@* value to ref_update::flags */ +#define REF_LOG_ONLY 0x80 +/* * Return true iff refname is minimally safe. "Safe" here means that * deleting a loose reference by this name will not do any damage, for
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 13/21 refs: resolve symbolic refs first
In reply to David TurnerBefore committing ref updates, split symbolic ref updates into two
parts: an update to the underlying ref, and a log-only update to the symbolic ref. This ensures that both references are locked correctly while their reflogs are updated.
It is still possible to confuse git by concurrent updates, since the splitting of symbolic refs does not happen under lock. So a symbolic ref could be replaced by a plain ref in the middle of this operation, which would lead to reflog discontinuities and missed old-ref checks.
--- refs.c | 69 +++++++++++++++++++++++++++ refs/files-backend.c | 132 ++++++++++++++++++++++++++------------------------- refs/refs-internal.h | 8 ++++ 3 files changed, 145 insertions(+), 64 deletions(-)
refs.c +69 -0
@@ -1152,6 +1152,71 @@ int refs_init_db(struct strbuf *err, int shared)return the_refs_backend->init_db (err, shared); } +/* +* Special case for symbolic refs when REF_NODEREF is not turned on. +* Dereference them here, mark them REF_LOG_ONLY, and add an update +* for the underlying ref. +*/ +static int dereference_symrefs (struct ref_transaction *transaction, +struct strbuf *err) +{ +int i; +int nr = transaction->nr; + +for (i =0 ; i < nr; i++) { +struct ref_update *update = transaction->updates[i]; +const char *resolved; +unsigned char sha1[20 ]; +int resolve_flags =0 ; +int mustexist = update->flags &REF_HAVE_OLD && + !is_null_sha1 (update->old_sha1); +int deleting = (update->flags &REF_HAVE_NEW ) && +is_null_sha1 (update->new_sha1); + +if (mustexist) + resolve_flags |=RESOLVE_REF_READING ; +if (deleting) + resolve_flags |=RESOLVE_REF_ALLOW_BAD_NAME | +RESOLVE_REF_NO_RECURSE ; + +if (strcmp (update->refname,"HEAD" )) + update->flags |=REF_IS_NOT_HEAD ; + + resolved =resolve_ref_unsafe (update->refname, resolve_flags, + sha1, &update->type); +if (!resolved) { +/* +* We may notice this breakage later and die +* with a sensible error message +*/ + update->type |=REF_ISBROKEN ; +continue ; + } + +hashcpy (update->read_sha1, sha1); + +if (update->flags &REF_NODEREF || + !(update->type &REF_ISSYMREF )) +continue ; + +/* Create a new transaction for the underlying ref */ +if (ref_transaction_update (transaction, + resolved, + update->new_sha1, + (update->flags &REF_HAVE_OLD ) ? + update->old_sha1 :NULL , + update->flags & ~REF_IS_NOT_HEAD , + update->msg, err)) +return -1 ; + +/* Make the symbolic ref update non-recursive */ + update->flags |=REF_LOG_ONLY |REF_NODEREF ; + update->flags &= ~REF_HAVE_OLD ; + } + +return 0 ; +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {
@@ -1168,6 +1233,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,return 0 ; } + ret =dereference_symrefs (transaction, err); +if (ret) + goto done; +if (get_affected_refnames (transaction, &affected_refnames, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto done;
refs/files-backend.c +68 -64
232 lines not on this page · raw message
refs/refs-internal.h +8 -0
@@ -44,6 +44,8 @@#define REF_LOG_ONLY 0x80 +#define REF_IS_NOT_HEAD 0x100 +/* * Return true iff refname is minimally safe. "Safe" here means that * deleting a loose reference by this name will not do any damage, for
@@ -159,6 +161,12 @@ struct ref_update {*/ unsigned char old_sha1[20 ];/* +* During the symbolic ref split stage, we resolve refs. +* We'll re-resolve non-symbolic refs once they are locked, +* but we store this to avoid re-resolving symbolic refs. +*/ +unsigned char read_sha1[20 ]; +/* * One or more of REF_HAVE_NEW, REF_HAVE_OLD, REF_NODEREF, * REF_DELETING, and REF_ISPRUNING: */
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 14/21 refs: always handle non-normal refs in files backend
In reply to David TurnerAlways handle non-normal (per-worktree or pseudo) refs in the files backend instead of alternate backends.
Sometimes a ref transaction will update both a per-worktree ref and a normal ref. For instance, an ordinary commit might update refs/heads/master and HEAD (or at least HEAD's reflog).
Updates to normal refs continue to go through the chosen backend.
Updates to non-normal refs are moved to a separate files backend transaction.
--- refs.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-)
refs.c +79 -2
@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +static const char split_transaction_fail_warning[] =N_ ( +"A ref transaction was split across two refs backends. Part of the " +"transaction succeeded, but then the update to the per-worktree refs " +"failed. Your repository may be in an inconsistent state." ); +/* * We always have a files backend and it is the default. */
@@ -791,6 +796,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} +static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) {
@@ -798,8 +810,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,struct ref_update *update =xcalloc (1 ,sizeof (*update) + len);memcpy ((char *)update->refname, refname, len);/* includes NUL */ -ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); - transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }
@@ -1217,11 +1228,38 @@ static int dereference_symrefs(struct ref_transaction *transaction,return 0 ; } +/* +* Move all non-normal ref updates into a specially-created +* files-backend transaction +*/ +static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct strbuf *err) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +int last; +struct ref_update *update = transaction->updates[i]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL ) +continue ; + + last = --transaction->nr; + transaction->updates[i] = transaction->updates[last]; +add_update_obj (files_transaction, update); + } + +return 0 ; +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {int ret =-1 ;struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list files_affected_refnames =STRING_LIST_INIT_NODUP ; +struct ref_transaction *files_transaction =NULL ;assert (err);
@@ -1237,6 +1275,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,if (ret) goto done; +if (the_refs_backend != &refs_be_files) { + files_transaction =ref_transaction_begin (err); +if (!files_transaction) + goto done; + + ret =move_abnormal_ref_updates (transaction, files_transaction, + err); +if (ret) + goto done; + +/* files backend commit */ +if (get_affected_refnames (files_transaction, + &files_affected_refnames, + err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + } + +/* main backend commit */ if (get_affected_refnames (transaction, &affected_refnames, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto done;
@@ -1244,8 +1302,24 @@ int ref_transaction_commit(struct ref_transaction *transaction, ret = the_refs_backend->transaction_commit (transaction, &affected_refnames, err); +if (ret) + goto done; + +if (files_transaction) { + ret = refs_be_files.transaction_commit (files_transaction, + &files_affected_refnames, + err); +if (ret) { +warning (split_transaction_fail_warning); + goto done; + } + } + done:string_list_clear (&affected_refnames,0 ); +string_list_clear (&files_affected_refnames,0 ); +if (files_transaction) +ref_transaction_free (files_transaction);return ret; }
@@ -1285,6 +1359,9 @@ int peel_ref(const char *refname, unsigned char *sha1)int create_symref (const char *ref_target,const char *refs_heads_master,const char *logmsg) { +if (ref_type (ref_target) !=REF_TYPE_NORMAL ) +return refs_be_files.create_symref (ref_target, refs_heads_master, + logmsg);return the_refs_backend->create_symref (ref_target, refs_heads_master, logmsg); }
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 15/21 init: allow alternate ref strorage to be set for new repos
In reply to David Turnergit init learns a new argument --ref-storage. Presently, only "files" is supported, but later we will add other storage backends.
When this argument is used, the repository's extensions.refStorage configuration value is set (as well as core.repositoryformatversion), and the ref storage backend's initdb function is used to set up the ref database.
--- Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 7 +++++- builtin/init-db.c | 44 +++++++++++++++++++++++++++------- cache.h | 2 ++ contrib/completion/git-completion.bash | 3 ++- path.c | 29 ++++++++++++++++++++-- refs.c | 8 +++++++ refs.h | 8 +++++++ setup.c | 12 ++++++++++ t/t0001-init.sh | 25 +++++++++++++++++++ 10 files changed, 127 insertions(+), 13 deletions(-)
Documentation/git-init-db.txt +1 -1
@@ -9,7 +9,7 @@ git-init-db - Creates an empty Git repository SYNOPSIS -------- [verse] -'git init-db' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] +'git init-db' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [--ref-storage=<name>] DESCRIPTION
Documentation/git-init.txt +6 -1
@@ -12,7 +12,7 @@ SYNOPSIS 'git init' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [directory] - + [--ref-storage=<name>] DESCRIPTION -----------
@@ -113,6 +113,11 @@ does not exist, it will be created. -- +--ref-storage=<name>:: +Type of refs storage backend. Default is to use the original "files" +storage, which stores ref data in files in .git/refs and +.git/packed-refs. + TEMPLATE DIRECTORY ------------------
builtin/init-db.c +36 -8
85 lines not on this page · raw message
cache.h +2 -0
@@ -734,6 +734,8 @@ extern enum object_creation_mode object_creation_mode;extern char *notes_ref_name; +extern const char *ref_storage_backend; +extern int grafts_replace_parents;/*
contrib/completion/git-completion.bash +2 -1
@@ -1362,7 +1362,8 @@ _git_init () return ;; --*) - __gitcomp "--quiet --bare --template= --shared --shared=" + __gitcomp "--quiet --bare --template= --shared --shared= + --ref-storage=" return ;; esac
path.c +27 -2
@@ -2,6 +2,7 @@* Utilities for paths and pathnames */ #include "cache.h" +#include "refs.h" #include "strbuf.h" #include "string-list.h" #include "dir.h"
@@ -511,8 +512,32 @@ int validate_headref(const char *path)int fd;ssize_t len; -if (lstat (path, &st) <0 ) -return -1 ; +if (lstat (path, &st) <0 ) { +int backend_type_set; +struct strbuf config_path =STRBUF_INIT ; +char *pathdup =xstrdup (path); +char *git_dir =dirname (pathdup); +char *storage =NULL ; + +strbuf_addf (&config_path,"%s/%s" , git_dir,"config" ); +free (pathdup); + +if (git_config_from_file (ref_storage_backend_config, + config_path.buf, &storage)) { +strbuf_release (&config_path); +return -1 ; + } + + backend_type_set = !!storage; +free ((void *)storage); +strbuf_release (&config_path); +/* +* Alternate backends are assumed to keep HEAD +* in a valid state, so there's no need to do +* further validation. +*/ +return backend_type_set ?0 :-1 ; + }/* Make sure it is a "refs/.." symlink */ if (S_ISLNK (st.st_mode)) {
refs.c +8 -0
@@ -23,6 +23,14 @@ static struct ref_storage_be *the_refs_backend = &refs_be_files;*/ static struct ref_storage_be *refs_backends = &refs_be_files; +const char *ref_storage_backend ="files" ; + +void register_ref_storage_backend (struct ref_storage_be *be) +{ + be->next = refs_backends; + refs_backends = be; +} +int set_ref_storage_backend (const char *name) {struct ref_storage_be *be;
refs.h +8 -0
@@ -511,10 +511,18 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1, void *policy_cb_data);/* +* Read the refdb storage backend name out of the config file +*/ +int ref_storage_backend_config (const char *var,const char *value,void *ptr); + +struct ref_storage_be ; +/* * Switch to an alternate ref storage backend. */ int set_ref_storage_backend (const char *name);int ref_storage_backend_exists (const char *name); +void register_ref_storage_backend (struct ref_storage_be *be); +#endif /* REFS_H */
setup.c +12 -0
@@ -1,5 +1,6 @@#include "cache.h" #include "dir.h" +#include "refs.h" #include "string-list.h" static int inside_git_dir =-1 ;
@@ -263,6 +264,15 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)return ret; } +int ref_storage_backend_config (const char *var,const char *value,void *ptr) +{ +char **cdata = ptr; + +if (!strcmp (var,"extensions.refstorage" )) + *cdata =xstrdup (value); +return 0 ; +} +/* * Test if it looks like we're at a git directory. * We want to see:
@@ -390,6 +400,8 @@ static int check_repo_format(const char *var, const char *value, void *cb) ; else if (!strcmp(ext, "preciousobjects")) repository_format_precious_objects =git_config_bool (var, value); + else if (!strcmp(ext, "refstorage")) + ref_storage_backend =xstrdup (value); else string_list_append(&unknown_extensions, ext); }
t/t0001-init.sh +25 -0
@@ -174,6 +174,31 @@ test_expect_success 'reinit' ' test_i18ncmp again/empty again/err2 ' +test_expect_success 'init with bogus storage backend fails' ' + + ( + mkdir again2 && + cd again2 && + test_must_fail git init --ref-storage=test >out2 2>err2 && + test_i18ngrep "Unknown ref storage backend test" err2 + ) +' + +test_expect_failure 'reinit with changed storage backend fails' ' + + ( + mkdir again3 && + cd again3 && + git init >out1 2>err1 && + git init --ref-storage=test >out2 2>err2 + ) && + test_i18ngrep "Initialized empty" again3/out1 && + test_i18ngrep "Reinitialized existing" again3/out2 && + >again3/empty && + test_i18ncmp again3/empty again3/err1 && + test_i18ncmp again3/empty again3/err2 +' + test_expect_success 'init with --template' ' mkdir template-source && echo content >template-source/file &&
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 16/21 refs: check submodules ref storage config
In reply to David TurnerAll submodules must have the same ref storage (for now). Confirm that this is so before attempting to do anything with submodule refs.
--- refs.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 8 ++------ 2 files changed, 58 insertions(+), 6 deletions(-)
refs.c +56 -0
@@ -308,6 +308,54 @@ int for_each_tag_ref(each_ref_fn fn, void *cb_data)return for_each_ref_in ("refs/tags/" , fn, cb_data); } +static int submodule_backend (const char *key,const char *value,void *data) +{ +const char **path = data; +char **old_path = data; +if (!strcmp (key,"extensions.refstorage" ) && + !git_config_string (path, key,"extensions.refstorage" )) +free (*old_path); + +return 0 ; +} + +/* +* Check that a submodule exists. If its ref storage backend differs +* from the current backend, die. If the submodule exists, return +* 0. Else return -1. +*/ +static int check_submodule_backend (const char *submodule) +{ +struct strbuf sb =STRBUF_INIT ; +char *submodule_storage_backend; +int result =-1 ; + +if (!submodule) +return 0 ; + + submodule_storage_backend =xstrdup ("files" ); + +strbuf_addstr (&sb, submodule); +if (!is_nonbare_repository_dir (&sb)) + goto done; + +strbuf_reset (&sb); +strbuf_git_path_submodule (&sb, submodule,"config" ); + +git_config_from_file (submodule_backend, sb.buf, + &submodule_storage_backend); +if (strcmp (submodule_storage_backend, ref_storage_backend)) +die (_ ("Ref storage '%s' for submodule '%s' does not match our storage, '%s'" ), + submodule_storage_backend, submodule, ref_storage_backend); + + result =0 ; +done: +free (submodule_storage_backend); +strbuf_release (&sb); + +return result; +} +int for_each_tag_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) {return for_each_ref_in_submodule (submodule,"refs/tags/" , fn, cb_data);
@@ -320,6 +368,7 @@ int for_each_branch_ref(each_ref_fn fn, void *cb_data)int for_each_branch_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) { +check_submodule_backend (submodule);return for_each_ref_in_submodule (submodule,"refs/heads/" , fn, cb_data); }
@@ -330,6 +379,7 @@ int for_each_remote_ref(each_ref_fn fn, void *cb_data)int for_each_remote_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) { +check_submodule_backend (submodule);return for_each_ref_in_submodule (submodule,"refs/remotes/" , fn, cb_data); }
@@ -1377,6 +1427,9 @@ int create_symref(const char *ref_target, const char *refs_heads_master,int resolve_gitlink_ref (const char *path,const char *refname,unsigned char *sha1) { +if (check_submodule_backend (path)) +return -1 ; +return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); }
@@ -1387,6 +1440,7 @@ int head_ref(each_ref_fn fn, void *cb_data)int head_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) { +check_submodule_backend (submodule);return the_refs_backend->head_ref_submodule (submodule, fn, cb_data); }
@@ -1397,6 +1451,7 @@ int for_each_ref(each_ref_fn fn, void *cb_data)int for_each_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) { +check_submodule_backend (submodule);return the_refs_backend->for_each_ref_submodule (submodule, fn, cb_data); }
@@ -1415,6 +1470,7 @@ int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data,int for_each_ref_in_submodule (const char *submodule,const char *prefix,each_ref_fn fn,void *cb_data) { +check_submodule_backend (submodule);return the_refs_backend->for_each_ref_in_submodule (submodule, prefix, fn, cb_data); }
refs/files-backend.c +2 -6
@@ -1357,13 +1357,9 @@ static int files_resolve_gitlink_ref(const char *path, const char *refname,strbuf_add (&submodule, path, len); refs =lookup_ref_cache (submodule.buf); -if (!refs) { -if (!is_nonbare_repository_dir (&submodule)) { -strbuf_release (&submodule); -return -1 ; - } +if (!refs) refs =create_ref_cache (submodule.buf); - } +strbuf_release (&submodule); retval =resolve_gitlink_ref_recursive (refs, refname, sha1,0 );
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 17/21 clone: allow ref storage backend to be set for clone
In reply to David TurnerAdd a new option, --ref-storage, to allow the ref storage backend to be set on new clones.
Submodules must use the same ref storage as the parent repository, so we also pass the --ref-storage option option when cloning submodules.
--- Documentation/git-clone.txt | 5 +++++ builtin/clone.c | 5 +++++ builtin/submodule--helper.c | 2 +- contrib/completion/git-completion.bash | 1 + git-submodule.sh | 13 +++++++++++++ 5 files changed, 25 insertions(+), 1 deletion(-)
Documentation/git-clone.txt +5 -0
@@ -14,6 +14,7 @@ SYNOPSIS [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>] [--dissociate] [--separate-git-dir <git dir>] [--depth <depth>] [--[no-]single-branch] + [--ref-storage=<name>] [--recursive | --recurse-submodules] [--jobs <n>] [--] <repository> [<directory>]
@@ -224,6 +225,10 @@ objects from the source repository into a pack in the cloned repository. The number of submodules fetched at the same time. Defaults to the `submodule.fetchJobs` option. +--ref-storage=<name>:: + Type of ref storage backend. Default is to use the original files + based ref storage. + <repository>:: The (possibly remote) repository to clone from. See the <<URLS,URLS>> section below for more information on specifying
builtin/clone.c +5 -0
@@ -95,6 +95,8 @@ static struct option builtin_clone_options[] = { N_("separate git dir from working tree")),OPT_STRING_LIST ('c' ,"config" , &option_config,N_ ("key=value" ),N_ ("set config inside the new repository" )), +OPT_STRING (0 ,"ref-storage" , &ref_storage_backend, +N_ ("name" ),N_ ("name of backend type to use" )), OPT_END() };
@@ -733,6 +735,9 @@ static int checkout(void)if (max_jobs !=-1 )argv_array_pushf (&args,"--jobs=%d" , max_jobs); +argv_array_pushl (&args,"--ref-storage" , + ref_storage_backend,NULL ); + err =run_command_v_opt (args.argv,RUN_GIT_CMD );argv_array_clear (&args); }
builtin/submodule--helper.c +1 -1
@@ -457,7 +457,7 @@ static int clone_submodule(const char *path, const char *gitdir, const char *urlargv_array_pushl (&cp.args,"--reference" , reference,NULL );if (gitdir && *gitdir)argv_array_pushl (&cp.args,"--separate-git-dir" , gitdir,NULL ); - +argv_array_pushl (&cp.args,"--ref-storage" , ref_storage_backend,NULL );argv_array_push (&cp.args, url);argv_array_push (&cp.args, path);
contrib/completion/git-completion.bash +1 -0
@@ -1092,6 +1092,7 @@ _git_clone () --depth --single-branch --branch + --ref-storage= " return ;;
git-submodule.sh +13 -0
@@ -527,6 +527,14 @@ cmd_update() --checkout) update="checkout" ;; + --ref-storage=*) + ref_storage="$1" + ;; + --ref-storage) + case "$2" in '') usage ;; esac + ref_storage="$2" + shift + ;; --depth) case "$2" in '') usage ;; esac depth="--depth=$2"
@@ -560,6 +568,11 @@ cmd_update() if test -n "$init" then cmd_init "--" "$@" || return + else + if test -n "$ref_storage" + then + usage + fi fi git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 18/21 svn: learn ref-storage argument
In reply to David Turnergit svn learns to pass the ref-storage command-line argument (to init and clone) through to git init.
--- contrib/completion/git-completion.bash | 2 +- git-svn.perl | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-)
contrib/completion/git-completion.bash +1 -1
@@ -2477,7 +2477,7 @@ _git_svn () --branches= --stdlayout --minimize-url --no-metadata --use-svm-props --use-svnsync-props --rewrite-root= --prefix= --use-log-author - --add-author-from $remote_opts + --add-author-from --ref-storage= $remote_opts " local cmt_opts=" --edit --rmdir --find-copies-harder --copy-similarity=
git-svn.perl +5 -1
@@ -141,7 +141,7 @@ my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent, 'localtime' => \$Git::SVN::_localtime, %remote_opts ); -my ($_trunk, @_tags, @_branches, $_stdlayout); +my ($_trunk, @_tags, @_branches, $_stdlayout, $_ref_storage); my %icv; my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared, 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
@@ -153,6 +153,7 @@ my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared, 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 }, 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] }, 'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] }, + 'ref-storage=s' => \$_ref_storage, %remote_opts ); my %cmt_opts = ( 'edit|e' => \$_edit, 'rmdir' => \$Git::SVN::Editor::_rmdir,
@@ -469,6 +470,9 @@ sub do_git_init_db { push @init_db, "--shared"; } } + if (defined $_ref_storage) { + push @init_db, "--ref-storage=" . $_ref_storage; + } command_noisy(@init_db); $_repository = Git->repository(Repository => ".git"); }
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 19/21 refs: add register_ref_storage_backends()
In reply to David TurnerThis new function will register all known ref storage backends... once there are any other than the default. For now, it's a no-op.
--- builtin/init-db.c | 3 +++ config.c | 25 +++++++++++++++++++++++++ refs.c | 8 ++++++++ refs.h | 2 ++ 4 files changed, 38 insertions(+)
builtin/init-db.c +3 -0
@@ -226,6 +226,7 @@ static int create_default_files(const char *template_path)if (strcmp (ref_storage_backend,"files" )) {git_config_set ("extensions.refStorage" , ref_storage_backend);git_config_set ("core.repositoryformatversion" , ref_storage_backend); +register_ref_storage_backends ();if (set_ref_storage_backend (ref_storage_backend))die (_ ("Unknown ref storage backend %s" ), ref_storage_backend);
@@ -503,6 +504,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) argc =parse_options (argc, argv, prefix, init_db_options, init_db_usage,0 ); +register_ref_storage_backends (); +if (requested_ref_storage_backend && !ref_storage_backend_exists (requested_ref_storage_backend)) die(_("Unknown ref storage backend %s"),
config.c +25 -0
@@ -11,6 +11,7 @@#include "strbuf.h" #include "quote.h" #include "hashmap.h" +#include "refs.h" #include "string-list.h" #include "utf8.h"
@@ -1207,6 +1208,30 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) }if (repo_config && !access_or_die (repo_config,R_OK ,0 )) { +char *storage =NULL ; + +/* +* make sure we always read the ref storage config +* from the extensions section on startup +*/ + ret +=git_config_from_file (ref_storage_backend_config, + repo_config, &storage); + +register_ref_storage_backends (); +if (!storage) + storage =xstrdup ("" ); + +if ((!*storage) || + (!strcmp (storage,"files" ))) { +/* default backend, nothing to do */ +free (storage); + }else { + ref_storage_backend = storage; +if (set_ref_storage_backend (ref_storage_backend)) +die (_ ("Unknown ref storage backend %s" ), + ref_storage_backend); + } + ret +=git_config_from_file (fn, repo_config, data); found +=1 ; }
refs.c +8 -0
@@ -1554,3 +1554,11 @@ done:string_list_clear (&affected_refnames,0 );return ret; } + +void register_ref_storage_backends (void ) { +/* +* No need to register the files backend; it's registered by +* default. Add register_ref_storage_backend(ptr-to-backend) +* entries below when you add a new backend. +*/ +}
refs.h +2 -0
@@ -525,4 +525,6 @@ int ref_storage_backend_exists(const char *name);void register_ref_storage_backend (struct ref_storage_be *be); +void register_ref_storage_backends (void ); +#endif /* REFS_H */
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 20/21 refs: add LMDB refs storage backend
In reply to David TurnerAdd a database backend for refs using LMDB. This backend runs git for-each-ref about 30% faster than the files backend with fully-packed refs on a repo with ~120k refs. It's also about 4x faster than using fully-unpacked refs. In addition, and perhaps more importantly, it avoids case-conflict issues on OS X.
LMDB has a few features that make it suitable for usage in git:
1. It is relatively lightweight; it requires only one header file, and the library code takes under 64k at runtime.
2. It is well-tested: it's been used in OpenLDAP for years.
3. It's very fast. LMDB's benchmarks show that it is among the fastest key-value stores.
4. It has a relatively simple concurrency story; readers don't block writers and writers don't block readers.
Ronnie Sahlberg's original version of this patchset used tdb. The major disadvantage of tdb is that tdb is hard to build on OS X. It's also not in homebrew. So lmdb seemed simpler.
To test this backend's correctness, I hacked test-lib.sh and test-lib-functions.sh to run all tests under the refs backend. Dozens of tests use manual ref/reflog reading/writing, or create submodules without passing --ref-storage to git init. If those tests are changed to use the update-ref machinery or test-refs-lmdb-backend (or, in the case of packed-refs, corrupt refs, and dumb fetch tests, are skipped), the only remaining failing tests are the git-new-workdir tests and the gitweb tests.
--- .gitignore | 1 + Documentation/config.txt | 7 + Documentation/git-clone.txt | 3 +- Documentation/git-init.txt | 2 +- Documentation/technical/refs-lmdb-backend.txt | 52 + Documentation/technical/repository-version.txt | 5 + Makefile | 12 + configure.ac | 33 + contrib/workdir/git-new-workdir | 3 + refs.c | 3 + refs.h | 2 + refs/lmdb-backend.c | 2052 ++++++++++++++++++++++++ setup.c | 11 +- test-refs-lmdb-backend.c | 64 + transport.c | 7 +- 15 files changed, 2250 insertions(+), 7 deletions(-) create mode 100644 Documentation/technical/refs-lmdb-backend.txt create mode 100644 refs/lmdb-backend.c create mode 100644 test-refs-lmdb-backend.c
.gitignore +1 -0
@@ -199,6 +199,7 @@ /test-path-utils /test-prio-queue /test-read-cache +/test-refs-lmdb-backend /test-regex /test-revision-walking /test-run-command
Documentation/config.txt +7 -0
@@ -1153,6 +1153,13 @@ difftool.<tool>.cmd:: difftool.prompt:: Prompt before each invocation of the diff tool. +extensions.refsStorage:: + Type of refs storage backend. Default is to use the original + files based ref storage. When set to "lmdb", refs are stored in + the lmdb database backend. This setting reflects the refs + backend that is currently in use; it is not possible to change + the backend by changing this setting. + fetch.recurseSubmodules:: This option can be either set to a boolean value or to 'on-demand'. Setting it to a boolean changes the behavior of fetch and pull to
Documentation/git-clone.txt +2 -1
@@ -227,7 +227,8 @@ objects from the source repository into a pack in the cloned repository. --ref-storage=<name>:: Type of ref storage backend. Default is to use the original files - based ref storage. + based ref storage. Set to "lmdb" to store refs in the lmdb database + backend. <repository>:: The (possibly remote) repository to clone from. See the
Documentation/git-init.txt +1 -1
@@ -116,7 +116,7 @@ does not exist, it will be created. --ref-storage=<name>:: Type of refs storage backend. Default is to use the original "files" storage, which stores ref data in files in .git/refs and -.git/packed-refs. +.git/packed-refs. Set to "lmdb" to activate the lmdb storage backend. TEMPLATE DIRECTORY ------------------
Documentation/technical/refs-lmdb-backend.txt new file +52 -0
52 lines not on this page · raw message
Documentation/technical/repository-version.txt +5 -0
@@ -86,3 +86,8 @@ for testing format-1 compatibility. When the config key `extensions.preciousObjects` is set to `true`, objects in the repository MUST NOT be deleted (e.g., by `git-prune` or `git repack -d`). + +`refStorage` +~~~~~~~~~~~~ +This extension allows the use of alternate ref storage backends. The +only defined value is `lmdb`.
Makefile +12 -0
@@ -1042,6 +1042,17 @@ ifdef USE_LIBPCRE EXTLIBS += -lpcre endif +ifdef USE_LIBLMDB + BASIC_CFLAGS += -DUSE_LIBLMDB + ifdef LIBLMDBDIR + BASIC_CFLAGS += -I$(LIBLMDBDIR)/include + EXTLIBS += -L$(LIBLMDBDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBLMDBDIR)/$(lib) + endif + EXTLIBS += -llmdb + LIB_OBJS += refs/lmdb-backend.o + TEST_PROGRAMS_NEED_X += test-refs-lmdb-backend +endif + ifdef HAVE_ALLOCA_H BASIC_CFLAGS += -DHAVE_ALLOCA_H endif
@@ -2140,6 +2151,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_CURL=\''$(subst ','\'',$(subst ','\'',$(NO_CURL)))'\' >>$@+ @echo NO_EXPAT=\''$(subst ','\'',$(subst ','\'',$(NO_EXPAT)))'\' >>$@+ @echo USE_LIBPCRE=\''$(subst ','\'',$(subst ','\'',$(USE_LIBPCRE)))'\' >>$@+ + @echo USE_LIBLMDB=\''$(subst ','\'',$(subst ','\'',$(USE_LIBLMDB)))'\' >>$@+ @echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+ @echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+ @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+
configure.ac +33 -0
@@ -271,6 +271,24 @@ AS_HELP_STRING([], [ARG can be also prefix for libpcre library and hea dnl it yet. GIT_CONF_SUBST([LIBPCREDIR]) fi) + +USE_LIBLMDB=YesPlease +AC_ARG_WITH(liblmdb, +AS_HELP_STRING([--with-liblmdb],[support lmdb (default is YES]) +AS_HELP_STRING([], [ARG can be also prefix for liblmdb library and headers]), + if test "$withval" = "no"; then + USE_LIBLMDB= + elif test "$withval" = "yes"; then + USE_LIBLMDB=YesPlease + else + USE_LIBLMDB=YesPlease + LIBLMDBDIR=$withval + AC_MSG_NOTICE([Setting LIBLMDBDIR to $LIBLMDBDIR]) + dnl USE_LIBLMDB can still be modified below, so don't substitute + dnl it yet. + GIT_CONF_SUBST([LIBLMDBDIR]) + fi) + # # Define HAVE_ALLOCA_H if you have working alloca(3) defined in that header. AC_FUNC_ALLOCA
@@ -510,6 +528,21 @@ GIT_CONF_SUBST([USE_LIBPCRE]) fi +if test -n "$USE_LIBLMDB"; then + +GIT_STASH_FLAGS($LIBLMDBDIR) + +AC_CHECK_LIB([lmdb], [mdb_env_open], +[USE_LIBLMDB=YesPlease], +[USE_LIBLMDB=]) + +GIT_UNSTASH_FLAGS($LIBLMDBDIR) + +GIT_CONF_SUBST([USE_LIBLMDB]) + +fi + + # # Define NO_CURL if you do not have libcurl installed. git-http-pull and # git-http-push are not built, and you cannot use http:// and https://
contrib/workdir/git-new-workdir +3 -0
@@ -28,6 +28,9 @@ git_dir=$(cd "$orig_git" 2>/dev/null && git rev-parse --git-dir 2>/dev/null) || die "Not a git repository: \"$orig_git\"" +ref_storage=$(git config extensions.refstorage || echo "files") +test "$ref_storage" != "files" && die "git-new-workdir is incompatible with ref storage other than 'files'" + case "$git_dir" in .git) git_dir="$orig_git/.git"
refs.c +3 -0
@@ -1561,4 +1561,7 @@ void register_ref_storage_backends(void) {* default. Add register_ref_storage_backend(ptr-to-backend) * entries below when you add a new backend. */ +#ifdef USE_LIBLMDB +register_ref_storage_backend (&refs_be_lmdb); +#endif }
refs.h +2 -0
@@ -516,6 +516,8 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,int ref_storage_backend_config (const char *var,const char *value,void *ptr);struct ref_storage_be ; + +extern struct ref_storage_be refs_be_lmdb;/* * Switch to an alternate ref storage backend. */
refs/lmdb-backend.c new file +2052 -0
2,052 lines not on this page · raw message
setup.c +8 -3
@@ -279,7 +279,7 @@ int ref_storage_backend_config(const char *var, const char *value, void *ptr)* * - either an objects/ directory _or_ the proper * GIT_OBJECT_DIRECTORY environment variable -* - a refs/ directory +* - a refdb/ directory or a refs/ directory * - either a HEAD symlink or a HEAD file that is formatted as * a proper "ref:", or a regular file HEAD that has a properly * formatted sha1 object name.
@@ -313,8 +313,13 @@ int is_git_directory(const char *suspect)strbuf_setlen (&path, len);strbuf_addstr (&path,"/refs" ); -if (access (path.buf,X_OK )) - goto done; + +if (access (path.buf,X_OK )) { +strbuf_setlen (&path, len); +strbuf_addstr (&path,"/refdb" ); +if (access (path.buf,X_OK )) + goto done; + } ret =1 ; done:
test-refs-lmdb-backend.c new file +64 -0
64 lines not on this page · raw message
transport.c +5 -2
@@ -1089,8 +1089,11 @@ static int refs_from_alternate_cb(struct alternate_object_database *e, goto out;/* Is this a git repository with refs? */ memcpy (other + len -8 ,"/refs" ,6 ); -if (!is_directory (other)) - goto out; +if (!is_directory (other)) { +memcpy (other + len -8 ,"/refdb" ,7 ); +if (!is_directory (other)) + goto out; + } other[len -8 ] ='\0' ; remote =remote_get (other); transport =transport_get (remote, other);
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v4 21/21 refs: tests for lmdb backend
In reply to David TurnerAdd tests for the database backend.
--- t/t1460-refs-lmdb-backend.sh | 1109 +++++++++++++++++++++++++++++++++++ t/t1470-refs-lmdb-backend-reflog.sh | 359 ++++++++++++ t/t1480-refs-lmdb-submodule.sh | 85 +++ t/test-lib.sh | 1 + 4 files changed, 1554 insertions(+) create mode 100755 t/t1460-refs-lmdb-backend.sh create mode 100755 t/t1470-refs-lmdb-backend-reflog.sh create mode 100755 t/t1480-refs-lmdb-submodule.sh
t/t1460-refs-lmdb-backend.sh new file +1109 -0
@@ -0,0 +1,1109 @@ +#!/bin/sh +# +# Copyright (c) 2015 Twitter, Inc +# Copyright (c) 2006 Shawn Pearce +# This test is based on t1400-update-ref.sh +# + +test_description='Test lmdb refs backend' +TEST_NO_CREATE_REPO=1 +. ./test-lib.sh + +if ! test_have_prereq LMDB +then + skip_all="Skipping lmdb refs backend tests, lmdb backend not built" + test_done +fi + +raw_ref() { + test-refs-lmdb-backend "$1" +} + +delete_ref() { + test-refs-lmdb-backend -d "$1" +} + +write_ref() { + test-refs-lmdb-backend "$1" "$2" +} + +raw_reflog() { + test-refs-lmdb-backend -l "$1" +} + +delete_all_reflogs() { + test-refs-lmdb-backend -c +} + +append_reflog() { + test-refs-lmdb-backend -a "$1" +} + +Z=$_z40 + +test_expect_success setup ' + git init --ref-storage=lmdb && + for name in A B C D E F + do + test_tick && + T=$(git write-tree) && + sha1=$(echo $name | git commit-tree $T) && + eval $name=$sha1 + done +' + +m=refs/heads/master +n_dir=refs/heads/gu +n=$n_dir/fixes + +test_expect_success \ + "create $m" \ + "git update-ref $m $A && + test $A"' = $(raw_ref '"$m"')' +test_expect_success \ + "create $m" \ + "git update-ref $m $B $A && + test $B"' = $(raw_ref '"$m"')' +test_expect_success "fail to delete $m with stale ref" ' + test_must_fail git update-ref -d $m $A && + test $B = "$(raw_ref $m)" +' +test_expect_success "delete $m" ' + git update-ref -d $m $B && + ! raw_ref $m +' +delete_ref $m + +test_expect_success "delete $m without oldvalue verification" " + git update-ref $m $A && + test $A = \$(raw_ref $m) && + git update-ref -d $m && + ! raw_ref $m +" +delete_ref $m + +test_expect_success \ + "fail to create $n" \ + "git update-ref $n_dir $A && + test_must_fail git update-ref $n $A >out 2>err" + +delete_ref $n_dir +rm -f out err + +test_expect_success \ + "create $m (by HEAD)" \ + "git update-ref HEAD $A && + test $A"' = $(raw_ref '"$m"')' +test_expect_success \ + "create $m (by HEAD)" \ + "git update-ref HEAD $B $A && + test $B"' = $(raw_ref '"$m"')' +test_expect_success "fail to delete $m (by HEAD) with stale ref" ' + test_must_fail git update-ref -d HEAD $A && + test $B = $(raw_ref '"$m"') +' +test_expect_success "delete $m (by HEAD)" ' + git update-ref -d HEAD $B && + ! raw_ref $m +' +delete_ref $m + +test_expect_success \ + "create $m (by HEAD)" \ + "git update-ref HEAD $A && + test $A"' = $(raw_ref '"$m"')' +test_expect_success \ + "pack refs" \ + "git pack-refs --all" +test_expect_success \ + "move $m (by HEAD)" \ + "git update-ref HEAD $B $A && + test $B"' = $(raw_ref '"$m"')' +test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" ' + git update-ref -d HEAD $B && + ! raw_ref $m +' +delete_ref $m + +OLD_HEAD=$(raw_ref HEAD) +test_expect_success "delete symref without dereference" ' + git update-ref --no-deref -d HEAD && + ! raw_ref HEAD +' +write_ref HEAD "$OLD_HEAD" + +test_expect_success "delete symref without dereference when the referred ref is packed" ' + echo foo >foo.c && + git add foo.c && + git commit -m foo && + git pack-refs --all && + git update-ref --no-deref -d HEAD && + ! raw_ref HEAD +' +write_ref HEAD "$OLD_HEAD" +delete_ref $m + +test_expect_success 'update-ref -d is not confused by self-reference' ' + git symbolic-ref refs/heads/self refs/heads/self && + test_when_finished "delete_ref refs/heads/self" && + test_must_fail git update-ref -d refs/heads/self +' + +test_expect_success 'update-ref --no-deref -d can delete self-reference' ' + git symbolic-ref refs/heads/self refs/heads/self && + test_when_finished "delete_ref refs/heads/self" && + git update-ref --no-deref -d refs/heads/self +' + +test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' ' + test-refs-lmdb-backend refs/heads/bad "" && + test_when_finished "delete_ref refs/heads/bad" && + git symbolic-ref refs/heads/ref-to-bad refs/heads/bad && + test_when_finished "delete_ref refs/heads/ref-to-bad" && + raw_ref refs/heads/ref-to-bad && + git update-ref --no-deref -d refs/heads/ref-to-bad && + ! raw_ref refs/heads/ref-to-bad +' + +test_expect_success '(not) create HEAD with old sha1' " + test_must_fail git update-ref HEAD $A $B +" +test_expect_success "(not) prior created .git/$m" " + ! raw_ref $m +" +delete_ref $m + +test_expect_success \ + "create HEAD" \ + "git update-ref HEAD $A" +test_expect_success '(not) change HEAD with wrong SHA1' " + test_must_fail git update-ref HEAD $B $Z +" +test_expect_success "(not) changed .git/$m" " + ! test $B"' = $(raw_ref '"$m"') +' + +: a repository with working tree always has reflog these days... +delete_all_reflogs +: | append_reflog $m +delete_ref $m + +test_expect_success \ + "create $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:30" \ + git update-ref HEAD '"$A"' -m "Initial Creation" && + test '"$A"' = $(raw_ref '"$m"')' +test_expect_success \ + "update $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:31" \ + git update-ref HEAD'" $B $A "'-m "Switch" && + test '"$B"' = $(raw_ref '"$m"')' +test_expect_success \ + "set $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:41" \ + git update-ref HEAD'" $A && + test $A"' = $(raw_ref '"$m"')' + +cat >expect <<EOF +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 +EOF +test_expect_success \ + "verifying $m's log" \ + "raw_reflog $m >actual && + test_cmp expect actual" +delete_ref $m +delete_all_reflogs +: | append_reflog $m +rm -f actual expect + +test_expect_success \ + 'enable core.logAllRefUpdates' \ + 'git config core.logAllRefUpdates true && + test true = $(git config --bool --get core.logAllRefUpdates)' + +test_expect_success \ + "create $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:32" \ + git update-ref HEAD'" $A "'-m "Initial Creation" && + test '"$A"' = $(raw_ref '"$m"')' +test_expect_success \ + "update $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:33" \ + git update-ref HEAD'" $B $A "'-m "Switch" && + test '"$B"' = $(raw_ref '"$m"')' +test_expect_success \ + "set $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:43" \ + git update-ref HEAD '"$A && + test $A"' = $(raw_ref '"$m"')' + +cat >expect <<EOF +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000 +EOF +test_expect_success \ + "verifying $m's log" \ + 'raw_reflog $m >actual && + test_cmp expect actual' +delete_ref $m +rm -f expect + +git update-ref $m $D +git reflog expire --expire=all $m + +append_reflog $m <<EOF +0000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500 +$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500 +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500 +$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500 +$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500 +EOF + +ed="Thu, 26 May 2005 18:32:00 -0500" +gd="Thu, 26 May 2005 18:33:00 -0500" +ld="Thu, 26 May 2005 18:43:00 -0500" +test_expect_success \ + 'Query "master@{May 25 2005}" (before history)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 25 2005}" >o 2>e && + test '"$C"' = $(cat o) && + test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"' +test_expect_success \ + "Query master@{2005-05-25} (before history)" \ + 'rm -f o e && + git rev-parse --verify master@{2005-05-25} >o 2>e && + test '"$C"' = $(cat o) && + echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e && + test '"$C"' = $(cat o) && + test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e && + test '"$C"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:32:30}" (first non-creation change)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 26 2005 23:32:30}" >o 2>e && + test '"$A"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e && + test '"$B"' = $(cat o) && + test "warning: Log for ref '"$m has gap after $gd"'." = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:38:00}" (middle of history)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e && + test '"$Z"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e && + test '"$E"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-28}" (past end of history)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-28}" >o 2>e && + test '"$D"' = $(cat o) && + test "warning: Log for ref '"$m unexpectedly ended on $ld"'." = "$(cat e)"' + + +git reflog expire --expire=all $m +delete_ref $m + +test_expect_success \ + 'creating initial files' \ + 'echo TEST >F && + git add F && + GIT_AUTHOR_DATE="2005-05-26 23:30" \ + GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a && + h_TEST=$(git rev-parse --verify HEAD) && + echo The other day this did not work. >M && + echo And then Bob told me how to fix it. >>M && + echo OTHER >F && + GIT_AUTHOR_DATE="2005-05-26 23:41" \ + GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a && + h_OTHER=$(git rev-parse --verify HEAD) && + GIT_AUTHOR_DATE="2005-05-26 23:44" \ + GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend && + h_FIXED=$(git rev-parse --verify HEAD) && + echo Merged initial commit and a later commit. >M && + echo $h_TEST >.git/MERGE_HEAD && + GIT_AUTHOR_DATE="2005-05-26 23:45" \ + GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M && + h_MERGED=$(git rev-parse --verify HEAD) && + rm -f M' + +cat >expect <<EOF +$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add +$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work. +$h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work. +$h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit. +EOF +test_expect_success \ + 'git commit logged updates' \ + "raw_reflog $m >actual && + test_cmp expect actual" +unset h_TEST h_OTHER h_FIXED h_MERGED + +test_expect_success \ + 'git cat-file blob master:F (expect OTHER)' \ + 'test OTHER = $(git cat-file blob master:F)' +test_expect_success \ + 'git cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \ + 'test TEST = $(git cat-file blob "master@{2005-05-26 23:30}:F")' +test_expect_success \ + 'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \ + 'test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")' + +a=refs/heads/a +b=refs/heads/b +c=refs/heads/c +E='""' +F='%s\0' +pws='path with space' + +test_expect_success 'stdin test setup' ' + echo "$pws" >"$pws" && + git add -- "$pws" && + git commit -m "$pws" +' + +test_expect_success '-z fails without --stdin' ' + test_must_fail git update-ref -z $m $m $m 2>err && + grep "usage: git update-ref" err +' + +test_expect_success 'stdin works with no input' ' + >stdin && + git update-ref --stdin <stdin && + git rev-parse --verify -q $m +' + +test_expect_success 'stdin fails on empty line' ' + echo "" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: empty command in input" err +' + +test_expect_success 'stdin fails on only whitespace' ' + echo " " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: whitespace before command: " err +' + +test_expect_success 'stdin fails on leading whitespace' ' + echo " create $a $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: whitespace before command: create $a $m" err +' + +test_expect_success 'stdin fails on unknown command' ' + echo "unknown $a" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: unknown command: unknown $a" err +' + +test_expect_success 'stdin fails on unbalanced quotes' ' + echo "create $a \"master" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: badly quoted argument: \\\"master" err +' + +test_expect_success 'stdin fails on invalid escape' ' + echo "create $a \"ma\zter\"" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: badly quoted argument: \\\"ma\\\\zter\\\"" err +' + +test_expect_success 'stdin fails on junk after quoted argument' ' + echo "create \"$a\"master" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: unexpected character after quoted argument: \\\"$a\\\"master" err +' + +test_expect_success 'stdin fails create with no ref' ' + echo "create " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create: missing <ref>" err +' + +test_expect_success 'stdin fails create with no new value' ' + echo "create $a" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $a: missing <newvalue>" err +' + +test_expect_success 'stdin fails create with too many arguments' ' + echo "create $a $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $a: extra input: $m" err +' + +test_expect_success 'stdin fails update with no ref' ' + echo "update " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update: missing <ref>" err +' + +test_expect_success 'stdin fails update with no new value' ' + echo "update $a" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update $a: missing <newvalue>" err +' + +test_expect_success 'stdin fails update with too many arguments' ' + echo "update $a $m $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update $a: extra input: $m" err +' + +test_expect_success 'stdin fails delete with no ref' ' + echo "delete " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: delete: missing <ref>" err +' + +test_expect_success 'stdin fails delete with too many arguments' ' + echo "delete $a $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: delete $a: extra input: $m" err +' + +test_expect_success 'stdin fails verify with too many arguments' ' + echo "verify $a $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: verify $a: extra input: $m" err +' + +test_expect_success 'stdin fails option with unknown name' ' + echo "option unknown" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: option unknown: unknown" err +' + +test_expect_success 'stdin fails with duplicate refs' ' + cat >stdin <<-EOF && + create $a $m + create $b $m + create $a $m + EOF + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err +' + +test_expect_success 'stdin create ref works' ' + echo "create $a $m" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin succeeds with quoted argument' ' + git update-ref -d $a && + echo "create $a \"$m\"" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin succeeds with escaped character' ' + git update-ref -d $a && + echo "create $a \"ma\\163ter\"" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin update ref creates with zero old value' ' + echo "update $b $m $Z" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual && + git update-ref -d $b +' + +test_expect_success 'stdin update ref creates with empty old value' ' + echo "update $b $m $E" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin create ref works with path with space to blob' ' + echo "create refs/blobs/pws \"$m:$pws\"" >stdin && + git update-ref --stdin <stdin && + git rev-parse "$m:$pws" >expect && + git rev-parse refs/blobs/pws >actual && + test_cmp expect actual && + git update-ref -d refs/blobs/pws +' + +test_expect_success 'stdin update ref fails with wrong old value' ' + echo "update $c $m $m~1" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin update ref fails with bad old value' ' + echo "update $c $m does-not-exist" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin create ref fails with bad new value' ' + echo "create $c does-not-exist" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $c: invalid <newvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin create ref fails with zero new value' ' + echo "create $c " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $c: zero <newvalue>" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin update ref works with right old value' ' + echo "update $b $m~1 $m" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete ref fails with wrong old value' ' + echo "delete $a $m~1" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$a'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete ref fails with zero old value' ' + echo "delete $a " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: delete $a: zero <oldvalue>" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin update symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + cat >stdin <<-EOF && + option no-deref + update refs/TESTSYMREF $a $b + EOF + git update-ref --stdin <stdin && + git rev-parse refs/TESTSYMREF >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + cat >stdin <<-EOF && + option no-deref + delete refs/TESTSYMREF $b + EOF + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/TESTSYMREF && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete ref works with right old value' ' + echo "delete $b $m~1" >stdin && + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q $b +' + +test_expect_success 'stdin update/create/verify combination works' ' + cat >stdin <<-EOF && + update $a $m + create $b $m + verify $c + EOF + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin verify succeeds for correct value' ' + git rev-parse $m >expect && + echo "verify $m $m" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin verify succeeds for missing reference' ' + echo "verify refs/heads/missing $Z" >stdin && + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin verify treats no value as missing' ' + echo "verify refs/heads/missing" >stdin && + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin verify fails for wrong value' ' + git rev-parse $m >expect && + echo "verify $m $m~1" >stdin && + test_must_fail git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin verify fails for mistaken null value' ' + git rev-parse $m >expect && + echo "verify $m $Z" >stdin && + test_must_fail git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin verify fails for mistaken empty value' ' + M=$(git rev-parse $m) && + test_when_finished "git update-ref $m $M" && + git rev-parse $m >expect && + echo "verify $m" >stdin && + test_must_fail git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin update refs works with identity updates' ' + cat >stdin <<-EOF && + update $a $m $m + update $b $m $m + update $c $Z $E + EOF + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin update refs fails with wrong old value' ' + git update-ref $c $m && + cat >stdin <<-EOF && + update $a $m $m + update $b $m $m + update $c '' + EOF + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + git rev-parse $c >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete refs works with packed and loose refs' ' + git pack-refs --all && + git update-ref $c $m~1 && + cat >stdin <<-EOF && + delete $a $m + update $b $Z $m + update $c $E $m~1 + EOF + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q $a && + test_must_fail git rev-parse --verify -q $b && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z works on empty input' ' + >stdin && + git update-ref -z --stdin <stdin && + git rev-parse --verify -q $m +' + +test_expect_success 'stdin -z fails on empty line' ' + echo "" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: whitespace before command: " err +' + +test_expect_success 'stdin -z fails on empty command' ' + printf $F "" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: empty command in input" err +' + +test_expect_success 'stdin -z fails on only whitespace' ' + printf $F " " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: whitespace before command: " err +' + +test_expect_success 'stdin -z fails on leading whitespace' ' + printf $F " create $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: whitespace before command: create $a" err +' + +test_expect_success 'stdin -z fails on unknown command' ' + printf $F "unknown $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: unknown $a" err +' + +test_expect_success 'stdin -z fails create with no ref' ' + printf $F "create " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create: missing <ref>" err +' + +test_expect_success 'stdin -z fails create with no new value' ' + printf $F "create $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create $a: unexpected end of input when reading <newvalue>" err +' + +test_expect_success 'stdin -z fails create with too many arguments' ' + printf $F "create $a" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails update with no ref' ' + printf $F "update " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update: missing <ref>" err +' + +test_expect_success 'stdin -z fails update with too few args' ' + printf $F "update $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z emits warning with empty new value' ' + git update-ref $a $m && + printf $F "update $a" "" "" >stdin && + git update-ref -z --stdin <stdin 2>err && + grep "warning: update $a: missing <newvalue>, treating as zero" err && + test_must_fail git rev-parse --verify -q $a +' + +test_expect_success 'stdin -z fails update with no new value' ' + printf $F "update $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $a: unexpected end of input when reading <newvalue>" err +' + +test_expect_success 'stdin -z fails update with no old value' ' + printf $F "update $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z fails update with too many arguments' ' + printf $F "update $m" "$m" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails delete with no ref' ' + printf $F "delete " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: delete: missing <ref>" err +' + +test_expect_success 'stdin -z fails delete with no old value' ' + printf $F "delete $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z fails delete with too many arguments' ' + printf $F "delete $m" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails verify with too many arguments' ' + printf $F "verify $m" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails verify with no old value' ' + printf $F "verify $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z fails option with unknown name' ' + printf $F "option unknown" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: option unknown: unknown" err +' + +test_expect_success 'stdin -z fails with duplicate refs' ' + printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err +' + +test_expect_success 'stdin -z create ref works' ' + printf $F "create $a" "$m" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z update ref creates with zero old value' ' + printf $F "update $b" "$m" "$Z" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual && + git update-ref -d $b +' + +test_expect_success 'stdin -z update ref creates with empty old value' ' + printf $F "update $b" "$m" "" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z create ref works with path with space to blob' ' + printf $F "create refs/blobs/pws" "$m:$pws" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse "$m:$pws" >expect && + git rev-parse refs/blobs/pws >actual && + test_cmp expect actual && + git update-ref -d refs/blobs/pws +' + +test_expect_success 'stdin -z update ref fails with wrong old value' ' + printf $F "update $c" "$m" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z update ref fails with bad old value' ' + printf $F "update $c" "$m" "does-not-exist" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z create ref fails when ref exists' ' + git update-ref $c $m && + git rev-parse "$c" >expect && + printf $F "create $c" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + git rev-parse "$c" >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z create ref fails with bad new value' ' + git update-ref -d "$c" && + printf $F "create $c" "does-not-exist" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create $c: invalid <newvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z create ref fails with empty new value' ' + printf $F "create $c" "" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create $c: missing <newvalue>" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z update ref works with right old value' ' + printf $F "update $b" "$m~1" "$m" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete ref fails with wrong old value' ' + printf $F "delete $a" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$a'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete ref fails with zero old value' ' + printf $F "delete $a" "$Z" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: delete $a: zero <oldvalue>" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z update symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + printf $F "option no-deref" "update refs/TESTSYMREF" "$a" "$b" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse refs/TESTSYMREF >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + printf $F "option no-deref" "delete refs/TESTSYMREF" "$b" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/TESTSYMREF && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete ref works with right old value' ' + printf $F "delete $b" "$m~1" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q $b +' + +test_expect_success 'stdin -z update/create/verify combination works' ' + printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z verify succeeds for correct value' ' + git rev-parse $m >expect && + printf $F "verify $m" "$m" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z verify succeeds for missing reference' ' + printf $F "verify refs/heads/missing" "$Z" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin -z verify treats no value as missing' ' + printf $F "verify refs/heads/missing" "" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin -z verify fails for wrong value' ' + git rev-parse $m >expect && + printf $F "verify $m" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z verify fails for mistaken null value' ' + git rev-parse $m >expect && + printf $F "verify $m" "$Z" >stdin && + test_must_fail git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z verify fails for mistaken empty value' ' + M=$(git rev-parse $m) && + test_when_finished "git update-ref $m $M" && + git rev-parse $m >expect && + printf $F "verify $m" "" >stdin && + test_must_fail git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z update refs works with identity updates' ' + printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z update refs fails with wrong old value' ' + git update-ref $c $m && + printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + git rev-parse $c >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete refs works with packed and loose refs' ' + git pack-refs --all && + git update-ref $c $m~1 && + printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q $a && + test_must_fail git rev-parse --verify -q $b && + test_must_fail git rev-parse --verify -q $c +' + +test_done
t/t1470-refs-lmdb-backend-reflog.sh new file +359 -0
359 lines not on this page · raw message
t/t1480-refs-lmdb-submodule.sh new file +85 -0
85 lines not on this page · raw message
t/test-lib.sh +1 -0
@@ -960,6 +960,7 @@ test -z "$NO_PERL" && test_set_prereq PERL test -z "$NO_PYTHON" && test_set_prereq PYTHON test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE test -z "$NO_GETTEXT" && test_set_prereq GETTEXT +test -n "$USE_LIBLMDB" && test_set_prereq LMDB # Can we rely on git's output in the C locale? if test -n "$GETTEXT_POISON"
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
Quoted 20 lines · David Turner
Changes to this version:
re-rolled on top of pu as-of9db66d9f1aa.Bug fixes include:
For submodules: memory leaks; segfault on bad config. (thanks to Peff) In symref splitting: check that would always succeed (thanks to Peff) A bogus double-declaration of a var (thanks to Ramsay Jones) Two memory leaks (thanks to Thomas Gummerer)
An unused var (thanks to Duy Nguyen)Other improvements are:
Strings prepped for 18n (thanks to Duy Nguyen)
Cleaner submodule handling (thanks to Peff)
Whitelisting instead of blacklisting in git-new-workdir (thanks to
Thomas Gummerer)
Allow older gits to recognize lmdb-backend git repos (thanks to Duy Nguyen) Tab completion and cleaner commit messages (thanks SZEDER Gábor) Removed some #ifdefs, moving all backend setup to one place (thanks to Duy Nguyen)
Quoted 1 line
Thanks to all for reviews.
Thanks for a reroll.
Applied this on top of a merge between the current 'master' and 'sb/submodule-parallel-update' topic to untangle the dependency; otherwise there is no way for this topic to make progress X-<.
Let's see how well this round goes.
Re: [PATCH v4 02/21] refs: add methods for misc ref operations
In reply to David TurnerQuoted 20 lines · David Turner
From: Ronnie Sahlberg <[email protected]>
Add ref backend methods for:
resolve_ref_unsafe,verify_refname_available,pack_refs,peel_ref,create_symref,resolve_gitlink_ref.Signed-off-by Ronnie Sahlberg <[email protected]>Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 1 + refs.c | 36 ++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 33 +++++++++++++++++++++++---------- refs/refs-internal.h | 23 +++++++++++++++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-)builtin/init-db.c +0
-0
builtin/init-db.c line 11
Quoted 4 lines
builtin/init-db.c +1
-0@@ -8,6 +8,7 @@#include "builtin.h" #include "exec_cmd.h" #include "parse-options.h" +#include "refs.h"
You can't see it in this diff's context, but there is already an '#include "refs.h"' in this file.
Quoted 1 line
[...]
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 03/21] refs: add methods for the ref iterators
In reply to David TurnerQuoted 19 lines · David Turner
From: Ronnie Sahlberg <[email protected]>
Signed-off-by Ronnie Sahlberg <[email protected]>Signed-off-by David Turner <[email protected]>--- refs.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 41 +++++++++++++++++++++++++++------------ refs/refs-internal.h | 29 ++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 12 deletions(-)refs.c +1
-0@@ -1158,3 +1158,57 @@ int resolve_gitlink_ref(const char *path, const char *refname, {return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); } +
refs.c int resolve_gitlink_ref(const char *path, const char *refname, · line 1171
Quoted 10 lines
refs.c +10
-0+int head_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref (fn, cb_data); +} + +int head_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref_submodule (submodule, fn, cb_data); +} +
I think it is unnecessary to have so many virtual functions. For example, here you have made head_ref_submodule() virtual. But the files and lmdb implementations of this function are identical. They both call do_head_ref(), which are (confusingly) two independent static functions. But those functions are *also* defined identically, namely
Quoted 18 lines
static int
do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data){ struct object_id oid; int flag; if (submodule) { if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0) return fn("HEAD", &oid, 0, cb_data); return 0; } if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag)) return fn("HEAD", &oid, flag, cb_data); return 0; }
After all, what else would they possibly want to do? And both resolve_gitlink_ref() and read_ref_full() are already virtual functions (actually read_ref_full() only has a single definition but it calls the virtualized resolve_ref_unsafe()).
So it seems to me that it is unnecessary for head_ref_submodule() to be virtual, and that there only needs to be one definition of do_head_ref().
(Off-topic: for that matter, do_head_ref() is itself almost pointless. It could easily be inlined if we were sure that nobody passes it submodule==NULL.)
Similarly, I bet that if do_for_each_ref() were just a little bit more capable, then the following functions could also remain non-virtual (i.e., one definition could be shared across all backend implementations):
* for_each_ref()
* for_each_ref_submodule()
* for_each_ref_in()
* for_each_fullref_in()
* for_each_ref_in_submodule()
* for_each_replace_ref()
* for_each_namespaced_ref()
* for_each_rawref()
Quoted 39 lines
[...]
refs/refs-internal.h +22
-0@@ -216,6 +216,24 @@ typedef int verify_refname_available_fn(const char *refname, struct string_listtypedef int resolve_gitlink_ref_fn (const char *path,const char *refname,unsigned char *sha1); +/* iteration methods */ +typedef int head_ref_fn (each_ref_fn fn,void *cb_data); +typedef int head_ref_submodule_fn (const char *submodule,each_ref_fn fn, +void *cb_data); +typedef int for_each_ref_fn (each_ref_fn fn,void *cb_data); +typedef int for_each_ref_submodule_fn (const char *submodule,each_ref_fn fn, +void *cb_data); +typedef int for_each_ref_in_fn (const char *prefix,each_ref_fn fn, +void *cb_data); +typedef int for_each_fullref_in_fn (const char *prefix,each_ref_fn fn, +void *cb_data,unsigned int broken); +typedef int for_each_ref_in_submodule_fn (const char *submodule, +const char *prefix, +each_ref_fn fn,void *cb_data); +typedef int for_each_rawref_fn (each_ref_fn fn,void *cb_data); +typedef int for_each_namespaced_ref_fn (each_ref_fn fn,void *cb_data); +typedef int for_each_replace_ref_fn (each_ref_fn fn,void *cb_data); +struct ref_storage_be {struct ref_storage_be *next;const char *name;@@ -228,6 +246,17 @@ struct ref_storage_be {resolve_ref_unsafe_fn *resolve_ref_unsafe;verify_refname_available_fn *verify_refname_available;resolve_gitlink_ref_fn *resolve_gitlink_ref; + +head_ref_fn *head_ref; +head_ref_submodule_fn *head_ref_submodule; +for_each_ref_fn *for_each_ref;
refs/refs-internal.h struct ref_storage_be { · line 262
Quoted 10 lines
refs/refs-internal.h +7
-0+for_each_ref_submodule_fn *for_each_ref_submodule; +for_each_ref_in_fn *for_each_ref_in; +for_each_fullref_in_fn *for_each_fullref_in; +for_each_ref_in_submodule_fn *for_each_ref_in_submodule; +for_each_rawref_fn *for_each_rawref; +for_each_namespaced_ref_fn *for_each_namespaced_ref; +for_each_replace_ref_fn *for_each_replace_ref; };extern struct ref_storage_be refs_be_files;
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 08/21] refs: add methods to init refs db
In reply to David TurnerQuoted 18 lines · David Turner
Alternate refs backends might not need the refs/heads directory and so on, so we make ref db initialization part of the backend.
Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 20 ++++++++++---------- refs.c | 5 +++++ refs.h | 2 ++ refs/files-backend.c | 16 ++++++++++++++++ refs/refs-internal.h | 2 ++ 5 files changed, 35 insertions(+), 10 deletions(-)[...]
refs.h +0
-0
refs.h extern int ref_exists(const char *refname); · line 69
Quoted 4 lines
refs.h +1
-0@@ -66,6 +66,8 @@ extern int ref_exists(const char *refname);extern int is_branch (const char *refname); +extern int refs_init_db (struct strbuf *err,int shared);
The convention elsewhere is for "struct strbuf *err" to be the *last* parameter.
Quoted 1 line
[...]
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 08/21] refs: add methods to init refs db
Quoted 21 lines · Michael Haggerty
Quoted 20 lines · David Turner
Alternate refs backends might not need the refs/heads directory and so
on, so we make ref db initialization part of the backend.Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 20 ++++++++++---------- refs.c | 5 +++++ refs.h | 2 ++ refs/files-backend.c | 16 ++++++++++++++++ refs/refs-internal.h | 2 ++ 5 files changed, 35 insertions(+), 10 deletions(-)[...]
refs.h +0
-0@@ -66,6 +66,8 @@ extern int ref_exists(const char *refname);
Quoted 6 lines
refs.h extern int ref_exists(const char *refname); · line 69
Quoted 3 lines
refs.h +1
-0extern int is_branch (const char *refname); +extern int refs_init_db (struct strbuf *err,int shared);The convention elsewhere is for "struct strbuf *err" to be the *last* parameter.
Fixed, thanks.
Re: [PATCH v4 09/21] refs: add method to rename refs
In reply to David TurnerQuoted 13 lines · David Turner
Signed-off-by David Turner <[email protected]>--- refs.c | 5 +++++ refs/files-backend.c | 4 +++- refs/refs-internal.h | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-)[...]
refs/refs-internal.h +0
-0
refs/refs-internal.h int do_for_each_per_worktree_ref(const char *submodule, const char *base, · line 75
Quoted 9 lines
refs/refs-internal.h +6
-0@@ -67,6 +67,13 @@ int do_for_each_per_worktree_ref(const char *submodule, const char *base, each_ref_fn fn, int trim, int flags, void *cb_data); +/* +* Check if the new name does not conflict with any existing refs +* (other than possibly the old ref). Return 0 if the ref can be +* renamed to the new name. +*/ +int rename_ref_available (const char *oldname,const char *newname);
Thanks for adding a docstring for this function. But this function already has a declaration later in the file so you should remove one or the other.
Quoted 1 line
[...]
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 09/21] refs: add method to rename refs
Quoted 19 lines · Michael Haggerty
Quoted 18 lines · David Turner
Signed-off-by David Turner <[email protected]>--- refs.c | 5 +++++ refs/files-backend.c | 4 +++- refs/refs-internal.h | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-)[...]
refs/refs-internal.h +0
-0@@ -67,6 +67,13 @@ int do_for_each_per_worktree_ref(const char *submodule, const char *base, each_ref_fn fn, int trim, int flags, void *cb_data);
Quoted 12 lines
refs/refs-internal.h int do_for_each_per_worktree_ref(const char *submodule, const char *base,
Quoted 7 lines
refs/refs-internal.h +6
-0+/* +* Check if the new name does not conflict with any existing refs +* (other than possibly the old ref). Return 0 if the ref can be +* renamed to the new name. +*/ +int rename_ref_available (const char *oldname,const char *newname);Thanks for adding a docstring for this function. But this function already has a declaration later in the file so you should remove one or
the other.
Fixed, thanks.
Re: [PATCH v4 12/21] refs: allow log-only updates
In reply to David TurnerQuoted 3 lines · David Turner
The refs infrastructure learns about log-only ref updates, which only update the reflog. Later, we will use this to separate symbolic reference resolution from ref updating.
This looks good. I assume it will get some testing later in the series.
refs/refs-internal.h line 46
Quoted 11 lines
[...]
refs/refs-internal.h +2
-0@@ -42,6 +42,8 @@* value to ref_update::flags */ +#define REF_LOG_ONLY 0x80 +
Please add a comment explaining the meaning/purpose of this flag.
Quoted 1 line
[...]
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 12/21] refs: allow log-only updates
Quoted 8 lines · Michael Haggerty
Quoted 4 lines · David Turner
The refs infrastructure learns about log-only ref updates, which only
update the reflog. Later, we will use this to separate symbolic reference resolution from ref updating.This looks good. I assume it will get some testing later in the series.
The existing tests cover this pretty well, I think. It's not intended to have user-visible results, so it's hard to specifically test.
Quoted 9 lines
Quoted 9 lines
[...]
refs/refs-internal.h +0
-0@@ -42,6 +42,8 @@* value to ref_update::flags */
Quoted 4 lines
refs/refs-internal.h line 46
Quoted 2 lines
refs/refs-internal.h +2
-0+#define REF_LOG_ONLY 0x80 +Please add a comment explaining the meaning/purpose of this flag.
Commented, thanks.
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
In reply to David TurnerQuoted 5 lines · David Turner
Add a database backend for refs using LMDB. This backend runs git for-each-ref about 30% faster than the files backend with fully-packed refs on a repo with ~120k refs. It's also about 4x faster than using fully-unpacked refs. In addition, and perhaps more importantly, it avoids case-conflict issues on OS X.
When I compile this using gcc with -Werror=pointer-arith, I get the following errors:
Quoted 6 lines
refs/lmdb-backend.c: In function ‘rename_reflog_ent’:refs/lmdb-backend.c:1068:39: error: pointer of type ‘void *’ used in arithmetic [-Werror=pointer-arith]strbuf_add(&new_key_buf, key.mv_data + key.mv_size - 8, 8); ^
refs/lmdb-backend.c:1068:53: error: pointer of type ‘void *’ used in arithmetic [-Werror=pointer-arith]
strbuf_add(&new_key_buf, key.mv_data + key.mv_size - 8, 8);
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
Quoted 9 lines · Michael Haggerty
Quoted 7 lines · David Turner
Add a database backend for refs using LMDB. This backend runs git for-each-ref about 30% faster than the files backend with fully -packed
refs on a repo with ~120k refs. It's also about 4x faster than using
fully-unpacked refs. In addition, and perhaps more importantly, it avoids case-conflict issues on OS X.
Quoted 11 lines
When I compile this using gcc with -Werror=pointer-arith, I get the following errors:
Quoted 8 lines
refs/lmdb-backend.c: In function ‘rename_reflog_ent’:refs/lmdb-backend.c:1068:39: error: pointer of type ‘void *’ used in arithmetic [-Werror=pointer-arith]strbuf_add(&new_key_buf, key.mv_data + key.mv_size - 8, 8); ^
refs/lmdb-backend.c:1068:53: error: pointer of type ‘void *’ used in arithmetic [-Werror=pointer-arith]
strbuf_add(&new_key_buf, key.mv_data + key.mv_size - 8, 8);
Fixed, thanks.
Re: [PATCH v4 02/21] refs: add methods for misc ref operations
In reply to Michael HaggertyQuoted 16 lines · Michael Haggerty
Quoted 15 lines · David Turner
From: Ronnie Sahlberg <[email protected]>
Add ref backend methods for:
resolve_ref_unsafe,verify_refname_available,pack_refs,peel_ref,create_symref,resolve_gitlink_ref.Signed-off-by Ronnie Sahlberg <[email protected]>Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 1 + refs.c | 36 ++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 33 +++++++++++++++++++++++---------- refs/refs-internal.h | 23 +++++++++++++++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-)
Quoted 12 lines
builtin/init-db.c line 11
Quoted 9 lines
builtin/init-db.c +1
-0@@ -8,6 +8,7 @@#include "builtin.h" #include "exec_cmd.h" #include "parse-options.h" +#include "refs.h" You can't see it in this diff's context, but there is already an '#include "
refs.h"' in this file.
Fixed this.
I think that's all of your comments so far, but I'm going to wait for your second batch to reroll, to avoid unnecessary churn.
Re: [PATCH v4 03/21] refs: add methods for the ref iterators
In reply to Michael HaggertyQuoted 57 lines · Michael Haggerty
Quoted 32 lines · David Turner
From: Ronnie Sahlberg <[email protected]>
Signed-off-by Ronnie Sahlberg <[email protected]>Signed-off-by David Turner <[email protected]>--- refs.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 41 +++++++++++++++++++++++++++------------ refs/refs-internal.h | 29 ++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 12 deletions(-)refs.c +0
-0@@ -1158,3 +1158,57 @@ int resolve_gitlink_ref(const char *path, const char *refname, { return the_refs_backend->resolve_gitlink_ref(path,refname, sha1);
}refs.c +7
-0+ +int head_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref (fn, cb_data); +} + +int head_ref_submodule(const char *submodule, each_ref_fn fn, void
*cb_data)refs.c +2
-0+{ + return the_refs_backend->head_ref_submodule(submodule, fn,
cb_data);refs.c
Quoted 2 lines
refs.c +2
-0+} +I think it is unnecessary to have so many virtual functions. For example, here you have made
head_ref_submodule()virtual. But the files
and lmdb implementations of this function are identical. They both calldo_head_ref(), which are (confusingly) two independent static functions.
But those functions are *also* defined identically, namelyAfter all, what else would they possibly want to do? And both
resolve_gitlink_ref()andread_ref_full()are already virtual functions
(actuallyread_ref_full()only has a single definition but it calls the
virtualizedresolve_ref_unsafe()).So it seems to me that it is unnecessary for
head_ref_submodule()to be
virtual, and that there only needs to be one definition ofdo_head_ref().
Quoted 4 lines
(Off-topic: for that matter,
do_head_ref()is itself almost pointless.
It could easily be inlined if we were sure that nobody passes it submodule==NULL.)
Added a commit to move these to common code and inline do_head_ref.
Quoted 8 lines
Similarly, I bet that if
do_for_each_ref()were just a little bit more
capable, then the following functions could also remain non-virtual (i.e., one definition could be shared across all backend implementations):*
for_each_ref()
...
Yeah, that makes sense. I think we've grown a few more of those funcs since Ronnie's version, so virtualizing do_for_each_ref makes more sense.
Re: [PATCH v4 13/21] refs: resolve symbolic refs first
In reply to David TurnerQuoted 9 lines · David Turner
Before committing ref updates, split symbolic ref updates into two
parts: an update to the underlying ref, and a log-only update to the symbolic ref. This ensures that both references are locked correctly while their reflogs are updated.It is still possible to confuse git by concurrent updates, since the splitting of symbolic refs does not happen under lock. So a symbolic ref could be replaced by a plain ref in the middle of this operation, which would lead to reflog discontinuities and missed old-ref checks.
This patch is doing too much at once for my little brain to follow.
My first hangup is the change to setting RESOLVE_REF_NO_RECURSE unconditionally in lock_ref_sha1_basic(). I count five callers of that function and see no justification for why the change is OK in the context of each caller. Here are some thoughts:
* The call from files_create_symref() sets REF_NODEREF, so it is unaffected by this change.
* The call from files_transaction_commit() is preceded by a call to dereference_symrefs(), which I assume effectively replaces the need for RESOLVE_REF_NO_RECURSE.
* There are two calls from files_rename_ref(). Why is it OK to do without RESOLVE_REF_NO_RECURSE there?
* For the oldrefname call, I suppose the justification is the "(flag &REF_ISSYMREF)" check earlier in the function. (But does this introduce a significant TOCTOU race?)
* For the newrefname call, I suppose it's because the code a little
higher up tries to delete any existing reference with that name. It looks to me like the old code was slightly broken: if newrefname was an unborn symbolic reference, then: read_ref_full() would fail; delete_ref() would be skipped; lock_ref_sha1_basic() would lock the *referred-to* reference; the referred-to reference would be overwritten instead of newrefname. So it could be that here REF_NODEREF indirectly fixes a bug?
* The last call, from files_reflog_expire(), is also questionable before your patch. If refname is a symref, then the function is expiring the reflog of the symref. But (before this patch) it locks not the symref but its referent. This was discussed in some length before on the mailing list [1], and the conclusion was that the current behavior is wrong, but for backwards compatibility reasons it would be safest to change it to locking *both* the symref and its referent.
If possible, it would be better to split this patch up into several: the first few would each add the REF_NODEREF flag at one callsite, with a careful justification of why that is OK. Once all the callsites (except the one in files_transaction_commit()) have been changed, then the last patch could add the dereference_symrefs() machinery and change the last callsite.
(I'm not certain that those steps are actually doable independently, given that REF_NODEREF has other effects besides setting RESOLVE_REF_NO_RECURSE.)
I'm not just being pedantic here. The patch as written is really too big to review effectively.
Michael
[1]
http://thread.gmane.org/gmane.comp.version-control.git/263552/focus=263555
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 14/21] refs: always handle non-normal refs in files backend
In reply to David TurnerQuoted 76 lines · David Turner
Always handle non-normal (per-worktree or pseudo) refs in the files backend instead of alternate backends.
Sometimes a ref transaction will update both a per-worktree ref and a normal ref. For instance, an ordinary commit might update refs/heads/master and HEAD (or at least HEAD's reflog).
Updates to normal refs continue to go through the chosen backend.
Updates to non-normal refs are moved to a separate files backend transaction.
Signed-off-by David Turner <[email protected]>--- refs.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-)refs.c +28
-2@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +static const char split_transaction_fail_warning[] =N_ ( +"A ref transaction was split across two refs backends. Part of the " +"transaction succeeded, but then the update to the per-worktree refs " +"failed. Your repository may be in an inconsistent state." ); +/* * We always have a files backend and it is the default. */ @@ -791,6 +796,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} +static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) {@@ -798,8 +810,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,struct ref_update *update =xcalloc (1 ,sizeof (*update) + len);memcpy ((char *)update->refname, refname, len);/* includes NUL */ -ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); - transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }@@ -1217,11 +1228,38 @@ static int dereference_symrefs(struct ref_transaction *transaction,return 0 ; } +/* +* Move all non-normal ref updates into a specially-created +* files-backend transaction +*/ +static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct strbuf *err) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +int last; +struct ref_update *update = transaction->updates[i]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL )
refs.c static int dereference_symrefs(struct ref_transaction *transaction, · line 1255
Quoted 10 lines
refs.c +10
-0+continue ; + + last = --transaction->nr; + transaction->updates[i] = transaction->updates[last]; +add_update_obj (files_transaction, update); + } + +return 0 ; +} +
I think this function is incorrect. The update that was previously at transaction->updates[i] never gets checked for abnormality. You could fix that and also avoid gratuitously changing the order of the updates like this:
static int move_abnormal_ref_updates(struct ref_transaction *transaction,
struct ref_transaction *files_transaction, struct strbuf *err)
{
int i, normal_nr = 0;
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];
if (ref_type(update->refname) == REF_TYPE_NORMAL)
transaction->updates[normal_nr++] = update;
else
add_update_obj(files_transaction, update);
}
transaction->nr = normal_nr;
return 0;
}Another alternative would be to set
update->flags |= REF_ABNORMAL
for the abnormal references and *leave them* in the original transaction while also adding them to files_transactions. Then teach non-files backends to skip over updates with REF_ABNORMAL.
The reason I thought of this is that long-term I'd like to make it possible for some reference updates to fail without aborting the transaction. To implement that, we would want a way for ref_transaction_commit() to report back to its caller *which* updates failed, and an obvious way to do that would be to store the reference-specific errors in struct ref_update. If you leave the abnormal ref_updates in the main transaction, then my hoped-for change would be easier.
But that's a separate and hypothetical idea, so you don't have to let it influence how you implement this patch.
Quoted 64 lines
refs.c +38
-0int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {int ret =-1 ;struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list files_affected_refnames =STRING_LIST_INIT_NODUP ; +struct ref_transaction *files_transaction =NULL ;assert (err);@@ -1237,6 +1275,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,if (ret) goto done; +if (the_refs_backend != &refs_be_files) { + files_transaction =ref_transaction_begin (err); +if (!files_transaction) + goto done; + + ret =move_abnormal_ref_updates (transaction, files_transaction, + err); +if (ret) + goto done; + +/* files backend commit */ +if (get_affected_refnames (files_transaction, + &files_affected_refnames, + err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + } + +/* main backend commit */ if (get_affected_refnames (transaction, &affected_refnames, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto done;@@ -1244,8 +1302,24 @@ int ref_transaction_commit(struct ref_transaction *transaction, ret = the_refs_backend->transaction_commit (transaction, &affected_refnames, err); +if (ret) + goto done; + +if (files_transaction) { + ret = refs_be_files.transaction_commit (files_transaction, + &files_affected_refnames, + err); +if (ret) { +warning (split_transaction_fail_warning); + goto done; + } + } + done:string_list_clear (&affected_refnames,0 ); +string_list_clear (&files_affected_refnames,0 ); +if (files_transaction) +ref_transaction_free (files_transaction);return ret; }
refs.c int peel_ref(const char *refname, unsigned char *sha1) · line 1367
Quoted 9 lines
refs.c +3
-0@@ -1285,6 +1359,9 @@ int peel_ref(const char *refname, unsigned char *sha1)int create_symref (const char *ref_target,const char *refs_heads_master,const char *logmsg) { +if (ref_type (ref_target) !=REF_TYPE_NORMAL ) +return refs_be_files.create_symref (ref_target, refs_heads_master, + logmsg);return the_refs_backend->create_symref (ref_target, refs_heads_master, logmsg); }
Does initial_ref_transaction_commit() need the same treatment?
I think files_rename_ref() will break if one of the references is normal and one is abnormal. I think it would be OK to prohibit renaming abnormal refs entirely (can anybody think of an important use case?), but that function should at least do the checks.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 15/21] init: allow alternate ref strorage to be set for new repos
In reply to David TurnerQuoted 38 lines · David Turner
git init learns a new argument --ref-storage. Presently, only "files" is supported, but later we will add other storage backends.
When this argument is used, the repository's extensions.refStorage configuration value is set (as well as core.repositoryformatversion), and the ref storage backend's initdb function is used to set up the ref database.
Signed-off-by David Turner <[email protected]>Signed-off-by SZEDER Gábor <[email protected]>--- Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 7 +++++- builtin/init-db.c | 44 +++++++++++++++++++++++++++------- cache.h | 2 ++ contrib/completion/git-completion.bash | 3 ++- path.c | 29 ++++++++++++++++++++-- refs.c | 8 +++++++ refs.h | 8 +++++++ setup.c | 12 ++++++++++ t/t0001-init.sh | 25 +++++++++++++++++++ 10 files changed, 127 insertions(+), 13 deletions(-)[...]
Documentation/git-init.txt +1
-1@@ -12,7 +12,7 @@ SYNOPSIS 'git init' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [directory] - + [--ref-storage=<name>] DESCRIPTION -----------
Documentation/git-init.txt does not exist, it will be created. · line 120
Quoted 8 lines
Documentation/git-init.txt +5
-0@@ -113,6 +113,11 @@ does not exist, it will be created. -- +--ref-storage=<name>:: +Type of refs storage backend. Default is to use the original "files" +storage, which stores ref data in files in .git/refs and +.git/packed-refs. +
Technically, that should be $GIT_DIR/refs and $GIT_DIR/packed-refs. But it might be that we are not so picky about the distinction in the user docs.
Quoted 24 lines
Documentation/git-init.txt +0
-0TEMPLATE DIRECTORY ------------------builtin/init-db.c +2
-0@@ -24,6 +24,7 @@ static int init_is_bare_repository = 0;static int init_shared_repository =-1 ;static const char *init_db_template_dir;static const char *git_link; +static char *requested_ref_storage_backend;static void copy_templates_1 (struct strbuf *path,struct strbuf *template,DIR *dir)@@ -179,6 +180,7 @@ static int create_default_files(const char *template_path)int reinit;int filemode;struct strbuf err =STRBUF_INIT ; +int repo_version =0 ;/* Just look for `init.templatedir` */ git_config (git_init_db_config,NULL );
builtin/init-db.c static int create_default_files(const char *template_path) · line 210
Quoted 4 lines
builtin/init-db.c +1
-0@@ -205,6 +207,32 @@ static int create_default_files(const char *template_path) }/* +* Create the default symlink from ".git/HEAD" to the "master"
I know that you are just moving this comment, but s/symlink/symref/ would make it more up-to-date.
Quoted 62 lines
builtin/init-db.c +2
-0+* branch, if it does not exist yet. +*/ setup.c +12
-0@@ -1,5 +1,6 @@#include "cache.h" #include "dir.h" +#include "refs.h" #include "string-list.h" static int inside_git_dir =-1 ;@@ -263,6 +264,15 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)return ret; } +int ref_storage_backend_config (const char *var,const char *value,void *ptr) +{ +char **cdata = ptr; + +if (!strcmp (var,"extensions.refstorage" )) + *cdata =xstrdup (value); +return 0 ; +} +/* * Test if it looks like we're at a git directory. * We want to see: @@ -390,6 +400,8 @@ static int check_repo_format(const char *var, const char *value, void *cb) ; else if (!strcmp(ext, "preciousobjects")) repository_format_precious_objects =git_config_bool (var, value); + else if (!strcmp(ext, "refstorage")) + ref_storage_backend =xstrdup (value); else string_list_append(&unknown_extensions, ext); }t/t0001-init.sh +15
-0@@ -174,6 +174,31 @@ test_expect_success 'reinit' ' test_i18ncmp again/empty again/err2 ' +test_expect_success 'init with bogus storage backend fails' ' + + ( + mkdir again2 && + cd again2 && + test_must_fail git init --ref-storage=test >out2 2>err2 && + test_i18ngrep "Unknown ref storage backend test" err2 + ) +' + +test_expect_failure 'reinit with changed storage backend fails' ' + + ( + mkdir again3 && + cd again3 &&
t/t0001-init.sh test_expect_success 'reinit' ' · line 201
Quoted 10 lines
t/t0001-init.sh +10
-0+ git init >out1 2>err1 && + git init --ref-storage=test >out2 2>err2 + ) && + test_i18ngrep "Initialized empty" again3/out1 && + test_i18ngrep "Reinitialized existing" again3/out2 && + >again3/empty && + test_i18ncmp again3/empty again3/err1 && + test_i18ncmp again3/empty again3/err2 +' +
Is it worth testing re-initializing with the same --ref-storage? Perhaps not.
t/t0001-init.sh test_expect_success 'reinit' ' · line 204
Quoted 4 lines
t/t0001-init.sh +0
-0test_expect_success 'init with --template' ' mkdir template-source && echo content >template-source/file &&
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 19/21] refs: add register_ref_storage_backends()
In reply to David TurnerQuoted 33 lines · David Turner
This new function will register all known ref storage backends... once there are any other than the default. For now, it's a no-op.
Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 3 +++ config.c | 25 +++++++++++++++++++++++++ refs.c | 8 ++++++++ refs.h | 2 ++ 4 files changed, 38 insertions(+)[...]
config.c +6
-0@@ -11,6 +11,7 @@#include "strbuf.h" #include "quote.h" #include "hashmap.h" +#include "refs.h" #include "string-list.h" #include "utf8.h" @@ -1207,6 +1208,30 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) }if (repo_config && !access_or_die (repo_config,R_OK ,0 )) { +char *storage =NULL ; + +/* +* make sure we always read the ref storage config +* from the extensions section on startup
config.c int git_config_early(config_fn_t fn, void *data, const char *repo_config) · line 1225
Quoted 10 lines
config.c +10
-0+*/ + ret +=git_config_from_file (ref_storage_backend_config, + repo_config, &storage); + +register_ref_storage_backends (); +if (!storage) + storage =xstrdup ("" ); + +if ((!*storage) || + (!strcmp (storage,"files" ))) {
Nit: you have some unnecessary parentheses here.
Quoted 17 lines
config.c +9
-0+/* default backend, nothing to do */ +free (storage); + }else { + ref_storage_backend = storage; +if (set_ref_storage_backend (ref_storage_backend)) +die (_ ("Unknown ref storage backend %s" ), + ref_storage_backend); + } + ret +=git_config_from_file (fn, repo_config, data); found +=1 ; }refs.c +0
-0
refs.c done: · line 1563
Quoted 10 lines
refs.c +7
-0@@ -1554,3 +1554,11 @@ done:string_list_clear (&affected_refnames,0 );return ret; } + +void register_ref_storage_backends (void ) { +/* +* No need to register the files backend; it's registered by +* default. Add register_ref_storage_backend(ptr-to-backend) +* entries below when you add a new backend. +*/
This function must be called every run, right? So why not make it register the "files" backend explicitly? That would make it obvious really quick if this function fails to get called in some code path. Just a thought...
refs.c done: · line 1564
Quoted 2 lines
refs.c +1
-0+}
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
In reply to David TurnerQuoted 8 lines · David Turner
Add a database backend for refs using LMDB. This backend runs git for-each-ref about 30% faster than the files backend with fully-packed refs on a repo with ~120k refs. It's also about 4x faster than using fully-unpacked refs. In addition, and perhaps more importantly, it avoids case-conflict issues on OS X.
LMDB has a few features that make it suitable for usage in git:
0. It is licensed under the OpenLDAP Public License, which is apparently GPL compatible [1].
[1] http://www.gnu.org/licenses/license-list.en.html#newOpenLDAP
Quoted 15 lines
1. It is relatively lightweight; it requires only one header file, and the library code takes under 64k at runtime.
2. It is well-tested: it's been used in OpenLDAP for years.
3. It's very fast. LMDB's benchmarks show that it is among the fastest key-value stores.
4. It has a relatively simple concurrency story; readers don't block writers and writers don't block readers.
Ronnie Sahlberg's original version of this patchset used tdb. The major disadvantage of tdb is that tdb is hard to build on OS X. It's also not in homebrew. So lmdb seemed simpler.
Quoted 8 lines
To test this backend's correctness, I hacked
test-lib.shandtest-lib-functions.shto run all tests under the refs backend. Dozens of tests use manual ref/reflog reading/writing, or create submodules without passing --ref-storage to git init. If those tests are changed to use the update-ref machinery or test-refs-lmdb-backend (or, in the case of packed-refs, corrupt refs, and dumb fetch tests, are skipped), the only remaining failing tests are the git-new-workdir tests and the gitweb tests.
Would it possible to build your "hack" into the test suite? For example, perhaps one could implement an option that requests tests to use LMDB wherever possible and skip the tests that are not LMDB-compatible. Over time, we could try to increase the fraction of tests that are LMDB-compatible by (for example) using git update-ref and git rev-parse rather than reading/writing reference files via the filesystem directly.
Otherwise, I'm worried that LMDB support will bitrot quickly because it will be too much of a nuisance to exercise the code.
Quoted 39 lines
Signed-off-by David Turner <[email protected]>--- .gitignore | 1 + Documentation/config.txt | 7 + Documentation/git-clone.txt | 3 +- Documentation/git-init.txt | 2 +- Documentation/technical/refs-lmdb-backend.txt | 52 + Documentation/technical/repository-version.txt | 5 + Makefile | 12 + configure.ac | 33 + contrib/workdir/git-new-workdir | 3 + refs.c | 3 + refs.h | 2 + refs/lmdb-backend.c | 2052 ++++++++++++++++++++++++ setup.c | 11 +- test-refs-lmdb-backend.c | 64 + transport.c | 7 +- 15 files changed, 2250 insertions(+), 7 deletions(-) create mode 100644 Documentation/technical/refs-lmdb-backend.txt create mode 100644 refs/lmdb-backend.c create mode 100644 test-refs-lmdb-backend.c.gitignore +1
-0@@ -199,6 +199,7 @@ /test-path-utils /test-prio-queue /test-read-cache +/test-refs-lmdb-backend /test-regex /test-revision-walking /test-run-commandDocumentation/config.txt +0
-0
Documentation/config.txt difftool.<tool>.cmd:: · line 1162
Quoted 10 lines
Documentation/config.txt +7
-0@@ -1153,6 +1153,13 @@ difftool.<tool>.cmd:: difftool.prompt:: Prompt before each invocation of the diff tool. +extensions.refsStorage:: + Type of refs storage backend. Default is to use the original + files based ref storage. When set to "lmdb", refs are stored in + the lmdb database backend. This setting reflects the refs + backend that is currently in use; it is not possible to change + the backend by changing this setting. +
Let's give the users a pointer to how they *can* change this setting. Maybe
Type of refs storage backend. Default is to use the original files based ref storage. When set to "lmdb", refs are stored in an LMDB database. This setting reflects the refs storage backend that was chosen via the --ref-storage option when the repository was originally created. It is currently not possible to change the refs storage backend of an existing repository.
Quoted 8 lines
Documentation/config.txt +0
-0fetch.recurseSubmodules:: This option can be either set to a boolean value or to 'on-demand'. Setting it to a boolean changes the behavior of fetch and pull toDocumentation/git-clone.txt +0
-0
Documentation/git-clone.txt objects from the source repository into a pack in the cloned repository. · line 231
Quoted 6 lines
Documentation/git-clone.txt +2
-1@@ -227,7 +227,8 @@ objects from the source repository into a pack in the cloned repository. --ref-storage=<name>:: Type of ref storage backend. Default is to use the original files - based ref storage. + based ref storage. Set to "lmdb" to store refs in the lmdb database + backend.
Should the second "lmdb" be upper-case? Maybe also in other places where the LMDB database is being referred to?
Documentation/git-clone.txt objects from the source repository into a pack in the cloned repository. · line 234
Quoted 4 lines
Documentation/git-clone.txt +0
-0<repository>:: The (possibly remote) repository to clone from. See the
I still haven't reviewed the actual lmdb-backend code. I guess I'm going to have to make myself an LMDB expert one of these days...
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
Quoted 9 lines · Michael Haggerty
Quoted 8 lines · David Turner
Add a database backend for refs using LMDB. This backend runs git for-each-ref about 30% faster than the files backend with fully -packed
refs on a repo with ~120k refs. It's also about 4x faster than using
fully-unpacked refs. In addition, and perhaps more importantly, it avoids case-conflict issues on OS X.
Quoted 8 lines
Quoted 2 lines
LMDB has a few features that make it suitable for usage in git:
0. It is licensed under the OpenLDAP Public License, which is apparently
GPL compatible [1].[1] http://www.gnu.org/licenses/license-list.en.html#newOpenLDAP
Oh, yeah, I checked that before choosing it and then forgot all about it.
Quoted 39 lines
Quoted 17 lines
1. It is relatively lightweight; it requires only one header file, and
the library code takes under 64k at runtime.2. It is well-tested: it's been used in OpenLDAP for years.
3. It's very fast. LMDB's benchmarks show that it is among the fastest key-value stores.
4. It has a relatively simple concurrency story; readers don't block writers and writers don't block readers.
Ronnie Sahlberg's original version of this patchset used tdb. The major disadvantage of tdb is that tdb is hard to build on OS X.
It's
also not in homebrew. So lmdb seemed simpler.Quoted 11 lines
To test this backend's correctness, I hacked
test-lib.shandtest-lib-functions.shto run all tests under the refs backend. Dozens
of tests use manual ref/reflog reading/writing, or create submodules
without passing --ref-storage to git init. If those tests are changed to use the update-ref machinery or test-refs-lmdb-backend (or,
in the case of packed-refs, corrupt refs, and dumb fetch tests, are skipped), the only remaining failing tests are the git-new-workdir tests and the gitweb tests.Would it possible to build your "hack" into the test suite? For example,
perhaps one could implement an option that requests tests to use LMDB wherever possible and skip the tests that are not LMDB-compatible. Over
time, we could try to increase the fraction of tests that are LMDB-compatible by (for example) usinggit update-refandgit rev-parserather than reading/writing reference files via the filesystem directly.
Quoted 3 lines
Otherwise, I'm worried that LMDB support will bitrot quickly because it
will be too much of a nuisance to exercise the code.
I can add that. It requires minor changes to a large number of tests, but that's OK.
Quoted 57 lines
Quoted 51 lines
Signed-off-by David Turner <[email protected]>--- .gitignore | 1 + Documentation/config.txt | 7 + Documentation/git-clone.txt | 3 +- Documentation/git-init.txt | 2 +- Documentation/technical/refs-lmdb-backend.txt | 52 + Documentation/technical/repository-version.txt | 5 + Makefile | 12 + configure.ac | 33 + contrib/workdir/git-new-workdir | 3 + refs.c | 3 + refs.h | 2 + refs/lmdb-backend.c | 2052 ++++++++++++++++++++++++ setup.c | 11 +- test-refs-lmdb-backend.c | 64 + transport.c | 7 +- 15 files changed, 2250 insertions(+), 7 deletions(-) create mode 100644 Documentation/technical/refs-lmdb-backend.txt create mode 100644 refs/lmdb-backend.c create mode 100644 test-refs-lmdb-backend.c.gitignore +1
-0@@ -199,6 +199,7 @@ /test-path-utils /test-prio-queue /test-read-cache +/test-refs-lmdb-backend /test-regex /test-revision-walking /test-run-commandDocumentation/config.txt +2
-0@@ -1153,6 +1153,13 @@ difftool.<tool>.cmd:: difftool.prompt:: Prompt before each invocation of the diff tool. +extensions.refsStorage:: + Type of refs storage backend. Default is to use theoriginal
Documentation/config.txt +1
-0+ files based ref storage. When set to "lmdb", refs arestored in
Documentation/config.txt +2
-0+ the lmdb database backend. This setting reflects the refs + backend that is currently in use; it is not possible tochange
Documentation/config.txt difftool.<tool>.cmd:: · line 1162
Quoted 2 lines
Documentation/config.txt +2
-0+ the backend by changing this setting. +Let's give the users a pointer to how they *can* change this setting. Maybe
Quoted 8 lines
Type of refs storage backend. Default is to use the original files based ref storage. When set to "lmdb", refs are stored
in
an LMDB database. This setting reflects the refs storage backend that was chosen via the --ref-storage option when the repository was originally created. It is currently not possible to change the refs storage backend of an existing repository.
OK.
Quoted 13 lines
Quoted 13 lines
fetch.recurseSubmodules::
Documentation/config.txt +0
-1This option can be either set to a boolean value or to 'on -demand'. Setting it to a boolean changes the behavior of fetch andpull to
Documentation/git renamed from Documentation/git-clone.txt +0
-1-clone.txtindex 68f56a7..b9c52ce 100644
Documentation/git-clone.txt +0
-0@@ -227,7 +227,8 @@ objects from the source repository into a packin the cloned repository.
Quoted 11 lines
Documentation/git-clone.txt objects from the source repository into a pack in the cloned repository. · line 231
Quoted 7 lines
--ref-storage=<name>::
Type of ref storage backend. Default is to use the
original filesDocumentation/git-clone.txt +1
-1- based ref storage. + based ref storage. Set to "lmdb" to store refs in the lmdbdatabase
Documentation/git-clone.txt +1
-0+ backend.Should the second "lmdb" be upper-case? Maybe also in other places where
the LMDB database is being referred to?
OK, I think I got most of them.
Quoted 7 lines
Quoted 3 lines
<repository>::
The (possibly remote) repository to clone from. See the
[...]I still haven't reviewed the actual lmdb-backend code. I guess I'm going
to have to make myself an LMDB expert one of these days...
I highly recommend it! The API is pretty reasonable, and it does seem to be fast. The error messages could use some work, but that's my only complaint.
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
In reply to David TurnerQuoted 7 lines · David Turner
refs/lmdb-backend.c new file +7
-0+static char *get_refdb_path (const char *base) +{ +static struct strbuf path_buf =STRBUF_INIT ; +strbuf_reset (&path_buf); +strbuf_addf (&path_buf,"%s/refdb" , base); +return path_buf.buf; +}
...
refs/lmdb-backend.c line 80
Quoted 9 lines
refs/lmdb-backend.c new file +9
-0+static int lmdb_init_db (struct strbuf *err,int shared) +{ +/* +* To create a db, all we need to do is make a directory for +* it to live in; lmdb will do the rest. +*/ + +if (!db_path) + db_path =xstrdup (real_path (get_refdb_path (get_git_common_dir ())));
This works for multiple worktrees. But scripts may have harder time getting the path. The recommended way is "git rev-parse --git-path refdb" but because "refdb" is not registered in path.c:common_list[], that command becomes git_path("refdb") instead of get_refdb(get_git_... like here. And I will need to know that .git/refdb is _not_ per-worktree when I migrate/convert main worktree (it's very likely I have to go that route to solve .git/config issue in multi worktree).
The solution is register refdb to common_list[] and you can do git_path("refdb") here. But then what happens when another backend is added? Will the new backend use the same path "refdb", or say "refdb.sqlite"? If all backends share the name "refdb", why can't we just reuse "refs" instead because the default filesystem-based backend is technically just another backend?
Signature · 1 line
Duy
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
Quoted 28 lines · Duy Nguyen
Quoted 7 lines · David Turner
refs/lmdb-backend.c new file +7
-0+static char *get_refdb_path (const char *base) +{ +static struct strbuf path_buf =STRBUF_INIT ; +strbuf_reset (&path_buf); +strbuf_addf (&path_buf,"%s/refdb" , base); +return path_buf.buf; +}...
refs/lmdb-backend.c line 80
Quoted 9 lines
refs/lmdb-backend.c new file +9
-0+static int lmdb_init_db (struct strbuf *err,int shared) +{ +/* +* To create a db, all we need to do is make a directory for +* it to live in; lmdb will do the rest. +*/ + +if (!db_path) + db_path =xstrdup (real_path (get_refdb_path (get_git_common_dir ())));This works for multiple worktrees. But scripts may have harder time getting the path. The recommended way is "git rev-parse --git-path refdb" but because "refdb" is not registered in path.c:common_list[], that command becomes
git_path("refdb")instead of get_refdb(get_git_... like here. And I will need to know that .git/refdb is _not_ per-worktree when I migrate/convert main worktree (it's very likely I have to go that route to solve .git/config issue in multi worktree).
Quoted 6 lines
The solution is register refdb to common_list[] and you can do
git_path("refdb")here. But then what happens when another backend is added? Will the new backend use the same path "refdb", or say "refdb.sqlite"? If all backends share the name "refdb", why can't we just reuse "refs" instead because the default filesystem-based backend is technically just another backend?
To answer myself: I forgot that there were per-worktree refs (e.g. refs/bisect). It makes me wonder if we should put per-worktree refs to lmdb as well (maybe one per worktree if we don't want to put all in one db). One of the advantages of moving away from fs-based backend is the ability to deal with case sensitivity, "nested" refs (e.g. a/b and a/b/c are both refs). With this split, I think some refs are still left behind.. Sorry if this was discussed before, I haven't followed this closely.
Signature · 1 line
Duy
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
Quoted 46 lines · Duy Nguyen
Quoted 34 lines · Duy Nguyen
Quoted 7 lines · David Turner
refs/lmdb-backend.c new file +7
-0+static char *get_refdb_path (const char *base) +{ +static struct strbuf path_buf =STRBUF_INIT ; +strbuf_reset (&path_buf); +strbuf_addf (&path_buf,"%s/refdb" , base); +return path_buf.buf; +}...
Quoted 11 lines
refs/lmdb-backend.c new file +4
-0+static int lmdb_init_db (struct strbuf *err,int shared) +{ +/* +* To create a db, all we need to do is make a directory for
refs/lmdb-backend.c new file +5
-0+* it to live in; lmdb will do the rest. +*/ + +if (!db_path) + db_path =xstrdup(real_path(get_refdb_path(get_git_common_dir())));
This works for multiple worktrees. But scripts may have harder time getting the path. The recommended way is "git rev-parse --git-path refdb" but because "refdb" is not registered in path.c:common_list[],
that command becomesgit_path("refdb")instead of get_refdb(get_git_... like here. And I will need to know that .git/refdb is _not_ per-worktree when I migrate/convert main worktree
(it's very likely I have to go that route to solve .git/config issue
in multi worktree).Quoted 9 lines
The solution is register refdb to common_list[] and you can do
git_path("refdb")here. But then what happens when another backend is
added? Will the new backend use the same path "refdb", or say "refdb.sqlite"? If all backends share the name "refdb", why can't we
just reuse "refs" instead because the default filesystem-based backend
is technically just another backend?
Quoted 9 lines
To answer myself: I forgot that there were per-worktree refs (e.g. refs/bisect). It makes me wonder if we should put per-worktree refs to
lmdb as well (maybe one per worktree if we don't want to put all in one db). One of the advantages of moving away from fs-based backend is
the ability to deal with case sensitivity, "nested" refs (e.g. a/b and
a/b/c are both refs).
I don't think case-sensitivity is a huge problem here: per-worktree refs are now HEAD or refs/bisect/*, and refs/bisect is controlled entirely by the bisection machinery (that is, it won't have user -defined refs with case conflicts).
Quoted 3 lines
With this split, I think some refs are still
left behind.. Sorry if this was discussed before, I haven't followed this closely.
Previous discussion:
http://comments.gmane.org/gmane.comp.version-control.git/275097
Re: [PATCH v4 13/21] refs: resolve symbolic refs first
In reply to Michael HaggertyOn Fri, 201-02-12 at 15:09 +0100, Michael Haggerty wrote:]
Quoted 18 lines · David Turner
Quoted 7 lines
Before committing ref updates, split symbolic ref updates into two
parts: an update to the underlying ref, and a log-only update to the
symbolic ref. This ensures that both references are locked correctly
while their reflogs are updated.Quoted 7 lines
It is still possible to confuse git by concurrent updates, since the
splitting of symbolic refs does not happen under lock. So a symbolic ref
could be replaced by a plain ref in the middle of this operation, which
would lead to reflog discontinuities and missed old-ref checks.This patch is doing too much at once for my little brain to follow.
Quoted 8 lines
My first hangup is the change to setting
RESOLVE_REF_NO_RECURSEunconditionally inlock_ref_sha1_basic(). I count five callers of that
function and see no justification for why the change is OK in the context of each caller. Here are some thoughts:* The call from
files_create_symref()setsREF_NODEREF, so it is unaffected by this change.
Yes.
Quoted 4 lines
* The call from
files_transaction_commit()is preceded by a call todereference_symrefs(), which I assume effectively replaces the need forRESOLVE_REF_NO_RECURSE.
Yes.
Quoted 8 lines
* There are two calls from
files_rename_ref(). Why is it OK to do withoutRESOLVE_REF_NO_RECURSEthere?* For the oldrefname call, I suppose the justification is the
"(flag &REF_ISSYMREF)" check earlier in the function. (But does this introduce a
significant TOCTOU race?)
The refs code as a whole seems likely to have TOCTOU issues. In general, anywhere we check/set flag & REF_ISSYMREF without holding a lock, we have a potential problem. I haven't generally tried to handle these cases, since they're not presently handled.
The central problem with this area of the code is that commit interacts so intimately with the locking machinery. I understand some of why it's done that way. In particular, your change to ref locking to not hold lots of open files was a big win for us at Twitter. But this means that it's hard to deal with cross-backend ref updates: you want to hold multiple locks, and backends don't have the machinery for it.
We could add backend hooks to specifically lock and unlock refs. Then the backend commit code would just be handled a bundle of locked refs and would commit them. This might be hairy, but it could fix the TOCTOU problems. So, first lock the outer refs, then split out updates for any which are symbolic refs, and lock those. Finally, commit all updates (split by backend).
One downside of this is that right now, the backend API is relatively close to the front-end, and this would leak what should be an implementation detail. But maybe this is necessary to knit multiple backends together.
But I'm not sure that this is necessary right now, because I'm not sure that I'm actually making TOCTOU issues much worse.
Quoted 11 lines
* For the newrefname call, I suppose it's because the code a little
higher up tries to delete any existing reference with that name. It looks to me like the old code was slightly broken: if newrefname was an
unborn symbolic reference, then:read_ref_full()would fail;delete_ref()would be skipped;lock_ref_sha1_basic()would lock the *referred-to* reference; the referred-to reference would be overwritten
instead of newrefname. So it could be that hereREF_NODEREFindirectly
fixes a bug?
Yes, that's correct. These two appears to be separable, so I'll make it an independent patch (and add a test for that case).
Quoted 4 lines
* The last call, from
files_reflog_expire(), is also questionable before your patch. If refname is a symref, then the function is expiring the reflog of the symref. But (before this patch) it locks not the symref but its referent.
I can also separate this one.
Quoted 4 lines
This was discussed in some length before on the mailing list [1], and the conclusion was that the current behavior is wrong, but for backwards compatibility reasons it would be safest to change it to locking *both* the symref and its referent.
Yes, that would be the right thing to do. But for the reasons I discuss above, that requires a serious change in the way that backends work.
Quoted 11 lines
If possible, it would be better to split this patch up into several:
the
first few would each add theREF_NODEREFflag at one callsite, with a careful justification of why that is OK. Once all the callsites (except
the one infiles_transaction_commit()) have been changed, then the last
patch could add thedereference_symrefs()machinery and change the last
callsite.
Quoted 7 lines
(I'm not certain that those steps are actually doable independently, given that
REF_NODEREFhas other effects besides settingRESOLVE_REF_NO_RECURSE.)I'm not just being pedantic here. The patch as written is really too big
to review effectively.
That's a legit complaint. The problem, as you note, is that doing some of these steps completely independently doesn't work. But I'll try splitting out what I can.
Re: [PATCH v4 14/21] refs: always handle non-normal refs in files backend
In reply to Michael HaggertyQuoted 99 lines · Michael Haggerty
Quoted 98 lines · David Turner
Always handle non-normal (per-worktree or pseudo) refs in the files backend instead of alternate backends.
Sometimes a ref transaction will update both a per-worktree ref and a
normal ref. For instance, an ordinary commit might update refs/heads/master and HEAD (or at least HEAD's reflog).Updates to normal refs continue to go through the chosen backend.
Updates to non-normal refs are moved to a separate files backend transaction.
Signed-off-by David Turner <[email protected]>--- refs.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-)refs.c +12
-1@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +static const char split_transaction_fail_warning[] = N_( + "A ref transaction was split across two refs backends. Part of the " + "transaction succeeded, but then the update to the per -worktree refs " + "failed. Your repository may be in an inconsistent state."); +/* * We always have a files backend and it is the default. */ @@ -791,6 +796,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction ); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} + static struct ref_update *add_update(struct ref_transaction*transaction,
refs.c +0
-0const char *refname) {@@ -798,8 +810,7 @@ static struct ref_update *add_update(structref_transaction *transaction, struct ref_update *update = xcalloc(1, sizeof(*update) + len);memcpy ((char *)update->refname, refname, len);/* includes NUL */
refs.c +0
-1- ALLOC_GROW(transaction->updates, transaction->nr + 1,
transaction->alloc);refs.c +21
-3- transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }@@ -1217,11 +1228,38 @@ static int dereference_symrefs(structref_transaction *transaction, return 0; } +/* +* Move all non-normal ref updates into a specially-created +* files-backend transaction +*/ +static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct strbuf *err) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +int last; + struct ref_update *update = transaction ->updates[i]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL ) +continue ; + + last = --transaction->nr; + transaction->updates[i] = transaction ->updates[last]; +add_update_obj (files_transaction, update);
Quoted 8 lines
refs.c static int dereference_symrefs(struct ref_transaction *transaction, · line 1255
Quoted 5 lines
refs.c +5
-0+ } + +return 0 ; +} +I think this function is incorrect. The update that was previously at transaction->updates[i] never gets checked for abnormality.
Yes it does; that's the "update" variable that we just checked.
Quoted 10 lines
You could
fix that and also avoid gratuitously changing the order of the updates
like this:static int move_abnormal_ref_updates(struct
ref_transaction*transaction,
structref_transaction*files_transaction,
struct strbuf *err)
Quoted 15 lines
{
int i, normal_nr = 0;
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];
if (ref_type(update->refname) == REF_TYPE_NORMAL)
transaction->updates[normal_nr++] = update;
else
add_update_obj(files_transaction, update);
}
transaction->nr = normal_nr;
return 0;
}Sure, I can make that change.
Quoted 19 lines
Another alternative would be to set
update->flags |= REF_ABNORMALfor the abnormal references and *leave them* in the original transaction
while also adding them tofiles_transactions. Then teach non-files backends to skip over updates withREF_ABNORMAL.The reason I thought of this is that long-term I'd like to make it possible for some reference updates to fail without aborting the transaction. To implement that, we would want a way for
ref_transaction_commit()to report back to its caller *which* updates failed, and an obvious way to do that would be to store the reference-specific errors in structref_update. If you leave the abnormalref_updatesin the main transaction, then my hoped-for change
would be easier.
Quoted 3 lines
But that's a separate and hypothetical idea, so you don't have to let it
influence how you implement this patch.
I'm also interested in this idea. Perhaps it would also be nice to report *why* they fail (e.g. the conflicting ref name). I did a variant of this with for the journal code, but my way of doing it turned out to be a bad idea (long story). But I want to stay focused on the simplest thing possible, for now.
Quoted 81 lines
Quoted 81 lines
refs.c +0
-0int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {int ret =-1 ; struct string_list affected_refnames =
STRING_LIST_INIT_NODUP;refs.c +1
-0+ struct string_list files_affected_refnames =
STRING_LIST_INIT_NODUP;refs.c +21
-0+struct ref_transaction *files_transaction =NULL ;assert (err);@@ -1237,6 +1275,26 @@ int ref_transaction_commit(struct ref_transaction *transaction, if (ret) goto done; +if (the_refs_backend != &refs_be_files) { + files_transaction =ref_transaction_begin (err); +if (!files_transaction) + goto done; + + ret =move_abnormal_ref_updates (transaction, files_transaction, + err); +if (ret) + goto done; + +/* files backend commit */ +if (get_affected_refnames (files_transaction, + &files_affected_r efnames, + err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + } + +/* main backend commit */ if (get_affected_refnames(transaction,
&affected_refnames,
err)) {refs.c +18
-0ret =TRANSACTION_GENERIC_ERROR ; goto done;@@ -1244,8 +1302,24 @@ int ref_transaction_commit(structref_transaction *transaction, ret = the_refs_backend->transaction_commit (transaction, &affected_refnames, err); +if (ret) + goto done; + +if (files_transaction) { + ret = refs_be_files.transaction_commit (files_transaction, + &files_affected_refnames, + err); +if (ret) { +warning (split_transaction_fail_warning); + goto done; + } + } + done:string_list_clear (&affected_refnames,0 ); +string_list_clear (&files_affected_refnames,0 ); +if (files_transaction) +ref_transaction_free (files_transaction);return ret; }@@ -1285,6 +1359,9 @@ int peel_ref(const char *refname, unsigned char *sha1)int create_symref (const char *ref_target,const char *refs_heads_master,const char *logmsg) { +if (ref_type (ref_target) !=REF_TYPE_NORMAL ) + return refs_be_files.create_symref(ref_target, refs_heads_master,
Quoted 8 lines
Quoted 6 lines
refs.c +1
-0+ logmsg);return the_refs_backend->create_symref(ref_target,
refs_heads_master,
logmsg);
}Does
initial_ref_transaction_commit()need the same treatment?
We only use that for remote refs -- I'm not sure if those can be symrefs. Wouldn't hurt.
Quoted 5 lines
I think
files_rename_ref()will break if one of the references is normal
and one is abnormal. I think it would be OK to prohibit renaming abnormal refs entirely (can anybody think of an important use case?), but that function should at least do the checks.
The files backend would work fine, but I guess I'll add the check anyway, since that would be a weird thing to do.
Re: [PATCH v4 15/21] init: allow alternate ref strorage to be set for new repos
In reply to Michael HaggertyQuoted 46 lines · Michael Haggerty
Quoted 45 lines · David Turner
git init learns a new argument --ref-storage. Presently, only "files" is supported, but later we will add other storage backends.
When this argument is used, the repository's extensions.refStorage configuration value is set (as well as core.repositoryformatversion),
and the ref storage backend's initdb function is used to set up the ref database.Signed-off-by David Turner <[email protected]>Signed-off-by SZEDER Gábor <[email protected]>--- Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 7 +++++- builtin/init-db.c | 44 +++++++++++++++++++++++++++------- cache.h | 2 ++ contrib/completion/git-completion.bash | 3 ++- path.c | 29 ++++++++++++++++++++-- refs.c | 8 +++++++ refs.h | 8 +++++++ setup.c | 12 ++++++++++ t/t0001-init.sh | 25 +++++++++++++++++++ 10 files changed, 127 insertions(+), 13 deletions(-)[...]
Documentation/git renamed from Documentation/git-init.txt +0
-1-init.txtindex 8174d27..d2b150f 100644
Documentation/git-init.txt +1
-2@@ -12,7 +12,7 @@ SYNOPSIS 'git init' [-q | --quiet] [--bare] [- -template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [directory] - + [--ref-storage=<name>] DESCRIPTION -----------@@ -113,6 +113,11 @@ does not exist, it will be created. --
Quoted 11 lines
Documentation/git-init.txt does not exist, it will be created. · line 120
Quoted 6 lines
Documentation/git-init.txt +2
-0+--ref-storage=<name>:: +Type of refs storage backend. Default is to use the original"files"
Documentation/git-init.txt +3
-0+storage, which stores ref data in files in .git/refs and +.git/packed-refs. +Technically, that should be $GIT_DIR/refs and $GIT_DIR/packed-refs. But
it might be that we are not so picky about the distinction in the user docs.
We're not (according to grep), but we probably should be.
Quoted 29 lines
Quoted 29 lines
TEMPLATE DIRECTORY
------------------builtin/init-db.c +1
-0@@ -24,6 +24,7 @@ static int init_is_bare_repository = 0;static int init_shared_repository =-1 ;static const char *init_db_template_dir;static const char *git_link; +static char *requested_ref_storage_backend; static void copy_templates_1(struct strbuf *path, struct strbuf *template,builtin/init-db.c +1
-0DIR *dir)@@ -179,6 +180,7 @@ static int create_default_files(const char *template_path)int reinit;int filemode;struct strbuf err =STRBUF_INIT ; +int repo_version =0 ;/* Just look for `init.templatedir` */ builtin/init-db.c +0
-0git_config (git_init_db_config,NULL );@@ -205,6 +207,32 @@ static int create_default_files(const char *template_path) }
Quoted 6 lines
Quoted 3 lines
builtin/init-db.c +1
-0/* +* Create the default symlink from ".git/HEAD" to the "master"
I know that you are just moving this comment, but s/symlink/symref/ would make it more up-to-date.
OK.
Quoted 75 lines
Quoted 75 lines
builtin/init-db.c +2
-0+* branch, if it does not exist yet. +*/ setup.c +10
-0@@ -1,5 +1,6 @@#include "cache.h" #include "dir.h" +#include "refs.h" #include "string-list.h" static int inside_git_dir =-1 ;@@ -263,6 +264,15 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) return ret; } +int ref_storage_backend_config (const char *var,const char *value,void *ptr) +{ +char **cdata = ptr; + +if (!strcmp (var,"extensions.refstorage" )) + *cdata =xstrdup (value); +return 0 ; +} +/* * Test if it looks like we're at a git directory.
* We want to see:setup.c +2
-0@@ -390,6 +400,8 @@ static int check_repo_format(const char *var, const char *value, void *cb) ; else if (!strcmp(ext, "preciousobjects")) repository_format_precious_objects =git_config_bool (var, value); + else if (!strcmp(ext, "refstorage")) + ref_storage_backend =xstrdup (value); elsestring_list_append(&unknown_extensions,
ext);setup.c +0
-0}t/t0001-init.sh +6
-0@@ -174,6 +174,31 @@ test_expect_success 'reinit' ' test_i18ncmp again/empty again/err2 ' +test_expect_success 'init with bogus storage backend fails' ' + + ( + mkdir again2 && + cd again2 && + test_must_fail git init --ref-storage=test >out22>err2 &&
t/t0001-init.sh +1
-0+ test_i18ngrep "Unknown ref storage backend test"err2
t/t0001-init.sh +14
-0+ ) +' + +test_expect_failure 'reinit with changed storage backend fails' ' + + ( + mkdir again3 && + cd again3 && + git init >out1 2>err1 && + git init --ref-storage=test >out2 2>err2 + ) && + test_i18ngrep "Initialized empty" again3/out1 && + test_i18ngrep "Reinitialized existing" again3/out2 && + >again3/empty &&
Quoted 8 lines
t/t0001-init.sh test_expect_success 'reinit' ' · line 201
Quoted 4 lines
t/t0001-init.sh +4
-0+ test_i18ncmp again3/empty again3/err1 && + test_i18ncmp again3/empty again3/err2 +' +Is it worth testing re-initializing with the same --ref-storage? Perhaps
not.
Would be nice, but since we cannot guarantee that any alternate backend exists, we have no way to test this. We'd have to add an entire "test" ref backend just for tests, which seems a bit overboard.
Quoted 7 lines
t/t0001-init.sh test_expect_success 'reinit' ' · line 204
Quoted 4 lines
test_expect_success'init with --template' 't/t0001-init.sh +0
-0mkdir template-source && echo content >template-source/file &&Michael
Re: [PATCH v4 19/21] refs: add register_ref_storage_backends()
In reply to Michael HaggertyQuoted 42 lines · Michael Haggerty
Quoted 41 lines · David Turner
This new function will register all known ref storage backends... once
there are any other than the default. For now, it's a no-op.Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 3 +++ config.c | 25 +++++++++++++++++++++++++ refs.c | 8 ++++++++ refs.h | 2 ++ 4 files changed, 38 insertions(+)[...]
config.c +10
-0@@ -11,6 +11,7 @@#include "strbuf.h" #include "quote.h" #include "hashmap.h" +#include "refs.h" #include "string-list.h" #include "utf8.h" @@ -1207,6 +1208,30 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) }if (repo_config && !access_or_die (repo_config,R_OK ,0 )) { +char *storage =NULL ; + +/* +* make sure we always read the ref storage config +* from the extensions section on startup +*/ + ret +=git_config_from_file (ref_storage_backend_config, + repo_config, &storage); +
Quoted 8 lines
config.c int git_config_early(config_fn_t fn, void *data, const char *repo_config) · line 1225
Quoted 6 lines
config.c +6
-0+register_ref_storage_backends (); +if (!storage) + storage =xstrdup ("" ); + +if ((!*storage) || + (!strcmp (storage,"files" ))) {Nit: you have some unnecessary parentheses here.
Fixed, thanks.
Quoted 29 lines
Quoted 29 lines
config.c +5
-0+/* default backend, nothing to do */ +free (storage); + }else { + ref_storage_backend = storage; + if(
set_ref_storage_backend(ref_storage_backend))config.c +1
-0+ die(_("Unknown ref storage backend%s"),
config.c +3
-0+ ref_storage_backend); + } + ret += git_config_from_file(fn, repo_config,data);
config.c +0
-0found +=1 ; }refs.c +4
-0@@ -1554,3 +1554,11 @@ done:string_list_clear (&affected_refnames,0 );return ret; } + +void register_ref_storage_backends (void ) { +/* +* No need to register the files backend; it's registered by
refs.c +1
-0+* default. Add register_ref_storage_backend(ptr-to
Quoted 8 lines
refs.c done: · line 1563
Quoted 3 lines
refs.c +2
-1-backend) +* entries below when you add a new backend. +*/ This function must be called every run, right? So why not make it register the "files" backend explicitly? That would make it obvious really quick if this function fails to get called in some code path. Just a thought...
Good idea.
Re: [PATCH v4 20/21] refs: add LMDB refs storage backend
In reply to Duy NguyenQuoted 22 lines · Duy Nguyen
Quoted 7 lines · David Turner
refs/lmdb-backend.c new file +7
-0+static char *get_refdb_path (const char *base) +{ +static struct strbuf path_buf =STRBUF_INIT ; +strbuf_reset (&path_buf); +strbuf_addf (&path_buf,"%s/refdb" , base); +return path_buf.buf; +}...
Quoted 11 lines
refs/lmdb-backend.c new file +4
-0+static int lmdb_init_db (struct strbuf *err,int shared) +{ +/* +* To create a db, all we need to do is make a directory for
refs/lmdb-backend.c new file +5
-0+* it to live in; lmdb will do the rest. +*/ + +if (!db_path) + db_path =xstrdup(real_path(get_refdb_path(get_git_common_dir())));
Quoted 8 lines
This works for multiple worktrees. But scripts may have harder time getting the path. The recommended way is "git rev-parse --git-path refdb" but because "refdb" is not registered in path.c:common_list[], that command becomes
git_path("refdb")instead of get_refdb(get_git_... like here. And I will need to know that .git/refdb is _not_ per-worktree when I migrate/convert main worktree (it's very likely I have to go that route to solve .git/config issue in multi worktree).
I'll fix common_list.
Quoted 6 lines
The solution is register refdb to common_list[] and you can do
git_path("refdb")here. But then what happens when another backend is added? Will the new backend use the same path "refdb", or say "refdb.sqlite"? If all backends share the name "refdb", why can't we just reuse "refs" instead because the default filesystem-based backend is technically just another backend?
I'll change it to "refs.lmdb".
I don't want to use "refs" because it makes it hard for software to distinguish the format (in some cases, like sqlite, it won't even be a directory).
Re: [PATCH v4 13/21] refs: resolve symbolic refs first
In reply to David TurnerQuoted 46 lines · David Turner
On Fri, 201-02-12 at 15:09 +0100, Michael Haggerty wrote:]
Quoted 18 lines · David Turner
Quoted 7 lines
Before committing ref updates, split symbolic ref updates into two
parts: an update to the underlying ref, and a log-only update to the
symbolic ref. This ensures that both references are locked correctly
while their reflogs are updated.Quoted 7 lines
It is still possible to confuse git by concurrent updates, since the
splitting of symbolic refs does not happen under lock. So a symbolic ref
could be replaced by a plain ref in the middle of this operation, which
would lead to reflog discontinuities and missed old-ref checks.This patch is doing too much at once for my little brain to follow.
Quoted 8 lines
My first hangup is the change to setting
RESOLVE_REF_NO_RECURSEunconditionally inlock_ref_sha1_basic(). I count five callers of that
function and see no justification for why the change is OK in the context of each caller. Here are some thoughts:* The call from
files_create_symref()setsREF_NODEREF, so it is unaffected by this change.Yes.
Quoted 4 lines
* The call from
files_transaction_commit()is preceded by a call todereference_symrefs(), which I assume effectively replaces the need forRESOLVE_REF_NO_RECURSE.Yes.
Quoted 8 lines
* There are two calls from
files_rename_ref(). Why is it OK to do withoutRESOLVE_REF_NO_RECURSEthere?* For the oldrefname call, I suppose the justification is the
"(flag &REF_ISSYMREF)" check earlier in the function. (But does this introduce a
significant TOCTOU race?)
Quoted 4 lines
The refs code as a whole seems likely to have TOCTOU issues. In general, anywhere we check/set flag &
REF_ISSYMREFwithout holding a lock, we have a potential problem. I haven't generally tried to handle these cases, since they're not presently handled.
I agree that we don't do so well here, though I think that most races would result in reading/writing a ref that was pointed to by the symref a moment ago, which is usually indistinguishable to the user from their update having gone through the moment before the symref was updated. So I don't think your change makes this bit of code significantly worse.
Quoted 7 lines
The central problem with this area of the code is that commit interacts so intimately with the locking machinery. I understand some of why it's done that way. In particular, your change to ref locking to not hold lots of open files was a big win for us at Twitter. But this means that it's hard to deal with cross-backend ref updates: you want to hold multiple locks, and backends don't have the machinery for it.
Quoted 6 lines
We could add backend hooks to specifically lock and unlock refs. Then the backend commit code would just be handled a bundle of locked refs and would commit them. This might be hairy, but it could fix the TOCTOU problems. So, first lock the outer refs, then split out updates for any which are symbolic refs, and lock those. Finally, commit all updates (split by backend).
As chance would have it, for an internal GitHub project I've implemented hooks that can be called *during* a ref transaction. The hooks can, for example, take arbitrary actions between the time that the reflocks are all acquired and the time that the updates start to be committed. I didn't submit this code upstream because I didn't think that it would benefit other users, but many it would be useful for implementing split-backend reference transaction commits. E.g., the primary reference transaction could run the secondary backend's commit while holding the locks for the primary backend references.
Let me think about it.
I don't think this is urgent though. The current code is not significantly racy in mainstream usage scenarios, right?
Quoted 7 lines
One downside of this is that right now, the backend API is relatively close to the front-end, and this would leak what should be an implementation detail. But maybe this is necessary to knit multiple backends together.
But I'm not sure that this is necessary right now, because I'm not sure that I'm actually making TOCTOU issues much worse.
Agreed.
Quoted 4 lines
[...]
That's a legit complaint. The problem, as you note, is that doing some of these steps completely independently doesn't work. But I'll try splitting out what I can.
Thanks!
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 14/21] refs: always handle non-normal refs in files backend
In reply to David TurnerQuoted 109 lines · David Turner
Quoted 99 lines · Michael Haggerty
Quoted 98 lines · David Turner
Always handle non-normal (per-worktree or pseudo) refs in the files backend instead of alternate backends.
Sometimes a ref transaction will update both a per-worktree ref and a
normal ref. For instance, an ordinary commit might update refs/heads/master and HEAD (or at least HEAD's reflog).Updates to normal refs continue to go through the chosen backend.
Updates to non-normal refs are moved to a separate files backend transaction.
Signed-off-by David Turner <[email protected]>--- refs.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-)refs.c +12
-1@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +static const char split_transaction_fail_warning[] = N_( + "A ref transaction was split across two refs backends. Part of the " + "transaction succeeded, but then the update to the per -worktree refs " + "failed. Your repository may be in an inconsistent state."); +/* * We always have a files backend and it is the default. */ @@ -791,6 +796,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction ); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} + static struct ref_update *add_update(struct ref_transaction*transaction,
refs.c +0
-0const char *refname) {@@ -798,8 +810,7 @@ static struct ref_update *add_update(structref_transaction *transaction, struct ref_update *update = xcalloc(1, sizeof(*update) + len);memcpy ((char *)update->refname, refname, len);/* includes NUL */
refs.c +0
-1- ALLOC_GROW(transaction->updates, transaction->nr + 1,
transaction->alloc);refs.c +21
-3- transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }@@ -1217,11 +1228,38 @@ static int dereference_symrefs(structref_transaction *transaction, return 0; } +/* +* Move all non-normal ref updates into a specially-created +* files-backend transaction +*/ +static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct strbuf *err) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +int last; + struct ref_update *update = transaction ->updates[i]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL ) +continue ; + + last = --transaction->nr; + transaction->updates[i] = transaction ->updates[last]; +add_update_obj (files_transaction, update);Quoted 8 lines
refs.c static int dereference_symrefs(struct ref_transaction *transaction, · line 1255
Quoted 5 lines
refs.c +5
-0+ } + +return 0 ; +} +I think this function is incorrect. The update that was previously at transaction->updates[i] never gets checked for abnormality.
Quoted 1 line
Yes it does; that's the "update" variable that we just checked.
Sorry, I meant to say "the update that was previously at transaction->updates[transaction->nr - 1] never gets checked for abnormality". Because it gets moved to transaction->updates[i], then i is incremented without checking it.
Quoted 10 lines
[...]
Quoted 4 lines
Another alternative would be to set
update->flags |= REF_ABNORMAL[...]
I'm also interested in this idea. Perhaps it would also be nice to report *why* they fail (e.g. the conflicting ref name). I did a variant of this with for the journal code, but my way of doing it turned out to be a bad idea (long story). But I want to stay focused on the simplest thing possible, for now.
Fair enough.
Quoted 5 lines
[...]
Quoted 1 line
Does
initial_ref_transaction_commit()need the same treatment?We only use that for remote refs -- I'm not sure if those can be symrefs. Wouldn't hurt.
Hmmm, good point. I think the only symrefs that can be set during a clone are HEAD and refs/remotes/origin/HEAD. But I guess that no other references are updated *through* these symrefs, so it's probably OK. I haven't checked carefully, though.
Quoted 1 line
[...]
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v4 14/21] refs: always handle non-normal refs in files backend
Quoted 120 lines · Michael Haggerty
Quoted 119 lines · David Turner
Quoted 117 lines · Michael Haggerty
Quoted 102 lines · David Turner
Always handle non-normal (per-worktree or pseudo) refs in the files
backend instead of alternate backends.Sometimes a ref transaction will update both a per-worktree ref and
a
normal ref. For instance, an ordinary commit might update refs/heads/master and HEAD (or at least HEAD's reflog).Updates to normal refs continue to go through the chosen backend.
Updates to non-normal refs are moved to a separate files backend
transaction.Signed-off-by David Turner <[email protected]>--- refs.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +-- 1 file changed, 79 insertions(+), 2 deletions(-)refs.c +12
-1@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +static const char split_transaction_fail_warning[] = N_( + "A ref transaction was split across two refs backends. Part of the " + "transaction succeeded, but then the update to the per -worktree refs " + "failed. Your repository may be in an inconsistent state."); +/* * We always have a files backend and it is the default. */ @@ -791,6 +796,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction ); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} +static struct
ref_update*add_update(structref_transaction
*transaction,refs.c +1
-0const char *refname) {@@ -798,8 +810,7 @@ static struct ref_update *add_update(structref_transaction *transaction, struct ref_update *update = xcalloc(1, sizeof(*update) + len);memcpy ((char *)update->refname, refname, len);/* includes
NUL */refs.c +0
-1- ALLOC_GROW(transaction->updates, transaction->nr + 1,
transaction->alloc);refs.c +17
-2- transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }@@ -1217,11 +1228,38 @@ static int dereference_symrefs(structref_transaction *transaction, return 0; } +/* +* Move all non-normal ref updates into a specially-created +* files-backend transaction +*/ +static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct strbuf *err) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +int last; + struct ref_update *update = transaction ->updates[i]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL ) +continue ;refs.c static int dereference_symrefs(struct · line 1259
Quoted 10 lines
refs.c +9
-1+ + last = --transaction->nr; + transaction->updates[i] = transaction ->updates[last]; +add_update_obj (files_transaction, update); + } + +return 0 ; +} +I think this function is incorrect. The update that was previously at
transaction->updates[i] never gets checked for abnormality.
Quoted 7 lines
Quoted 1 line
Yes it does; that's the "update" variable that we just checked.
Sorry, I meant to say "the update that was previously at
transaction->updates[transaction->nr - 1]never gets checked for abnormality". Because it gets moved totransaction->updates[i], theniis incremented without checking it.
Oh, right.
Re: [PATCH v4 15/21] init: allow alternate ref strorage to be set for new repos
In reply to David TurnerQuoted 9 lines · David Turner
Quoted 4 lines · Michael Haggerty
[...]
Is it worth testing re-initializing with the same --ref-storage? Perhaps
not.Would be nice, but since we cannot guarantee that any alternate backend exists, we have no way to test this. We'd have to add an entire "test" ref backend just for tests, which seems a bit overboard.
I meant re-initializing a repository that is using the "files" backend with --ref-storage=files to make sure that it is a NOP. But I think this functionality is unlikely to be broken so it wouldn't be a very high-value test.
Michael
Signature · 2 lines
Michael Haggerty [email protected]