Use ref transactions
v14 cover Use ref transactions
This patch series can also be found at
https://github.com/rsahlberg/git/tree/ref-transactions
This patch series is based on next and expands on the transaction API. It converts all ref updates, inside refs.c as well as external, to use the transaction API for updates. This makes most of the ref updates to become atomic when there are failures locking or writing to a ref.
This version completes the work to convert all ref updates to use transactions. Now that all updates are through transactions I will start working on cleaning up the reading of refs and to create an api for managing reflogs but all that will go in a different patch series.
Version 14:
- Remove the patch to pack the refs before rename. We do not need this with the reworked renames that will come 2 series later.
The rename_ref changes are thus no longer part of this series.
Version 13:
- This version should cover all of JNs suggestions on the previous series. If I missed anything I appologize.
...
Ronnie Sahlberg (40):
refs.c: remove ref_transaction_rollback
refs.c: ref_transaction_commit should not free the transaction refs.c: constify the sha arguments for
ref_transaction_create|delete|update
refs.c: allow passing NULL to ref_transaction_free
refs.c: add a strbuf argument to ref_transaction_commit for error
logging
refs.c: add an err argument to repack_without_refs
refs.c: make ref_update_reject_duplicates take a strbuf argument for
errors
refs.c: add an err argument to delete_ref_loose
refs.c: make update_ref_write update a strbuf on failure
update-ref: use err argument to get error from ref_transaction_commit refs.c: remove the onerr argument to ref_transaction_commit refs.c: change ref_transaction_update() to do error checking and
return status
refs.c: change ref_transaction_create to do error checking and return
status
refs.c: update ref_transaction_delete to check for error and return
status
refs.c: make ref_transaction_begin take an err argument refs.c: add transaction.status and track OPEN/CLOSED/ERROR tag.c: use ref transactions when doing updates
replace.c: use the ref transaction functions for updates commit.c: use ref transactions for updates
sequencer.c: use ref transactions for all ref updates fast-import.c: change update_branch to use ref transactions branch.c: use ref transaction for all ref updates
refs.c: change update_ref to use a transaction receive-pack.c: use a reference transaction for updating the refs fast-import.c: use a ref transaction when dumping tags walker.c: use ref transaction for ref updates
refs.c: make lock_ref_sha1 static
refs.c: remove the update_ref_lock function
refs.c: remove the update_ref_write function
refs.c: remove lock_ref_sha1
refs.c: make prune_ref use a transaction to delete the ref refs.c: make delete_ref use a transaction
refs.c: pass the ref log message to _create/delete/update instead of
_commit
refs.c: pass NULL as *flags to read_ref_full
refs.c: move the check for valid refname to lock_ref_sha1_basic refs.c: call lock_ref_sha1_basic directly from commit refs.c: pass a skip list to name_conflict_fn
refs.c: propagate any errno==ENOTDIR from _commit back to the callers fetch.c: change s_update_ref to use a ref transaction refs.c: make write_ref_sha1 static
branch.c | 30 +-- builtin/commit.c | 24 ++- builtin/fetch.c | 36 ++-- builtin/receive-pack.c | 32 +++- builtin/replace.c | 15 +- builtin/tag.c | 15 +- builtin/update-ref.c | 34 ++-- cache.h | 2 + fast-import.c | 54 ++++-- lockfile.c | 37 ++-- refs.c | 500 ++++++++++++++++++++++++++++++++----------------- refs.h | 118 +++++++++--- sequencer.c | 24 ++- walker.c | 58 +++--- 14 files changed, 641 insertions(+), 338 deletions(-)
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 1/40 refs.c: remove ref_transaction_rollback
We do not yet need both a rollback and a free function for transactions. Remove ref_transaction_rollback and use ref_transaction_free instead.
At a later stage we may reintroduce a rollback function if we want to start adding reusable transactions and similar.
--- refs.c | 7 +------ refs.h | 16 +++++++--------- 2 files changed, 8 insertions(+), 15 deletions(-)
refs.c +1 -6
@@ -3308,7 +3308,7 @@ struct ref_transaction *ref_transaction_begin(void)return xcalloc (1 ,sizeof (struct ref_transaction )); } -static void ref_transaction_free (struct ref_transaction *transaction) +void ref_transaction_free (struct ref_transaction *transaction) {int i;
@@ -3319,11 +3319,6 @@ static void ref_transaction_free(struct ref_transaction *transaction)free (transaction); } -void ref_transaction_rollback (struct ref_transaction *transaction) -{ -ref_transaction_free (transaction); -} -static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) {
refs.h +7 -9
@@ -216,18 +216,12 @@ enum action_on_err {/* * Begin a reference transaction. The reference transaction must -* eventually be commited using ref_transaction_commit() or rolled -* back using ref_transaction_rollback(). +* eventually be commited using ref_transaction_commit() or freed by +* calling ref_transaction_free(). */ struct ref_transaction *ref_transaction_begin (void );/* -* Roll back a ref_transaction and free all associated data. -*/ -void ref_transaction_rollback (struct ref_transaction *transaction); - - -/* * The following functions add a reference check or update to a * ref_transaction. In all of them, refname is the name of the * reference to be affected. The functions make internal copies of
@@ -235,7 +229,6 @@ void ref_transaction_rollback(struct ref_transaction *transaction);* can be REF_NODEREF; it is passed to update_ref_lock(). */ -/* * Add a reference update to transaction. new_sha1 is the value that * the reference should have after the update, or zeros if it should
@@ -277,6 +270,11 @@ void ref_transaction_delete(struct ref_transaction *transaction,int ref_transaction_commit (struct ref_transaction *transaction,const char *msg,enum action_on_err onerr); +/* +* Free an existing transaction and all associated data. +*/ +void ref_transaction_free (struct ref_transaction *transaction); +/** Lock a ref and then write its file */ int update_ref(const char *action, const char *refname, const unsigned char *sha1, const unsigned char *oldval,
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 2/40 refs.c: ref_transaction_commit should not free the transaction
In reply to Ronnie Sahlberg--- builtin/update-ref.c | 1 + refs.c | 1 - refs.h | 5 ++--- 3 files changed, 3 insertions(+), 4 deletions(-)
builtin/update-ref.c +1 -0
@@ -369,6 +369,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)update_refs_stdin (); ret =ref_transaction_commit (transaction, msg,UPDATE_REFS_DIE_ON_ERR ); +ref_transaction_free (transaction);return ret; }
refs.c +0 -1
@@ -3483,7 +3483,6 @@ cleanup:if (updates[i]->lock)unlock_ref (updates[i]->lock);free (delnames); -ref_transaction_free (transaction);return ret; }
refs.h +2 -3
@@ -216,8 +216,7 @@ enum action_on_err {/* * Begin a reference transaction. The reference transaction must -* eventually be commited using ref_transaction_commit() or freed by -* calling ref_transaction_free(). +* be freed by calling ref_transaction_free(). */ struct ref_transaction *ref_transaction_begin (void );
@@ -265,7 +264,7 @@ void ref_transaction_delete(struct ref_transaction *transaction,/* * Commit all of the changes that have been queued in transaction, as * atomically as possible. Return a nonzero value if there is a -* problem. The ref_transaction is freed by this function. +* problem. */ int ref_transaction_commit (struct ref_transaction *transaction,const char *msg,enum action_on_err onerr);
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 3/40 refs.c: constify the sha arguments for ref_transaction_create|delete|update
In reply to Ronnie Sahlbergref_transaction_create|delete|update has no need to modify the sha1 arguments passed to it so it should use const unsigned char* instead of unsigned char*.
Some functions, such as fast_forward_to(), already have its old/new sha1 arguments as consts. This function will at some point need to use ref_transaction_update() in which case this change is required.
--- refs.c | 7 ++++--- refs.h | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-)
refs.c +4 -3
@@ -3333,7 +3333,8 @@ static struct ref_update *add_update(struct ref_transaction *transaction,void ref_transaction_update (struct ref_transaction *transaction,const char *refname, -unsigned char *new_sha1,unsigned char *old_sha1, +const unsigned char *new_sha1, +const unsigned char *old_sha1,int flags,int have_old) {struct ref_update *update =add_update (transaction, refname);
@@ -3347,7 +3348,7 @@ void ref_transaction_update(struct ref_transaction *transaction,void ref_transaction_create (struct ref_transaction *transaction,const char *refname, -unsigned char *new_sha1, +const unsigned char *new_sha1,int flags) {struct ref_update *update =add_update (transaction, refname);
@@ -3361,7 +3362,7 @@ void ref_transaction_create(struct ref_transaction *transaction,void ref_transaction_delete (struct ref_transaction *transaction,const char *refname, -unsigned char *old_sha1, +const unsigned char *old_sha1,int flags,int have_old) {struct ref_update *update =add_update (transaction, refname);
refs.h +4 -3
@@ -237,7 +237,8 @@ struct ref_transaction *ref_transaction_begin(void);*/ void ref_transaction_update (struct ref_transaction *transaction,const char *refname, -unsigned char *new_sha1,unsigned char *old_sha1, +const unsigned char *new_sha1, +const unsigned char *old_sha1,int flags,int have_old);/*
@@ -248,7 +249,7 @@ void ref_transaction_update(struct ref_transaction *transaction,*/ void ref_transaction_create (struct ref_transaction *transaction,const char *refname, -unsigned char *new_sha1, +const unsigned char *new_sha1,int flags);/*
@@ -258,7 +259,7 @@ void ref_transaction_create(struct ref_transaction *transaction,*/ void ref_transaction_delete (struct ref_transaction *transaction,const char *refname, -unsigned char *old_sha1, +const unsigned char *old_sha1,int flags,int have_old);/*
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 4/40 refs.c: allow passing NULL to ref_transaction_free
In reply to Ronnie SahlbergAllow ref_transaction_free(NULL) as a no-op. This makes ref_transaction_free easier to use and more similar to plain 'free'.
In particular, it lets us rollback unconditionally as part of cleanup code after setting 'transaction = NULL' if a transaction has been committed or rolled back already.
--- refs.c | 3 +++ 1 file changed, 3 insertions(+)
refs.c +3 -0
@@ -3312,6 +3312,9 @@ void ref_transaction_free(struct ref_transaction *transaction) {int i; +if (!transaction) +return ; +for (i =0 ; i < transaction->nr; i++)free (transaction->updates[i]);
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 5/40 refs.c: add a strbuf argument to ref_transaction_commit for error logging
In reply to Ronnie SahlbergAdd a strbuf argument to _commit so that we can pass an error string back to the caller. So that we can do error logging from the caller instead of from _commit.
Longer term plan is to first convert all callers to use onerr==QUIET_ON_ERR and craft any log messages from the callers themselves and finally remove the onerr argument completely.
--- builtin/update-ref.c | 2 +- refs.c | 6 +++++- refs.h | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-)
builtin/update-ref.c +1 -1
@@ -367,7 +367,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)if (end_null) line_termination ='\0' ;update_refs_stdin (); - ret =ref_transaction_commit (transaction, msg, + ret =ref_transaction_commit (transaction, msg,NULL ,UPDATE_REFS_DIE_ON_ERR );ref_transaction_free (transaction);return ret;
refs.c +5 -1
@@ -3418,7 +3418,8 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n, }int ref_transaction_commit (struct ref_transaction *transaction, -const char *msg,enum action_on_err onerr) +const char *msg,struct strbuf *err, +enum action_on_err onerr) {int ret =0 , delnum =0 , i;const char **delnames;
@@ -3447,6 +3448,9 @@ int ref_transaction_commit(struct ref_transaction *transaction, update->flags, &update->type, onerr);if (!update->lock) { +if (err) +strbuf_addf (err,"Cannot lock the ref '%s'." , + update->refname); ret =1 ; goto cleanup; }
refs.h +4 -1
@@ -266,9 +266,12 @@ void ref_transaction_delete(struct ref_transaction *transaction,* Commit all of the changes that have been queued in transaction, as * atomically as possible. Return a nonzero value if there is a * problem. +* If err is non-NULL we will add an error string to it to explain why +* the transaction failed. The string does not end in newline. */ int ref_transaction_commit (struct ref_transaction *transaction, -const char *msg,enum action_on_err onerr); +const char *msg,struct strbuf *err, +enum action_on_err onerr);/* * Free an existing transaction and all associated data.
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 6/40 refs.c: add an err argument to repack_without_refs
In reply to Ronnie SahlbergUpdate repack_without_refs to take an err argument and update it if there is a failure. Pass the err variable from ref_transaction_commit to this function so that callers can print a meaningful error message if _commit fails due to a problem in repack_without_refs.
Add a new function unable_to_lock_message that takes a strbuf argument and fills in the reason for the failure.
In commit_packed_refs, make sure that we propagate any errno that commit_lock_file might have set back to our caller.
Make sure both commit_packed_refs and lock_file has errno set to a meaningful value on error. Update a whole bunch of other places to keep errno from beeing clobbered.
--- cache.h | 2 ++ lockfile.c | 37 +++++++++++++-------- refs.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 110 insertions(+), 40 deletions(-)
cache.h +2 -0
@@ -559,6 +559,8 @@ struct lock_file {#define LOCK_DIE_ON_ERROR 1#define LOCK_NODEREF 2extern int unable_to_lock_error (const char *path,int err); +extern void unable_to_lock_message (const char *path,int err, +struct strbuf *buf); extern NORETURN void unable_to_lock_index_die(const char *path, int err);extern int hold_lock_file_for_update (struct lock_file *,const char *path,int );extern int hold_lock_file_for_append (struct lock_file *,const char *path,int );
lockfile.c +23 -14
@@ -121,7 +121,7 @@ static char *resolve_symlink(char *p, size_t s)return p; } - +/* Make sure errno contains a meaningful value on error */ static int lock_file (struct lock_file *lk,const char *path,int flags) {/*
@@ -130,8 +130,10 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)*/ static const size_t max_path_len =sizeof (lk->filename) -5 ; -if (strlen (path) >= max_path_len) +if (strlen (path) >= max_path_len) { + errno =ENAMETOOLONG ;return -1 ; + }strcpy (lk->filename, path);if (!(flags &LOCK_NODEREF ))resolve_symlink (lk->filename, max_path_len);
@@ -148,42 +150,49 @@ static int lock_file(struct lock_file *lk, const char *path, int flags) lock_file_list = lk; lk->on_list =1 ; } -if (adjust_shared_perm (lk->filename)) -return error ("cannot fix permission bits on %s" , - lk->filename); +if (adjust_shared_perm (lk->filename)) { +int save_errno = errno; +error ("cannot fix permission bits on %s" , + lk->filename); + errno = save_errno; +return -1 ; + } }else lk->filename[0 ] =0 ;return lk->fd; } -static char *unable_to_lock_message (const char *path,int err) +void unable_to_lock_message (const char *path,int err,struct strbuf *buf) { -struct strbuf buf =STRBUF_INIT ;if (err ==EEXIST ) { -strbuf_addf (&buf,"Unable to create '%s.lock': %s.\n\n" +strbuf_addf (buf,"Unable to create '%s.lock': %s.\n\n" "If no other git process is currently running, this probably means a\n" "git process crashed in this repository earlier. Make sure no other git\n" "process is running and remove the file manually to continue." ,absolute_path (path),strerror (err)); }else -strbuf_addf (&buf,"Unable to create '%s.lock': %s" , +strbuf_addf (buf,"Unable to create '%s.lock': %s" ,absolute_path (path),strerror (err)); -return strbuf_detach (&buf,NULL ); }int unable_to_lock_error (const char *path,int err) { -char *msg =unable_to_lock_message (path, err); -error ("%s" , msg); -free (msg); +struct strbuf buf =STRBUF_INIT ; + +unable_to_lock_message (path, err, &buf); +error ("%s" , buf.buf); +strbuf_release (&buf);return -1 ; } NORETURN void unable_to_lock_index_die(const char *path, int err) { -die ("%s" ,unable_to_lock_message (path, err)); +struct strbuf buf =STRBUF_INIT ; + +unable_to_lock_message (path, err, &buf); +die ("%s" , buf.buf); }int hold_lock_file_for_update (struct lock_file *lk,const char *path,int flags)
refs.c +85 -26
266 lines not on this page · raw message
Signature · 1 line
2.0.0.582.ge25c160
Re: [PATCH v14 06/40] refs.c: add an err argument to repack_without_refs
Quoted 8 lines · Ronnie Sahlberg
Update
repack_without_refsto take an err argument and update it if there is a failure. Pass the err variable fromref_transaction_committo this function so that callers can print a meaningful error message if_commitfails due to a problem inrepack_without_refs.Add a new function
unable_to_lock_messagethat takes a strbuf argument and fills in the reason for the failure.
Quoted 6 lines
In
commit_packed_refs, make sure that we propagate any errno thatcommit_lock_filemight have set back to our caller.Make sure both
commit_packed_refsandlock_filehas errno set to a meaningful value on error. Update a whole bunch of other places to keep errno from beeing clobbered.
This patch is doing several (all nice) things. Are they connected? Would splitting some of them out into separate patches make sense or would it be too much fuss to be worth the trouble?
[...]
cache.h struct lock_file { · line 563
Quoted 8 lines
cache.h +2
-0@@ -559,6 +559,8 @@ struct lock_file {#define LOCK_DIE_ON_ERROR 1#define LOCK_NODEREF 2extern int unable_to_lock_error (const char *path,int err); +extern void unable_to_lock_message (const char *path,int err, +struct strbuf *buf);
Introducing a new unable_to_lock_message helper, which has nicer semantics than unable_to_lock_error and cleans up lockfile.c a little.
[...]
lockfile.c static char *resolve_symlink(char *p, size_t s) · line 125
Quoted 9 lines
lockfile.c +1
-1@@ -121,7 +121,7 @@ static char *resolve_symlink(char *p, size_t s)return p; } - +/* Make sure errno contains a meaningful value on error */ static int lock_file (struct lock_file *lk,const char *path,int flags)
Making errno when returning from lock_file() meaningful, which should fix
* an existing almost-bug in lock_ref_sha1_basic where it assumes errno==ENOENT is meaningful and could waste some work on retries
* an existing bug in repack_without_refs where it prints strerror(errno) and picks advice based on errno, despite errno potentially being zero and potentially having been clobbered by that point
To make sure we don't lose these fixes, it would be helpful to also mention that errno is set in the API documentation for the relevant functions:
* hold_lock_file_for_update (declared in cache.h)
* lock_packed_refs (declared in refs.h)
[...]
refs.c const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
Quoted 3 lines
refs.c +0
-0@@ -1333,8 +1333,10 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
Making errno when returning from resolve_ref_unsafe() meaningful, which should fix
* a bug in lock_ref_sha1_basic, where it assumes EISDIR means it failed due to a directory being in the way
To make sure we don't lose this fix, it would be helpful to change Michael's understated comment (why wasn't it marked with NEEDSWORK, btw?)
* errno is sometimes set on errors, but not always.
to describe the new guarantee.
[...]
refs.c static struct ref_lock *verify_lock(struct ref_lock *lock,
Quoted 1 line
refs.c +0
-0@@ -1908,14 +1918,17 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,
Making errno when returning from verify_lock() meaningful, which should almost but not completely fix
* a bug in "git fetch"'s s_update_ref, which trusts the result of an errno == ENOTDIR check to detect D/F conflicts
To make sure we don't lose this fix, it would be helpful to also mention that errno is set in the API documentation for the relevant functions:
* lock_any_ref_for_update (declared in refs.h), after handling the check_refname_format case
* lock_ref_sha1_basic
ENOTDIR makes sense as a sign that a file was in the way of a directory we wanted to create. Should "git fetch" also look for ENOTEMPTY or EEXIST to catch cases where a directory was in the way of a file to be created?
refs.c static int remove_empty_directories(const char *file)
Quoted 1 line
refs.c +0
-0@@ -1928,13 +1941,15 @@ static int remove_empty_directories(const char *file)
Making errno when returning from remove_empty_directories() more obviously meaningful, which should provide some peace of mind for people auditing lock_ref_sha1_basic.
[...]
Quoted 1 line
refs.c +0
-0@@ -2203,11 +2218,16 @@ int lock_packed_refs(int flags)
[...]
Quoted 1 line
int
commit_packed_refs(void)
Making errno when returning from commit_packed_refs() meaningful, which should fix
* a bug in "git clone" where it prints strerror(errno) based on errno, despite errno possibly being zero and potentially having been clobbered by that point
* the same kind of bug in "git pack-refs"
and prepares for repack_without_refs() to get a meaningful error message when commit_packed_refs() fails without falling into the same bug.
To make sure we don't lose this fix, it would be helpful to also mention that errno is set in the API documentation for commit_packed_refs in refs.h.
[...]
refs.c static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data) · line 2453
Quoted 6 lines
refs.c +1
-1@@ -2427,12 +2450,12 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)return 0 ; } -static int repack_without_refs (const char **refnames,int n) +static int repack_without_refs (const char **refnames,int n,struct strbuf *err)
Adding an 'err' argument to repack_without_refs and writing to it on error.
[...]
refs.c int log_ref_setup(const char *refname, char *logfile, int bufsize)
Quoted 1 line
refs.c +0
-0@@ -2723,9 +2755,12 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize)
Making errno when returning from log_ref_setup() meaningful, presumably as preparation for making errno from log_ref_write() and write_ref_sha1() meaningful.
To make sure we don't break it by mistake, it would be helpful to also mention that errno is set in the API documentation for log_ref_setup in refs.h (or to make that function static --- why is it public?).
[...]
refs.c static int log_ref_write(const char *refname, const unsigned char *old_sha1,
Quoted 1 line
refs.c +0
-0@@ -2784,8 +2826,19 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1,
Making errno from log_ref_write() meaningful.
[...]
refs.c int write_ref_sha1(struct ref_lock *lock,
Quoted 1 line
refs.c +0
-0@@ -2800,8 +2853,10 @@ int write_ref_sha1(struct ref_lock *lock,
Making errno from write_ref_sha1() meaningful, which should fix
* a bug in "git checkout -b" where it prints strerror(errno) despite errno possibly being zero or clobbered
* a bug in "git fetch"'s s_update_ref, which trusts the result of an errno == ENOTDIR check to detect D/F conflicts
To make sure we don't lose these fixes, it would be helpful to also mention that errno is set in the API documentation for write_ref_sha1 in refs.h.
So this could potentially be multiple patches:
1. Set errno in lock_packed_refs, fixing a bug in repack_without_refs
2. Set errno in resolve_ref_unsafe, fixing a longstanding TODO
3. Set errno in lock_any_ref_for_update, tightening half of the D/F check
4. Set errno in commit_packed_refs, fixing a bug in clone, packed-refs
5. Set errno in log_ref_setup, log_ref_write, and write_ref_sha1, fixing a bug in 'checkout -b' and the other half of the D/F check
6. Introduce unable_to_lock_message helper
7. Add an 'err' argument to repack_without_refs and write to it.
cache.h +8 -1
@@ -562,8 +562,10 @@ extern int unable_to_lock_error(const char *path, int err);extern void unable_to_lock_message (const char *path,int err,struct strbuf *buf); extern NORETURN void unable_to_lock_index_die(const char *path, int err); +/* Sets errno on error. */ extern int hold_lock_file_for_update (struct lock_file *,const char *path,int );extern int hold_lock_file_for_append (struct lock_file *,const char *path,int ); +/* Sets errno on error. */ extern int commit_lock_file (struct lock_file *);extern void update_index_if_able (struct index_state *,struct lock_file *);
@@ -785,6 +787,11 @@ enum sharedrepo { PERM_EVERYBODY = 0664 };int git_config_perm (const char *var,const char *value); + +/* +* Widens permissions on 'path' to respect core.sharedrepository. +* Sets errno on error. +*/ int adjust_shared_perm (const char *path);/*
@@ -979,7 +986,7 @@ extern int read_ref(const char *refname, unsigned char *sha1);* NULL. If more than MAXDEPTH recursive symbolic lookups are needed, * give up and return NULL. * -* errno is sometimes set on errors, but not always. +* On error, sets errno to indicate what went wrong. */ extern const char *resolve_ref_unsafe (const char *ref,unsigned char *sha1,int reading,int *flag);extern char *resolve_refdup (const char *ref,unsigned char *sha1,int reading,int *flag);
dir.c +8 -4
@@ -1527,7 +1527,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) {DIR *dir;struct dirent *e; -int ret =0 , original_len = path->len, len, kept_down =0 ; +int ret =0 , save_errno =0 ; +int original_len = path->len, len, kept_down =0 ;int only_empty = (flag &REMOVE_DIR_EMPTY_ONLY );int keep_toplevel = (flag &REMOVE_DIR_KEEP_TOPLEVEL );unsigned char submodule_head[20 ];
@@ -1582,20 +1583,23 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) }/* path too long, stat fails, or non-directory still exists */ + save_errno = errno; ret =-1 ;break ; }closedir (dir);strbuf_setlen (path, original_len); -if (!ret && !keep_toplevel && !kept_down) - ret = (!rmdir (path->buf) || errno ==ENOENT ) ?0 :-1 ; -else if (kept_up) +if (!ret && !keep_toplevel && !kept_down) { +if (rmdir (path->buf) && errno !=ENOENT ) +return -1 ; + }else if (kept_up)/* * report the uplevel that it is not an error that we * did not rmdir() our directory. */ *kept_up = !ret; + errno = save_errno;return ret; }
dir.h +1 -0
@@ -185,6 +185,7 @@ extern int is_empty_dir(const char *dir);extern void setup_standard_excludes (struct dir_struct *dir); +/* sets errno on error */ #define REMOVE_DIR_EMPTY_ONLY 01#define REMOVE_DIR_KEEP_NESTED_GIT 02#define REMOVE_DIR_KEEP_TOPLEVEL 04
refs.c +19 -10
@@ -1295,6 +1295,8 @@ static struct ref_entry *get_packed_ref(const char *refname)/* * A loose ref file doesn't exist; check for a packed ref. The * options are forwarded from resolve_safe_unsafe(). +* +* Sets errno on error. */ static const char *handle_missing_loose_ref(const char *refname, unsigned char *sha1,
@@ -1316,6 +1318,7 @@ static const char *handle_missing_loose_ref(const char *refname, }/* The reference is not a packed reference, either. */ if (reading) { + errno =ENOENT ;return NULL ; }else {hashclr (sha1);
@@ -1413,7 +1416,7 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reaint save_errno = errno;close (fd); errno = save_errno; -return NULL ; +return NULL ; }close (fd);while (len &&isspace (buffer[len-1 ]))
@@ -1949,9 +1952,9 @@ static int remove_empty_directories(const char *file) result =remove_dir_recursively (&path,REMOVE_DIR_EMPTY_ONLY ); save_errno = errno; - errno = save_errno;strbuf_release (&path); + errno = save_errno;return result; }
@@ -2039,6 +2042,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)return logs_found; } +/* Sets errno on error. */ static struct ref_lock *lock_ref_sha1_basic (const char *refname,const unsigned char *old_sha1,int flags,int *type_p)
@@ -2152,8 +2156,10 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,const unsigned char *old_sha1, int flags, int *type_p) { - if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) + if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { + errno =EINVAL ;return NULL ; + }return lock_ref_sha1_basic (refname, old_sha1, flags, type_p); }
@@ -2218,10 +2224,6 @@ int lock_packed_refs(int flags)return 0 ; } -/* -* Commit the packed refs changes. -* On error we must make sure that errno contains a meaningful value. -*/ int commit_packed_refs (void ) { struct packed_ref_cache *packed_ref_cache =
@@ -2776,7 +2778,7 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize) logfile); errno = save_errno;return -1 ; - } + } logfd =open (logfile, oflags,0666 ); }
@@ -2794,6 +2796,7 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize)return 0 ; } +/* Sets errno on error. */ static int log_ref_write (const char *refname,const unsigned char *old_sha1,const unsigned char *new_sha1,const char *msg) {
@@ -2824,7 +2827,9 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1, committer);if (msglen) len +=copy_msg (logrec + len -1 , msg) -1 ; - written = len <= maxlen ?write_in_full (logfd, logrec, len) :-1 ; +if (len > maxlen) +die ("BUG: log_ref_write buffer not large enough?" ); + written =write_in_full (logfd, logrec, len);free (logrec);if (written != len) {int save_errno = errno;
@@ -2867,7 +2872,7 @@ int write_ref_sha1(struct ref_lock *lock, lock->ref_name, sha1_to_hex(sha1));unlock_ref (lock); errno =EINVAL ; -return -1 ; +return -1 ; }if (o->type !=OBJ_COMMIT &&is_branch (lock->ref_name)) { error("Trying to write non-commit object %s to branch %s",
@@ -2889,7 +2894,9 @@ int write_ref_sha1(struct ref_lock *lock,if (log_ref_write (lock->ref_name, lock->old_sha1, sha1, logmsg) <0 || (strcmp (lock->ref_name, lock->orig_ref_name) &&log_ref_write (lock->orig_ref_name, lock->old_sha1, sha1, logmsg) <0 )) { +int save_errno = errno;unlock_ref (lock); + errno = save_errno;return -1 ; }if (strcmp (lock->orig_ref_name,"HEAD" ) !=0 ) {
@@ -2914,8 +2921,10 @@ int write_ref_sha1(struct ref_lock *lock,log_ref_write ("HEAD" , lock->old_sha1, sha1, logmsg); }if (commit_ref (lock)) { +int save_errno = errno;error ("Couldn't set %s" , lock->ref_name);unlock_ref (lock); + errno = save_errno;return -1 ; }unlock_ref (lock);
refs.h +18 -4
@@ -81,6 +81,7 @@ extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refn/* * Lock the packed-refs file for writing. Flags is passed to * hold_lock_file_for_update(). Return 0 on success. +* On error, sets errno to indicate what went wrong. */ extern int lock_packed_refs (int flags);
@@ -96,6 +97,7 @@ 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. +* On error, sets errno to indicate what went wrong. */ extern int commit_packed_refs (void );
@@ -135,7 +137,10 @@ extern int peel_ref(const char *refname, unsigned char *sha1);/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/ extern struct ref_lock *lock_ref_sha1 (const char *refname,const unsigned char *old_sha1); -/** Locks any ref (for 'HEAD' type refs). */ +/** +* Locks any ref (for 'HEAD' type refs). +* On error, returns NULL and sets errno to describe the error. +*/ #define REF_NODEREF 0x01 extern struct ref_lock *lock_any_ref_for_update(const char *refname, const unsigned char *old_sha1,
@@ -144,16 +149,25 @@ extern struct ref_lock *lock_any_ref_for_update(const char *refname,/** Close the file descriptor owned by a lock and return the status */ extern int close_ref (struct ref_lock *lock); -/** Close and commit the ref locked by the lock */ +/** +* Close and commit the ref locked by the lock. +* Sets errno on error. +*/ extern int commit_ref (struct ref_lock *lock);/** Release any lock taken but not written. **/ extern void unlock_ref (struct ref_lock *lock); -/** Writes sha1 into the ref specified by the lock. **/ +/** +* Writes sha1 into the ref specified by the lock. +* Sets errno on error. +*/ extern int write_ref_sha1 (struct ref_lock *lock,const unsigned char *sha1,const char *msg); -/** Setup reflog before using. **/ +/** +* Setup reflog before using. +* Sets errno on error. +*/ int log_ref_setup (const char *refname,char *logfile,int bufsize);/** Reads log for the value of ref during at_time. **/
Re: [PATCH v14 06/40] refs.c: add an err argument to repack_without_refs
Thanks.
Quoted 22 lines · Jonathan Nieder
Quoted 8 lines · Ronnie Sahlberg
Update
repack_without_refsto take an err argument and update it if there is a failure. Pass the err variable fromref_transaction_committo this function so that callers can print a meaningful error message if_commitfails due to a problem inrepack_without_refs.Add a new function
unable_to_lock_messagethat takes a strbuf argument and fills in the reason for the failure.Quoted 6 lines
In
commit_packed_refs, make sure that we propagate any errno thatcommit_lock_filemight have set back to our caller.Make sure both
commit_packed_refsandlock_filehas errno set to a meaningful value on error. Update a whole bunch of other places to keep errno from beeing clobbered.This patch is doing several (all nice) things. Are they connected? Would splitting some of them out into separate patches make sense or would it be too much fuss to be worth the trouble?
[...]
Quoted 12 lines
cache.h struct lock_file { · line 563
Quoted 8 lines
cache.h +2
-0@@ -559,6 +559,8 @@ struct lock_file {#define LOCK_DIE_ON_ERROR 1#define LOCK_NODEREF 2extern int unable_to_lock_error (const char *path,int err); +extern void unable_to_lock_message (const char *path,int err, +struct strbuf *buf);Introducing a new
unable_to_lock_messagehelper, which has nicer semantics thanunable_to_lock_errorand cleans uplockfile.ca little.
Done.
Quoted 22 lines
[...]
lockfile.c static char *resolve_symlink(char *p, size_t s) · line 125
Quoted 9 lines
lockfile.c +1
-1@@ -121,7 +121,7 @@ static char *resolve_symlink(char *p, size_t s)return p; } - +/* Make sure errno contains a meaningful value on error */ static int lock_file (struct lock_file *lk,const char *path,int flags)Making errno when returning from
lock_file()meaningful, which should fix* an existing almost-bug in
lock_ref_sha1_basicwhere it assumes errno==ENOENT is meaningful and could waste some work on retries* an existing bug in
repack_without_refswhere it prints strerror(errno) and picks advice based on errno, despite errno potentially being zero and potentially having been clobbered by that point
Quoted 6 lines
To make sure we don't lose these fixes, it would be helpful to also mention that errno is set in the API documentation for the relevant functions:
*
hold_lock_file_for_update(declared incache.h)
*lock_packed_refs(declared inrefs.h)
Done.
Quoted 12 lines
[...]
refs.c const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int rea
Quoted 3 lines
refs.c +0
-0@@ -1333,8 +1333,10 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reaMaking errno when returning from
resolve_ref_unsafe()meaningful, which should fix* a bug in
lock_ref_sha1_basic, where it assumes EISDIR means it failed due to a directory being in the way
Quoted 7 lines
To make sure we don't lose this fix, it would be helpful to change Michael's understated comment (why wasn't it marked with NEEDSWORK, btw?)
* errno is sometimes set on errors, but not always.
to describe the new guarantee.
Done.
Quoted 14 lines
[...]
refs.c static struct ref_lock *verify_lock(struct ref_lock *lock,
Quoted 1 line
refs.c +0
-0@@ -1908,14 +1918,17 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,Making errno when returning from
verify_lock()meaningful, which should almost but not completely fix* a bug in "git fetch"'s
s_update_ref, which trusts the result of an errno == ENOTDIR check to detect D/F conflictsTo make sure we don't lose this fix, it would be helpful to also mention that errno is set in the API documentation for the relevant functions:
Quoted 9 lines
*
lock_any_ref_for_update(declared inrefs.h), after handling thecheck_refname_formatcase
*lock_ref_sha1_basicENOTDIR makes sense as a sign that a file was in the way of a directory we wanted to create. Should "git fetch" also look for ENOTEMPTY or EEXIST to catch cases where a directory was in the way of a file to be created?
Done.
Quoted 6 lines
refs.c static int remove_empty_directories(const char *file)
Quoted 1 line
refs.c +0
-0@@ -1928,13 +1941,15 @@ static int remove_empty_directories(const char *file)Making errno when returning from
remove_empty_directories()more obviously meaningful, which should provide some peace of mind for people auditinglock_ref_sha1_basic.
Done.
Quoted 13 lines
[...]
Quoted 1 line
refs.c +0
-0@@ -2203,11 +2218,16 @@ int lock_packed_refs(int flags)[...]
Quoted 1 line
int
commit_packed_refs(void)Making errno when returning from
commit_packed_refs()meaningful, which should fix* a bug in "git clone" where it prints strerror(errno) based on errno, despite errno possibly being zero and potentially having been clobbered by that point
* the same kind of bug in "git pack-refs"
Quoted 8 lines
and prepares for
repack_without_refs()to get a meaningful error message whencommit_packed_refs()fails without falling into the same bug.To make sure we don't lose this fix, it would be helpful to also mention that errno is set in the API documentation for
commit_packed_refsinrefs.h.
Done.
Quoted 11 lines
[...]
refs.c static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data) · line 2453
Quoted 6 lines
refs.c +1
-1@@ -2427,12 +2450,12 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)return 0 ; } -static int repack_without_refs (const char **refnames,int n) +static int repack_without_refs (const char **refnames,int n,struct strbuf *err)Adding an 'err' argument to
repack_without_refsand writing to it on error.
Done.
Quoted 11 lines
[...]
refs.c int log_ref_setup(const char *refname, char *logfile, int bufsize)
Quoted 1 line
refs.c +0
-0@@ -2723,9 +2755,12 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize)Making errno when returning from
log_ref_setup()meaningful, presumably as preparation for making errno fromlog_ref_write()andwrite_ref_sha1()meaningful.To make sure we don't break it by mistake, it would be helpful to also mention that errno is set in the API documentation for
log_ref_setupinrefs.h(or to make that function static --- why is it public?).
Done.
Quoted 13 lines
[...]
refs.c static int log_ref_write(const char *refname, const unsigned char *old_sha1,
Quoted 1 line
refs.c +0
-0@@ -2784,8 +2826,19 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1,Making errno from
log_ref_write()meaningful.[...]
refs.c int write_ref_sha1(struct ref_lock *lock,
Quoted 1 line
refs.c +0
-0@@ -2800,8 +2853,10 @@ int write_ref_sha1(struct ref_lock *lock,Making errno from
write_ref_sha1()meaningful, which should fix* a bug in "git checkout -b" where it prints strerror(errno) despite errno possibly being zero or clobbered
Quoted 6 lines
* a bug in "git fetch"'s
s_update_ref, which trusts the result of an errno == ENOTDIR check to detect D/F conflictsTo make sure we don't lose these fixes, it would be helpful to also mention that errno is set in the API documentation for
write_ref_sha1inrefs.h.
Done.
Quoted 11 lines
So this could potentially be multiple patches:
1. Set errno in
lock_packed_refs, fixing a bug inrepack_without_refs
2. Set errno inresolve_ref_unsafe, fixing a longstanding TODO
3. Set errno inlock_any_ref_for_update, tightening half of the D/F check
4. Set errno incommit_packed_refs, fixing a bug in clone, packed-refs
5. Set errno inlog_ref_setup,log_ref_write, andwrite_ref_sha1, fixing a bug in 'checkout -b' and the other half of the D/F check
6. Introduceunable_to_lock_messagehelper
7. Add an 'err' argument torepack_without_refsand write to it.
I split it up into a bunch of patches.
Quoted 281 lines
cache.h +8
-1@@ -562,8 +562,10 @@ extern int unable_to_lock_error(const char *path, int err);extern void unable_to_lock_message (const char *path,int err,struct strbuf *buf); extern NORETURN void unable_to_lock_index_die(const char *path, int err); +/* Sets errno on error. */ extern int hold_lock_file_for_update (struct lock_file *,const char *path,int );extern int hold_lock_file_for_append (struct lock_file *,const char *path,int ); +/* Sets errno on error. */ extern int commit_lock_file (struct lock_file *);extern void update_index_if_able (struct index_state *,struct lock_file *);@@ -785,6 +787,11 @@ enum sharedrepo { PERM_EVERYBODY = 0664 };int git_config_perm (const char *var,const char *value); + +/* +* Widens permissions on 'path' to respect core.sharedrepository. +* Sets errno on error. +*/ int adjust_shared_perm (const char *path);/* @@ -979,7 +986,7 @@ extern int read_ref(const char *refname, unsigned char *sha1);* NULL. If more than MAXDEPTH recursive symbolic lookups are needed, * give up and return NULL. * -* errno is sometimes set on errors, but not always. +* On error, sets errno to indicate what went wrong. */ extern const char *resolve_ref_unsafe (const char *ref,unsigned char *sha1,int reading,int *flag);extern char *resolve_refdup (const char *ref,unsigned char *sha1,int reading,int *flag);dir.c +8
-4@@ -1527,7 +1527,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) {DIR *dir;struct dirent *e; -int ret =0 , original_len = path->len, len, kept_down =0 ; +int ret =0 , save_errno =0 ; +int original_len = path->len, len, kept_down =0 ;int only_empty = (flag &REMOVE_DIR_EMPTY_ONLY );int keep_toplevel = (flag &REMOVE_DIR_KEEP_TOPLEVEL );unsigned char submodule_head[20 ];@@ -1582,20 +1583,23 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) }/* path too long, stat fails, or non-directory still exists */ + save_errno = errno; ret =-1 ;break ; }closedir (dir);strbuf_setlen (path, original_len); -if (!ret && !keep_toplevel && !kept_down) - ret = (!rmdir (path->buf) || errno ==ENOENT ) ?0 :-1 ; -else if (kept_up) +if (!ret && !keep_toplevel && !kept_down) { +if (rmdir (path->buf) && errno !=ENOENT ) +return -1 ; + }else if (kept_up)/* * report the uplevel that it is not an error that we * did not rmdir() our directory. */ *kept_up = !ret; + errno = save_errno;return ret; }dir.h +1
-0@@ -185,6 +185,7 @@ extern int is_empty_dir(const char *dir);extern void setup_standard_excludes (struct dir_struct *dir); +/* sets errno on error */ #define REMOVE_DIR_EMPTY_ONLY 01#define REMOVE_DIR_KEEP_NESTED_GIT 02#define REMOVE_DIR_KEEP_TOPLEVEL 04refs.c +19
-10@@ -1295,6 +1295,8 @@ static struct ref_entry *get_packed_ref(const char *refname)/* * A loose ref file doesn't exist; check for a packed ref. The * options are forwarded from resolve_safe_unsafe(). +* +* Sets errno on error. */ static const char *handle_missing_loose_ref(const char *refname, unsigned char *sha1,@@ -1316,6 +1318,7 @@ static const char *handle_missing_loose_ref(const char *refname, }/* The reference is not a packed reference, either. */ if (reading) { + errno =ENOENT ;return NULL ; }else {hashclr (sha1);@@ -1413,7 +1416,7 @@ const char *resolve_ref_unsafe(const char *refname, unsigned char *sha1, int reaint save_errno = errno;close (fd); errno = save_errno; -return NULL ; +return NULL ; }close (fd);while (len &&isspace (buffer[len-1 ]))@@ -1949,9 +1952,9 @@ static int remove_empty_directories(const char *file) result =remove_dir_recursively (&path,REMOVE_DIR_EMPTY_ONLY ); save_errno = errno; - errno = save_errno;strbuf_release (&path); + errno = save_errno;return result; }@@ -2039,6 +2042,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)return logs_found; } +/* Sets errno on error. */ static struct ref_lock *lock_ref_sha1_basic (const char *refname,const unsigned char *old_sha1,int flags,int *type_p)@@ -2152,8 +2156,10 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,const unsigned char *old_sha1, int flags, int *type_p) { - if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) + if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { + errno =EINVAL ;return NULL ; + }return lock_ref_sha1_basic (refname, old_sha1, flags, type_p); }@@ -2218,10 +2224,6 @@ int lock_packed_refs(int flags)return 0 ; } -/* -* Commit the packed refs changes. -* On error we must make sure that errno contains a meaningful value. -*/ int commit_packed_refs (void ) { struct packed_ref_cache *packed_ref_cache =@@ -2776,7 +2778,7 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize) logfile); errno = save_errno;return -1 ; - } + } logfd =open (logfile, oflags,0666 ); }@@ -2794,6 +2796,7 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize)return 0 ; } +/* Sets errno on error. */ static int log_ref_write (const char *refname,const unsigned char *old_sha1,const unsigned char *new_sha1,const char *msg) {@@ -2824,7 +2827,9 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1, committer);if (msglen) len +=copy_msg (logrec + len -1 , msg) -1 ; - written = len <= maxlen ?write_in_full (logfd, logrec, len) :-1 ; +if (len > maxlen) +die ("BUG: log_ref_write buffer not large enough?" ); + written =write_in_full (logfd, logrec, len);free (logrec);if (written != len) {int save_errno = errno;@@ -2867,7 +2872,7 @@ int write_ref_sha1(struct ref_lock *lock, lock->ref_name, sha1_to_hex(sha1));unlock_ref (lock); errno =EINVAL ; -return -1 ; +return -1 ; }if (o->type !=OBJ_COMMIT &&is_branch (lock->ref_name)) { error("Trying to write non-commit object %s to branch %s",@@ -2889,7 +2894,9 @@ int write_ref_sha1(struct ref_lock *lock,if (log_ref_write (lock->ref_name, lock->old_sha1, sha1, logmsg) <0 || (strcmp (lock->ref_name, lock->orig_ref_name) &&log_ref_write (lock->orig_ref_name, lock->old_sha1, sha1, logmsg) <0 )) { +int save_errno = errno;unlock_ref (lock); + errno = save_errno;return -1 ; }if (strcmp (lock->orig_ref_name,"HEAD" ) !=0 ) {@@ -2914,8 +2921,10 @@ int write_ref_sha1(struct ref_lock *lock,log_ref_write ("HEAD" , lock->old_sha1, sha1, logmsg); }if (commit_ref (lock)) { +int save_errno = errno;error ("Couldn't set %s" , lock->ref_name);unlock_ref (lock); + errno = save_errno;return -1 ; }unlock_ref (lock);refs.h +18
-4@@ -81,6 +81,7 @@ extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refn/* * Lock the packed-refs file for writing. Flags is passed to * hold_lock_file_for_update(). Return 0 on success. +* On error, sets errno to indicate what went wrong. */ extern int lock_packed_refs (int flags);@@ -96,6 +97,7 @@ 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. +* On error, sets errno to indicate what went wrong. */ extern int commit_packed_refs (void );@@ -135,7 +137,10 @@ extern int peel_ref(const char *refname, unsigned char *sha1);/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/ extern struct ref_lock *lock_ref_sha1 (const char *refname,const unsigned char *old_sha1); -/** Locks any ref (for 'HEAD' type refs). */ +/** +* Locks any ref (for 'HEAD' type refs). +* On error, returns NULL and sets errno to describe the error. +*/ #define REF_NODEREF 0x01 extern struct ref_lock *lock_any_ref_for_update(const char *refname, const unsigned char *old_sha1,@@ -144,16 +149,25 @@ extern struct ref_lock *lock_any_ref_for_update(const char *refname,/** Close the file descriptor owned by a lock and return the status */ extern int close_ref (struct ref_lock *lock); -/** Close and commit the ref locked by the lock */ +/** +* Close and commit the ref locked by the lock. +* Sets errno on error. +*/ extern int commit_ref (struct ref_lock *lock);/** Release any lock taken but not written. **/ extern void unlock_ref (struct ref_lock *lock); -/** Writes sha1 into the ref specified by the lock. **/ +/** +* Writes sha1 into the ref specified by the lock. +* Sets errno on error. +*/ extern int write_ref_sha1 (struct ref_lock *lock,const unsigned char *sha1,const char *msg); -/** Setup reflog before using. **/ +/** +* Setup reflog before using. +* Sets errno on error. +*/ int log_ref_setup (const char *refname,char *logfile,int bufsize);/** Reads log for the value of ref during at_time. **/
PATCH v14 7/40 refs.c: make ref_update_reject_duplicates take a strbuf argument for errors
In reply to Ronnie SahlbergMake ref_update_reject_duplicates return any error that occurs through a new strbuf argument. This means that when a transaction commit fails in this function we will now be able to pass a helpful error message back to the caller.
--- refs.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
refs.c +5 -1
@@ -3456,6 +3456,7 @@ static int ref_update_compare(const void *r1, const void *r2) }static int ref_update_reject_duplicates (struct ref_update **updates,int n, +struct strbuf *err,enum action_on_err onerr) {int i;
@@ -3463,6 +3464,9 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,if (!strcmp (updates[i -1 ]->refname, updates[i]->refname)) {const char *str ="Multiple updates for ref '%s' not allowed." ; +if (err) +strbuf_addf (err, str, updates[i]->refname); +switch (onerr) {case UPDATE_REFS_MSG_ON_ERR :error (str, updates[i]->refname);break ;
@@ -3493,7 +3497,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,/* Copy, sort, and reject duplicate refs */ qsort (updates, n,sizeof (*updates), ref_update_compare); - ret =ref_update_reject_duplicates (updates, n, onerr); + ret =ref_update_reject_duplicates (updates, n, err, onerr);if (ret) goto cleanup;
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 8/40 refs.c: add an err argument to delete_ref_loose
In reply to Ronnie SahlbergAdd an err argument to delete_loose_ref so that we can pass a descriptive error string back to the caller. Pass the err argument from transaction commit to this function so that transaction users will have a nice error string if the transaction failed due to delete_loose_ref.
Add a new function unlink_or_err that we can call from delete_ref_loose. This function is similar to unlink_or_warn except that we can pass it an err argument. If err is non-NULL the function will populate err instead of printing a warning().
--- refs.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-)
refs.c +29 -6
@@ -2510,16 +2510,38 @@ static int repack_without_ref(const char *refname)return repack_without_refs (&refname,1 ,NULL ); } -static int delete_ref_loose (struct ref_lock *lock,int flag) +static int add_err_if_unremovable (const char *op,const char *file, +struct strbuf *e,int rc) +{ +int err = errno; +if (rc <0 && errno !=ENOENT ) { +strbuf_addf (e,"unable to %s %s: %s" , + op, file,strerror (errno)); + errno = err; +return -1 ; + } +return 0 ; +} + +static int unlink_or_err (const char *file,struct strbuf *err) +{ +if (err) +return add_err_if_unremovable ("unlink" , file, err, +unlink (file)); +else +return unlink_or_warn (file); +} + +static int delete_ref_loose (struct ref_lock *lock,int flag,struct strbuf *err) {if (!(flag &REF_ISPACKED ) || flag &REF_ISSYMREF ) {/* loose */ -int err, i =strlen (lock->lk->filename) -5 ;/* .lock */ +int res, i =strlen (lock->lk->filename) -5 ;/* .lock */ lock->lk->filename[i] =0 ; - err =unlink_or_warn (lock->lk->filename); + res =unlink_or_err (lock->lk->filename, err); lock->lk->filename[i] ='.' ; -if (err && errno !=ENOENT ) +if (res && errno !=ENOENT )return 1 ; }return 0 ;
@@ -2533,7 +2555,7 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt) lock =lock_ref_sha1_basic (refname, sha1, delopt, &flag);if (!lock)return 1 ; - ret |=delete_ref_loose (lock, flag); + ret |=delete_ref_loose (lock, flag,NULL );/* removing the loose one could have resurrected an earlier * packed one. Also, if it was not loose we need to repack
@@ -3540,7 +3562,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,if (update->lock) { delnames[delnum++] = update->lock->ref_name; - ret |=delete_ref_loose (update->lock, update->type); + ret |=delete_ref_loose (update->lock, update->type, + err); } }
Signature · 1 line
2.0.0.582.ge25c160
Re: [PATCH v14 08/40] refs.c: add an err argument to delete_ref_loose
Quoted 3 lines · Ronnie Sahlberg
refs.c +0
-0@@ -2510,16 +2510,38 @@ static int repack_without_ref(const char *refname)
[...]
refs.c
Quoted 8 lines
refs.c +8
-0+static int unlink_or_err (const char *file,struct strbuf *err) +{ +if (err) +return add_err_if_unremovable ("unlink" , file, err, +unlink (file)); +else +return unlink_or_warn (file); +}
This has two different return value conventions (my fault, I know): it returns -1 for ENOENT when err == NULL but 0 for ENOENT when not. The usual reason to call unlink_or_warn / remove_or_warn is to be silent about ENOENT so it seems simplest to fix them at the same time, as below. The only callers that could mind:
* unpack-trees.c::unlink_entry, where this would be fixing a minor bug
* builtin/apply.c::remove_file, likewise
* delete_ref_loose, which checks errno so this should have no effect there
refs.c | 2 +- wrapper.c | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-)
refs.c +1 -1
@@ -2541,7 +2541,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err) lock->lk->filename[i] =0 ; res =unlink_or_err (lock->lk->filename, err); lock->lk->filename[i] ='.' ; -if (res && errno !=ENOENT ) +if (res)return 1 ; }return 0 ;
wrapper.c +6 -8
@@ -429,14 +429,12 @@ int xmkstemp_mode(char *template, int mode)static int warn_if_unremovable (const char *op,const char *file,int rc) { -if (rc <0 ) { -int err = errno; -if (ENOENT != err) { -warning ("unable to %s %s: %s" , - op, file,strerror (errno)); - errno = err; - } - } +int err; +if (!rc || errno ==ENOENT ) +return 0 ; + err = errno; +warning ("unable to %s %s: %s" , op, file,strerror (errno)); + errno = err;return rc; }
Re: [PATCH v14 08/40] refs.c: add an err argument to delete_ref_loose
Done.
Thanks.
Quoted 67 lines · Jonathan Nieder
Quoted 3 lines · Ronnie Sahlberg
refs.c +0
-0@@ -2510,16 +2510,38 @@ static int repack_without_ref(const char *refname)[...]
refs.c
Quoted 8 lines
refs.c +8
-0+static int unlink_or_err (const char *file,struct strbuf *err) +{ +if (err) +return add_err_if_unremovable ("unlink" , file, err, +unlink (file)); +else +return unlink_or_warn (file); +}This has two different return value conventions (my fault, I know): it returns -1 for ENOENT when err == NULL but 0 for ENOENT when not. The usual reason to call
unlink_or_warn/remove_or_warnis to be silent about ENOENT so it seems simplest to fix them at the same time, as below. The only callers that could mind:* unpack-trees.c::unlink_entry, where this would be fixing a minor bug
* builtin/apply.c::remove_file, likewise
*delete_ref_loose, which checks errno so this should have no effect thererefs.c | 2 +- wrapper.c | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-)refs.c +1
-1@@ -2541,7 +2541,7 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err) lock->lk->filename[i] =0 ; res =unlink_or_err (lock->lk->filename, err); lock->lk->filename[i] ='.' ; -if (res && errno !=ENOENT ) +if (res)return 1 ; }return 0 ;wrapper.c +6
-8@@ -429,14 +429,12 @@ int xmkstemp_mode(char *template, int mode)static int warn_if_unremovable (const char *op,const char *file,int rc) { -if (rc <0 ) { -int err = errno; -if (ENOENT != err) { -warning ("unable to %s %s: %s" , - op, file,strerror (errno)); - errno = err; - } - } +int err; +if (!rc || errno ==ENOENT ) +return 0 ; + err = errno; +warning ("unable to %s %s: %s" , op, file,strerror (errno)); + errno = err;return rc; }
PATCH v14 9/40 refs.c: make update_ref_write update a strbuf on failure
In reply to Ronnie SahlbergChange update_ref_write to also update an error strbuf on failure. This makes the error available to ref_transaction_commit callers if the transaction failed due to update_ref_sha1/write_ref_sha1 failures.
--- refs.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
refs.c +6 -3
@@ -3343,10 +3343,13 @@ static struct ref_lock *update_ref_lock(const char *refname,static int update_ref_write (const char *action,const char *refname,const unsigned char *sha1,struct ref_lock *lock, -enum action_on_err onerr) +struct strbuf *err,enum action_on_err onerr) {if (write_ref_sha1 (lock, sha1, action) <0 ) {const char *str ="Cannot update the ref '%s'." ; +if (err) +strbuf_addf (err, str, refname); +switch (onerr) {case UPDATE_REFS_MSG_ON_ERR :error (str, refname);break ;case UPDATE_REFS_DIE_ON_ERR :die (str, refname);break ;
@@ -3467,7 +3470,7 @@ int update_ref(const char *action, const char *refname, lock =update_ref_lock (refname, oldval, flags,NULL , onerr);if (!lock)return 1 ; -return update_ref_write (action, refname, sha1, lock, onerr); +return update_ref_write (action, refname, sha1, lock,NULL , onerr); }static int ref_update_compare (const void *r1,const void *r2)
@@ -3549,7 +3552,7 @@ int ref_transaction_commit(struct ref_transaction *transaction, ret =update_ref_write (msg, update->refname, update->new_sha1, - update->lock, onerr); + update->lock, err, onerr); update->lock =NULL ;/* freed by update_ref_write */ if (ret) goto cleanup;
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 10/40 update-ref: use err argument to get error from ref_transaction_commit
In reply to Ronnie SahlbergCall ref_transaction_commit with QUIET_ON_ERR and use the strbuf that is returned to print a log message if/after the transaction fails.
--- builtin/update-ref.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
builtin/update-ref.c +5 -5
@@ -342,6 +342,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)const char *refname, *oldval, *msg =NULL ;unsigned char sha1[20 ], oldsha1[20 ];int delete =0 , no_deref =0 , read_stdin =0 , end_null =0 , flags =0 ; +struct strbuf err =STRBUF_INIT ;struct option options[] = {OPT_STRING ('m' ,NULL , &msg,N_ ("reason" ),N_ ("reason of the update" )),OPT_BOOL ('d' ,NULL , &delete,N_ ("delete the reference" )),
@@ -359,18 +360,17 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)die ("Refusing to perform update with empty message." );if (read_stdin) { -int ret; transaction =ref_transaction_begin (); -if (delete || no_deref || argc >0 )usage_with_options (git_update_ref_usage, options);if (end_null) line_termination ='\0' ;update_refs_stdin (); - ret =ref_transaction_commit (transaction, msg,NULL , -UPDATE_REFS_DIE_ON_ERR ); +if (ref_transaction_commit (transaction, msg, &err, +UPDATE_REFS_QUIET_ON_ERR )) +die ("%s" , err.buf);ref_transaction_free (transaction); -return ret; +return 0 ; }if (end_null)
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 11/40 refs.c: remove the onerr argument to ref_transaction_commit
In reply to Ronnie SahlbergSince all callers now use QUIET_ON_ERR we no longer need to provide an onerr argument any more. Remove the onerr argument from the ref_transaction_commit signature.
--- builtin/update-ref.c | 3 +-- refs.c | 22 +++++++--------------- refs.h | 3 +-- 3 files changed, 9 insertions(+), 19 deletions(-)
builtin/update-ref.c +1 -2
@@ -366,8 +366,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)if (end_null) line_termination ='\0' ;update_refs_stdin (); -if (ref_transaction_commit (transaction, msg, &err, -UPDATE_REFS_QUIET_ON_ERR )) +if (ref_transaction_commit (transaction, msg, &err))die ("%s" , err.buf);ref_transaction_free (transaction);return 0 ;
refs.c +7 -15
@@ -3481,8 +3481,7 @@ static int ref_update_compare(const void *r1, const void *r2) }static int ref_update_reject_duplicates (struct ref_update **updates,int n, -struct strbuf *err, -enum action_on_err onerr) +struct strbuf *err) {int i;for (i =1 ; i < n; i++)
@@ -3492,22 +3491,13 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,if (err)strbuf_addf (err, str, updates[i]->refname); -switch (onerr) { -case UPDATE_REFS_MSG_ON_ERR : -error (str, updates[i]->refname);break ; -case UPDATE_REFS_DIE_ON_ERR : -die (str, updates[i]->refname);break ; -case UPDATE_REFS_QUIET_ON_ERR : -break ; - }return 1 ; }return 0 ; }int ref_transaction_commit (struct ref_transaction *transaction, -const char *msg,struct strbuf *err, -enum action_on_err onerr) +const char *msg,struct strbuf *err) {int ret =0 , delnum =0 , i;const char **delnames;
@@ -3522,7 +3512,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,/* Copy, sort, and reject duplicate refs */ qsort (updates, n,sizeof (*updates), ref_update_compare); - ret =ref_update_reject_duplicates (updates, n, err, onerr); + ret =ref_update_reject_duplicates (updates, n, err);if (ret) goto cleanup;
@@ -3534,7 +3524,8 @@ int ref_transaction_commit(struct ref_transaction *transaction, (update->have_old ? update->old_sha1 :NULL ), update->flags, - &update->type, onerr); + &update->type, + UPDATE_REFS_QUIET_ON_ERR);if (!update->lock) {if (err) strbuf_addf(err, "Cannot lock the ref '%s'.",
@@ -3552,7 +3543,8 @@ int ref_transaction_commit(struct ref_transaction *transaction, ret =update_ref_write (msg, update->refname, update->new_sha1, - update->lock, err, onerr); + update->lock, err, +UPDATE_REFS_QUIET_ON_ERR ); update->lock =NULL ;/* freed by update_ref_write */ if (ret) goto cleanup;
refs.h +1 -2
@@ -270,8 +270,7 @@ void ref_transaction_delete(struct ref_transaction *transaction,* the transaction failed. The string does not end in newline. */ int ref_transaction_commit (struct ref_transaction *transaction, -const char *msg,struct strbuf *err, -enum action_on_err onerr); +const char *msg,struct strbuf *err);/* * Free an existing transaction and all associated data.
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 12/40 refs.c: change ref_transaction_update() to do error checking and return status
In reply to Ronnie SahlbergUpdate ref_transaction_update() do some basic error checking and return non-zero on error. Update all callers to check ref_transaction_update() for error. There are currently no conditions in _update that will return error but there will be in the future. Add an err argument that will be updated on failure. In future patches we will start doing both locking and checking for name conflicts in _update instead of _commit at which time this function will start returning errors for these conditions.
Also check for BUGs during update and die(BUG:...) if we are calling _update with have_old but the old_sha1 pointer is NULL.
--- builtin/update-ref.c | 12 +++++++----- refs.c | 18 ++++++++++++------ refs.h | 14 +++++++++----- 3 files changed, 28 insertions(+), 16 deletions(-)
builtin/update-ref.c +7 -5
@@ -16,6 +16,7 @@ static struct ref_transaction *transaction;static char line_termination ='\n' ;static int update_flags; +static struct strbuf err =STRBUF_INIT ;/* * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
@@ -197,8 +198,9 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)if (*next != line_termination)die ("update %s: extra input: %s" , refname, next); -ref_transaction_update (transaction, refname, new_sha1, old_sha1, - update_flags, have_old); +if (ref_transaction_update (transaction, refname, new_sha1, old_sha1, + update_flags, have_old, &err)) +die ("%s" , err.buf); update_flags =0 ;free (refname);
@@ -286,8 +288,9 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)if (*next != line_termination)die ("verify %s: extra input: %s" , refname, next); -ref_transaction_update (transaction, refname, new_sha1, old_sha1, - update_flags, have_old); +if (ref_transaction_update (transaction, refname, new_sha1, old_sha1, + update_flags, have_old, &err)) +die ("%s" , err.buf); update_flags =0 ;free (refname);
@@ -342,7 +345,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)const char *refname, *oldval, *msg =NULL ;unsigned char sha1[20 ], oldsha1[20 ];int delete =0 , no_deref =0 , read_stdin =0 , end_null =0 , flags =0 ; -struct strbuf err =STRBUF_INIT ;struct option options[] = {OPT_STRING ('m' ,NULL , &msg,N_ ("reason" ),N_ ("reason of the update" )),OPT_BOOL ('d' ,NULL , &delete,N_ ("delete the reference" )),
refs.c +12 -6
@@ -3418,19 +3418,25 @@ static struct ref_update *add_update(struct ref_transaction *transaction,return update; } -void ref_transaction_update (struct ref_transaction *transaction, -const char *refname, -const unsigned char *new_sha1, -const unsigned char *old_sha1, -int flags,int have_old) +int ref_transaction_update (struct ref_transaction *transaction, +const char *refname, +const unsigned char *new_sha1, +const unsigned char *old_sha1, +int flags,int have_old, +struct strbuf *err) { -struct ref_update *update =add_update (transaction, refname); +struct ref_update *update; + +if (have_old && !old_sha1) +die ("BUG: have_old is true but old_sha1 is NULL" ); + update =add_update (transaction, refname);hashcpy (update->new_sha1, new_sha1); update->flags = flags; update->have_old = have_old;if (have_old)hashcpy (update->old_sha1, old_sha1); +return 0 ; } void ref_transaction_create(struct ref_transaction *transaction,
refs.h +9 -5
@@ -234,12 +234,16 @@ struct ref_transaction *ref_transaction_begin(void);* be deleted. If have_old is true, then old_sha1 holds the value * that the reference should have had before the update, or zeros if * it must not have existed beforehand. +* Function returns 0 on success and non-zero on failure. A failure to update +* means that the transaction as a whole has failed and will need to be +* rolled back. On failure the err buffer will be updated. */ -void ref_transaction_update (struct ref_transaction *transaction, -const char *refname, -const unsigned char *new_sha1, -const unsigned char *old_sha1, -int flags,int have_old); +int ref_transaction_update (struct ref_transaction *transaction, +const char *refname, +const unsigned char *new_sha1, +const unsigned char *old_sha1, +int flags,int have_old, +struct strbuf *err);/* * Add a reference creation to transaction. new_sha1 is the value
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 13/40 refs.c: change ref_transaction_create to do error checking and return status
In reply to Ronnie SahlbergDo basic error checking in ref_transaction_create() and make it return non-zero on error. Update all callers to check the result of ref_transaction_create(). There are currently no conditions in _create that will return error but there will be in the future. Add an err argument that will be updated on failure.
--- builtin/update-ref.c | 4 +++- refs.c | 18 +++++++++++------ refs.h | 55 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 63 insertions(+), 14 deletions(-)
builtin/update-ref.c +3 -1
@@ -226,7 +226,9 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)if (*next != line_termination)die ("create %s: extra input: %s" , refname, next); -ref_transaction_create (transaction, refname, new_sha1, update_flags); +if (ref_transaction_create (transaction, refname, new_sha1, + update_flags, &err)) +die ("%s" , err.buf); update_flags =0 ;free (refname);
refs.c +12 -6
@@ -3439,18 +3439,24 @@ int ref_transaction_update(struct ref_transaction *transaction,return 0 ; } -void ref_transaction_create (struct ref_transaction *transaction, -const char *refname, -const unsigned char *new_sha1, -int flags) +int ref_transaction_create (struct ref_transaction *transaction, +const char *refname, +const unsigned char *new_sha1, +int flags, +struct strbuf *err) { -struct ref_update *update =add_update (transaction, refname); +struct ref_update *update; + +if (!new_sha1 ||is_null_sha1 (new_sha1)) +die ("BUG: create ref with null new_sha1" ); + + update =add_update (transaction, refname); -assert (!is_null_sha1 (new_sha1));hashcpy (update->new_sha1, new_sha1);hashclr (update->old_sha1); update->flags = flags; update->have_old =1 ; +return 0 ; } void ref_transaction_delete(struct ref_transaction *transaction,
refs.h +48 -7
80 lines not on this page · raw message
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 14/40 refs.c: update ref_transaction_delete to check for error and return status
In reply to Ronnie SahlbergChange ref_transaction_delete() to do basic error checking and return non-zero of error. Update all callers to check the return for ref_transaction_delete(). There are currently no conditions in _delete that will return error but there will be in the future. Add an err argument that will be updated on failure.
--- builtin/update-ref.c | 5 +++-- refs.c | 16 +++++++++++----- refs.h | 12 ++++++++---- 3 files changed, 22 insertions(+), 11 deletions(-)
builtin/update-ref.c +3 -2
@@ -258,8 +258,9 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)if (*next != line_termination)die ("delete %s: extra input: %s" , refname, next); -ref_transaction_delete (transaction, refname, old_sha1, - update_flags, have_old); +if (ref_transaction_delete (transaction, refname, old_sha1, + update_flags, have_old, &err)) +die ("%s" , err.buf); update_flags =0 ;free (refname);
refs.c +11 -5
@@ -3459,19 +3459,25 @@ int ref_transaction_create(struct ref_transaction *transaction,return 0 ; } -void ref_transaction_delete (struct ref_transaction *transaction, -const char *refname, -const unsigned char *old_sha1, -int flags,int have_old) +int ref_transaction_delete (struct ref_transaction *transaction, +const char *refname, +const unsigned char *old_sha1, +int flags,int have_old, +struct strbuf *err) { -struct ref_update *update =add_update (transaction, refname); +struct ref_update *update; +if (have_old && !old_sha1) +die ("BUG: have_old is true but old_sha1 is NULL" ); + + update =add_update (transaction, refname); update->flags = flags; update->have_old = have_old;if (have_old) {assert (!is_null_sha1 (old_sha1));hashcpy (update->old_sha1, old_sha1); } +return 0 ; } int update_ref(const char *action, const char *refname,
refs.h +8 -4
@@ -303,11 +303,15 @@ int ref_transaction_create(struct ref_transaction *transaction,* Add a reference deletion to transaction. If have_old is true, then * old_sha1 holds the value that the reference should have had before * the update (which must not be the null SHA-1). +* Function returns 0 on success and non-zero on failure. A failure to delete +* means that the transaction as a whole has failed and will need to be +* rolled back. */ -void ref_transaction_delete (struct ref_transaction *transaction, -const char *refname, -const unsigned char *old_sha1, -int flags,int have_old); +int ref_transaction_delete (struct ref_transaction *transaction, +const char *refname, +const unsigned char *old_sha1, +int flags,int have_old, +struct strbuf *err);/* * Commit all of the changes that have been queued in transaction, as
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 15/40 refs.c: make ref_transaction_begin take an err argument
In reply to Ronnie SahlbergAdd an err argument to _begin so that on non-fatal failures in future ref backends we can report a nice error back to the caller.
While _begin can currently never fail for other reasons than OOM, in which case we die() anyway, we may add other types of backends in the future. For example, a hypothetical MySQL backend could fail in _being with "Can not connect to MySQL server. No route to host".
--- builtin/update-ref.c | 2 +- refs.c | 2 +- refs.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
builtin/update-ref.c +1 -1
@@ -365,7 +365,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)die ("Refusing to perform update with empty message." );if (read_stdin) { - transaction =ref_transaction_begin (); + transaction =ref_transaction_begin (&err);if (delete || no_deref || argc >0 )usage_with_options (git_update_ref_usage, options);if (end_null)
refs.c +1 -1
@@ -3387,7 +3387,7 @@ struct ref_transaction {size_t nr; }; -struct ref_transaction *ref_transaction_begin (void ) +struct ref_transaction *ref_transaction_begin (struct strbuf *err) {return xcalloc (1 ,sizeof (struct ref_transaction )); }
refs.h +1 -1
@@ -257,7 +257,7 @@ enum action_on_err {* Begin a reference transaction. The reference transaction must * be freed by calling ref_transaction_free(). */ -struct ref_transaction *ref_transaction_begin (void ); +struct ref_transaction *ref_transaction_begin (struct strbuf *err);/* * The following functions add a reference check or update to a
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 16/40 refs.c: add transaction.status and track OPEN/CLOSED/ERROR
In reply to Ronnie SahlbergTrack the status of a transaction in a new status field. Check the field for sanity, i.e. that status must be OPEN when _commit/_create/_delete or _update is called or else die(BUG:...)
--- refs.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-)
refs.c +39 -1
@@ -3377,6 +3377,25 @@ struct ref_update { };/* +* Transaction states. +* OPEN: The transaction is in a valid state and can accept new updates. +* An OPEN transaction can be committed. +* CLOSED: If an open transaction is successfully committed the state will +* change to CLOSED. No further changes can be made to a CLOSED +* transaction. +* CLOSED means that all updates have been successfully committed and +* the only thing that remains is to free the completed transaction. +* ERROR: The transaction has failed and is no longer committable. +* No further changes can be made to a CLOSED transaction and it must +* be rolled back using transaction_free. +*/ +enum ref_transaction_state { +REF_TRANSACTION_OPEN =0 , +REF_TRANSACTION_CLOSED =1 , +REF_TRANSACTION_ERROR =2 , +}; + +/* * Data structure for holding a reference transaction, which can * consist of checks and updates to multiple references, carried out * as atomically as possible. This structure is opaque to callers.
@@ -3385,6 +3404,8 @@ struct ref_transaction {struct ref_update **updates;size_t alloc;size_t nr; +enum ref_transaction_state state; +int status; };struct ref_transaction *ref_transaction_begin (struct strbuf *err)
@@ -3427,6 +3448,9 @@ int ref_transaction_update(struct ref_transaction *transaction, {struct ref_update *update; +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: update called for transaction that is not open" ); +if (have_old && !old_sha1)die ("BUG: have_old is true but old_sha1 is NULL" );
@@ -3447,6 +3471,9 @@ int ref_transaction_create(struct ref_transaction *transaction, {struct ref_update *update; +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: create called for transaction that is not open" ); +if (!new_sha1 ||is_null_sha1 (new_sha1))die ("BUG: create ref with null new_sha1" );
@@ -3467,6 +3494,9 @@ int ref_transaction_delete(struct ref_transaction *transaction, {struct ref_update *update; +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: delete called for transaction that is not open" ); +if (have_old && !old_sha1)die ("BUG: have_old is true but old_sha1 is NULL" );
@@ -3522,8 +3552,13 @@ int ref_transaction_commit(struct ref_transaction *transaction,int n = transaction->nr;struct ref_update **updates = transaction->updates; -if (!n) +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: commit called for transaction that is not open" ); + +if (!n) { + transaction->state =REF_TRANSACTION_CLOSED ;return 0 ; + }/* Allocate work space */ delnames =xmalloc (sizeof (*delnames) * n);
@@ -3586,6 +3621,9 @@ int ref_transaction_commit(struct ref_transaction *transaction,clear_loose_ref_cache (&ref_cache); cleanup: + transaction->state = ret ?REF_TRANSACTION_ERROR + :REF_TRANSACTION_CLOSED ; +for (i =0 ; i < n; i++)if (updates[i]->lock)unlock_ref (updates[i]->lock);
Signature · 1 line
2.0.0.582.ge25c160
Re: [PATCH v14 16/40] refs.c: add transaction.status and track OPEN/CLOSED/ERROR
Quoted 4 lines · Ronnie Sahlberg
Signed-off-by Ronnie Sahlberg <[email protected]>--- refs.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-)
This makes the API pretty strict, which should make it easier to experiment later if operations on closed transactions turn out to be useful.
PATCH v14 17/40 tag.c: use ref transactions when doing updates
In reply to Ronnie SahlbergChange tag.c to use ref transactions for all ref updates.
--- builtin/tag.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
builtin/tag.c +9 -6
@@ -548,7 +548,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)struct strbuf ref =STRBUF_INIT ;unsigned char object[20 ], prev[20 ];const char *object_ref, *tag; -struct ref_lock *lock;struct create_tag_options opt;char *cleanup_arg =NULL ;int annotate =0 , force =0 , lines =-1 ;
@@ -556,6 +555,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)const char *msgfile =NULL , *keyid =NULL ;struct msg_arg msg = {0 ,STRBUF_INIT };struct commit_list *with_commit =NULL ; +struct ref_transaction *transaction; +struct strbuf err =STRBUF_INIT ;struct option options[] = {OPT_CMDMODE ('l' ,"list" , &cmdmode,N_ ("list tag names" ),'l' ), {OPTION_INTEGER ,'n' ,NULL , &lines,N_ ("n" ),
@@ -701,11 +702,13 @@ int cmd_tag(int argc, const char **argv, const char *prefix)if (annotate)create_tag (object, tag, &buf, &opt, prev, object); - lock =lock_any_ref_for_update (ref.buf, prev,0 ,NULL ); -if (!lock) -die (_ ("%s: cannot lock the ref" ), ref.buf); -if (write_ref_sha1 (lock, object,NULL ) <0 ) -die (_ ("%s: cannot update the ref" ), ref.buf); + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, ref.buf, object, prev, +0 , !is_null_sha1 (prev), &err) || +ref_transaction_commit (transaction,NULL , &err)) +die ("%s" , err.buf); +ref_transaction_free (transaction);if (force && !is_null_sha1 (prev) &&hashcmp (prev, object))printf (_ ("Updated tag '%s' (was %s)\n" ), tag,find_unique_abbrev (prev,DEFAULT_ABBREV ));
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 18/40 replace.c: use the ref transaction functions for updates
In reply to Ronnie SahlbergUpdate replace.c to use ref transactions for updates.
--- builtin/replace.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
builtin/replace.c +9 -6
@@ -154,7 +154,8 @@ static int replace_object_sha1(const char *object_ref,unsigned char prev[20 ];enum object_type obj_type, repl_type;char ref[PATH_MAX ]; -struct ref_lock *lock; +struct ref_transaction *transaction; +struct strbuf err =STRBUF_INIT ; obj_type =sha1_object_info (object,NULL ); repl_type =sha1_object_info (repl,NULL );
@@ -167,12 +168,14 @@ static int replace_object_sha1(const char *object_ref,check_ref_valid (object, prev, ref,sizeof (ref), force); - lock =lock_any_ref_for_update (ref, prev,0 ,NULL ); -if (!lock) -die ("%s: cannot lock the ref" , ref); -if (write_ref_sha1 (lock, repl,NULL ) <0 ) -die ("%s: cannot update the ref" , ref); + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, ref, repl, prev, +0 , !is_null_sha1 (prev), &err) || +ref_transaction_commit (transaction,NULL , &err)) +die ("%s" , err.buf); +ref_transaction_free (transaction);return 0 ; }
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 19/40 commit.c: use ref transactions for updates
In reply to Ronnie SahlbergChange commit.c to use ref transactions for all ref updates. Make sure we pass a NULL pointer to ref_transaction_update if have_old is false.
--- builtin/commit.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
builtin/commit.c +11 -13
@@ -1613,11 +1613,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)const char *index_file, *reflog_msg;char *nl;unsigned char sha1[20 ]; -struct ref_lock *ref_lock;struct commit_list *parents =NULL , **pptr = &parents;struct stat statbuf;struct commit *current_head =NULL ;struct commit_extra_header *extra =NULL ; +struct ref_transaction *transaction; +struct strbuf err =STRBUF_INIT ;if (argc ==2 && !strcmp (argv[1 ],"-h" ))usage_with_options (builtin_commit_usage, builtin_commit_options);
@@ -1739,16 +1740,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)strbuf_release (&author_ident);free_commit_extra_headers (extra); - ref_lock =lock_any_ref_for_update ("HEAD" , - !current_head - ?NULL - : current_head->object.sha1, -0 ,NULL ); -if (!ref_lock) { -rollback_index_files (); -die (_ ("cannot lock HEAD ref" )); - } - nl =strchr (sb.buf,'\n' );if (nl)strbuf_setlen (&sb, nl +1 - sb.buf);
@@ -1757,10 +1748,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)strbuf_insert (&sb,0 , reflog_msg,strlen (reflog_msg));strbuf_insert (&sb,strlen (reflog_msg),": " ,2 ); -if (write_ref_sha1 (ref_lock, sha1, sb.buf) <0 ) { + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction,"HEAD" , sha1, + current_head ? + current_head->object.sha1 :NULL , +0 , !!current_head, &err) || +ref_transaction_commit (transaction, sb.buf, &err)) {rollback_index_files (); -die (_ ("cannot update HEAD ref" )); +die ("%s" , err.buf); } +ref_transaction_free (transaction);unlink (git_path ("CHERRY_PICK_HEAD" ));unlink (git_path ("REVERT_HEAD" ));
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 20/40 sequencer.c: use ref transactions for all ref updates
In reply to Ronnie SahlbergChange to use ref transactions for all updates to refs.
--- sequencer.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
sequencer.c +16 -8
@@ -272,23 +272,31 @@ static int error_dirty_index(struct replay_opts *opts)static int fast_forward_to (const unsigned char *to,const unsigned char *from,int unborn,struct replay_opts *opts) { -struct ref_lock *ref_lock; +struct ref_transaction *transaction;struct strbuf sb =STRBUF_INIT ; -int ret; +struct strbuf err =STRBUF_INIT ;read_cache ();if (checkout_fast_forward (from, to,1 ))exit (1 );/* the callee should have complained already */ - ref_lock =lock_any_ref_for_update ("HEAD" , unborn ? null_sha1 : from, -0 ,NULL ); -if (!ref_lock) -return error (_ ("Failed to lock HEAD during fast_forward_to" ));strbuf_addf (&sb,"%s: fast-forward" ,action_name (opts)); - ret =write_ref_sha1 (ref_lock, to, sb.buf); + + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction,"HEAD" , to, from, +0 , !unborn, &err) || +ref_transaction_commit (transaction, sb.buf, &err)) { +ref_transaction_free (transaction); +error ("%s" , err.buf); +strbuf_release (&sb); +strbuf_release (&err); +return -1 ; + }strbuf_release (&sb); -return ret; +ref_transaction_free (transaction); +return 0 ; } static int do_recursive_merge(struct commit *base, struct commit *next,
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 21/40 fast-import.c: change update_branch to use ref transactions
In reply to Ronnie SahlbergChange update_branch() to use ref transactions for updates.
--- fast-import.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
fast-import.c +15 -10
@@ -1679,39 +1679,44 @@ found_entry:static int update_branch (struct branch *b) {static const char *msg ="fast-import" ; -struct ref_lock *lock; +struct ref_transaction *transaction;unsigned char old_sha1[20 ]; +struct strbuf err =STRBUF_INIT ;if (read_ref (b->name, old_sha1))hashclr (old_sha1); +if (is_null_sha1 (b->sha1)) {if (b->delete)delete_ref (b->name, old_sha1,0 );return 0 ; } - lock =lock_any_ref_for_update (b->name, old_sha1,0 ,NULL ); -if (!lock) -return error ("Unable to lock %s" , b->name);if (!force_update && !is_null_sha1 (old_sha1)) {struct commit *old_cmit, *new_cmit; old_cmit =lookup_commit_reference_gently (old_sha1,0 ); new_cmit =lookup_commit_reference_gently (b->sha1,0 ); -if (!old_cmit || !new_cmit) { -unlock_ref (lock); +if (!old_cmit || !new_cmit)return error ("Branch %s is missing commits." , b->name); - }if (!in_merge_bases (old_cmit, new_cmit)) { -unlock_ref (lock);warning ("Not updating %s" " (new tip %s does not contain %s)" , b->name,sha1_to_hex (b->sha1),sha1_to_hex (old_sha1));return -1 ; } } -if (write_ref_sha1 (lock, b->sha1, msg) <0 ) -return error ("Unable to update %s" , b->name); + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, b->name, b->sha1, old_sha1, +0 ,1 , &err) || +ref_transaction_commit (transaction, msg, &err)) { +ref_transaction_free (transaction); +error ("%s" , err.buf); +strbuf_release (&err); +return -1 ; + } +ref_transaction_free (transaction);return 0 ; }
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 22/40 branch.c: use ref transaction for all ref updates
In reply to Ronnie SahlbergChange create_branch to use a ref transaction when creating the new branch.
This also fixes a race condition in the old code where two concurrent create_branch could race since the lock_any_ref_for_update/write_ref_sha1 did not protect against the ref already existing. I.e. one thread could end up overwriting a branch even if the forcing flag is false.
--- branch.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)
branch.c +16 -14
@@ -226,7 +226,6 @@ void create_branch(const char *head, int force, int reflog, int clobber_head, int quiet, enum branch_track track) { - struct ref_lock *lock = NULL; struct commit *commit;unsigned char sha1[20 ];char *real_ref, msg[PATH_MAX +20 ];
@@ -285,15 +284,6 @@ void create_branch(const char *head,die (_ ("Not a valid branch point: '%s'." ), start_name);hashcpy (sha1, commit->object.sha1); -if (!dont_change_ref) { - lock =lock_any_ref_for_update (ref.buf,NULL ,0 ,NULL ); -if (!lock) -die_errno (_ ("Failed to lock ref for update" )); - } - -if (reflog) - log_all_ref_updates =1 ; -if (forcing)snprintf (msg,sizeof msg,"branch: Reset to %s" , start_name);
@@ -301,13 +291,25 @@ void create_branch(const char *head,snprintf (msg,sizeof msg,"branch: Created from %s" , start_name); +if (reflog) + log_all_ref_updates =1 ; + +if (!dont_change_ref) { +struct ref_transaction *transaction; +struct strbuf err =STRBUF_INIT ; + + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, ref.buf, sha1, + null_sha1,0 , !forcing, &err) || +ref_transaction_commit (transaction, msg, &err)) +die ("%s" , err.buf); +ref_transaction_free (transaction); + } +if (real_ref && track)setup_tracking (ref.buf +11 , real_ref, track, quiet); -if (!dont_change_ref) -if (write_ref_sha1 (lock, sha1, msg) <0 ) -die_errno (_ ("Failed to write ref" )); -strbuf_release (&ref);free (real_ref); }
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 23/40 refs.c: change update_ref to use a transaction
In reply to Ronnie SahlbergChange the update_ref helper function to use a ref transaction internally.
--- refs.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
refs.c +21 -4
@@ -3514,11 +3514,28 @@ int update_ref(const char *action, const char *refname, const unsigned char *sha1, const unsigned char *oldval, int flags, enum action_on_err onerr) { - struct ref_lock *lock; - lock =update_ref_lock (refname, oldval, flags,NULL , onerr); -if (!lock) + struct ref_transaction *t; +struct strbuf err =STRBUF_INIT ; + + t =ref_transaction_begin (&err); +if (!t || +ref_transaction_update (t, refname, sha1, oldval, flags, + !!oldval, &err) || +ref_transaction_commit (t, action, &err)) { +const char *str ="update_ref failed for ref '%s': %s" ; + +ref_transaction_free (t); +switch (onerr) { +case UPDATE_REFS_MSG_ON_ERR : +error (str, refname, err.buf);break ; +case UPDATE_REFS_DIE_ON_ERR : +die (str, refname, err.buf);break ; +case UPDATE_REFS_QUIET_ON_ERR :break ; + } +strbuf_release (&err);return 1 ; -return update_ref_write (action, refname, sha1, lock,NULL , onerr); + } +return 0 ; }static int ref_update_compare (const void *r1,const void *r2)
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 24/40 receive-pack.c: use a reference transaction for updating the refs
In reply to Ronnie SahlbergWrap all the ref updates inside a transaction.
--- builtin/receive-pack.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-)
builtin/receive-pack.c +22 -9
@@ -46,6 +46,7 @@ static void *head_name_to_free;static int sent_capabilities;static int shallow_update;static const char *alt_shallow_file; +static struct string_list error_strings =STRING_LIST_INIT_DUP ;static enum deny_action parse_deny_action (const char *var,const char *value) {
@@ -475,7 +476,6 @@ static const char *update(struct command *cmd, struct shallow_info *si)const char *namespaced_name;unsigned char *old_sha1 = cmd->old_sha1;unsigned char *new_sha1 = cmd->new_sha1; -struct ref_lock *lock;/* only refs/... are allowed */ if (!starts_with (name,"refs/" ) ||check_refname_format (name +5 ,0 )) {
@@ -576,19 +576,31 @@ static const char *update(struct command *cmd, struct shallow_info *si)return NULL ;/* good */ }else { +struct strbuf err =STRBUF_INIT ; +struct ref_transaction *transaction; +if (shallow_update && si->shallow_ref[cmd->index] &&update_shallow_ref (cmd, si))return "shallow error" ; - lock =lock_any_ref_for_update (namespaced_name, old_sha1, -0 ,NULL ); -if (!lock) { -rp_error ("failed to lock %s" , name); -return "failed to lock" ; - } -if (write_ref_sha1 (lock, new_sha1,"push" )) { -return "failed to write" ;/* error() already called */ + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, namespaced_name, + new_sha1, old_sha1,0 ,1 , &err) || +ref_transaction_commit (transaction,"push" , &err)) { + +const char *str; +string_list_append (&error_strings, err.buf); + str = error_strings.items[error_strings.nr -1 ].string; +strbuf_release (&err); + +ref_transaction_free (transaction); +rp_error ("%s" , str); +return str; } + +ref_transaction_free (transaction); +strbuf_release (&err);return NULL ;/* good */ } }
@@ -1215,5 +1227,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)packet_flush (1 );sha1_array_clear (&shallow);sha1_array_clear (&ref); +string_list_clear (&error_strings,0 );return 0 ; }
Signature · 1 line
2.0.0.582.ge25c160
Re: [PATCH v14 24/40] receive-pack.c: use a reference transaction for updating the refs
Quoted 7 lines · Ronnie Sahlberg
builtin/receive-pack.c +1
-0@@ -46,6 +46,7 @@ static void *head_name_to_free;static int sent_capabilities;static int shallow_update;static const char *alt_shallow_file; +static struct string_list error_strings =STRING_LIST_INIT_DUP ;
[...]
Quoted 1 line
builtin/receive-pack.c +0
-0@@ -576,19 +576,31 @@ static const char *update(struct command *cmd, struct shallow_info *si)
[...]
Quoted 9 lines
builtin/receive-pack.c +9
-0+ transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, namespaced_name, + new_sha1, old_sha1,0 ,1 , &err) || +ref_transaction_commit (transaction,"push" , &err)) { + +const char *str; +string_list_append (&error_strings, err.buf); + str = error_strings.items[error_strings.nr -1 ].string;
[...]
Quoted 1 line
builtin/receive-pack.c +1
-0+return str;
[...]
builtin/receive-pack.c int cmd_receive_pack(int argc, const char **argv, const char *prefix) · line 1231
Quoted 6 lines
builtin/receive-pack.c +1
-0@@ -1215,5 +1227,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)packet_flush (1 );sha1_array_clear (&shallow);sha1_array_clear (&ref); +string_list_clear (&error_strings,0 );return 0 ;
I think it's okay to let error strings accumulate like this because there will not be too many of them. Still I wonder, would it work to change the convention to transfer ownership of the string to the caller?
Ultimately 'commands' is leaked so we could even avoid the strdups but I'd rather leave it possible to clean up.
Something like this:
builtin/receive-pack.c +26 -20
@@ -46,7 +46,6 @@ static void *head_name_to_free;static int sent_capabilities;static int shallow_update;static const char *alt_shallow_file; -static struct string_list error_strings =STRING_LIST_INIT_DUP ;static enum deny_action parse_deny_action (const char *var,const char *value) {
@@ -195,7 +194,7 @@ static void write_head_info(void)struct command {struct command *next; -const char *error_string; +char *error_string;unsigned int skip_update:1 , did_not_exist:1 ;int index;
@@ -469,7 +468,7 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)return 0 ; } -static const char *update (struct command *cmd,struct shallow_info *si) +static char *update (struct command *cmd,struct shallow_info *si) {const char *name = cmd->ref_name;struct strbuf namespaced_name_buf =STRBUF_INIT ;
@@ -589,12 +588,9 @@ static const char *update(struct command *cmd, struct shallow_info *si) new_sha1, old_sha1, 0, 1, &err) || ref_transaction_commit(transaction, "push", &err)) { -const char *str; -string_list_append (&error_strings, err.buf); - str = error_strings.items[error_strings.nr -1 ].string; -strbuf_release (&err); - +char *str =strbuf_detach (&err,NULL );ref_transaction_free (transaction); +rp_error ("%s" , str);return str; }
@@ -659,6 +655,9 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)char cmd_oldh[41 ], cmd_newh[41 ], dst_oldh[41 ], dst_newh[41 ];int flag; +if (cmd->error_string) +die ("BUG: check_alised_update called with failed cmd" ); +strbuf_addf (&buf,"%s%s" ,get_git_namespace (), cmd->ref_name); dst_name =resolve_ref_unsafe (buf.buf, sha1,0 , &flag);strbuf_release (&buf);
@@ -670,7 +669,7 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)if (!dst_name) {rp_error ("refusing update to broken symref '%s'" , cmd->ref_name); cmd->skip_update =1 ; - cmd->error_string ="broken symref" ; + cmd->error_string =xstrdup ("broken symref" );return ; }
@@ -696,8 +695,9 @@ static void check_aliased_update(struct command *cmd, struct string_list *list) cmd->ref_name, cmd_oldh, cmd_newh, dst_cmd->ref_name, dst_oldh, dst_newh); - cmd->error_string = dst_cmd->error_string = -"inconsistent aliased update" ; + cmd->error_string =xstrdup ("inconsistent aliased update" ); +free (dst_cmd->error_string); + dst_cmd->error_string =xstrdup ("inconsistent aliased update" ); }static void check_aliased_updates (struct command *commands)
@@ -745,7 +745,9 @@ static void set_connectivity_errors(struct command *commands,if (!check_everything_connected (command_singleton_iterator,0 , &singleton))continue ; - cmd->error_string ="missing necessary objects" ; +if (cmd->error_string)/* can't happen */ +continue ; + cmd->error_string =xstrdup ("missing necessary objects" ); } }
@@ -782,9 +784,9 @@ static void reject_updates_to_hidden(struct command *commands)if (cmd->error_string || !ref_is_hidden (cmd->ref_name))continue ;if (is_null_sha1 (cmd->new_sha1)) - cmd->error_string ="deny deleting a hidden ref" ; + cmd->error_string =xstrdup ("deny deleting a hidden ref" );else - cmd->error_string ="deny updating a hidden ref" ; + cmd->error_string =xstrdup ("deny updating a hidden ref" ); } }
@@ -798,8 +800,11 @@ static void execute_commands(struct command *commands,struct iterate_data data;if (unpacker_error) { -for (cmd = commands; cmd; cmd = cmd->next) - cmd->error_string ="unpacker error" ; +for (cmd = commands; cmd; cmd = cmd->next) { +if (cmd->error_string)/* can't happen */ +continue ; + cmd->error_string =xstrdup ("unpacker error" ); + }return ; }
@@ -812,8 +817,9 @@ static void execute_commands(struct command *commands,if (run_receive_hook (commands,"pre-receive" ,0 )) {for (cmd = commands; cmd; cmd = cmd->next) { -if (!cmd->error_string) - cmd->error_string ="pre-receive hook declined" ; +if (cmd->error_string) +continue ; + cmd->error_string =xstrdup ("pre-receive hook declined" ); }return ; }
@@ -1091,7 +1097,8 @@ static void update_shallow_info(struct command *commands,if (is_null_sha1 (cmd->new_sha1))continue ;if (ref_status[cmd->index]) { - cmd->error_string ="shallow update not allowed" ; +free (cmd->error_string); + cmd->error_string =xstrdup ("shallow update not allowed" ); cmd->skip_update =1 ; } }
@@ -1227,6 +1234,5 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)packet_flush (1 );sha1_array_clear (&shallow);sha1_array_clear (&ref); -string_list_clear (&error_strings,0 );return 0 ; }
Re: [PATCH v14 24/40] receive-pack.c: use a reference transaction for updating the refs
Thanks.
Done.
I added a function to stop leaking commands too.
Quoted 182 lines · Jonathan Nieder
Quoted 7 lines · Ronnie Sahlberg
builtin/receive-pack.c +1
-0@@ -46,6 +46,7 @@ static void *head_name_to_free;static int sent_capabilities;static int shallow_update;static const char *alt_shallow_file; +static struct string_list error_strings =STRING_LIST_INIT_DUP ;[...]
Quoted 1 line
builtin/receive-pack.c +0
-0@@ -576,19 +576,31 @@ static const char *update(struct command *cmd, struct shallow_info *si)[...]
Quoted 9 lines
builtin/receive-pack.c +9
-0+ transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, namespaced_name, + new_sha1, old_sha1,0 ,1 , &err) || +ref_transaction_commit (transaction,"push" , &err)) { + +const char *str; +string_list_append (&error_strings, err.buf); + str = error_strings.items[error_strings.nr -1 ].string;[...]
Quoted 1 line
+ return str;
[...]
builtin/receive-pack.c int cmd_receive_pack(int argc, const char **argv, const char *prefix) · line 1231
Quoted 6 lines
builtin/receive-pack.c +1
-0@@ -1215,5 +1227,6 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)packet_flush (1 );sha1_array_clear (&shallow);sha1_array_clear (&ref); +string_list_clear (&error_strings,0 );return 0 ;I think it's okay to let error strings accumulate like this because there will not be too many of them. Still I wonder, would it work to change the convention to transfer ownership of the string to the caller?
Ultimately 'commands' is leaked so we could even avoid the strdups but I'd rather leave it possible to clean up.
Something like this:
builtin/receive-pack.c +26
-20@@ -46,7 +46,6 @@ static void *head_name_to_free;static int sent_capabilities;static int shallow_update;static const char *alt_shallow_file; -static struct string_list error_strings =STRING_LIST_INIT_DUP ;static enum deny_action parse_deny_action (const char *var,const char *value) {@@ -195,7 +194,7 @@ static void write_head_info(void)struct command {struct command *next; -const char *error_string; +char *error_string;unsigned int skip_update:1 , did_not_exist:1 ;int index;@@ -469,7 +468,7 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)return 0 ; } -static const char *update (struct command *cmd,struct shallow_info *si) +static char *update (struct command *cmd,struct shallow_info *si) {const char *name = cmd->ref_name;struct strbuf namespaced_name_buf =STRBUF_INIT ;@@ -589,12 +588,9 @@ static const char *update(struct command *cmd, struct shallow_info *si) new_sha1, old_sha1, 0, 1, &err) || ref_transaction_commit(transaction, "push", &err)) { -const char *str; -string_list_append (&error_strings, err.buf); - str = error_strings.items[error_strings.nr -1 ].string; -strbuf_release (&err); - +char *str =strbuf_detach (&err,NULL );ref_transaction_free (transaction); +rp_error ("%s" , str);return str; }@@ -659,6 +655,9 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)char cmd_oldh[41 ], cmd_newh[41 ], dst_oldh[41 ], dst_newh[41 ];int flag; +if (cmd->error_string) +die ("BUG: check_alised_update called with failed cmd" ); +strbuf_addf (&buf,"%s%s" ,get_git_namespace (), cmd->ref_name); dst_name =resolve_ref_unsafe (buf.buf, sha1,0 , &flag);strbuf_release (&buf);@@ -670,7 +669,7 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)if (!dst_name) {rp_error ("refusing update to broken symref '%s'" , cmd->ref_name); cmd->skip_update =1 ; - cmd->error_string ="broken symref" ; + cmd->error_string =xstrdup ("broken symref" );return ; }@@ -696,8 +695,9 @@ static void check_aliased_update(struct command *cmd, struct string_list *list) cmd->ref_name, cmd_oldh, cmd_newh, dst_cmd->ref_name, dst_oldh, dst_newh); - cmd->error_string = dst_cmd->error_string = -"inconsistent aliased update" ; + cmd->error_string =xstrdup ("inconsistent aliased update" ); +free (dst_cmd->error_string); + dst_cmd->error_string =xstrdup ("inconsistent aliased update" ); }static void check_aliased_updates (struct command *commands)@@ -745,7 +745,9 @@ static void set_connectivity_errors(struct command *commands,if (!check_everything_connected (command_singleton_iterator,0 , &singleton))continue ; - cmd->error_string ="missing necessary objects" ; +if (cmd->error_string)/* can't happen */ +continue ; + cmd->error_string =xstrdup ("missing necessary objects" ); } }@@ -782,9 +784,9 @@ static void reject_updates_to_hidden(struct command *commands)if (cmd->error_string || !ref_is_hidden (cmd->ref_name))continue ;if (is_null_sha1 (cmd->new_sha1)) - cmd->error_string ="deny deleting a hidden ref" ; + cmd->error_string =xstrdup ("deny deleting a hidden ref" );else - cmd->error_string ="deny updating a hidden ref" ; + cmd->error_string =xstrdup ("deny updating a hidden ref" ); } }@@ -798,8 +800,11 @@ static void execute_commands(struct command *commands,struct iterate_data data;if (unpacker_error) { -for (cmd = commands; cmd; cmd = cmd->next) - cmd->error_string ="unpacker error" ; +for (cmd = commands; cmd; cmd = cmd->next) { +if (cmd->error_string)/* can't happen */ +continue ; + cmd->error_string =xstrdup ("unpacker error" ); + }return ; }@@ -812,8 +817,9 @@ static void execute_commands(struct command *commands,if (run_receive_hook (commands,"pre-receive" ,0 )) {for (cmd = commands; cmd; cmd = cmd->next) { -if (!cmd->error_string) - cmd->error_string ="pre-receive hook declined" ; +if (cmd->error_string) +continue ; + cmd->error_string =xstrdup ("pre-receive hook declined" ); }return ; }@@ -1091,7 +1097,8 @@ static void update_shallow_info(struct command *commands,if (is_null_sha1 (cmd->new_sha1))continue ;if (ref_status[cmd->index]) { - cmd->error_string ="shallow update not allowed" ; +free (cmd->error_string); + cmd->error_string =xstrdup ("shallow update not allowed" ); cmd->skip_update =1 ; } }@@ -1227,6 +1234,5 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)packet_flush (1 );sha1_array_clear (&shallow);sha1_array_clear (&ref); -string_list_clear (&error_strings,0 );return 0 ; }
PATCH v14 25/40 fast-import.c: use a ref transaction when dumping tags
In reply to Ronnie Sahlberg--- fast-import.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-)
fast-import.c +23 -6
@@ -1735,15 +1735,32 @@ static void dump_tags(void) {static const char *msg ="fast-import" ;struct tag *t; -struct ref_lock *lock; -char ref_name[PATH_MAX ]; +struct strbuf ref_name =STRBUF_INIT ; +struct strbuf err =STRBUF_INIT ; +struct ref_transaction *transaction; + transaction =ref_transaction_begin (&err); +if (!transaction) { + failure |=error ("%s" , err.buf); + goto cleanup; + }for (t = first_tag; t; t = t->next_tag) { -sprintf (ref_name,"tags/%s" , t->name); - lock =lock_ref_sha1 (ref_name,NULL ); -if (!lock ||write_ref_sha1 (lock, t->sha1, msg) <0 ) - failure |=error ("Unable to update %s" , ref_name); +strbuf_reset (&ref_name); +strbuf_addf (&ref_name,"refs/tags/%s" , t->name); + +if (ref_transaction_update (transaction, ref_name.buf, t->sha1, +NULL ,0 ,0 , &err)) { + failure |=error ("%s" , err.buf); + goto cleanup; + } } +if (ref_transaction_commit (transaction, msg, &err)) + failure |=error ("%s" , err.buf); + + cleanup: +ref_transaction_free (transaction); +strbuf_release (&ref_name); +strbuf_release (&err); } static void dump_marks_helper(FILE *f,
Signature · 1 line
2.0.0.582.ge25c160
Re: [PATCH v14 25/40] fast-import.c: use a ref transaction when dumping tags
Quoted 4 lines · Ronnie Sahlberg
Signed-off-by Ronnie Sahlberg <[email protected]>--- fast-import.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-)
PATCH v14 26/40 walker.c: use ref transaction for ref updates
In reply to Ronnie SahlbergSwitch to using ref transactions in walker_fetch(). As part of the refactoring to use ref transactions we also fix a potential memory leak where in the original code if write_ref_sha1() would fail we would end up returning from the function without free()ing the msg string.
Note that this function is only called when fetching from a remote HTTP repository onto the local (most of the time single-user) repository which likely means that the type of collissions that the previous locking would protect against and cause the fetch to fail for to be even more rare.
--- walker.c | 59 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 24 deletions(-)
walker.c +35 -24
@@ -251,39 +251,36 @@ void walker_targets_free(int targets, char **target, const char **write_ref)int walker_fetch (struct walker *walker,int targets,char **target,const char **write_ref,const char *write_ref_log_details) { -struct ref_lock **lock =xcalloc (targets,sizeof (struct ref_lock *)); +struct strbuf ref_name =STRBUF_INIT ; +struct strbuf err =STRBUF_INIT ; +struct ref_transaction *transaction =NULL ;unsigned char *sha1 =xmalloc (targets *20 ); -char *msg; -int ret; +char *msg =NULL ;int i; save_commit_buffer =0 ; -for (i =0 ; i < targets; i++) { -if (!write_ref || !write_ref[i]) -continue ; - - lock[i] =lock_ref_sha1 (write_ref[i],NULL ); -if (!lock[i]) { -error ("Can't lock ref %s" , write_ref[i]); - goto unlock_and_fail; +if (write_ref) { + transaction =ref_transaction_begin (&err); +if (!transaction) { +error ("%s" , err.buf); + goto rollback_and_fail; } } -if (!walker->get_recover)for_each_ref (mark_complete,NULL );for (i =0 ; i < targets; i++) {if (interpret_target (walker, target[i], &sha1[20 * i])) {error ("Could not interpret response from server '%s' as something to pull" , target[i]); - goto unlock_and_fail; + goto rollback_and_fail; }if (process (walker,lookup_unknown_object (&sha1[20 * i]))) - goto unlock_and_fail; + goto rollback_and_fail; }if (loop (walker)) - goto unlock_and_fail; + goto rollback_and_fail;if (write_ref_log_details) { msg =xmalloc (strlen (write_ref_log_details) +12 );
@@ -294,19 +291,33 @@ int walker_fetch(struct walker *walker, int targets, char **target,for (i =0 ; i < targets; i++) {if (!write_ref || !write_ref[i])continue ; - ret =write_ref_sha1 (lock[i], &sha1[20 * i], msg ? msg :"fetch (unknown)" ); - lock[i] =NULL ; -if (ret) - goto unlock_and_fail; +strbuf_reset (&ref_name); +strbuf_addf (&ref_name,"refs/%s" , write_ref[i]); +if (ref_transaction_update (transaction, ref_name.buf, + &sha1[20 * i],NULL ,0 ,0 , + &err)) { +error ("%s" , err.buf); + goto rollback_and_fail; + } + } +if (write_ref) { +if (ref_transaction_commit (transaction, + msg ? msg :"fetch (unknown)" , + &err)) { +error ("%s" , err.buf); + goto rollback_and_fail; + } +ref_transaction_free (transaction); } -free (msg); +free (msg);return 0 ; -unlock_and_fail: -for (i =0 ; i < targets; i++) -if (lock[i]) -unlock_ref (lock[i]); +rollback_and_fail: +ref_transaction_free (transaction); +free (msg); +strbuf_release (&err); +strbuf_release (&ref_name);return -1 ; }
Signature · 1 line
2.0.0.582.ge25c160
Re: [PATCH v14 26/40] walker.c: use ref transaction for ref updates
Quoted 4 lines · Ronnie Sahlberg
Signed-off-by Ronnie Sahlberg <[email protected]>--- walker.c | 59 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 24 deletions(-)
PATCH v14 27/40 refs.c: make lock_ref_sha1 static
In reply to Ronnie SahlbergNo external callers reference lock_ref_sha1 any more so lets declare it static.
--- refs.c | 2 +- refs.h | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-)
refs.c +1 -1
@@ -2139,7 +2139,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,return NULL ; } -struct ref_lock *lock_ref_sha1 (const char *refname,const unsigned char *old_sha1) +static struct ref_lock *lock_ref_sha1 (const char *refname,const unsigned char *old_sha1) {char refpath[PATH_MAX ];if (check_refname_format (refname,0 ))
refs.h +0 -3
@@ -171,9 +171,6 @@ extern int ref_exists(const char *);*/ extern int peel_ref (const char *refname,unsigned char *sha1); -/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/ -extern struct ref_lock *lock_ref_sha1 (const char *refname,const unsigned char *old_sha1); -/** Locks any ref (for 'HEAD' type refs). */ #define REF_NODEREF 0x01 extern struct ref_lock *lock_any_ref_for_update(const char *refname,
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 28/40 refs.c: remove the update_ref_lock function
In reply to Ronnie SahlbergSince we now only call update_ref_lock with onerr==QUIET_ON_ERR we no longer need this function and can replace it with just calling lock_any_ref_for_update directly.
--- refs.c | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-)
refs.c +6 -24
@@ -3323,24 +3323,6 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)return retval; } -static struct ref_lock *update_ref_lock (const char *refname, -const unsigned char *oldval, -int flags,int *type_p, -enum action_on_err onerr) -{ -struct ref_lock *lock; - lock =lock_any_ref_for_update (refname, oldval, flags, type_p); -if (!lock) { -const char *str ="Cannot lock the ref '%s'." ; -switch (onerr) { -case UPDATE_REFS_MSG_ON_ERR :error (str, refname);break ; -case UPDATE_REFS_DIE_ON_ERR :die (str, refname);break ; -case UPDATE_REFS_QUIET_ON_ERR :break ; - } - } -return lock; -} -static int update_ref_write (const char *action,const char *refname,const unsigned char *sha1,struct ref_lock *lock,struct strbuf *err,enum action_on_err onerr)
@@ -3590,12 +3572,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; - update->lock =update_ref_lock (update->refname, - (update->have_old ? - update->old_sha1 :NULL ), - update->flags, - &update->type, -UPDATE_REFS_QUIET_ON_ERR ); + update->lock =lock_any_ref_for_update (update->refname, + (update->have_old ? + update->old_sha1 : +NULL ), + update->flags, + &update->type);if (!update->lock) {if (err) strbuf_addf(err, "Cannot lock the ref '%s'.",
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 29/40 refs.c: remove the update_ref_write function
In reply to Ronnie SahlbergSince we only call update_ref_write from a single place and we only call it with onerr==QUIET_ON_ERR we can just as well get rid of it and just call write_ref_sha1 directly. This changes the return status for _commit from 1 to -1 on failures when writing to the ref. Eventually we will want _commit to start returning more detailed error conditions than the current simple success/failure. For example if the commit failed due to name conflicts etc.
--- refs.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-)
refs.c +9 -26
@@ -3323,25 +3323,6 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)return retval; } -static int update_ref_write (const char *action,const char *refname, -const unsigned char *sha1,struct ref_lock *lock, -struct strbuf *err,enum action_on_err onerr) -{ -if (write_ref_sha1 (lock, sha1, action) <0 ) { -const char *str ="Cannot update the ref '%s'." ; -if (err) -strbuf_addf (err, str, refname); - -switch (onerr) { -case UPDATE_REFS_MSG_ON_ERR :error (str, refname);break ; -case UPDATE_REFS_DIE_ON_ERR :die (str, refname);break ; -case UPDATE_REFS_QUIET_ON_ERR :break ; - } -return 1 ; - } -return 0 ; -} -/** * Information needed for a single ref update. Set new_sha1 to the * new value or to zero to delete the ref. To check the old value
@@ -3592,14 +3573,16 @@ int ref_transaction_commit(struct ref_transaction *transaction,struct ref_update *update = updates[i];if (!is_null_sha1 (update->new_sha1)) { - ret =update_ref_write (msg, - update->refname, - update->new_sha1, - update->lock, err, -UPDATE_REFS_QUIET_ON_ERR ); - update->lock =NULL ;/* freed by update_ref_write */ -if (ret) + ret =write_ref_sha1 (update->lock, update->new_sha1, + msg); + update->lock =NULL ;/* freed by write_ref_sha1 */ +if (ret) { +const char *str ="Cannot update the ref '%s'." ; + +if (err) +strbuf_addf (err, str, update->refname); goto cleanup; + } } }
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 30/40 refs.c: remove lock_ref_sha1
In reply to Ronnie Sahlberglock_ref_sha1 was only called from one place in refc.c and only provided a check that the refname was sane before adding back the initial "refs/" part of the ref path name, the initial "refs/" that this caller had already stripped off before calling lock_ref_sha1.
--- refs.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
refs.c +5 -10
@@ -2139,15 +2139,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,return NULL ; } -static struct ref_lock *lock_ref_sha1 (const char *refname,const unsigned char *old_sha1) -{ -char refpath[PATH_MAX ]; -if (check_refname_format (refname,0 )) -return NULL ; -strcpy (refpath,mkpath ("refs/%s" , refname)); -return lock_ref_sha1_basic (refpath, old_sha1,0 ,NULL ); -} -struct ref_lock *lock_any_ref_for_update (const char *refname,const unsigned char *old_sha1,int flags,int *type_p)
@@ -2356,8 +2347,12 @@ static void try_remove_empty_parents(char *name)/* make sure nobody touched the ref, and unlink */ static void prune_ref (struct ref_to_prune *r) { -struct ref_lock *lock =lock_ref_sha1 (r->name +5 , r->sha1); +struct ref_lock *lock; + +if (check_refname_format (r->name +5 ,0 )) +return ; + lock =lock_ref_sha1_basic (r->name, r->sha1,0 ,NULL );if (lock) {unlink_or_warn (git_path ("%s" , r->name));unlock_ref (lock);
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 31/40 refs.c: make prune_ref use a transaction to delete the ref
In reply to Ronnie SahlbergChange prune_ref to delete the ref using a ref transaction. To do this we also need to add a new flag REF_ISPRUNING that will tell the transaction that we do not want to delete this ref from the packed refs. This flag is private to refs.c and not exposed to external callers.
--- refs.c | 28 +++++++++++++++++++++------- refs.h | 11 ++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-)
refs.c +21 -7
@@ -30,6 +30,12 @@ static inline int bad_ref_char(int ch) }/* +* Used as a flag to ref_transaction_delete when a loose ref is being +* pruned. +*/ +#define REF_ISPRUNING 0x0100 + +/* * Try to read one refname component from the front of refname. Return * the length of the component found, or -1 if the component is not * legal.
@@ -2347,17 +2353,24 @@ static void try_remove_empty_parents(char *name)/* make sure nobody touched the ref, and unlink */ static void prune_ref (struct ref_to_prune *r) { -struct ref_lock *lock; +struct ref_transaction *transaction; +struct strbuf err =STRBUF_INIT ;if (check_refname_format (r->name +5 ,0 ))return ; - lock =lock_ref_sha1_basic (r->name, r->sha1,0 ,NULL ); -if (lock) { -unlink_or_warn (git_path ("%s" , r->name)); -unlock_ref (lock); -try_remove_empty_parents (r->name); + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_delete (transaction, r->name, r->sha1, +REF_ISPRUNING ,1 , &err) || +ref_transaction_commit (transaction,NULL , &err)) { +ref_transaction_free (transaction); +error ("%s" , err.buf); +strbuf_release (&err); +return ; } +ref_transaction_free (transaction); +try_remove_empty_parents (r->name); }static void prune_refs (struct ref_to_prune *r)
@@ -3586,9 +3599,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,struct ref_update *update = updates[i];if (update->lock) { - delnames[delnum++] = update->lock->ref_name; ret |=delete_ref_loose (update->lock, update->type, err); +if (!(update->flags &REF_ISPRUNING )) + delnames[delnum++] = update->lock->ref_name; } }
refs.h +10 -1
@@ -171,8 +171,17 @@ extern int ref_exists(const char *);*/ extern int peel_ref (const char *refname,unsigned char *sha1); -/** Locks any ref (for 'HEAD' type refs). */ +/* +* Flags controlling lock_any_ref_for_update(), ref_transaction_update(), +* ref_transaction_create(), etc. +* REF_NODEREF: act on the ref directly, instead of dereferencing +* symbolic references. +* +* Flags >= 0x100 are reserved for internal use. +*/ #define REF_NODEREF 0x01 + +/** Locks any ref (for 'HEAD' type refs). */ extern struct ref_lock *lock_any_ref_for_update (const char *refname,const unsigned char *old_sha1,int flags,int *type_p);
Signature · 1 line
2.0.0.582.ge25c160
Re: [PATCH v14 31/40] refs.c: make prune_ref use a transaction to delete the ref
Quoted 5 lines · Ronnie Sahlberg
Signed-off-by Ronnie Sahlberg <[email protected]>--- refs.c | 28 +++++++++++++++++++++------- refs.h | 11 ++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-)
PATCH v14 32/40 refs.c: make delete_ref use a transaction
In reply to Ronnie SahlbergChange delete_ref to use a ref transaction for the deletion. At the same time since we no longer have any callers of repack_without_ref we can now delete this function.
Change delete_ref to return 0 on success and 1 on failure instead of the previous 0 on success either 1 or -1 on failure.
--- refs.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-)
refs.c +13 -21
@@ -2513,11 +2513,6 @@ static int repack_without_refs(const char **refnames, int n, struct strbuf *err)return ret; } -static int repack_without_ref (const char *refname) -{ -return repack_without_refs (&refname,1 ,NULL ); -} -static int add_err_if_unremovable (const char *op,const char *file,struct strbuf *e,int rc) {
@@ -2557,24 +2552,21 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)int delete_ref (const char *refname,const unsigned char *sha1,int delopt) { -struct ref_lock *lock; -int ret =0 , flag =0 ; +struct ref_transaction *transaction; +struct strbuf err =STRBUF_INIT ; - lock =lock_ref_sha1_basic (refname, sha1, delopt, &flag); -if (!lock) + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_delete (transaction, refname, sha1, delopt, + sha1 && !is_null_sha1 (sha1), &err) || +ref_transaction_commit (transaction,NULL , &err)) { +error ("%s" , err.buf); +ref_transaction_free (transaction); +strbuf_release (&err);return 1 ; - ret |=delete_ref_loose (lock, flag,NULL ); - -/* removing the loose one could have resurrected an earlier -* packed one. Also, if it was not loose we need to repack -* without it. -*/ - ret |=repack_without_ref (lock->ref_name); - -unlink_or_warn (git_path ("logs/%s" , lock->ref_name)); -clear_loose_ref_cache (&ref_cache); -unlock_ref (lock); -return ret; + } +ref_transaction_free (transaction); +return 0 ; }/*
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 33/40 refs.c: pass the ref log message to _create/delete/update instead of _commit
In reply to Ronnie SahlbergChange the reference transactions so that we pass the reflog message through to the create/delete/update function instead of the commit message. This allows for individual messages for each change in a multi ref transaction.
--- branch.c | 4 ++-- builtin/commit.c | 4 ++-- builtin/fetch.c | 3 +-- builtin/receive-pack.c | 7 ++++--- builtin/replace.c | 4 ++-- builtin/tag.c | 4 ++-- builtin/update-ref.c | 13 +++++++------ fast-import.c | 8 ++++---- refs.c | 34 +++++++++++++++++++++------------- refs.h | 8 ++++---- sequencer.c | 4 ++-- walker.c | 5 ++--- 12 files changed, 53 insertions(+), 45 deletions(-)
branch.c +2 -2
@@ -301,8 +301,8 @@ void create_branch(const char *head, transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_update (transaction, ref.buf, sha1, - null_sha1,0 , !forcing, &err) || -ref_transaction_commit (transaction, msg, &err)) + null_sha1,0 , !forcing, msg, &err) || +ref_transaction_commit (transaction, &err))die ("%s" , err.buf);ref_transaction_free (transaction); }
builtin/commit.c +2 -2
@@ -1753,8 +1753,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix) ref_transaction_update(transaction, "HEAD", sha1, current_head ? current_head->object.sha1 : NULL, - 0, !!current_head, &err) || - ref_transaction_commit(transaction, sb.buf, &err)) { + 0, !!current_head, sb.buf, &err) || + ref_transaction_commit(transaction, &err)) {rollback_index_files ();die ("%s" , err.buf); }
builtin/fetch.c +1 -2
@@ -673,10 +673,9 @@ static int store_updated_refs(const char *raw_url, const char *remote_name, } } } -if (rc &STORE_REF_ERROR_DF_CONFLICT )error (_ ("some local refs could not be updated; try running\n" -" 'git remote prune %s' to remove any old, conflicting " +"'git remote prune %s' to remove any old, conflicting " "branches" ), remote_name); abort:
builtin/receive-pack.c +4 -3
@@ -586,10 +586,11 @@ static const char *update(struct command *cmd, struct shallow_info *si) transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_update (transaction, namespaced_name, - new_sha1, old_sha1,0 ,1 , &err) || -ref_transaction_commit (transaction,"push" , &err)) { - + new_sha1, old_sha1,0 ,1 ,"push" , + &err) || +ref_transaction_commit (transaction, &err)) {const char *str; +string_list_append (&error_strings, err.buf); str = error_strings.items[error_strings.nr -1 ].string;strbuf_release (&err);
builtin/replace.c +2 -2
@@ -171,8 +171,8 @@ static int replace_object_sha1(const char *object_ref, transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_update (transaction, ref, repl, prev, -0 , !is_null_sha1 (prev), &err) || -ref_transaction_commit (transaction,NULL , &err)) +0 , !is_null_sha1 (prev),NULL , &err) || +ref_transaction_commit (transaction, &err))die ("%s" , err.buf);ref_transaction_free (transaction);
builtin/tag.c +2 -2
@@ -705,8 +705,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix) transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_update (transaction, ref.buf, object, prev, -0 , !is_null_sha1 (prev), &err) || -ref_transaction_commit (transaction,NULL , &err)) +0 , !is_null_sha1 (prev),NULL , &err) || +ref_transaction_commit (transaction, &err))die ("%s" , err.buf);ref_transaction_free (transaction);if (force && !is_null_sha1 (prev) &&hashcmp (prev, object))
builtin/update-ref.c +7 -6
@@ -16,6 +16,7 @@ static struct ref_transaction *transaction;static char line_termination ='\n' ;static int update_flags; +static const char *msg;static struct strbuf err =STRBUF_INIT ;/*
@@ -199,7 +200,7 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)die ("update %s: extra input: %s" , refname, next);if (ref_transaction_update (transaction, refname, new_sha1, old_sha1, - update_flags, have_old, &err)) + update_flags, have_old, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -227,7 +228,7 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)die ("create %s: extra input: %s" , refname, next);if (ref_transaction_create (transaction, refname, new_sha1, - update_flags, &err)) + update_flags, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -259,7 +260,7 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)die ("delete %s: extra input: %s" , refname, next);if (ref_transaction_delete (transaction, refname, old_sha1, - update_flags, have_old, &err)) + update_flags, have_old, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -292,7 +293,7 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)die ("verify %s: extra input: %s" , refname, next);if (ref_transaction_update (transaction, refname, new_sha1, old_sha1, - update_flags, have_old, &err)) + update_flags, have_old, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -345,7 +346,7 @@ static void update_refs_stdin(void)int cmd_update_ref (int argc,const char **argv,const char *prefix) { -const char *refname, *oldval, *msg =NULL ; +const char *refname, *oldval;unsigned char sha1[20 ], oldsha1[20 ];int delete =0 , no_deref =0 , read_stdin =0 , end_null =0 , flags =0 ;struct option options[] = {
@@ -371,7 +372,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)if (end_null) line_termination ='\0' ;update_refs_stdin (); -if (ref_transaction_commit (transaction, msg, &err)) +if (ref_transaction_commit (transaction, &err))die ("%s" , err.buf);ref_transaction_free (transaction);return 0 ;
fast-import.c +4 -4
@@ -1709,8 +1709,8 @@ static int update_branch(struct branch *b) transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_update (transaction, b->name, b->sha1, old_sha1, -0 ,1 , &err) || -ref_transaction_commit (transaction, msg, &err)) { +0 ,1 , msg, &err) || +ref_transaction_commit (transaction, &err)) {ref_transaction_free (transaction);error ("%s" , err.buf);strbuf_release (&err);
@@ -1749,12 +1749,12 @@ static void dump_tags(void)strbuf_addf (&ref_name,"refs/tags/%s" , t->name);if (ref_transaction_update (transaction, ref_name.buf, t->sha1, -NULL ,0 ,0 , &err)) { +NULL ,0 ,0 , msg, &err)) { failure |=error ("%s" , err.buf); goto cleanup; } } -if (ref_transaction_commit (transaction, msg, &err)) +if (ref_transaction_commit (transaction, &err)) failure |=error ("%s" , err.buf); cleanup:
refs.c +21 -13
@@ -2362,8 +2362,8 @@ static void prune_ref(struct ref_to_prune *r) transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_delete (transaction, r->name, r->sha1, -REF_ISPRUNING ,1 , &err) || -ref_transaction_commit (transaction,NULL , &err)) { +REF_ISPRUNING ,1 ,NULL , &err) || +ref_transaction_commit (transaction, &err)) {ref_transaction_free (transaction);error ("%s" , err.buf);strbuf_release (&err);
@@ -2558,8 +2558,8 @@ int delete_ref(const char *refname, const unsigned char *sha1, int delopt) transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_delete (transaction, refname, sha1, delopt, - sha1 && !is_null_sha1 (sha1), &err) || -ref_transaction_commit (transaction,NULL , &err)) { + sha1 && !is_null_sha1 (sha1),NULL , &err) || +ref_transaction_commit (transaction, &err)) {error ("%s" , err.buf);ref_transaction_free (transaction);strbuf_release (&err);
@@ -3336,6 +3336,7 @@ struct ref_update {int have_old;/* 1 if old_sha1 is valid, 0 otherwise */ struct ref_lock *lock;int type; +char *msg;const char refname[FLEX_ARRAY ]; };
@@ -3383,9 +3384,10 @@ void ref_transaction_free(struct ref_transaction *transaction)if (!transaction)return ; -for (i =0 ; i < transaction->nr; i++) +for (i =0 ; i < transaction->nr; i++) { +free (transaction->updates[i]->msg);free (transaction->updates[i]); - + }free (transaction->updates);free (transaction); }
@@ -3406,7 +3408,7 @@ int ref_transaction_update(struct ref_transaction *transaction,const char *refname, const unsigned char *new_sha1, const unsigned char *old_sha1, - int flags, int have_old, + int flags, int have_old, const char *msg, struct strbuf *err) { struct ref_update *update;
@@ -3423,13 +3425,15 @@ int ref_transaction_update(struct ref_transaction *transaction, update->have_old = have_old;if (have_old)hashcpy (update->old_sha1, old_sha1); +if (msg) + update->msg =xstrdup (msg);return 0 ; }int ref_transaction_create (struct ref_transaction *transaction,const char *refname,const unsigned char *new_sha1, -int flags, +int flags,const char *msg,struct strbuf *err) {struct ref_update *update;
@@ -3446,13 +3450,15 @@ int ref_transaction_create(struct ref_transaction *transaction,hashclr (update->old_sha1); update->flags = flags; update->have_old =1 ; +if (msg) + update->msg =xstrdup (msg);return 0 ; }int ref_transaction_delete (struct ref_transaction *transaction,const char *refname,const unsigned char *old_sha1, -int flags,int have_old, +int flags,int have_old,const char *msg,struct strbuf *err) {struct ref_update *update;
@@ -3470,6 +3476,8 @@ int ref_transaction_delete(struct ref_transaction *transaction,assert (!is_null_sha1 (old_sha1));hashcpy (update->old_sha1, old_sha1); } +if (msg) + update->msg =xstrdup (msg);return 0 ; }
@@ -3483,8 +3491,8 @@ int update_ref(const char *action, const char *refname, t =ref_transaction_begin (&err);if (!t ||ref_transaction_update (t, refname, sha1, oldval, flags, - !!oldval, &err) || -ref_transaction_commit (t, action, &err)) { + !!oldval, action, &err) || +ref_transaction_commit (t, &err)) {const char *str ="update_ref failed for ref '%s': %s" ;ref_transaction_free (t);
@@ -3525,7 +3533,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n, }int ref_transaction_commit (struct ref_transaction *transaction, -const char *msg,struct strbuf *err) +struct strbuf *err) {int ret =0 , delnum =0 , i;const char **delnames;
@@ -3574,7 +3582,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,if (!is_null_sha1 (update->new_sha1)) { ret =write_ref_sha1 (update->lock, update->new_sha1, - msg); + update->msg); update->lock =NULL ;/* freed by write_ref_sha1 */ if (ret) {const char *str ="Cannot update the ref '%s'." ;
refs.h +4 -4
@@ -287,7 +287,7 @@ int ref_transaction_update(struct ref_transaction *transaction,const char *refname, const unsigned char *new_sha1, const unsigned char *old_sha1, - int flags, int have_old, + int flags, int have_old, const char *msg, struct strbuf *err);/*
@@ -302,7 +302,7 @@ int ref_transaction_update(struct ref_transaction *transaction,int ref_transaction_create (struct ref_transaction *transaction,const char *refname,const unsigned char *new_sha1, -int flags, +int flags,const char *msg,struct strbuf *err);/*
@@ -316,7 +316,7 @@ int ref_transaction_create(struct ref_transaction *transaction,int ref_transaction_delete (struct ref_transaction *transaction,const char *refname,const unsigned char *old_sha1, -int flags,int have_old, +int flags,int have_old,const char *msg,struct strbuf *err);/*
@@ -325,7 +325,7 @@ int ref_transaction_delete(struct ref_transaction *transaction,* problem. */ int ref_transaction_commit (struct ref_transaction *transaction, -const char *msg,struct strbuf *err); +struct strbuf *err);/* * Free an existing transaction and all associated data.
sequencer.c +2 -2
@@ -285,8 +285,8 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from, transaction =ref_transaction_begin (&err);if (!transaction ||ref_transaction_update (transaction,"HEAD" , to, from, -0 , !unborn, &err) || -ref_transaction_commit (transaction, sb.buf, &err)) { +0 , !unborn, sb.buf, &err) || +ref_transaction_commit (transaction, &err)) {ref_transaction_free (transaction);error ("%s" , err.buf);strbuf_release (&sb);
walker.c +2 -3
@@ -295,15 +295,14 @@ int walker_fetch(struct walker *walker, int targets, char **target,strbuf_addf (&ref_name,"refs/%s" , write_ref[i]);if (ref_transaction_update (transaction, ref_name.buf, &sha1[20 * i],NULL ,0 ,0 , + msg ? msg :"fetch (unknown)" , &err)) {error ("%s" , err.buf); goto rollback_and_fail; } }if (write_ref) { -if (ref_transaction_commit (transaction, - msg ? msg :"fetch (unknown)" , - &err)) { +if (ref_transaction_commit (transaction, &err)) {error ("%s" , err.buf); goto rollback_and_fail; }
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 34/40 refs.c: pass NULL as *flags to read_ref_full
In reply to Ronnie SahlbergWe call read_ref_full with a pointer to flags from rename_ref but since we never actually use the returned flags we can just pass NULL here instead.
--- refs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
refs.c +1 -1
@@ -2657,7 +2657,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms goto rollback; } -if (!read_ref_full (newrefname, sha1,1 , &flag) && +if (!read_ref_full (newrefname, sha1,1 ,NULL ) &&delete_ref (newrefname, sha1,REF_NODEREF )) {if (errno==EISDIR ) {if (remove_empty_directories (git_path ("%s" , newrefname))) {
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 35/40 refs.c: move the check for valid refname to lock_ref_sha1_basic
In reply to Ronnie SahlbergMove the check for check_refname_format from lock_any_ref_for_update to lock_ref_sha1_basic. At some later stage we will get rid of lock_any_ref_for_update completely.
If lock_ref_sha1_basic fails the check_refname_format test, set errno to EINVAL before returning NULL. This to guarantee that we will not return an error without updating errno.
This leaves lock_any_ref_for_updates as a no-op wrapper which could be removed. But this wrapper is also called from an external caller and we will soon make changes to the signature to lock_ref_sha1_basic that we do not want to expose to that caller.
This changes semantics for lock_ref_sha1_basic slightly. With this change it is no longer possible to open a ref that has a badly name which breaks any codepaths that tries to open and repair badly named refs. The normal refs API should not allow neither creating nor accessing refs with invalid names. If we need such recovery code we could add it as an option to git fsck and have git fsck be the only sanctioned way of bypassing the normal API and checks.
--- refs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
refs.c +5 -2
@@ -2058,6 +2058,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,int missing =0 ;int attempts_remaining =3 ; +if (check_refname_format (refname,REFNAME_ALLOW_ONELEVEL )) { + errno =EINVAL ; +return NULL ; + } + lock =xcalloc (1 ,sizeof (struct ref_lock )); lock->lock_fd =-1 ;
@@ -2149,8 +2154,6 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,const unsigned char *old_sha1, int flags, int *type_p) { - if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) - return NULL; return lock_ref_sha1_basic(refname, old_sha1, flags, type_p); }
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 36/40 refs.c: call lock_ref_sha1_basic directly from commit
In reply to Ronnie SahlbergSkip using the lock_any_ref_for_update wrapper and call lock_ref_sha1_basic directly from the commit function.
--- refs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
refs.c +6 -6
@@ -3564,12 +3564,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; - update->lock =lock_any_ref_for_update (update->refname, - (update->have_old ? - update->old_sha1 : -NULL ), - update->flags, - &update->type); + update->lock =lock_ref_sha1_basic (update->refname, + (update->have_old ? + update->old_sha1 : +NULL ), + update->flags, + &update->type);if (!update->lock) {if (err) strbuf_addf(err, "Cannot lock the ref '%s'.",
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 37/40 refs.c: pass a skip list to name_conflict_fn
In reply to Ronnie SahlbergAllow passing a list of refs to skip checking to name_conflict_fn. There are some conditions where we want to allow a temporary conflict and skip checking those refs. For example if we have a transaction that
1, guarantees that m is a packed refs and there is no loose ref for m 2, the transaction will delete m from the packed ref
3, the transaction will create conflicting m/m
For this case we want to be able to lock and create m/m since we know that the conflict is only transient. I.e. the conflict will be automatically resolved by the transaction when it deletes m.
--- refs.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-)
refs.c +26 -15
@@ -791,15 +791,18 @@ static int names_conflict(const char *refname1, const char *refname2)struct name_conflict_cb {const char *refname; -const char *oldrefname;const char *conflicting_refname; +const char **skip; +int skipnum; };static int name_conflict_fn (struct ref_entry *entry,void *cb_data) {struct name_conflict_cb *data = (struct name_conflict_cb *)cb_data; -if (data->oldrefname && !strcmp (data->oldrefname, entry->name)) -return 0 ; +int i; +for (i =0 ; i < data->skipnum; i++) +if (!strcmp (entry->name, data->skip[i])) +return 0 ;if (names_conflict (data->refname, entry->name)) { data->conflicting_refname = entry->name;return 1 ;
@@ -812,15 +815,18 @@ static int name_conflict_fn(struct ref_entry *entry, void *cb_data)* conflicting with the name of an existing reference in dir. If * oldrefname is non-NULL, ignore potential conflicts with oldrefname * (e.g., because oldrefname is scheduled for deletion in the same -* operation). +* operation). skip contains a list of refs we want to skip checking for +* conflicts with. */ -static int is_refname_available (const char *refname,const char *oldrefname, -struct ref_dir *dir) +static int is_refname_available (const char *refname, +struct ref_dir *dir, +const char **skip,int skipnum) {struct name_conflict_cb data; data.refname = refname; - data.oldrefname = oldrefname; data.conflicting_refname =NULL ; + data.skip = skip; + data.skipnum = skipnum;sort_ref_dir (dir);if (do_for_each_entry_in_dir (dir,0 , name_conflict_fn, &data)) {
@@ -2047,7 +2053,8 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)static struct ref_lock *lock_ref_sha1_basic (const char *refname,const unsigned char *old_sha1, -int flags,int *type_p) +int flags,int *type_p, +const char **skip,int skipnum) {char *ref_file;const char *orig_refname = refname;
@@ -2096,7 +2103,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,* name is a proper prefix of our refname. */ if (missing && - !is_refname_available (refname,NULL ,get_packed_refs (&ref_cache))) { + !is_refname_available (refname,get_packed_refs (&ref_cache), + skip, skipnum)) { last_errno =ENOTDIR ; goto error_return; }
@@ -2154,7 +2162,7 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,const unsigned char *old_sha1, int flags, int *type_p) { - return lock_ref_sha1_basic(refname, old_sha1, flags, type_p); + return lock_ref_sha1_basic(refname, old_sha1, flags, type_p, NULL, 0); }/*
@@ -2645,10 +2653,12 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsif (!symref)return error ("refname %s not found" , oldrefname); -if (!is_refname_available (newrefname, oldrefname,get_packed_refs (&ref_cache))) +if (!is_refname_available (newrefname,get_packed_refs (&ref_cache), + &oldrefname,1 ))return 1 ; -if (!is_refname_available (newrefname, oldrefname,get_loose_refs (&ref_cache))) +if (!is_refname_available (newrefname,get_loose_refs (&ref_cache), + &oldrefname,1 ))return 1 ;if (log &&rename (git_path ("logs/%s" , oldrefname),git_path (TMP_RENAMED_LOG )))
@@ -2678,7 +2688,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms logmoved = log; - lock =lock_ref_sha1_basic (newrefname,NULL ,0 ,NULL ); + lock =lock_ref_sha1_basic (newrefname,NULL ,0 ,NULL ,NULL ,0 );if (!lock) {error ("unable to lock %s for update" , newrefname); goto rollback;
@@ -2693,7 +2703,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsreturn 0 ; rollback: - lock =lock_ref_sha1_basic (oldrefname,NULL ,0 ,NULL ); + lock =lock_ref_sha1_basic (oldrefname,NULL ,0 ,NULL ,NULL ,0 );if (!lock) {error ("unable to lock %s for rollback" , oldrefname); goto rollbacklog;
@@ -3569,7 +3579,8 @@ int ref_transaction_commit(struct ref_transaction *transaction, update->old_sha1 : NULL), update->flags, - &update->type); + &update->type, + delnames, delnum);if (!update->lock) {if (err) strbuf_addf(err, "Cannot lock the ref '%s'.",
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 38/40 refs.c: propagate any errno==ENOTDIR from _commit back to the callers
In reply to Ronnie SahlbergIn _commit, ENOTDIR can happen in the call to lock_ref_sha1_basic, either when we lstat the new refname and it returns ENOTDIR or if the name checking function reports that the same type of conflict happened. In both cases it means that we can not create the new ref due to a name conflict.
For these cases, save the errno value and abort and make sure that the caller can see errno==ENOTDIR.
Also start defining specific return codes for _commit, assign -1 as a generic error and -2 as the error that refers to a name conflict. Callers can (and should) use that return value inspecting errno directly.
--- refs.c | 22 +++++++++++++++------- refs.h | 6 ++++++ 2 files changed, 21 insertions(+), 7 deletions(-)
refs.c +15 -7
@@ -3548,7 +3548,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err) { -int ret =0 , delnum =0 , i; +int ret =0 , delnum =0 , i, df_conflict =0 ;const char **delnames;int n = transaction->nr;struct ref_update **updates = transaction->updates;
@@ -3566,9 +3566,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,/* Copy, sort, and reject duplicate refs */ qsort (updates, n,sizeof (*updates), ref_update_compare); - ret =ref_update_reject_duplicates (updates, n, err); -if (ret) +if (ref_update_reject_duplicates (updates, n, err)) { + ret =-1 ; goto cleanup; + }/* Acquire all locks while verifying old values */ for (i =0 ; i < n; i++) {
@@ -3582,10 +3583,12 @@ int ref_transaction_commit(struct ref_transaction *transaction, &update->type, delnames, delnum);if (!update->lock) { +if (errno ==ENOTDIR ) + df_conflict =1 ;if (err)strbuf_addf (err,"Cannot lock the ref '%s'." , update->refname); - ret =1 ; + ret =-1 ; goto cleanup; } }
@@ -3603,6 +3606,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,if (err)strbuf_addf (err, str, update->refname); + ret =-1 ; goto cleanup; } }
@@ -3613,14 +3617,16 @@ int ref_transaction_commit(struct ref_transaction *transaction,struct ref_update *update = updates[i];if (update->lock) { - ret |=delete_ref_loose (update->lock, update->type, - err); +if (delete_ref_loose (update->lock, update->type, err)) + ret =-1 ; +if (!(update->flags &REF_ISPRUNING )) delnames[delnum++] = update->lock->ref_name; } } - ret |=repack_without_refs (delnames, delnum, err); +if (repack_without_refs (delnames, delnum, err)) + ret =-1 ;for (i =0 ; i < delnum; i++)unlink_or_warn (git_path ("logs/%s" , delnames[i]));clear_loose_ref_cache (&ref_cache);
@@ -3633,6 +3639,8 @@ cleanup:if (updates[i]->lock)unlock_ref (updates[i]->lock);free (delnames); +if (df_conflict) + ret =-2 ;return ret; }
refs.h +6 -0
@@ -323,7 +323,13 @@ int ref_transaction_delete(struct ref_transaction *transaction,* Commit all of the changes that have been queued in transaction, as * atomically as possible. Return a nonzero value if there is a * problem. +* If the transaction is already in failed state this function will return +* an error. +* Function returns 0 on success, -1 for generic failures and +* UPDATE_REFS_NAME_CONFLICT (-2) if the failure was due to a name +* collision (ENOTDIR). */ +#define UPDATE_REFS_NAME_CONFLICT -2int ref_transaction_commit (struct ref_transaction *transaction,struct strbuf *err);
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 39/40 fetch.c: change s_update_ref to use a ref transaction
In reply to Ronnie SahlbergChange s_update_ref to use a ref transaction for the ref update.
--- builtin/fetch.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-)
builtin/fetch.c +23 -10
@@ -375,23 +375,36 @@ static int s_update_ref(const char *action, {char msg[1024 ];char *rla =getenv ("GIT_REFLOG_ACTION" ); -static struct ref_lock *lock; +struct ref_transaction *transaction; +struct strbuf err =STRBUF_INIT ; +int ret, df_conflict =0 ;if (dry_run)return 0 ;if (!rla) rla = default_rla.buf;snprintf (msg,sizeof (msg),"%s: %s" , rla, action); - lock =lock_any_ref_for_update (ref->name, - check_old ? ref->old_sha1 :NULL , -0 ,NULL ); -if (!lock) -return errno ==ENOTDIR ?STORE_REF_ERROR_DF_CONFLICT : -STORE_REF_ERROR_OTHER ; -if (write_ref_sha1 (lock, ref->new_sha1, msg) <0 ) -return errno ==ENOTDIR ?STORE_REF_ERROR_DF_CONFLICT : -STORE_REF_ERROR_OTHER ; + + transaction =ref_transaction_begin (&err); +if (!transaction || +ref_transaction_update (transaction, ref->name, ref->new_sha1, + ref->old_sha1,0 , check_old, msg, &err)) + goto fail; + + ret =ref_transaction_commit (transaction, &err); +if (ret ==UPDATE_REFS_NAME_CONFLICT ) + df_conflict =1 ; +if (ret) + goto fail; + +ref_transaction_free (transaction);return 0 ; +fail: +ref_transaction_free (transaction); +error ("%s" , err.buf); +strbuf_release (&err); +return df_conflict ?STORE_REF_ERROR_DF_CONFLICT + :STORE_REF_ERROR_OTHER ; }#define REFCOL_WIDTH 10
Signature · 1 line
2.0.0.582.ge25c160
PATCH v14 40/40 refs.c: make write_ref_sha1 static
In reply to Ronnie SahlbergNo external users call write_ref_sha1 any more so lets declare it static.
--- refs.c | 6 +++++- refs.h | 3 --- 2 files changed, 5 insertions(+), 4 deletions(-)
refs.c +5 -1
@@ -2634,6 +2634,9 @@ static int rename_tmp_log(const char *newrefname)return 0 ; } +static int write_ref_sha1 (struct ref_lock *lock,const unsigned char *sha1, +const char *logmsg); +int rename_ref (const char *oldrefname,const char *newrefname,const char *logmsg) {unsigned char sha1[20 ], orig_sha1[20 ];
@@ -2882,7 +2885,8 @@ static int is_branch(const char *refname)return !strcmp (refname,"HEAD" ) ||starts_with (refname,"refs/heads/" ); } -int write_ref_sha1 (struct ref_lock *lock, +/** Writes sha1 into the ref specified by the lock. **/ +static int write_ref_sha1 (struct ref_lock *lock,const unsigned char *sha1,const char *logmsg) {static char term ='\n' ;
refs.h +0 -3
@@ -195,9 +195,6 @@ extern int commit_ref(struct ref_lock *lock);/** Release any lock taken but not written. **/ extern void unlock_ref (struct ref_lock *lock); -/** Writes sha1 into the ref specified by the lock. **/ -extern int write_ref_sha1 (struct ref_lock *lock,const unsigned char *sha1,const char *msg); -/** Setup reflog before using. **/ int log_ref_setup (const char *refname,char *logfile,int bufsize);
Signature · 1 line
2.0.0.582.ge25c160