ref-transactions for reflogs
cover ref-transactions for reflogs
List, Jun,
This is the next patch series for ref-transactions. It is also available at
https://github.com/rsahlberg/git/tree/ref-transactions-reflog
and is the same patch series that has been posted previously with one exception:
This series now contains an additional patch that fixes ref handling of broken refs. This patch allows a user to delete a ref that can not be resolved and contains a broken SHA1.
The main part of the patch series is to refactor the reflog handling to use transactions and eventually leads up to :
* only a single place in the code where we marshall the reflog text line
* atomic transaction for reflog.c for performing the reflog expire operation.
This series is based on and builds on the previous series that is now in origin/pu:
===
commit c52f85eb59d26a7036d2149bc5b4347d0ecbbbeb
Merge: 44fc7ba 282edb2
Author: Junio C Hamano <[email protected]>
Date: Mon Jul 21 12:35:51 2014 -0700
Merge branch 'rs/ref-transaction' into jch
* rs/ref-transaction:
refs.c: fix handling of badly named refs
refs.c: make write_ref_sha1 static
fetch.c: change s_update_ref to use a ref transaction
refs.c: propagate any errno==ENOTDIR from _commit back to the callers refs.c: pass a skip list to name_conflict_fn
refs.c: call lock_ref_sha1_basic directly from commit
refs.c: move the check for valid refname to lock_ref_sha1_basic refs.c: pass NULL as *flags to read_ref_full
refs.c: pass the ref log message to _create/delete/update instead of _commit refs.c: add an err argument to delete_ref_loose
wrapper.c: add a new function unlink_or_msg
wrapper.c: simplify warn_if_unremovable
===
Ronnie Sahlberg (15):
refs.c make ref_transaction_create a wrapper to ref_transaction_update refs.c: make ref_transaction_delete a wrapper for
ref_transaction_update
refs.c: rename the transaction functions
refs.c: add a new update_type field to ref_update
refs.c: add a function to append a reflog entry to a fd lockfile.c: make hold_lock_file_for_append preserve meaningful errno refs.c: add a transaction function to append a reflog entry refs.c: add a flag to allow reflog updates to truncate the log refs.c: only write reflog update if msg is non-NULL refs.c: allow multiple reflog updates during a single transaction reflog.c: use a reflog transaction when writing during expire refs.c: rename log_ref_setup to create_reflog
refs.c: make unlock_ref/close_ref/commit_ref static refs.c: remove lock_any_ref_for_update
refs.c: allow deleting refs with a broken sha1
branch.c | 11 +- builtin/branch.c | 6 +- builtin/checkout.c | 8 +- builtin/commit.c | 14 +- builtin/fetch.c | 12 +- builtin/receive-pack.c | 14 +- builtin/reflog.c | 84 +++++------ builtin/replace.c | 10 +- builtin/tag.c | 10 +- builtin/update-ref.c | 22 +-- copy.c | 20 ++- fast-import.c | 22 +-- lockfile.c | 7 +- refs.c | 392 ++++++++++++++++++++++++++++++++++--------------- refs.h | 110 +++++++------- sequencer.c | 14 +- walker.c | 16 +- 17 files changed, 461 insertions(+), 311 deletions(-)
Signature · 1 line
2.0.1.508.g763ab16
PATCH 1/15 refs.c make ref_transaction_create a wrapper to ref_transaction_update
--- refs.c | 18 ++---------------- refs.h | 7 ++++--- 2 files changed, 6 insertions(+), 19 deletions(-)
refs.c +2 -16
@@ -3490,28 +3490,14 @@ int ref_transaction_create(struct ref_transaction *transaction, int flags, const char *msg, struct strbuf *err) { - 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" ); -if (check_refname_format (refname,REFNAME_ALLOW_ONELEVEL )) { -strbuf_addf (err,"Bad refname: %s" , refname); -return -1 ; - } - - update =add_update (transaction, refname); - -hashcpy (update->new_sha1, new_sha1); -hashclr (update->old_sha1); - update->flags = flags; - update->have_old =1 ; -if (msg) - update->msg =xstrdup (msg); -return 0 ; +return ref_transaction_update (transaction, refname, new_sha1, + null_sha1, flags,1 , msg, err); } int ref_transaction_delete(struct ref_transaction *transaction,
refs.h +4 -3
@@ -276,9 +276,10 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);/* * Add a reference update to transaction. new_sha1 is the value that * the reference should have after the update, or zeros if it should -* 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. +* be deleted. If have_old is true and old_sha is not the null_sha1 +* then the previous value of the ref must match or the update will fail. +* If have_old is true and old_sha1 is the null_sha1 then the ref must not +* already exist and a new ref will be created with new_sha1. * 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.
Signature · 1 line
2.0.1.508.g763ab16
PATCH 2/15 refs.c: make ref_transaction_delete a wrapper for ref_transaction_update
In reply to Ronnie Sahlberg--- refs.c | 17 +++++------------ refs.h | 2 +- 2 files changed, 6 insertions(+), 13 deletions(-)
refs.c +5 -12
@@ -3506,24 +3506,17 @@ int ref_transaction_delete(struct ref_transaction *transaction, int flags, int have_old, const char *msg, struct strbuf *err) { - 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" ); - 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); - } -if (msg) - update->msg =xstrdup (msg); -return 0 ; +if (have_old &&is_null_sha1 (old_sha1)) +die ("BUG: have_old is true but old_sha1 is null_sha1" ); + +return ref_transaction_update (transaction, refname, null_sha1, + old_sha1, flags, have_old, msg, err); } int update_ref(const char *action, const char *refname,
refs.h +1 -1
@@ -275,7 +275,7 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);/* * Add a reference update to transaction. new_sha1 is the value that -* the reference should have after the update, or zeros if it should +* the reference should have after the update, or null_sha1 if it should * be deleted. If have_old is true and old_sha is not the null_sha1 * then the previous value of the ref must match or the update will fail. * If have_old is true and old_sha1 is the null_sha1 then the ref must not
Signature · 1 line
2.0.1.508.g763ab16
PATCH 3/15 refs.c: rename the transaction functions
In reply to Ronnie SahlbergRename the transaction functions. Remove the leading ref_ from the names and append _sha1 to the names for functions that create/delete/ update sha1 refs.
--- branch.c | 11 ++++--- builtin/commit.c | 14 ++++----- builtin/fetch.c | 12 ++++---- builtin/receive-pack.c | 14 ++++----- builtin/replace.c | 10 +++--- builtin/tag.c | 10 +++--- builtin/update-ref.c | 22 +++++++------- fast-import.c | 22 +++++++------- refs.c | 82 +++++++++++++++++++++++++------------------------- refs.h | 54 ++++++++++++++++----------------- sequencer.c | 14 ++++----- walker.c | 16 +++++----- 12 files changed, 141 insertions(+), 140 deletions(-)
branch.c +6 -5
@@ -298,13 +298,14 @@ void create_branch(const char *head,struct ref_transaction *transaction;struct strbuf err =STRBUF_INIT ; - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction, ref.buf, sha1, - null_sha1,0 , !forcing, msg, &err) || -ref_transaction_commit (transaction, &err)) +transaction_update_sha1 (transaction, ref.buf, sha1, + null_sha1,0 , !forcing, msg, + &err) || +transaction_commit (transaction, &err))die ("%s" , err.buf); -ref_transaction_free (transaction); +transaction_free (transaction); }if (real_ref && track)
builtin/commit.c +7 -7
@@ -1762,17 +1762,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 ); - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction,"HEAD" , sha1, - current_head ? - current_head->object.sha1 :NULL , -0 , !!current_head, sb.buf, &err) || -ref_transaction_commit (transaction, &err)) { +transaction_update_sha1 (transaction,"HEAD" , sha1, + current_head ? + current_head->object.sha1 :NULL , +0 , !!current_head, sb.buf, &err) || +transaction_commit (transaction, &err)) {rollback_index_files ();die ("%s" , err.buf); } -ref_transaction_free (transaction); +transaction_free (transaction);unlink (git_path ("CHERRY_PICK_HEAD" ));unlink (git_path ("REVERT_HEAD" ));
builtin/fetch.c +6 -6
@@ -414,22 +414,22 @@ static int s_update_ref(const char *action, rla = default_rla.buf;snprintf (msg,sizeof (msg),"%s: %s" , rla, action); - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction, ref->name, ref->new_sha1, - ref->old_sha1,0 , check_old, msg, &err)) +transaction_update_sha1 (transaction, ref->name, ref->new_sha1, + ref->old_sha1,0 , check_old, msg, &err)) goto fail; - ret =ref_transaction_commit (transaction, &err); + ret =transaction_commit (transaction, &err);if (ret ==UPDATE_REFS_NAME_CONFLICT ) df_conflict =1 ;if (ret) goto fail; -ref_transaction_free (transaction); +transaction_free (transaction);return 0 ; fail: -ref_transaction_free (transaction); +transaction_free (transaction);error ("%s" , err.buf);strbuf_release (&err); return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
builtin/receive-pack.c +7 -7
@@ -582,20 +582,20 @@ static char *update(struct command *cmd, struct shallow_info *si) update_shallow_ref(cmd, si))return xstrdup ("shallow error" ); - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction, namespaced_name, - new_sha1, old_sha1,0 ,1 ,"push" , - &err) || -ref_transaction_commit (transaction, &err)) { +transaction_update_sha1 (transaction, namespaced_name, + new_sha1, old_sha1,0 ,1 ,"push" , + &err) || +transaction_commit (transaction, &err)) {char *str =strbuf_detach (&err,NULL ); -ref_transaction_free (transaction); +transaction_free (transaction);rp_error ("%s" , str);return str; } -ref_transaction_free (transaction); +transaction_free (transaction);strbuf_release (&err);return NULL ;/* good */ }
builtin/replace.c +5 -5
@@ -167,14 +167,14 @@ static int replace_object_sha1(const char *object_ref,check_ref_valid (object, prev, ref,sizeof (ref), force); - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction, ref, repl, prev, -0 , !is_null_sha1 (prev),NULL , &err) || -ref_transaction_commit (transaction, &err)) +transaction_update_sha1 (transaction, ref, repl, prev, +0 , !is_null_sha1 (prev),NULL , &err) || +transaction_commit (transaction, &err))die ("%s" , err.buf); -ref_transaction_free (transaction); +transaction_free (transaction);return 0 ; }
builtin/tag.c +5 -5
@@ -702,13 +702,13 @@ int cmd_tag(int argc, const char **argv, const char *prefix)if (annotate)create_tag (object, tag, &buf, &opt, prev, object); - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction, ref.buf, object, prev, -0 ,1 ,NULL , &err) || -ref_transaction_commit (transaction, &err)) +transaction_update_sha1 (transaction, ref.buf, object, prev, +0 ,1 ,NULL , &err) || +transaction_commit (transaction, &err))die ("%s" , err.buf); -ref_transaction_free (transaction); +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 ));
builtin/update-ref.c +11 -11
@@ -199,8 +199,8 @@ static const char *parse_cmd_update(struct strbuf *input, const char *next)if (*next != line_termination)die ("update %s: extra input: %s" , refname, next); -if (ref_transaction_update (transaction, refname, new_sha1, old_sha1, - update_flags, have_old, msg, &err)) +if (transaction_update_sha1 (transaction, refname, new_sha1, old_sha1, + update_flags, have_old, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -227,8 +227,8 @@ static const char *parse_cmd_create(struct strbuf *input, const char *next)if (*next != line_termination)die ("create %s: extra input: %s" , refname, next); -if (ref_transaction_create (transaction, refname, new_sha1, - update_flags, msg, &err)) +if (transaction_create_sha1 (transaction, refname, new_sha1, + update_flags, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -259,8 +259,8 @@ static const char *parse_cmd_delete(struct strbuf *input, const char *next)if (*next != line_termination)die ("delete %s: extra input: %s" , refname, next); -if (ref_transaction_delete (transaction, refname, old_sha1, - update_flags, have_old, msg, &err)) +if (transaction_delete_sha1 (transaction, refname, old_sha1, + update_flags, have_old, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -292,8 +292,8 @@ static const char *parse_cmd_verify(struct strbuf *input, const char *next)if (*next != line_termination)die ("verify %s: extra input: %s" , refname, next); -if (ref_transaction_update (transaction, refname, new_sha1, old_sha1, - update_flags, have_old, msg, &err)) +if (transaction_update_sha1 (transaction, refname, new_sha1, old_sha1, + update_flags, have_old, msg, &err))die ("%s" , err.buf); update_flags =0 ;
@@ -366,15 +366,15 @@ 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 (&err); + transaction =transaction_begin (&err);if (delete || no_deref || argc >0 )usage_with_options (git_update_ref_usage, options);if (end_null) line_termination ='\0' ;update_refs_stdin (); -if (ref_transaction_commit (transaction, &err)) +if (transaction_commit (transaction, &err))die ("%s" , err.buf); -ref_transaction_free (transaction); +transaction_free (transaction);return 0 ; }
fast-import.c +11 -11
@@ -1705,17 +1705,17 @@ static int update_branch(struct branch *b)return -1 ; } } - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction, b->name, b->sha1, old_sha1, -0 ,1 , msg, &err) || -ref_transaction_commit (transaction, &err)) { -ref_transaction_free (transaction); +transaction_update_sha1 (transaction, b->name, b->sha1, old_sha1, +0 ,1 , msg, &err) || +transaction_commit (transaction, &err)) { +transaction_free (transaction);error ("%s" , err.buf);strbuf_release (&err);return -1 ; } -ref_transaction_free (transaction); +transaction_free (transaction);return 0 ; }
@@ -1738,7 +1738,7 @@ static void dump_tags(void)struct strbuf err =STRBUF_INIT ;struct ref_transaction *transaction; - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction) { failure |=error ("%s" , err.buf); goto cleanup;
@@ -1747,17 +1747,17 @@ static void dump_tags(void)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 , msg, &err)) { +if (transaction_update_sha1 (transaction, ref_name.buf, t->sha1, +NULL ,0 ,0 , msg, &err)) { failure |=error ("%s" , err.buf); goto cleanup; } } -if (ref_transaction_commit (transaction, &err)) +if (transaction_commit (transaction, &err)) failure |=error ("%s" , err.buf); cleanup: -ref_transaction_free (transaction); +transaction_free (transaction);strbuf_release (&ref_name);strbuf_release (&err); }
refs.c +41 -41
161 lines not on this page · raw message
refs.h +27 -27
107 lines not on this page · raw message
sequencer.c +7 -7
@@ -282,13 +282,13 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,strbuf_addf (&sb,"%s: fast-forward" ,action_name (opts)); - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction || -ref_transaction_update (transaction,"HEAD" , - to, unborn ? null_sha1 : from, -0 ,1 , sb.buf, &err) || -ref_transaction_commit (transaction, &err)) { -ref_transaction_free (transaction); +transaction_update_sha1 (transaction,"HEAD" , + to, unborn ? null_sha1 : from, +0 ,1 , sb.buf, &err) || +transaction_commit (transaction, &err)) { +transaction_free (transaction);error ("%s" , err.buf);strbuf_release (&sb);strbuf_release (&err);
@@ -296,7 +296,7 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from, }strbuf_release (&sb); -ref_transaction_free (transaction); +transaction_free (transaction);return 0 ; }
walker.c +8 -8
@@ -261,7 +261,7 @@ int walker_fetch(struct walker *walker, int targets, char **target, save_commit_buffer =0 ;if (write_ref) { - transaction =ref_transaction_begin (&err); + transaction =transaction_begin (&err);if (!transaction) {error ("%s" , err.buf); goto rollback_and_fail;
@@ -293,27 +293,27 @@ int walker_fetch(struct walker *walker, int targets, char **target,continue ;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 , - msg ? msg :"fetch (unknown)" , - &err)) { +if (transaction_update_sha1 (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, &err)) { +if (transaction_commit (transaction, &err)) {error ("%s" , err.buf); goto rollback_and_fail; } -ref_transaction_free (transaction); +transaction_free (transaction); }free (msg);return 0 ; rollback_and_fail: -ref_transaction_free (transaction); +transaction_free (transaction);free (msg);strbuf_release (&err);strbuf_release (&ref_name);
Signature · 1 line
2.0.1.508.g763ab16
PATCH 4/15 refs.c: add a new update_type field to ref_update
In reply to Ronnie SahlbergAdd a field that describes what type of update this refers to. For now the only type is UPDATE_SHA1 but we will soon add more types.
--- refs.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
refs.c +21 -4
@@ -3372,6 +3372,10 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)return retval; } +enum transaction_update_type { +UPDATE_SHA1 =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
@@ -3379,6 +3383,7 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)* value or to zero to ensure the ref does not exist before update. */ struct ref_update { +enum transaction_update_type update_type;unsigned char new_sha1[20 ];unsigned char old_sha1[20 ];int flags;/* REF_NODEREF? */
@@ -3441,12 +3446,14 @@ void transaction_free(struct ref_transaction *transaction) }static struct ref_update *add_update (struct ref_transaction *transaction, -const char *refname) +const char *refname, +enum transaction_update_type update_type) {size_t len =strlen (refname);struct ref_update *update =xcalloc (1 ,sizeof (*update) + len +1 );strcpy ((char *)update->refname, refname); + update->update_type = update_type;ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); transaction->updates[transaction->nr++] = update;return update;
@@ -3473,7 +3480,7 @@ int transaction_update_sha1(struct ref_transaction *transaction,return -1 ; } - update =add_update (transaction, refname); + update =add_update (transaction, refname,UPDATE_SHA1 );hashcpy (update->new_sha1, new_sha1); update->flags = flags; update->have_old = have_old;
@@ -3561,7 +3568,10 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n, struct strbuf *err) {int i; -for (i =1 ; i < n; i++) +for (i =1 ; i < n; i++) { +if (updates[i -1 ]->update_type !=UPDATE_SHA1 || + updates[i]->update_type !=UPDATE_SHA1 ) +continue ;if (!strcmp (updates[i -1 ]->refname, updates[i]->refname)) {const char *str ="Multiple updates for ref '%s' not allowed." ;
@@ -3570,6 +3580,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,return 1 ; } + }return 0 ; }
@@ -3599,10 +3610,12 @@ int transaction_commit(struct ref_transaction *transaction, goto cleanup; } -/* Acquire all locks while verifying old values */ +/* Acquire all ref locks while verifying old values */ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; +if (update->update_type !=UPDATE_SHA1 ) +continue ; update->lock = lock_ref_sha1_basic(update->refname, (update->have_old ? update->old_sha1 :
@@ -3625,6 +3638,8 @@ int transaction_commit(struct ref_transaction *transaction,for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; +if (update->update_type !=UPDATE_SHA1 ) +continue ;if (!is_null_sha1 (update->new_sha1)) { ret =write_ref_sha1 (update->lock, update->new_sha1, update->msg);
@@ -3644,6 +3659,8 @@ int transaction_commit(struct ref_transaction *transaction,for (i =0 ; i < n; i++) {struct ref_update *update = updates[i]; +if (update->update_type !=UPDATE_SHA1 ) +continue ;if (update->lock) {if (delete_ref_loose (update->lock, update->type, err)) ret =-1 ;
Signature · 1 line
2.0.1.508.g763ab16
PATCH 5/15 refs.c: add a function to append a reflog entry to a fd
In reply to Ronnie SahlbergBreak out the code to create the string and writing it to the file descriptor from log_ref_write and into a dedicated function log_ref_write_fd. For now this is only used from log_ref_write but later on we will call this function from reflog transactions too which means that we will end up with only a single place where we write a reflog entry to a file instead of the current two places (log_ref_write and builtin/reflog.c).
--- refs.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-)
refs.c +30 -18
@@ -2862,15 +2862,37 @@ int log_ref_setup(const char *refname, char *logfile, int bufsize)return 0 ; } +static int log_ref_write_fd (int fd,const unsigned char *old_sha1, +const unsigned char *new_sha1, +const char *committer,const char *msg) +{ +int msglen, written; +unsigned maxlen, len; +char *logrec; + + msglen = msg ?strlen (msg) :0 ; + maxlen =strlen (committer) + msglen +100 ; + logrec =xmalloc (maxlen); + len =sprintf (logrec,"%s %s %s\n" , +sha1_to_hex (old_sha1), +sha1_to_hex (new_sha1), + committer); +if (msglen) + len +=copy_msg (logrec + len -1 , msg) -1 ; + + written = len <= maxlen ?write_in_full (fd, logrec, len) :-1 ; +free (logrec); +if (written != len) +return -1 ; + +return 0 ; +} +static int log_ref_write (const char *refname,const unsigned char *old_sha1,const unsigned char *new_sha1,const char *msg) { -int logfd, result, written, oflags =O_APPEND |O_WRONLY ; -unsigned maxlen, len; -int msglen; +int logfd, result, oflags =O_APPEND |O_WRONLY ;char log_file[PATH_MAX ]; -char *logrec; -const char *committer;if (log_all_ref_updates <0 ) log_all_ref_updates = !is_bare_repository ();
@@ -2882,19 +2904,9 @@ static int log_ref_write(const char *refname, const unsigned char *old_sha1, logfd =open (log_file, oflags);if (logfd <0 )return 0 ; - msglen = msg ?strlen (msg) :0 ; - committer =git_committer_info (0 ); - maxlen =strlen (committer) + msglen +100 ; - logrec =xmalloc (maxlen); - len =sprintf (logrec,"%s %s %s\n" , -sha1_to_hex (old_sha1), -sha1_to_hex (new_sha1), - committer); -if (msglen) - len +=copy_msg (logrec + len -1 , msg) -1 ; - written = len <= maxlen ?write_in_full (logfd, logrec, len) :-1 ; -free (logrec); -if (written != len) { + result =log_ref_write_fd (logfd, old_sha1, new_sha1, +git_committer_info (0 ), msg); +if (result) {int save_errno = errno;close (logfd);error ("Unable to append to %s" , log_file);
Signature · 1 line
2.0.1.508.g763ab16
PATCH 6/15 lockfile.c: make hold_lock_file_for_append preserve meaningful errno
In reply to Ronnie SahlbergUpdate hold_lock_file_for_append and copy_fd to return a meaningful errno on failure.
--- copy.c | 20 +++++++++++++------- lockfile.c | 7 ++++++- 2 files changed, 19 insertions(+), 8 deletions(-)
copy.c +13 -7
@@ -9,10 +9,12 @@ int copy_fd(int ifd, int ofd)if (!len)break ;if (len <0 ) { -int read_error = errno; +int save_errno = errno;close (ifd); -return error ("copy-fd: read returned %s" , -strerror (read_error)); +error ("copy-fd: read returned %s" , +strerror (save_errno)); + errno = save_errno; +return -1 ; }while (len) {int written =xwrite (ofd, buf, len);
@@ -22,12 +24,16 @@ int copy_fd(int ifd, int ofd) }else if (!written) {close (ifd); -return error ("copy-fd: write returned 0" ); +error ("copy-fd: write returned 0" ); + errno =EAGAIN ; +return -1 ; }else { -int write_error = errno; +int save_errno = errno;close (ifd); -return error ("copy-fd: write returned %s" , -strerror (write_error)); +error ("copy-fd: write returned %s" , +strerror (save_errno)); + errno = save_errno; +return -1 ; } } }
lockfile.c +6 -1
@@ -217,15 +217,20 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags) orig_fd =open (path,O_RDONLY );if (orig_fd <0 ) {if (errno !=ENOENT ) { +int save_errno = errno;if (flags &LOCK_DIE_ON_ERROR )die ("cannot open '%s' for copying" , path);close (fd); -return error ("cannot open '%s' for copying" , path); +error ("cannot open '%s' for copying" , path); + errno = save_errno; +return -1 ; } }else if (copy_fd (orig_fd, fd)) { +int save_errno = errno;if (flags &LOCK_DIE_ON_ERROR )exit (128 );close (fd); + errno = save_errno;return -1 ; }return fd;
Signature · 1 line
2.0.1.508.g763ab16
PATCH 7/15 refs.c: add a transaction function to append a reflog entry
In reply to Ronnie SahlbergDefine a new transaction update type, UPDATE_LOG, and a new function transaction_update_reflog. This function will lock the reflog and append an entry to it during transaction commit.
--- refs.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- refs.h | 12 ++++++++ 2 files changed, 113 insertions(+), 2 deletions(-)
refs.c +101 -2
@@ -3385,7 +3385,8 @@ int for_each_reflog(each_ref_fn fn, void *cb_data) }enum transaction_update_type { -UPDATE_SHA1 =0 +UPDATE_SHA1 =0 , +UPDATE_LOG =1 };/**
@@ -3403,6 +3404,12 @@ struct ref_update {struct ref_lock *lock;int type;char *msg; + +/* used by reflog updates */ +int reflog_fd; +struct lock_file reflog_lock; +char *committer; +const char refname[FLEX_ARRAY ]; };
@@ -3451,6 +3458,7 @@ void transaction_free(struct ref_transaction *transaction)for (i =0 ; i < transaction->nr; i++) {free (transaction->updates[i]->msg); +free (transaction->updates[i]->committer);free (transaction->updates[i]); }free (transaction->updates);
@@ -3471,6 +3479,42 @@ static struct ref_update *add_update(struct ref_transaction *transaction,return update; } +int transaction_update_reflog (struct ref_transaction *transaction, +const char *refname, +const unsigned char *new_sha1, +const unsigned char *old_sha1, +const unsigned char *email, +unsigned long timestamp,int tz, +const char *msg,int flags, +struct strbuf *err) +{ +struct ref_update *update; + +if (transaction->state !=REF_TRANSACTION_OPEN ) +die ("BUG: update_reflog called for transaction that is not " +"open" ); + + update =add_update (transaction, refname,UPDATE_LOG ); +hashcpy (update->new_sha1, new_sha1); +hashcpy (update->old_sha1, old_sha1); + update->reflog_fd =-1 ; +if (email) { +struct strbuf buf =STRBUF_INIT ; +char sign = (tz <0 ) ?'-' :'+' ; +int zone = (tz <0 ) ? (-tz) : tz; + +strbuf_addf (&buf,"%s %lu %c%04d" , email, timestamp, sign, + zone); + update->committer =xstrdup (buf.buf); +strbuf_release (&buf); + } +if (msg) + update->msg =xstrdup (msg); + update->flags = flags; + +return 0 ; +} + int transaction_update_sha1(struct ref_transaction *transaction, const char *refname, const unsigned char *new_sha1,
@@ -3646,7 +3690,28 @@ int transaction_commit(struct ref_transaction *transaction, } } -/* Perform updates first so live commits remain referenced */ +/* Lock all reflog files */ +for (i =0 ; i < n; i++) { +struct ref_update *update = updates[i]; + +if (update->update_type !=UPDATE_LOG ) +continue ; + update->reflog_fd =hold_lock_file_for_append ( + &update->reflog_lock, +git_path ("logs/%s" , update->refname), +0 ); +if (update->reflog_fd <0 ) { +const char *str ="Cannot lock reflog for '%s'. %s" ; + + ret =-1 ; +if (err) +strbuf_addf (err, str, update->refname, +strerror (errno)); + goto cleanup; + } + } + +/* Perform ref updates first so live commits remain referenced */ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i];
@@ -3682,6 +3747,40 @@ int transaction_commit(struct ref_transaction *transaction, } } +/* Update all reflog files */ +for (i =0 ; i < n; i++) { +struct ref_update *update = updates[i]; + +if (update->update_type !=UPDATE_LOG ) +continue ; +if (update->reflog_fd ==-1 ) +continue ; + +if (log_ref_write_fd (update->reflog_fd, update->old_sha1, + update->new_sha1, + update->committer, update->msg)) { +error ("Could write to reflog: %s. %s" , + update->refname,strerror (errno)); +rollback_lock_file (&update->reflog_lock); + update->reflog_fd =-1 ; + } + } + +/* Commit all reflog files */ +for (i =0 ; i < n; i++) { +struct ref_update *update = updates[i]; + +if (update->update_type !=UPDATE_LOG ) +continue ; +if (update->reflog_fd ==-1 ) +continue ; +if (commit_lock_file (&update->reflog_lock)) { +error ("Could not commit reflog: %s. %s" , + update->refname,strerror (errno)); + update->reflog_fd =-1 ; + } + } +if (repack_without_refs (delnames, delnum, err)) ret =-1 ;for (i =0 ; i < delnum; i++)
refs.h +12 -0
@@ -321,6 +321,18 @@ int transaction_delete_sha1(struct ref_transaction *transaction, struct strbuf *err);/* +* Append a reflog entry for refname. +*/ +int transaction_update_reflog (struct ref_transaction *transaction, +const char *refname, +const unsigned char *new_sha1, +const unsigned char *old_sha1, +const unsigned char *email, +unsigned long timestamp,int tz, +const char *msg,int flags, +struct strbuf *err); + +/* * Commit all of the changes that have been queued in transaction, as * atomically as possible. Return a nonzero value if there is a * problem.
Signature · 1 line
2.0.1.508.g763ab16
PATCH 8/15 refs.c: add a flag to allow reflog updates to truncate the log
In reply to Ronnie SahlbergAdd a flag that allows us to truncate the reflog before we write the update.
--- refs.c | 17 +++++++++++++++-- refs.h | 10 +++++++++- 2 files changed, 24 insertions(+), 3 deletions(-)
refs.c +15 -2
@@ -3747,7 +3747,12 @@ int transaction_commit(struct ref_transaction *transaction, } } -/* Update all reflog files */ +/* +* Update all reflog files +* We have already done all ref updates and deletes. +* There is not much we can do here if there are any reflog +* update errors other than complain. +*/ for (i =0 ; i < n; i++) {struct ref_update *update = updates[i];
@@ -3755,7 +3760,15 @@ int transaction_commit(struct ref_transaction *transaction,continue ;if (update->reflog_fd ==-1 )continue ; - +if (update->flags &REFLOG_TRUNCATE ) +if (lseek (update->reflog_fd,0 ,SEEK_SET ) <0 || +ftruncate (update->reflog_fd,0 )) { +error ("Could not truncate reflog: %s. %s" , + update->refname,strerror (errno)); +rollback_lock_file (&update->reflog_lock); + update->reflog_fd =-1 ; +continue ; + }if (log_ref_write_fd (update->reflog_fd, update->old_sha1, update->new_sha1, update->committer, update->msg)) {
refs.h +9 -1
@@ -321,7 +321,15 @@ int transaction_delete_sha1(struct ref_transaction *transaction, struct strbuf *err);/* -* Append a reflog entry for refname. +* Flags controlling transaction_update_reflog(). +* REFLOG_TRUNCATE: Truncate the reflog. +* +* Flags >= 0x100 are reserved for internal use. +*/ +#define REFLOG_TRUNCATE 0x01 +/* +* Append a reflog entry for refname. If the REFLOG_TRUNCATE flag is set +* this update will first truncate the reflog before writing the entry. */ int transaction_update_reflog(struct ref_transaction *transaction, const char *refname,
Signature · 1 line
2.0.1.508.g763ab16
PATCH 9/15 refs.c: only write reflog update if msg is non-NULL
In reply to Ronnie SahlbergWhen performing a reflog transaction update, only write to the reflog iff msg is non-NULL. This can then be combined with REFLOG_TRUNCATE to perform an update that only truncates but does not write.
--- refs.c | 5 +++-- refs.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-)
refs.c +3 -2
@@ -3769,8 +3769,9 @@ int transaction_commit(struct ref_transaction *transaction, update->reflog_fd =-1 ;continue ; } -if (log_ref_write_fd (update->reflog_fd, update->old_sha1, - update->new_sha1, +if (update->msg && +log_ref_write_fd (update->reflog_fd, + update->old_sha1, update->new_sha1, update->committer, update->msg)) {error ("Could write to reflog: %s. %s" , update->refname,strerror (errno));
refs.h +1 -0
@@ -330,6 +330,7 @@ int transaction_delete_sha1(struct ref_transaction *transaction,/* * Append a reflog entry for refname. If the REFLOG_TRUNCATE flag is set * this update will first truncate the reflog before writing the entry. +* If msg is NULL no update will be written to the log. */ int transaction_update_reflog(struct ref_transaction *transaction, const char *refname,
Signature · 1 line
2.0.1.508.g763ab16
PATCH 10/15 refs.c: allow multiple reflog updates during a single transaction
In reply to Ronnie SahlbergAllow to make multiple reflog updates to the same ref during a transaction. This means we only need to lock the reflog once, during the first update that touches the reflog, and that all further updates can just write the reflog entry since the reflog is already locked.
This allows us to write code such as:
t = transaction_begin() transaction_reflog_update(t, "foo", REFLOG_TRUNCATE, NULL); loop-over-somehting...
transaction_reflog_update(t, "foo", 0, <message>);transaction_commit(t)
where we first truncate the reflog and then build the new content one line at a time.
--- refs.c | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-)
refs.c +37 -9
@@ -30,6 +30,12 @@ static unsigned char refname_disposition[256] = {*/ #define REF_ISPRUNING 0x0100/* +* Only the first reflog update needs to lock the reflog file. Further updates +* just use the lock taken by the first update. +*/ +#define UPDATE_REFLOG_NOLOCK 0x0200 + +/* * 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. It is legal if it is something reasonable to have under
@@ -3389,7 +3395,7 @@ enum transaction_update_type { UPDATE_LOG = 1 }; -/** +/* * 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 * while locking the ref, set have_old to 1 and set old_sha1 to the
@@ -3399,7 +3405,7 @@ struct ref_update {enum transaction_update_type update_type;unsigned char new_sha1[20 ];unsigned char old_sha1[20 ]; -int flags;/* REF_NODEREF? */ +int flags;/* REF_NODEREF? or private flags */ int have_old;/* 1 if old_sha1 is valid, 0 otherwise */ struct ref_lock *lock;int type;
@@ -3407,8 +3413,9 @@ struct ref_update {/* used by reflog updates */ int reflog_fd; -struct lock_file reflog_lock; +struct lock_file *reflog_lock;char *committer; +struct ref_update *orig_update;/* For UPDATE_REFLOG_NOLOCK */ const char refname[FLEX_ARRAY ]; };
@@ -3489,12 +3496,27 @@ int transaction_update_reflog(struct ref_transaction *transaction, struct strbuf *err) {struct ref_update *update; +int i;if (transaction->state !=REF_TRANSACTION_OPEN )die ("BUG: update_reflog called for transaction that is not " "open" ); update =add_update (transaction, refname,UPDATE_LOG ); + update->flags = flags; +for (i =0 ; i < transaction->nr -1 ; i++) { +if (transaction->updates[i]->update_type !=UPDATE_LOG ) +continue ; +if (!strcmp (transaction->updates[i]->refname, + update->refname)) { + update->flags |=UPDATE_REFLOG_NOLOCK ; + update->orig_update = transaction->updates[i]; +break ; + } + } +if (!(update->flags &UPDATE_REFLOG_NOLOCK )) + update->reflog_lock =xcalloc (1 ,sizeof (struct lock_file )); +hashcpy (update->new_sha1, new_sha1);hashcpy (update->old_sha1, old_sha1); update->reflog_fd =-1 ;
@@ -3510,7 +3532,6 @@ int transaction_update_reflog(struct ref_transaction *transaction, }if (msg) update->msg =xstrdup (msg); - update->flags = flags;return 0 ; }
@@ -3696,10 +3717,15 @@ int transaction_commit(struct ref_transaction *transaction,if (update->update_type !=UPDATE_LOG )continue ; +if (update->flags &UPDATE_REFLOG_NOLOCK ) { + update->reflog_fd = update->orig_update->reflog_fd; + update->reflog_lock = update->orig_update->reflog_lock; +continue ; + } update->reflog_fd =hold_lock_file_for_append ( - &update->reflog_lock, + update->reflog_lock,git_path ("logs/%s" , update->refname), -0 ); +LOCK_NODEREF );if (update->reflog_fd <0 ) {const char *str ="Cannot lock reflog for '%s'. %s" ;
@@ -3765,7 +3791,7 @@ int transaction_commit(struct ref_transaction *transaction, ftruncate(update->reflog_fd, 0)) {error ("Could not truncate reflog: %s. %s" , update->refname,strerror (errno)); -rollback_lock_file (&update->reflog_lock); +rollback_lock_file (update->reflog_lock); update->reflog_fd =-1 ;continue ; }
@@ -3775,7 +3801,7 @@ int transaction_commit(struct ref_transaction *transaction, update->committer, update->msg)) {error ("Could write to reflog: %s. %s" , update->refname,strerror (errno)); -rollback_lock_file (&update->reflog_lock); +rollback_lock_file (update->reflog_lock); update->reflog_fd =-1 ; } }
@@ -3786,9 +3812,11 @@ int transaction_commit(struct ref_transaction *transaction,if (update->update_type !=UPDATE_LOG )continue ; +if (update->flags &UPDATE_REFLOG_NOLOCK ) +continue ;if (update->reflog_fd ==-1 )continue ; -if (commit_lock_file (&update->reflog_lock)) { +if (commit_lock_file (update->reflog_lock)) {error ("Could not commit reflog: %s. %s" , update->refname,strerror (errno)); update->reflog_fd =-1 ;
Signature · 1 line
2.0.1.508.g763ab16
PATCH 11/15 reflog.c: use a reflog transaction when writing during expire
In reply to Ronnie SahlbergUse a transaction for all updates during expire_reflog.
--- builtin/reflog.c | 84 ++++++++++++++++++++++++-------------------------------- refs.c | 4 +-- refs.h | 2 +- 3 files changed, 39 insertions(+), 51 deletions(-)
builtin/reflog.c +36 -48
@@ -32,8 +32,11 @@ struct cmd_reflog_expire_cb {int recno; }; +static struct strbuf err =STRBUF_INIT ; +struct expire_reflog_cb { -FILE *newlog; +struct ref_transaction *t; +const char *refname;enum {UE_NORMAL ,UE_ALWAYS ,
@@ -316,20 +319,18 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,if (cb->cmd->recno && --(cb->cmd->recno) ==0 ) goto prune; -if (cb->newlog) { -char sign = (tz <0 ) ?'-' :'+' ; -int zone = (tz <0 ) ? (-tz) : tz; -fprintf (cb->newlog,"%s %s %s %lu %c%04d\t%s" , -sha1_to_hex (osha1),sha1_to_hex (nsha1), - email, timestamp, sign, zone, - message); +if (cb->t) { +if (transaction_update_reflog (cb->t, cb->refname, nsha1, osha1, + email, timestamp, tz, message,0 , + &err)) +return -1 ;hashcpy (cb->last_kept_sha1, nsha1); }if (cb->cmd->verbose)printf ("keep %s" , message);return 0 ; prune: -if (!cb->newlog) +if (!cb->t)printf ("would prune %s" , message);else if (cb->cmd->verbose)printf ("prune %s" , message);
@@ -353,29 +354,26 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, {struct cmd_reflog_expire_cb *cmd = cb_data;struct expire_reflog_cb cb; -struct ref_lock *lock; -char *log_file, *newlog_path =NULL ;struct commit *tip_commit;struct commit_list *tips;int status =0 ;memset (&cb,0 ,sizeof (cb)); + cb.refname = ref; -/* -* we take the lock for the ref itself to prevent it from -* getting updated. -*/ - lock =lock_any_ref_for_update (ref, sha1,0 ,NULL ); -if (!lock) -return error ("cannot lock ref '%s'" , ref); - log_file =git_pathdup ("logs/%s" , ref);if (!reflog_exists (ref)) goto finish; -if (!cmd->dry_run) { - newlog_path =git_pathdup ("logs/%s.lock" , ref); - cb.newlog =fopen (newlog_path,"w" ); + cb.t =transaction_begin (&err); +if (!cb.t) { + status |=error ("%s" , err.buf); + goto cleanup; + } +if (transaction_update_reflog (cb.t, cb.refname, null_sha1, null_sha1, +NULL ,0 ,0 ,NULL ,REFLOG_TRUNCATE , + &err)) { + status |=error ("%s" , err.buf); + goto cleanup; } - cb.cmd = cmd;if (!cmd->expire_unreachable || !strcmp (ref,"HEAD" )) {
@@ -407,7 +405,10 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,mark_reachable (&cb); } -for_each_reflog_ent (ref, expire_reflog_ent, &cb); +if (for_each_reflog_ent (ref, expire_reflog_ent, &cb)) { + status |=error ("%s" , err.buf); + goto cleanup; + }if (cb.unreachable_expire_kind !=UE_ALWAYS ) {if (cb.unreachable_expire_kind ==UE_HEAD ) {
@@ -420,32 +421,19 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, } } finish: -if (cb.newlog) { -if (fclose (cb.newlog)) { - status |=error ("%s: %s" ,strerror (errno), - newlog_path); -unlink (newlog_path); - }else if (cmd->updateref && - (write_in_full (lock->lock_fd, -sha1_to_hex (cb.last_kept_sha1),40 ) !=40 || -write_str_in_full (lock->lock_fd,"\n" ) !=1 || -close_ref (lock) <0 )) { - status |=error ("Couldn't write %s" , - lock->lk->filename); -unlink (newlog_path); - }else if (rename (newlog_path, log_file)) { - status |=error ("cannot rename %s to %s" , - newlog_path, log_file); -unlink (newlog_path); - }else if (cmd->updateref &&commit_ref (lock)) { - status |=error ("Couldn't set %s" , lock->ref_name); - }else { -adjust_shared_perm (log_file); +if (!cmd->dry_run) { +if (cmd->updateref && +transaction_update_sha1 (cb.t, cb.refname, + cb.last_kept_sha1, sha1, +0 ,1 ,NULL , &err)) { + status |=error ("%s" , err.buf); + }else if (transaction_commit (cb.t, &err)) { + status |=error ("%s" , err.buf); } } -free (newlog_path); -free (log_file); -unlock_ref (lock); + cleanup: +transaction_free (cb.t); +strbuf_release (&err);return status; }
refs.c +2 -2
@@ -3490,7 +3490,7 @@ int transaction_update_reflog(struct ref_transaction *transaction,const char *refname, const unsigned char *new_sha1, const unsigned char *old_sha1, - const unsigned char *email, + const char *email, unsigned long timestamp, int tz, const char *msg, int flags, struct strbuf *err)
@@ -3775,7 +3775,7 @@ int transaction_commit(struct ref_transaction *transaction,/* * Update all reflog files -* We have already done all ref updates and deletes. +* We have already committed all ref updates and deletes. * There is not much we can do here if there are any reflog * update errors other than complain. */
refs.h +1 -1
@@ -336,7 +336,7 @@ int transaction_update_reflog(struct ref_transaction *transaction,const char *refname, const unsigned char *new_sha1, const unsigned char *old_sha1, - const unsigned char *email, + const char *email, unsigned long timestamp, int tz, const char *msg, int flags, struct strbuf *err);
Signature · 1 line
2.0.1.508.g763ab16
PATCH 12/15 refs.c: rename log_ref_setup to create_reflog
In reply to Ronnie Sahlberglog_ref_setup is used to do several semi-related things :
* sometimes it will create a new reflog including missing parent directories and cleaning up any conflicting stale directories in the path.
* fill in a filename buffer for the full path to the reflog.
* unconditionally re-adjust the permissions for the file.
This function is only called from two places: checkout.c where it is always used to create a reflog and refs.c:log_ref_write where it sometimes are used to create a reflog and sometimes just used to fill in the filename.
Rename log_ref_setup to create_reflog and change it to only take the refname as argument to make its signature similar to delete_reflog and reflog_exists. Change create_reflog to ignore log_all_ref_updates and "unconditionally" create the reflog when called. Since checkout.c always wants to create a reflog we can call create_reflog directly and avoid the temp-and-log_all_ref_update dance.
In log_ref_write, only call create_reflog iff we want to create a reflog and if the reflog does not yet exist. This means that for the common case where the log already exists we now only need to perform a single lstat() instead of a open(O_CREAT)+lstat()+close().
--- builtin/checkout.c | 8 +------- refs.c | 22 +++++++++++++--------- refs.h | 8 +++----- 3 files changed, 17 insertions(+), 21 deletions(-)
builtin/checkout.c +1 -7
@@ -583,19 +583,13 @@ static void update_refs_for_switch(const struct checkout_opts *opts,if (opts->new_branch) {if (opts->new_orphan_branch) {if (opts->new_branch_log && !log_all_ref_updates) { -int temp; -char log_file[PATH_MAX ];char *ref_name =mkpath ("refs/heads/%s" , opts->new_orphan_branch); - temp = log_all_ref_updates; - log_all_ref_updates =1 ; -if (log_ref_setup (ref_name, log_file,sizeof (log_file))) { +if (create_reflog (ref_name)) {fprintf (stderr,_ ("Can not do reflog for '%s'\n" ), opts->new_orphan_branch); - log_all_ref_updates = temp;return ; } - log_all_ref_updates = temp; } }else
refs.c +13 -9
@@ -2819,16 +2819,16 @@ static int copy_msg(char *buf, const char *msg) }/* This function must set a meaningful errno on failure */ -int log_ref_setup (const char *refname,char *logfile,int bufsize) +int create_reflog (const char *refname) {int logfd, oflags =O_APPEND |O_WRONLY ; +char logfile[PATH_MAX ]; -git_snpath (logfile, bufsize,"logs/%s" , refname); -if (log_all_ref_updates && - (starts_with (refname,"refs/heads/" ) || -starts_with (refname,"refs/remotes/" ) || -starts_with (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ))) { +git_snpath (logfile,sizeof (logfile),"logs/%s" , refname); +if (starts_with (refname,"refs/heads/" ) || +starts_with (refname,"refs/remotes/" ) || +starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" )) {if (safe_create_leading_directories (logfile) <0 ) {int save_errno = errno;error ("unable to create directory for %s" , logfile);
@@ -2897,16 +2897,20 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,static int log_ref_write (const char *refname,const unsigned char *old_sha1,const unsigned char *new_sha1,const char *msg) { -int logfd, result, oflags =O_APPEND |O_WRONLY ; +int logfd, result =0 , oflags =O_APPEND |O_WRONLY ;char log_file[PATH_MAX ];if (log_all_ref_updates <0 ) log_all_ref_updates = !is_bare_repository (); - result =log_ref_setup (refname, log_file,sizeof (log_file)); +if (log_all_ref_updates && !reflog_exists (refname)) + result =create_reflog (refname); +if (result)return result; +git_snpath (log_file,sizeof (log_file),"logs/%s" , refname); + logfd =open (log_file, oflags);if (logfd <0 )return 0 ;
refs.h +3 -5
@@ -196,11 +196,6 @@ extern int commit_ref(struct ref_lock *lock);/** Release any lock taken but not written. **/ extern void unlock_ref (struct ref_lock *lock); -/* -* Setup reflog before using. Set errno to something meaningful on failure. -*/ -int log_ref_setup (const char *refname,char *logfile,int bufsize); -/** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt, unsigned char *sha1, char **msg,
@@ -209,6 +204,9 @@ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,/** Check if a particular reflog exists */ extern int reflog_exists (const char *refname); +/** Create reflog. Set errno to something meaningful on failure. */ +extern int create_reflog (const char *refname); +/** Delete a reflog */ extern int delete_reflog (const char *refname);
Signature · 1 line
2.0.1.508.g763ab16
PATCH 13/15 refs.c: make unlock_ref/close_ref/commit_ref static
In reply to Ronnie Sahlbergunlock|close|commit_ref can be made static since there are no more external callers.
--- refs.c | 24 ++++++++++++------------ refs.h | 9 --------- 2 files changed, 12 insertions(+), 21 deletions(-)
refs.c +12 -12
@@ -1969,6 +1969,16 @@ int refname_match(const char *abbrev_name, const char *full_name)return 0 ; } +static void unlock_ref (struct ref_lock *lock) +{ +/* Do not free lock->lk -- atexit() still looks at them */ +if (lock->lk) +rollback_lock_file (lock->lk); +free (lock->ref_name); +free (lock->orig_ref_name); +free (lock); +} +/* This function should make sure errno is meaningful on error */ static struct ref_lock *verify_lock (struct ref_lock *lock,const unsigned char *old_sha1,int mustexist)
@@ -2766,7 +2776,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsreturn 1 ; } -int close_ref (struct ref_lock *lock) +static int close_ref (struct ref_lock *lock) {if (close_lock_file (lock->lk))return -1 ;
@@ -2774,7 +2784,7 @@ int close_ref(struct ref_lock *lock)return 0 ; } -int commit_ref (struct ref_lock *lock) +static int commit_ref (struct ref_lock *lock) {if (commit_lock_file (lock->lk))return -1 ;
@@ -2782,16 +2792,6 @@ int commit_ref(struct ref_lock *lock)return 0 ; } -void unlock_ref (struct ref_lock *lock) -{ -/* Do not free lock->lk -- atexit() still looks at them */ -if (lock->lk) -rollback_lock_file (lock->lk); -free (lock->ref_name); -free (lock->orig_ref_name); -free (lock); -} -/* * copy the reflog message msg to buf, which has been allocated sufficiently * large, while cleaning up the whitespaces. Especially, convert LF to space,
refs.h +0 -9
@@ -187,15 +187,6 @@ extern struct ref_lock *lock_any_ref_for_update(const char *refname,const unsigned char *old_sha1, int flags, int *type_p); -/** 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 */ -extern int commit_ref (struct ref_lock *lock); - -/** Release any lock taken but not written. **/ -extern void unlock_ref (struct ref_lock *lock); -/** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt, unsigned char *sha1, char **msg,
Signature · 1 line
2.0.1.508.g763ab16
PATCH 14/15 refs.c: remove lock_any_ref_for_update
In reply to Ronnie SahlbergNo one is using this function so we can delete it.
--- refs.c | 7 ------- refs.h | 10 +--------- 2 files changed, 1 insertion(+), 16 deletions(-)
refs.c +0 -7
@@ -2222,13 +2222,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,return NULL ; } -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,NULL ,0 ); -} -/* * Write an entry to the packed-refs file for the specified refname. * If peeled is non-NULL, write it as the entry's peeled value.
refs.h +1 -9
@@ -171,21 +171,13 @@ extern int ref_exists(const char *);extern int peel_ref (const char *refname,unsigned char *sha1);/* -* Flags controlling lock_any_ref_for_update(), transaction_update_sha1(), -* transaction_create_sha1(), etc. +* Flags controlling transaction_update_sha1(), transaction_create_sha1(), 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) and sets errno to something -* meaningful on failure. -*/ -extern struct ref_lock *lock_any_ref_for_update (const char *refname, -const unsigned char *old_sha1, -int flags,int *type_p);/** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,
Signature · 1 line
2.0.1.508.g763ab16
PATCH 15/15 refs.c: allow deleting refs with a broken sha1
In reply to Ronnie SahlbergAdd (back?) support to make it possible to delete refs that are broken.
Add a new flag REF_ALLOWBROKEN that can be passed to the functions to lock a ref. If this flag is set we allow locking the ref even if the ref points to a broken sha1. For example a sha1 that is created by :
echo "Broken ref" > .git/refs/heads/foo-broken-1
Use this flag when calling from branch.c dusing a ref delete so that we only allow locking those broken refs IFF when called during a branch delete.
--- builtin/branch.c | 6 ++++-- refs.c | 10 +++++++--- refs.h | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-)
builtin/branch.c +4 -2
@@ -236,6 +236,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds, name =mkpathdup (fmt, bname.buf); target =resolve_ref_unsafe (name, sha1,RESOLVE_REF_ALLOW_BAD_NAME , &flags); +if (!target && (flags &REF_ISBROKEN )) + target = name;if (!target || (!(flags &REF_ISSYMREF ) &&is_null_sha1 (sha1))) { error(remote_branch
@@ -245,14 +247,14 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,continue ; } -if (!(flags &REF_ISSYMREF ) && +if (!(flags & (REF_ISSYMREF |REF_ISBROKEN )) &&check_branch_commit (bname.buf, name, sha1, head_rev, kinds, force)) { ret =1 ;continue ; } -if (delete_ref (name, sha1,REF_NODEREF )) { +if (delete_ref (name, sha1,REF_NODEREF |REF_ALLOWBROKEN )) { error(remote_branch ? _("Error deleting remote branch '%s'") : _("Error deleting branch '%s'"),
refs.c +7 -3
@@ -2122,12 +2122,12 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,int resolve_flags;int missing =0 ;int attempts_remaining =3 ; -int bad_refname; +int bad_ref; lock =xcalloc (1 ,sizeof (struct ref_lock )); lock->lock_fd =-1 ; - bad_refname =check_refname_format (refname,REFNAME_ALLOW_ONELEVEL ); + bad_ref =check_refname_format (refname,REFNAME_ALLOW_ONELEVEL ); resolve_flags =RESOLVE_REF_ALLOW_BAD_NAME ;if (mustexist)
@@ -2150,6 +2150,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname, refname =resolve_ref_unsafe (orig_refname, lock->old_sha1, resolve_flags, &type); } +if (!refname && (flags &REF_ALLOWBROKEN ) && (type &REF_ISBROKEN )) { + bad_ref =1 ; + refname = orig_refname; + }if (type_p) *type_p = type;if (!refname) {
@@ -2212,7 +2216,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,else unable_to_lock_index_die (ref_file, errno); } -if (bad_refname) +if (bad_ref)return lock;return old_sha1 ?verify_lock (lock, old_sha1, mustexist) : lock;
refs.h +2 -1
@@ -174,10 +174,11 @@ extern int peel_ref(const char *refname, unsigned char *sha1);* Flags controlling transaction_update_sha1(), transaction_create_sha1(), etc. * REF_NODEREF: act on the ref directly, instead of dereferencing * symbolic references. -* +* REF_ALLOWBROKEN: allow locking refs that are broken. * Flags >= 0x100 are reserved for internal use. */ #define REF_NODEREF 0x01 +#define REF_ALLOWBROKEN 0x02/** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,
Signature · 1 line
2.0.1.508.g763ab16
Re: [PATCH 01/15] refs.c make ref_transaction_create a wrapper to ref_transaction_update
In reply to Ronnie SahlbergQuoted 29 lines · Ronnie Sahlberg
Signed-off-by Ronnie Sahlberg <[email protected]>--- refs.c | 18 ++---------------- refs.h | 7 ++++--- 2 files changed, 6 insertions(+), 19 deletions(-)refs.c +0
-9@@ -3490,28 +3490,14 @@ int ref_transaction_create(struct ref_transaction *transaction, int flags, const char *msg, struct strbuf *err) { - 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" ); -if (check_refname_format (refname,REFNAME_ALLOW_ONELEVEL )) { -strbuf_addf (err,"Bad refname: %s" , refname); -return -1 ; - } - - update =add_update (transaction, refname); -
refs.c int ref_transaction_create(struct ref_transaction *transaction, · line 3501
Quoted 10 lines
refs.c +2
-7-hashcpy (update->new_sha1, new_sha1); -hashclr (update->old_sha1); - update->flags = flags; - update->have_old =1 ; -if (msg) - update->msg =xstrdup (msg); -return 0 ; +return ref_transaction_update (transaction, refname, new_sha1, + null_sha1, flags,1 , msg, err); }
Makes sense, but at the same time makes me wonder why no code is moved to ref_transaction_update() while removing code from here, which would only mean that code in ref_transaction_update() was added redundantly in the first place.
An ideal series would have had only "update" code in _update() when the function is added, and later with a patch like this would lose code from _create() while adding some code to _update(), I would think.
Or if all the code in _update() was necessary from day one, then perhaps this change should have been part of the same patch.
It's not a big deal either way, though.
Thanks.
Quoted 19 lines
refs.c +0
-0int ref_transaction_delete(struct ref_transaction *transaction,refs.h +4
-3@@ -276,9 +276,10 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err);/* * Add a reference update to transaction. new_sha1 is the value that * the reference should have after the update, or zeros if it should -* 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. +* be deleted. If have_old is true and old_sha is not the null_sha1 +* then the previous value of the ref must match or the update will fail. +* If have_old is true and old_sha1 is the null_sha1 then the ref must not +* already exist and a new ref will be created with new_sha1. * 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.
Re: [PATCH 15/15] refs.c: allow deleting refs with a broken sha1
In reply to Ronnie SahlbergQuoted 9 lines · Ronnie Sahlberg
Add (back?) support to make it possible to delete refs that are broken.
Add a new flag
REF_ALLOWBROKENthat can be passed to the functions to lock a ref. If this flag is set we allow locking the ref even if the ref points to a broken sha1. For example a sha1 that is created by :echo "Broken ref" > .git/refs/heads/foo-broken-1
Use this flag when calling from
branch.cdusing a ref delete so that we
Presumably: s/dusing/doing/
Quoted 103 lines
only allow locking those broken refs IFF when called during a branch delete.
Signed-off-by Ronnie Sahlberg <[email protected]>--- builtin/branch.c | 6 ++++-- refs.c | 10 +++++++--- refs.h | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-)builtin/branch.c +4
-2@@ -236,6 +236,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds, name =mkpathdup (fmt, bname.buf); target =resolve_ref_unsafe (name, sha1,RESOLVE_REF_ALLOW_BAD_NAME , &flags); +if (!target && (flags &REF_ISBROKEN )) + target = name;if (!target || (!(flags &REF_ISSYMREF ) &&is_null_sha1 (sha1))) { error(remote_branch@@ -245,14 +247,14 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,continue ; } -if (!(flags &REF_ISSYMREF ) && +if (!(flags & (REF_ISSYMREF |REF_ISBROKEN )) &&check_branch_commit (bname.buf, name, sha1, head_rev, kinds, force)) { ret =1 ;continue ; } -if (delete_ref (name, sha1,REF_NODEREF )) { +if (delete_ref (name, sha1,REF_NODEREF |REF_ALLOWBROKEN )) { error(remote_branch ? _("Error deleting remote branch '%s'") : _("Error deleting branch '%s'"),refs.c +7
-3@@ -2122,12 +2122,12 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,int resolve_flags;int missing =0 ;int attempts_remaining =3 ; -int bad_refname; +int bad_ref; lock =xcalloc (1 ,sizeof (struct ref_lock )); lock->lock_fd =-1 ; - bad_refname =check_refname_format (refname,REFNAME_ALLOW_ONELEVEL ); + bad_ref =check_refname_format (refname,REFNAME_ALLOW_ONELEVEL ); resolve_flags =RESOLVE_REF_ALLOW_BAD_NAME ;if (mustexist)@@ -2150,6 +2150,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname, refname =resolve_ref_unsafe (orig_refname, lock->old_sha1, resolve_flags, &type); } +if (!refname && (flags &REF_ALLOWBROKEN ) && (type &REF_ISBROKEN )) { + bad_ref =1 ; + refname = orig_refname; + }if (type_p) *type_p = type;if (!refname) {@@ -2212,7 +2216,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,else unable_to_lock_index_die (ref_file, errno); } -if (bad_refname) +if (bad_ref)return lock;return old_sha1 ?verify_lock (lock, old_sha1, mustexist) : lock;refs.h +2
-2@@ -174,10 +174,11 @@ extern int peel_ref(const char *refname, unsigned char *sha1);* Flags controlling transaction_update_sha1(), transaction_create_sha1(), etc. * REF_NODEREF: act on the ref directly, instead of dereferencing * symbolic references. -* +* REF_ALLOWBROKEN: allow locking refs that are broken. * Flags >= 0x100 are reserved for internal use. */ #define REF_NODEREF 0x01 +#define REF_ALLOWBROKEN 0x02/** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt, --2.0.1.508.g763ab16
--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 7 lines · Ronnie Sahlberg
This is the next patch series for ref-transactions. It is also available at
https://github.com/rsahlberg/git/tree/ref-transactions-reflog
and is the same patch series that has been posted previously with one exception:
I've received, queued and started reading it, but it has fairly heavy interactions with other topics in flight when merged to 'pu', so today's integration result will not have this topic anywhere.
https://github.com/gitster/git/ is a mirror of my primary working space and rs/ref-transaction-reflog topic can be seen there, though.
Thanks.