refs.c: add err arguments to reflog functions
PATCH v3 1/7 refs.c: add err arguments to reflog functions
Add an err argument to log_ref_setup that can explain the reason for a failure. This then eliminates the need to manage errno through this function since we can just add strerror(errno) to the err string when meaningful. No callers relied on errno from this function for anything else than the error message.
Also add err arguments to private functions write_ref_to_lockfile, log_ref_write_1, commit_ref_update. This again eliminates the need to manage errno in these functions.
Update of a patch by Ronnie Sahlberg.
--- builtin/checkout.c | 8 ++-- refs.c | 111 ++++++++++++++++++++++++++++------------------------- refs.h | 4 +- 3 files changed, 66 insertions(+), 57 deletions(-)
builtin/checkout.c +5 -3
@@ -624,16 +624,18 @@ static void update_refs_for_switch(const struct checkout_opts *opts,struct strbuf log_file =STRBUF_INIT ;int ret;const char *ref_name; +struct strbuf err =STRBUF_INIT ; ref_name =mkpath ("refs/heads/%s" , opts->new_orphan_branch); temp = log_all_ref_updates; log_all_ref_updates =1 ; - ret =log_ref_setup (ref_name, &log_file); + ret =log_ref_setup (ref_name, &log_file, &err); log_all_ref_updates = temp;strbuf_release (&log_file);if (ret) { -fprintf (stderr,_ ("Can not do reflog for '%s'\n" ), - opts->new_orphan_branch); +fprintf (stderr,_ ("Can not do reflog for '%s'. %s\n" ), + opts->new_orphan_branch, err.buf); +strbuf_release (&err);return ; } }
refs.c +59 -52
256 lines not on this page · raw message
refs.h +2 -2
@@ -226,9 +226,9 @@ int pack_refs(unsigned int flags);#define REF_NODEREF 0x01/* -* Setup reflog before using. Set errno to something meaningful on failure. +* Setup reflog before using. Fill in err and return -1 on failure. */ -int log_ref_setup (const char *refname,struct strbuf *logfile); +int log_ref_setup (const char *refname,struct strbuf *logfile,struct strbuf *err);/** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *refname, unsigned int flags,
Signature · 1 line
2.0.4.314.gdbf7a51-twtrsrc
PATCH v3 2/7 cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs
Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with CHERRY_PICK_HEAD and REVERT_HEAD.
--- branch.c | 4 ++-- builtin/commit.c | 6 +++--- builtin/merge.c | 2 +- contrib/completion/git-prompt.sh | 4 ++-- git-gui/lib/commit.tcl | 2 +- sequencer.c | 37 +++++++++++++++++++------------------ t/t7509-commit.sh | 4 ++-- wt-status.c | 6 ++---- 8 files changed, 32 insertions(+), 33 deletions(-)
branch.c +2 -2
@@ -302,8 +302,8 @@ void create_branch(const char *head,void remove_branch_state (void ) { -unlink (git_path ("CHERRY_PICK_HEAD" )); -unlink (git_path ("REVERT_HEAD" )); +delete_ref ("CHERRY_PICK_HEAD" ,NULL ,REF_NODEREF ); +delete_ref ("REVERT_HEAD" ,NULL ,REF_NODEREF );unlink (git_path ("MERGE_HEAD" ));unlink (git_path ("MERGE_RR" ));unlink (git_path ("MERGE_MSG" ));
builtin/commit.c +3 -3
@@ -168,7 +168,7 @@ static void determine_whence(struct wt_status *s) {if (file_exists (git_path ("MERGE_HEAD" ))) whence =FROM_MERGE ; -else if (file_exists (git_path ("CHERRY_PICK_HEAD" ))) { +else if (ref_exists ("CHERRY_PICK_HEAD" )) { whence =FROM_CHERRY_PICK ;if (file_exists (git_path (SEQ_DIR ))) sequencer_in_use =1 ;
@@ -1777,8 +1777,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix) }ref_transaction_free (transaction); -unlink (git_path ("CHERRY_PICK_HEAD" )); -unlink (git_path ("REVERT_HEAD" )); +delete_ref ("CHERRY_PICK_HEAD" ,NULL ,REF_NODEREF ); +delete_ref ("REVERT_HEAD" ,NULL ,REF_NODEREF );unlink (git_path ("MERGE_HEAD" ));unlink (git_path ("MERGE_MSG" ));unlink (git_path ("MERGE_MODE" ));
builtin/merge.c +1 -1
@@ -1206,7 +1206,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)else die (_ ("You have not concluded your merge (MERGE_HEAD exists)." )); } -if (file_exists (git_path ("CHERRY_PICK_HEAD" ))) { +if (ref_exists ("CHERRY_PICK_HEAD" )) {if (advice_resolve_conflict)die (_ ("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" "Please, commit your changes before you merge." ));
contrib/completion/git-prompt.sh +2 -2
@@ -415,9 +415,9 @@ __git_ps1 () fi elif [ -f "$g/MERGE_HEAD" ]; then r="|MERGING" - elif [ -f "$g/CHERRY_PICK_HEAD" ]; then + elif git rev-parse --quiet --verify "CHERRY_PICK_HEAD" >/dev/null; then r="|CHERRY-PICKING" - elif [ -f "$g/REVERT_HEAD" ]; then + elif git rev-parse --quiet --verify "REVERT_HEAD" >/dev/null; then r="|REVERTING" elif [ -f "$g/BISECT_LOG" ]; then r="|BISECTING"
git-gui/lib/commit.tcl +1 -1
@@ -409,7 +409,7 @@ A rescan will be automatically started now. catch {file delete [gitdir MERGE_MSG]} catch {file delete [gitdir SQUASH_MSG]} catch {file delete [gitdir GITGUI_MSG]} - catch {file delete [gitdir CHERRY_PICK_HEAD]} + catch {git update-ref -d --no-deref CHERRY_PICK_HEAD} # -- Let rerere do its thing. #
sequencer.c +19 -18
@@ -160,19 +160,20 @@ static void free_message(struct commit *commit, struct commit_message *msg)static void write_cherry_pick_head (struct commit *commit,const char *pseudoref) { -const char *filename; -int fd; -struct strbuf buf =STRBUF_INIT ; +struct strbuf err =STRBUF_INIT ; +void *transaction; -strbuf_addf (&buf,"%s\n" ,sha1_to_hex (commit->object.sha1)); + transaction =ref_transaction_begin (&err); +if (!transaction) +die (_ ("Could not create transaction: %s" ), err.buf); - filename =git_path ("%s" , pseudoref); - fd =open (filename,O_WRONLY |O_CREAT ,0666 ); -if (fd <0 ) -die_errno (_ ("Could not open '%s' for writing" ), filename); -if (write_in_full (fd, buf.buf, buf.len) != buf.len ||close (fd)) -die_errno (_ ("Could not write to '%s'" ), filename); -strbuf_release (&buf); +if (ref_transaction_update (transaction, pseudoref, commit->object.sha1, +NULL ,REF_NODEREF ,NULL , + &err)) +die (_ ("Could not write ref %s: %s" ), pseudoref, err.buf); + +if (ref_transaction_commit (transaction, &err)) +die (_ ("Could not commit ref write %s: %s" ), pseudoref, err.buf); }static void print_advice (int show_hint,struct replay_opts *opts)
@@ -186,7 +187,7 @@ static void print_advice(int show_hint, struct replay_opts *opts)* (typically rebase --interactive) wants to take care * of the commit itself so remove CHERRY_PICK_HEAD */ -unlink (git_path ("CHERRY_PICK_HEAD" )); +delete_ref ("CHERRY_PICK_HEAD" ,NULL ,REF_NODEREF );return ; }
@@ -878,8 +879,8 @@ static int rollback_single_pick(void) {unsigned char head_sha1[20 ]; -if (!file_exists (git_path ("CHERRY_PICK_HEAD" )) && - !file_exists (git_path ("REVERT_HEAD" ))) +if (read_ref ("CHERRY_PICK_HEAD" , head_sha1) && +read_ref ("REVERT_HEAD" , head_sha1))return error (_ ("no cherry-pick or revert in progress" ));if (read_ref_full ("HEAD" ,0 , head_sha1,NULL ))return error (_ ("cannot resolve HEAD" ));
@@ -1014,8 +1015,8 @@ static int continue_single_pick(void) {const char *argv[] = {"commit" ,NULL }; -if (!file_exists (git_path ("CHERRY_PICK_HEAD" )) && - !file_exists (git_path ("REVERT_HEAD" ))) +if (!ref_exists ("CHERRY_PICK_HEAD" ) && + !ref_exists ("REVERT_HEAD" ))return error (_ ("no cherry-pick or revert in progress" ));return run_command_v_opt (argv,RUN_GIT_CMD ); }
@@ -1030,8 +1031,8 @@ static int sequencer_continue(struct replay_opts *opts)read_populate_todo (&todo_list, opts);/* Verify that the conflict has been resolved */ -if (file_exists (git_path ("CHERRY_PICK_HEAD" )) || -file_exists (git_path ("REVERT_HEAD" ))) { +if (ref_exists ("CHERRY_PICK_HEAD" ) || +ref_exists ("REVERT_HEAD" )) {int ret =continue_single_pick ();if (ret)return ret;
t/t7509-commit.sh +2 -2
@@ -163,7 +163,7 @@ test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' ' test_tick && git commit -am "cherry-pick 1" --author="Cherry <[email protected]>" && git tag cherry-pick-head && - git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD && + git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) && echo "This is a MERGE_MSG" >.git/MERGE_MSG && echo "cherry-pick 1b" >>foo && test_tick &&
@@ -178,7 +178,7 @@ test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' ' ' test_expect_success '--reset-author with CHERRY_PICK_HEAD' ' - git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD && + git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) && echo "cherry-pick 2" >>foo && test_tick && git commit -am "cherry-pick 2" --reset-author &&
wt-status.c +2 -4
@@ -1336,8 +1336,7 @@ void wt_status_get_state(struct wt_status_state *state, state->rebase_in_progress =1 ; state->branch =read_and_strip_branch ("rebase-merge/head-name" ); state->onto =read_and_strip_branch ("rebase-merge/onto" ); - }else if (!stat (git_path ("CHERRY_PICK_HEAD" ), &st) && - !get_sha1 ("CHERRY_PICK_HEAD" , sha1)) { + }else if (!read_ref ("CHERRY_PICK_HEAD" , sha1)) { state->cherry_pick_in_progress =1 ;hashcpy (state->cherry_pick_head_sha1, sha1); }
@@ -1345,8 +1344,7 @@ void wt_status_get_state(struct wt_status_state *state, state->bisect_in_progress =1 ; state->branch =read_and_strip_branch ("BISECT_START" ); } -if (!stat (git_path ("REVERT_HEAD" ), &st) && - !get_sha1 ("REVERT_HEAD" , sha1)) { +if (!read_ref ("REVERT_HEAD" , sha1)) { state->revert_in_progress =1 ;hashcpy (state->revert_head_sha1, sha1); }
Signature · 1 line
2.0.4.314.gdbf7a51-twtrsrc
Re: [PATCH v3 2/7] cherry-pick: treat CHERRY_PICK_HEAD and REVERT_HEAD as refs
sequencer.c static void free_message(struct commit *commit, struct commit_message *msg) · line 161
Quoted 11 lines · David Turner
Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with
CHERRY_PICK_HEADandREVERT_HEAD.
...sequencer.c +0
-0@@ -160,19 +160,20 @@ static void free_message(struct commit *commit, struct commit_message *msg)static void write_cherry_pick_head (struct commit *commit,const char *pseudoref)
I guess this is no longer "pseudoref"?
sequencer.c static int rollback_single_pick(void) · line 886
Quoted 11 lines
sequencer.c +2
-2@@ -878,8 +879,8 @@ static int rollback_single_pick(void) {unsigned char head_sha1[20 ]; -if (!file_exists (git_path ("CHERRY_PICK_HEAD" )) && - !file_exists (git_path ("REVERT_HEAD" ))) +if (read_ref ("CHERRY_PICK_HEAD" , head_sha1) && +read_ref ("REVERT_HEAD" , head_sha1))return error (_ ("no cherry-pick or revert in progress" ));if (read_ref_full ("HEAD" ,0 , head_sha1,NULL ))return error (_ ("cannot resolve HEAD" ));
I would have expected that you would use ref_exists() here; you are not reading "HEAD" yet writing it into head_sha1[], and not using the value of the ref.
Other than that, looked very sensible. Thanks.
PATCH v3 3/7 bisect: treat BISECT_HEAD as a ref
In reply to David TurnerInstead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with BISECT_HEAD.
--- git-bisect.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
git-bisect.sh +7 -3
@@ -35,7 +35,7 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" bisect_head() { - if test -f "$GIT_DIR/BISECT_HEAD" + if bisect_head_exists then echo BISECT_HEAD else
@@ -209,6 +209,10 @@ check_expected_revs() { done } +bisect_head_exists() { + git rev-parse --quiet --verify "BISECT_HEAD" >/dev/null +} + bisect_skip() { all='' for arg in "$@"
@@ -310,7 +314,7 @@ bisect_next() { bisect_next_check good # Perform all bisection computation, display and checkout - git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout) + git bisect--helper --next-all $(bisect_head_exists && echo --no-checkout) res=$? # Check if we should exit because bisection is finished
@@ -377,7 +381,7 @@ bisect_reset() { usage ;; esac - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- + if ! bisect_head_exists && ! git checkout "$branch" -- then die "$(eval_gettext "Could not check out original HEAD '\$branch'. Try 'git bisect reset <commit>'.")"
Signature · 1 line
2.0.4.314.gdbf7a51-twtrsrc
Re: [PATCH v3 3/7] bisect: treat BISECT_HEAD as a ref
Quoted 5 lines · David Turner
Instead of directly writing to and reading from files in $GIT_DIR, use ref API to interact with
BISECT_HEAD.Signed-off-by David Turner <[email protected]>---
Interesting. From the patch, it seems that the codepath that writes BISECT_HEAD were already doing update-ref instead of writing into or removing a file.
The result of applying this patch looked sane. Thanks.
Quoted 45 lines
git-bisect.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)git-bisect.sh +7
-3@@ -35,7 +35,7 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40" bisect_head() { - if test -f "$GIT_DIR/BISECT_HEAD" + if bisect_head_exists then echo BISECT_HEAD else@@ -209,6 +209,10 @@ check_expected_revs() { done } +bisect_head_exists() { + git rev-parse --quiet --verify "BISECT_HEAD" >/dev/null +} + bisect_skip() { all='' for arg in "$@"@@ -310,7 +314,7 @@ bisect_next() { bisect_next_check good # Perform all bisection computation, display and checkout - git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout) + git bisect--helper --next-all $(bisect_head_exists && echo --no-checkout) res=$? # Check if we should exit because bisection is finished@@ -377,7 +381,7 @@ bisect_reset() { usage ;; esac - if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" -- + if ! bisect_head_exists && ! git checkout "$branch" -- then die "$(eval_gettext "Could not check out original HEAD '\$branch'. Try 'git bisect reset <commit>'.")"
PATCH v3 4/7 refs: Break out check for reflog autocreation
In reply to David TurnerThis is just for clarity.
--- refs.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
refs.c +9 -5
@@ -3118,6 +3118,14 @@ static int copy_msg(char *buf, const char *msg)return cp - buf; } +static int should_autocreate_reflog (const char *refname) +{ +return starts_with (refname,"refs/heads/" ) || +starts_with (refname,"refs/remotes/" ) || +starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" ); +} +/* This function will fill in *err and return -1 on failure */ int log_ref_setup (const char *refname,struct strbuf *sb_logfile,struct strbuf *err) {
@@ -3128,11 +3136,7 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf logfile = sb_logfile->buf;/* make sure the rest of the function can't change "logfile" */ sb_logfile =NULL ; -if (log_all_ref_updates && - (starts_with (refname,"refs/heads/" ) || -starts_with (refname,"refs/remotes/" ) || -starts_with (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ))) { +if (log_all_ref_updates &&should_autocreate_reflog (refname)) {if (safe_create_leading_directories (logfile) <0 ) {strbuf_addf (err,"unable to create directory for %s. " "%s" , logfile,strerror (errno));
Signature · 1 line
2.0.4.314.gdbf7a51-twtrsrc
Re: [PATCH v3 4/7] refs: Break out check for reflog autocreation
Quoted 27 lines · David Turner
This is just for clarity.
Signed-off-by David Turner <[email protected]>--- refs.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)refs.c +8
-0@@ -3118,6 +3118,14 @@ static int copy_msg(char *buf, const char *msg)return cp - buf; } +static int should_autocreate_reflog (const char *refname) +{ +return starts_with (refname,"refs/heads/" ) || +starts_with (refname,"refs/remotes/" ) || +starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" ); +} +/* This function will fill in *err and return -1 on failure */ int log_ref_setup (const char *refname,struct strbuf *sb_logfile,struct strbuf *err) {
refs.c int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf · line 3139
Quoted 9 lines
refs.c +1
-5@@ -3128,11 +3136,7 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf logfile = sb_logfile->buf;/* make sure the rest of the function can't change "logfile" */ sb_logfile =NULL ; -if (log_all_ref_updates && - (starts_with (refname,"refs/heads/" ) || -starts_with (refname,"refs/remotes/" ) || -starts_with (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ))) { +if (log_all_ref_updates &&should_autocreate_reflog (refname)) {
This makes me wonder if "log_all_ref_updates &&" part should also be inside the helper. The use of the new helper in 5/7 tells me that it is the case, I would say.
Besides, the answer to the question "should we auto-create reflog?" is "if we are told to log all, and then the thing is one of these, then we should:", so it is natural from the name of the new helper that "log_all_ref_updates &&" should be in there ;-)
PATCH v3 5/7 refs: add safe_create_reflog function
In reply to David TurnerMake log_ref_setup private, and add public safe_create_reflog which calls log_ref_setup.
In a moment, we will use safe_create_reflog to add reflog creation commands to git-reflog.
--- builtin/checkout.c | 4 +--- refs.c | 19 ++++++++++++++++--- refs.h | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-)
builtin/checkout.c +1 -3
@@ -621,7 +621,6 @@ static void update_refs_for_switch(const struct checkout_opts *opts,if (opts->new_orphan_branch) {if (opts->new_branch_log && !log_all_ref_updates) {int temp; -struct strbuf log_file =STRBUF_INIT ;int ret;const char *ref_name;struct strbuf err =STRBUF_INIT ;
@@ -629,9 +628,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, ref_name =mkpath ("refs/heads/%s" , opts->new_orphan_branch); temp = log_all_ref_updates; log_all_ref_updates =1 ; - ret =log_ref_setup (ref_name, &log_file, &err); + ret =safe_create_reflog (ref_name, &err,0 ); log_all_ref_updates = temp; -strbuf_release (&log_file);if (ret) {fprintf (stderr,_ ("Can not do reflog for '%s'. %s\n" ), opts->new_orphan_branch, err.buf);
refs.c +16 -3
@@ -3123,11 +3123,12 @@ static int should_autocreate_reflog(const char *refname)return starts_with (refname,"refs/heads/" ) ||starts_with (refname,"refs/remotes/" ) ||starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"refs/stash" ) || !strcmp (refname,"HEAD" ); }/* This function will fill in *err and return -1 on failure */ -int log_ref_setup (const char *refname,struct strbuf *sb_logfile,struct strbuf *err) +static int log_ref_setup (const char *refname,struct strbuf *sb_logfile,struct strbuf *err,int force_create) {int logfd, oflags =O_APPEND |O_WRONLY ;char *logfile;
@@ -3136,7 +3137,8 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf logfile = sb_logfile->buf;/* make sure the rest of the function can't change "logfile" */ sb_logfile =NULL ; -if (log_all_ref_updates &&should_autocreate_reflog (refname)) { +if (log_all_ref_updates && + (force_create ||should_autocreate_reflog (refname))) {if (safe_create_leading_directories (logfile) <0 ) {strbuf_addf (err,"unable to create directory for %s. " "%s" , logfile,strerror (errno));
@@ -3171,6 +3173,17 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbufreturn 0 ; } + +int safe_create_reflog (const char *refname,struct strbuf *err,int force_create) +{ +int ret; +struct strbuf sb =STRBUF_INIT ; + + ret =log_ref_setup (refname, &sb, err, force_create); +strbuf_release (&sb); +return ret; +} +static int log_ref_write_fd (int fd,const unsigned char *old_sha1,const unsigned char *new_sha1,const char *committer,const char *msg)
@@ -3207,7 +3220,7 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,if (log_all_ref_updates <0 ) log_all_ref_updates = !is_bare_repository (); - result =log_ref_setup (refname, sb_log_file, err); + result =log_ref_setup (refname, sb_log_file, err,0 );if (result)return result;
refs.h +1 -1
@@ -228,7 +228,7 @@ int pack_refs(unsigned int flags);/* * Setup reflog before using. Fill in err and return -1 on failure. */ -int log_ref_setup (const char *refname,struct strbuf *logfile,struct strbuf *err); +int safe_create_reflog (const char *refname,struct strbuf *err,int force_create);/** Reads log for the value of ref during at_time. **/ extern int read_ref_at(const char *refname, unsigned int flags,
Signature · 1 line
2.0.4.314.gdbf7a51-twtrsrc
Re: [PATCH v3 5/7] refs: add safe_create_reflog function
Quoted 10 lines · David Turner
Make
log_ref_setupprivate, and add publicsafe_create_reflogwhich callslog_ref_setup.In a moment, we will use
safe_create_reflogto add reflog creation commands to git-reflog.Signed-off-by David Turner <[email protected]>---
builtin/checkout.c static void update_refs_for_switch(const struct checkout_opts *opts, · line 634
Quoted 7 lines
builtin/checkout.c +1
-2@@ -629,9 +628,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, ref_name =mkpath ("refs/heads/%s" , opts->new_orphan_branch); temp = log_all_ref_updates; log_all_ref_updates =1 ; - ret =log_ref_setup (ref_name, &log_file, &err); + ret =safe_create_reflog (ref_name, &err,0 ); log_all_ref_updates = temp; -strbuf_release (&log_file);
What the result of applying this patch does around here is incoherent, isn't it? I do understand and agree that adding the "force-create" argument to safe_create_reflog() is a good idea, but then I do not think you need to temporarily set log-all-ref-updates while calling it (and revert it afterwards). Not having to do that is the whole point of "force-create", isn't it?
refs.c static int should_autocreate_reflog(const char *refname) · line 3128
Quoted 11 lines
refs.c +1
-0@@ -3123,11 +3123,12 @@ static int should_autocreate_reflog(const char *refname)return starts_with (refname,"refs/heads/" ) ||starts_with (refname,"refs/remotes/" ) ||starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"refs/stash" ) || !strcmp (refname,"HEAD" ); }
This change is *not* part of creating safe-create-reflog.
It may be part of "allowing us to use safe_create_reflog() to manage stash", but I'd imagine that a new "reflog create" command should ignore log-all-ref-updates settings (after all, the end user is explicitly telling us to create one by issuing a command), so I doubt it matters if 'stash' is on the auto-create list.
refs.c int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf · line 3141
Quoted 13 lines
refs.c +3
-2/* This function will fill in *err and return -1 on failure */ -int log_ref_setup (const char *refname,struct strbuf *sb_logfile,struct strbuf *err) +static int log_ref_setup (const char *refname,struct strbuf *sb_logfile,struct strbuf *err,int force_create) {int logfd, oflags =O_APPEND |O_WRONLY ;char *logfile;@@ -3136,7 +3137,8 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf logfile = sb_logfile->buf;/* make sure the rest of the function can't change "logfile" */ sb_logfile =NULL ; -if (log_all_ref_updates &&should_autocreate_reflog (refname)) { +if (log_all_ref_updates && + (force_create ||should_autocreate_reflog (refname))) {
With this patch, force_create is used as "we are throwing you an unusual name that do not automatically get a reflog when log-all is set, and we are overriding that selection-by-name aspect of auto creation" but does not defeat log-all settings. Is that intended?
If somebody asks safe_create_reflog() to create a reflog and passes force_create, shouldn't we create that reflog regardless of log_all_ref_updates settings? That is
if (force_create || should_autocreate_reflog(refname)) {
... create it ...would be more natural to read here, and you would get that for free if 4/7 checked log_all_ref_updates in should_autocreate_reflog().
PATCH v3 6/7 git-reflog: add create and exists functions
In reply to David TurnerThese are necessary because alternate ref backends might store reflogs somewhere other than .git/logs. Code that now directly manipulates .git/logs should instead go through git-reflog.
In a moment, we will use these functions to make git stash work with alternate ref backends.
--- builtin/reflog.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++- t/t1411-reflog-show.sh | 12 ++++++++ 2 files changed, 90 insertions(+), 1 deletion(-)
builtin/reflog.c +78 -1
@@ -13,6 +13,10 @@ static const char reflog_expire_usage[] ="git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>..." ;static const char reflog_delete_usage[] ="git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>..." ; +static const char reflog_create_usage[] = +"git reflog create <refs>..." ; +static const char reflog_exists_usage[] = +"git reflog exists <ref>" ;static unsigned long default_reflog_expire;static unsigned long default_reflog_expire_unreachable;
@@ -699,12 +703,79 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)return status; } +static int cmd_reflog_create (int argc,const char **argv,const char *prefix) +{ +int i, status =0 , start =0 ; +struct strbuf err =STRBUF_INIT ; +int temp; + +for (i =1 ; i < argc; i++) { +const char *arg = argv[i]; +if (!strcmp (arg,"--" )) { + i++; +break ; + } +else if (arg[0 ] =='-' ) +usage (reflog_create_usage); +else +break ; + } + + start = i; + +if (argc - start <1 ) +return error ("Nothing to create?" ); + + temp = log_all_ref_updates; + log_all_ref_updates =1 ; +for (i = start; i < argc; i++) { +if (check_refname_format (argv[i],REFNAME_ALLOW_ONELEVEL )) +die ("invalid ref format: %s" , argv[i]); + } +for (i = start; i < argc; i++) { +if (safe_create_reflog (argv[i], &err,1 )) { +error ("could not create reflog %s: %s" , argv[i], + err.buf); + status =1 ; +strbuf_release (&err); + } + } + log_all_ref_updates = temp; +return status; +} + +static int cmd_reflog_exists (int argc,const char **argv,const char *prefix) +{ +int i, status =0 , start =0 ; + +for (i =1 ; i < argc; i++) { +const char *arg = argv[i]; +if (!strcmp (arg,"--" )) { + i++; +break ; + } +else if (arg[0 ] =='-' ) +usage (reflog_exists_usage); +else +break ; + } + + start = i; + +if (argc - start !=1 ) +usage (reflog_exists_usage); + +if (check_refname_format (argv[start],REFNAME_ALLOW_ONELEVEL )) +die ("invalid ref format: %s" , argv[start]); +return !reflog_exists (argv[start]); +} +/* * main "reflog" */ static const char reflog_usage[] = -"git reflog [ show | expire | delete ]" ; +"git reflog [ show | expire | delete | create | exists ]" ;int cmd_reflog (int argc,const char **argv,const char *prefix) {
@@ -724,5 +795,11 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)if (!strcmp (argv[1 ],"delete" ))return cmd_reflog_delete (argc -1 , argv +1 , prefix); +if (!strcmp (argv[1 ],"create" )) +return cmd_reflog_create (argc -1 , argv +1 , prefix); + +if (!strcmp (argv[1 ],"exists" )) +return cmd_reflog_exists (argc -1 , argv +1 , prefix); +return cmd_log_reflog (argc, argv, prefix); }
t/t1411-reflog-show.sh +12 -0
@@ -166,4 +166,16 @@ test_expect_success 'git log -g -p shows diffs vs. parents' ' test_cmp expect actual ' +test_expect_success 'reflog exists works' ' + git reflog exists refs/heads/master && + ! git reflog exists refs/heads/nonexistent +' + +test_expect_success 'reflog create works' ' + git update-ref non-refs-dir HEAD && + ! git reflog exists non-refs-dir && + git reflog create non-refs-dir && + git reflog exists non-refs-dir +' + test_done
Signature · 1 line
2.0.4.314.gdbf7a51-twtrsrc
PATCH v3 7/7 git-stash: use git-reflog instead of creating files
In reply to David TurnerThis is in support of alternate ref backends which don't necessarily store reflogs as files.
--- git-stash.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
git-stash.sh +2 -2
@@ -184,7 +184,7 @@ store_stash () { fi # Make sure the reflog for stash is kept. - : >>"$(git rev-parse --git-path logs/$ref_stash)" + git reflog create "$ref_stash" git update-ref -m "$stash_msg" $ref_stash $w_commit ret=$? test $ret != 0 && test -z $quiet &&
@@ -262,7 +262,7 @@ save_stash () { say "$(gettext "No local changes to save")" exit 0 fi - test -f "$(git rev-parse --git-path logs/$ref_stash)" || + git reflog exists $ref_stash || clear_stash || die "$(gettext "Cannot initialize stash")" create_stash "$stash_msg" $untracked
Signature · 1 line
2.0.4.314.gdbf7a51-twtrsrc
Re: [PATCH v3 7/7] git-stash: use git-reflog instead of creating files
Quoted 5 lines · David Turner
This is in support of alternate ref backends which don't necessarily store reflogs as files.
Signed-off-by David Turner <[email protected]>---
6/7 and 7/7 looked sensible; "git reflog create/exists" needs a doc update, though.
Thanks.