LMDB refs backend atop pre-vtable
Subsystems (2)
cover LMDB refs backend atop pre-vtable
I'm starting the patchset numbering over from 1 here, because this version of the patchset is a subset of the last version.
This version of the patch set applies on top of dt/refs-backend-pre-vtable. This required moving a bunch of stuff that was in refs.h in previous versions into refs/refs-internal.h.
Since the last patchset, I added support for symlink HEAD refs, and broke out the initdb stuff into a separate commit.
I also rearranged the order of the backend functions to make the vtable easier to read.
I removed for_each_reftype_fullpath, which was at one point in next but is not anymore. And I did a bit more code cleanup/rearrangement on the LMDB stuff: I removed memory leaks, improved style, and just generally spruced things up.
As usual, the normal tests and the same set of hacked tests pass.
I've read over each of these patches a few times, but I've probably still managed to miss things. I look forward to your review.
David Turner (13):
refs: add do_for_each_per_worktree_ref
refs: add methods for reflog
refs: add method for initial ref transaction commit
refs: add method for delete_refs
refs: add methods to init refs backend and db
refs: add method to rename refs
refs: make lock generic
refs: move duplicate check to common code
refs: always handle non-normal refs in files backend
init: allow alternate backends to be set for new repos
refs: allow ref backend to be set for clone
refs: add LMDB refs backend
refs: tests for lmdb backend
Ronnie Sahlberg (3):
refs: add a backend method structure with transaction functions
refs: add methods for misc ref operations
refs: add methods for the ref iterators
.gitignore | 1 + Documentation/config.txt | 7 + Documentation/git-clone.txt | 4 + Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 7 +- Documentation/technical/refs-lmdb-backend.txt | 50 + Documentation/technical/repository-version.txt | 5 + Makefile | 12 + builtin/clone.c | 27 +- builtin/init-db.c | 35 +- builtin/submodule--helper.c | 5 +- cache.h | 10 + config.c | 34 + configure.ac | 33 + contrib/workdir/git-new-workdir | 3 + environment.c | 1 + path.c | 32 +- refs.c | 406 ++++- refs.h | 17 + refs/files-backend.c | 257 +-- refs/lmdb-backend.c | 2054 ++++++++++++++++++++++++ refs/refs-internal.h | 118 +- setup.c | 32 +- t/t1460-refs-lmdb-backend.sh | 1109 +++++++++++++ t/t1470-refs-lmdb-backend-reflog.sh | 359 +++++ t/test-lib.sh | 1 + test-refs-lmdb-backend.c | 68 + 27 files changed, 4553 insertions(+), 136 deletions(-) create mode 100644 Documentation/technical/refs-lmdb-backend.txt create mode 100644 refs/lmdb-backend.c create mode 100755 t/t1460-refs-lmdb-backend.sh create mode 100755 t/t1470-refs-lmdb-backend-reflog.sh create mode 100644 test-refs-lmdb-backend.c
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
PATCH 1/16 refs: add a backend method structure with transaction functions
From: Ronnie Sahlberg <[email protected]>
Add a ref structure for backend methods. Start by adding a method pointer for the transaction commit function.
Add a function set_refs_backend to switch between backends. The files based backend is the default.
--- refs.c | 32 ++++++++++++++++++++++++++++++++ refs.h | 2 ++ refs/files-backend.c | 10 ++++++++-- refs/refs-internal.h | 10 ++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-)
refs.c +32 -0
@@ -10,6 +10,31 @@#include "tag.h" /* +* We always have a files backend and it is the default. +*/ +extern struct ref_be refs_be_files; +struct ref_be *the_refs_backend = &refs_be_files; +/* +* List of all available backends +*/ +struct ref_be *refs_backends = &refs_be_files; + +/* +* This function is used to switch to an alternate backend. +*/ +int set_refs_backend (const char *name) +{ +struct ref_be *be; + +for (be = refs_backends; be; be = be->next) +if (!strcmp (be->name, name)) { + the_refs_backend = be; +return 0 ; + } +return 1 ; +} + +/* * How to handle various characters in refnames: * 0: An acceptable character for refs * 1: End-of-component
@@ -1082,3 +1107,10 @@ int rename_ref_available(const char *oldname, const char *newname)strbuf_release (&err);return ret; } + +/* backend functions */ +int ref_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) +{ +return the_refs_backend->transaction_commit (transaction, err); +}
refs.h +2 -0
@@ -508,4 +508,6 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data); +int set_refs_backend (const char *name); +#endif /* REFS_H */
refs/files-backend.c +8 -2
@@ -3123,8 +3123,8 @@ static int ref_update_reject_duplicates(struct string_list *refnames,return 0 ; } -int ref_transaction_commit (struct ref_transaction *transaction, -struct strbuf *err) +static int files_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) {int ret =0 , i;int n = transaction->nr;
@@ -3510,3 +3510,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1,unlock_ref (lock);return -1 ; } + +struct ref_be refs_be_files = { +NULL , +"files" , + files_transaction_commit, +};
refs/refs-internal.h +10 -0
@@ -197,4 +197,14 @@ const char *find_descendant_ref(const char *dirname,int rename_ref_available (const char *oldname,const char *newname); +/* refs backends */ +typedef int ref_transaction_commit_fn (struct ref_transaction *transaction, +struct strbuf *err); + +struct ref_be { +struct ref_be *next; +const char *name; +ref_transaction_commit_fn *transaction_commit; +}; +#endif /* REFS_REFS_INTERNAL_H */
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 01/16] refs: add a backend method structure with transaction functions
refs.c line 15
Quoted 11 lines · David Turner
refs.c +3
-0@@ -10,6 +10,31 @@#include "tag.h" /* +* We always have a files backend and it is the default. +*/ +extern struct ref_be refs_be_files;
It is customary to s/extern //; in C sources.
PATCH 2/16 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 + cache.h | 7 +++++++ refs.c | 36 ++++++++++++++++++++++++++++++++++++ refs/files-backend.c | 34 +++++++++++++++++++++++----------- refs/refs-internal.h | 23 +++++++++++++++++++++++ 5 files changed, 90 insertions(+), 11 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"
cache.h +7 -0
@@ -1111,6 +1111,13 @@ extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sextern int interpret_branch_name (const char *str,int len,struct strbuf *);extern int get_sha1_mb (const char *str,unsigned char *sha1); +/* +* Return true iff abbrev_name is a possible abbreviation for +* full_name according to the rules defined by ref_rev_parse_rules in +* refs.c. +*/ +extern int refname_match (const char *abbrev_name,const char *full_name); +extern int validate_headref (const char *ref);extern int base_name_compare (const char *name1,int len1,int mode1,const char *name2,int len2,int mode2);
refs.c +36 -0
@@ -1114,3 +1114,39 @@ int ref_transaction_commit(struct ref_transaction *transaction, {return the_refs_backend->transaction_commit (transaction, err); } + +const char *resolve_ref_unsafe (const char *ref,int resolve_flags, +unsigned char *sha1,int *flags) +{ +return the_refs_backend->resolve_ref_unsafe (ref, resolve_flags, sha1, + flags); +} + +int verify_refname_available (const char *refname,struct string_list *extra, +struct string_list *skip,struct strbuf *err) +{ +return the_refs_backend->verify_refname_available (refname, extra, skip, err); +} + +int pack_refs (unsigned int flags) +{ +return the_refs_backend->pack_refs (flags); +} + +int peel_ref (const char *refname,unsigned char *sha1) +{ +return the_refs_backend->peel_ref (refname, sha1); +} + +int create_symref (const char *ref_target,const char *refs_heads_master, +const char *logmsg) +{ +return the_refs_backend->create_symref (ref_target, refs_heads_master, + logmsg); +} + +int resolve_gitlink_ref (const char *path,const char *refname, +unsigned char *sha1) +{ +return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); +}
refs/files-backend.c +23 -11
@@ -1333,7 +1333,8 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,return resolve_gitlink_ref_recursive (refs, p, sha1, recursion+1 ); } -int resolve_gitlink_ref (const char *path,const char *refname,unsigned char *sha1) +static int files_resolve_gitlink_ref (const char *path,const char *refname, +unsigned char *sha1) {int len =strlen (path), retval;char *submodule;
@@ -1565,8 +1566,10 @@ static const char *resolve_ref_1(const char *refname, } } -const char *resolve_ref_unsafe (const char *refname,int resolve_flags, -unsigned char *sha1,int *flags) +static const char *files_resolve_ref_unsafe (const char *refname, +int resolve_flags, +unsigned char *sha1, +int *flags) {static struct strbuf sb_refname =STRBUF_INIT ;struct strbuf sb_contents =STRBUF_INIT ;
@@ -1615,7 +1618,7 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)return status; } -int peel_ref (const char *refname,unsigned char *sha1) +static int files_peel_ref (const char *refname,unsigned char *sha1) {int flag;unsigned char base[20 ];
@@ -2246,7 +2249,7 @@ static void prune_refs(struct ref_to_prune *r) } } -int pack_refs (unsigned int flags) +static int files_pack_refs (unsigned int flags) {struct pack_refs_cb_data cbdata;
@@ -2437,10 +2440,10 @@ out:return ret; } -int verify_refname_available (const char *newname, -struct string_list *extras, -struct string_list *skip, -struct strbuf *err) +static int files_verify_refname_available (const char *newname, +struct string_list *extras, +struct string_list *skip, +struct strbuf *err) {struct ref_dir *packed_refs =get_packed_refs (&ref_cache);struct ref_dir *loose_refs =get_loose_refs (&ref_cache);
@@ -2811,8 +2814,9 @@ static int commit_ref_update(struct ref_lock *lock,return 0 ; } -int create_symref (const char *ref_target,const char *refs_heads_master, -const char *logmsg) +static int files_create_symref (const char *ref_target, +const char *refs_heads_master, +const char *logmsg) {char *lockpath =NULL ;char ref[1000 ];
@@ -3515,4 +3519,12 @@ struct ref_be refs_be_files = {NULL ,"files" , files_transaction_commit, + + files_pack_refs, + files_peel_ref, + files_create_symref, + + files_resolve_ref_unsafe, + files_verify_refname_available, + files_resolve_gitlink_ref, };
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_be {struct ref_be *next;const char *name;ref_transaction_commit_fn *transaction_commit; + +pack_refs_fn *pack_refs; +peel_ref_fn *peel_ref; +create_symref_fn *create_symref; + +resolve_ref_unsafe_fn *resolve_ref_unsafe; +verify_refname_available_fn *verify_refname_available; +resolve_gitlink_ref_fn *resolve_gitlink_ref; };#endif /* REFS_REFS_INTERNAL_H */
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 02/16] refs: add methods for misc ref operations
refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 231
Quoted 13 lines · David Turner
struct
ref_be{refs/refs-internal.h +8
-0struct ref_be *next;const char *name;ref_transaction_commit_fn *transaction_commit; + +pack_refs_fn *pack_refs; +peel_ref_fn *peel_ref; +create_symref_fn *create_symref; + +resolve_ref_unsafe_fn *resolve_ref_unsafe; +verify_refname_available_fn *verify_refname_available; +resolve_gitlink_ref_fn *resolve_gitlink_ref; };
This may have been pointed out in the previous reviews by somebody else, but I think it is more customary to declare a struct member that is a pointer to a customization function without leading '*', i.e.
typedef TYPE (*customize_fn)(ARGS);
struct vtable {
...
cutomize_fn fn;
...
};in our codebase (cf. string_list::cmp, prio_queue::compare).
Re: [PATCH 02/16] refs: add methods for misc ref operations
Quoted 29 lines · Junio C Hamano
refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 231
Quoted 13 lines · David Turner
struct
ref_be{refs/refs-internal.h +8
-0struct ref_be *next;const char *name;ref_transaction_commit_fn *transaction_commit; + +pack_refs_fn *pack_refs; +peel_ref_fn *peel_ref; +create_symref_fn *create_symref; + +resolve_ref_unsafe_fn *resolve_ref_unsafe; +verify_refname_available_fn *verify_refname_available; +resolve_gitlink_ref_fn *resolve_gitlink_ref; };This may have been pointed out in the previous reviews by somebody else, but I think it is more customary to declare a struct member that is a pointer to a customization function without leading '*', i.e.
typedef TYPE (*customize_fn)(ARGS); struct vtable { ... cutomize_fn fn; ... };
Quoted 1 line
in our codebase (cf.
string_list::cmp,prio_queue::compare).
The previous review was here:
http://permalink.gmane.org/gmane.comp.version-control.git/279062
Quoted 9 lines · Michael
Hmmm, I thought our convention was to define typedefs for functions themselves, not for the pointer-to-function; e.g.,
typedef struct
ref_transaction*ref_transaction_begin_fn(struct
strbuf *err);(which would require
struct ref_beto be changed toref_transaction_begin_fn *transaction_begin;
And you agreed. So I changed it. Do you want me to change it back?
Re: [PATCH 02/16] refs: add methods for misc ref operations
Quoted 7 lines · David Turner
The previous review was here:
http://permalink.gmane.org/gmane.comp.version-control.git/279062Quoted 3 lines · Michael
Hmmm, I thought our convention was to define typedefs for functions themselves, not for the pointer-to-function; e.g.,
Quoted 8 lines
Quoted 6 lines
typedef struct
ref_transaction*ref_transaction_begin_fn(struct
strbuf *err);(which would require
struct ref_beto be changed toref_transaction_begin_fn *transaction_begin;And you agreed. So I changed it. Do you want me to change it back?
Sorry about that. I was agreeing to my mistaken understanding of what was pointed out X-<.
We do prefer fn(args) over (*fn)(args) and somehow I mixed that up with the declaration style.
Re: [PATCH 02/16] refs: add methods for misc ref operations
Quoted 16 lines · Junio C Hamano
Quoted 14 lines · David Turner
The previous review was here:
http://permalink.gmane.org/gmane.comp.version-control.git/279062Quoted 9 lines · Michael
Hmmm, I thought our convention was to define typedefs for functions themselves, not for the pointer-to-function; e.g.,
typedef struct
ref_transaction*ref_transaction_begin_fn(struct
strbuf *err);(which would require
struct ref_beto be changed toref_transaction_begin_fn *transaction_begin;
Quoted 7 lines
Quoted 1 line
And you agreed. So I changed it. Do you want me to change it back?
Sorry about that. I was agreeing to my mistaken understanding of what was pointed out X-<.
We do prefer fn(args) over (*fn)(args) and somehow I mixed that up with the declaration style.
So I should change it back?
Re: [PATCH 02/16] refs: add methods for misc ref operations
In reply to Junio C HamanoQuoted 30 lines · Junio C Hamano <gitster <at> pobox.com> writes:
refs/refs-internal.h int rename_ref_available(const char *oldname, const char *newname); · line 231
Quoted 13 lines · David Turner <dturner <at> twopensource.com> writes:
struct
ref_be{refs/refs-internal.h +8
-0struct ref_be *next;const char *name;ref_transaction_commit_fn *transaction_commit; + +pack_refs_fn *pack_refs; +peel_ref_fn *peel_ref; +create_symref_fn *create_symref; + +resolve_ref_unsafe_fn *resolve_ref_unsafe; +verify_refname_available_fn *verify_refname_available; +resolve_gitlink_ref_fn *resolve_gitlink_ref; };This may have been pointed out in the previous reviews by somebody else, but I think it is more customary to declare a struct member that is a pointer to a customization function without leading '*', i.e.
typedef TYPE (*customize_fn)(ARGS); struct vtable { ... cutomize_fn fn; ... };
Quoted 1 line
in our codebase (cf.
string_list::cmp,prio_queue::compare).
(LMDB author here, just passing by)
IMO you're making a mistake. You should always typedef the thing itself, not a pointer to the thing. If you only typedef the pointer, you can only use the typedef to declare pointers and never the thing itself. If you typedef the actual thing, you can use the typedef to declare both the thing and pointer to thing.
It's particularly useful to have function typedefs because later on you can use the actual typedef to declare instances of that function type in header files, and guarantee that your function definitions match what they're intended to match. Otherwise, assigning a bare (*func) to something that mismatches only generates a warning, instead of an error.
Signature · 4 lines
-- Howard Chu CTO, Symas Corp. http://www.symas.com Director, Highland Sun http://highlandsun.com/hyc/ Chief Architect, OpenLDAP http://www.openldap.org/project/
Re: [PATCH 02/16] refs: add methods for misc ref operations
In reply to David TurnerQuoted 6 lines · David Turner
cache.h +0
-0@@ -1111,6 +1111,13 @@ extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sextern int interpret_branch_name (const char *str,int len,struct strbuf *);
cache.h extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as s · line 1121
Quoted 10 lines
cache.h +7
-0extern int get_sha1_mb (const char *str,unsigned char *sha1); +/* +* Return true iff abbrev_name is a possible abbreviation for +* full_name according to the rules defined by ref_rev_parse_rules in +* refs.c. +*/ +extern int refname_match (const char *abbrev_name,const char *full_name); +extern int validate_headref (const char *ref);
This hunk is strange. It duplicates what is in refs.h, risking the two copies to drift away from each other over time.
As the only callsites are in remote.c that includes refs.h, this probably should be dropped.
Other changes that give redirection to various operations via vtable looked all sensible.
Quoted 15 lines
cache.h +0
-0struct ref_be {struct ref_be *next;cache.h +8
-0const char *name;ref_transaction_commit_fn *transaction_commit; + +pack_refs_fn *pack_refs; +peel_ref_fn *peel_ref; +create_symref_fn *create_symref; + +resolve_ref_unsafe_fn *resolve_ref_unsafe; +verify_refname_available_fn *verify_refname_available; + resolve_gitlink_ref_fn *resolve_gitlink_ref; };#endif /* REFS_REFS_INTERNAL_H */
Re: [PATCH 02/16] refs: add methods for misc ref operations
Quoted 22 lines · Junio C Hamano
Quoted 6 lines · David Turner
cache.h +0
-0@@ -1111,6 +1111,13 @@ extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as sextern int interpret_branch_name (const char *str,int len,struct strbuf *);cache.h extern char *oid_to_hex(const struct object_id *oid); /* same static buffer as s · line 1121
Quoted 10 lines
cache.h +7
-0extern int get_sha1_mb (const char *str,unsigned char *sha1); +/* +* Return true iff abbrev_name is a possible abbreviation for +* full_name according to the rules defined by ref_rev_parse_rules in +* refs.c. +*/ +extern int refname_match (const char *abbrev_name,const char *full_name); +extern int validate_headref (const char *ref);This hunk is strange. It duplicates what is in
refs.h, risking the two copies to drift away from each other over time.
Quoted 2 lines
As the only callsites are in
remote.cthat includesrefs.h, this probably should be dropped.
Will fix, thanks.
PATCH 3/16 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
@@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname, {return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); } + +int head_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref (fn, cb_data); +} + +int head_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref_submodule (submodule, fn, cb_data); +} + +int for_each_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref (fn, cb_data); +} + +int for_each_ref_submodule (const char *submodule,each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref_submodule (submodule, fn, cb_data); +} + +int for_each_ref_in (const char *prefix,each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref_in (prefix, fn, cb_data); +} + +int for_each_fullref_in (const char *prefix,each_ref_fn fn,void *cb_data, +unsigned int broken) +{ +return the_refs_backend->for_each_fullref_in (prefix, fn, cb_data, + broken); +} + +int for_each_ref_in_submodule (const char *submodule,const char *prefix, +each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_ref_in_submodule (submodule, prefix, + fn, cb_data); +} + +int for_each_rawref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_rawref (fn, cb_data); +} + +int for_each_namespaced_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_namespaced_ref (fn, cb_data); +} + +int for_each_replace_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_replace_ref (fn, cb_data); +}
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_be {struct ref_be *next;const char *name;
@@ -228,6 +246,17 @@ struct ref_be {resolve_ref_unsafe_fn *resolve_ref_unsafe;verify_refname_available_fn *verify_refname_available;resolve_gitlink_ref_fn *resolve_gitlink_ref; + +head_ref_fn *head_ref; +head_ref_submodule_fn *head_ref_submodule; +for_each_ref_fn *for_each_ref; +for_each_ref_submodule_fn *for_each_ref_submodule; +for_each_ref_in_fn *for_each_ref_in; +for_each_fullref_in_fn *for_each_fullref_in; +for_each_ref_in_submodule_fn *for_each_ref_in_submodule; +for_each_rawref_fn *for_each_rawref; +for_each_namespaced_ref_fn *for_each_namespaced_ref; + for_each_replace_ref_fn *for_each_replace_ref; };#endif /* REFS_REFS_INTERNAL_H */
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 03/16] refs: add methods for the ref iterators
Apologies for the late review, and this review should probably go on patch 01 or 02 but I don't have it in my mbox atm...
Quoted 15 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
refs.c int resolve_gitlink_ref(const char *path, const char *refname, · line 1157
Quoted 8 lines
refs.c +5
-0@@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname, {return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); } + +int head_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref (fn, cb_data); +}
My only comment is that it seems like having a single static global the_refs_backend seems like it should be avoided.
It seems like the intention was to keep the existing interface as-is, which explains why this is using globals, but it seems like the refs backend should be part of some "application context" struct on the stack or allocated during main(). It can then be passed around in the API so that we do not need to have a global.
That way the code will not be tied to a specific single the_refs_backend and could in theory have multiple backends working at the same time. If submodule were ever rewritten in C then this could potentially be a useful feature.
That said, the refs backend is not the only global static data in git that would need to be factored out, but it wouldn't hurt to make this part a little more tidy.
Thoughts?
Signature · 1 line
David
Re: [PATCH 03/16] refs: add methods for the ref iterators
Quoted 31 lines · David Aguilar
Apologies for the late review, and this review should probably go on patch 01 or 02 but I don't have it in my mbox atm...
Quoted 15 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
-0refs.c int resolve_gitlink_ref(const char *path, const char *refname, · line 1157
Quoted 8 lines
refs.c +5
-0@@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname, {return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); } + +int head_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref (fn, cb_data); +}My only comment is that it seems like having a single static global
the_refs_backendseems like it should be avoided.
Quoted 6 lines
It seems like the intention was to keep the existing interface as-is, which explains why this is using globals, but it seems like the refs backend should be part of some "application context" struct on the stack or allocated during
main(). It can then be passed around in the API so that we do not need to have a global.
Not commenting on whether this is the right direction or not. A global variable holding a methods table might not be most aesthetic design, but there are practicalities.
However, that kind of change would change the function signatures for all public refs functions and probably most private refs functions as well and will likely have massive conflicts with almost any other patch that touches the refs code.
If doing this API change is desired it is probably best to do that as a separate patch later that ONLY does this signature change and nothing else to make review easier and to possibly make merge conflict changes easier to manage.
Quoted 13 lines
That way the code will not be tied to a specific single
the_refs_backendand could in theory have multiple backends working at the same time. If submodule were ever rewritten in C then this could potentially be a useful feature.That said, the refs backend is not the only global static data in git that would need to be factored out, but it wouldn't hurt ice, to make this part a little more tidy.
Thoughts?
--
David
Re: [PATCH 03/16] refs: add methods for the ref iterators
Quoted 8 lines · Ronnie Sahlberg
Not commenting on whether this is the right direction or not. A global variable holding a methods table might not be most aesthetic design, but there are practicalities.
However, that kind of change would change the function signatures for all public refs functions and probably most private refs functions as well and will likely have massive conflicts with almost any other patch that touches the refs code.
I do not think "the_backend" that is the default is wrong. Most callers want to interact with the single default backend that shows refs from the single default repository (i.e. ours), and having a set of functions that interact with that single default instance of the ref data without having to explicitly pass it as a parameter makes sense.
It is just like how we handle the in-core index. Most callers want to interact with the_index, the single default one, and we have a set of "cache" functions written back in Linus's days to access it without having to pass &the_index as the parameter. Later in April 2007 when I invented the support for multiple in-core index, we kept the traditional "works on the default instance" API in place but made them thin wrappers to the "works on the given instance", i.e. a set of "index" functions that was introduced at da94faf6 (Merge branch 'jc/the-index', 2007-04-24). Even after that change, most callers that work on the in-core index intract with "the_index", the single default instance, via the "cache" functions.
An API enhancement to allow us to handle refs in multiple repositories separately would be a very welcome move (it would get rid of the hacky interface for_each_ref_in_submodule(), for one thing), but even after that happens, I'd expect we would have most callers asking to interact with "the_backend", the single default instance.
Re: [PATCH 03/16] refs: add methods for the ref iterators
Quoted 6 lines · Junio C Hamano
An API enhancement to allow us to handle refs in multiple repositories separately would be a very welcome move (it would get rid of the hacky interface
for_each_ref_in_submodule(), for one thing), but even after that happens, I'd expect we would have most callers asking to interact with "the_backend", the single default instance.
I am just a bystander here, but I think I agree. I do not know how we will handle migrations from one format to another, but if we can instantiate two separate ref backends at once, it seems like we should be able to just for_each_ref() in the "old" backend, and update_ref() in the new one.
I did not dig in the code, but I think that is similar to why we have the multiple-index code. It is not about juggling a bunch of independent instances, but about moving data between them (though not specifically for migration in the case of the index).
As much as it would be nice to clean this up before moving to multiple backends, though, I don't think we should make it a pre-requisite. This is a difficult topic as it is, and I'd rather see us make incremental improvement (backends, then hopefully more flexible mixing of backends) than get bogged down and get neither.
-Peff
Re: [PATCH 03/16] refs: add methods for the ref iterators
Quoted 5 lines · Jeff King
As much as it would be nice to clean this up before moving to multiple backends, though, I don't think we should make it a pre-requisite. This is a difficult topic as it is, and I'd rather see us make incremental improvement (backends, then hopefully more flexible mixing of backends) than get bogged down and get neither.
Oh, yes, if I sounded like this needs to be cleaned up before it can go in, that wasn't what I meant. After all, the single in-core index support was adequate for our use even before the multiple in-core index support was introduced, and we still use the API to access the single default instance in most of the codepaths. I'd expect that the ref API will evolve in the same way.
Re: [PATCH 03/16] refs: add methods for the ref iterators
In reply to David AguilarQuoted 17 lines · David Aguilar
refs.c int resolve_gitlink_ref(const char *path, const char *refname, · line 1157
Quoted 13 lines
refs.c +5
-0@@ -1150,3 +1150,57 @@ int resolve_gitlink_ref(const char *path, const char *refname, {return the_refs_backend->resolve_gitlink_ref (path, refname, sha1); } + +int head_ref (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->head_ref (fn, cb_data); +}My only comment is that it seems like having a single static global
the_refs_backendseems like it should be avoided.
Quoted 6 lines
It seems like the intention was to keep the existing interface as-is, which explains why this is using globals, but it seems like the refs backend should be part of some "application context" struct on the stack or allocated during
main(). It can then be passed around in the API so that we do not need to have a global.
I think the ship is sailing in a different direction. The API to deal with refs, possibly in multiple repositories, is that you use a single ref backend, and that backend is expected to handle refs in submodule repositories. I.e. refs.c::for_each_ref_in_submodule() calls into the backend implementation of the same method. So from that point of view, you cannot say "the top level repository uses LMDB backend but this and that submodule refs are looked up by consulting files backend".
If we want to go the opposite direction, so that we can keep more than one in-core representations of "repository" (what you called "application context") and optionally have different refs backend handling different repositories, we most likely would want to rethink the part of the refs API that special cases the submodule refs from within the current repository. Their implementation should be excised from the API set, and instead made to be a set of thin wrapper functions that explicitly refer to a repository instance that represents the submodule.
The refs and the object database go hand in hand (i.e. the former gives the anchoring points to keep objects in the latter alive), so an instance of "repository" would hold "the_refs_backend", "objects[]", and possibly "the_index" for that repository.
The caching of already read refs is a responsiblity of each ref backend in the current codebase. I am not sure if that is a good placement, or a single implementation of the caching layer should instead sit on top of any and ll ref backends.
Re: [PATCH 03/16] refs: add methods for the ref iterators
Quoted 15 lines · Junio C Hamano
David Aguilar <[email protected]> writes:
[...]Quoted 3 lines
My only comment is that it seems like having a single static global
the_refs_backendseems like it should be avoided.
[...]I think the ship is sailing in a different direction. The API to deal with refs, possibly in multiple repositories, is that you use a single ref backend, and that backend is expected to handle refs in submodule repositories. I.e.
refs.c::for_each_ref_in_submodule()calls into the backend implementation of the same method. So from that point of view, you cannot say "the top level repository uses LMDB backend but this and that submodule refs are looked up by consulting files backend".
Quoted 9 lines
If we want to go the opposite direction, so that we can keep more than one in-core representations of "repository" (what you called "application context") and optionally have different refs backend handling different repositories, we most likely would want to rethink the part of the refs API that special cases the submodule refs from within the current repository. Their implementation should be excised from the API set, and instead made to be a set of thin wrapper functions that explicitly refer to a repository instance that represents the submodule.
I definitely think that the "one and only one refs backend per process" model is not a good long-term approach. For example, submodules are relatively independent but are nevertheless sometimes accessed from a single process. I don't think insisting that they all use the same references backend is tenable over the long term.
But I haven't insisted on this flexibility yet because I don't think it will be much extra work in total to retrofit it later. What we mostly want to avoid is the need to rewrite all call sites more than once. The current patch series hasn't had to rewrite callers at all, so we still have a rewrite in reserve :-)
Actually, maybe we will never have to rewrite all callers. I rather think that we will retain one simplified API for dealing with "*this* repository's references" and a second for dealing with other repos' refs.
By the way, I think the next step towards the ability to work with multiple backends at the same time would be to store a pointer to the refs backend inside the ref_cache instance (possibly renaming the latter) and allowing them both to be looked up by submodule name as is already done by get_ref_cache(). I think this change could also be done without rewriting callers.
Quoted 5 lines
[...]
The caching of already read refs is a responsiblity of each ref backend in the current codebase. I am not sure if that is a good placement, or a single implementation of the caching layer should instead sit on top of any and ll ref backends.
I still dream about having compoundable reference backends, in which case, ultimately, a "CacheReferenceStore" instance would optionally wrap on top of an arbitrary other "ReferenceStore" instance (so to speak) to give it caching functionality.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 03/16] refs: add methods for the ref iterators
Quoted 3 lines · Michael Haggerty
Actually, maybe we will never have to rewrite all callers. I rather think that we will retain one simplified API for dealing with "*this* repository's references" and a second for dealing with other repos' refs.
Yeah, I think the similarity with the multiple in-core index support Peff brought up holds true here as well.
Quoted 10 lines
Quoted 5 lines
[...]
The caching of already read refs is a responsiblity of each ref backend in the current codebase. I am not sure if that is a good placement, or a single implementation of the caching layer should instead sit on top of any and ll ref backends.I still dream about having compoundable reference backends, in which case, ultimately, a "CacheReferenceStore" instance would optionally wrap on top of an arbitrary other "ReferenceStore" instance (so to speak) to give it caching functionality.
I am not sure if we need or want the fully stackable backends, but I can see that the implementation of the API could be structured like so:
(1) Users of API would interact solely with the in-core caching layer when creating, reading, modifying, enumerating and deleting refs and contents of their logs. The caching layer has a handle for each in-core representation of a "repository", and there is a single default one, i.e. our repository. Also there is a way to ask for the in-core represention of a "repository" for submodules.
(2) An instance of an in-core "repository" caching layer knows what "storage backend" the repository uses and this is used to dispatch the requests to suitable backends. The caching layer would be responsible for maintaining the validity of in-core cache it keeps while it relays the requests.
(3) The implementation of for_each_ref_in_submodule() family of functions may need to be restructured; having to have the backend method for them would force storage backends to be aware of "other" repositories. Other "only in this repository" methods David and Ronnie's topics abstracts out of the files backend would remain the same.
which may be clean and flexible enough for our purpose.
Just to reiterate to avoid misunderstanding, I do not think that kind of restructuring has to be a prerequisite for the current effort to allow multiple backends, though.
Thanks.
PATCH 4/16 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 ;
@@ -1738,6 +1739,14 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,return do_for_each_entry (refs, base, do_one_ref, &data); } +int do_for_each_per_worktree_ref (const char *submodule,const char *base, +each_ref_fn fn,int trim,int flags, +void *cb_data) +{ +return do_for_each_ref (get_ref_cache (submodule), base, fn, trim, + flags |DO_FOR_EACH_PER_WORKTREE_ONLY , cb_data); +} +static int do_head_ref (const char *submodule,each_ref_fn fn,void *cb_data) {struct object_id oid;
refs/refs-internal.h +10 -0
@@ -42,6 +42,16 @@* value to ref_update::flags */ +/* Include broken references in a do_for_each_ref*() iteration */ +#define DO_FOR_EACH_INCLUDE_BROKEN 0x01 + +/* Only include per-worktree refs in a do_for_each_ref*() iteration */ +#define DO_FOR_EACH_PER_WORKTREE_ONLY 0x02 + +int do_for_each_per_worktree_ref (const char *submodule,const char *base, +each_ref_fn fn,int trim,int flags, +void *cb_data); +/* * Return true iff refname is minimally safe. "Safe" here means that * deleting a loose reference by this name will not do any damage, for
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 04/16] refs: add do_for_each_per_worktree_ref
Quoted 4 lines · David Turner
Alternate refs backends might still use files to store per-worktree refs. So the files backend's ref-loading infrastructure should be available to those backends, just for use on per-worktree refs. Add
do_for_each_per_worktree_ref, which iterates over per-worktree refs.
Is this "might still use"? I am instead getting an impression that, we are declaring that the recommended way to store per-worktree refs is with filesystem backend.
Not complaining against either such a design decision (if that is what is made here) or the above description in the log message--just want to understand the intention better.
I also wonder if it is cleaner to have a single interface that lets anybody ask a pointer to the backend struct by name. With such an interface, a new backend that wants to delegate per-worktree refs to files backend could
static int DB_for_each_ref(...)
{
struct ref_be *files = locate_ref_backend("files");
files->for_each_ref(... | WORKTREE_ONLY, ...);
... do its own enumeration of non-per-worktree refs ...
}and such a delegation does not need to be limited to per-worktree iteration.
Or is that a road to expose too much implementation details of a backend to other backends?
Quoted 67 lines
Signed-off-by David Turner <[email protected]>--- 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 ;@@ -1738,6 +1739,14 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,return do_for_each_entry (refs, base, do_one_ref, &data); } +int do_for_each_per_worktree_ref (const char *submodule,const char *base, +each_ref_fn fn,int trim,int flags, +void *cb_data) +{ +return do_for_each_ref (get_ref_cache (submodule), base, fn, trim, + flags |DO_FOR_EACH_PER_WORKTREE_ONLY , cb_data); +} +static int do_head_ref (const char *submodule,each_ref_fn fn,void *cb_data) {struct object_id oid;refs/refs-internal.h +10
-0@@ -42,6 +42,16 @@* value to ref_update::flags */ +/* Include broken references in a do_for_each_ref*() iteration */ +#define DO_FOR_EACH_INCLUDE_BROKEN 0x01 + +/* Only include per-worktree refs in a do_for_each_ref*() iteration */ +#define DO_FOR_EACH_PER_WORKTREE_ONLY 0x02 + +int do_for_each_per_worktree_ref (const char *submodule,const char *base, +each_ref_fn fn,int trim,int flags, +void *cb_data); +/* * Return true iff refname is minimally safe. "Safe" here means that * deleting a loose reference by this name will not do any damage, for
Re: [PATCH 04/16] refs: add do_for_each_per_worktree_ref
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · David Turner
Alternate refs backends might still use files to store per-worktree refs. So the files backend's ref-loading infrastructure should be available to those backends, just for use on per-worktree refs. Add
do_for_each_per_worktree_ref, which iterates over per-worktree refs.
Quoted 7 lines
Is this "might still use"? I am instead getting an impression that, we are declaring that the recommended way to store per-worktree refs is with filesystem backend.
Not complaining against either such a design decision (if that is what is made here) or the above description in the log message--just want to understand the intention better.
I'm not super-happy with the notion of splitting data across backends; it's a bit messy. I've resigned myself to it because it seems like the simplest way forward in the presence of per-worktree refs. But I figured the message could leave it open in case we change our minds.
Quoted 12 lines
I also wonder if it is cleaner to have a single interface that lets anybody ask a pointer to the backend struct by name. With such an interface, a new backend that wants to delegate per-worktree refs to files backend could
static int DB_for_each_ref(...) { struct ref_be *files = locate_ref_backend("files"); files->for_each_ref(... | WORKTREE_ONLY, ...); ... do its own enumeration of non-per-worktree refs ... }
Quoted 5 lines
and such a delegation does not need to be limited to per-worktree iteration.
Or is that a road to expose too much implementation details of a backend to other backends?
Given that the files backend is special in terms of its rights and responsibilities, I think it's OK to just refer to it directly. I think allowing backends to generally pick and choose bits from other backends would be confusing.
PATCH 5/16 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
@@ -1204,3 +1204,49 @@ int for_each_replace_ref(each_ref_fn fn, void *cb_data) {return the_refs_backend->for_each_replace_ref (fn, cb_data); } + +int for_each_reflog_ent_reverse (const char *refname,each_reflog_ent_fn fn, +void *cb_data) +{ +return the_refs_backend->for_each_reflog_ent_reverse (refname, fn, + cb_data); +} + +int for_each_reflog_ent (const char *refname,each_reflog_ent_fn fn, +void *cb_data) +{ +return the_refs_backend->for_each_reflog_ent (refname, fn, cb_data); +} + +int for_each_reflog (each_ref_fn fn,void *cb_data) +{ +return the_refs_backend->for_each_reflog (fn, cb_data); +} + +int reflog_exists (const char *refname) +{ +return the_refs_backend->reflog_exists (refname); +} + +int safe_create_reflog (const char *refname,int force_create, +struct strbuf *err) +{ +return the_refs_backend->create_reflog (refname, force_create, err); +} + +int delete_reflog (const char *refname) +{ +return the_refs_backend->delete_reflog (refname); +} + +int reflog_expire (const char *refname,const unsigned char *sha1, +unsigned int flags, +reflog_expiry_prepare_fn prepare_fn, +reflog_expiry_should_prune_fn should_prune_fn, +reflog_expiry_cleanup_fn cleanup_fn, +void *policy_cb_data) +{ +return the_refs_backend->reflog_expire (refname, sha1, flags, + prepare_fn, should_prune_fn, + cleanup_fn, policy_cb_data); +}
refs/files-backend.c +24 -12
@@ -2643,7 +2643,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str } -int safe_create_reflog (const char *refname,int force_create,struct strbuf *err) +static int files_create_reflog (const char *refname,int force_create, +struct strbuf *err) {int ret;struct strbuf sb =STRBUF_INIT ;
@@ -2899,7 +2900,7 @@ static int files_create_symref(const char *ref_target,return 0 ; } -int reflog_exists (const char *refname) +static int files_reflog_exists (const char *refname) {struct stat st;
@@ -2907,7 +2908,7 @@ int reflog_exists(const char *refname)S_ISREG (st.st_mode); } -int delete_reflog (const char *refname) +static int files_delete_reflog (const char *refname) {return remove_path (git_path ("logs/%s" , refname)); }
@@ -2951,7 +2952,9 @@ static char *find_beginning_of_line(char *bob, char *scan)return scan; } -int for_each_reflog_ent_reverse (const char *refname,each_reflog_ent_fn fn,void *cb_data) +static int files_for_each_reflog_ent_reverse (const char *refname, +each_reflog_ent_fn fn, +void *cb_data) {struct strbuf sb =STRBUF_INIT ;FILE *logfp;
@@ -3053,7 +3056,8 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, 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 ;
@@ -3115,7 +3119,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;
@@ -3425,12 +3429,12 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,return 0 ; } -int reflog_expire (const char *refname,const unsigned char *sha1, -unsigned int flags, -reflog_expiry_prepare_fn prepare_fn, -reflog_expiry_should_prune_fn should_prune_fn, -reflog_expiry_cleanup_fn cleanup_fn, -void *policy_cb_data) +static int files_reflog_expire (const char *refname,const unsigned char *sha1, +unsigned int flags, +reflog_expiry_prepare_fn prepare_fn, +reflog_expiry_should_prune_fn should_prune_fn, +reflog_expiry_cleanup_fn cleanup_fn, +void *policy_cb_data) {static struct lock_file reflog_lock;struct expire_reflog_cb cb;
@@ -3535,6 +3539,14 @@ struct ref_be refs_be_files = {"files" , files_transaction_commit, + files_for_each_reflog_ent, + files_for_each_reflog_ent_reverse, + files_for_each_reflog, + files_reflog_exists, + files_create_reflog, + files_delete_reflog, + files_reflog_expire, + files_pack_refs, files_peel_ref, files_create_symref,
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_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.g0ed01d8-twtrsrc
PATCH 6/16 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
@@ -1250,3 +1250,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1, prepare_fn, should_prune_fn, cleanup_fn, policy_cb_data); } + +int initial_ref_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) +{ +return the_refs_backend->initial_transaction_commit (transaction, err); +}
refs/files-backend.c +3 -2
@@ -3313,8 +3313,8 @@ static int ref_present(const char *refname,return string_list_has_string (affected_refnames, refname); } -int initial_ref_transaction_commit (struct ref_transaction *transaction, -struct strbuf *err) +static int files_initial_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err) {int ret =0 , i;int n = transaction->nr;
@@ -3538,6 +3538,7 @@ struct ref_be refs_be_files = {NULL ,"files" , files_transaction_commit, + files_initial_transaction_commit, files_for_each_reflog_ent, files_for_each_reflog_ent_reverse,
refs/refs-internal.h +1 -0
@@ -267,6 +267,7 @@ struct ref_be {struct ref_be *next;const char *name;ref_transaction_commit_fn *transaction_commit; +ref_transaction_commit_fn *initial_transaction_commit;for_each_reflog_ent_fn *for_each_reflog_ent;for_each_reflog_ent_reverse_fn *for_each_reflog_ent_reverse;
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
PATCH 7/16 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
@@ -1115,6 +1115,11 @@ int ref_transaction_commit(struct ref_transaction *transaction,return the_refs_backend->transaction_commit (transaction, err); } +int delete_refs (struct string_list *refnames) +{ +return the_refs_backend->delete_refs (refnames); +} +const char *resolve_ref_unsafe (const char *ref,int resolve_flags,unsigned char *sha1,int *flags) {
refs/files-backend.c +2 -1
@@ -2356,7 +2356,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)return 0 ; } -int delete_refs (struct string_list *refnames) +static int files_delete_refs (struct string_list *refnames) {struct strbuf err =STRBUF_INIT ;int i, result =0 ;
@@ -3551,6 +3551,7 @@ struct ref_be refs_be_files = { files_pack_refs, files_peel_ref, files_create_symref, + files_delete_refs, files_resolve_ref_unsafe, files_verify_refname_available,
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_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.g0ed01d8-twtrsrc
PATCH 8/16 refs: add methods to init refs backend and 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. We also might need to initialize ref backends themselves, so we'll add a method for that as well.
--- builtin/init-db.c | 14 ++++---------- refs.c | 8 +++++++- refs.h | 4 +++- refs/files-backend.c | 23 +++++++++++++++++++++++ refs/refs-internal.h | 4 ++++ 5 files changed, 41 insertions(+), 12 deletions(-)
builtin/init-db.c +4 -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,11 +202,11 @@ static int create_default_files(const char *template_path)*/ if (shared_repository) {adjust_shared_perm (get_git_dir ()); -adjust_shared_perm (git_path_buf (&buf,"refs" )); -adjust_shared_perm (git_path_buf (&buf,"refs/heads" )); -adjust_shared_perm (git_path_buf (&buf,"refs/tags" )); } +if (refs_init_db (&err, shared_repository)) +die ("failed to set up refs db: %s" , err.buf); +/* * Create the default symlink from ".git/HEAD" to the "master" * branch, if it does not exist yet.
refs.c +7 -1
@@ -22,13 +22,14 @@ struct ref_be *refs_backends = &refs_be_files;/* * This function is used to switch to an alternate backend. */ -int set_refs_backend (const char *name) +int set_refs_backend (const char *name,void *data) {struct ref_be *be;for (be = refs_backends; be; be = be->next)if (!strcmp (be->name, name)) { the_refs_backend = be; + be->init_backend (data);return 0 ; }return 1 ;
@@ -1109,6 +1110,11 @@ int rename_ref_available(const char *oldname, const char *newname) }/* backend functions */ +int refs_init_db (struct strbuf *err,int shared) +{ +return the_refs_backend->init_db (err, shared); +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {
refs.h +3 -1
@@ -66,6 +66,8 @@ extern int ref_exists(const char *refname);extern int is_branch (const char *refname); +extern int refs_init_db (struct strbuf *err,int shared); +/* * If refname is a non-symbolic reference that refers to a tag object, * and the tag can be (recursively) dereferenced to a non-tag object,
@@ -508,6 +510,6 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data); -int set_refs_backend (const char *name); +int set_refs_backend (const char *name,void *data);#endif /* REFS_H */
refs/files-backend.c +23 -0
@@ -3313,6 +3313,11 @@ static int ref_present(const char *refname,return string_list_has_string (affected_refnames, refname); } +void files_init_backend (void *data) +{ +/* do nothing */ +} +static int files_initial_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {
@@ -3534,9 +3539,27 @@ static int files_reflog_expire(const char *refname, const unsigned char *sha1,return -1 ; } +static int files_init_db (struct strbuf *err,int shared) +{ +/* +* Create .git/refs/{heads,tags} +*/ +safe_create_dir (git_path ("refs" ),1 ); +safe_create_dir (git_path ("refs/heads" ),1 ); +safe_create_dir (git_path ("refs/tags" ),1 ); +if (shared) { +adjust_shared_perm (git_path ("refs" )); +adjust_shared_perm (git_path ("refs/heads" )); +adjust_shared_perm (git_path ("refs/tags" )); + } +return 0 ; +} +struct ref_be refs_be_files = {NULL ,"files" , + files_init_backend, + files_init_db, files_transaction_commit, files_initial_transaction_commit,
refs/refs-internal.h +4 -0
@@ -208,6 +208,8 @@ const char *find_descendant_ref(const char *dirname,int rename_ref_available (const char *oldname,const char *newname);/* refs backends */ +typedef void ref_backend_init_fn (void *data); +typedef int ref_backend_init_db_fn (struct strbuf *err,int shared);typedef int ref_transaction_commit_fn (struct ref_transaction *transaction,struct strbuf *err);
@@ -267,6 +269,8 @@ typedef int for_each_replace_ref_fn(each_ref_fn fn, void *cb_data);struct ref_be {struct ref_be *next;const char *name; +ref_backend_init_fn *init_backend; +ref_backend_init_db_fn *init_db;ref_transaction_commit_fn *transaction_commit;ref_transaction_commit_fn *initial_transaction_commit;
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 08/16] refs: add methods to init refs backend and db
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. We also might need to initialize ref backends themselves, so we'll add a method for that as well.
Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 14 ++++---------- refs.c | 8 +++++++- refs.h | 4 +++- refs/files-backend.c | 23 +++++++++++++++++++++++ refs/refs-internal.h | 4 ++++ 5 files changed, 41 insertions(+), 12 deletions(-)[...]
refs.c +0
-0
refs.c struct ref_be *refs_backends = &refs_be_files; · line 25
Quoted 5 lines
refs.c +1
-1@@ -22,13 +22,14 @@ struct ref_be *refs_backends = &refs_be_files;/* * This function is used to switch to an alternate backend. */ -int set_refs_backend (const char *name) +int set_refs_backend (const char *name,void *data)
The purpose of the data argument is rather mysterious at this point, especially since set_refs_backend() still doesn't have any callers. I hope that will become clearer later in the series. Nevertheless, it would be nice for its use to be described in the docstring (which should preferably be moved to the header file).
Quoted 60 lines
[...]
refs/files-backend.c +23
-0@@ -3313,6 +3313,11 @@ static int ref_present(const char *refname,return string_list_has_string (affected_refnames, refname); } +void files_init_backend (void *data) +{ +/* do nothing */ +} +static int files_initial_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {@@ -3534,9 +3539,27 @@ static int files_reflog_expire(const char *refname, const unsigned char *sha1,return -1 ; } +static int files_init_db (struct strbuf *err,int shared) +{ +/* +* Create .git/refs/{heads,tags} +*/ +safe_create_dir (git_path ("refs" ),1 ); +safe_create_dir (git_path ("refs/heads" ),1 ); +safe_create_dir (git_path ("refs/tags" ),1 ); +if (shared) { +adjust_shared_perm (git_path ("refs" )); +adjust_shared_perm (git_path ("refs/heads" )); +adjust_shared_perm (git_path ("refs/tags" )); + } +return 0 ; +} +struct ref_be refs_be_files = {NULL ,"files" , + files_init_backend, + files_init_db, files_transaction_commit, files_initial_transaction_commit,refs/refs-internal.h +2
-0@@ -208,6 +208,8 @@ const char *find_descendant_ref(const char *dirname,int rename_ref_available (const char *oldname,const char *newname);/* refs backends */ +typedef void ref_backend_init_fn (void *data); +typedef int ref_backend_init_db_fn (struct strbuf *err,int shared);typedef int ref_transaction_commit_fn (struct ref_transaction *transaction,struct strbuf *err);
refs/refs-internal.h typedef int for_each_replace_ref_fn(each_ref_fn fn, void *cb_data); · line 276
Quoted 8 lines
refs/refs-internal.h +2
-0@@ -267,6 +269,8 @@ typedef int for_each_replace_ref_fn(each_ref_fn fn, void *cb_data);struct ref_be {struct ref_be *next;const char *name; +ref_backend_init_fn *init_backend; +ref_backend_init_db_fn *init_db;ref_transaction_commit_fn *transaction_commit;ref_transaction_commit_fn *initial_transaction_commit;
Your naming seems inconsistent here. I would have expected a "files_init_backend()" function to satisfy the typedef "ref_backend_init_backend_fn" or "ref_init_backend_fn", not "ref_backend_init_fn". Or, conversely, for the function implementing "ref_backend_init_fn" to be called "files_init()".
More generally, it would be nice to have a consistent naming pattern between (1) the name of the typedef, (2) the name of the member in "struct ref_be", (3) the names of concrete, backend-specific implementations of the functions.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 08/16] refs: add methods to init refs backend and db
Quoted 26 lines · Michael Haggerty
Quoted 25 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. We also might need to initialize ref backends themselves, so we'll add a method for that as well.Signed-off-by David Turner <[email protected]>--- builtin/init-db.c | 14 ++++---------- refs.c | 8 +++++++- refs.h | 4 +++- refs/files-backend.c | 23 +++++++++++++++++++++++ refs/refs-internal.h | 4 ++++ 5 files changed, 41 insertions(+), 12 deletions(-)[...]
refs.c +0
-1@@ -22,13 +22,14 @@ struct ref_be *refs_backends = &refs_be_files;/* * This function is used to switch to an alternate backend. */ -int set_refs_backend (const char *name)
Quoted 8 lines
refs.c struct ref_be *refs_backends = &refs_be_files; · line 25
Quoted 1 line
refs.c +1
-0+int set_refs_backend (const char *name,void *data)The purpose of the data argument is rather mysterious at this point, especially since
set_refs_backend()still doesn't have any callers. I hope that will become clearer later in the series. Nevertheless, it would be nice for its use to be described in the docstring (which should
preferably be moved to the header file).
Will fix.
Quoted 16 lines
refs.c struct ref_be *refs_backends = &refs_be_files; · line 34
Quoted 9 lines
refs.c +2
-0struct ref_be {struct ref_be *next;const char *name; +ref_backend_init_fn *init_backend; +ref_backend_init_db_fn *init_db;ref_transaction_commit_fn *transaction_commit;ref_transaction_commit_fn *initial_transaction_commit;Your naming seems inconsistent here. I would have expected a "
files_init_backend()" function to satisfy the typedef "ref_backend_init_backend_fn" or "ref_init_backend_fn", not "ref_backend_init_fn". Or, conversely, for the function implementing "ref_backend_init_fn" to be called "files_init()".
Quoted 4 lines
More generally, it would be nice to have a consistent naming pattern between (1) the name of the typedef, (2) the name of the member in "struct
ref_be", (3) the names of concrete, backend-specific implementations of the functions.
I'll change this to "refs_init_backend_fn *init_backend" and "refs_init_db_fn *init_db" (since we already have an init_db function), and make any other similar changes I happen to notice.
On the naming in general, I am somewhat at the mercy of history here.
For example, git presently has functions named ref_transaction_commit
(object_verb) and create_reflog (verb_object). So nothing I do will be consistent with everything. I could, of course, do some initial commits to clean up the naming, but that would create code churn.
PATCH 9/16 refs: add method to rename refs
In reply to David Turner--- refs.c | 37 +++++++++++++++++++++---------------- refs/files-backend.c | 4 +++- refs/refs-internal.h | 9 +++++++++ 3 files changed, 33 insertions(+), 17 deletions(-)
refs.c +21 -16
@@ -1093,22 +1093,6 @@ const char *find_descendant_ref(const char *dirname,return NULL ; } -int rename_ref_available (const char *oldname,const char *newname) -{ -struct string_list skip =STRING_LIST_INIT_NODUP ; -struct strbuf err =STRBUF_INIT ; -int ret; - -string_list_insert (&skip, oldname); - ret = !verify_refname_available (newname,NULL , &skip, &err); -if (!ret) -error ("%s" , err.buf); - -string_list_clear (&skip,0 ); -strbuf_release (&err); -return ret; -} -/* backend functions */ int refs_init_db (struct strbuf *err,int shared) {
@@ -1126,6 +1110,11 @@ int delete_refs(struct string_list *refnames)return the_refs_backend->delete_refs (refnames); } +int rename_ref (const char *oldref,const char *newref,const char *logmsg) +{ +return the_refs_backend->rename_ref (oldref, newref, logmsg); +} +const char *resolve_ref_unsafe (const char *ref,int resolve_flags,unsigned char *sha1,int *flags) {
@@ -1139,6 +1128,22 @@ int verify_refname_available(const char *refname, struct string_list *extra,return the_refs_backend->verify_refname_available (refname, extra, skip, err); } +int rename_ref_available (const char *oldname,const char *newname) +{ +struct string_list skip =STRING_LIST_INIT_NODUP ; +struct strbuf err =STRBUF_INIT ; +int ret; + +string_list_insert (&skip, oldname); + ret = !verify_refname_available (newname,NULL , &skip, &err); +if (!ret) +error ("%s" , err.buf); + +string_list_clear (&skip,0 ); +strbuf_release (&err); +return ret; +} +int pack_refs (unsigned int flags) {return the_refs_backend->pack_refs (flags);
refs/files-backend.c +3 -1
@@ -2478,7 +2478,8 @@ static int commit_ref_update(struct ref_lock *lock, const unsigned char *sha1, const char *logmsg, int flags, struct strbuf *err); -int rename_ref (const char *oldrefname,const char *newrefname,const char *logmsg) +static int files_rename_ref (const char *oldrefname,const char *newrefname, +const char *logmsg) {unsigned char sha1[20 ], orig_sha1[20 ];int flag =0 , logmoved =0 ;
@@ -3575,6 +3576,7 @@ struct ref_be refs_be_files = { files_peel_ref, files_create_symref, files_delete_refs, + files_rename_ref, files_resolve_ref_unsafe, files_verify_refname_available,
refs/refs-internal.h +9 -0
@@ -53,6 +53,13 @@ int do_for_each_per_worktree_ref(const char *submodule, const char *base, void *cb_data);/* +* Check if the new name does not conflict with any existing refs +* (other than possibly the old ref). Return 0 if the ref can be +* renamed to the new name. +*/ +int rename_ref_available (const char *oldname,const char *newname); + +/* * Return true iff refname is minimally safe. "Safe" here means that * deleting a loose reference by this name will not do any damage, for * example by causing a file that is not a reference to be deleted.
@@ -247,6 +254,7 @@ typedef const char *resolve_ref_unsafe_fn(const char *ref,typedef int verify_refname_available_fn (const char *refname,struct string_list *extra,struct string_list *skip,struct strbuf *err);typedef int resolve_gitlink_ref_fn (const char *path,const char *refname,unsigned char *sha1); +typedef int rename_ref_fn (const char *oldref,const char *newref,const char *logmsg);/* iteration methods */ typedef int head_ref_fn (each_ref_fn fn,void *cb_data);
@@ -286,6 +294,7 @@ struct ref_be {peel_ref_fn *peel_ref;create_symref_fn *create_symref;delete_refs_fn *delete_refs; +rename_ref_fn *rename_ref;resolve_ref_unsafe_fn *resolve_ref_unsafe;verify_refname_available_fn *verify_refname_available;
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
PATCH 10/16 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
@@ -3184,11 +3184,12 @@ static int files_transaction_commit(struct ref_transaction *transaction,*/ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; +struct ref_lock *lock;if ((update->flags &REF_HAVE_NEW ) &&is_null_sha1 (update->new_sha1)) update->flags |=REF_DELETING ; - update->lock = lock_ref_sha1_basic( + lock = lock_ref_sha1_basic( update->refname, ((update->flags & REF_HAVE_OLD) ? update->old_sha1 : NULL),
@@ -3196,7 +3197,8 @@ static int files_transaction_commit(struct ref_transaction *transaction, update->flags, &update->type, err); -if (!update->lock) { + update->backend_data = lock; +if (!lock) {char *reason; ret = (errno ==ENOTDIR )
@@ -3214,12 +3216,12 @@ static int files_transaction_commit(struct ref_transaction *transaction, (update->flags & REF_NODEREF));if (!overwriting_symref && - !hashcmp (update->lock->old_oid.hash, update->new_sha1)) { + !hashcmp (lock->old_oid.hash, update->new_sha1)) {/* * The reference already has the desired * value, so we don't need to write it. */ - }else if (write_ref_to_lockfile (update->lock, + }else if (write_ref_to_lockfile (lock, update->new_sha1, err)) {char *write_err =strbuf_detach (err,NULL );
@@ -3228,7 +3230,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,* The lock was freed upon failure of * write_ref_to_lockfile(): */ - update->lock =NULL ; + update->backend_data =NULL ;strbuf_addf (err,"cannot update the ref '%s': %s" , update->refname, write_err);
@@ -3244,7 +3246,7 @@ static int files_transaction_commit(struct ref_transaction *transaction,* We didn't have to write anything to the lockfile. * Close it to free up the file descriptor: */ -if (close_ref (update->lock)) { +if (close_ref (lock)) {strbuf_addf (err,"Couldn't close %s.lock" , update->refname); goto cleanup;
@@ -3257,16 +3259,16 @@ static int files_transaction_commit(struct ref_transaction *transaction,struct ref_update *update = updates[i];if (update->flags &REF_NEEDS_COMMIT ) { -if (commit_ref_update (update->lock, +if (commit_ref_update (update->backend_data, update->new_sha1, update->msg, update->flags, err)) {/* freed by commit_ref_update(): */ - update->lock =NULL ; + update->backend_data =NULL ; ret =TRANSACTION_GENERIC_ERROR ; goto cleanup; }else {/* freed by commit_ref_update(): */ - update->lock =NULL ; + update->backend_data =NULL ; } } }
@@ -3274,16 +3276,17 @@ static int files_transaction_commit(struct ref_transaction *transaction,/* Perform deletes now that updates are safely completed */ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; +struct ref_lock *lock = update->backend_data;if (update->flags &REF_DELETING ) { -if (delete_ref_loose (update->lock, update->type, err)) { +if (delete_ref_loose (lock, update->type, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto cleanup; }if (!(update->flags &REF_ISPRUNING ))string_list_append (&refs_to_delete, - update->lock->ref_name); + lock->ref_name); } }
@@ -3299,8 +3302,8 @@ cleanup: transaction->state =REF_TRANSACTION_CLOSED ;for (i =0 ; i < n; i++) -if (updates[i]->lock) -unlock_ref (updates[i]->lock); +if (updates[i]->backend_data) +unlock_ref (updates[i]->backend_data);string_list_clear (&refs_to_delete,0 );string_list_clear (&affected_refnames,0 );return ret;
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.g0ed01d8-twtrsrc
PATCH 11/16 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 | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++-- refs/files-backend.c | 57 ++++------------------------------------- refs/refs-internal.h | 1 + 3 files changed, 75 insertions(+), 54 deletions(-)
refs.c +69 -2
@@ -1093,6 +1093,37 @@ const char *find_descendant_ref(const char *dirname,return NULL ; } +/* +* Return 1 if there are any duplicate refnames in the updates in +* `transaction`, and fill in err with an appropriate error message. +* Fill in `refnames` with the refnames from the transaction. +*/ + +static int ref_update_reject_duplicates (struct ref_transaction *transaction, +struct string_list *refnames, +struct strbuf *err) +{ +int i, n = transaction->nr; +struct ref_update **updates; + +assert (err); + + updates = transaction->updates; +/* Fail if a refname appears more than once in the transaction: */ +for (i =0 ; i < n; i++) +string_list_append (refnames, updates[i]->refname); +string_list_sort (refnames); + +for (i =1 ; i < n; i++) +if (!strcmp (refnames->items[i -1 ].string, refnames->items[i].string)) { +strbuf_addf (err, +"Multiple updates for ref '%s' not allowed." , + refnames->items[i].string); +return 1 ; + } +return 0 ; +} +/* backend functions */ int refs_init_db (struct strbuf *err,int shared) {
@@ -1102,7 +1133,29 @@ int refs_init_db(struct strbuf *err, int shared)int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) { -return the_refs_backend->transaction_commit (transaction, err); +int ret =-1 ; +struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; + +assert (err); + +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: commit called for transaction that is not open" ); + +if (!transaction->nr) { + transaction->state =REF_TRANSACTION_CLOSED ; +return 0 ; + } + +if (ref_update_reject_duplicates (transaction, &affected_refnames, err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + + ret = the_refs_backend->transaction_commit (transaction, + &affected_refnames, err); +done: +string_list_clear (&affected_refnames,0 ); +return ret; }int delete_refs (struct string_list *refnames)
@@ -1270,5 +1323,19 @@ int reflog_expire(const char *refname, const unsigned char *sha1,int initial_ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) { -return the_refs_backend->initial_transaction_commit (transaction, err); + +struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; +int ret; + +if (ref_update_reject_duplicates (transaction, + &affected_refnames, err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + ret = the_refs_backend->initial_transaction_commit (transaction, + &affected_refnames, + err); +done: +string_list_clear (&affected_refnames,0 ); +return ret; }
refs/files-backend.c +5 -52
117 lines not on this page · raw message
refs/refs-internal.h +1 -0
@@ -218,6 +218,7 @@ int rename_ref_available(const char *oldname, const char *newname);typedef void ref_backend_init_fn (void *data);typedef int ref_backend_init_db_fn (struct strbuf *err,int shared);typedef int ref_transaction_commit_fn (struct ref_transaction *transaction, +struct string_list *affected_refnames,struct strbuf *err);/* reflog functions */
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 11/16] refs: move duplicate check to common code
Quoted 20 lines · David Turner
The check for duplicate refnames in a transaction is needed for all backends, so move it to the common code.
ref_transaction_commit_fngains a new argument, the sortedstring_listof affected refnames.Signed-off-by David Turner <[email protected]>--- refs.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++-- refs/files-backend.c | 57 ++++------------------------------------- refs/refs-internal.h | 1 + 3 files changed, 75 insertions(+), 54 deletions(-)refs.c +0
-0@@ -1093,6 +1093,37 @@ const char *find_descendant_ref(const char *dirname,return NULL ; }
refs.c const char *find_descendant_ref(const char *dirname, · line 1104
Quoted 10 lines
refs.c +9
-0+/* +* Return 1 if there are any duplicate refnames in the updates in +* `transaction`, and fill in err with an appropriate error message. +* Fill in `refnames` with the refnames from the transaction. +*/ + +static int ref_update_reject_duplicates (struct ref_transaction *transaction, +struct string_list *refnames, +struct strbuf *err)
I like that you extract this code into a function. Though it feels awkward to have a function called "ref_update_reject_duplicates()" that has a side effect of filling the names into a string list. I think it would feel more natural to call the function get_affected_refnames(), and treat the duplicate check as an extra bonus.
You could go even farther and split it into two functions,
void get_affected_refnames(struct ref_transaction *transaction,
struct string_list *refnames);
int ref_update_reject_duplicates(struct string_list *refnames,
struct strbuf *err);Quoted 44 lines
refs.c +36
-1+{ +int i, n = transaction->nr; +struct ref_update **updates; + +assert (err); + + updates = transaction->updates; +/* Fail if a refname appears more than once in the transaction: */ +for (i =0 ; i < n; i++) +string_list_append (refnames, updates[i]->refname); +string_list_sort (refnames); + +for (i =1 ; i < n; i++) +if (!strcmp (refnames->items[i -1 ].string, refnames->items[i].string)) { +strbuf_addf (err, +"Multiple updates for ref '%s' not allowed." , + refnames->items[i].string); +return 1 ; + } +return 0 ; +} +/* backend functions */ int refs_init_db (struct strbuf *err,int shared) {@@ -1102,7 +1133,29 @@ int refs_init_db(struct strbuf *err, int shared)int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) { -return the_refs_backend->transaction_commit (transaction, err); +int ret =-1 ; +struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; + +assert (err); + +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: commit called for transaction that is not open" ); + +if (!transaction->nr) { + transaction->state =REF_TRANSACTION_CLOSED ; +return 0 ; + } + +if (ref_update_reject_duplicates (transaction, &affected_refnames, err)) {
refs.c int refs_init_db(struct strbuf *err, int shared) · line 1159
Quoted 10 lines
refs.c +9
-0+ ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + + ret = the_refs_backend->transaction_commit (transaction, + &affected_refnames, err); +done: +string_list_clear (&affected_refnames,0 ); +return ret; }
Here you are avoiding a small amount of code duplication by calling ref_update_reject_duplicates() here rather than in the backend-specific code. But you are doing so at the cost of having to compute affected_refnames here and pass it (redundantly) to the backend's transaction_commit function. This increases the coupling between these functions, which in my opinion is worse than the small amount of code duplication. But maybe it's just me.
The check of transaction->state, on the other hand, makes sense here.
Quoted 1 line
[...]
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 11/16] refs: move duplicate check to common code
Quoted 4 lines · Michael Haggerty
Quoted 4 lines
+static int ref_update_reject_duplicates(struct
ref_transaction*transaction,(quoted diff) +1
-0+ struct string_list*refnames,
Quoted 8 lines
refs.c const char *find_descendant_ref(const char *dirname, · line 1104
Quoted 1 line
refs.c +1
-0+ struct strbuf *err)I like that you extract this code into a function. Though it feels awkward to have a function called "
ref_update_reject_duplicates()" that
has a side effect of filling the names into a string list. I think it would feel more natural to call the functionget_affected_refnames(), and treat the duplicate check as an extra bonus.
Renamed, thanks.
Quoted 35 lines
Quoted 23 lines
int ref_transaction_commit(struct
ref_transaction*transaction,refs.c +0
-1struct strbuf *err) { - return the_refs_backend->transaction_commit(transaction,err);
(quoted diff) +2
-0+ int ret = -1; + struct string_list affected_refnames =
STRING_LIST_INIT_NODUP;refs.c +5
-0+ +assert (err); + +if (transaction->state !=REF_TRANSACTION_OPEN ) + die("BUG: commit called for transaction that isnot open");
refs.c +7
-0+ +if (!transaction->nr) { + transaction->state =REF_TRANSACTION_CLOSED ; +return 0 ; + } + + if (ref_update_reject_duplicates(transaction,
&affected_refnames, err)) {refs.c +1
-0+ ret =TRANSACTION_GENERIC_ERROR ;refs.c int refs_init_db(struct strbuf *err, int shared) · line 1159
Quoted 10 lines
refs.c +8
-0+ goto done; + } + + ret = the_refs_backend->transaction_commit (transaction, + &affected_refnames, err); +done: +string_list_clear (&affected_refnames,0 ); +return ret; }Here you are avoiding a small amount of code duplication by calling
Quoted 8 lines
ref_update_reject_duplicates()here rather than in the backend -specific
code. But you are doing so at the cost of having to computeaffected_refnameshere and pass it (redundantly) to the backend'stransaction_commitfunction. This increases the coupling between these
functions, which in my opinion is worse than the small amount of code duplication. But maybe it's just me.
Hm. I'm trying to follow the principle that the backends should do as little as possible (and that the common code should do as much as possible). This reduces the cognitive load when writing new backends -
- a new backend author need not know that the list of ref updates must be unique.
What do others think?
PATCH 12/16 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).
We handle three cases here:
1. updates to normal refs continue to go through the chosen backend
2. updates to non-normal refs with REF_NODEREF or to non-symbolic refs are moved to a separate files backend transaction.
3. updates to symbolic refs are dereferenced to their base ref. The update to the base ref then goes through the ordinary backend, while the files backend is directly called to update the symref's reflog.
--- refs.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 2 deletions(-)
refs.c +139 -2
@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +const char split_transaction_fail_warning[] = +"A ref transaction was split across two refs backends. Part of the " +"transaction succeeded, but then the update to the per-worktree refs " +"failed. Your repository may be in an inconsistent state." ; +/* * We always have a files backend and it is the default. */
@@ -784,6 +789,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} +static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) {
@@ -791,8 +803,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,struct ref_update *update =xcalloc (1 ,sizeof (*update) + len);memcpy ((char *)update->refname, refname, len);/* includes NUL */ -ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); - transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }
@@ -1130,11 +1141,87 @@ int refs_init_db(struct strbuf *err, int shared)return the_refs_backend->init_db (err, shared); } +/* +* Special case for non-normal refs. For symbolic-refs when +* REF_NODEREF is not turned on, we dereference them here and replace +* updates to the symbolic refs with updates to the underlying ref. +* Then we do our own reflogging for the symbolic ref. +* +* We move other non-normal ref updates with into a specially-created +* files-backend transaction +*/ +static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct string_list *symrefs) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +struct ref_update *update = transaction->updates[i]; +const char *resolved; +int flags =0 ; +unsigned char sha1[20 ]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL ) +continue ; + + resolved =resolve_ref_unsafe (update->refname,0 , sha1, &flags); + +if (update->flags &REF_NODEREF || !(flags &REF_ISSYMREF )) { +int last; + +add_update_obj (files_transaction, update); +/* +* Replace this transaction with the +* last transaction, removing it from +* the list of backend transactions +*/ + last = --transaction->nr; + transaction->updates[i] = transaction->updates[last]; +continue ; + } + +if (resolved) { +struct ref_update *new_update; +struct string_list_item *item; + +if (ref_type (resolved) !=REF_TYPE_NORMAL ) +die ("Non-normal symbolic ref `%s` points to non-normal ref `%s`" , update->refname, resolved); + + new_update =xmalloc (sizeof (*new_update) + +strlen (resolved) +1 ); +memcpy (new_update, update,sizeof (*update)); + +if (update->flags &REF_HAVE_OLD && +hashcmp (sha1, update->old_sha1)) { +/* consistency check failed */ +free (new_update); +return -1 ; + }else { +hashcpy (update->old_sha1, sha1); + } + +strcpy ((char *)new_update->refname, resolved); + transaction->updates[i] = new_update; + + item =string_list_append (symrefs, update->refname); + item->util = new_update; +free (update); + } + } + +return 0 ; +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {int ret =-1 ;struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list files_affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list symrefs =STRING_LIST_INIT_DUP ; +struct string_list_item *item; +struct ref_transaction *files_transaction =NULL ;assert (err);
@@ -1146,6 +1233,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,return 0 ; } +if (the_refs_backend != &refs_be_files) { + files_transaction =ref_transaction_begin (err); +if (!files_transaction) +die ("%s" , err->buf); + + ret =move_abnormal_ref_updates (transaction, files_transaction, + &symrefs); +if (ret) + goto done; + +/* files backend commit */ +if (ref_update_reject_duplicates (files_transaction, + &files_affected_refnames, + err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + } + +/* main backend commit */ if (ref_update_reject_duplicates (transaction, &affected_refnames, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto done;
@@ -1153,8 +1260,35 @@ int ref_transaction_commit(struct ref_transaction *transaction, ret = the_refs_backend->transaction_commit (transaction, &affected_refnames, err); +if (ret) + goto done; + +if (the_refs_backend != &refs_be_files) { + ret = refs_be_files.transaction_commit (files_transaction, + &files_affected_refnames, + err); +if (ret) { +warning (split_transaction_fail_warning); + goto done; + } + +/* reflogging for dereferenced symbolic refs */ +for_each_string_list_item (item, &symrefs) { +struct ref_update *update = item->util; +if (files_log_ref_write (item->string, update->old_sha1, + update->new_sha1, + update->msg, update->flags, err)) +warning ("failed to log ref update for symref %s" , + item->string); + } + } + done:string_list_clear (&affected_refnames,0 ); +string_list_clear (&files_affected_refnames,0 ); +if (files_transaction) +ref_transaction_free (files_transaction); +string_list_clear (&symrefs,0 );return ret; }
@@ -1210,6 +1344,9 @@ int peel_ref(const char *refname, unsigned char *sha1)int create_symref (const char *ref_target,const char *refs_heads_master,const char *logmsg) { +if (ref_type (ref_target) !=REF_TYPE_NORMAL ) +return refs_be_files.create_symref (ref_target, refs_heads_master, + logmsg);return the_refs_backend->create_symref (ref_target, refs_heads_master, logmsg); }
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 12/16] refs: always handle non-normal refs in files backend
Quoted 66 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).
We handle three cases here:
1. updates to normal refs continue to go through the chosen backend
2. updates to non-normal refs with
REF_NODEREFor to non-symbolic refs are moved to a separate files backend transaction.3. updates to symbolic refs are dereferenced to their base ref. The update to the base ref then goes through the ordinary backend, while the files backend is directly called to update the symref's reflog.
Signed-off-by David Turner <[email protected]>--- refs.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 2 deletions(-)refs.c +13
-2@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +const char split_transaction_fail_warning[] = +"A ref transaction was split across two refs backends. Part of the " +"transaction succeeded, but then the update to the per-worktree refs " +"failed. Your repository may be in an inconsistent state." ; +/* * We always have a files backend and it is the default. */ @@ -784,6 +789,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} +static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) {@@ -791,8 +803,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,struct ref_update *update =xcalloc (1 ,sizeof (*update) + len);memcpy ((char *)update->refname, refname, len);/* includes NUL */ -ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); - transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }@@ -1130,11 +1141,87 @@ int refs_init_db(struct strbuf *err, int shared)return the_refs_backend->init_db (err, shared); }
refs.c int refs_init_db(struct strbuf *err, int shared) · line 1152
Quoted 10 lines
refs.c +9
-0+/* +* Special case for non-normal refs. For symbolic-refs when +* REF_NODEREF is not turned on, we dereference them here and replace +* updates to the symbolic refs with updates to the underlying ref. +* Then we do our own reflogging for the symbolic ref. +* +* We move other non-normal ref updates with into a specially-created +* files-backend transaction +*/
Extra word? s/with//?
Quoted 18 lines
refs.c +18
-0+static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct string_list *symrefs) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +struct ref_update *update = transaction->updates[i]; +const char *resolved; +int flags =0 ; +unsigned char sha1[20 ]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL ) +continue ; + + resolved =resolve_ref_unsafe (update->refname,0 , sha1, &flags); + +if (update->flags &REF_NODEREF || !(flags &REF_ISSYMREF )) {
refs.c int refs_init_db(struct strbuf *err, int shared) · line 1180
Quoted 10 lines
refs.c +10
-0+int last; + +add_update_obj (files_transaction, update); +/* +* Replace this transaction with the +* last transaction, removing it from +* the list of backend transactions +*/ + last = --transaction->nr; + transaction->updates[i] = transaction->updates[last];
The "last" temporary variable could be trivially inlined.
refs.c int refs_init_db(struct strbuf *err, int shared) · line 1189
Quoted 9 lines
refs.c +9
-0+continue ; + } + +if (resolved) { +struct ref_update *new_update; +struct string_list_item *item; + +if (ref_type (resolved) !=REF_TYPE_NORMAL ) +die ("Non-normal symbolic ref `%s` points to non-normal ref `%s`" , update->refname, resolved);
We don't usually use backticks in error messages. Please use "'" instead.
Also, please store this error message into a "strbuf *err" and report it via the usual mechanism.
refs.c int refs_init_db(struct strbuf *err, int shared) · line 1193
Quoted 3 lines
refs.c +3
-0+ new_update =xmalloc (sizeof (*new_update) + +strlen (resolved) +1 ); +memcpy (new_update, update,sizeof (*update));
Wouldn't it be preferable to replace this messy replacement code (including the memcpy(), which can't be checked by the type system) with a call to ref_transaction_update() followed by moving the new update to this position in the list and possibly tweaking some of its fields?
refs.c int refs_init_db(struct strbuf *err, int shared) · line 1199
Quoted 5 lines
refs.c +5
-0+if (update->flags &REF_HAVE_OLD && +hashcmp (sha1, update->old_sha1)) { +/* consistency check failed */ +free (new_update); +return -1 ;
We need an error message to be reported in this case; i.e., via a "struct strbuf *err" argument.
But actually, I don't understand why this check is needed here at all. Isn't it redundant with a similar check that will be done later (and properly, under lock) as part of the main ref_transaction_commit()?
Quoted 29 lines
refs.c +20
-0+ }else { +hashcpy (update->old_sha1, sha1); + } + +strcpy ((char *)new_update->refname, resolved); + transaction->updates[i] = new_update; + + item =string_list_append (symrefs, update->refname); + item->util = new_update; +free (update); + } + } + +return 0 ; +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {int ret =-1 ;struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list files_affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list symrefs =STRING_LIST_INIT_DUP ; +struct string_list_item *item; +struct ref_transaction *files_transaction =NULL ;assert (err);
refs.c int ref_transaction_commit(struct ref_transaction *transaction, · line 1239
Quoted 7 lines
refs.c +4
-0@@ -1146,6 +1233,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,return 0 ; } +if (the_refs_backend != &refs_be_files) { + files_transaction =ref_transaction_begin (err); +if (!files_transaction) +die ("%s" , err->buf);
I think dying here is too abrupt. Some callers try to recover from a failed ref_transaction_commit(). Couldn't you "goto done" and let the caller deal with err?
refs.c int ref_transaction_commit(struct ref_transaction *transaction, · line 1252
Quoted 12 lines
refs.c +12
-0+ ret =move_abnormal_ref_updates (transaction, files_transaction, + &symrefs); +if (ret) + goto done; + +/* files backend commit */ +if (ref_update_reject_duplicates (files_transaction, + &files_affected_refnames, + err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + }
Is it correct to reject_duplicates among "abnormal" references and "normal" references separately? ***
Quoted 7 lines
refs.c +3
-0+ } + +/* main backend commit */ if (ref_update_reject_duplicates (transaction, &affected_refnames, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto done;
refs.c int ref_transaction_commit(struct ref_transaction *transaction, · line 1266
Quoted 7 lines
refs.c +4
-0@@ -1153,8 +1260,35 @@ int ref_transaction_commit(struct ref_transaction *transaction, ret = the_refs_backend->transaction_commit (transaction, &affected_refnames, err); +if (ret) + goto done; + +if (the_refs_backend != &refs_be_files) {
This conditional would perhaps be more to the point if expressed as "if (files_transaction)".
Quoted 7 lines
refs.c +7
-0+ ret = refs_be_files.transaction_commit (files_transaction, + &files_affected_refnames, + err); +if (ret) { +warning (split_transaction_fail_warning); + goto done; + }
refs.c int ref_transaction_commit(struct ref_transaction *transaction, · line 1283
Quoted 10 lines
refs.c +10
-0+ +/* reflogging for dereferenced symbolic refs */ +for_each_string_list_item (item, &symrefs) { +struct ref_update *update = item->util; +if (files_log_ref_write (item->string, update->old_sha1, + update->new_sha1, + update->msg, update->flags, err)) +warning ("failed to log ref update for symref %s" , + item->string); + }
I think this code is incorrect because it doesn't lock the symbolic reference before modifying its reflog (though I seem to recall that the old code was buggy in this respect, too).
I wonder whether it would be simpler overall to leave the ref_update for the symbolic ref in the files_transaction, but set a special internal internal flag like REF_LOG_ONLY which tells the usual transaction_commit code to add a reflog entry for update->old_sha1 to update->new_sha1, without actually changing the reference.
Quoted 13 lines
refs.c +6
-0+ } + done:string_list_clear (&affected_refnames,0 ); +string_list_clear (&files_affected_refnames,0 ); +if (files_transaction) +ref_transaction_free (files_transaction); +string_list_clear (&symrefs,0 );return ret; }
refs.c int peel_ref(const char *refname, unsigned char *sha1) · line 1352
Quoted 9 lines
refs.c +3
-0@@ -1210,6 +1344,9 @@ int peel_ref(const char *refname, unsigned char *sha1)int create_symref (const char *ref_target,const char *refs_heads_master,const char *logmsg) { +if (ref_type (ref_target) !=REF_TYPE_NORMAL ) +return refs_be_files.create_symref (ref_target, refs_heads_master, + logmsg);return the_refs_backend->create_symref (ref_target, refs_heads_master, logmsg); }
I very much like the idea of introducing special handling for symbolic reference updates within a transaction. In fact, I think I would go even farther:
Let's take the example of an update to HEAD, which currently points at refs/heads/master. I think it would *always* be a good idea (i.e., even when only the files backend is in use) to split that ref_update into two ref_updates:
1. One to update refs/heads/master and add a reflog entry for that reference.
2. One to add a reflog entry for HEAD (i.e. using the new REF_LOG_ONLY flag suggested above).
Why?
* It ensures that both references are locked correctly while their reflogs are updated. (I believe the current code gets this wrong.)
* It improves the reject_duplicates coverage, which (I think) currently wouldn't detect the conflict between a direct update of refs/heads/master with a simultaneous update of the same reference via HEAD.
* It could later be generalized to an update that goes through multiple layers of symref indirection (though this would be a very low priority).
This might benefit the split-backend situation that you are implementing here. You could first do the symref-splitting step I just described, and *then* separate the non-normal from the normal refs. I think the net result would be simpler.
This patch is a lot to digest. I'm not yet confident that I have thought through all of the ramifications of this patch. I guess a few iterations will be needed in any case.
By the way, all of the patches preceding this one that I haven't commented on look OK to me.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 12/16] refs: always handle non-normal refs in files backend
Quoted 6 lines · Michael Haggerty
+ * We move other non-normal ref updates with into a specially
Quoted 3 lines
-created
+ * files-backend transaction
+ */Extra word? s/with//?
Fixed, thanks.
Quoted 11 lines
refs.c int refs_init_db(struct strbuf *err, int shared)
Quoted 9 lines
refs.c +8
-1+add_update_obj (files_transaction, update); +/* +* Replace this transaction with the +* last transaction, removing it from +* the list of backend transactions +*/ + last = --transaction->nr; + transaction->updates[i] = transaction ->updates[last];The "last" temporary variable could be trivially inlined.
Yes, but then the line would be > 80 characters.
Quoted 5 lines
Quoted 5 lines
refs.c +5
-0+continue ; + } + +if (resolved) { +struct ref_update *new_update;
Quoted 8 lines
Quoted 5 lines
refs.c +4
-0+struct string_list_item *item; + +if (ref_type (resolved) !=REF_TYPE_NORMAL ) + die("Non-normal symbolic ref `%s`points to non-normal ref
%s",update->refname, resolved);We don't usually use backticks in error messages. Please use "'" instead.
Fixed.
Quoted 3 lines
Also, please store this error message into a "strbuf *err" and report it
via the usual mechanism.
Fixed.
Quoted 21 lines
Review
Quoted 4 lines
(quoted diff) +2
-0+ new_update = xmalloc(sizeof(*new_update) + + strlen(resolved) +1);
(quoted diff) +1
-0+ memcpy(new_update, update,sizeof(*update));z
Wouldn't it be preferable to replace this messy replacement code (including thememcpy(), which can't be checked by the type system) with
a call toref_transaction_update()followed by moving the new update to
this position in the list and possibly tweaking some of its fields?refs.c int refs_init_db(struct strbuf *err, int shared) · line 1199
Quoted 5 lines
refs.c +5
-0+if (update->flags &REF_HAVE_OLD && +hashcmp (sha1, update->old_sha1)) { +/* consistency check failed */ +free (new_update); +return -1 ;We need an error message to be reported in this case; i.e., via a "struct strbuf *err" argument.
Quoted 4 lines
But actually, I don't understand why this check is needed here at all.
Isn't it redundant with a similar check that will be done later (and properly, under lock) as part of the main ref_transaction_commit(
Looks like probably yes.
Quoted 37 lines
Quoted 37 lines
refs.c +5
-0+ }else { +hashcpy (update->old_sha1, sha1); + } + + strcpy((char *)new_update->refname,resolved);
refs.c +11
-1+ transaction->updates[i] = new_update; + + item =string_list_append (symrefs, update ->refname); + item->util = new_update; +free (update); + } + } + +return 0 ; +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {int ret =-1 ; struct string_list affected_refnames =
STRING_LIST_INIT_NODUP;(quoted diff) +1
-0+ struct string_list files_affected_refnames =
STRING_LIST_INIT_NODUP;refs.c +3
-0+struct string_list symrefs =STRING_LIST_INIT_DUP ; +struct string_list_item *item; +struct ref_transaction *files_transaction =NULL ;assert (err);@@ -1146,6 +1233,26 @@ int ref_transaction_commit(structref_transaction *transaction, return 0; }
Quoted 8 lines
refs.c int ref_transaction_commit(struct ref_transaction *transaction, · line 1239
Quoted 4 lines
refs.c +4
-0+if (the_refs_backend != &refs_be_files) { + files_transaction =ref_transaction_begin (err); +if (!files_transaction) +die ("%s" , err->buf);I think dying here is too abrupt. Some callers try to recover from a failed
ref_transaction_commit(). Couldn't you "goto done" and let the caller deal with err?
Sure.
Quoted 10 lines
Quoted 10 lines
refs.c +8
-0+ ret =move_abnormal_ref_updates (transaction, files_transaction, + &symrefs); +if (ret) + goto done; + +/* files backend commit */ + if (ref_update_reject_duplicates(files_transaction, + &files_affected_r
Quoted 8 lines
refs.c int ref_transaction_commit(struct ref_transaction *transaction, · line 1252
Quoted 5 lines
efnames,
refs.c +4
-0+ err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + }Is it correct to
reject_duplicatesamong "abnormal" references and "normal" references separately? ***
Yes. No reference is both normal and abnormal.
Quoted 12 lines
Quoted 12 lines
refs.c +3
-0+ } + +/* main backend commit */ if (ref_update_reject_duplicates(transaction,
&affected_refnames, err)) {refs.c +0
-0ret =TRANSACTION_GENERIC_ERROR ; goto done;@@ -1153,8 +1260,35 @@ int ref_transaction_commit(structref_transaction *transaction, ret = the_refs_backend->transaction_commit(transaction,
Quoted 9 lines
refs.c int ref_transaction_commit(struct ref_transaction *transaction, · line 1266
Quoted 5 lines
refs.c +4
-0&affected_refnames, err); +if (ret) + goto done; + +if (the_refs_backend != &refs_be_files) {This conditional would perhaps be more to the point if expressed as "if
(files_transaction)".
OK.
Quoted 28 lines
Quoted 20 lines
refs.c +14
-0+ ret = refs_be_files.transaction_commit (files_transaction, + &files_affected_refnames, + err); +if (ret) { +warning (split_transaction_fail_warning); + goto done; + } + +/* reflogging for dereferenced symbolic refs */ +for_each_string_list_item (item, &symrefs) { +struct ref_update *update = item->util; + if (files_log_ref_write(item->string, update->old_sha1, + update->new_sha1, + update->msg,
update->flags, err))(quoted diff) +1
-0+ warning("failed to log ref updatefor symref %s",
Review
Quoted 2 lines
(quoted diff) +2
-0+ item->string); + }I think this code is incorrect because it doesn't lock the symbolic reference before modifying its reflog (though I seem to recall that the
old code was buggy in this respect, too).
Quoted 7 lines
I wonder whether it would be simpler overall to leave the
ref_updatefor
the symbolic ref in thefiles_transaction, but set a special internal internal flag likeREF_LOG_ONLYwhich tells the usualtransaction_commit
code to add a reflog entry forupdate->old_sha1toupdate->new_sha1, without actually changing the reference.
Hm. This seems to work -- see below for more details.
Quoted 40 lines
Quoted 21 lines
refs.c +9
-0+ } + done:string_list_clear (&affected_refnames,0 ); +string_list_clear (&files_affected_refnames,0 ); +if (files_transaction) +ref_transaction_free (files_transaction); +string_list_clear (&symrefs,0 );return ret; }@@ -1210,6 +1344,9 @@ int peel_ref(const char *refname, unsigned char *sha1)int create_symref (const char *ref_target,const char *refs_heads_master,const char *logmsg) { +if (ref_type (ref_target) !=REF_TYPE_NORMAL ) +return refs_be_files.create_symref (ref_target, refs_heads_master, + logmsg);Quoted 5 lines
return the_refs_backend->create_symref(ref_target,
refs_heads_master,
logmsg);
}I very much like the idea of introducing special handling for symbolic reference updates within a transaction. In fact, I think I would go even farther:
Let's take the example of an update to HEAD, which currently points at
refs/heads/master. I think it would *always* be a good idea (i.e., even when only the files backend is in use) to split thatref_updateinto tworef_updates:1. One to update refs/heads/master and add a reflog entry for that reference.
Quoted 7 lines
2. One to add a reflog entry for HEAD (i.e. using the new
REF_LOG_ONLYflag suggested above).Why?
* It ensures that both references are locked correctly while their reflogs are updated. (I believe the current code gets this wrong.)
It's true that both refs are locked while their reflogs are updated. However, the symbolic ref isn't locked while the update to its referent happens, which is still bad. I guess we could fix that later, since I don't think this would make the situation any worse.
One thing to note is that there's a bunch of dereferencing code in files-backend.c which we could eliminate by doing the split outside the backend. I've made the basic version of this change, but haven't yet looked at whether and how we can eliminate that code. Tomorrow (I hope), I'll do some more work on that.
Quoted 5 lines
* It improves the
reject_duplicatescoverage, which (I think) currently
wouldn't detect the conflict between a direct update of refs/heads/master with a simultaneous update of the same reference via HEAD.
As it happens, the current code handles this case indirectly; the ref is locked by the first update, so the update through the second can't do get the lock, causing the whole transaction to fail. After this patch, it is handled directly (since the main transaction now contains the referent of HEAD).
Quoted 12 lines
* It could later be generalized to an update that goes through multiple
layers of symref indirection (though this would be a very low priority).This might benefit the split-backend situation that you are implementing
here. You could first do the symref-splitting step I just described, and
*then* separate the non-normal from the normal refs. I think the net result would be simpler.
Quoted 5 lines
This patch is a lot to digest. I'm not yet confident that I have thought
through all of the ramifications of this patch. I guess a few iterations
will be needed in any case.
I'm sure that's true.
Quoted 2 lines
By the way, all of the patches preceding this one that I haven't commented on look OK to me.
Thanks.
PATCH v2 1/3 refs: allow log-only updates
In reply to Michael HaggertyThe 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
@@ -2821,7 +2821,7 @@ static int commit_ref_update(struct ref_lock *lock, } } } -if (commit_ref (lock)) { +if (!(flags &REF_LOG_ONLY ) &&commit_ref (lock)) {error ("Couldn't set %s" , lock->ref_name);unlock_ref (lock);return -1 ;
@@ -3175,7 +3175,8 @@ static int files_transaction_commit(struct ref_transaction *transaction, goto cleanup; }if ((update->flags &REF_HAVE_NEW ) && - !(update->flags &REF_DELETING )) { + !(update->flags &REF_DELETING ) && + !(update->flags &REF_LOG_ONLY )) {int overwriting_symref = ((update->type &REF_ISSYMREF ) && (update->flags &REF_NODEREF ));
@@ -3205,7 +3206,9 @@ static int files_transaction_commit(struct ref_transaction *transaction, update->flags |=REF_NEEDS_COMMIT ; } } -if (!(update->flags &REF_NEEDS_COMMIT )) { + +if (!(update->flags &REF_LOG_ONLY ) && + !(update->flags &REF_NEEDS_COMMIT )) {/* * We didn't have to write anything to the lockfile. * Close it to free up the file descriptor:
@@ -3222,7 +3225,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; -if (update->flags &REF_NEEDS_COMMIT ) { +if (update->flags &REF_NEEDS_COMMIT || + update->flags &REF_LOG_ONLY ) {if (commit_ref_update (update->backend_data, update->new_sha1, update->msg, update->flags, err)) {
@@ -3242,7 +3246,8 @@ static int files_transaction_commit(struct ref_transaction *transaction,struct ref_update *update = updates[i];struct ref_lock *lock = update->backend_data; -if (update->flags &REF_DELETING ) { +if (update->flags &REF_DELETING && + !(update->flags &REF_LOG_ONLY )) {if (delete_ref_loose (lock, update->type, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto cleanup;
refs/refs-internal.h +2 -0
@@ -42,6 +42,8 @@* value to ref_update::flags */ +#define REF_LOG_ONLY 0x80 +/* Include broken references in a do_for_each_ref*() iteration */ #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v2 2/3 refs: resolve symbolic refs first
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.
--- refs.c | 70 +++++++++++++++++++++++++++++ refs/files-backend.c | 123 +++++++++++++++++++++++++-------------------------- refs/refs-internal.h | 8 ++++ 3 files changed, 139 insertions(+), 62 deletions(-)
refs.c +70 -0
@@ -1126,6 +1126,72 @@ int refs_init_db(struct strbuf *err, int shared)return the_refs_backend->init_db (err, shared); } +/* +* Special case for symbolic refs when REF_NODEREF is not turned on. +* Dereference them here, mark them REF_LOG_ONLY, and add an update +* for the underlying ref. +*/ +static int dereference_symrefs (struct ref_transaction *transaction, +struct strbuf *err) +{ +int i; +int nr = transaction->nr; + +for (i =0 ; i < nr; i++) { +struct ref_update *update = transaction->updates[i]; +const char *resolved; +unsigned char sha1[20 ]; +int resolve_flags =0 ; +int mustexist = (update->old_sha1 && + !is_null_sha1 (update->old_sha1)); +int deleting = (update->flags &REF_HAVE_NEW ) && +is_null_sha1 (update->new_sha1); +struct ref_update *new_update; + +if (mustexist) + resolve_flags |=RESOLVE_REF_READING ; +if (deleting) + resolve_flags |=RESOLVE_REF_ALLOW_BAD_NAME | +RESOLVE_REF_NO_RECURSE ; + +if (strcmp (update->refname,"HEAD" )) + update->flags |=REF_IS_NOT_HEAD ; + + resolved =resolve_ref_unsafe (update->refname, resolve_flags, + sha1, &update->type); +if (!resolved) { +/* +* We'll try again to resolve this during +* commit and give a better error message +* then, but we know it's not a symbolic ref +* (or, indeed, any sort of ref). +*/ +continue ; + } + +hashcpy (update->read_sha1, sha1); + +if (update->flags &REF_NODEREF || + !(update->type &REF_ISSYMREF )) +continue ; + +/* Create a new transaction for the underlying ref */ +if (ref_transaction_update (transaction, + resolved, + update->new_sha1, + (update->flags &REF_HAVE_OLD ) ? + update->old_sha1 :NULL , + update->flags & ~REF_IS_NOT_HEAD , + update->msg, err)) +return -1 ; + +/* Make the symbolic ref update log-only */ + update->flags |=REF_LOG_ONLY |REF_NODEREF ; + } + +return 0 ; +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {
@@ -1142,6 +1208,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,return 0 ; } + ret =dereference_symrefs (transaction, err); +if (ret) + goto done; +if (get_affected_refnames (transaction, &affected_refnames, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto done;
refs/files-backend.c +61 -62
211 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 +/* Include broken references in a do_for_each_ref*() iteration */ #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
@@ -159,6 +161,12 @@ struct ref_update {*/ unsigned char old_sha1[20 ];/* +* During the symbolic ref split stage, we resolve refs. +* We'll re-resolve non-symbolic refs once they are locked, +* but we store this to avoid re-resolving symbolic refs. +*/ +unsigned char read_sha1[20 ]; +/* * One or more of REF_HAVE_NEW, REF_HAVE_OLD, REF_NODEREF, * REF_DELETING, and REF_ISPRUNING: */
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
PATCH v2 3/3 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 | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- refs/refs-internal.h | 2 ++ 2 files changed, 82 insertions(+), 2 deletions(-)
refs.c +80 -2
@@ -9,6 +9,11 @@#include "object.h" #include "tag.h" +static const char split_transaction_fail_warning[] = +"A ref transaction was split across two refs backends. Part of the " +"transaction succeeded, but then the update to the per-worktree refs " +"failed. Your repository may be in an inconsistent state." ; +/* * We always have a files backend and it is the default. */
@@ -781,6 +786,13 @@ void ref_transaction_free(struct ref_transaction *transaction)free (transaction); } +static void add_update_obj (struct ref_transaction *transaction, +struct ref_update *update) +{ +ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); + transaction->updates[transaction->nr++] = update; +} +static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) {
@@ -788,8 +800,7 @@ static struct ref_update *add_update(struct ref_transaction *transaction,struct ref_update *update =xcalloc (1 ,sizeof (*update) + len);memcpy ((char *)update->refname, refname, len);/* includes NUL */ -ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); - transaction->updates[transaction->nr++] = update; +add_update_obj (transaction, update);return update; }
@@ -1192,11 +1203,39 @@ static int dereference_symrefs(struct ref_transaction *transaction,return 0 ; } +/* +* Move all non-normal ref updates into a specially-created +* files-backend transaction +*/ +static int move_abnormal_ref_updates (struct ref_transaction *transaction, +struct ref_transaction *files_transaction, +struct strbuf *err) +{ +int i; + +for (i =0 ; i < transaction->nr; i++) { +int last; +struct ref_update *update = transaction->updates[i]; + +if (ref_type (update->refname) ==REF_TYPE_NORMAL ) +continue ; + + last = --transaction->nr; + transaction->updates[i] = transaction->updates[last]; + files_transaction->updates[files_transaction->nr++] = update; + } + +return 0 ; +} +int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) {int ret =-1 ;struct string_list affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list files_affected_refnames =STRING_LIST_INIT_NODUP ; +struct string_list_item *item; +struct ref_transaction *files_transaction =NULL ;assert (err);
@@ -1212,6 +1251,26 @@ int ref_transaction_commit(struct ref_transaction *transaction,if (ret) goto done; +if (the_refs_backend != &refs_be_files) { + files_transaction =ref_transaction_begin (err); +if (!files_transaction) + goto done; + + ret =move_abnormal_ref_updates (transaction, files_transaction, + err); +if (ret) + goto done; + +/* files backend commit */ +if (get_affected_refnames (files_transaction, + &files_affected_refnames, + err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto done; + } + } + +/* main backend commit */ if (get_affected_refnames (transaction, &affected_refnames, err)) { ret =TRANSACTION_GENERIC_ERROR ; goto done;
@@ -1219,8 +1278,24 @@ int ref_transaction_commit(struct ref_transaction *transaction, ret = the_refs_backend->transaction_commit (transaction, &affected_refnames, err); +if (ret) + goto done; + +if (files_transaction) { + ret = refs_be_files.transaction_commit (files_transaction, + &files_affected_refnames, + err); +if (ret) { +warning (split_transaction_fail_warning); + goto done; + } + } + done:string_list_clear (&affected_refnames,0 ); +string_list_clear (&files_affected_refnames,0 ); +if (files_transaction) +ref_transaction_free (files_transaction);return ret; }
@@ -1276,6 +1351,9 @@ int peel_ref(const char *refname, unsigned char *sha1)int create_symref (const char *ref_target,const char *refs_heads_master,const char *logmsg) { +if (ref_type (ref_target) !=REF_TYPE_NORMAL ) +return refs_be_files.create_symref (ref_target, refs_heads_master, + logmsg);return the_refs_backend->create_symref (ref_target, refs_heads_master, logmsg); }
refs/refs-internal.h +2 -0
@@ -323,4 +323,6 @@ struct ref_storage_be { for_each_replace_ref_fn *for_each_replace_ref; }; +extern struct ref_storage_be refs_be_files; +#endif /* REFS_REFS_INTERNAL_H */
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
Re: [PATCH/RFC v2 3/3] refs: always handle non-normal refs in files backend
On Wed, 2016-01-06 at 18:41 -0500, David Turner wrote:
+ files_transaction->updates[files_transaction->nr++]
Quoted 1 line
= update;
oops, need to allocate this (fixed on my branch).
Re: [PATCH 12/16] refs: always handle non-normal refs in files backend
In reply to Michael HaggertyQuoted 14 lines · Michael Haggerty
I very much like the idea of introducing special handling for symbolic
reference updates within a transaction. In fact, I think I would go even
farther:Let's take the example of an update to HEAD, which currently points at
refs/heads/master. I think it would *always* be a good idea (i.e., even
when only the files backend is in use) to split thatref_updateinto tworef_updates:
Quoted 6 lines
1. One to update refs/heads/master and add a reflog entry for that reference.
2. One to add a reflog entry for HEAD (i.e. using the new
REF_LOG_ONLY
flag suggested above).
Having now implemented this, I think it's ugly.
While committing a ref_update, we read the ref's old value. We need this value for two things: 1. confirming that it matches old_sha1 in the ref_update and 2. writing to the reflog. In the case of a symbolic ref, we need to resolve it in order to get its old value. But we've already resolved it during the splitting step (in order to know that it was a symbolic ref). So we're duplicating effort. We can store the resolved SHA from the split phase (along with the resolved ref type), but that's just leaking more weird details.
In addition, this doesn't actually solve the problem that this patch is intended to solve; I still have to split refs into normal and non -normal (files-backend). It's true that this splitting is simplified, since we don't need to do weird things for the reflogs, but I'm not sure it's a net win.
I'm going to send just these patches in reply to your mail (I hope), and see what you think before continuing down this path.
PATCH 13/16 init: allow alternate backends to be set for new repos
In reply to David Turnergit init learns a new argument --refs-backend-type. Presently, only "files" is supported, but later we will add other backends.
When this argument is used, the repository's core.refsBackendType configuration value is set, and the refs backend's initdb function is used to set up the ref database.
--- Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 6 +++++- builtin/init-db.c | 10 ++++++++++ cache.h | 2 ++ config.c | 20 ++++++++++++++++++++ environment.c | 1 + path.c | 32 ++++++++++++++++++++++++++++++-- refs.c | 8 ++++++++ refs.h | 12 ++++++++++++ setup.c | 10 ++++++++++ 10 files changed, 99 insertions(+), 4 deletions(-)
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>]] [--refs-backend-type=<name>] DESCRIPTION
Documentation/git-init.txt +5 -1
@@ -12,7 +12,7 @@ SYNOPSIS 'git init' [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [directory] - + [--refs-backend-type=<name>] DESCRIPTION -----------
@@ -113,6 +113,10 @@ does not exist, it will be created. -- +--refs-backend-type=<name>:: +Type of refs backend. Default is to use the original "files" backend, +which stores ref data in files in .git/refs and .git/packed-refs. + TEMPLATE DIRECTORY ------------------
builtin/init-db.c +10 -0
@@ -204,6 +204,14 @@ static int create_default_files(const char *template_path)adjust_shared_perm (get_git_dir ()); } +if (refs_backend_type) { +struct refdb_config_data config_data = {NULL }; +git_config_set ("core.refsBackendType" , refs_backend_type); + config_data.refs_backend_type = refs_backend_type; + config_data.refs_base =get_git_dir (); +set_refs_backend (refs_backend_type, &config_data); + } +if (refs_init_db (&err, shared_repository))die ("failed to set up refs db: %s" , err.buf);
@@ -469,6 +477,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)OPT_BIT ('q' ,"quiet" , &flags,N_ ("be quiet" ),INIT_DB_QUIET ),OPT_STRING (0 ,"separate-git-dir" , &real_git_dir,N_ ("gitdir" ),N_ ("separate git dir from working tree" )), +OPT_STRING (0 ,"refs-backend-type" , &refs_backend_type, +N_ ("name" ),N_ ("name of backend type to use" )), OPT_END() };
cache.h +2 -0
@@ -696,6 +696,8 @@ extern enum object_creation_mode object_creation_mode;extern char *notes_ref_name; +extern const char *refs_backend_type; +extern int grafts_replace_parents;/*
config.c +20 -0
@@ -10,6 +10,7 @@#include "exec_cmd.h" #include "strbuf.h" #include "quote.h" +#include "refs.h" #include "hashmap.h" #include "string-list.h" #include "utf8.h"
@@ -1207,6 +1208,25 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) }if (repo_config && !access_or_die (repo_config,R_OK ,0 )) { +struct refdb_config_data refdb_data = {NULL }; +char *repo_config_copy; + +/* +* make sure we always read the backend config from the +* core section on startup +*/ + ret +=git_config_from_file (refdb_config, repo_config, + &refdb_data); + + repo_config_copy =xstrdup (repo_config); + refdb_data.refs_base =xstrdup (dirname (repo_config_copy)); +free (repo_config_copy); + +if (refdb_data.refs_backend_type && +strcmp (refdb_data.refs_backend_type,"files" )) { +die ("Unexpected backend %s" , refdb_data.refs_backend_type); + } + ret +=git_config_from_file (fn, repo_config, data); found +=1 ; }
environment.c +1 -0
@@ -66,6 +66,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ struct startup_info *startup_info;unsigned long pack_size_limit_cfg; +const char *refs_backend_type;#ifndef PROTECT_HFS_DEFAULT #define PROTECT_HFS_DEFAULT 0
path.c +30 -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"
@@ -510,9 +511,36 @@ int validate_headref(const char *path)unsigned char sha1[20 ];int fd;ssize_t len; +struct refdb_config_data refdb_data = {NULL ,NULL }; + +if (lstat (path, &st) <0 ) { +int backend_type_set; +struct strbuf config_path =STRBUF_INIT ; +if (path) { +char *pathdup =xstrdup (path); +char *git_dir =dirname (pathdup); +strbuf_addf (&config_path,"%s/%s" , git_dir,"config" ); +free (pathdup); + }else { +strbuf_addstr (&config_path,"config" ); + } -if (lstat (path, &st) <0 ) -return -1 ; +if (git_config_from_file (refdb_config, config_path.buf, &refdb_data)) { +strbuf_release (&config_path); +return -1 ; + } + + backend_type_set = !!refdb_data.refs_backend_type; +free ((void *)refdb_data.refs_backend_type); +free ((void *)refdb_data.refs_base); +strbuf_release (&config_path); +/* +* Alternate backends are assumed to keep HEAD +* in a valid state, so there's no need to do +* further validation. +*/ +return backend_type_set ?0 :-1 ; + }/* Make sure it is a "refs/.." symlink */ if (S_ISLNK (st.st_mode)) {
refs.c +8 -0
@@ -24,6 +24,14 @@ struct ref_be *the_refs_backend = &refs_be_files;*/ struct ref_be *refs_backends = &refs_be_files; +const char *refs_backend_type; + +void register_refs_backend (struct ref_be *be) +{ + be->next = refs_backends; + refs_backends = be; +} +/* * This function is used to switch to an alternate backend. */
refs.h +12 -0
@@ -510,6 +510,18 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data); +struct refdb_config_data { +const char *refs_backend_type; +const char *refs_base; +}; +/* +* Read the refdb configuration data out of the config file +*/ +int refdb_config (const char *var,const char *value,void *ptr); + +struct ref_be ;int set_refs_backend (const char *name,void *data); +void register_refs_backend (struct ref_be *be); +#endif /* REFS_H */
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 refdb_config (const char *var,const char *value,void *ptr) +{ +struct refdb_config_data *cdata = ptr; + +if (!strcmp (var,"core.refsbackendtype" )) + cdata->refs_backend_type =xstrdup ((char *)value); +return 0 ; +} +/* * Test if it looks like we're at a git directory. * We want to see:
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 6 lines · David Turner
git init learns a new argument --refs-backend-type. Presently, only "files" is supported, but later we will add other backends.
When this argument is used, the repository's core.refsBackendType configuration value is set, and the refs backend's initdb function is used to set up the ref database.
git-init can also be used on existing repos. What happens in that case if we use --refs-backend-type. Will existing refs be migrated to the new backend or hidden away?
Signature · 1 line
Duy
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 11 lines · Duy Nguyen
Quoted 6 lines · David Turner
git init learns a new argument --refs-backend-type. Presently, only "files" is supported, but later we will add other backends.
When this argument is used, the repository's core.refsBackendType configuration value is set, and the refs backend's initdb function is used to set up the ref database.
git-init can also be used on existing repos. What happens in that case if we use --refs-backend-type. Will existing refs be migrated to the new backend or hidden away?
It would be neat if it migrated, but I suspect that may introduce complications. It's probably OK in the initial implementation to bail if the option is used in an existing repo.
I think the config option needs to be extensions.refsBackendType, too, per the logic in 00a09d5 (introduce "extensions" form of core.repositoryformatversion, 2015-06-23). And I guess it needs to bump core.repositoryformatversion to "1".
-Peff
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 6 lines · Jeff King
Quoted 4 lines · Duy Nguyen
Quoted 3 lines · David Turner
git init learns a new argument --refs-backend-type. Presently, only "files" is supported, but later we will add other backends.
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
When this argument is used, the repository's core.refsBackendType configuration value is set, and the refs backend's initdb function is used to set up the ref database.
git-init can also be used on existing repos. What happens in that case if we use --refs-backend-type. Will existing refs be migrated to the new backend or hidden away?
I'd like to migrate, but I don't want to write that code prematurely. I figure we can get everything else right first.
Quoted 3 lines
It would be neat if it migrated, but I suspect that may introduce complications. It's probably OK in the initial implementation to bail if the option is used in an existing repo.
Will fix.
Quoted 4 lines
I think the config option needs to be extensions.refsBackendType, too, per the logic in 00a09d5 (introduce "extensions" form of core.repositoryformatversion, 2015-06-23). And I guess it needs to bump core.repositoryformatversion to "1".
I did that in a later patch, but now is a better time; will fix.
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
In reply to Jeff KingQuoted 5 lines · Jeff King
[...]
I think the config option needs to be extensions.refsBackendType, too, per the logic in 00a09d5 (introduce "extensions" form of core.repositoryformatversion, 2015-06-23). And I guess it needs to bump core.repositoryformatversion to "1".
I think also, strictly speaking, the extensions.refsBackendType option should be ignored if core.repositoryFormatVersion is not "1". In practice, it probably makes more sense for the code to error out in that case because it is likely the result of a bug.
Note that if the user explicitly chooses the "files" backend, it would be preferable to leave "core.repositoryFormatVersion" at "0" (assuming that no other extension is being used) and leave "extensions.refsBackendType" unset. This approach creates a repository that is compatible with older clients that don't know about refsBackendTypes.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 11 lines · Michael Haggerty
Quoted 5 lines · Jeff King
[...]
I think the config option needs to be extensions.refsBackendType, too, per the logic in 00a09d5 (introduce "extensions" form of core.repositoryformatversion, 2015-06-23). And I guess it needs to bump core.repositoryformatversion to "1".I think also, strictly speaking, the extensions.refsBackendType option should be ignored if core.repositoryFormatVersion is not "1". In practice, it probably makes more sense for the code to error out in that case because it is likely the result of a bug.
Yeah, I agree. It's not wrong, but it may be a good indication the user is confused. Perhaps issuing a warning would be appropriate.
Quoted 6 lines
Note that if the user explicitly chooses the "files" backend, it would be preferable to leave "core.repositoryFormatVersion" at "0" (assuming that no other extension is being used) and leave "extensions.refsBackendType" unset. This approach creates a repository that is compatible with older clients that don't know about refsBackendTypes.
Also agreed. This is going to be the case for basically every "extension" option. If the extension config key isn't present, there will be some default backwards-compatible behavior, and for compatibility, you are always better off turning off the extension than setting turning it "on" with that default value. This is the case for the "preciousObjects" extension: there is no point in setting it to "false" as opposed to removing it.
So I think the rules for setting an extension value should be:
1. If you're setting it to the default value, then remove it completely.
2. If you're removing it, and there are no other extensions, set repositoryFormatVersion back to 0.
Of course there's currently no code that sets extension values. And doing step 1 is going to be specific to each extension. So this a general philosophy to follow, not something we would write in a function.
-Peff
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
In reply to David TurnerQuoted 14 lines · David Turner
setup.c +1
-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; }
setup.c int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) · line 275
Quoted 10 lines
setup.c +9
-0+int refdb_config (const char *var,const char *value,void *ptr) +{ +struct refdb_config_data *cdata = ptr; + +if (!strcmp (var,"core.refsbackendtype" )) + cdata->refs_backend_type =xstrdup ((char *)value); +return 0 ; +} +
These lines seem to be indented with SPs not HTs.
Quoted 3 lines
setup.c +0
-0/* * Test if it looks like we're at a git directory. * We want to see:
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
In reply to David TurnerQuoted 7 lines · David Turner
path.c +3
-0@@ -510,9 +511,36 @@ int validate_headref(const char *path)unsigned char sha1[20 ];int fd;ssize_t len; +struct refdb_config_data refdb_data = {NULL ,NULL }; + +if (lstat (path, &st) <0 ) {
path.c int validate_headref(const char *path) · line 526
Quoted 10 lines
path.c +10
-0+int backend_type_set; +struct strbuf config_path =STRBUF_INIT ; +if (path) { +char *pathdup =xstrdup (path); +char *git_dir =dirname (pathdup); +strbuf_addf (&config_path,"%s/%s" , git_dir,"config" ); +free (pathdup); + }else { +strbuf_addstr (&config_path,"config" ); + }
Can we ever follow this "else" branch? It would mean feeding NULL to lstat.
-Peff
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 14 lines · Jeff King
Quoted 12 lines · David Turner
path.c +8
-0@@ -510,9 +511,36 @@ int validate_headref(const char *path)unsigned char sha1[20 ];int fd;ssize_t len; +struct refdb_config_data refdb_data = {NULL ,NULL }; + +if (lstat (path, &st) <0 ) { +int backend_type_set; +struct strbuf config_path =STRBUF_INIT ; +if (path) { +char *pathdup =xstrdup (path); +char *git_dir =dirname (pathdup);
Quoted 8 lines
path.c int validate_headref(const char *path) · line 526
Quoted 5 lines
path.c +5
-0+strbuf_addf (&config_path,"%s/%s" , git_dir,"config" ); +free (pathdup); + }else { +strbuf_addstr (&config_path,"config" ); + }Can we ever follow this "else" branch? It would mean feeding NULL to lstat.
Probably cruft from an old version of the patch. Removed if/else. Thanks.
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
In reply to David TurnerQuoted 39 lines · David Turner
git init learns a new argument --refs-backend-type. Presently, only "files" is supported, but later we will add other backends.
When this argument is used, the repository's core.refsBackendType configuration value is set, and the refs backend's initdb function is used to set up the ref database.
Signed-off-by David Turner <[email protected]>--- Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 6 +++++- builtin/init-db.c | 10 ++++++++++ cache.h | 2 ++ config.c | 20 ++++++++++++++++++++ environment.c | 1 + path.c | 32 ++++++++++++++++++++++++++++++-- refs.c | 8 ++++++++ refs.h | 12 ++++++++++++ setup.c | 10 ++++++++++ 10 files changed, 99 insertions(+), 4 deletions(-)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>]] [--refs-backend-type=<name>] DESCRIPTIONDocumentation/git-init.txt +0
-0
Documentation/git-init.txt SYNOPSIS · line 15
Quoted 5 lines
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] - + [--refs-backend-type=<name>]
ISTM that "backend" (used here in this option name, and in the manpage) is not such a meaningful term to users. Could we pick a less obscure term? E.g., maybe "--ref-storage=<name>"?
Quoted 19 lines
Documentation/git-init.txt +4
-0DESCRIPTION -----------@@ -113,6 +113,10 @@ does not exist, it will be created. -- +--refs-backend-type=<name>:: +Type of refs backend. Default is to use the original "files" backend, +which stores ref data in files in .git/refs and .git/packed-refs. + TEMPLATE DIRECTORY ------------------builtin/init-db.c +0
-0@@ -204,6 +204,14 @@ static int create_default_files(const char *template_path)adjust_shared_perm (get_git_dir ());
builtin/init-db.c static int create_default_files(const char *template_path) · line 214
Quoted 10 lines
builtin/init-db.c +8
-0} +if (refs_backend_type) { +struct refdb_config_data config_data = {NULL }; +git_config_set ("core.refsBackendType" , refs_backend_type); + config_data.refs_backend_type = refs_backend_type; + config_data.refs_base =get_git_dir (); +set_refs_backend (refs_backend_type, &config_data); + } +
Here is the source of the "void *data" argument that puzzled me in patch 08/16. I'm still puzzled. This code, which is later also used for the LMDB backend, *always* passes that function a "struct refdb_config_data *". So why is the argument declared to be "void *"?
If, on the other hand, you want to preserve a backend's freedom to require different extra data in this parameter, then this code in init-db.c, and code like it elsewhere, would have to know about the reference backends so that it knows what data to pass to each one. In that case, there would be no reason to make this function virtual; this code could just as well call a different function (with a different signature) depending on the backend that is in use.
Either way, something seems strange here.
(Remember that another alternative is to let the refs backend read whatever specialized information it needs from the config by itself.)
Quoted 49 lines
builtin/init-db.c +2
-0if (refs_init_db (&err, shared_repository))die ("failed to set up refs db: %s" , err.buf);@@ -469,6 +477,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)OPT_BIT ('q' ,"quiet" , &flags,N_ ("be quiet" ),INIT_DB_QUIET ),OPT_STRING (0 ,"separate-git-dir" , &real_git_dir,N_ ("gitdir" ),N_ ("separate git dir from working tree" )), +OPT_STRING (0 ,"refs-backend-type" , &refs_backend_type, +N_ ("name" ),N_ ("name of backend type to use" )), OPT_END() };cache.h +2
-0@@ -696,6 +696,8 @@ extern enum object_creation_mode object_creation_mode;extern char *notes_ref_name; +extern const char *refs_backend_type; +extern int grafts_replace_parents;/* config.c +9
-0@@ -10,6 +10,7 @@#include "exec_cmd.h" #include "strbuf.h" #include "quote.h" +#include "refs.h" #include "hashmap.h" #include "string-list.h" #include "utf8.h" @@ -1207,6 +1208,25 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) }if (repo_config && !access_or_die (repo_config,R_OK ,0 )) { +struct refdb_config_data refdb_data = {NULL }; +char *repo_config_copy; + +/* +* make sure we always read the backend config from the +* core section on startup +*/ + ret += git_config_from_file(refdb_config, repo_config,
config.c int git_config_early(config_fn_t fn, void *data, const char *repo_config) · line 1228
Quoted 10 lines
config.c +10
-0+ &refdb_data); + + repo_config_copy =xstrdup (repo_config); + refdb_data.refs_base =xstrdup (dirname (repo_config_copy)); +free (repo_config_copy); + +if (refdb_data.refs_backend_type && +strcmp (refdb_data.refs_backend_type,"files" )) { +die ("Unexpected backend %s" , refdb_data.refs_backend_type); + }
The refs code already maintains a list of valid backend names. It would be better to ask it to validate this parameter than to maintain a second list here. Or just call set_refs_backend() and let *it* fail.
Quoted 54 lines
config.c +1
-0+ ret +=git_config_from_file (fn, repo_config, data); found +=1 ; }environment.c +1
-0@@ -66,6 +66,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ struct startup_info *startup_info;unsigned long pack_size_limit_cfg; +const char *refs_backend_type;#ifndef PROTECT_HFS_DEFAULT #define PROTECT_HFS_DEFAULT 0path.c +20
-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" @@ -510,9 +511,36 @@ int validate_headref(const char *path)unsigned char sha1[20 ];int fd;ssize_t len; +struct refdb_config_data refdb_data = {NULL ,NULL }; + +if (lstat (path, &st) <0 ) { +int backend_type_set; +struct strbuf config_path =STRBUF_INIT ; +if (path) { +char *pathdup =xstrdup (path); +char *git_dir =dirname (pathdup); +strbuf_addf (&config_path,"%s/%s" , git_dir,"config" ); +free (pathdup); + }else { +strbuf_addstr (&config_path,"config" ); + } -if (lstat (path, &st) <0 ) -return -1 ; +if (git_config_from_file (refdb_config, config_path.buf, &refdb_data)) { +strbuf_release (&config_path); +return -1 ; + } + + backend_type_set = !!refdb_data.refs_backend_type;
path.c int validate_headref(const char *path) · line 543
Quoted 10 lines
path.c +10
-0+free ((void *)refdb_data.refs_backend_type); +free ((void *)refdb_data.refs_base); +strbuf_release (&config_path); +/* +* Alternate backends are assumed to keep HEAD +* in a valid state, so there's no need to do +* further validation. +*/ +return backend_type_set ?0 :-1 ; + }
I'm not sure what problem you are trying to solve with this particular change. Won't a lmdb-backed repository still have a "HEAD" file with the usual contents?
In any case, it seems like you are doing work that belongs at a higher level.
validate_headref() has only one caller, is_git_directory(). The purpose of that function is to check whether a specified directory is a plausible Git directory, which it does by looking for some required files:
* "objects/" (unless GIT_OBJECT_DIRECTORY is set)
* "refs/"
* "HEAD" (which must also have appropriate contents)
NB: it doesn't care whether there is a "config" file. Presumably that was invented later.
It seems to me that whether a repository should have a "refs/" and/or "HEAD", and if so what contents "HEAD" should have, or whether (for example) it should have a "refdb" file, depends on what reference backend is in use. But *that* depends on the contents of "config" or its absence.
You are reading "config" down here in validate_headref(). It feels to me more like is_git_directory() should read "config", determine which reference backend seems to be in use, and then ask *that* reference backend whether the directory is a plausible Git directory. The files backend would check for "HEAD" and "refs/" and the LMDB backend would check for "HEAD" and "refdb/".
Note that this approach would require "config" to be read and parsed an extra time. That might be considered too expensive to do so early in the startup code. I don't know.
By the way, this code ends up reading "config" files a bit more promiscuously than the old code. I don't think this raises any security considerations, but I wanted to point it out anyway. If somebody can put a hostile "config" file in a directory where you are running git, then they can probably put an "objects/", "refs/", and "HEAD" file there, which is all that is needed to get even the old code to read the "config" file.
Quoted 56 lines
path.c +0
-0/* Make sure it is a "refs/.." symlink */ if (S_ISLNK (st.st_mode)) {refs.c +8
-0@@ -24,6 +24,14 @@ struct ref_be *the_refs_backend = &refs_be_files;*/ struct ref_be *refs_backends = &refs_be_files; +const char *refs_backend_type; + +void register_refs_backend (struct ref_be *be) +{ + be->next = refs_backends; + refs_backends = be; +} +/* * This function is used to switch to an alternate backend. */ refs.h +12
-0@@ -510,6 +510,18 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data); +struct refdb_config_data { +const char *refs_backend_type; +const char *refs_base; +}; +/* +* Read the refdb configuration data out of the config file +*/ +int refdb_config (const char *var,const char *value,void *ptr); + +struct ref_be ;int set_refs_backend (const char *name,void *data); +void register_refs_backend (struct ref_be *be); +#endif /* REFS_H */ setup.c +1
-0@@ -1,5 +1,6 @@#include "cache.h" #include "dir.h" +#include "refs.h" #include "string-list.h" static int inside_git_dir =-1 ;
setup.c int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) · line 272
Quoted 9 lines
setup.c +6
-0@@ -263,6 +264,15 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)return ret; } +int refdb_config (const char *var,const char *value,void *ptr) +{ +struct refdb_config_data *cdata = ptr; + +if (!strcmp (var,"core.refsbackendtype" )) + cdata->refs_backend_type =xstrdup ((char *)value);
Unnecessary cast.
setup.c int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) · line 278
Quoted 6 lines
setup.c +3
-0+return 0 ; +} +/* * Test if it looks like we're at a git directory. * We want to see:
I can guarantee that before long somebody will want a config option that can be put in a global .gitconfig to choose the default refs backend type for new repositories when it is not specified explicitly via the command line. But that can wait :-)
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 52 lines · Michael Haggerty
Quoted 51 lines · David Turner
git init learns a new argument --refs-backend-type. Presently, only
"files" is supported, but later we will add other backends.When this argument is used, the repository's core.refsBackendType configuration value is set, and the refs backend's initdb function is
used to set up the ref database.Signed-off-by David Turner <[email protected]>--- Documentation/git-init-db.txt | 2 +- Documentation/git-init.txt | 6 +++++- builtin/init-db.c | 10 ++++++++++ cache.h | 2 ++ config.c | 20 ++++++++++++++++++++ environment.c | 1 + path.c | 32 ++++++++++++++++++++++++++++++-(quoted diff) +0
-1- refs.c | 8 ++++++++ refs.h | 12 ++++++++++++ setup.c | 10 ++++++++++ 10 files changed, 99 insertions(+), 4 deletions(-)Documentation/git-init renamed from Documentation/git-init-db.txt +0
-1-db.txtindex 648a6cd..72fbd71 100644
Documentation/git-init-db.txt +1
-5@@ -9,7 +9,7 @@ git-init-db - Creates an empty Git repository SYNOPSIS -------- [verse] -'git init-db' [-q | --quiet] [--bare] [- -template=<template_directory>] [--separate-git-dir <git dir>] [- -shared[=<permissions>]] +'git init-db' [-q | --quiet] [--bare] [- -template=<template_directory>] [--separate-git-dir <git dir>] [- -shared[=<permissions>]] [--refs-backend-type=<name>] DESCRIPTIONDocumentation/git renamed from Documentation/git-init.txt +0
-1-init.txtindex 8174d27..9ea6753 100644
Documentation/git-init.txt +0
-1@@ -12,7 +12,7 @@ SYNOPSIS 'git init' [-q | --quiet] [--bare] [- -template=<template_directory>] [--separate-git-dir <git dir>]
Quoted 8 lines
Documentation/git-init.txt SYNOPSIS · line 15
Quoted 3 lines
Documentation/git-init.txt +1
-1[--shared[=<permissions>]] [directory] - + [--refs-backend-type=<name>]ISTM that "backend" (used here in this option name, and in the manpage)
is not such a meaningful term to users. Could we pick a less obscure term? E.g., maybe "--ref-storage=<name>"?
I'm working on the rest now, but wanted to comment on this first. I went ahead and made this change, but I'm not sure I like it. In the git codebase, the concept will continue to be called "backend"; there are already-accepted patches using that terminology. Having two separate names for the same thing seems confusing to me. We have this problem already with the index ("cache" or "staging area"), which is a little annoying when grepping the code.
(For refs-backend-type, the name of the config variable is a third separate thing, where we'll have to choose between the internal and the external name).
So I think maybe we should just go with refs-backend here. What do others think?
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 5 lines · David Turner
I'm working on the rest now, but wanted to comment on this first. I went ahead and made this change, but I'm not sure I like it. In the git codebase, the concept will continue to be called "backend"; there are already-accepted patches using that terminology. Having two separate names for the same thing seems confusing to me.
We have the option to update whatever "are already-accepted" [*1*]. That would allow us to uniformly call it "ref storage", if we wanted to.
In any case, we shouldn't be using an unqualified "backend" (or "storage" for that matter); we should always say "ref", i.e. either "ref backend" or "ref storage", in the name.
Between "backend" and "storage", I am slightly in favor of the latter, but I am not good at naming things so...
[Footnote]
*1* Output from
$ git grep backend master --
seems to show me only
master:refs.c: * The backend-independent part of the reference module.
and all others are other kinds of backends, e.g. "merge backend", "http-backend", etc. so that may not be too bad.
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 29 lines · Junio C Hamano
Quoted 8 lines · David Turner
I'm working on the rest now, but wanted to comment on this first.
I
went ahead and made this change, but I'm not sure I like it. In the
git codebase, the concept will continue to be called "backend"; there
are already-accepted patches using that terminology. Having two separate names for the same thing seems confusing to me.We have the option to update whatever "are already-accepted" [*1*]. That would allow us to uniformly call it "ref storage", if we wanted to.
In any case, we shouldn't be using an unqualified "backend" (or "storage" for that matter); we should always say "ref", i.e. either "ref backend" or "ref storage", in the name.
Between "backend" and "storage", I am slightly in favor of the latter, but I am not good at naming things so...
[Footnote]
*1* Output from
$ git grep backend master --
Quoted 7 lines
seems to show me only
master:refs.c: * The backend-independent part of the reference
module.and all others are other kinds of backends, e.g. "merge backend", "http-backend", etc. so that may not be too bad.
There's refs/files-backend.c in master.
I guess the argument for "backend" is that it is a better description of the struct. That is, "a storage" sounds funny. Usually "storage" is a mass noun. I guess we could call them "storage backends" (with "ref-storage" in the UI), which would split the difference. I guess I'll go with that, and we can decide later whether to rename those files.
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
In reply to Junio C HamanoQuoted 11 lines · Junio C Hamano
Quoted 5 lines · David Turner
I'm working on the rest now, but wanted to comment on this first. I went ahead and made this change, but I'm not sure I like it. In the git codebase, the concept will continue to be called "backend"; there are already-accepted patches using that terminology. Having two separate names for the same thing seems confusing to me.
We have the option to update whatever "are already-accepted" [*1*]. That would allow us to uniformly call it "ref storage", if we wanted to.
...whereas whatever we name the option, we have to live with forever because it is user-facing. It's more important to get the option name correct (though I agree that it would be nice for the nomenclature used in the code to be reminiscent of the option name).
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
In reply to Michael HaggertyQuoted 16 lines · Michael Haggerty
Documentation/git-init.txt SYNOPSIS · line 15
Quoted 10 lines · David Turner
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] - + [--refs-backend-type=<name>]ISTM that "backend" (used here in this option name, and in the manpage) is not such a meaningful term to users. Could we pick a less obscure term? E.g., maybe "--ref-storage=<name>"?
Quoted 1 line
From an (ex-)translator point of view, storage is also easier to
translate than the technical term "backend". I know we do not translate option names, but whatever term you use usually show up in some user-facing messages that need translating. But I do prefer backend in source code, I think it expresses the idea much better than storage.
Signature · 1 line
Duy
Re: [PATCH 13/16] init: allow alternate backends to be set for new repos
Quoted 17 lines · Duy Nguyen
Quoted 15 lines · Michael Haggerty
Documentation/git-init.txt SYNOPSIS · line 15
Quoted 10 lines · David Turner
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] - + [--refs-backend-type=<name>]ISTM that "backend" (used here in this option name, and in the manpage) is not such a meaningful term to users. Could we pick a less obscure term? E.g., maybe "--ref-storage=<name>"?
Quoted 6 lines
From an (ex-)translator point of view, storage is also easier to translate than the technical term "backend". I know we do not translate option names, but whatever term you use usually show up in some user-facing messages that need translating. But I do prefer backend in source code, I think it expresses the idea much better than storage.
And JGit calls these things RefDatabases. Because Java.
Also, its a table of ref -> SHA-1.
And you put tables into databases.
Or something.
</two-cents>
PATCH 1/1 clone: use child_process for recursive checkouts
In reply to David Turner---
David, I think if you insert this patch before your
13/16 refs: allow ref backend to be set for clone
, then the hunk in builtin/clone.c:checkout() of your patch becomes trivial:
if (refs_backend_type) argv_array_pushf(&cmd.args, "--refs-backend-type=%s", refs_backend_type);
builtin/clone.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
builtin/clone.c +8 -6
@@ -95,10 +95,6 @@ static struct option builtin_clone_options[] = { OPT_END() }; -static const char *argv_submodule[] = { -"submodule" ,"update" ,"--init" ,"--recursive" ,NULL -}; -static const char *get_repo_path_1 (struct strbuf *path,int *is_bundle) {static char *suffix[] = {"/.git" ,"" ,".git/.git" ,".git" };
@@ -724,8 +720,14 @@ static int checkout(void) err |=run_hook_le (NULL ,"post-checkout" ,sha1_to_hex (null_sha1),sha1_to_hex (sha1),"1" ,NULL ); -if (!err && option_recursive) - err =run_command_v_opt (argv_submodule,RUN_GIT_CMD ); +if (!err && option_recursive) { +struct child_process cmd =CHILD_PROCESS_INIT ; + + cmd.git_cmd =1 ; +argv_array_pushl (&cmd.args,"submodule" ,"update" , +"--init" ,"--recursive" ,NULL ); + err =run_command (&cmd); + }return err; }
Signature · 1 line
2.6.4
Re: [PATCH] clone: use child_process for recursive checkouts
Quoted 4 lines · Michael Haggerty
Signed-off-by Michael Haggerty <[email protected]>---
David, I think if you insert this patch before your
Quoted 8 lines
13/16 refs: allow ref backend to be set for clone
, then the hunk in builtin/clone.c:checkout() of your patch becomes trivial:
if (refs_backend_type) argv_array_pushf(&cmd.args, "--refs-backend-type=%s", refs_backend_type);
Inserted, thanks.
PATCH 14/16 refs: allow ref backend to be set for clone
In reply to David TurnerAdd a new option, --refs-backend-type, to allow the ref backend type to be set on new clones.
Submodules must use the same ref backend as the parent repository, so we also pass the --refs-backend-type option option when cloning submodules.
--- Documentation/git-clone.txt | 4 ++++ builtin/clone.c | 27 +++++++++++++++++++++++++-- builtin/submodule--helper.c | 5 ++++- cache.h | 1 + 4 files changed, 34 insertions(+), 3 deletions(-)
Documentation/git-clone.txt +4 -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] + [--refs-backend-type=<name>] [--recursive | --recurse-submodules] [--] <repository> [<directory>]
@@ -221,6 +222,9 @@ objects from the source repository into a pack in the cloned repository. The result is Git repository can be separated from working tree. +--refs-backend-type=<name>:: + Type of refs backend. Default is to use the original files based + backend. <repository>:: The (possibly remote) repository to clone from. See the
builtin/clone.c +25 -2
@@ -92,11 +92,13 @@ static struct option builtin_clone_options[] = { N_("separate git dir from working tree")),OPT_STRING_LIST ('c' ,"config" , &option_config,N_ ("key=value" ),N_ ("set config inside the new repository" )), +OPT_STRING (0 ,"refs-backend-type" , &refs_backend_type, +N_ ("name" ),N_ ("name of backend type to use" )), OPT_END() };static const char *argv_submodule[] = { -"submodule" ,"update" ,"--init" ,"--recursive" ,NULL +"submodule" ,"update" ,"--init" ,"--recursive" ,NULL ,NULL };static const char *get_repo_path_1 (struct strbuf *path,int *is_bundle)
@@ -724,8 +726,24 @@ static int checkout(void) err |=run_hook_le (NULL ,"post-checkout" ,sha1_to_hex (null_sha1),sha1_to_hex (sha1),"1" ,NULL ); -if (!err && option_recursive) +if (!err && option_recursive) { +const char **backend_arg = argv_submodule; +char *new_backend_arg =NULL ; +if (refs_backend_type) { +while (*backend_arg) + ++backend_arg; + + new_backend_arg =xmalloc (21 +strlen (refs_backend_type)); +sprintf (new_backend_arg,"--refs-backend-type=%s" , + refs_backend_type); + *backend_arg = new_backend_arg; + } err =run_command_v_opt (argv_submodule,RUN_GIT_CMD ); +if (refs_backend_type) { +free (new_backend_arg); + *backend_arg =NULL ; + } + }return err; }
@@ -744,6 +762,11 @@ static void write_config(struct string_list *config) write_one_config, NULL) < 0)die ("unable to write parameters to config file" ); } + +if (refs_backend_type && +write_one_config ("core.refsBackendType" , + refs_backend_type,NULL ) <0 ) +die ("unable to write backend parameter to config file" ); } static void write_refspec_config(const char *src_ref_prefix,
builtin/submodule--helper.c +4 -1
@@ -140,7 +140,10 @@ 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 ); - +if (refs_backend_type && *refs_backend_type) { +argv_array_push (&cp.args,"--refs-backend-type" ); +argv_array_push (&cp.args, refs_backend_type); + }argv_array_push (&cp.args, url);argv_array_push (&cp.args, path);
cache.h +1 -0
@@ -695,6 +695,7 @@ enum object_creation_mode {extern enum object_creation_mode object_creation_mode;extern char *notes_ref_name; +extern const char *refs_backend_type;extern const char *refs_backend_type;
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 14/16] refs: allow ref backend to be set for clone
Quoted 4 lines · David Turner
Add a new option, --refs-backend-type, to allow the ref backend type to be set on new clones.
Submodules must use the same ref backend as the parent repository, so
^^^ is this verified anywhere? Hint: if not, it probably should be. For example, imagine a future where an existing repository can be migrated in place from files -> lmdb. If somebody runs that command in a submodule, this invariant would be broken and *this* version of git would have to at least detect the problem and refuse to corrupt things.
I added CC to a couple submodules experts in case they have any feedback. Are there any other code paths that can lead to submodules being newly cloned, where this option would also have to be passed?
Quoted 65 lines
we also pass the --refs-backend-type option option when cloning submodules.
Signed-off-by David Turner <[email protected]>--- Documentation/git-clone.txt | 4 ++++ builtin/clone.c | 27 +++++++++++++++++++++++++-- builtin/submodule--helper.c | 5 ++++- cache.h | 1 + 4 files changed, 34 insertions(+), 3 deletions(-)Documentation/git-clone.txt +4
-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] + [--refs-backend-type=<name>] [--recursive | --recurse-submodules] [--] <repository> [<directory>]@@ -221,6 +222,9 @@ objects from the source repository into a pack in the cloned repository. The result is Git repository can be separated from working tree. +--refs-backend-type=<name>:: + Type of refs backend. Default is to use the original files based + backend. <repository>:: The (possibly remote) repository to clone from. See thebuiltin/clone.c +11
-2@@ -92,11 +92,13 @@ static struct option builtin_clone_options[] = { N_("separate git dir from working tree")),OPT_STRING_LIST ('c' ,"config" , &option_config,N_ ("key=value" ),N_ ("set config inside the new repository" )), +OPT_STRING (0 ,"refs-backend-type" , &refs_backend_type, +N_ ("name" ),N_ ("name of backend type to use" )), OPT_END() };static const char *argv_submodule[] = { -"submodule" ,"update" ,"--init" ,"--recursive" ,NULL +"submodule" ,"update" ,"--init" ,"--recursive" ,NULL ,NULL };static const char *get_repo_path_1 (struct strbuf *path,int *is_bundle)@@ -724,8 +726,24 @@ static int checkout(void) err |=run_hook_le (NULL ,"post-checkout" ,sha1_to_hex (null_sha1),sha1_to_hex (sha1),"1" ,NULL ); -if (!err && option_recursive) +if (!err && option_recursive) { +const char **backend_arg = argv_submodule; +char *new_backend_arg =NULL ; +if (refs_backend_type) { +while (*backend_arg) + ++backend_arg; + + new_backend_arg =xmalloc (21 +strlen (refs_backend_type));
builtin/clone.c static int checkout(void) · line 746
Quoted 10 lines
builtin/clone.c +9
-0+sprintf (new_backend_arg,"--refs-backend-type=%s" , + refs_backend_type); + *backend_arg = new_backend_arg; + } err =run_command_v_opt (argv_submodule,RUN_GIT_CMD ); +if (refs_backend_type) { +free (new_backend_arg); + *backend_arg =NULL ; + } + }
I sent a separate email about this hunk:
http://mid.gmane.org/c21eb4a5d3a3a4886c45da0abe307fe1772e932e.1450869637.git.mhagger@alum.mit.edu
Quoted 20 lines
builtin/clone.c +5
-0return err; }@@ -744,6 +762,11 @@ static void write_config(struct string_list *config) write_one_config, NULL) < 0)die ("unable to write parameters to config file" ); } + +if (refs_backend_type && +write_one_config ("core.refsBackendType" , + refs_backend_type,NULL ) <0 ) +die ("unable to write backend parameter to config file" ); } static void write_refspec_config(const char *src_ref_prefix,builtin/submodule--helper.c +0
-0
builtin/submodule--helper.c static int clone_submodule(const char *path, const char *gitdir, const char *url · line 146
Quoted 8 lines
builtin/submodule--helper.c +4
-1@@ -140,7 +140,10 @@ 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 ); - +if (refs_backend_type && *refs_backend_type) { +argv_array_push (&cp.args,"--refs-backend-type" ); +argv_array_push (&cp.args, refs_backend_type); + }
This could be shortened to
argv_array_pushf(&cp.args, "--refs-backend-type=%s", refs_backend_type);
Quoted 8 lines
builtin/submodule--helper.c +0
-0argv_array_push (&cp.args, url);argv_array_push (&cp.args, path);cache.h +0
-0
cache.h enum object_creation_mode { · line 700
Quoted 6 lines
cache.h +1
-0@@ -695,6 +695,7 @@ enum object_creation_mode {extern enum object_creation_mode object_creation_mode;extern char *notes_ref_name; +extern const char *refs_backend_type;extern const char *refs_backend_type;
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 14/16] refs: allow ref backend to be set for clone
Quoted 9 lines · Michael Haggerty
builtin/submodule--helper.c static int clone_submodule(const char *path, const char *gitdir, const char *url · line 146
Quoted 4 lines · David Turner
builtin/submodule--helper.c +4
-0+if (refs_backend_type && *refs_backend_type) { +argv_array_push (&cp.args,"--refs-backend-type" ); +argv_array_push (&cp.args, refs_backend_type); + }This could be shortened to
argv_array_pushf(&cp.args, "--refs-backend-type=%s", refs_backend_type);
Or, without interpolation:
argv_array_pushl(&cp.args, "--refs-backend-type", refs_backend_type, NULL);
PATCH 15/16 refs: add LMDB refs 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 --refs-backend-type to git init. If those tests are changed to use the update-ref machinery or test-refs-lmdb-backend (or, in the case of packed-refs, corrupt refs, and dumb fetch tests, are skipped), the only remaining failing tests are the git-new-workdir tests and the gitweb tests.
--- .gitignore | 1 + Documentation/config.txt | 7 + Documentation/git-clone.txt | 2 +- Documentation/git-init.txt | 3 +- Documentation/technical/refs-lmdb-backend.txt | 50 + Documentation/technical/repository-version.txt | 5 + Makefile | 12 + builtin/init-db.c | 10 +- config.c | 20 +- configure.ac | 33 + contrib/workdir/git-new-workdir | 3 + refs.h | 1 + refs/lmdb-backend.c | 2054 ++++++++++++++++++++++++ setup.c | 22 +- test-refs-lmdb-backend.c | 68 + 15 files changed, 2279 insertions(+), 12 deletions(-) create mode 100644 Documentation/technical/refs-lmdb-backend.txt create mode 100644 refs/lmdb-backend.c create mode 100644 test-refs-lmdb-backend.c
.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
@@ -496,6 +496,13 @@ core.repositoryFormatVersion:: Internal variable identifying the repository format and layout version. +core.refsBackendType:: + Type of refs backend. Default is to use the original files + based backend. Set to 'lmdb' to activate the lmdb database + backend. If you use the lmdb backend, + core.repositoryFormatVersion must be set to 1, and + extensions.refBackend must be set to 'lmdb'. + core.sharedRepository:: When 'group' (or 'true'), the repository is made shareable between several users in a group (making sure all the files and objects are
Documentation/git-clone.txt +1 -1
@@ -224,7 +224,7 @@ objects from the source repository into a pack in the cloned repository. --refs-backend-type=<name>:: Type of refs backend. Default is to use the original files based - backend. + backend. Set to "lmdb" to activate the lmdb database backend. <repository>:: The (possibly remote) repository to clone from. See the
Documentation/git-init.txt +2 -1
@@ -115,7 +115,8 @@ does not exist, it will be created. --refs-backend-type=<name>:: Type of refs backend. Default is to use the original "files" backend, -which stores ref data in files in .git/refs and .git/packed-refs. +which stores ref data in files in .git/refs and .git/packed-refs. Set +to "lmdb" to activate the lmdb database backend. TEMPLATE DIRECTORY ------------------
Documentation/technical/refs-lmdb-backend.txt new file +50 -0
50 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`). + +`refBackend` +~~~~~~~~~~~~ +This extension allows the user of alternate ref backends. The only +defined value is `lmdb`.
Makefile +12 -0
@@ -1037,6 +1037,17 @@ ifdef USE_LIBPCRE EXTLIBS += -lpcre endif +ifdef USE_LIBLMDB + BASIC_CFLAGS += -DUSE_LIBLMDB + ifdef LIBLMDBDIR + BASIC_CFLAGS += -I$(LIBLMDBDIR)/include + EXTLIBS += -L$(LIBLMDBDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBLMDBDIR)/$(lib) + endif + EXTLIBS += -llmdb + LIB_OBJS += refs/lmdb-backend.o + TEST_PROGRAMS_NEED_X += test-refs-lmdb-backend +endif + ifdef HAVE_ALLOCA_H BASIC_CFLAGS += -DHAVE_ALLOCA_H endif
@@ -2124,6 +2135,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_CURL=\''$(subst ','\'',$(subst ','\'',$(NO_CURL)))'\' >>$@+ @echo NO_EXPAT=\''$(subst ','\'',$(subst ','\'',$(NO_EXPAT)))'\' >>$@+ @echo USE_LIBPCRE=\''$(subst ','\'',$(subst ','\'',$(USE_LIBPCRE)))'\' >>$@+ + @echo USE_LIBLMDB=\''$(subst ','\'',$(subst ','\'',$(USE_LIBLMDB)))'\' >>$@+ @echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+ @echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+ @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+
builtin/init-db.c +9 -1
@@ -179,6 +179,7 @@ static int create_default_files(const char *template_path)int reinit;int filemode;struct strbuf err =STRBUF_INIT ; +int repo_version =0 ;/* Just look for `init.templatedir` */ git_config (git_init_db_config,NULL );
@@ -209,7 +210,14 @@ static int create_default_files(const char *template_path)git_config_set ("core.refsBackendType" , refs_backend_type); config_data.refs_backend_type = refs_backend_type; config_data.refs_base =get_git_dir (); +#ifdef USE_LIBLMDB +register_refs_backend (&refs_be_lmdb); +#endif set_refs_backend (refs_backend_type, &config_data); +if (!strcmp (refs_backend_type,"lmdb" )) { +git_config_set ("extensions.refbackend" ,"lmdb" ); + repo_version =1 ; + } }if (refs_init_db (&err, shared_repository))
@@ -229,7 +237,7 @@ static int create_default_files(const char *template_path)/* This forces creation of new config file */ xsnprintf (repo_version_string,sizeof (repo_version_string), -"%d" ,GIT_REPO_VERSION ); +"%d" , repo_version);git_config_set ("core.repositoryformatversion" , repo_version_string);/* Check filemode trustability */
config.c +17 -3
@@ -1222,9 +1222,23 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) refdb_data.refs_base =xstrdup (dirname (repo_config_copy));free (repo_config_copy); -if (refdb_data.refs_backend_type && -strcmp (refdb_data.refs_backend_type,"files" )) { -die ("Unexpected backend %s" , refdb_data.refs_backend_type); +if (!refdb_data.refs_backend_type) + refdb_data.refs_backend_type ="" ; + +if ((!*refdb_data.refs_backend_type) || + (!strcmp (refdb_data.refs_backend_type,"files" ))) { +/* default backend, nothing to do */ + }else if (!strcmp (refdb_data.refs_backend_type,"lmdb" )) { + +#ifdef USE_LIBLMDB + refs_backend_type = refdb_data.refs_backend_type; +register_refs_backend (&refs_be_lmdb); +set_refs_backend (refs_backend_type, &refdb_data); +#else +die ("Git was not built with USE_LIBLMDB, so the db refs backend is not available" ); +#endif + }else { +die ("Unknown ref backend type '%s'" , refdb_data.refs_backend_type); } ret +=git_config_from_file (fn, repo_config, data);
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\"" + +test "$(git config core.refsbackendtype)" = "lmdb" && die "git-new-workdir is incompatible with the refs lmdb backend" + case "$git_dir" in .git) git_dir="$orig_git/.git"
refs.h +1 -0
@@ -520,6 +520,7 @@ struct refdb_config_data {int refdb_config (const char *var,const char *value,void *ptr);struct ref_be ; +extern struct ref_be refs_be_lmdb;int set_refs_backend (const char *name,void *data);void register_refs_backend (struct ref_be *be);
refs/lmdb-backend.c new file +2054 -0
2,054 lines not on this page · raw message
setup.c +16 -6
@@ -279,10 +279,11 @@ int refdb_config(const char *var, const char *value, void *ptr)* * - either an objects/ directory _or_ the proper * GIT_OBJECT_DIRECTORY environment variable -* - a refs/ directory -* - either a HEAD symlink or a HEAD file that is formatted as -* a proper "ref:", or a regular file HEAD that has a properly -* formatted sha1 object name. +* - a refdb/ directory or +* - a refs/ directory +* - either a HEAD symlink or a HEAD file that is formatted as +* a proper "ref:", or a regular file HEAD that has a properly +* formatted sha1 object name. */ int is_git_directory (const char *suspect) {
@@ -313,8 +314,13 @@ int is_git_directory(const char *suspect)strbuf_setlen (&path, len);strbuf_addstr (&path,"/refs" ); -if (access (path.buf,X_OK )) - goto done; + +if (access (path.buf,X_OK )) { +strbuf_setlen (&path, len); +strbuf_addstr (&path,"/refdb" ); +if (access (path.buf,X_OK )) + goto done; + } ret =1 ; done:
@@ -383,6 +389,10 @@ static int check_repo_format(const char *var, const char *value, void *cb) ; else if (!strcmp(ext, "preciousobjects")) repository_format_precious_objects =git_config_bool (var, value); +#ifdef USE_LIBLMDB + else if (!strcmp(ext, "refbackend") && !strcmp(value, "lmdb")) + ; +#endif else string_list_append(&unknown_extensions, ext); }
test-refs-lmdb-backend.c new file +68 -0
68 lines not on this page · raw message
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Re: [PATCH 15/16] refs: add LMDB refs backend
refs/lmdb-backend.c line 514
Quoted 3 lines · David Turner
refs/lmdb-backend.c new file +3
-0+while (!mdb_ret) { +if (starts_with (key.mv_data, refname) && + ((char *)key.mv_data)[refname_len -2 ] =='/' ) {
ERROR: "(foo*)" should be "(foo *)"
#877: FILE: refs/lmdb-backend.c:514:
+ ((char*)key.mv_data)[refname_len - 2] == '/') {
Quoted 7 lines
refs/lmdb-backend.c new file +7
-0+static int show_one_reflog_ent (struct strbuf *sb,each_reflog_ent_fn fn,void *cb_data) +{ +unsigned char osha1[20 ], nsha1[20 ]; +char *email_end, *message; +unsigned long timestamp; +int tz; +
refs/lmdb-backend.c line 669
Quoted 10 lines
refs/lmdb-backend.c new file +10
-0+/* old (raw) new (raw) name <email> SP time TAB msg LF */ +if (sb->len <41 || sb->buf[sb->len -1 ] !='\n' || + !(email_end =strchr (sb->buf +40 ,'>' )) || + email_end[1 ] !=' ' || + !(timestamp =strtoul (email_end +2 , &message,10 )) || + !message || message[0 ] !=' ' || + (message[1 ] !='+' && message[1 ] !='-' ) || + !isdigit (message[2 ]) || !isdigit (message[3 ]) || + !isdigit (message[4 ]) || !isdigit (message[5 ])) +return 0 ;/* corrupt? */
ERROR: do not use assignment in if condition
#1024: FILE: refs/lmdb-backend.c:661:
+ if (sb->len < 41 || sb->buf[sb->len - 1] != '\n' ||
Quoted 17 lines
refs/lmdb-backend.c new file +17
-0+static char *check_ref (MDB_txn *txn,const char *refname, +const unsigned char *old_sha1, +unsigned char *resolved_sha1,int flags, +int *type_p) +{ +int mustexist = (old_sha1 && !is_null_sha1 (old_sha1)); +int resolve_flags =0 ; +int type; +char *resolved_refname; + +if (mustexist) + resolve_flags |=RESOLVE_REF_READING ; +if (flags &REF_DELETING ) { + resolve_flags |=RESOLVE_REF_ALLOW_BAD_NAME ; +if (flags &REF_NODEREF ) + resolve_flags |=RESOLVE_REF_NO_RECURSE ; + }
refs/lmdb-backend.c line 815
Quoted 10 lines
refs/lmdb-backend.c new file +10
-0+ +/* +* The first time we resolve the refname, we're just trying to +* see if there is any ref at all by this name, even if it is +* a broken symref. +*/ + refname =resolve_ref_unsafe (refname, resolve_flags, + resolved_sha1, &type); +if (type_p) + *type_p = type;
WARNING: suspect code indent for conditional statements (8, 12) #1177: FILE: refs/lmdb-backend.c:814:
+ if (type_p)
+ *type_p = type;
Indeed, this line should be indented with two HTs.
refs/lmdb-backend.c line 891
Quoted 5 lines
refs/lmdb-backend.c new file +5
-0+while (!mdb_ret) { +if (key.mv_size < len) +break ; + +if (!starts_with (key.mv_data, log_path) || ((char *)key.mv_data)[len -1 ] !=0 )
ERROR: "(foo*)" should be "(foo *)"
#1254: FILE: refs/lmdb-backend.c:891:
+ if (!starts_with(key.mv_data, log_path) || ((char*)key.mv_data)[len - 1] != 0)
refs/lmdb-backend.c line 1003
Quoted 1 line
refs/lmdb-backend.c new file +1
-0+ if (strcmp (refname, orig_refname) &&
WARNING: space prohibited between function name and open parenthesis '(' #1366: FILE: refs/lmdb-backend.c:1003:
+ if (strcmp (refname, orig_refname) &&
This concludes my first pass, mechanical "lint" (with help from checkpatch.pl). I'll hopefully have time to do the more meaningful design and implementation review later.
Thanks.
Re: [PATCH 15/16] refs: add LMDB refs backend
Quoted 23 lines · Junio C Hamano
refs/lmdb-backend.c line 514
Quoted 3 lines · David Turner
refs/lmdb-backend.c new file +3
-0+while (!mdb_ret) { +if (starts_with (key.mv_data, refname) && + ((char *)key.mv_data)[refname_len -2 ] =='/' ) {ERROR: "(foo*)" should be "(foo *)"
#877: FILE:refs/lmdb-backend.c:514:
+ ((char*)key.mv_data)[refname_len - 2] == '/') {Quoted 13 lines
refs/lmdb-backend.c new file +13
-0+static int show_one_reflog_ent (struct strbuf *sb,each_reflog_ent_fn fn,void *cb_data) +{ +unsigned char osha1[20 ], nsha1[20 ]; +char *email_end, *message; +unsigned long timestamp; +int tz; + +/* old (raw) new (raw) name <email> SP time TAB msg LF */ + if (sb->len < 41 || sb->buf[sb->len - 1] != '\n' || + !(email_end = strchr(sb->buf + 40, '>')) || + email_end[1] != ' ' || + !(timestamp = strtoul(email_end + 2, &message, 10)) || + !message || message[0] != ' ' ||
Quoted 8 lines
refs/lmdb-backend.c line 669
Quoted 4 lines
refs/lmdb-backend.c new file +4
-0+ (message[1 ] !='+' && message[1 ] !='-' ) || + !isdigit (message[2 ]) || !isdigit (message[3 ]) || + !isdigit(message[4]) || !isdigit(message[5])) + return 0; /* corrupt? */ERROR: do not use assignment in if condition
#1024: FILE:refs/lmdb-backend.c:661:
+ if (sb->len< 41 || sb->buf[sb->len - 1] != '\n' ||
This code is based on code from files-backend.c. But I can change it anyway.
I'll also fix the rest, but wait for further review to post.
Re: [PATCH 15/16] refs: add LMDB refs backend
In reply to David TurnerDocumentation/config.txt core.repositoryFormatVersion:: · line 504
Quoted 12 lines · David Turner
Documentation/config.txt +6
-0@@ -496,6 +496,13 @@ core.repositoryFormatVersion:: Internal variable identifying the repository format and layout version. +core.refsBackendType:: + Type of refs backend. Default is to use the original files + based backend. Set to 'lmdb' to activate the lmdb database + backend. If you use the lmdb backend, + core.repositoryFormatVersion must be set to 1, and + extensions.refBackend must be set to 'lmdb'.
If core.refsBackendType names one backend and extensions.refBackend names another, which wins?
Thanks,
Jonathan
Re: [PATCH 15/16] refs: add LMDB refs backend
Quoted 9 lines · Jonathan Nieder
Quoted 7 lines · David Turner
Documentation/config.txt +0
-0@@ -496,6 +496,13 @@ core.repositoryFormatVersion:: Internal variable identifying the repository format andlayout
version.
Quoted 9 lines
Documentation/config.txt core.repositoryFormatVersion:: · line 504
Quoted 6 lines
Documentation/config.txt +6
-0+core.refsBackendType:: + Type of refs backend. Default is to use the original files + based backend. Set to 'lmdb' to activate the lmdb database + backend. If you use the lmdb backend, + core.repositoryFormatVersion must be set to 1, and + extensions.refBackend must be set to 'lmdb'.If core.refsBackendType names one backend and extensions.refBackend names another, which wins?
I've simplified this to just use extensions.refsBackendType for everything. Will re-roll once I finish getting comments on the rest of this series.
Re: [PATCH 15/16] refs: add LMDB refs backend
Quoted 13 lines · David Turner
Quoted 11 lines · Jonathan Nieder
Documentation/config.txt core.repositoryFormatVersion:: · line 504
Quoted 6 lines · David Turner
Documentation/config.txt +6
-0+core.refsBackendType:: + Type of refs backend. Default is to use the original files + based backend. Set to 'lmdb' to activate the lmdb database + backend. If you use the lmdb backend, + core.repositoryFormatVersion must be set to 1, and + extensions.refBackend must be set to 'lmdb'.If core.refsBackendType names one backend and extensions.refBackend names another, which wins?
Quoted 3 lines
I've simplified this to just use extensions.refsBackendType for everything. Will re-roll once I finish getting comments on the rest of this series.
Perfect. Thanks for the quick reply.
Jonathan
Re: [PATCH 15/16] refs: add LMDB refs backend
In reply to David TurnerQuoted 71 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:
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.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 --refs-backend-type to git init. If those tests are changed to use the update-ref machinery or test-refs-lmdb-backend (or, in the case of packed-refs, corrupt refs, and dumb fetch tests, are skipped), the only remaining failing tests are the git-new-workdir tests and the gitweb tests.Signed-off-by David Turner <[email protected]>--- .gitignore | 1 + Documentation/config.txt | 7 + Documentation/git-clone.txt | 2 +- Documentation/git-init.txt | 3 +- Documentation/technical/refs-lmdb-backend.txt | 50 + Documentation/technical/repository-version.txt | 5 + Makefile | 12 + builtin/init-db.c | 10 +- config.c | 20 +- configure.ac | 33 + contrib/workdir/git-new-workdir | 3 + refs.h | 1 + refs/lmdb-backend.c | 2054 ++++++++++++++++++++++++ setup.c | 22 +- test-refs-lmdb-backend.c | 68 + 15 files changed, 2279 insertions(+), 12 deletions(-) create mode 100644 Documentation/technical/refs-lmdb-backend.txt create mode 100644 refs/lmdb-backend.c create mode 100644 test-refs-lmdb-backend.c.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 core.repositoryFormatVersion:: · line 504
Quoted 9 lines
Documentation/config.txt +6
-0@@ -496,6 +496,13 @@ core.repositoryFormatVersion:: Internal variable identifying the repository format and layout version. +core.refsBackendType:: + Type of refs backend. Default is to use the original files + based backend. Set to 'lmdb' to activate the lmdb database + backend. If you use the lmdb backend, + core.repositoryFormatVersion must be set to 1, and + extensions.refBackend must be set to 'lmdb'.
This phrasing makes it sound like I can go into an existing repository, change this repository setting, and *presto* I will have a LMDB-backed repository. I suggest rewording it more along the lines of "this setting reflects the refs backend that is currently in use".
Also please see my earlier question about whether users should see the term "backend" or whether other terms would be easier to understand.
Quoted 41 lines
Documentation/config.txt +1
-0+ core.sharedRepository:: When 'group' (or 'true'), the repository is made shareable between several users in a group (making sure all the files and objects areDocumentation/git-clone.txt +1
-1@@ -224,7 +224,7 @@ objects from the source repository into a pack in the cloned repository. --refs-backend-type=<name>:: Type of refs backend. Default is to use the original files based - backend. + backend. Set to "lmdb" to activate the lmdb database backend. <repository>:: The (possibly remote) repository to clone from. See theDocumentation/git-init.txt +2
-1@@ -115,7 +115,8 @@ does not exist, it will be created. --refs-backend-type=<name>:: Type of refs backend. Default is to use the original "files" backend, -which stores ref data in files in .git/refs and .git/packed-refs. +which stores ref data in files in .git/refs and .git/packed-refs. Set +to "lmdb" to activate the lmdb database backend. TEMPLATE DIRECTORY ------------------Documentation/technical/refs-lmdb-backend.txt new file +4
-0@@ -0,0 +1,50 @@ +Notes on the LMDB refs backend +============================== + +Design:
Documentation/technical/refs-lmdb-backend.txt line 14
Quoted 10 lines
Documentation/technical/refs-lmdb-backend.txt new file +10
-0+------ + +Refs and reflogs are stored in a lmdb database in .git/refdb. All +keys and values are \0-terminated. + +Keys for refs are the name of the ref (e.g. refs/heads/master). +Values are the value of the ref, in hex +(e.g. 61f23eb0f81357c19fa91e2b8c6f3906c3a8f9b0). + +All per-worktree refs (refs/bisect/* and HEAD) are store using
s/store/stored/
Documentation/technical/refs-lmdb-backend.txt line 23
Quoted 9 lines
Documentation/technical/refs-lmdb-backend.txt new file +9
-0+the traditional files-based backend. + +Reflogs are stored as a series of database entries. + +For non-empty reflogs, there is one entry per logged ref +update. The key format is logs/[refname]\0[timestamp]. The timestamp +is a 64-bit unsigned integer number of nanoseconds since 1/1/1970. +This means that reflog entries are chronologically ordered. Because +LMDB is a btree database, we can efficiently iterate over these keys.
Is there a guarantee that the reflog entries for all references updated in a single transaction have the same timestamp? Is there a guarantee that updates that happened in *different* transactions have different timestamps? These might be useful properties if they are easy to implement, because then one could deduce the scope of transactions from the reflog. But of course, they admittedly go beyond what the files backend offers and so are not a requirement.
Is the timestamp in ASCII base 10, binary format, or or something else?
Documentation/technical/refs-lmdb-backend.txt line 30
Quoted 6 lines
Documentation/technical/refs-lmdb-backend.txt new file +6
-0+For an empty reflog, there is a "header" entry to show that a reflog +exists. The header has the same format as an ordinary reflog, but with +a timeztamp of all zeros and an empty value. + +Reflog values are in the same format as the original files-based +reflog.
I assume that means that each entry has the same contents as one line from a reflog file. Does the value include a trailing LF? (From above I guess it does have a trailing NUL.)
Is the timestamp within the reflog entry guaranteed to agree with the timestamp in the key? This might be a convenient property for debugging.
Quoted 24 lines
Documentation/technical/refs-lmdb-backend.txt new file +19
-0+Weaknesses: +----------- + +The reflog format is somewhat inefficient: a binary format could store +reflog date/time information in somewhat less space. + +The rsync and file:// transports don't work yet, because they +don't use the refs API. + +git new-workdir is incompatible with the lmdb backend. Fortunately, +git new-workdir is deprecated, and worktrees work fine. + +LMDB locks the entire database for write. Any other writer waits +until the first writer is done before beginning. Readers do not wait +for writers, and writers do not wait for readers. The underlying +scheme is approximately MVCC; each reader's queries see the state of +the database as-of the time that the reader acquired its read lock. +This is not too far off from the files backend, which loads all refs +into memory when one is requested.Documentation/technical/repository-version.txt +0
-0
Documentation/technical/repository-version.txt for testing format-1 compatibility. · line 93
Quoted 8 lines
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`). + +`refBackend` +~~~~~~~~~~~~ +This extension allows the user of alternate ref backends. The only +defined value is `lmdb`.
s/user/use/
Quoted 43 lines
Makefile +12
-0@@ -1037,6 +1037,17 @@ ifdef USE_LIBPCRE EXTLIBS += -lpcre endif +ifdef USE_LIBLMDB + BASIC_CFLAGS += -DUSE_LIBLMDB + ifdef LIBLMDBDIR + BASIC_CFLAGS += -I$(LIBLMDBDIR)/include + EXTLIBS += -L$(LIBLMDBDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBLMDBDIR)/$(lib) + endif + EXTLIBS += -llmdb + LIB_OBJS += refs/lmdb-backend.o + TEST_PROGRAMS_NEED_X += test-refs-lmdb-backend +endif + ifdef HAVE_ALLOCA_H BASIC_CFLAGS += -DHAVE_ALLOCA_H endif@@ -2124,6 +2135,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_CURL=\''$(subst ','\'',$(subst ','\'',$(NO_CURL)))'\' >>$@+ @echo NO_EXPAT=\''$(subst ','\'',$(subst ','\'',$(NO_EXPAT)))'\' >>$@+ @echo USE_LIBPCRE=\''$(subst ','\'',$(subst ','\'',$(USE_LIBPCRE)))'\' >>$@+ + @echo USE_LIBLMDB=\''$(subst ','\'',$(subst ','\'',$(USE_LIBLMDB)))'\' >>$@+ @echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+ @echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+ @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+builtin/init-db.c +1
-0@@ -179,6 +179,7 @@ static int create_default_files(const char *template_path)int reinit;int filemode;struct strbuf err =STRBUF_INIT ; +int repo_version =0 ;/* Just look for `init.templatedir` */ git_config (git_init_db_config,NULL );
builtin/init-db.c static int create_default_files(const char *template_path) · line 217
Quoted 8 lines
builtin/init-db.c +4
-0@@ -209,7 +210,14 @@ static int create_default_files(const char *template_path)git_config_set ("core.refsBackendType" , refs_backend_type); config_data.refs_backend_type = refs_backend_type; config_data.refs_base =get_git_dir (); +#ifdef USE_LIBLMDB +register_refs_backend (&refs_be_lmdb); +#endif set_refs_backend (refs_backend_type, &config_data); +if (!strcmp (refs_backend_type,"lmdb" )) {
Wouldn't expressing this condition as
if (strcmp(refs_backend_type, "files"))
better reflect the reason that we might need to set "extensions.refBackend" and use repo_version "1"? (You might also need a "refs_backend_type &&" and/or a "*refs_backend_type &&" in there.)
Quoted 136 lines
builtin/init-db.c +4
-1+git_config_set ("extensions.refbackend" ,"lmdb" ); + repo_version =1 ; + } }if (refs_init_db (&err, shared_repository))@@ -229,7 +237,7 @@ static int create_default_files(const char *template_path)/* This forces creation of new config file */ xsnprintf (repo_version_string,sizeof (repo_version_string), -"%d" ,GIT_REPO_VERSION ); +"%d" , repo_version);git_config_set ("core.repositoryformatversion" , repo_version_string);/* Check filemode trustability */ config.c +17
-3@@ -1222,9 +1222,23 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config) refdb_data.refs_base =xstrdup (dirname (repo_config_copy));free (repo_config_copy); -if (refdb_data.refs_backend_type && -strcmp (refdb_data.refs_backend_type,"files" )) { -die ("Unexpected backend %s" , refdb_data.refs_backend_type); +if (!refdb_data.refs_backend_type) + refdb_data.refs_backend_type ="" ; + +if ((!*refdb_data.refs_backend_type) || + (!strcmp (refdb_data.refs_backend_type,"files" ))) { +/* default backend, nothing to do */ + }else if (!strcmp (refdb_data.refs_backend_type,"lmdb" )) { + +#ifdef USE_LIBLMDB + refs_backend_type = refdb_data.refs_backend_type; +register_refs_backend (&refs_be_lmdb); +set_refs_backend (refs_backend_type, &refdb_data); +#else +die ("Git was not built with USE_LIBLMDB, so the db refs backend is not available" ); +#endif + }else { +die ("Unknown ref backend type '%s'" , refdb_data.refs_backend_type); } ret +=git_config_from_file (fn, repo_config, data);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\"" + +test "$(git config core.refsbackendtype)" = "lmdb" && die "git-new-workdir is incompatible with the refs lmdb backend" + case "$git_dir" in .git) git_dir="$orig_git/.git"refs.h +1
-0@@ -520,6 +520,7 @@ struct refdb_config_data {int refdb_config (const char *var,const char *value,void *ptr);struct ref_be ; +extern struct ref_be refs_be_lmdb;int set_refs_backend (const char *name,void *data);void register_refs_backend (struct ref_be *be);refs/lmdb-backend.c +0
-0setup.c +0
-0@@ -279,10 +279,11 @@ int refdb_config(const char *var, const char *value, void *ptr)* * - either an objects/ directory _or_ the proper
setup.c int refdb_config(const char *var, const char *value, void *ptr) · line 286
Quoted 10 lines
setup.c +5
-4* GIT_OBJECT_DIRECTORY environment variable -* - a refs/ directory -* - either a HEAD symlink or a HEAD file that is formatted as -* a proper "ref:", or a regular file HEAD that has a properly -* formatted sha1 object name. +* - a refdb/ directory or +* - a refs/ directory +* - either a HEAD symlink or a HEAD file that is formatted as +* a proper "ref:", or a regular file HEAD that has a properly +* formatted sha1 object name.
The indentation looks wrong here. I think the requirement is
* - either a refdb/ directory or a refs/ directory
* - either a HEAD symlink or a HEAD file that is formatted as
* a proper "ref:", or a regular file HEAD that has a properly
* formatted sha1 object name.
though the test of the *contents* of HEAD is currently skipped for non-files backends.
Quoted 31 lines
setup.c +11
-2*/ int is_git_directory (const char *suspect) {@@ -313,8 +314,13 @@ int is_git_directory(const char *suspect)strbuf_setlen (&path, len);strbuf_addstr (&path,"/refs" ); -if (access (path.buf,X_OK )) - goto done; + +if (access (path.buf,X_OK )) { +strbuf_setlen (&path, len); +strbuf_addstr (&path,"/refdb" ); +if (access (path.buf,X_OK )) + goto done; + } ret =1 ; done:@@ -383,6 +389,10 @@ static int check_repo_format(const char *var, const char *value, void *cb) ; else if (!strcmp(ext, "preciousobjects")) repository_format_precious_objects =git_config_bool (var, value); +#ifdef USE_LIBLMDB + else if (!strcmp(ext, "refbackend") && !strcmp(value, "lmdb")) + ; +#endif else string_list_append(&unknown_extensions, ext); }
test-refs-lmdb-backend.c
Quoted 1 line
test-refs-lmdb-backend.c +0
-0
I haven't actually reviewed the LMDB-specific code, just the glue code.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 15/16] refs: add LMDB refs backend
Quoted 15 lines · Michael Haggerty
+core.refsBackendType::
Review
Quoted 5 lines
(quoted diff) +5
-0+ Type of refs backend. Default is to use the original files + based backend. Set to 'lmdb' to activate the lmdb database + backend. If you use the lmdb backend, + core.repositoryFormatVersion must be set to 1, and + extensions.refBackend must be set to 'lmdb'.This phrasing makes it sound like I can go into an existing repository,
change this repository setting, and *presto* I will have a LMDB -backed
repository. I suggest rewording it more along the lines of "this setting
reflects the refs backend that is currently in use".
Quoted 3 lines
Also please see my earlier question about whether users should see the
term "backend" or whether other terms would be easier to understand.
Adjusted, thanks.
Quoted 3 lines
Quoted 1 line
+All per-worktree refs (refs/bisect/* and HEAD) are store using
s/store/stored/
Fixed.
Quoted 14 lines
Quoted 11 lines
(quoted diff) +6
-0+the traditional files-based backend. + +Reflogs are stored as a series of database entries. + +For non-empty reflogs, there is one entry per logged ref +update. The key format is logs/[refname]\0[timestamp]. Thetimestamp
(quoted diff) +3
-0+is a 64-bit unsigned integer number of nanoseconds since 1/1/1970. +This means that reflog entries are chronologically ordered. Because +LMDB is a btree database, we can efficiently iterate over theseQuoted 1 line
keys.
Is there a guarantee that the reflog entries for all references
Quoted 8 lines
updated
in a single transaction have the same timestamp? Is there a guarantee that updates that happened in *different* transactions have different timestamps? These might be useful properties if they are easy to implement, because then one could deduce the scope of transactions from
the reflog. But of course, they admittedly go beyond what the files backend offers and so are not a requirement.
All of this stuff is handled outside of the storage backend, so I don't want to address it here. That is, this offers the same guarantees as the files-based backend (neither of those, I think).
Quoted 2 lines
Is the timestamp in ASCII base 10, binary format, or or something else?
I've added the words "network byte order" to make it clear that this is binary.
Quoted 5 lines
Quoted 5 lines
+For an empty reflog, there is a "header" entry to show that a reflog
+exists. The header has the same format as an ordinary reflog, but with
+a timeztamp of all zeros and an empty value.
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ +Reflog values are in the same format as the original files-based +reflog.I assume that means that each entry has the same contents as one line from a reflog file. Does the value include a trailing LF? (From above I
guess it does have a trailing NUL.)
Trailing LF, yes; will fix.
Quoted 3 lines
Is the timestamp within the reflog entry guaranteed to agree with the timestamp in the key? This might be a convenient property for debugging.
Yes; edited.
Makefile +0 -0
Quoted 64 lines
Quoted 51 lines
index 5bd68e0..77b96d9 100644
Makefile +11
-0@@ -1037,6 +1037,17 @@ ifdef USE_LIBPCRE EXTLIBS += -lpcre endif +ifdef USE_LIBLMDB + BASIC_CFLAGS += -DUSE_LIBLMDB + ifdef LIBLMDBDIR + BASIC_CFLAGS += -I$(LIBLMDBDIR)/include + EXTLIBS += -L$(LIBLMDBDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBLMDBDIR)/$(lib) + endif + EXTLIBS += -llmdb + LIB_OBJS += refs/lmdb-backend.o + TEST_PROGRAMS_NEED_X += test-refs-lmdb-backend +endif + ifdef HAVE_ALLOCA_H BASIC_CFLAGS += -DHAVE_ALLOCA_Hendif
Makefile +0
-0@@ -2124,6 +2135,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_CURL=\''$(subst ','\'',$(subst','\'',$(NO_CURL)))'\' >>$@+
@echo NO_EXPAT=\''$(subst ','\'',$(subst
','\'',$(NO_EXPAT)))'\' >>$@+
@echo USE_LIBPCRE=\''$(subst ','\'',$(subst
','\'',$(USE_LIBPCRE)))'\' >>$@+Makefile +1
-0+ @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)))'\' >>$@+builtin/init-db.c +1
-0@@ -179,6 +179,7 @@ static int create_default_files(const char *template_path)int reinit;int filemode;struct strbuf err =STRBUF_INIT ; +int repo_version =0 ;/* Just look for `init.templatedir` */ builtin/init-db.c +0
-0git_config (git_init_db_config,NULL );builtin/init-db.c static int create_default_files(const char · line 219
Quoted 10 lines
builtin/init-db.c +4
-0@@ -209,7 +210,14 @@ static int create_default_files(const char *template_path)git_config_set ("core.refsBackendType" , refs_backend_type); config_data.refs_backend_type = refs_backend_type; config_data.refs_base =get_git_dir (); +#ifdef USE_LIBLMDB +register_refs_backend (&refs_be_lmdb); +#endif set_refs_backend (refs_backend_type, &config_data); +if (!strcmp (refs_backend_type,"lmdb" )) {Wouldn't expressing this condition as
Quoted 4 lines
if (
strcmp(refs_backend_type, "files"))better reflect the reason that we might need to set "extensions.refBackend" and use
repo_version"1"?
Yeah.
Quoted 2 lines
(You might also need a
"refs_backend_type&&" and/or a "*refs_backend_type&&" in there.)
I've just decided to set this var to be "files" by default, to simplify a lot of this code.
Quoted 13 lines
Quoted 10 lines
builtin/init-db.c +4
-4* - either an objects/ directory _or_ the proper * GIT_OBJECT_DIRECTORY environment variable -* - a refs/ directory -* - either a HEAD symlink or a HEAD file that is formatted as -* a proper "ref:", or a regular file HEAD that has a properly -* formatted sha1 object name. +* - a refdb/ directory or +* - a refs/ directory +* - either a HEAD symlink or a HEAD file that is formatted as +* a proper "ref:", or a regular file HEAD that has a Quoted 2 lines
properly
+ * formatted sha1 object name.
Quoted 8 lines
The indentation looks wrong here. I think the requirement is
* - either a refdb/ directory or a refs/ directory
* - either a HEAD symlink or a HEAD file that is formatted as
* a proper "ref:", or a regular file HEAD that has a properly
* formatted sha1 object name.though the test of the *contents* of HEAD is currently skipped for non-files backends.
Yes.
Thanks for the review. Hopefully another version by EOD, but we'll see.
PATCH 16/16 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/test-lib.sh | 1 + 3 files changed, 1469 insertions(+) create mode 100755 t/t1460-refs-lmdb-backend.sh create mode 100755 t/t1470-refs-lmdb-backend-reflog.sh
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 --refs-backend-type=lmdb && + for name in A B C D E F + do + test_tick && + T=$(git write-tree) && + sha1=$(echo $name | git commit-tree $T) && + eval $name=$sha1 + done +' + +m=refs/heads/master +n_dir=refs/heads/gu +n=$n_dir/fixes + +test_expect_success \ + "create $m" \ + "git update-ref $m $A && + test $A"' = $(raw_ref '"$m"')' +test_expect_success \ + "create $m" \ + "git update-ref $m $B $A && + test $B"' = $(raw_ref '"$m"')' +test_expect_success "fail to delete $m with stale ref" ' + test_must_fail git update-ref -d $m $A && + test $B = "$(raw_ref $m)" +' +test_expect_success "delete $m" ' + git update-ref -d $m $B && + ! raw_ref $m +' +delete_ref $m + +test_expect_success "delete $m without oldvalue verification" " + git update-ref $m $A && + test $A = \$(raw_ref $m) && + git update-ref -d $m && + ! raw_ref $m +" +delete_ref $m + +test_expect_success \ + "fail to create $n" \ + "git update-ref $n_dir $A && + test_must_fail git update-ref $n $A >out 2>err" + +delete_ref $n_dir +rm -f out err + +test_expect_success \ + "create $m (by HEAD)" \ + "git update-ref HEAD $A && + test $A"' = $(raw_ref '"$m"')' +test_expect_success \ + "create $m (by HEAD)" \ + "git update-ref HEAD $B $A && + test $B"' = $(raw_ref '"$m"')' +test_expect_success "fail to delete $m (by HEAD) with stale ref" ' + test_must_fail git update-ref -d HEAD $A && + test $B = $(raw_ref '"$m"') +' +test_expect_success "delete $m (by HEAD)" ' + git update-ref -d HEAD $B && + ! raw_ref $m +' +delete_ref $m + +test_expect_success \ + "create $m (by HEAD)" \ + "git update-ref HEAD $A && + test $A"' = $(raw_ref '"$m"')' +test_expect_success \ + "pack refs" \ + "git pack-refs --all" +test_expect_success \ + "move $m (by HEAD)" \ + "git update-ref HEAD $B $A && + test $B"' = $(raw_ref '"$m"')' +test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" ' + git update-ref -d HEAD $B && + ! raw_ref $m +' +delete_ref $m + +OLD_HEAD=$(raw_ref HEAD) +test_expect_success "delete symref without dereference" ' + git update-ref --no-deref -d HEAD && + ! raw_ref HEAD +' +write_ref HEAD "$OLD_HEAD" + +test_expect_success "delete symref without dereference when the referred ref is packed" ' + echo foo >foo.c && + git add foo.c && + git commit -m foo && + git pack-refs --all && + git update-ref --no-deref -d HEAD && + ! raw_ref HEAD +' +write_ref HEAD "$OLD_HEAD" +delete_ref $m + +test_expect_success 'update-ref -d is not confused by self-reference' ' + git symbolic-ref refs/heads/self refs/heads/self && + test_when_finished "delete_ref refs/heads/self" && + test_must_fail git update-ref -d refs/heads/self +' + +test_expect_success 'update-ref --no-deref -d can delete self-reference' ' + git symbolic-ref refs/heads/self refs/heads/self && + test_when_finished "delete_ref refs/heads/self" && + git update-ref --no-deref -d refs/heads/self +' + +test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' ' + test-refs-lmdb-backend refs/heads/bad "" && + test_when_finished "delete_ref refs/heads/bad" && + git symbolic-ref refs/heads/ref-to-bad refs/heads/bad && + test_when_finished "delete_ref refs/heads/ref-to-bad" && + raw_ref refs/heads/ref-to-bad && + git update-ref --no-deref -d refs/heads/ref-to-bad && + ! raw_ref refs/heads/ref-to-bad +' + +test_expect_success '(not) create HEAD with old sha1' " + test_must_fail git update-ref HEAD $A $B +" +test_expect_success "(not) prior created .git/$m" " + ! raw_ref $m +" +delete_ref $m + +test_expect_success \ + "create HEAD" \ + "git update-ref HEAD $A" +test_expect_success '(not) change HEAD with wrong SHA1' " + test_must_fail git update-ref HEAD $B $Z +" +test_expect_success "(not) changed .git/$m" " + ! test $B"' = $(raw_ref '"$m"') +' + +: a repository with working tree always has reflog these days... +delete_all_reflogs +: | append_reflog $m +delete_ref $m + +test_expect_success \ + "create $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:30" \ + git update-ref HEAD '"$A"' -m "Initial Creation" && + test '"$A"' = $(raw_ref '"$m"')' +test_expect_success \ + "update $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:31" \ + git update-ref HEAD'" $B $A "'-m "Switch" && + test '"$B"' = $(raw_ref '"$m"')' +test_expect_success \ + "set $m (logged by touch)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:41" \ + git update-ref HEAD'" $A && + test $A"' = $(raw_ref '"$m"')' + +cat >expect <<EOF +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 +EOF +test_expect_success \ + "verifying $m's log" \ + "raw_reflog $m >actual && + test_cmp expect actual" +delete_ref $m +delete_all_reflogs +: | append_reflog $m +rm -f actual expect + +test_expect_success \ + 'enable core.logAllRefUpdates' \ + 'git config core.logAllRefUpdates true && + test true = $(git config --bool --get core.logAllRefUpdates)' + +test_expect_success \ + "create $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:32" \ + git update-ref HEAD'" $A "'-m "Initial Creation" && + test '"$A"' = $(raw_ref '"$m"')' +test_expect_success \ + "update $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:33" \ + git update-ref HEAD'" $B $A "'-m "Switch" && + test '"$B"' = $(raw_ref '"$m"')' +test_expect_success \ + "set $m (logged by config)" \ + 'GIT_COMMITTER_DATE="2005-05-26 23:43" \ + git update-ref HEAD '"$A && + test $A"' = $(raw_ref '"$m"')' + +cat >expect <<EOF +$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch +$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000 +EOF +test_expect_success \ + "verifying $m's log" \ + 'raw_reflog $m >actual && + test_cmp expect actual' +delete_ref $m +rm -f expect + +git update-ref $m $D +git reflog expire --expire=all $m + +append_reflog $m <<EOF +0000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500 +$C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500 +$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500 +$F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500 +$Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500 +EOF + +ed="Thu, 26 May 2005 18:32:00 -0500" +gd="Thu, 26 May 2005 18:33:00 -0500" +ld="Thu, 26 May 2005 18:43:00 -0500" +test_expect_success \ + 'Query "master@{May 25 2005}" (before history)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 25 2005}" >o 2>e && + test '"$C"' = $(cat o) && + test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"' +test_expect_success \ + "Query master@{2005-05-25} (before history)" \ + 'rm -f o e && + git rev-parse --verify master@{2005-05-25} >o 2>e && + test '"$C"' = $(cat o) && + echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e && + test '"$C"' = $(cat o) && + test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e && + test '"$C"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{May 26 2005 23:32:30}" (first non-creation change)' \ + 'rm -f o e && + git rev-parse --verify "master@{May 26 2005 23:32:30}" >o 2>e && + test '"$A"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e && + test '"$B"' = $(cat o) && + test "warning: Log for ref '"$m has gap after $gd"'." = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:38:00}" (middle of history)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e && + test '"$Z"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e && + test '"$E"' = $(cat o) && + test "" = "$(cat e)"' +test_expect_success \ + 'Query "master@{2005-05-28}" (past end of history)' \ + 'rm -f o e && + git rev-parse --verify "master@{2005-05-28}" >o 2>e && + test '"$D"' = $(cat o) && + test "warning: Log for ref '"$m unexpectedly ended on $ld"'." = "$(cat e)"' + + +git reflog expire --expire=all $m +delete_ref $m + +test_expect_success \ + 'creating initial files' \ + 'echo TEST >F && + git add F && + GIT_AUTHOR_DATE="2005-05-26 23:30" \ + GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a && + h_TEST=$(git rev-parse --verify HEAD) && + echo The other day this did not work. >M && + echo And then Bob told me how to fix it. >>M && + echo OTHER >F && + GIT_AUTHOR_DATE="2005-05-26 23:41" \ + GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a && + h_OTHER=$(git rev-parse --verify HEAD) && + GIT_AUTHOR_DATE="2005-05-26 23:44" \ + GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend && + h_FIXED=$(git rev-parse --verify HEAD) && + echo Merged initial commit and a later commit. >M && + echo $h_TEST >.git/MERGE_HEAD && + GIT_AUTHOR_DATE="2005-05-26 23:45" \ + GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M && + h_MERGED=$(git rev-parse --verify HEAD) && + rm -f M' + +cat >expect <<EOF +$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add +$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work. +$h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work. +$h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit. +EOF +test_expect_success \ + 'git commit logged updates' \ + "raw_reflog $m >actual && + test_cmp expect actual" +unset h_TEST h_OTHER h_FIXED h_MERGED + +test_expect_success \ + 'git cat-file blob master:F (expect OTHER)' \ + 'test OTHER = $(git cat-file blob master:F)' +test_expect_success \ + 'git cat-file blob master@{2005-05-26 23:30}:F (expect TEST)' \ + 'test TEST = $(git cat-file blob "master@{2005-05-26 23:30}:F")' +test_expect_success \ + 'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \ + 'test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")' + +a=refs/heads/a +b=refs/heads/b +c=refs/heads/c +E='""' +F='%s\0' +pws='path with space' + +test_expect_success 'stdin test setup' ' + echo "$pws" >"$pws" && + git add -- "$pws" && + git commit -m "$pws" +' + +test_expect_success '-z fails without --stdin' ' + test_must_fail git update-ref -z $m $m $m 2>err && + grep "usage: git update-ref" err +' + +test_expect_success 'stdin works with no input' ' + >stdin && + git update-ref --stdin <stdin && + git rev-parse --verify -q $m +' + +test_expect_success 'stdin fails on empty line' ' + echo "" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: empty command in input" err +' + +test_expect_success 'stdin fails on only whitespace' ' + echo " " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: whitespace before command: " err +' + +test_expect_success 'stdin fails on leading whitespace' ' + echo " create $a $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: whitespace before command: create $a $m" err +' + +test_expect_success 'stdin fails on unknown command' ' + echo "unknown $a" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: unknown command: unknown $a" err +' + +test_expect_success 'stdin fails on unbalanced quotes' ' + echo "create $a \"master" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: badly quoted argument: \\\"master" err +' + +test_expect_success 'stdin fails on invalid escape' ' + echo "create $a \"ma\zter\"" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: badly quoted argument: \\\"ma\\\\zter\\\"" err +' + +test_expect_success 'stdin fails on junk after quoted argument' ' + echo "create \"$a\"master" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: unexpected character after quoted argument: \\\"$a\\\"master" err +' + +test_expect_success 'stdin fails create with no ref' ' + echo "create " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create: missing <ref>" err +' + +test_expect_success 'stdin fails create with no new value' ' + echo "create $a" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $a: missing <newvalue>" err +' + +test_expect_success 'stdin fails create with too many arguments' ' + echo "create $a $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $a: extra input: $m" err +' + +test_expect_success 'stdin fails update with no ref' ' + echo "update " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update: missing <ref>" err +' + +test_expect_success 'stdin fails update with no new value' ' + echo "update $a" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update $a: missing <newvalue>" err +' + +test_expect_success 'stdin fails update with too many arguments' ' + echo "update $a $m $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update $a: extra input: $m" err +' + +test_expect_success 'stdin fails delete with no ref' ' + echo "delete " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: delete: missing <ref>" err +' + +test_expect_success 'stdin fails delete with too many arguments' ' + echo "delete $a $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: delete $a: extra input: $m" err +' + +test_expect_success 'stdin fails verify with too many arguments' ' + echo "verify $a $m $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: verify $a: extra input: $m" err +' + +test_expect_success 'stdin fails option with unknown name' ' + echo "option unknown" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: option unknown: unknown" err +' + +test_expect_success 'stdin fails with duplicate refs' ' + cat >stdin <<-EOF && + create $a $m + create $b $m + create $a $m + EOF + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err +' + +test_expect_success 'stdin create ref works' ' + echo "create $a $m" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin succeeds with quoted argument' ' + git update-ref -d $a && + echo "create $a \"$m\"" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin succeeds with escaped character' ' + git update-ref -d $a && + echo "create $a \"ma\\163ter\"" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin update ref creates with zero old value' ' + echo "update $b $m $Z" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual && + git update-ref -d $b +' + +test_expect_success 'stdin update ref creates with empty old value' ' + echo "update $b $m $E" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin create ref works with path with space to blob' ' + echo "create refs/blobs/pws \"$m:$pws\"" >stdin && + git update-ref --stdin <stdin && + git rev-parse "$m:$pws" >expect && + git rev-parse refs/blobs/pws >actual && + test_cmp expect actual && + git update-ref -d refs/blobs/pws +' + +test_expect_success 'stdin update ref fails with wrong old value' ' + echo "update $c $m $m~1" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin update ref fails with bad old value' ' + echo "update $c $m does-not-exist" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin create ref fails with bad new value' ' + echo "create $c does-not-exist" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $c: invalid <newvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin create ref fails with zero new value' ' + echo "create $c " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: create $c: zero <newvalue>" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin update ref works with right old value' ' + echo "update $b $m~1 $m" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete ref fails with wrong old value' ' + echo "delete $a $m~1" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$a'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete ref fails with zero old value' ' + echo "delete $a " >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: delete $a: zero <oldvalue>" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin update symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + cat >stdin <<-EOF && + option no-deref + update refs/TESTSYMREF $a $b + EOF + git update-ref --stdin <stdin && + git rev-parse refs/TESTSYMREF >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + cat >stdin <<-EOF && + option no-deref + delete refs/TESTSYMREF $b + EOF + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/TESTSYMREF && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete ref works with right old value' ' + echo "delete $b $m~1" >stdin && + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q $b +' + +test_expect_success 'stdin update/create/verify combination works' ' + cat >stdin <<-EOF && + update $a $m + create $b $m + verify $c + EOF + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin verify succeeds for correct value' ' + git rev-parse $m >expect && + echo "verify $m $m" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin verify succeeds for missing reference' ' + echo "verify refs/heads/missing $Z" >stdin && + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin verify treats no value as missing' ' + echo "verify refs/heads/missing" >stdin && + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin verify fails for wrong value' ' + git rev-parse $m >expect && + echo "verify $m $m~1" >stdin && + test_must_fail git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin verify fails for mistaken null value' ' + git rev-parse $m >expect && + echo "verify $m $Z" >stdin && + test_must_fail git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin verify fails for mistaken empty value' ' + M=$(git rev-parse $m) && + test_when_finished "git update-ref $m $M" && + git rev-parse $m >expect && + echo "verify $m" >stdin && + test_must_fail git update-ref --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin update refs works with identity updates' ' + cat >stdin <<-EOF && + update $a $m $m + update $b $m $m + update $c $Z $E + EOF + git update-ref --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin update refs fails with wrong old value' ' + git update-ref $c $m && + cat >stdin <<-EOF && + update $a $m $m + update $b $m $m + update $c '' + EOF + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + git rev-parse $c >actual && + test_cmp expect actual +' + +test_expect_success 'stdin delete refs works with packed and loose refs' ' + git pack-refs --all && + git update-ref $c $m~1 && + cat >stdin <<-EOF && + delete $a $m + update $b $Z $m + update $c $E $m~1 + EOF + git update-ref --stdin <stdin && + test_must_fail git rev-parse --verify -q $a && + test_must_fail git rev-parse --verify -q $b && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z works on empty input' ' + >stdin && + git update-ref -z --stdin <stdin && + git rev-parse --verify -q $m +' + +test_expect_success 'stdin -z fails on empty line' ' + echo "" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: whitespace before command: " err +' + +test_expect_success 'stdin -z fails on empty command' ' + printf $F "" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: empty command in input" err +' + +test_expect_success 'stdin -z fails on only whitespace' ' + printf $F " " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: whitespace before command: " err +' + +test_expect_success 'stdin -z fails on leading whitespace' ' + printf $F " create $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: whitespace before command: create $a" err +' + +test_expect_success 'stdin -z fails on unknown command' ' + printf $F "unknown $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: unknown $a" err +' + +test_expect_success 'stdin -z fails create with no ref' ' + printf $F "create " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create: missing <ref>" err +' + +test_expect_success 'stdin -z fails create with no new value' ' + printf $F "create $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create $a: unexpected end of input when reading <newvalue>" err +' + +test_expect_success 'stdin -z fails create with too many arguments' ' + printf $F "create $a" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails update with no ref' ' + printf $F "update " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update: missing <ref>" err +' + +test_expect_success 'stdin -z fails update with too few args' ' + printf $F "update $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z emits warning with empty new value' ' + git update-ref $a $m && + printf $F "update $a" "" "" >stdin && + git update-ref -z --stdin <stdin 2>err && + grep "warning: update $a: missing <newvalue>, treating as zero" err && + test_must_fail git rev-parse --verify -q $a +' + +test_expect_success 'stdin -z fails update with no new value' ' + printf $F "update $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $a: unexpected end of input when reading <newvalue>" err +' + +test_expect_success 'stdin -z fails update with no old value' ' + printf $F "update $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z fails update with too many arguments' ' + printf $F "update $m" "$m" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails delete with no ref' ' + printf $F "delete " >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: delete: missing <ref>" err +' + +test_expect_success 'stdin -z fails delete with no old value' ' + printf $F "delete $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z fails delete with too many arguments' ' + printf $F "delete $m" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails verify with too many arguments' ' + printf $F "verify $m" "$m" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: unknown command: $m" err +' + +test_expect_success 'stdin -z fails verify with no old value' ' + printf $F "verify $a" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err +' + +test_expect_success 'stdin -z fails option with unknown name' ' + printf $F "option unknown" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: option unknown: unknown" err +' + +test_expect_success 'stdin -z fails with duplicate refs' ' + printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: Multiple updates for ref '"'"'$a'"'"' not allowed." err +' + +test_expect_success 'stdin -z create ref works' ' + printf $F "create $a" "$m" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z update ref creates with zero old value' ' + printf $F "update $b" "$m" "$Z" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual && + git update-ref -d $b +' + +test_expect_success 'stdin -z update ref creates with empty old value' ' + printf $F "update $b" "$m" "" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z create ref works with path with space to blob' ' + printf $F "create refs/blobs/pws" "$m:$pws" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse "$m:$pws" >expect && + git rev-parse refs/blobs/pws >actual && + test_cmp expect actual && + git update-ref -d refs/blobs/pws +' + +test_expect_success 'stdin -z update ref fails with wrong old value' ' + printf $F "update $c" "$m" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z update ref fails with bad old value' ' + printf $F "update $c" "$m" "does-not-exist" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z create ref fails when ref exists' ' + git update-ref $c $m && + git rev-parse "$c" >expect && + printf $F "create $c" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + git rev-parse "$c" >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z create ref fails with bad new value' ' + git update-ref -d "$c" && + printf $F "create $c" "does-not-exist" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create $c: invalid <newvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z create ref fails with empty new value' ' + printf $F "create $c" "" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: create $c: missing <newvalue>" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z update ref works with right old value' ' + printf $F "update $b" "$m~1" "$m" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete ref fails with wrong old value' ' + printf $F "delete $a" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$a'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete ref fails with zero old value' ' + printf $F "delete $a" "$Z" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: delete $a: zero <oldvalue>" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z update symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + printf $F "option no-deref" "update refs/TESTSYMREF" "$a" "$b" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse refs/TESTSYMREF >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete symref works option no-deref' ' + git symbolic-ref refs/TESTSYMREF $b && + printf $F "option no-deref" "delete refs/TESTSYMREF" "$b" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/TESTSYMREF && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete ref works with right old value' ' + printf $F "delete $b" "$m~1" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q $b +' + +test_expect_success 'stdin -z update/create/verify combination works' ' + printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z verify succeeds for correct value' ' + git rev-parse $m >expect && + printf $F "verify $m" "$m" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z verify succeeds for missing reference' ' + printf $F "verify refs/heads/missing" "$Z" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin -z verify treats no value as missing' ' + printf $F "verify refs/heads/missing" "" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q refs/heads/missing +' + +test_expect_success 'stdin -z verify fails for wrong value' ' + git rev-parse $m >expect && + printf $F "verify $m" "$m~1" >stdin && + test_must_fail git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z verify fails for mistaken null value' ' + git rev-parse $m >expect && + printf $F "verify $m" "$Z" >stdin && + test_must_fail git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z verify fails for mistaken empty value' ' + M=$(git rev-parse $m) && + test_when_finished "git update-ref $m $M" && + git rev-parse $m >expect && + printf $F "verify $m" "" >stdin && + test_must_fail git update-ref -z --stdin <stdin && + git rev-parse $m >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z update refs works with identity updates' ' + printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin && + git update-ref -z --stdin <stdin && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin -z update refs fails with wrong old value' ' + git update-ref $c $m && + printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin && + test_must_fail git update-ref -z --stdin <stdin 2>err && + grep "fatal: cannot lock the ref '"'"'$c'"'"'" err && + git rev-parse $m >expect && + git rev-parse $a >actual && + test_cmp expect actual && + git rev-parse $b >actual && + test_cmp expect actual && + git rev-parse $c >actual && + test_cmp expect actual +' + +test_expect_success 'stdin -z delete refs works with packed and loose refs' ' + git pack-refs --all && + git update-ref $c $m~1 && + printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin && + git update-ref -z --stdin <stdin && + test_must_fail git rev-parse --verify -q $a && + test_must_fail git rev-parse --verify -q $b && + test_must_fail git rev-parse --verify -q $c +' + +test_done
t/t1470-refs-lmdb-backend-reflog.sh new file +359 -0
359 lines not on this page · raw message
t/test-lib.sh +1 -0
@@ -958,6 +958,7 @@ test -z "$NO_PERL" && test_set_prereq PERL test -z "$NO_PYTHON" && test_set_prereq PYTHON test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE test -z "$NO_GETTEXT" && test_set_prereq GETTEXT +test -n "$USE_LIBLMDB" && test_set_prereq LMDB # Can we rely on git's output in the C locale? if test -n "$GETTEXT_POISON"
Signature · 1 line
2.4.2.749.g0ed01d8-twtrsrc
Quoted 2 lines · David Turner
I'm starting the patchset numbering over from 1 here, because this version of the patchset is a subset of the last version.
I'm going out of town on 12/24 and will return 1/5. I won't be reading this email address. When I get back, I'll read any reviews.