tag: add tag.createReflog option
PATCH 1/1 tag: add tag.createReflog option
From: Cornelius Weig <[email protected]>
Git does not create a history for tags, in contrast to common expectation to simply version everything. This can be changed by using the --create-reflog flag when creating the tag. However, a config option to enable this behavior by default is missing.
This commit adds the configuration variable tag.createReflog which enables reflogs for new tags by default.
--- Documentation/config.txt | 5 +++++ Documentation/git-tag.txt | 8 +++++--- builtin/tag.c | 6 +++++- t/t7004-tag.sh | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-)
Documentation/config.txt +5 -0
@@ -2945,6 +2945,11 @@ submodule.alternateErrorStrategy as computed via `submodule.alternateLocation`. Possible values are `ignore`, `info`, `die`. Default is `die`. +tag.createReflog:: + A boolean to specify whether newly created tags should have a reflog. + If `--[no-]create-reflog` is specified on the command line, it takes + precedence. Defaults to `false`. + tag.forceSignAnnotated:: A boolean to specify whether annotated tags created should be GPG signed. If `--annotate` is specified on the command line, it takes
Documentation/git-tag.txt +5 -3
@@ -13,7 +13,7 @@ SYNOPSIS <tagname> [<commit> | <object>] 'git tag' -d <tagname>... 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>] - [--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] + [--column[=<options>] | --no-column] [--[no-]create-reflog] [--sort=<key>] [--format=<format>] [--[no-]merged [<commit>]] [<pattern>...] 'git tag' -v <tagname>...
@@ -149,8 +149,10 @@ This option is only applicable when listing tags without annotation lines. all, 'whitespace' removes just leading/trailing whitespace lines and 'strip' removes both whitespace and commentary. ---create-reflog:: - Create a reflog for the tag. +--[no-]create-reflog:: + Force to create a reflog for the tag, or no reflog if `--no-create-reflog` + is used. Unless the `tag.createReflog` config variable is set to true, no + reflog is created by default. See linkgit:git-config[1]. <tagname>:: The name of the tag to create, delete, or describe.
builtin/tag.c +5 -1
@@ -30,6 +30,7 @@ static const char * const git_tag_usage[] = {static unsigned int colopts;static int force_sign_annotate; +static int create_reflog;static int list_tags (struct ref_filter *filter,struct ref_sorting *sorting,const char *format) {
@@ -165,6 +166,10 @@ static int git_tag_config(const char *var, const char *value, void *cb) force_sign_annotate =git_config_bool (var, value);return 0 ; } +if (!strcmp (var,"tag.createreflog" )) { + create_reflog =git_config_bool (var, value); +return 0 ; + }if (starts_with (var,"column." ))return git_column_config (var, value,"tag" , &colopts);
@@ -325,7 +330,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)const char *object_ref, *tag;struct create_tag_options opt;char *cleanup_arg =NULL ; -int create_reflog =0 ;int annotate =0 , force =0 ;int cmdmode =0 , create_tag_object =0 ;const char *msgfile =NULL , *keyid =NULL ;
t/t7004-tag.sh +14 -0
@@ -90,6 +90,20 @@ test_expect_success '--create-reflog does not create reflog on failure' ' test_must_fail git reflog exists refs/tags/mytag ' +test_expect_success 'option tag.createreflog creates reflog by default' ' + test_when_finished "git tag -d tag_with_reflog" && + git config tag.createReflog true && + git tag tag_with_reflog && + git reflog exists refs/tags/tag_with_reflog +' + +test_expect_success 'option tag.createreflog overridden by command line' ' + test_when_finished "git tag -d tag_without_reflog" && + git config tag.createReflog true && + git tag --no-create-reflog tag_without_reflog && + test_must_fail git reflog exists refs/tags/tag_without_reflog +' + test_expect_success 'listing all tags if one exists should succeed' ' git tag -l && git tag
Signature · 1 line
2.10.2
Hey Cornelius,
Quoted 11 lines · On Wed, Jan 25, 2017 at 5:49 AM, <[email protected]> wrote:
From: Cornelius Weig <[email protected]>
Git does not create a history for tags, in contrast to common expectation to simply version everything. This can be changed by using the
--create-reflogflag when creating the tag. However, a config option to enable this behavior by default is missing.This commit adds the configuration variable
tag.createReflogwhich enables reflogs for new tags by default.Signed-off-by Cornelius Weig <[email protected]>
You have also added the option --no-create-reflog so would it be worth to mention it in the commit message?
Quoted 12 lines
--- Documentation/config.txt | 5 +++++ Documentation/git-tag.txt | 8 +++++--- builtin/tag.c | 6 +++++- t/t7004-tag.sh | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-)Documentation/config.txt +0
-0
Documentation/config.txt submodule.alternateErrorStrategy · line 2951
Quoted 7 lines
Documentation/config.txt +4
-0@@ -2945,6 +2945,11 @@ submodule.alternateErrorStrategy as computed via `submodule.alternateLocation`. Possible values are `ignore`, `info`, `die`. Default is `die`. +tag.createReflog:: + A boolean to specify whether newly created tags should have a reflog. + If `--[no-]create-reflog` is specified on the command line, it takes + precedence. Defaults to `false`.
This follows the convention, good! :)
Quoted 88 lines
Documentation/config.txt +0
-0tag.forceSignAnnotated:: A boolean to specify whether annotated tags created should be GPG signed. If `--annotate` is specified on the command line, it takesDocumentation/git-tag.txt +5
-3@@ -13,7 +13,7 @@ SYNOPSIS <tagname> [<commit> | <object>] 'git tag' -d <tagname>... 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>] - [--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] + [--column[=<options>] | --no-column] [--[no-]create-reflog] [--sort=<key>] [--format=<format>] [--[no-]merged [<commit>]] [<pattern>...] 'git tag' -v <tagname>...@@ -149,8 +149,10 @@ This option is only applicable when listing tags without annotation lines. all, 'whitespace' removes just leading/trailing whitespace lines and 'strip' removes both whitespace and commentary. ---create-reflog:: - Create a reflog for the tag. +--[no-]create-reflog:: + Force to create a reflog for the tag, or no reflog if `--no-create-reflog` + is used. Unless the `tag.createReflog` config variable is set to true, no + reflog is created by default. See linkgit:git-config[1]. <tagname>:: The name of the tag to create, delete, or describe.builtin/tag.c +5
-1@@ -30,6 +30,7 @@ static const char * const git_tag_usage[] = {static unsigned int colopts;static int force_sign_annotate; +static int create_reflog;static int list_tags (struct ref_filter *filter,struct ref_sorting *sorting,const char *format) {@@ -165,6 +166,10 @@ static int git_tag_config(const char *var, const char *value, void *cb) force_sign_annotate =git_config_bool (var, value);return 0 ; } +if (!strcmp (var,"tag.createreflog" )) { + create_reflog =git_config_bool (var, value); +return 0 ; + }if (starts_with (var,"column." ))return git_column_config (var, value,"tag" , &colopts);@@ -325,7 +330,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)const char *object_ref, *tag;struct create_tag_options opt;char *cleanup_arg =NULL ; -int create_reflog =0 ;int annotate =0 , force =0 ;int cmdmode =0 , create_tag_object =0 ;const char *msgfile =NULL , *keyid =NULL ;t/t7004-tag.sh +14
-1@@ -90,6 +90,20 @@ test_expect_success '--create-reflog does not create reflog on failure' ' test_must_fail git reflog exists refs/tags/mytag ' +test_expect_success 'option tag.createreflog creates reflog by default' ' + test_when_finished "git tag -d tag_with_reflog" && + git config tag.createReflog true && + git tag tag_with_reflog && + git reflog exists refs/tags/tag_with_reflog +' + +test_expect_success 'option tag.createreflog overridden by command line' ' + test_when_finished "git tag -d tag_without_reflog" && + git config tag.createReflog true && + git tag --no-create-reflog tag_without_reflog && + test_must_fail git reflog exists refs/tags/tag_without_reflog +' + test_expect_success 'listing all tags if one exists should succeed' ' git tag -l && git tag --2.10.2
Quoted 6 lines · On Wed, Jan 25, 2017 at 01:19:06AM +0100, [email protected] wrote:
From: Cornelius Weig <[email protected]>
Git does not create a history for tags, in contrast to common expectation to simply version everything. This can be changed by using the
--create-reflogflag when creating the tag. However, a config option to enable this behavior by default is missing.
Hmm, I didn't even know we had "tag --create-reflog". Looks like it was added by 144c76fa39 (update-ref and tag: add --create-reflog arg, 2015-07-21).
IMHO it is a mistake. The "update-ref --create-reflog" variant makes sense to me as a plumbing operation. But are there end users who want to create a reflog for just _one_ tag?
As your patch shows, the more likely variant is "I want reflogs for all tags". But that raises two questions with your patch:
- yours isn't "reflogs for all tags". It's "reflogs for tags I created with git-tag". What about other operations that create tags, like fetching (or even just a script that uses update-ref under the hood).
IOW, instead of tag.createReflog, should this be tweaing core.logallrefupdates to have a mode that includes tags?
- Is that the end of it, or is the desire really "I want reflogs for _everything_"? That seems like a sane thing to want.
If so, then the update to core.logallrefupdates should turn it into a tri-state:
- false; no reflogs
- true; reflogs for branches, remotes, notes, as now
- always; reflogs for all refs under "refs/"
I made a lot of suppositions about your desires there, so maybe you really do want just tag.createReflog. But "core.logallrefupdates = always" sounds a lot more useful to me.
-Peff
Quoted 3 lines · Jeff King
I made a lot of suppositions about your desires there, so maybe you really do want just tag.createReflog. But "core.logallrefupdates = always" sounds a lot more useful to me.
Thanks for saving me from typing exactly the same thing ;-)
Quoted 12 lines · Jeff King
- Is that the end of it, or is the desire really "I want reflogs for _everything_"? That seems like a sane thing to want.
If so, then the update to core.logallrefupdates should turn it into a tri-state:
- false; no reflogs
- true; reflogs for branches, remotes, notes, as now
- always; reflogs for all refs under "refs/"
I think you nailed it. This is much more useful than what I suggested. I'll see if I can code it up.
Quoted 10 lines · Cornelius Weig
Quoted 8 lines · Jeff King
- Is that the end of it, or is the desire really "I want reflogs for _everything_"? That seems like a sane thing to want.
If so, then the update to core.logallrefupdates should turn it into a tri-state:
- false; no reflogs
Quoted 7 lines
Quoted 4 lines
- true; reflogs for branches, remotes, notes, as now
- always; reflogs for all refs under "refs/"
I think you nailed it. This is much more useful than what I suggested. I'll see if I can code it up.
I cheated a little. I actually wrote the "always" patch 3 years ago as part of another thing I was working on. But in the end I didn't need it, and never submitted it.
The patch is below for reference. I have no idea whether it even applies now, let alone runs and does the right thing. But perhaps you can salvage bits of it (but feel free to ignore it if it makes things harder).
-- >8 --
From: Jeff King <[email protected]>
Date: Mon, 15 Apr 2013 23:31:05 -0400
Subject: [PATCH] teach core.logallrefupdates an "always" mode
When core.logallrefupdates is true, we only create a new reflog for refs that are under certain well-known hierarchies. The reason is that we know that some hierarchies (like refs/tags) do not typically change, and that unknown hierarchies might not want reflogs at all (e.g., a hypothetical refs/foo might be meant to change often and drop old history immediately).
However, sometimes it is useful to override this decision and simply log for all refs, because the safety and audit trail is more important than the performance implications of keeping the log around.
This patch introduces a new "always" mode for the core.logallrefupdates option which will log updates to everything under refs/, regardless where in the hierarchy it is (we still will not log things like ORIG_HEAD and FETCH_HEAD, which are known to be transient).
--- Documentation/config.txt | 8 +++++--- branch.c | 2 +- builtin/checkout.c | 2 +- builtin/init-db.c | 2 +- cache.h | 9 ++++++++- config.c | 7 ++++++- environment.c | 2 +- refs.c | 23 +++++++++++++++++------ t/t1400-update-ref.sh | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 72 insertions(+), 15 deletions(-)
Documentation/config.txt +5 -3
@@ -390,10 +390,12 @@ core.logAllRefUpdates:: "$GIT_DIR/logs/<ref>", by appending the new and old SHA1, the date/time and the reason of the update, but only when the file exists. If this configuration - variable is set to true, missing "$GIT_DIR/logs/<ref>" + variable is set to `true`, a missing "$GIT_DIR/logs/<ref>" file is automatically created for branch heads (i.e. under - refs/heads/), remote refs (i.e. under refs/remotes/), - note refs (i.e. under refs/notes/), and the symbolic ref HEAD. + `refs/heads/`), remote refs (i.e. under `refs/remotes/`), + note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + If it is set to `always`, then a missing reflog is automatically + created for any ref under `refs/`. + This information can be used to determine what commit was the tip of a branch "2 days ago".
branch.c +1 -1
@@ -259,7 +259,7 @@ void create_branch(const char *head, }if (reflog) - log_all_ref_updates =1 ; + log_all_ref_updates =LOG_REFS_NORMAL ;if (forcing) snprintf(msg, sizeof msg, "branch: Reset to %s",
builtin/checkout.c +1 -1
@@ -564,7 +564,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,char *ref_name =mkpath ("refs/heads/%s" , opts->new_orphan_branch); temp = log_all_ref_updates; - log_all_ref_updates =1 ; + log_all_ref_updates =LOG_REFS_NORMAL ;if (log_ref_setup (ref_name, log_file,sizeof (log_file))) {fprintf (stderr,_ ("Can not do reflog for '%s'\n" ), opts->new_orphan_branch);
builtin/init-db.c +1 -1
@@ -264,7 +264,7 @@ static int create_default_files(const char *template_path)const char *work_tree =get_git_work_tree ();git_config_set ("core.bare" ,"false" );/* allow template config file to override the default */ -if (log_all_ref_updates ==-1 ) +if (log_all_ref_updates ==LOG_REFS_UNSET )git_config_set ("core.logallrefupdates" ,"true" );if (prefixcmp (git_dir, work_tree) ||strcmp (git_dir +strlen (work_tree),"/.git" )) {
cache.h +8 -1
@@ -536,7 +536,6 @@ extern int minimum_abbrev, default_abbrev;extern int ignore_case;extern int assume_unchanged;extern int prefer_symlink_refs; -extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int shared_repository;extern const char *apply_default_whitespace;
@@ -556,6 +555,14 @@ extern int core_preload_index;extern int core_apply_sparse_checkout;extern int precomposed_unicode; +enum log_refs_config { +LOG_REFS_UNSET =-1 , +LOG_REFS_NONE =0 , +LOG_REFS_NORMAL ,/* see should_create_reflog for rules */ +LOG_REFS_ALWAYS +}; +extern enum log_refs_config log_all_ref_updates; +enum branch_track {BRANCH_TRACK_UNSPECIFIED =-1 ,BRANCH_TRACK_NEVER =0 ,
config.c +6 -1
@@ -601,7 +601,12 @@ static int git_default_core_config(const char *var, const char *value) }if (!strcmp (var,"core.logallrefupdates" )) { - log_all_ref_updates =git_config_bool (var, value); +if (value && !strcasecmp (value,"always" )) + log_all_ref_updates =LOG_REFS_ALWAYS ; +else if (git_config_bool (var, value)) + log_all_ref_updates =LOG_REFS_NORMAL ; +else + log_all_ref_updates =LOG_REFS_NONE ;return 0 ; }
environment.c +1 -1
@@ -19,7 +19,7 @@ int ignore_case;int assume_unchanged;int prefer_symlink_refs;int is_bare_repository_cfg =-1 ;/* unspecified */ -int log_all_ref_updates =-1 ;/* unspecified */ +enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;int warn_ambiguous_refs =1 ;int repository_format_version;const char *git_commit_encoding;
refs.c +17 -6
@@ -1896,7 +1896,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms lock->force_write =1 ; flag = log_all_ref_updates; - log_all_ref_updates =0 ; + log_all_ref_updates =LOG_REFS_NONE ;if (write_ref_sha1 (lock, orig_sha1,NULL ))error ("unable to write current sha1 into %s" , oldrefname); log_all_ref_updates = flag;
@@ -1965,16 +1965,27 @@ static int copy_msg(char *buf, const char *msg)return cp - buf; } +int should_create_reflog (const char *refname) +{ +switch (log_all_ref_updates) { +case LOG_REFS_ALWAYS : +return 1 ; +case LOG_REFS_NORMAL : +return !prefixcmp (refname,"refs/heads/" ) || + !prefixcmp (refname,"refs/remotes/" ) || + !prefixcmp (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" ); +default : +return 0 ; + } +} +int log_ref_setup (const char *refname,char *logfile,int bufsize) {int logfd, oflags =O_APPEND |O_WRONLY ;git_snpath (logfile, bufsize,"logs/%s" , refname); -if (log_all_ref_updates && - (!prefixcmp (refname,"refs/heads/" ) || - !prefixcmp (refname,"refs/remotes/" ) || - !prefixcmp (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ))) { +if (should_create_reflog (refname)) {if (safe_create_leading_directories (logfile) <0 )return error ("unable to create directory for %s" , logfile);
t/t1400-update-ref.sh +32 -0
@@ -302,4 +302,36 @@ test_expect_success \ 'git cat-file blob master@{2005-05-26 23:42}:F (expect OTHER)' \ 'test OTHER = $(git cat-file blob "master@{2005-05-26 23:42}:F")' +test_expect_success 'core.logAllRefUpdates=true does not log refs/foo/' ' + test_config core.logAllRefUpdates true && + test_commit log-true && + git update-ref -m reflog-message refs/heads/logme HEAD && + git update-ref -m reflog-message refs/foo/logme HEAD && + { + echo "refs/heads/logme@{0} reflog-message" + } >expect && + { + git log -g -1 --format="%gD %gs" refs/heads/logme && + git log -g -1 --format="%gD %gs" refs/foo/logme + } >actual && + test_cmp expect actual +' + +test_expect_success 'core.logAllRefUpdates=always logs refs/foo/' ' + test_config core.logAllRefUpdates always && + test_commit log-always && + git update-ref -m reflog-message refs/heads/logme HEAD && + git update-ref -m reflog-message refs/foo/logme HEAD && + { + echo "refs/heads/logme@{0} reflog-message" + echo "refs/foo/logme@{0} reflog-message" + } >expect && + { + git log -g -1 --format="%gD %gs" refs/heads/logme && + git log -g -1 --format="%gD %gs" refs/foo/logme + } >actual && + test_cmp expect actual +' + + test_done
Signature · 1 line
2.11.0.840.gd37c5973a
Quoted 12 lines · Jeff King
(quoted diff) +12
-0+enum log_refs_config { + LOG_REFS_UNSET = -1, + LOG_REFS_NONE = 0, + LOG_REFS_NORMAL, /* see should_create_reflog for rules */ + LOG_REFS_ALWAYS +}; +extern enum log_refs_config log_all_ref_updates; +... +int should_create_reflog(const char *refname) +{ + switch (log_all_ref_updates) { + case LOG_REFS_ALWAYS:
Review
Quoted 10 lines
(quoted diff) +10
-0+ return 1; + case LOG_REFS_NORMAL: + return !prefixcmp(refname, "refs/heads/") || + !prefixcmp(refname, "refs/remotes/") || + !prefixcmp(refname, "refs/notes/") || + !strcmp(refname, "HEAD"); + default: + return 0; + } +}
Yup, this is how I expected for the feature to be done.
Just a hint for Cornelius; prefixcmp() is an old name for what is called starts_with() these days.
Quoted 27 lines · Junio C Hamano
Quoted 12 lines · Jeff King
(quoted diff) +12
-0+enum log_refs_config { + LOG_REFS_UNSET = -1, + LOG_REFS_NONE = 0, + LOG_REFS_NORMAL, /* see should_create_reflog for rules */ + LOG_REFS_ALWAYS +}; +extern enum log_refs_config log_all_ref_updates; +... +int should_create_reflog(const char *refname) +{ + switch (log_all_ref_updates) { + case LOG_REFS_ALWAYS:Review
Quoted 10 lines
(quoted diff) +10
-0+ return 1; + case LOG_REFS_NORMAL: + return !prefixcmp(refname, "refs/heads/") || + !prefixcmp(refname, "refs/remotes/") || + !prefixcmp(refname, "refs/notes/") || + !strcmp(refname, "HEAD"); + default: + return 0; + } +}Yup, this is how I expected for the feature to be done.
Quoted 2 lines
Just a hint for Cornelius;
prefixcmp()is an old name for what is calledstarts_with()these days.
It may have been obvious, but to be explicit for somebody new, !prefixcmp() corresponds to starts_with(). IOW, we changed the meaning of the return value when moving from cmp-lookalike (where 0 means "equal") to "does it start with this string?" bool (where 1 means "yes").
Quoted 7 lines
It may have been obvious, but to be explicit for somebody new, !prefixcmp() corresponds to
starts_with(). IOW, we changed the meaning of the return value when moving from cmp-lookalike (where 0 means "equal") to "does it start with this string?" bool (where 1 means "yes").
I see. It reads much better that way!
I re-did all the changes from Jeff's patch, but some tests are breaking now. I will have to mend that tomorrow, because it's already too late in my timezone.
Thanks a lot for your support m(_ _)m
PATCH 1/1 refs: add option core.logAllRefUpdates = always
In reply to Jeff KingHi peff,
you made it easy for me. Most of your patch still applied, only the tests
didn't quite fit. Maybe you can have a look if I've overlooked something, since you know the changes best?
Thanks for supporting this with your patch!
PATCH 1/1 refs: add option core.logAllRefUpdates = always
From: Cornelius Weig <[email protected]>
When core.logallrefupdates is true, we only create a new reflog for refs that are under certain well-known hierarchies. The reason is that we know that some hierarchies (like refs/tags) do not typically change, and that unknown hierarchies might not want reflogs at all (e.g., a hypothetical refs/foo might be meant to change often and drop old history immediately).
However, sometimes it is useful to override this decision and simply log for all refs, because the safety and audit trail is more important than the performance implications of keeping the log around.
This patch introduces a new "always" mode for the core.logallrefupdates option which will log updates to everything under refs/, regardless where in the hierarchy it is (we still will not log things like ORIG_HEAD and FETCH_HEAD, which are known to be transient).
--- Documentation/config.txt | 7 +++++-- Documentation/git-tag.txt | 3 ++- branch.c | 2 +- builtin/init-db.c | 2 +- cache.h | 9 +++++++- config.c | 7 ++++++- environment.c | 2 +- refs.c | 15 +++++++++----- refs/files-backend.c | 6 +++--- t/t1400-update-ref.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 90 insertions(+), 16 deletions(-)
Documentation/config.txt +5 -2
@@ -517,10 +517,13 @@ core.logAllRefUpdates:: "`$GIT_DIR/logs/<ref>`", by appending the new and old SHA-1, the date/time and the reason of the update, but only when the file exists. If this configuration - variable is set to true, missing "`$GIT_DIR/logs/<ref>`" + variable is set to `true`, missing "`$GIT_DIR/logs/<ref>`" file is automatically created for branch heads (i.e. under refs/heads/), remote refs (i.e. under refs/remotes/), - note refs (i.e. under refs/notes/), and the symbolic ref HEAD. + `refs/heads/`), remote refs (i.e. under `refs/remotes/`), + note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + If it is set to `always`, then a missing reflog is automatically + created for any ref under `refs/`. + This information can be used to determine what commit was the tip of a branch "2 days ago".
Documentation/git-tag.txt +2 -1
@@ -150,7 +150,8 @@ This option is only applicable when listing tags without annotation lines. 'strip' removes both whitespace and commentary. --create-reflog:: - Create a reflog for the tag. + Create a reflog for the tag. To globally enable reflogs for tags, see + `core.logAllRefUpdates` in linkgit:git-config[1]. <tagname>:: The name of the tag to create, delete, or describe.
branch.c +1 -1
@@ -298,7 +298,7 @@ void create_branch(const char *name, const char *start_name, start_name);if (reflog) - log_all_ref_updates =1 ; + log_all_ref_updates =LOG_REFS_NORMAL ;if (!dont_change_ref) {struct ref_transaction *transaction;
builtin/init-db.c +1 -1
@@ -262,7 +262,7 @@ static int create_default_files(const char *template_path,const char *work_tree =get_git_work_tree ();git_config_set ("core.bare" ,"false" );/* allow template config file to override the default */ -if (log_all_ref_updates ==-1 ) +if (log_all_ref_updates ==LOG_REFS_UNSET )git_config_set ("core.logallrefupdates" ,"true" );if (needs_work_tree_config (original_git_dir, work_tree))git_config_set ("core.worktree" , work_tree);
cache.h +8 -1
@@ -660,7 +660,6 @@ extern int minimum_abbrev, default_abbrev;extern int ignore_case;extern int assume_unchanged;extern int prefer_symlink_refs; -extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int warn_on_object_refname_ambiguity;extern const char *apply_default_whitespace;
@@ -728,6 +727,14 @@ enum hide_dotfiles_type { };extern enum hide_dotfiles_type hide_dotfiles; +enum log_refs_config { +LOG_REFS_UNSET =-1 , +LOG_REFS_NONE =0 , +LOG_REFS_NORMAL , +LOG_REFS_ALWAYS +}; +extern enum log_refs_config log_all_ref_updates; +enum branch_track {BRANCH_TRACK_UNSPECIFIED =-1 ,BRANCH_TRACK_NEVER =0 ,
config.c +6 -1
@@ -826,7 +826,12 @@ static int git_default_core_config(const char *var, const char *value) }if (!strcmp (var,"core.logallrefupdates" )) { - log_all_ref_updates =git_config_bool (var, value); +if (value && !strcasecmp (value,"always" )) + log_all_ref_updates =LOG_REFS_ALWAYS ; +else if (git_config_bool (var, value)) + log_all_ref_updates =LOG_REFS_NORMAL ; +else + log_all_ref_updates =LOG_REFS_NONE ;return 0 ; }
environment.c +1 -1
@@ -21,7 +21,6 @@ int ignore_case;int assume_unchanged;int prefer_symlink_refs;int is_bare_repository_cfg =-1 ;/* unspecified */ -int log_all_ref_updates =-1 ;/* unspecified */ int warn_ambiguous_refs =1 ;int warn_on_object_refname_ambiguity =1 ;int ref_paranoia =-1 ;
@@ -64,6 +63,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ unsigned long pack_size_limit_cfg;enum hide_dotfiles_type hide_dotfiles =HIDE_DOTFILES_DOTGITONLY ; +enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;#ifndef PROTECT_HFS_DEFAULT #define PROTECT_HFS_DEFAULT 0
refs.c +10 -5
@@ -638,12 +638,17 @@ int copy_reflog_msg(char *buf, const char *msg)int should_autocreate_reflog (const char *refname) { -if (!log_all_ref_updates) +switch (log_all_ref_updates) { +case LOG_REFS_ALWAYS : +return 1 ; +case LOG_REFS_NORMAL : +return starts_with (refname,"refs/heads/" ) || +starts_with (refname,"refs/remotes/" ) || +starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" ); +default :return 0 ; -return starts_with (refname,"refs/heads/" ) || -starts_with (refname,"refs/remotes/" ) || -starts_with (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ); + } }int is_branch (const char *refname)
refs/files-backend.c +3 -3
@@ -2682,7 +2682,7 @@ static int files_rename_ref(struct ref_store *ref_store, } flag = log_all_ref_updates; - log_all_ref_updates =0 ; + log_all_ref_updates =LOG_REFS_NONE ;if (write_ref_to_lockfile (lock, orig_sha1, &err) ||commit_ref_update (refs, lock, orig_sha1,NULL , &err)) {error ("unable to write current sha1 into %s: %s" , oldrefname, err.buf);
@@ -2835,8 +2835,8 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, {int logfd, result, oflags =O_APPEND |O_WRONLY ; -if (log_all_ref_updates <0 ) - log_all_ref_updates = !is_bare_repository (); +if (log_all_ref_updates ==LOG_REFS_UNSET ) + log_all_ref_updates =is_bare_repository () ?LOG_REFS_NONE :LOG_REFS_NORMAL ; result =log_ref_setup (refname, logfile, err, flags &REF_FORCE_CREATE_REFLOG );
t/t1400-update-ref.sh +53 -0
71 lines not on this page · raw message
Signature · 1 line
2.10.2
Re: [PATCH] refs: add option core.logAllRefUpdates = always
Quoted 8 lines · On Thu, Jan 26, 2017 at 02:16:54AM +0100, [email protected] wrote:
From: Cornelius Weig <[email protected]>
When core.logallrefupdates is true, we only create a new reflog for refs that are under certain well-known hierarchies. The reason is that we know that some hierarchies (like refs/tags) do not typically change, and that unknown hierarchies might not want reflogs at all (e.g., a hypothetical refs/foo might be meant to change often and drop old history immediately).
I tried to read this patch with fresh eyes. But given the history, you may take my review with a grain of salt. :)
Overall it looks OK to me. A few comments below.
Quoted 4 lines
This patch introduces a new "always" mode for the core.logallrefupdates option which will log updates to everything under refs/, regardless where in the hierarchy it is (we still will not log things like
ORIG_HEADandFETCH_HEAD, which are known to be transient).
I don't think my original had tests for this, but it might be worth adding a test for this last bit (i.e., that an update of ORIG_HEAD does not write a reflog when logallrefupdates is set to "always").
Quoted 7 lines
Documentation/config.txt +0
-0@@ -517,10 +517,13 @@ core.logAllRefUpdates:: "`$GIT_DIR/logs/<ref>`", by appending the new and old SHA-1, the date/time and the reason of the update, but
Documentation/config.txt core.logAllRefUpdates:: · line 526
Quoted 10 lines
Documentation/config.txt +5
-2only when the file exists. If this configuration - variable is set to true, missing "`$GIT_DIR/logs/<ref>`" + variable is set to `true`, missing "`$GIT_DIR/logs/<ref>`" file is automatically created for branch heads (i.e. under refs/heads/), remote refs (i.e. under refs/remotes/), - note refs (i.e. under refs/notes/), and the symbolic ref HEAD. + `refs/heads/`), remote refs (i.e. under `refs/remotes/`), + note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + If it is set to `always`, then a missing reflog is automatically + created for any ref under `refs/`.
I guess the backtick fixups came from my original. It might be easier to see the change if they were pulled into their own patch, but it's probably not that big a deal.
Documentation/git-tag.txt This option is only applicable when listing tags without annotation lines. · line 154
Quoted 9 lines
Documentation/git-tag.txt +2
-1@@ -150,7 +150,8 @@ This option is only applicable when listing tags without annotation lines. 'strip' removes both whitespace and commentary. --create-reflog:: - Create a reflog for the tag. + Create a reflog for the tag. To globally enable reflogs for tags, see + `core.logAllRefUpdates` in linkgit:git-config[1].
This documentation tweak makes sense to me.
builtin/init-db.c static int create_default_files(const char *template_path, · line 268
Quoted 13 lines
builtin/init-db.c +1
-1@@ -262,7 +262,7 @@ static int create_default_files(const char *template_path,const char *work_tree =get_git_work_tree ();git_config_set ("core.bare" ,"false" );/* allow template config file to override the default */ -if (log_all_ref_updates ==-1 ) +if (log_all_ref_updates ==LOG_REFS_UNSET )git_config_set ("core.logallrefupdates" ,"true" );if (needs_work_tree_config (original_git_dir, work_tree))git_config_set ("core.worktree" , work_tree);
I expected that this hunk would need tweaked due to refactoring around init-db that happened earlier this year. But it seems fine.
Quoted 15 lines
refs.c +6
-1@@ -638,12 +638,17 @@ int copy_reflog_msg(char *buf, const char *msg)int should_autocreate_reflog (const char *refname) { -if (!log_all_ref_updates) +switch (log_all_ref_updates) { +case LOG_REFS_ALWAYS : +return 1 ; +case LOG_REFS_NORMAL : +return starts_with (refname,"refs/heads/" ) || + starts_with(refname, "refs/remotes/") ||
refs.c int copy_reflog_msg(char *buf, const char *msg) · line 652
Quoted 10 lines
refs.c +4
-4+starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" ); +default :return 0 ; -return starts_with (refname,"refs/heads/" ) || -starts_with (refname,"refs/remotes/" ) || -starts_with (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ); + } }
And this function got broken out already by David in an earlier patch. Looks good.
refs.c static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, · line 2839
Quoted 8 lines
refs.c +2
-2@@ -2835,8 +2835,8 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, {int logfd, result, oflags =O_APPEND |O_WRONLY ; -if (log_all_ref_updates <0 ) - log_all_ref_updates = !is_bare_repository (); +if (log_all_ref_updates ==LOG_REFS_UNSET ) + log_all_ref_updates =is_bare_repository () ?LOG_REFS_NONE :LOG_REFS_NORMAL ;
This hunk is new, I think. The enum values are set in such a way that the original code would have continued to work, but I think using the symbolic names is an improvement.
I assume you grepped for log_all_ref_updates to find this. I see only one spot that now doesn't use the symbolic names. In builtin/checkout.c, update_refs_for_switch() checks:
if (opts->new_branch_log && !log_all_ref_updates)
That looks buggy, as it would treat LOG_REFS_NORMAL and LOG_REFS_UNSET the same, and I do not see us resolving the UNSET case to a true/false value. But I don't think the bug is new in your patch; the default value was "-1" already.
I doubt it can be triggered in practice, because either:
- the config value is set in the config file, and we pick up that value, whether it's "true" or "false"
- it's unset, in which case our default would be to enable reflogs in a non-bare repo. And since git-checkout would refuse to run in a bare repo, we must be non-bare, and thus enabling reflogs does the right thing.
But it works quite by accident. I wonder if we should this "is_bare_repository" check into a function that can be called instead of accessing log_all_ref_updates() directly.
Quoted 15 lines
t/t1400-update-ref.sh +9
-0@@ -93,6 +93,36 @@ test_expect_success 'update-ref creates reflogs with --create-reflog' ' git reflog exists $outside ' +test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' ' + test_config core.logAllRefUpdates true && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + test_must_fail git reflog exists $outside +'
t/t1400-update-ref.sh test_expect_success 'update-ref creates reflogs with --create-reflog' ' · line 114
Quoted 10 lines
t/t1400-update-ref.sh +10
-0+ +test_expect_success 'core.logAllRefUpdates=always creates reflog by default' ' + test_config core.logAllRefUpdates always && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + git reflog exists $outside +'
Adding the tests to the existing --create-reflog tests is a good choice.
t/t1400-update-ref.sh test_expect_success 'update-ref creates reflogs with --create-reflog' ' · line 116
Quoted 1 line
t/t1400-update-ref.sh +1
-0+test_expect_success 'update-ref does not create reflog with --no-create-reflog if core.logAllRefUpdates=always' '
This test title is _really_ long, and will wrap in the output on reasonable-sized terminals. Maybe '--no-create-reflog overrides core.logAllRefUpdates=always' would be shorter?
t/t1400-update-ref.sh test_expect_success 'stdin does not create reflogs by default' ' · line 537
Quoted 5 lines
t/t1400-update-ref.sh +1
-0test_expect_success 'stdin creates reflogs with --create-reflog' ' + test_when_finished "git update-ref -d $outside" && echo "create $outside $m" >stdin && git update-ref --create-reflog --stdin <stdin && git rev-parse $m >expect &&
Adding missing cleanup. Good.
Quoted 1 line
+test_expect_success 'stdin does not create reflog when core.logAllRefUpdates=true' '
I don't mind these extra stdin tests, but IMHO they are just redundant. The "--stdin --create-reflog" one makes sure the option is propagated down via the --stdin machinery. But we know the config option is handled at a low level anyway.
I guess it depends on how black-box we want the testing to be. It just seems unlikely for a regression to be found here and not in the tests above.
-Peff
Re: [PATCH] refs: add option core.logAllRefUpdates = always
Hi Peff,
thanks for your thoughts.
Quoted 2 lines
I tried to read this patch with fresh eyes. But given the history, you may take my review with a grain of salt. :)
Does it mean another reviewer is needed?
Quoted 3 lines
I don't think my original had tests for this, but it might be worth adding a test for this last bit (i.e., that an update of
ORIG_HEADdoes not write a reflog when logallrefupdates is set to "always").
Good point. I blindly copied your commit message without thinking too much about it.
Quoted 3 lines
I guess the backtick fixups came from my original. It might be easier to see the change if they were pulled into their own patch, but it's probably not that big a deal.
If it's best practice to break out such changes, I'll revise it.
Quoted 4 lines
Quoted 4 lines
refs/files-backend.c +0
-0@@ -2835,8 +2835,8 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, {int logfd, result, oflags =O_APPEND |O_WRONLY ;
Quoted 8 lines
refs/files-backend.c static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, · line 2839
Quoted 4 lines
refs/files-backend.c +2
-2-if (log_all_ref_updates <0 ) - log_all_ref_updates = !is_bare_repository (); +if (log_all_ref_updates ==LOG_REFS_UNSET ) + log_all_ref_updates =is_bare_repository () ?LOG_REFS_NONE :LOG_REFS_NORMAL ;This hunk is new, I think. The enum values are set in such a way that the original code would have continued to work, but I think using the symbolic names is an improvement.
Yes it's new.
Quoted 13 lines
I assume you grepped for
log_all_ref_updatesto find this. I see only one spot that now doesn't use the symbolic names. Inbuiltin/checkout.c,update_refs_for_switch()checks:if (opts->new_branch_log && !log_all_ref_updates)That looks buggy, as it would treat
LOG_REFS_NORMALandLOG_REFS_UNSETthe same, and I do not see us resolving the UNSET case to a true/false value. But I don't think the bug is new in your patch; the default value was "-1" already.I doubt it can be triggered in practice, because either:
Quoted 7 lines
- the config value is set in the config file, and we pick up that value, whether it's "true" or "false"
- it's unset, in which case our default would be to enable reflogs in a non-bare repo. And since git-checkout would refuse to run in a bare repo, we must be non-bare, and thus enabling reflogs does the right thing.
That far I can follow.
Quoted 3 lines
But it works quite by accident. I wonder if we should this "
is_bare_repository" check into a function that can be called instead of accessinglog_all_ref_updates()directly.
Are you saying that we should move the !log_all_ref_updates check into its own function where we should also check is_bare_repository? I don't see that this would win much, because as you said: checkouts in a bare repo are forbidden anyway.
Other than that, I guess it should better read log_all_ref_update != LOG_REFS_NONE instead of !log_all_ref_updates.
Quoted 5 lines
t/t1400-update-ref.sh test_expect_success 'update-ref creates reflogs with --create-reflog' ' · line 116
Quoted 1 line
t/t1400-update-ref.sh +1
-0+test_expect_success 'update-ref does not create reflog with --no-create-reflog if core.logAllRefUpdates=always' 'This test title is _really_ long, and will wrap in the output on reasonable-sized terminals. Maybe '--no-create-reflog overrides core.logAllRefUpdates=always' would be shorter?
Yes, I agree.
Quoted 10 lines
t/t1400-update-ref.sh test_expect_success 'stdin creates reflogs with --create-reflog' ' · line 543
Quoted 1 line
t/t1400-update-ref.sh +1
-0+test_expect_success 'stdin does not create reflog when core.logAllRefUpdates=true' 'I don't mind these extra stdin tests, but IMHO they are just redundant. The "--stdin --create-reflog" one makes sure the option is propagated down via the --stdin machinery. But we know the config option is handled at a low level anyway.
I guess it depends on how black-box we want the testing to be. It just seems unlikely for a regression to be found here and not in the tests above.
Since these other stdin tests were around, I added this variant. But you're right: this test breaks along with the other and doesn't add add more safety. I'll remove it.
However, I realized that I have not written tests about ref updates in a bare repository. Do you think it's worthwile?
Cheers,
Cornelius
Re: [PATCH] refs: add option core.logAllRefUpdates = always
Quoted 8 lines · Cornelius Weig
Quoted 3 lines
But it works quite by accident. I wonder if we should this "
is_bare_repository" check into a function that can be called instead of accessinglog_all_ref_updates()directly.Are you saying that we should move the
!log_all_ref_updatescheck into its own function where we should also checkis_bare_repository? I don't see that this would win much, because as you said: checkouts in a bare repo are forbidden anyway.
Yes, I'm suggesting making something like the should_autocreate_reflog() function public.
I agree it is working correctly now. It's just that it's rather subtle that it treats LOG_REFS_UNSET implicitly as LOG_REFS_NONE.
It would also possibly break if more values are added to the enum (depending on what those values are).
Quoted 2 lines
However, I realized that I have not written tests about ref updates in a bare repository. Do you think it's worthwile?
There should already be a test for logAllRefUpdates=true in a bare repository (if there isn't, we should probably add one). Testing the "always" case individually does not add much over testing it in a non-bare repository. IMHO.
-Peff
PATCH v2 2/3 refs: add option core.logAllRefUpdates = always
In reply to [email protected]From: Cornelius Weig <[email protected]>
When core.logallrefupdates is true, we only create a new reflog for refs that are under certain well-known hierarchies. The reason is that we know that some hierarchies (like refs/tags) do not typically change, and that unknown hierarchies might not want reflogs at all (e.g., a hypothetical refs/foo might be meant to change often and drop old history immediately).
However, sometimes it is useful to override this decision and simply log for all refs, because the safety and audit trail is more important than the performance implications of keeping the log around.
This patch introduces a new "always" mode for the core.logallrefupdates option which will log updates to everything under refs/, regardless where in the hierarchy it is (we still will not log things like ORIG_HEAD and FETCH_HEAD, which are known to be transient).
---
Notes:
Changes with respect to the previous version:
- add test that checks that no reflog is created for ORIG_HEAD if core.logAllRefUpdates=always
- remove redundant tests that check reflog creation when update-ref is called with --stdin
- make test description shorter
- make the function should_autocreate_reflog() public and use it in update_refs_for_switch().
The last item addresses Peff's concern that the previous version only works by accident and may break in the future (see [email protected]). In particular, this concerns the following change:
- if (opts->new_branch_log && !log_all_ref_updates) {
+ if (opts->new_branch_log && should_autocreate_reflog("refs/heads/")) {
The function call to should_autocreate_reflog() answers exactly the question that the original test !log_all_ref_updates tried to resolve in the original version.
Documentation/config.txt | 2 ++ Documentation/git-tag.txt | 3 ++- branch.c | 2 +- builtin/checkout.c | 2 +- builtin/init-db.c | 2 +- cache.h | 9 ++++++++- config.c | 7 ++++++- environment.c | 2 +- refs.c | 15 ++++++++++----- refs.h | 2 ++ refs/files-backend.c | 6 +++--- refs/refs-internal.h | 2 -- t/t1400-update-ref.sh | 37 +++++++++++++++++++++++++++++++++++++ 13 files changed, 74 insertions(+), 17 deletions(-)
Documentation/config.txt +2 -0
@@ -522,6 +522,8 @@ core.logAllRefUpdates:: refs/heads/), remote refs (i.e. under refs/remotes/), `refs/heads/`), remote refs (i.e. under `refs/remotes/`), note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + If it is set to `always`, then a missing reflog is automatically + created for any ref under `refs/`. + This information can be used to determine what commit was the tip of a branch "2 days ago".
Documentation/git-tag.txt +2 -1
@@ -150,7 +150,8 @@ This option is only applicable when listing tags without annotation lines. 'strip' removes both whitespace and commentary. --create-reflog:: - Create a reflog for the tag. + Create a reflog for the tag. To globally enable reflogs for tags, see + `core.logAllRefUpdates` in linkgit:git-config[1]. <tagname>:: The name of the tag to create, delete, or describe.
branch.c +1 -1
@@ -298,7 +298,7 @@ void create_branch(const char *name, const char *start_name, start_name);if (reflog) - log_all_ref_updates =1 ; + log_all_ref_updates =LOG_REFS_NORMAL ;if (!dont_change_ref) {struct ref_transaction *transaction;
builtin/checkout.c +1 -1
@@ -612,7 +612,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -if (opts->new_branch_log && !log_all_ref_updates) { +if (opts->new_branch_log &&should_autocreate_reflog ("refs/heads/" )) {int ret;char *refname;struct strbuf err =STRBUF_INIT ;
builtin/init-db.c +1 -1
@@ -262,7 +262,7 @@ static int create_default_files(const char *template_path,const char *work_tree =get_git_work_tree ();git_config_set ("core.bare" ,"false" );/* allow template config file to override the default */ -if (log_all_ref_updates ==-1 ) +if (log_all_ref_updates ==LOG_REFS_UNSET )git_config_set ("core.logallrefupdates" ,"true" );if (needs_work_tree_config (original_git_dir, work_tree))git_config_set ("core.worktree" , work_tree);
cache.h +8 -1
@@ -660,7 +660,6 @@ extern int minimum_abbrev, default_abbrev;extern int ignore_case;extern int assume_unchanged;extern int prefer_symlink_refs; -extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int warn_on_object_refname_ambiguity;extern const char *apply_default_whitespace;
@@ -728,6 +727,14 @@ enum hide_dotfiles_type { };extern enum hide_dotfiles_type hide_dotfiles; +enum log_refs_config { +LOG_REFS_UNSET =-1 , +LOG_REFS_NONE =0 , +LOG_REFS_NORMAL , +LOG_REFS_ALWAYS +}; +extern enum log_refs_config log_all_ref_updates; +enum branch_track {BRANCH_TRACK_UNSPECIFIED =-1 ,BRANCH_TRACK_NEVER =0 ,
config.c +6 -1
@@ -826,7 +826,12 @@ static int git_default_core_config(const char *var, const char *value) }if (!strcmp (var,"core.logallrefupdates" )) { - log_all_ref_updates =git_config_bool (var, value); +if (value && !strcasecmp (value,"always" )) + log_all_ref_updates =LOG_REFS_ALWAYS ; +else if (git_config_bool (var, value)) + log_all_ref_updates =LOG_REFS_NORMAL ; +else + log_all_ref_updates =LOG_REFS_NONE ;return 0 ; }
environment.c +1 -1
@@ -21,7 +21,6 @@ int ignore_case;int assume_unchanged;int prefer_symlink_refs;int is_bare_repository_cfg =-1 ;/* unspecified */ -int log_all_ref_updates =-1 ;/* unspecified */ int warn_ambiguous_refs =1 ;int warn_on_object_refname_ambiguity =1 ;int ref_paranoia =-1 ;
@@ -64,6 +63,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ unsigned long pack_size_limit_cfg;enum hide_dotfiles_type hide_dotfiles =HIDE_DOTFILES_DOTGITONLY ; +enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;#ifndef PROTECT_HFS_DEFAULT #define PROTECT_HFS_DEFAULT 0
refs.c +10 -5
@@ -638,12 +638,17 @@ int copy_reflog_msg(char *buf, const char *msg)int should_autocreate_reflog (const char *refname) { -if (!log_all_ref_updates) +switch (log_all_ref_updates) { +case LOG_REFS_ALWAYS : +return 1 ; +case LOG_REFS_NORMAL : +return starts_with (refname,"refs/heads/" ) || +starts_with (refname,"refs/remotes/" ) || +starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" ); +default :return 0 ; -return starts_with (refname,"refs/heads/" ) || -starts_with (refname,"refs/remotes/" ) || -starts_with (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ); + } }int is_branch (const char *refname)
refs.h +2 -0
@@ -64,6 +64,8 @@ int read_ref(const char *refname, unsigned char *sha1);int ref_exists (const char *refname); +int should_autocreate_reflog (const char *refname); +int is_branch (const char *refname);extern int refs_init_db (struct strbuf *err);
refs/files-backend.c +3 -3
@@ -2682,7 +2682,7 @@ static int files_rename_ref(struct ref_store *ref_store, } flag = log_all_ref_updates; - log_all_ref_updates =0 ; + log_all_ref_updates =LOG_REFS_NONE ;if (write_ref_to_lockfile (lock, orig_sha1, &err) ||commit_ref_update (refs, lock, orig_sha1,NULL , &err)) {error ("unable to write current sha1 into %s: %s" , oldrefname, err.buf);
@@ -2835,8 +2835,8 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, {int logfd, result, oflags =O_APPEND |O_WRONLY ; -if (log_all_ref_updates <0 ) - log_all_ref_updates = !is_bare_repository (); +if (log_all_ref_updates ==LOG_REFS_UNSET ) + log_all_ref_updates =is_bare_repository () ?LOG_REFS_NONE :LOG_REFS_NORMAL ; result =log_ref_setup (refname, logfile, err, flags &REF_FORCE_CREATE_REFLOG );
refs/refs-internal.h +0 -2
@@ -133,8 +133,6 @@ int verify_refname_available(const char *newname,*/ int copy_reflog_msg (char *buf,const char *msg); -int should_autocreate_reflog (const char *refname); -/** * Information needed for a single ref update. Set new_sha1 to the new * value or to null_sha1 to delete the ref. To check the old value
t/t1400-update-ref.sh +37 -0
@@ -93,6 +93,42 @@ test_expect_success 'update-ref creates reflogs with --create-reflog' ' git reflog exists $outside ' +test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' ' + test_config core.logAllRefUpdates true && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + test_must_fail git reflog exists $outside +' + +test_expect_success 'core.logAllRefUpdates=always creates reflog by default' ' + test_config core.logAllRefUpdates always && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + git reflog exists $outside +' + +test_expect_success 'core.logAllRefUpdates=always creates no reflog for ORIG_HEAD' ' + test_config core.logAllRefUpdates always && + git update-ref ORIG_HEAD $A && + test_must_fail git reflog exists ORIG_HEAD +' + +test_expect_success '--no-create-reflog overrides core.logAllRefUpdates=always' ' + test_config core.logAllRefUpdates true && + test_when_finished "git update-ref -d $outside" && + git update-ref --no-create-reflog $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + test_must_fail git reflog exists $outside +' + test_expect_success \ "create $m (by HEAD)" \ "git update-ref HEAD $A &&
@@ -501,6 +537,7 @@ test_expect_success 'stdin does not create reflogs by default' ' ' test_expect_success 'stdin creates reflogs with --create-reflog' ' + test_when_finished "git update-ref -d $outside" && echo "create $outside $m" >stdin && git update-ref --create-reflog --stdin <stdin && git rev-parse $m >expect &&
Signature · 1 line
2.10.2
PATCH v2 3/3 update-ref: add test cases for bare repository
In reply to [email protected]From: Cornelius Weig <[email protected]>
The default behavior of update-ref to create reflogs differs in repositories with worktree and bare ones. The existing tests cover only the behavior of repositories with worktree.
This commit adds tests that assert the correct behavior in bare repositories for update-ref. Two cases are covered:
- If core.logAllRefUpdates is not set, no reflogs should be created
- If core.logAllRefUpdates is true, reflogs should be created
--- t/t1400-update-ref.sh | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-)
t/t1400-update-ref.sh +36 -7
@@ -8,23 +8,33 @@ test_description='Test git update-ref and basic ref logging' Z=$_z40 -test_expect_success setup ' +m=refs/heads/master +n_dir=refs/heads/gu +n=$n_dir/fixes +outside=refs/foo +bare=bare-repo +create_test_objects () +{ + local T, sha1, prfx="$1" for name in A B C D E F do test_tick && T=$(git write-tree) && sha1=$(echo $name | git commit-tree $T) && - eval $name=$sha1 + eval $prfx$name=$sha1 done +} +test_expect_success setup ' + create_test_objects "" && + mkdir $bare && + cd $bare && + git init --bare && + create_test_objects "bare" && + cd - ' -m=refs/heads/master -n_dir=refs/heads/gu -n=$n_dir/fixes -outside=refs/foo - test_expect_success \ "create $m" \ "git update-ref $m $A &&
@@ -93,6 +103,25 @@ test_expect_success 'update-ref creates reflogs with --create-reflog' ' git reflog exists $outside ' +test_expect_success 'creates no reflog in bare repository' ' + git -C $bare update-ref $m $bareA && + git -C $bare rev-parse $bareA >expect && + git -C $bare rev-parse $m >actual && + test_cmp expect actual && + test_must_fail git -C $bare reflog exists $m +' + +test_expect_success 'core.logAllRefUpdates=true creates reflog in bare repository' ' + test_when_finished "git -C $bare config --unset core.logAllRefUpdates && \ + rm $bare/logs/$m" && + git -C $bare config core.logAllRefUpdates true && + git -C $bare update-ref $m $bareB && + git -C $bare rev-parse $bareB >expect && + git -C $bare rev-parse $m >actual && + test_cmp expect actual && + git -C $bare reflog exists $m +' + test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' ' test_config core.logAllRefUpdates true && test_when_finished "git update-ref -d $outside" &&
Signature · 1 line
2.10.2
PATCH v2 1/3 config: add markup to core.logAllRefUpdates doc
In reply to Jeff KingFrom: Cornelius Weig <[email protected]>
---
Notes:
As suggested, I moved the modification of the markup to its own commit.
Documentation/config.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
Documentation/config.txt +3 -2
@@ -517,10 +517,11 @@ core.logAllRefUpdates:: "`$GIT_DIR/logs/<ref>`", by appending the new and old SHA-1, the date/time and the reason of the update, but only when the file exists. If this configuration - variable is set to true, missing "`$GIT_DIR/logs/<ref>`" + variable is set to `true`, missing "`$GIT_DIR/logs/<ref>`" file is automatically created for branch heads (i.e. under refs/heads/), remote refs (i.e. under refs/remotes/), - note refs (i.e. under refs/notes/), and the symbolic ref HEAD. + `refs/heads/`), remote refs (i.e. under `refs/remotes/`), + note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + This information can be used to determine what commit was the tip of a branch "2 days ago".
Signature · 1 line
2.10.2
Re: [PATCH v2 1/3] config: add markup to core.logAllRefUpdates doc
Quoted 16 lines · [email protected] writes:
From: Cornelius Weig <[email protected]>
Signed-off-by Cornelius Weig <[email protected]>---
Notes:
As suggested, I moved the modification of the markup to its own commit.Documentation/config.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)Documentation/config.txt +0
-0
Documentation/config.txt core.logAllRefUpdates:: · line 524
Quoted 10 lines
Documentation/config.txt +3
-2@@ -517,10 +517,11 @@ core.logAllRefUpdates:: "`$GIT_DIR/logs/<ref>`", by appending the new and old SHA-1, the date/time and the reason of the update, but only when the file exists. If this configuration - variable is set to true, missing "`$GIT_DIR/logs/<ref>`" + variable is set to `true`, missing "`$GIT_DIR/logs/<ref>`" file is automatically created for branch heads (i.e. under refs/heads/), remote refs (i.e. under refs/remotes/), - note refs (i.e. under refs/notes/), and the symbolic ref HEAD. + `refs/heads/`), remote refs (i.e. under `refs/remotes/`), + note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`.
This is a peculiar patch.
Did you hand edit and lose a leading '-' from one of the lines by accident, or something?
Re: [PATCH v2 2/3] refs: add option core.logAllRefUpdates = always
In reply to [email protected]Quoted 5 lines · [email protected] writes:
From: Cornelius Weig <[email protected]>
When core.logallrefupdates is true, we only create a new reflog for refs that are under certain well-known hierarchies. The reason is that we know that some hierarchies (like refs/tags) do not typically change, and
s/do not typically/are not meant to/;
Quoted 4 lines
that unknown hierarchies might not want reflogs at all (e.g., a hypothetical refs/foo might be meant to change often and drop old history immediately).
Quoted 8 lines
However, sometimes it is useful to override this decision and simply log for all refs, because the safety and audit trail is more important than the performance implications of keeping the log around.
This patch introduces a new "always" mode for the core.logallrefupdates option which will log updates to everything under refs/, regardless where in the hierarchy it is (we still will not log things like
ORIG_HEADandFETCH_HEAD, which are known to be transient).
OK.
Documentation/config.txt core.logAllRefUpdates:: · line 523
Quoted 7 lines
Documentation/config.txt +0
-0@@ -522,6 +522,8 @@ core.logAllRefUpdates:: refs/heads/), remote refs (i.e. under refs/remotes/), `refs/heads/`), remote refs (i.e. under `refs/remotes/`),
Ahh, the answer to my question on 1/3 is "no, the commit that the patch was taken out of was already wrong, still having the old line in front of its rewrite".
Documentation/config.txt core.logAllRefUpdates:: · line 527
Quoted 4 lines
Documentation/config.txt +2
-0note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + If it is set to `always`, then a missing reflog is automatically + created for any ref under `refs/`. +
OK.
Documentation/git-tag.txt This option is only applicable when listing tags without annotation lines. · line 154
Quoted 11 lines
Documentation/git-tag.txt +2
-1@@ -150,7 +150,8 @@ This option is only applicable when listing tags without annotation lines. 'strip' removes both whitespace and commentary. --create-reflog:: - Create a reflog for the tag. + Create a reflog for the tag. To globally enable reflogs for tags, see + `core.logAllRefUpdates` in linkgit:git-config[1].
OK.
builtin/checkout.c static void update_refs_for_switch(const struct checkout_opts *opts, · line 615
Quoted 10 lines
builtin/checkout.c +1
-1@@ -612,7 +612,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -if (opts->new_branch_log && !log_all_ref_updates) { +if (opts->new_branch_log &&should_autocreate_reflog ("refs/heads/" )) {
This is inviting a maintenance nightmare. The helper function is defined to take the final refname, not a leading directory name. That is why you named the parameter "refname" in your patch like this:
--- a/refs.h
+++ b/refs.h
@@ -64,6 +64,8 @@ int read_ref(const char *refname, unsigned char *sha1);
int ref_exists(const char *refname);
+int should_autocreate_reflog(const char *refname);
+
int is_branch(const char *refname);The callers are not supposed to know that its current implementation happens to only use the leading prefix. When the definition of this helper function is changed (e.g. imagine a future where this "log.allrefupdate" is further enhanced to take glob patterns to match the refname against), this may break and nobody would notice for a few weeks, and we will get a regression report after a release is made.
Don't we have the refname for the branch already in this codepath?
Quoted 17 lines
t/t1400-update-ref.sh +9
-0@@ -93,6 +93,42 @@ test_expect_success 'update-ref creates reflogs with --create-reflog' ' git reflog exists $outside ' +test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' ' + test_config core.logAllRefUpdates true && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + test_must_fail git reflog exists $outside +'
t/t1400-update-ref.sh test_expect_success 'update-ref creates reflogs with --create-reflog' ' · line 114
Quoted 10 lines
t/t1400-update-ref.sh +10
-0+ +test_expect_success 'core.logAllRefUpdates=always creates reflog by default' ' + test_config core.logAllRefUpdates always && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + git reflog exists $outside +'
You might want to add two tests for your original motivation, i.e.
test_config core.logAllRefUpdates always && git tag a-tag && git reflog exists refs/tags/a-tag
and the other one that does not give reflog for a tag.
Other than that, looks good to me.
Re: [PATCH v2 3/3] update-ref: add test cases for bare repository
In reply to [email protected]Quoted 25 lines · [email protected] writes:
From: Cornelius Weig <[email protected]>
The default behavior of update-ref to create reflogs differs in repositories with worktree and bare ones. The existing tests cover only the behavior of repositories with worktree.
This commit adds tests that assert the correct behavior in bare repositories for update-ref. Two cases are covered:
- If core.logAllRefUpdates is not set, no reflogs should be created
- If core.logAllRefUpdates is true, reflogs should be createdSigned-off-by Cornelius Weig <[email protected]>--- t/t1400-update-ref.sh | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-)t/t1400-update-ref.sh +0
-0@@ -8,23 +8,33 @@ test_description='Test git update-ref and basic ref logging' Z=$_z40
t/t1400-update-ref.sh test_description='Test git update-ref and basic ref logging' · line 19
Quoted 10 lines
t/t1400-update-ref.sh +8
-1-test_expect_success setup ' +m=refs/heads/master +n_dir=refs/heads/gu +n=$n_dir/fixes +outside=refs/foo +bare=bare-repo +create_test_objects () +{ + local T, sha1, prfx="$1"
CodingGuidelines. Do not use bash-ism "local" (besides, I do not think you want to have comma here).
Quoted 9 lines
t/t1400-update-ref.sh +2
-1for name in A B C D E F do test_tick && T=$(git write-tree) && sha1=$(echo $name | git commit-tree $T) && - eval $name=$sha1 + eval $prfx$name=$sha1 done +}
PATCH v3 3/3 update-ref: add test cases for bare repository
In reply to [email protected]From: Cornelius Weig <[email protected]>
The default behavior of update-ref to create reflogs differs in repositories with worktree and bare ones. The existing tests cover only the behavior of repositories with worktree.
This commit adds tests that assert the correct behavior in bare repositories for update-ref. Two cases are covered:
- If core.logAllRefUpdates is not set, no reflogs should be created
- If core.logAllRefUpdates is true, reflogs should be created
---
Notes:
Changes wrt v2:
Remove bashism 'local' from test function
t/t1400-update-ref.sh | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-)
t/t1400-update-ref.sh +36 -7
@@ -8,23 +8,33 @@ test_description='Test git update-ref and basic ref logging' Z=$_z40 -test_expect_success setup ' +m=refs/heads/master +n_dir=refs/heads/gu +n=$n_dir/fixes +outside=refs/foo +bare=bare-repo +create_test_commits () +{ + prfx="$1" for name in A B C D E F do test_tick && T=$(git write-tree) && sha1=$(echo $name | git commit-tree $T) && - eval $name=$sha1 + eval $prfx$name=$sha1 done +} +test_expect_success setup ' + create_test_commits "" && + mkdir $bare && + cd $bare && + git init --bare && + create_test_commits "bare" && + cd - ' -m=refs/heads/master -n_dir=refs/heads/gu -n=$n_dir/fixes -outside=refs/foo - test_expect_success \ "create $m" \ "git update-ref $m $A &&
@@ -93,6 +103,25 @@ test_expect_success 'update-ref creates reflogs with --create-reflog' ' git reflog exists $outside ' +test_expect_success 'creates no reflog in bare repository' ' + git -C $bare update-ref $m $bareA && + git -C $bare rev-parse $bareA >expect && + git -C $bare rev-parse $m >actual && + test_cmp expect actual && + test_must_fail git -C $bare reflog exists $m +' + +test_expect_success 'core.logAllRefUpdates=true creates reflog in bare repository' ' + test_when_finished "git -C $bare config --unset core.logAllRefUpdates && \ + rm $bare/logs/$m" && + git -C $bare config core.logAllRefUpdates true && + git -C $bare update-ref $m $bareB && + git -C $bare rev-parse $bareB >expect && + git -C $bare rev-parse $m >actual && + test_cmp expect actual && + git -C $bare reflog exists $m +' + test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' ' test_config core.logAllRefUpdates true && test_when_finished "git update-ref -d $outside" &&
Signature · 1 line
2.10.2
PATCH v3 1/3 config: add markup to core.logAllRefUpdates doc
In reply to Junio C HamanoFrom: Cornelius Weig <[email protected]>
---
Notes:
Changes wrt v2:
Remove duplicated line.
Documentation/config.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/config.txt +3 -3
@@ -517,10 +517,10 @@ core.logAllRefUpdates:: "`$GIT_DIR/logs/<ref>`", by appending the new and old SHA-1, the date/time and the reason of the update, but only when the file exists. If this configuration - variable is set to true, missing "`$GIT_DIR/logs/<ref>`" + variable is set to `true`, missing "`$GIT_DIR/logs/<ref>`" file is automatically created for branch heads (i.e. under - refs/heads/), remote refs (i.e. under refs/remotes/), - note refs (i.e. under refs/notes/), and the symbolic ref HEAD. + `refs/heads/`), remote refs (i.e. under `refs/remotes/`), + note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + This information can be used to determine what commit was the tip of a branch "2 days ago".
Signature · 1 line
2.10.2
PATCH v3 2/3 refs: add option core.logAllRefUpdates = always
From: Cornelius Weig <[email protected]>
When core.logallrefupdates is true, we only create a new reflog for refs that are under certain well-known hierarchies. The reason is that we know that some hierarchies (like refs/tags) are not meant to change, and that unknown hierarchies might not want reflogs at all (e.g., a hypothetical refs/foo might be meant to change often and drop old history immediately).
However, sometimes it is useful to override this decision and simply log for all refs, because the safety and audit trail is more important than the performance implications of keeping the log around.
This patch introduces a new "always" mode for the core.logallrefupdates option which will log updates to everything under refs/, regardless where in the hierarchy it is (we still will not log things like ORIG_HEAD and FETCH_HEAD, which are known to be transient).
---
Notes:
Changes wrt v2:
- change wording in commit message s/do not typically/are not meant to/;
- in update_refs_for_switch move refname to the enclosing block, so that should_autocreate_reflog has access. Thanks Junio for spotting this potential bug early :)
- add test that asserts reflogs are created for tags if logAllRefUpdates=always. The case with logAllRefUpdates=true is IMHO already covered by the default case. To make that clearer, I explicitly added logAllRefUpdates=true.
When writing the test for git-tag, I realized that the option --no-create-reflog to git-tag does not take precedence over logAllRefUpdate=always. IOW the setting cannot be overridden on the command line. Do you think this is a defect or would it not be desirable to have this feature anyway?
Documentation/config.txt | 2 ++ Documentation/git-tag.txt | 3 ++- branch.c | 2 +- builtin/checkout.c | 7 +++---- builtin/init-db.c | 2 +- cache.h | 9 ++++++++- config.c | 7 ++++++- environment.c | 2 +- refs.c | 15 ++++++++++----- refs.h | 2 ++ refs/files-backend.c | 6 +++--- refs/refs-internal.h | 2 -- t/t1400-update-ref.sh | 37 +++++++++++++++++++++++++++++++++++++ t/t7004-tag.sh | 8 ++++++++ 14 files changed, 84 insertions(+), 20 deletions(-)
Documentation/config.txt +2 -0
@@ -521,6 +521,8 @@ core.logAllRefUpdates:: file is automatically created for branch heads (i.e. under `refs/heads/`), remote refs (i.e. under `refs/remotes/`), note refs (i.e. under `refs/notes/`), and the symbolic ref `HEAD`. + If it is set to `always`, then a missing reflog is automatically + created for any ref under `refs/`. + This information can be used to determine what commit was the tip of a branch "2 days ago".
Documentation/git-tag.txt +2 -1
@@ -150,7 +150,8 @@ This option is only applicable when listing tags without annotation lines. 'strip' removes both whitespace and commentary. --create-reflog:: - Create a reflog for the tag. + Create a reflog for the tag. To globally enable reflogs for tags, see + `core.logAllRefUpdates` in linkgit:git-config[1]. <tagname>:: The name of the tag to create, delete, or describe.
branch.c +1 -1
@@ -298,7 +298,7 @@ void create_branch(const char *name, const char *start_name, start_name);if (reflog) - log_all_ref_updates =1 ; + log_all_ref_updates =LOG_REFS_NORMAL ;if (!dont_change_ref) {struct ref_transaction *transaction;
builtin/checkout.c +3 -4
@@ -612,14 +612,12 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -if (opts->new_branch_log && !log_all_ref_updates) { +const char *refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); +if (opts->new_branch_log &&should_autocreate_reflog (refname)) {int ret; -char *refname;struct strbuf err =STRBUF_INIT ; - refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); ret =safe_create_reflog (refname,1 , &err); -free (refname);if (ret) {fprintf (stderr,_ ("Can not do reflog for '%s': %s\n" ), opts->new_orphan_branch, err.buf);
@@ -628,6 +626,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts, }strbuf_release (&err); } +free (refname); }else create_branch(opts->new_branch, new->name,
builtin/init-db.c +1 -1
@@ -262,7 +262,7 @@ static int create_default_files(const char *template_path,const char *work_tree =get_git_work_tree ();git_config_set ("core.bare" ,"false" );/* allow template config file to override the default */ -if (log_all_ref_updates ==-1 ) +if (log_all_ref_updates ==LOG_REFS_UNSET )git_config_set ("core.logallrefupdates" ,"true" );if (needs_work_tree_config (original_git_dir, work_tree))git_config_set ("core.worktree" , work_tree);
cache.h +8 -1
@@ -660,7 +660,6 @@ extern int minimum_abbrev, default_abbrev;extern int ignore_case;extern int assume_unchanged;extern int prefer_symlink_refs; -extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int warn_on_object_refname_ambiguity;extern const char *apply_default_whitespace;
@@ -728,6 +727,14 @@ enum hide_dotfiles_type { };extern enum hide_dotfiles_type hide_dotfiles; +enum log_refs_config { +LOG_REFS_UNSET =-1 , +LOG_REFS_NONE =0 , +LOG_REFS_NORMAL , +LOG_REFS_ALWAYS +}; +extern enum log_refs_config log_all_ref_updates; +enum branch_track {BRANCH_TRACK_UNSPECIFIED =-1 ,BRANCH_TRACK_NEVER =0 ,
config.c +6 -1
@@ -826,7 +826,12 @@ static int git_default_core_config(const char *var, const char *value) }if (!strcmp (var,"core.logallrefupdates" )) { - log_all_ref_updates =git_config_bool (var, value); +if (value && !strcasecmp (value,"always" )) + log_all_ref_updates =LOG_REFS_ALWAYS ; +else if (git_config_bool (var, value)) + log_all_ref_updates =LOG_REFS_NORMAL ; +else + log_all_ref_updates =LOG_REFS_NONE ;return 0 ; }
environment.c +1 -1
@@ -21,7 +21,6 @@ int ignore_case;int assume_unchanged;int prefer_symlink_refs;int is_bare_repository_cfg =-1 ;/* unspecified */ -int log_all_ref_updates =-1 ;/* unspecified */ int warn_ambiguous_refs =1 ;int warn_on_object_refname_ambiguity =1 ;int ref_paranoia =-1 ;
@@ -64,6 +63,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ unsigned long pack_size_limit_cfg;enum hide_dotfiles_type hide_dotfiles =HIDE_DOTFILES_DOTGITONLY ; +enum log_refs_config log_all_ref_updates =LOG_REFS_UNSET ;#ifndef PROTECT_HFS_DEFAULT #define PROTECT_HFS_DEFAULT 0
refs.c +10 -5
@@ -638,12 +638,17 @@ int copy_reflog_msg(char *buf, const char *msg)int should_autocreate_reflog (const char *refname) { -if (!log_all_ref_updates) +switch (log_all_ref_updates) { +case LOG_REFS_ALWAYS : +return 1 ; +case LOG_REFS_NORMAL : +return starts_with (refname,"refs/heads/" ) || +starts_with (refname,"refs/remotes/" ) || +starts_with (refname,"refs/notes/" ) || + !strcmp (refname,"HEAD" ); +default :return 0 ; -return starts_with (refname,"refs/heads/" ) || -starts_with (refname,"refs/remotes/" ) || -starts_with (refname,"refs/notes/" ) || - !strcmp (refname,"HEAD" ); + } }int is_branch (const char *refname)
refs.h +2 -0
@@ -64,6 +64,8 @@ int read_ref(const char *refname, unsigned char *sha1);int ref_exists (const char *refname); +int should_autocreate_reflog (const char *refname); +int is_branch (const char *refname);extern int refs_init_db (struct strbuf *err);
refs/files-backend.c +3 -3
@@ -2682,7 +2682,7 @@ static int files_rename_ref(struct ref_store *ref_store, } flag = log_all_ref_updates; - log_all_ref_updates =0 ; + log_all_ref_updates =LOG_REFS_NONE ;if (write_ref_to_lockfile (lock, orig_sha1, &err) ||commit_ref_update (refs, lock, orig_sha1,NULL , &err)) {error ("unable to write current sha1 into %s: %s" , oldrefname, err.buf);
@@ -2835,8 +2835,8 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, {int logfd, result, oflags =O_APPEND |O_WRONLY ; -if (log_all_ref_updates <0 ) - log_all_ref_updates = !is_bare_repository (); +if (log_all_ref_updates ==LOG_REFS_UNSET ) + log_all_ref_updates =is_bare_repository () ?LOG_REFS_NONE :LOG_REFS_NORMAL ; result =log_ref_setup (refname, logfile, err, flags &REF_FORCE_CREATE_REFLOG );
refs/refs-internal.h +0 -2
@@ -133,8 +133,6 @@ int verify_refname_available(const char *newname,*/ int copy_reflog_msg (char *buf,const char *msg); -int should_autocreate_reflog (const char *refname); -/** * Information needed for a single ref update. Set new_sha1 to the new * value or to null_sha1 to delete the ref. To check the old value
t/t1400-update-ref.sh +37 -0
@@ -93,6 +93,42 @@ test_expect_success 'update-ref creates reflogs with --create-reflog' ' git reflog exists $outside ' +test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' ' + test_config core.logAllRefUpdates true && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + test_must_fail git reflog exists $outside +' + +test_expect_success 'core.logAllRefUpdates=always creates reflog by default' ' + test_config core.logAllRefUpdates always && + test_when_finished "git update-ref -d $outside" && + git update-ref $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + git reflog exists $outside +' + +test_expect_success 'core.logAllRefUpdates=always creates no reflog for ORIG_HEAD' ' + test_config core.logAllRefUpdates always && + git update-ref ORIG_HEAD $A && + test_must_fail git reflog exists ORIG_HEAD +' + +test_expect_success '--no-create-reflog overrides core.logAllRefUpdates=always' ' + test_config core.logAllRefUpdates true && + test_when_finished "git update-ref -d $outside" && + git update-ref --no-create-reflog $outside $A && + git rev-parse $A >expect && + git rev-parse $outside >actual && + test_cmp expect actual && + test_must_fail git reflog exists $outside +' + test_expect_success \ "create $m (by HEAD)" \ "git update-ref HEAD $A &&
@@ -501,6 +537,7 @@ test_expect_success 'stdin does not create reflogs by default' ' ' test_expect_success 'stdin creates reflogs with --create-reflog' ' + test_when_finished "git update-ref -d $outside" && echo "create $outside $m" >stdin && git update-ref --create-reflog --stdin <stdin && git rev-parse $m >expect &&
t/t7004-tag.sh +8 -0
@@ -71,6 +71,7 @@ test_expect_success 'creating a tag for an unknown revision should fail' ' # commit used in the tests, test_tick is also called here to freeze the date: test_expect_success 'creating a tag using default HEAD should succeed' ' + test_config core.logAllRefUpdates true && test_tick && echo foo >foo && git add foo &&
@@ -90,6 +91,13 @@ test_expect_success '--create-reflog does not create reflog on failure' ' test_must_fail git reflog exists refs/tags/mytag ' +test_expect_success 'option core.logAllRefUpdates=always creates reflog' ' + test_when_finished "git tag -d tag_with_reflog" && + test_config core.logAllRefUpdates always && + git tag tag_with_reflog && + git reflog exists refs/tags/tag_with_reflog +' + test_expect_success 'listing all tags if one exists should succeed' ' git tag -l && git tag
Signature · 1 line
2.10.2
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
Quoted 11 lines · [email protected] writes:
Notes:
Changes wrt v2:- change wording in commit message s/do not typically/are not meant to/;
- inupdate_refs_for_switchmove refname to the enclosing block, so thatshould_autocreate_refloghas access. Thanks Junio for spotting this potential bug early :)
- add test that asserts reflogs are created for tags if logAllRefUpdates=always. The case with logAllRefUpdates=true is IMHO already covered by the default case. To make that clearer, I explicitly added logAllRefUpdates=true.
These look all sensible. Especially thanks for reordering the code to feed the real refname for the new branch in the "checkout" codepath.
Quoted 5 lines
When writing the test for git-tag, I realized that the option --no-create-reflog to git-tag does not take precedence over logAllRefUpdate=always. IOW the setting cannot be overridden on the command line. Do you think this is a defect or would it not be desirable to have this feature anyway?
"--no-create-reflog" should override the configuration set to "true" or "always". Also "--create-reflog" should override the configuration set to "false".
If the problem was inherited from the original code before your change (e.g. you set logAllRefUpdates to true and then did "update-ref --no-create-reflog refs/heads/foo". Does the code before your change ignore the command lne option and create a reflog for the branch?), then it would be ideal to fix the bug before this series as a preparatory fix. If the problem was introduced by this patch set, then we would need a fix not to introduce it ;-)
Quoted 11 lines
builtin/checkout.c +2
-1@@ -612,14 +612,12 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -if (opts->new_branch_log && !log_all_ref_updates) { +const char *refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); +if (opts->new_branch_log &&should_autocreate_reflog (refname)) {
builtin/checkout.c static void update_refs_for_switch(const struct checkout_opts *opts, · line 623
Quoted 10 lines
builtin/checkout.c +0
-3int ret; -char *refname;struct strbuf err =STRBUF_INIT ; - refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); ret =safe_create_reflog (refname,1 , &err); -free (refname);if (ret) {fprintf (stderr,_ ("Can not do reflog for '%s': %s\n" ), opts->new_orphan_branch, err.buf);
Here you need to have another free(), as this block makes an early return and you end up leaking refname.
t/t7004-tag.sh test_expect_success 'creating a tag for an unknown revision should fail' ' · line 77
Quoted 12 lines
t/t7004-tag.sh +1
-0@@ -71,6 +71,7 @@ test_expect_success 'creating a tag for an unknown revision should fail' ' # commit used in the tests, test_tick is also called here to freeze the date: test_expect_success 'creating a tag using default HEAD should succeed' ' + test_config core.logAllRefUpdates true && test_tick && echo foo >foo && git add foo &&
This change is to make sure that 'true' does not affect tags (but 'always' does as seen in the later new test)? I am just double checking, not objecting.
Thanks.
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
Quoted 9 lines · Junio C Hamano
Quoted 9 lines
builtin/checkout.c +0
-1@@ -612,14 +612,12 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -if (opts->new_branch_log && !log_all_ref_updates) {
Quoted 8 lines
builtin/checkout.c static void update_refs_for_switch(const struct checkout_opts *opts,
Quoted 5 lines
builtin/checkout.c +1
-0+const char *refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch);if (ret) {fprintf (stderr,_ ("Can not do reflog for '%s': %s\n" ), opts->new_orphan_branch, err.buf);Here you need to have another
free(), as this block makes an early return and you end up leaking refname.
I am building with the attached patch squashed on top.
The extra free(refname) is to plug the leak I pointed out, and the type of refname is no longer const, because "const char *" cannot be free()d without casting, and in this codepath I do not see a reason to mark it as const.
When queued on top of 4e59582ff7 ("Seventh batch for 2.12", 2017-01-23), however, this fails t2017#9 (orphan with -l makes reflog when core.logAllRefUpdates = false).
builtin/checkout.c +4 -1
@@ -612,7 +612,9 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -const char *refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); +char *refname; + + refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch);if (opts->new_branch_log &&should_autocreate_reflog (refname)) {int ret;struct strbuf err =STRBUF_INIT ;
@@ -622,6 +624,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,fprintf (stderr,_ ("Can not do reflog for '%s': %s\n" ), opts->new_orphan_branch, err.buf);strbuf_release (&err); +free (refname);return ; }strbuf_release (&err);
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
In reply to Junio C HamanoQuoted 10 lines · Junio C Hamano
Quoted 5 lines
When writing the test for git-tag, I realized that the option --no-create-reflog to git-tag does not take precedence over logAllRefUpdate=always. IOW the setting cannot be overridden on the command line. Do you think this is a defect or would it not be desirable to have this feature anyway?
"--no-create-reflog" should override the configuration set to "true" or "always". Also "--create-reflog" should override the configuration set to "false".
Quoted 7 lines
If the problem was inherited from the original code before your change (e.g. you set logAllRefUpdates to true and then did "update-ref --no-create-reflog refs/heads/foo". Does the code before your change ignore the command lne option and create a reflog for the branch?), then it would be ideal to fix the bug before this series as a preparatory fix. If the problem was introduced by this patch set, then we would need a fix not to introduce it ;-)
I hadn't thought about that. I think "git branch --no-create-reflog" has the same problem in the existing code.
I suspect nobody cares much in practice. Even if you say "don't create a reflog now", if you have core.logAllRefUpdates turned on, then it's likely that some _other_ operation will create the reflog later accidentally (e.g., as soon as you "git checkout foo && git commit", you'll get a reflog). I think you're fighting an uphill battle to turn logAllRefUpdates on and then try to disable some reflogs selectively.
So I agree the current behavior is quietly broken, which is not good. But I wonder if "--no-create-reflog" is really sane in the first place, and whether we might be better off to simply disallow it.
-Peff
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
In reply to Junio C HamanoHi,
Quoted 4 lines
The extra free(refname) is to plug the leak I pointed out, and the type of refname is no longer const, because "const char *" cannot be free()d without casting, and in this codepath I do not see a reason to mark it as const.
Ooops.. thanks for not yelling at me for that :-/
Quoted 3 lines
When queued on top of
4e59582ff7("Seventh batch for 2.12", 2017-01-23), however, this fails t2017#9 (orphan with -l makes reflog when core.logAllRefUpdates = false).
And again, thanks for not yelling. I overlooked that the "should_autocreate_reflog" return value should have been negated as shown below. Should I resend this patch, or is it easier for you to do the change yourself?
Interdiff v2..v3:
builtin/checkout.c +5 -2
@@ -612,8 +612,10 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -const char *refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); -if (opts->new_branch_log &&should_autocreate_reflog (refname)) { +char *refname; + + refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); +if (opts->new_branch_log && !should_autocreate_reflog (refname)) {int ret;struct strbuf err =STRBUF_INIT ;
@@ -622,6 +624,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,fprintf (stderr,_ ("Can not do reflog for '%s': %s\n" ), opts->new_orphan_branch, err.buf);strbuf_release (&err); +free (refname);return ; }strbuf_release (&err);
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
In reply to Jeff KingQuoted 8 lines · Jeff King
Quoted 6 lines · Junio C Hamano
Quoted 5 lines
When writing the test for git-tag, I realized that the option --no-create-reflog to git-tag does not take precedence over logAllRefUpdate=always. IOW the setting cannot be overridden on the command line. Do you think this is a defect or would it not be desirable to have this feature anyway?
Quoted 7 lines
Quoted 7 lines
"--no-create-reflog" should override the configuration set to "true" or "always". Also "--create-reflog" should override the configuration set to "false".
If the problem was inherited from the original code before your change (e.g. you set logAllRefUpdates to true and then did "update-ref --no-create-reflog refs/heads/foo".
I was actually not referring to update-ref, for which the --no-create-reflog option works fine. I was referring to git-tag which also has the --create-reflog option. For git-tag, the current code does not allow to override logAllRefUpdates=always with --no-create-reflog. On the other hand logAllRefUpdates=false is overridden by "git tag --create-reflog". The reason is that the file-backend does allow to force reflog creation, but it does not allow to force reflog non-creation. I have a patch that amends this, but it's not pretty and I don't think it will be useful (see last paragraph).
Quoted 2 lines
I hadn't thought about that. I think "git branch --no-create-reflog" has the same problem in the existing code.
You are right, git-branch also ignores --no-create-reflog.
Quoted 10 lines
I suspect nobody cares much in practice. Even if you say "don't create a reflog now", if you have core.logAllRefUpdates turned on, then it's likely that some _other_ operation will create the reflog later accidentally (e.g., as soon as you "git checkout foo && git commit", you'll get a reflog). I think you're fighting an uphill battle to turn logAllRefUpdates on and then try to disable some reflogs selectively.
So I agree the current behavior is quietly broken, which is not good. But I wonder if "--no-create-reflog" is really sane in the first place, and whether we might be better off to simply disallow it.
Concerning branches, I fully agree. For git-branch, the "--no-create-reflog" option does not make sense at all and should produce an error.
On the other hand, for tags it may make sense to override logAllRefUpdates=always. As tag updates come exclusively from force-creating the same tag on another revision, a reflog will actually not be created by accident.
Nevertheless, I don't think it is very useful to have the "--no-create-reflog" argument to any of git-branch or git-tag. It only takes effect if a user has configured logAllRefUpdates=always, and he probably has done that for a reason. Given that the overhead from a reflog is minuscule, IMHO no-one will ever bother about "--no-create-reflog".
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
In reply to Cornelius WeigQuoted 3 lines · Cornelius Weig
And again, thanks for not yelling. I overlooked that the "
should_autocreate_reflog" return value should have been negated as shown below.
Heh---I AM blind. I didn't spot it even though I was staring at the code and even tweaking it (for the constness thing).
Quoted 2 lines
Should I resend this patch, or is it easier for you to do the change yourself?
I can squash it in, now we have and the list saw all the bits necessary.
Thanks for working on this.
Quoted 26 lines
Interdiff v2..v3:
builtin/checkout.c +5
-2@@ -612,8 +612,10 @@ static void update_refs_for_switch(const struct checkout_opts *opts,const char *old_desc, *reflog_msg;if (opts->new_branch) {if (opts->new_orphan_branch) { -const char *refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); -if (opts->new_branch_log &&should_autocreate_reflog (refname)) { +char *refname; + + refname =mkpathdup ("refs/heads/%s" , opts->new_orphan_branch); +if (opts->new_branch_log && !should_autocreate_reflog (refname)) {int ret;struct strbuf err =STRBUF_INIT ;@@ -622,6 +624,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,fprintf (stderr,_ ("Can not do reflog for '%s': %s\n" ), opts->new_orphan_branch, err.buf);strbuf_release (&err); +free (refname);return ; }strbuf_release (&err);
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
In reply to Jeff KingQuoted 3 lines · Jeff King
So I agree the current behavior is quietly broken, which is not good. But I wonder if "--no-create-reflog" is really sane in the first place, and whether we might be better off to simply disallow it.
Thanks for a reasoned argument and a reasonable justification. I agree with all that.
I think it is probably a good idea to document the behaviour (i.e. "--no-create" single-shot from the command line is ignored). I am not sure we should error out, though, in order to "disallow" it---a documented silent no-op may be sufficient.
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
In reply to Cornelius WeigQuoted 8 lines · Cornelius Weig
Concerning branches, I fully agree. For git-branch, the "--no-create-reflog" option does not make sense at all and should produce an error.
On the other hand, for tags it may make sense to override logAllRefUpdates=always. As tag updates come exclusively from force-creating the same tag on another revision, a reflog will actually not be created by accident.
Hmm. I think you could also see tag creation and update via "git fetch", though only with explicit refspecs, I think, not tag-following.
So I think ultimately you'd need to use "git -c logallrefupdates=false" if you want to override reflog options for all commands. A saner interface would probably be put teaching the ref code to respect a configured list of exceptions ("I do want reflogs for refs/tags/, but not for refs/foo/"). But I don't think it's sensible for anybody to go to the work of doing that, given that I haven't heard a single useful reason for --no-create-reflog in the first place.
Personally, I'd be fine with leaving it in its current state as a known bug that somebody may fix later, if they actually care. But if it is not too hard to fix while we are all thinking about it, we can do that.
-Peff
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
In reply to Junio C HamanoQuoted 4 lines · Junio C Hamano
I think it is probably a good idea to document the behaviour (i.e. "--no-create" single-shot from the command line is ignored). I am not sure we should error out, though, in order to "disallow" it---a documented silent no-op may be sufficient.
Yes, maybe abort on seeing "--no-create-reflog" is a too drastic measure. I presume that the best place to have the documentation would be to print a warning when seeing the ignored argument?
Or did you just have man pages and code comment in mind?
Cheers,
Cornelius
Re: [PATCH v3 2/3] refs: add option core.logAllRefUpdates = always
Quoted 11 lines · Cornelius Weig
Quoted 4 lines · Junio C Hamano
I think it is probably a good idea to document the behaviour (i.e. "--no-create" single-shot from the command line is ignored). I am not sure we should error out, though, in order to "disallow" it---a documented silent no-op may be sufficient.
Yes, maybe abort on seeing "--no-create-reflog" is a too drastic measure. I presume that the best place to have the documentation would be to print a warning when seeing the ignored argument?
Or did you just have man pages and code comment in mind?
I meant only in the documentation, but "you gave me a no-op option" warning would not hurt. I do not care too deeply either way.