Improve "refs" module encapsulation
cover Improve "refs" module encapsulation
Add functions to the reference API to
* Delete a bunch of references at once, but *without* failing the whole transaction if one of the deletions fails. This functionality is used by git remote remove and git remote prune.
* Create initial references during git clone. (During clone, references are written directly to the packed-refs file without any locking.)
Also move the remaining "refs" function declarations from cache.h to refs.h.
This improves the encapsulation of the refs module. Especially, it means that code outside of the refs module should no longer need to care about the distinction between loose and packed references.
These patches are also available from my GitHub account [1] as branch "init-delete-refs-api".
[1] https://github.com/mhagger/git
Michael Haggerty (13):
delete_ref(): move declaration to refs.h remove_branches(): remove temporary
delete_ref(): handle special case more explicitly delete_refs(): new function for the refs API delete_refs(): improve error message
delete_refs(): convert error message to lower case prune_remote(): use delete_refs()
repack_without_refs(): make function private initial_ref_transaction_commit(): function for initial ref creation
refs: remove some functions from the module's public interface initial_ref_transaction_commit(): check for duplicate refs initial_ref_transaction_commit(): check for ref D/F conflicts
refs: move the remaining ref module declarations to refs.h
archive.c | 1 + builtin/blame.c | 1 + builtin/clone.c | 19 ++++- builtin/fast-export.c | 1 + builtin/fmt-merge-msg.c | 1 + builtin/init-db.c | 1 + builtin/log.c | 1 + builtin/remote.c | 33 +------- cache.h | 68 ---------------- refs.c | 167 +++++++++++++++++++++++++++++++++++--- refs.h | 210 +++++++++++++++++++++++++++++++----------------- remote-testsvn.c | 1 + 12 files changed, 316 insertions(+), 188 deletions(-)
Signature · 1 line
2.1.4
PATCH 1/13 delete_ref(): move declaration to refs.h
Also
* Add a docstring
* Rename the second parameter to "old_sha1", to be consistent with the convention used elsewhere in the refs module
--- cache.h | 2 -- refs.c | 5 +++-- refs.h | 9 +++++++++ 3 files changed, 12 insertions(+), 4 deletions(-)
cache.h +0 -2
@@ -585,8 +585,6 @@ extern void update_index_if_able(struct index_state *, struct lock_file *);extern int hold_locked_index (struct lock_file *,int );extern void set_alternate_index_output (const char *); -extern int delete_ref (const char *,const unsigned char *sha1,unsigned int flags); -/* Environment bits from configuration mechanism */ extern int trust_executable_bit;extern int trust_ctime;
refs.c +3 -2
@@ -2789,7 +2789,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)return 0 ; } -int delete_ref (const char *refname,const unsigned char *sha1,unsigned int flags) +int delete_ref (const char *refname,const unsigned char *old_sha1, +unsigned int flags) {struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ;
@@ -2797,7 +2798,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flag transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_delete (transaction, refname, - (sha1 && !is_null_sha1 (sha1)) ? sha1 :NULL , + (old_sha1 && !is_null_sha1 (old_sha1)) ? old_sha1 :NULL , flags,NULL , &err) ||ref_transaction_commit (transaction, &err)) {error ("%s" , err.buf);
refs.h +9 -0
@@ -202,6 +202,15 @@ extern int read_ref_at(const char *refname, unsigned int flags,/** Check if a particular reflog exists */ extern int reflog_exists (const char *refname); +/* +* Delete the specified reference. If old_sha1 is non-NULL and not +* NULL_SHA1, then verify that the current value of the reference is +* old_sha1 before deleting it. flags is passed to +* ref_transaction_delete(). +*/ +extern int delete_ref (const char *refname,const unsigned char *old_sha1, +unsigned int flags); +/** Delete a reflog */ extern int delete_reflog (const char *refname);
Signature · 1 line
2.1.4
Re: [PATCH 01/13] delete_ref(): move declaration to refs.h
Quoted 55 lines · Michael Haggerty
Also
* Add a docstring
* Rename the second parameter to "
old_sha1", to be consistent with the convention used elsewhere in the refs moduleSigned-off-by Michael Haggerty <[email protected]>--- cache.h | 2 -- refs.c | 5 +++-- refs.h | 9 +++++++++ 3 files changed, 12 insertions(+), 4 deletions(-)cache.h +0
-2@@ -585,8 +585,6 @@ extern void update_index_if_able(struct index_state *, struct lock_file *);extern int hold_locked_index (struct lock_file *,int );extern void set_alternate_index_output (const char *); -extern int delete_ref (const char *,const unsigned char *sha1,unsigned int flags); -/* Environment bits from configuration mechanism */ extern int trust_executable_bit;extern int trust_ctime;refs.c +3
-2@@ -2789,7 +2789,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)return 0 ; } -int delete_ref (const char *refname,const unsigned char *sha1,unsigned int flags) +int delete_ref (const char *refname,const unsigned char *old_sha1, +unsigned int flags) {struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ;@@ -2797,7 +2798,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flag transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_delete (transaction, refname, - (sha1 && !is_null_sha1 (sha1)) ? sha1 :NULL , + (old_sha1 && !is_null_sha1 (old_sha1)) ? old_sha1 :NULL , flags,NULL , &err) ||ref_transaction_commit (transaction, &err)) {error ("%s" , err.buf);refs.h +0
-0
refs.h extern int read_ref_at(const char *refname, unsigned int flags, · line 208
Quoted 7 lines
refs.h +4
-0@@ -202,6 +202,15 @@ extern int read_ref_at(const char *refname, unsigned int flags,/** Check if a particular reflog exists */ extern int reflog_exists (const char *refname); +/* +* Delete the specified reference. If old_sha1 is non-NULL and not +* NULL_SHA1, then verify that the current value of the reference is +* old_sha1 before deleting it.
And here I wondered what the distinction between NULL and non-NULL, but NULL_SHA1
is and digging into the code, there is none. (As you can also see in this patch above with
(old_sha1 && !is_null_sha1(old_sha1)) ? old_sha1 : NULL,
but when digging deeper, the !is_null_sha1(old_sha1) is an arbitrary limitation (i.e.ref_transaction_delete and ref_transaction_update don't differ between those two cases.)
The distinction comes in at lock_ref_sha1_basic, where I think we may want to get rid of
the is_null_sha1 check and depend on NULL/non-NULL as the difference for valid and invalid
sha1's?
That said, do we want to add another sentence to the doc here saying non-NULL and notNULL_SHA1 are treated the same or is it clear enough? With or without this question addressed:
Quoted 12 lines
flags is passed to
+ *ref_transaction_delete().
+ */refs.h +3
-1+extern int delete_ref (const char *refname,const unsigned char *old_sha1, +unsigned int flags); +/** Delete a reflog */ extern int delete_reflog (const char *refname); --2.1.4
Re: [PATCH 01/13] delete_ref(): move declaration to refs.h
Quoted 17 lines · Stefan Beller
Quoted 5 lines · Michael Haggerty
[...]
+/*
+ * Delete the specified reference. Ifold_sha1is non-NULL and not
+ *NULL_SHA1, then verify that the current value of the reference is
+ *old_sha1before deleting it.And here I wondered what the distinction between NULL and non-NULL, but
NULL_SHA1
is and digging into the code, there is none. (As you can also see in this patch above with(old_sha1 && !is_null_sha1(old_sha1)) ? old_sha1 : NULL,but when digging deeper, the !is_null_sha1(old_sha1) is an arbitrary limitation (i.e.
ref_transaction_deleteandref_transaction_updatedon't differ between those two cases.)
Quoted 5 lines
The distinction comes in at
lock_ref_sha1_basic, where I think we may want to get rid of
theis_null_sha1check and depend on NULL/non-NULL as the difference for valid and invalid
sha1's?
I'm having a little trouble understanding your comment.
The convention for ref_transaction_update() and friends is that
* old_sha1 == NULL
We don't care whether the reference existed prior to the update, nor what its value was.
* *old_sha1 is NULL_SHA1
(by which I mean that old_sha1 points at 20 zero bytes; I hope that's clear even though NULL_SHA1 is not actually defined anywhere): The reference must *not* have existed prior to the update.
* old_sha1 has some other value
The reference must have had that value prior to the update.
lock_ref_sha1_basic() distinguishes between { NULL vs. NULL_SHA1 vs. other values } in the same way that ref_transaction_update() does.
The delete_ref() function doesn't follow the same convention. It treats NULL and NULL_SHA1 identically, as "don't care".
It probably makes sense to change delete_ref() use the same convention as ref_transaction_update(), but there are quite a few callers and I didn't have the energy to review them all as part of this patch series. So I left it unchanged and just documented the status quo better.
Quoted 5 lines
That said, do we want to add another sentence to the doc here saying non-NULL and not
NULL_SHA1are treated the same or is it clear enough? With or without this question addressed:Reviewed-by Stefan Beller <[email protected]>
In set notation,
"non-NULL" =
"non-NULL and not NULL_SHA1" ∪
"non-NULL and equal to NULL_SHA1"
The latter two are *not* treated the same, so I don't see how we can claim that "non-NULL" and "not NULL_SHA1" are treated the same. I must be misunderstanding you.
Would it help if I changed the comment to
Delete the specified reference. If old_sha1 is non-NULL and not NULL_SHA1, then verify that the current value of the reference is old_sha1 before deleting it. If old_sha1 is NULL or NULL_SHA1, delete the reference it it exists, regardless of its old value.
?
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 01/13] delete_ref(): move declaration to refs.h
Quoted 33 lines · Michael Haggerty
Quoted 17 lines · Stefan Beller
Quoted 5 lines · Michael Haggerty
[...]
+/*
+ * Delete the specified reference. Ifold_sha1is non-NULL and not
+ *NULL_SHA1, then verify that the current value of the reference is
+ *old_sha1before deleting it.And here I wondered what the distinction between NULL and non-NULL, but
NULL_SHA1
is and digging into the code, there is none. (As you can also see in this patch above with(old_sha1 && !is_null_sha1(old_sha1)) ? old_sha1 : NULL,but when digging deeper, the !is_null_sha1(old_sha1) is an arbitrary limitation (i.e.
ref_transaction_deleteandref_transaction_updatedon't differ between those two cases.)Quoted 5 lines
The distinction comes in at
lock_ref_sha1_basic, where I think we may want to get rid of
theis_null_sha1check and depend on NULL/non-NULL as the difference for valid and invalid
sha1's?I'm having a little trouble understanding your comment.
The convention for
ref_transaction_update()and friends is that*
old_sha1== NULLWe don't care whether the reference existed prior to the update, nor what its value was.
Quoted 6 lines
*
*old_sha1isNULL_SHA1(by which I mean that
old_sha1points at 20 zero bytes; I hope that's clear even thoughNULL_SHA1is not actually defined anywhere): The reference must *not* have existed prior to the update.
Ok that's what I was missing.
Quoted 32 lines
*
old_sha1has some other valueThe reference must have had that value prior to the update.
lock_ref_sha1_basic()distinguishes between { NULL vs.NULL_SHA1vs. other values } in the same way thatref_transaction_update()does.The
delete_ref()function doesn't follow the same convention. It treats NULL andNULL_SHA1identically, as "don't care".It probably makes sense to change
delete_ref()use the same convention asref_transaction_update(), but there are quite a few callers and I didn't have the energy to review them all as part of this patch series. So I left it unchanged and just documented the status quo better.Quoted 5 lines
That said, do we want to add another sentence to the doc here saying non-NULL and not
NULL_SHA1are treated the same or is it clear enough? With or without this question addressed:Reviewed-by Stefan Beller <[email protected]>In set notation,
"non-NULL" =
"non-NULL and notNULL_SHA1" ∪
"non-NULL and equal toNULL_SHA1"The latter two are *not* treated the same, so I don't see how we can claim that "non-NULL" and "not
NULL_SHA1" are treated the same. I must be misunderstanding you.
Quoted 8 lines
Would it help if I changed the comment to
Delete the specified reference. If
old_sha1is non-NULL and notNULL_SHA1, then verify that the current value of the reference isold_sha1before deleting it. Ifold_sha1is NULL orNULL_SHA1, delete the reference it it exists, regardless of its old value.?
This is very clear to me.
Quoted 7 lines
Michael
--
Michael Haggerty
[email protected]
Re: [PATCH 01/13] delete_ref(): move declaration to refs.h
Quoted 13 lines · Stefan Beller
Quoted 12 lines · Michael Haggerty
In set notation,
"non-NULL" =
"non-NULL and notNULL_SHA1" ∪
"non-NULL and equal toNULL_SHA1"The latter two are *not* treated the same, so I don't see how we can claim that "non-NULL" and "not
NULL_SHA1" are treated the same. I must be misunderstanding you.Would it help if I changed the comment to
Quoted 8 lines
Quoted 6 lines
Delete the specified reference. If
old_sha1is non-NULL and notNULL_SHA1, then verify that the current value of the reference isold_sha1before deleting it. Ifold_sha1is NULL orNULL_SHA1, delete the reference it it exists, regardless of its old value.?
This is very clear to me.
OK, I will make the change in v2 (with s/it it/if it/).
Michael
Signature · 2 lines
Michael Haggerty [email protected]
PATCH 2/13 remove_branches(): remove temporary
In reply to Michael Haggerty--- builtin/remote.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
builtin/remote.c +1 -2
@@ -756,8 +756,7 @@ static int remove_branches(struct string_list *branches)strbuf_release (&err);for (i =0 ; i < branches->nr; i++) { -struct string_list_item *item = branches->items + i; -const char *refname = item->string; +const char *refname = branches->items[i].string;if (delete_ref (refname,NULL ,0 )) result |=error (_ ("Could not remove branch %s" ), refname);
Signature · 1 line
2.1.4
Re: [PATCH 02/13] remove_branches(): remove temporary
Quoted 1 line · Michael Haggerty
Signed-off-by Michael Haggerty <[email protected]>
Quoted 21 lines
--- builtin/remote.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)builtin/remote.c +1
-3@@ -756,8 +756,7 @@ static int remove_branches(struct string_list *branches)strbuf_release (&err);for (i =0 ; i < branches->nr; i++) { -struct string_list_item *item = branches->items + i; -const char *refname = item->string; +const char *refname = branches->items[i].string;if (delete_ref (refname,NULL ,0 )) result |=error (_ ("Could not remove branch %s" ), refname); --2.1.4
PATCH 3/13 delete_ref(): handle special case more explicitly
In reply to Michael Haggertydelete_ref() uses a different convention for its old_sha1 parameter than, say, ref_transaction_delete(): NULL_SHA1 means not to check the old value. Make this fact a little bit clearer in the code by handling it in explicit, commented code rather than burying it in a conditional expression.
--- refs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
refs.c +5 -2
@@ -2795,10 +2795,13 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ; +/* Treat NULL_SHA1 as "don't care" */ +if (old_sha1 &&is_null_sha1 (old_sha1)) + old_sha1 =NULL ; + transaction =ref_transaction_begin (&err);if (!transaction || -ref_transaction_delete (transaction, refname, - (old_sha1 && !is_null_sha1 (old_sha1)) ? old_sha1 :NULL , +ref_transaction_delete (transaction, refname, old_sha1, flags,NULL , &err) ||ref_transaction_commit (transaction, &err)) {error ("%s" , err.buf);
Signature · 1 line
2.1.4
Re: [PATCH 03/13] delete_ref(): handle special case more explicitly
Quoted 16 lines · Michael Haggerty
delete_ref()uses a different convention for itsold_sha1parameter than, say,ref_transaction_delete():NULL_SHA1means not to check the old value. Make this fact a little bit clearer in the code by handling it in explicit, commented code rather than burying it in a conditional expression.Signed-off-by Michael Haggerty <[email protected]>--- refs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)refs.c +0
-0
refs.c int delete_ref(const char *refname, const unsigned char *old_sha1, · line 2798
Quoted 4 lines
refs.c +1
-0@@ -2795,10 +2795,13 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ; +/* Treat NULL_SHA1 as "don't care" */
and by "don't care" you mean we still care about getting it deleted, the part we don't care about is the particular sha1 (could be a bogus ref).
Quoted 14 lines
refs.c +4
-3+if (old_sha1 &&is_null_sha1 (old_sha1)) + old_sha1 =NULL ; + transaction =ref_transaction_begin (&err);if (!transaction || -ref_transaction_delete (transaction, refname, - (old_sha1 && !is_null_sha1 (old_sha1)) ? old_sha1 :NULL , +ref_transaction_delete (transaction, refname, old_sha1, flags,NULL , &err) ||ref_transaction_commit (transaction, &err)) {error ("%s" , err.buf); --2.1.4
Re: [PATCH 03/13] delete_ref(): handle special case more explicitly
Quoted 20 lines · Stefan Beller
Quoted 19 lines · Michael Haggerty
delete_ref()uses a different convention for itsold_sha1parameter than, say,ref_transaction_delete():NULL_SHA1means not to check the old value. Make this fact a little bit clearer in the code by handling it in explicit, commented code rather than burying it in a conditional expression.Signed-off-by Michael Haggerty <[email protected]>--- refs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)refs.c +0
-0@@ -2795,10 +2795,13 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ;
Quoted 4 lines
refs.c int delete_ref(const char *refname, const unsigned char *old_sha1, · line 2798
Quoted 1 line
refs.c +1
-0+/* Treat NULL_SHA1 as "don't care" */ and by "don't care" you mean we still care about getting it deleted, the part we don't care about is the particular sha1 (could be a bogus ref).
Correct. I will to change the comment to
/*
* Treat NULL_SHA1 and NULL alike, to mean "we don't care what
* the old value of the reference was (or even if it didn't
* exist)":
*/
to make that clearer.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
PATCH 4/13 delete_refs(): new function for the refs API
In reply to Michael HaggertyMove the function remove_branches() from builtin/remote.c to refs.c, rename it to delete_refs(), and make it public.
--- builtin/remote.c | 21 +-------------------- refs.c | 19 +++++++++++++++++++ refs.h | 7 +++++++ 3 files changed, 27 insertions(+), 20 deletions(-)
builtin/remote.c +1 -20
@@ -746,25 +746,6 @@ static int mv(int argc, const char **argv)return 0 ; } -static int remove_branches (struct string_list *branches) -{ -struct strbuf err =STRBUF_INIT ; -int i, result =0 ; - -if (repack_without_refs (branches, &err)) - result |=error ("%s" , err.buf); -strbuf_release (&err); - -for (i =0 ; i < branches->nr; i++) { -const char *refname = branches->items[i].string; - -if (delete_ref (refname,NULL ,0 )) - result |=error (_ ("Could not remove branch %s" ), refname); - } - -return result; -} -static int rm (int argc,const char **argv) {struct option options[] = {
@@ -821,7 +802,7 @@ static int rm(int argc, const char **argv)strbuf_release (&buf);if (!result) - result =remove_branches (&branches); + result =delete_refs (&branches);string_list_clear (&branches,0 );if (skipped.nr) {
refs.c +19 -0
@@ -2814,6 +2814,25 @@ int delete_ref(const char *refname, const unsigned char *old_sha1,return 0 ; } +int delete_refs (struct string_list *refnames) +{ +struct strbuf err =STRBUF_INIT ; +int i, result =0 ; + +if (repack_without_refs (refnames, &err)) + result |=error ("%s" , err.buf); +strbuf_release (&err); + +for (i =0 ; i < refnames->nr; i++) { +const char *refname = refnames->items[i].string; + +if (delete_ref (refname,NULL ,0 )) + result |=error (_ ("Could not remove branch %s" ), refname); + } + +return result; +} +/* * People using contrib's git-new-workdir have .git/logs/refs -> * /some/other/path/.git/logs/refs, and that may live on another device.
refs.h +7 -0
@@ -211,6 +211,13 @@ extern int reflog_exists(const char *refname);extern int delete_ref (const char *refname,const unsigned char *old_sha1,unsigned int flags); +/* +* Delete the specified references. If there are any problems, emit +* errors but attempt to keep going (i.e., the deletes are not done in +* an all-or-nothing transaction). +*/ +extern int delete_refs (struct string_list *refnames); +/** Delete a reflog */ extern int delete_reflog (const char *refname);
Signature · 1 line
2.1.4
PATCH 5/13 delete_refs(): improve error message
In reply to Michael HaggertyChange the error message from
Could not remove branch %s
to
Could not remove reference %s
This change makes sense even for the existing caller, which uses the function to delete remote-tracking branches.
--- refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
refs.c +1 -1
@@ -2827,7 +2827,7 @@ int delete_refs(struct string_list *refnames)const char *refname = refnames->items[i].string;if (delete_ref (refname,NULL ,0 )) - result |=error (_ ("Could not remove branch %s" ), refname); + result |=error (_ ("Could not remove reference %s" ), refname); }return result;
Signature · 1 line
2.1.4
Re: [PATCH 05/13] delete_refs(): improve error message
Quoted 10 lines · Michael Haggerty
Change the error message from
Could not remove branch %s
to
Could not remove reference %s
This change makes sense even for the existing caller, which uses the function to delete remote-tracking branches.
I am 80% convinced ;-)
The existing caller never used this for removing tags, so 'could not remove branch' was equally correct for it and was more specific than 'could not remove reference'. If you change it to 'could not remove that thing %s', it would still be correct for the existing caller; it would be even less specific for them, though ;-)
The new callers you will add in later patch of course cannot live with 'could not remove branch', so I think that this is an acceptable compromise we can live with. If somebody later wants to make the message more specific, they can add code that switches on the prefix of the ref when coming up with the error message (and use that code consistently in other error messages e.g. 'could not add reference').
Quoted 19 lines
Signed-off-by Michael Haggerty <[email protected]>--- refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)refs.c +1
-1@@ -2827,7 +2827,7 @@ int delete_refs(struct string_list *refnames)const char *refname = refnames->items[i].string;if (delete_ref (refname,NULL ,0 )) - result |=error (_ ("Could not remove branch %s" ), refname); + result |=error (_ ("Could not remove reference %s" ), refname); }return result;
PATCH 6/13 delete_refs(): convert error message to lower case
In reply to Michael HaggertyThis string is going to have to be re-internationalized anyway because of the previous commit. So while we're at it, we might as well convert it to lower case as per our usual practice.
--- refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
refs.c +1 -1
@@ -2827,7 +2827,7 @@ int delete_refs(struct string_list *refnames)const char *refname = refnames->items[i].string;if (delete_ref (refname,NULL ,0 )) - result |=error (_ ("Could not remove reference %s" ), refname); + result |=error (_ ("could not remove reference %s" ), refname); }return result;
Signature · 1 line
2.1.4
Re: [PATCH 06/13] delete_refs(): convert error message to lower case
Quoted 3 lines · Michael Haggerty
This string is going to have to be re-internationalized anyway because of the previous commit. So while we're at it, we might as well convert it to lower case as per our usual practice.
Although the previous patch and this are addressing two slightly different things,
we may want to squash this into the previous without dropping any of the commit message?
(It might make reviewing easier, I'd assume)
Quoted 22 lines
Signed-off-by Michael Haggerty <[email protected]>--- refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)refs.c +1
-2@@ -2827,7 +2827,7 @@ int delete_refs(struct string_list *refnames)const char *refname = refnames->items[i].string;if (delete_ref (refname,NULL ,0 )) - result |=error (_ ("Could not remove reference %s" ), refname); + result |=error (_ ("could not remove reference %s" ), refname); }return result; --2.1.4
Re: [PATCH 06/13] delete_refs(): convert error message to lower case
Quoted 10 lines · Stefan Beller
Quoted 3 lines · Michael Haggerty
This string is going to have to be re-internationalized anyway because of the previous commit. So while we're at it, we might as well convert it to lower case as per our usual practice.
Although the previous patch and this are addressing two slightly different things,
we may want to squash this into the previous without dropping any of the commit message?
(It might make reviewing easier, I'd assume)
OK, I will squash them together.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
PATCH 7/13 prune_remote(): use delete_refs()
In reply to Michael HaggertyThis will result in errors being emitted for references that can't be deleted, but that is a good thing.
--- builtin/remote.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
builtin/remote.c +2 -9
@@ -1314,19 +1314,12 @@ static int prune_remote(const char *remote, int dry_run)string_list_append (&refs_to_prune, item->util);string_list_sort (&refs_to_prune); -if (!dry_run) { -struct strbuf err =STRBUF_INIT ; -if (repack_without_refs (&refs_to_prune, &err)) - result |=error ("%s" , err.buf); -strbuf_release (&err); - } +if (!dry_run) + result |=delete_refs (&refs_to_prune);for_each_string_list_item (item, &states.stale) {const char *refname = item->util; -if (!dry_run) - result |=delete_ref (refname,NULL ,0 ); -if (dry_run)printf_ln (_ (" * [would prune] %s" ),abbrev_ref (refname,"refs/remotes/" ));
Signature · 1 line
2.1.4
Re: [PATCH 07/13] prune_remote(): use delete_refs()
Quoted 2 lines · Michael Haggerty
This will result in errors being emitted for references that can't be deleted, but that is a good thing.
This sounds a bit like hand-waving to me. "Trust me, I'm an engineer!".
Quoted 35 lines
Signed-off-by Michael Haggerty <[email protected]>--- builtin/remote.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)builtin/remote.c +2
-10@@ -1314,19 +1314,12 @@ static int prune_remote(const char *remote, int dry_run)string_list_append (&refs_to_prune, item->util);string_list_sort (&refs_to_prune); -if (!dry_run) { -struct strbuf err =STRBUF_INIT ; -if (repack_without_refs (&refs_to_prune, &err)) - result |=error ("%s" , err.buf); -strbuf_release (&err); - } +if (!dry_run) + result |=delete_refs (&refs_to_prune);for_each_string_list_item (item, &states.stale) {const char *refname = item->util; -if (!dry_run) - result |=delete_ref (refname,NULL ,0 ); -if (dry_run)printf_ln (_ (" * [would prune] %s" ),abbrev_ref (refname,"refs/remotes/" )); --2.1.4
Re: [PATCH 07/13] prune_remote(): use delete_refs()
Quoted 5 lines · Stefan Beller
Quoted 2 lines · Michael Haggerty
This will result in errors being emitted for references that can't be deleted, but that is a good thing.
This sounds a bit like hand-waving to me. "Trust me, I'm an engineer!".
I think the argument is "we failed to do that the user asked for, but never reported the reason why".
But I don't see how that is the case. We already complain if repack_without_refs fail, and AFAICT the original call to delete_ref() would emit an error, as well.
-Peff
Re: [PATCH 07/13] prune_remote(): use delete_refs()
Quoted 6 lines · Jeff King
Quoted 4 lines · Stefan Beller
Quoted 2 lines · Michael Haggerty
This will result in errors being emitted for references that can't be deleted, but that is a good thing.
Quoted 8 lines
Quoted 1 line
This sounds a bit like hand-waving to me. "Trust me, I'm an engineer!".
I think the argument is "we failed to do that the user asked for, but never reported the reason why".
But I don't see how that is the case. We already complain if
repack_without_refsfail, and AFAICT the original call todelete_ref()would emit an error, as well.
The old and new code report errors that come from repack_without_refs() the same way. The difference is how they report errors within delete_ref().
The old code only allowed delete_ref() to emit its error.
The new code (in delete_refs()) allows delete_ref() to emit its error, but then follows it up with
error(_("could not remove reference %s"), refname)
The "could not remove reference" error originally came from a similar message in remove_branches() (from builtin/remote.c).
I *think* this is an improvement, because the error from delete_ref() (which usually comes from ref_transaction_commit()) can be pretty low-level, like
Cannot lock ref '%s': unable to resolve reference %s: %s
where the last "%s" is the original strerror.
I would be happy to change the behavior if somebody has a concrete wish. At the same time I don't think we need to sweat the details too much, because these errors should only ever be seen in the case of a broken repository or a race between two processes; i.e., only in pretty rare and anomalous situations.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH 07/13] prune_remote(): use delete_refs()
Quoted 14 lines · Michael Haggerty
The new code (in
delete_refs()) allowsdelete_ref()to emit its error, but then follows it up with
error(_("could not remove reference %s"), refname)The "could not remove reference" error originally came from a similar message in
remove_branches()(frombuiltin/remote.c).I *think* this is an improvement, because the error from
delete_ref()(which usually comes fromref_transaction_commit()) can be pretty low-level, likeCannot lock ref '%s': unable to resolve reference %s: %s
Quoted 7 lines
where the last "%s" is the original strerror.
I would be happy to change the behavior if somebody has a concrete wish. At the same time I don't think we need to sweat the details too much, because these errors should only ever be seen in the case of a broken repository or a race between two processes; i.e., only in pretty rare and anomalous situations.
Thanks for the explanation. I agree it probably doesn't matter much either way.
-Peff
PATCH 8/13 repack_without_refs(): make function private
In reply to Michael HaggertyIt is no longer called from outside of the refs module. Also move its docstring and change it to imperative voice.
--- refs.c | 9 ++++++++- refs.h | 11 ----------- 2 files changed, 8 insertions(+), 12 deletions(-)
refs.c +8 -1
@@ -2724,7 +2724,14 @@ int pack_refs(unsigned int flags)return 0 ; } -int repack_without_refs (struct string_list *refnames,struct strbuf *err) +/* +* Rewrite the packed-refs file, omitting any refs listed in +* 'refnames'. On error, leave packed-refs unchanged, write an error +* message to 'err', and return a nonzero value. +* +* The refs in 'refnames' needn't be sorted. `err` must not be NULL. +*/ +static int repack_without_refs (struct string_list *refnames,struct strbuf *err) {struct ref_dir *packed;struct string_list_item *refname;
refs.h +0 -11
@@ -154,17 +154,6 @@ extern void rollback_packed_refs(void);*/ int pack_refs (unsigned int flags); -/* -* Rewrite the packed-refs file, omitting any refs listed in -* 'refnames'. On error, packed-refs will be unchanged, the return -* value is nonzero, and a message about the error is written to the -* 'err' strbuf. -* -* The refs in 'refnames' needn't be sorted. `err` must not be NULL. -*/ -extern int repack_without_refs (struct string_list *refnames, -struct strbuf *err); -extern int ref_exists (const char *);extern int is_branch (const char *refname);
Signature · 1 line
2.1.4
PATCH 9/13 initial_ref_transaction_commit(): function for initial ref creation
In reply to Michael Haggerty"git clone" uses shortcuts when creating the initial set of references:
* It writes them directly to packed-refs.
* It doesn't lock the individual references (though it does lock the packed-refs file).
* It doesn't check for refname conflicts between two new references or between one new reference and any hypothetical old ones.
* It doesn't create reflog entries for the reference creations.
This functionality was implemented in builtin/clone.c. But really that file shouldn't have such intimate knowledge of how references are stored. So provide a new function in the refs API, initial_ref_transaction_commit(), which can be used for initial reference creation. The new function is based on the ref_transaction interface.
This means that we can make some other functions private to the refs module. That will be done in a followup commit.
It would seem to make sense to add a test here that there are no existing references, because that is how the function *should* be used. But in fact, the "testgit" remote helper appears to call it *after* having set up refs/remotes/<name>/HEAD and refs/remotes/<name>/master, so we can't be so strict. For now, the function trusts its caller to only call it when it makes sense. Future commits will add some more limited sanity checks.
--- builtin/clone.c | 19 +++++++++++++++---- refs.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ refs.h | 14 ++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-)
builtin/clone.c +15 -4
@@ -500,16 +500,27 @@ static void write_remote_refs(const struct ref *local_refs) {const struct ref *r; -lock_packed_refs (LOCK_DIE_ON_ERROR ); +struct ref_transaction *t; +struct strbuf err =STRBUF_INIT ; + + t =ref_transaction_begin (&err); +if (!t) +die (err.buf);for (r = local_refs; r; r = r->next) {if (!r->peer_ref)continue ; -add_packed_ref (r->peer_ref->name, r->old_sha1); +if (ref_transaction_create (t, r->peer_ref->name, r->old_sha1, +0 ,NULL , &err)) +die (err.buf); + } + +if (initial_ref_transaction_commit (t, &err)) { +die (err.buf); } -if (commit_packed_refs ()) -die_errno ("unable to overwrite old ref-pack file" ); +strbuf_release (&err); +ref_transaction_free (t); }static void write_followtags (const struct ref *refs,const char *msg)
refs.c +47 -0
53 lines not on this page · raw message
refs.h +14 -0
@@ -365,6 +365,20 @@ int ref_transaction_commit(struct ref_transaction *transaction, struct strbuf *err);/* +* Like ref_transaction_commit(), but optimized for creating +* references when originally initializing a repository (e.g., by "git +* clone"). It writes the new references directly to packed-refs +* without locking the individual references. +* +* It is a bug to call this function when there might be other +* processes accessing the repository or if there are existing +* references that might conflict with the ones being created. All +* old_sha1 values must either be absent or NULL_SHA1. +*/ +int initial_ref_transaction_commit (struct ref_transaction *transaction, +struct strbuf *err); + +/* * Free an existing transaction and all associated data. */ void ref_transaction_free (struct ref_transaction *transaction);
Signature · 1 line
2.1.4
PATCH 10/13 refs: remove some functions from the module's public interface
In reply to Michael HaggertyThe following functions are no longer used from outside the refs module:
* lock_packed_refs()
* add_packed_ref()
* commit_packed_refs()
* rollback_packed_refs()
So make these functions private.
This is an important step, because it means that nobody outside of the refs module needs to know the difference between loose and packed references.
--- refs.c | 31 ++++++++++++++++++++++++------- refs.h | 30 ------------------------------ 2 files changed, 24 insertions(+), 37 deletions(-)
refs.c +24 -7
@@ -1314,7 +1314,13 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)return get_packed_ref_dir (get_packed_ref_cache (refs)); } -void add_packed_ref (const char *refname,const unsigned char *sha1) +/* +* Add a reference to the in-memory packed reference cache. This may +* only be called while the packed-refs file is locked (see +* lock_packed_refs()). To actually write the packed-refs file, call +* commit_packed_refs(). +*/ +static void add_packed_ref (const char *refname,const unsigned char *sha1) {struct packed_ref_cache *packed_ref_cache =get_packed_ref_cache (&ref_cache);
@@ -2503,8 +2509,12 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)return 0 ; } -/* This should return a meaningful errno on failure */ -int lock_packed_refs (int flags) +/* +* Lock the packed-refs file for writing. Flags is passed to +* hold_lock_file_for_update(). Return 0 on success. On errors, set +* errno appropriately and return a nonzero value. +*/ +static int lock_packed_refs (int flags) {static int timeout_configured =0 ;static int timeout_value =1000 ;
@@ -2534,10 +2544,12 @@ int lock_packed_refs(int flags) }/* -* Commit the packed refs changes. -* On error we must make sure that errno contains a meaningful value. +* Write the current version of the packed refs cache from memory to +* disk. The packed-refs file must already be locked for writing (see +* lock_packed_refs()). Return zero on success. On errors, set errno +* and return a nonzero value */ -int commit_packed_refs (void ) +static int commit_packed_refs (void ) {struct packed_ref_cache *packed_ref_cache =get_packed_ref_cache (&ref_cache);
@@ -2566,7 +2578,12 @@ int commit_packed_refs(void)return error; } -void rollback_packed_refs (void ) +/* +* Rollback the lockfile for the packed-refs file, and discard the +* in-memory packed reference cache. (The packed-refs file will be +* read anew if it is needed again after this function is called.) +*/ +static void rollback_packed_refs (void ) {struct packed_ref_cache *packed_ref_cache =get_packed_ref_cache (&ref_cache);
refs.h +0 -30
@@ -111,36 +111,6 @@ extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refnextern void warn_dangling_symrefs (FILE *fp,const char *msg_fmt,const struct string_list *refnames);/* -* Lock the packed-refs file for writing. Flags is passed to -* hold_lock_file_for_update(). Return 0 on success. -* Errno is set to something meaningful on error. -*/ -extern int lock_packed_refs (int flags); - -/* -* Add a reference to the in-memory packed reference cache. This may -* only be called while the packed-refs file is locked (see -* lock_packed_refs()). To actually write the packed-refs file, call -* commit_packed_refs(). -*/ -extern void add_packed_ref (const char *refname,const unsigned char *sha1); - -/* -* Write the current version of the packed refs cache from memory to -* disk. The packed-refs file must already be locked for writing (see -* lock_packed_refs()). Return zero on success. -* Sets errno to something meaningful on error. -*/ -extern int commit_packed_refs (void ); - -/* -* Rollback the lockfile for the packed-refs file, and discard the -* in-memory packed reference cache. (The packed-refs file will be -* read anew if it is needed again after this function is called.) -*/ -extern void rollback_packed_refs (void ); - -/* * Flags for controlling behaviour of pack_refs() * PACK_REFS_PRUNE: Prune loose refs after packing * PACK_REFS_ALL: Pack _all_ refs, not just tags and already packed refs
Signature · 1 line
2.1.4
PATCH 11/13 initial_ref_transaction_commit(): check for duplicate refs
In reply to Michael HaggertyError out if the ref_transaction includes more than one update for any refname.
--- refs.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
refs.c +11 -0
@@ -4064,12 +4064,22 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,int ret =0 , i;int n = transaction->nr;struct ref_update **updates = transaction->updates; +struct string_list affected_refnames =STRING_LIST_INIT_NODUP ;assert (err);if (transaction->state !=REF_TRANSACTION_OPEN )die ("BUG: commit called for transaction that is not open" ); +/* Fail if a refname appears more than once in the transaction: */ +for (i =0 ; i < n; i++) +string_list_append (&affected_refnames, updates[i]->refname); +string_list_sort (&affected_refnames); +if (ref_update_reject_duplicates (&affected_refnames, err)) { + ret =TRANSACTION_GENERIC_ERROR ; + goto cleanup; + } +for (i =0 ; i < n; i++) {struct ref_update *update = updates[i];
@@ -4102,6 +4112,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction, cleanup: transaction->state =REF_TRANSACTION_CLOSED ; +string_list_clear (&affected_refnames,0 );return ret; }
Signature · 1 line
2.1.4
PATCH 12/13 initial_ref_transaction_commit(): check for ref D/F conflicts
In reply to Michael HaggertyIn initial_ref_transaction_commit(), check for D/F conflicts (i.e., the type of conflict that exists between "refs/foo" and "refs/foo/bar") among the references being created and between the references being created and any hypothetical existing references.
Ideally, there shouldn't *be* any existing references when this function is called. But, at least in the case of the "testgit" remote helper, "clone" can be called after the remote-tracking "HEAD" and "master" branches have already been created. So let's just do the full-blown check.
---
Notes (discussion):
I'm a bit torn on this commit. Part of me says that we should avoid the overhead of the extra checks against loose_refs and packed_refs because there shouldn't be any references there anyway. Instead, the "testgit" remote helper should be fixed.
But these tests should be pretty cheap, and we would want to check for ref D/F conflicts among the new references in any case, so it seems safer to include these checks. Also, our documentation suggests that users refer to git-remote-testgit(1) as an example when writing their own remote helpers, so it could be that there is other code out there that has imitated its light misuse of this functionality.
refs.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
refs.c +34 -0
@@ -4058,9 +4058,19 @@ cleanup:return ret; } +static int ref_present (const char *refname, +const struct object_id *oid,int flags,void *cb_data) +{ +struct string_list *affected_refnames = cb_data; + +return string_list_has_string (affected_refnames, refname); +} +int initial_ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) { +struct ref_dir *loose_refs =get_loose_refs (&ref_cache); +struct ref_dir *packed_refs =get_packed_refs (&ref_cache);int ret =0 , i;int n = transaction->nr;struct ref_update **updates = transaction->updates;
@@ -4080,12 +4090,36 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction, goto cleanup; } +/* +* It's really undefined to call this function in an active +* repository or when there are existing references: we are +* only locking and changing packed-refs, so (1) any +* simultaneous processes might try to change a reference at +* the same time we do, and (2) any existing loose versions of +* the references that we are setting would have precedence +* over our values. But some remote helpers create the remote +* "HEAD" and "master" branches before calling this function, +* so here we really only check that none of the references +* that we are creating already exists. +*/ +if (for_each_rawref (ref_present, &affected_refnames)) +die ("BUG: initial ref transaction called with existing refs" ); +for (i =0 ; i < n; i++) {struct ref_update *update = updates[i];if ((update->flags &REF_HAVE_OLD ) && !is_null_sha1 (update->old_sha1))die ("BUG: initial ref transaction with old_sha1 set" ); +if (verify_refname_available (update->refname, + &affected_refnames,NULL , + loose_refs, err) || +verify_refname_available (update->refname, + &affected_refnames,NULL , + packed_refs, err)) { + ret =TRANSACTION_NAME_CONFLICT ; + goto cleanup; + } }if (lock_packed_refs (0 )) {
Signature · 1 line
2.1.4
PATCH 13/13 refs: move the remaining ref module declarations to refs.h
In reply to Michael HaggertySome functions from the refs module were still declared in cache.h. Move them to refs.h.
Add some parameter names where they were missing.
--- archive.c | 1 + builtin/blame.c | 1 + builtin/fast-export.c | 1 + builtin/fmt-merge-msg.c | 1 + builtin/init-db.c | 1 + builtin/log.c | 1 + cache.h | 66 ----------------------- refs.c | 6 ++- refs.h | 139 +++++++++++++++++++++++++++++++++++++----------- remote-testsvn.c | 1 + 10 files changed, 118 insertions(+), 100 deletions(-)
archive.c +1 -0
@@ -1,4 +1,5 @@#include "cache.h" +#include "refs.h" #include "commit.h" #include "tree-walk.h" #include "attr.h"
builtin/blame.c +1 -0
@@ -6,6 +6,7 @@*/ #include "cache.h" +#include "refs.h" #include "builtin.h" #include "blob.h" #include "commit.h"
builtin/fast-export.c +1 -0
@@ -5,6 +5,7 @@*/ #include "builtin.h" #include "cache.h" +#include "refs.h" #include "commit.h" #include "object.h" #include "tag.h"
builtin/fmt-merge-msg.c +1 -0
@@ -1,5 +1,6 @@#include "builtin.h" #include "cache.h" +#include "refs.h" #include "commit.h" #include "diff.h" #include "revision.h"
builtin/init-db.c +1 -0
@@ -4,6 +4,7 @@* Copyright (C) Linus Torvalds, 2005 */ #include "cache.h" +#include "refs.h" #include "builtin.h" #include "exec_cmd.h" #include "parse-options.h"
builtin/log.c +1 -0
@@ -5,6 +5,7 @@* 2006 Junio Hamano */ #include "cache.h" +#include "refs.h" #include "color.h" #include "commit.h" #include "diff.h"
cache.h +0 -66
76 lines not on this page · raw message
refs.c +4 -2
@@ -1732,9 +1732,11 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,return ret; } -char *resolve_refdup (const char *ref,int resolve_flags,unsigned char *sha1,int *flags) +char *resolve_refdup (const char *refname,int resolve_flags, +unsigned char *sha1,int *flags) { -return xstrdup_or_null (resolve_ref_unsafe (ref, resolve_flags, sha1, flags)); +return xstrdup_or_null (resolve_ref_unsafe (refname, resolve_flags, + sha1, flags)); }/* The argument to filter_refs */
refs.h +107 -32
185 lines not on this page · raw message
remote-testsvn.c +1 -0
@@ -1,4 +1,5 @@#include "cache.h" +#include "refs.h" #include "remote.h" #include "strbuf.h" #include "url.h"
Signature · 1 line
2.1.4
Quoted 13 lines · Michael Haggerty
Add functions to the reference API to
* Delete a bunch of references at once, but *without* failing the whole transaction if one of the deletions fails. This functionality is used by
git remote removeandgit remote prune.* Create initial references during
git clone. (During clone, references are written directly to thepacked-refsfile without any locking.)Also move the remaining "refs" function declarations from
cache.htorefs.h.
Quoted 8 lines
This improves the encapsulation of the refs module. Especially, it means that code outside of the refs module should no longer need to care about the distinction between loose and packed references.
These patches are also available from my GitHub account [1] as branch "init-delete-refs-api".
Thw whole series looks good to me.
Quoted 33 lines
Michael Haggerty (13):
delete_ref(): move declaration torefs.hremove_branches(): remove temporary
delete_ref(): handle special case more explicitlydelete_refs(): new function for the refs APIdelete_refs(): improve error message
delete_refs(): convert error message to lower caseprune_remote(): usedelete_refs()
repack_without_refs(): make function privateinitial_ref_transaction_commit(): function for initial ref creation
refs: remove some functions from the module's public interfaceinitial_ref_transaction_commit(): check for duplicate refsinitial_ref_transaction_commit(): check for ref D/F conflicts
refs: move the remaining ref module declarations torefs.harchive.c | 1 + builtin/blame.c | 1 + builtin/clone.c | 19 ++++- builtin/fast-export.c | 1 + builtin/fmt-merge-msg.c | 1 + builtin/init-db.c | 1 + builtin/log.c | 1 + builtin/remote.c | 33 +------- cache.h | 68 ---------------- refs.c | 167 +++++++++++++++++++++++++++++++++++--- refs.h | 210 +++++++++++++++++++++++++++++++----------------- remote-testsvn.c | 1 + 12 files changed, 316 insertions(+), 188 deletions(-)--
2.1.4
Quoted 10 lines · Michael Haggerty
Add functions to the reference API to
* Delete a bunch of references at once, but *without* failing the whole transaction if one of the deletions fails. This functionality is used by
git remote removeandgit remote prune.* Create initial references during
git clone. (During clone, references are written directly to thepacked-refsfile without any locking.)
Quoted 6 lines
Also move the remaining "refs" function declarations from
cache.htorefs.h.This improves the encapsulation of the refs module. Especially, it means that code outside of the refs module should no longer need to care about the distinction between loose and packed references.
Yay. The subject of the series matches this "primary value" of the topic, even though the cover text makes it sound as if it was just a "while we are at adding two functions" with "Also move..." ;-)