show-ref: Allow -d, --head to work with --verify
PATCH v2 1/1 show-ref: Allow -d, --head to work with --verify
This is the second take for "[PATCH] show-ref: Allow --head to work with --verify". Thanks to Junio for his extensive feedback.
PATCH v2 1/4 show-ref: Allow --head to work with --verify
Previously, when --verify was specified, show-ref would use a separate code path which ignored --head. Thus, "git show-ref HEAD" used with "--verify" (because the user is not interested in seeing refs/remotes/origin/HEAD), and used with "--head" (because the user does not want HEAD to be filtered out), i.e. "git show-ref --head --verify HEAD", did not work as expected.
Instead of insisting that the input begins with "refs/", allow "HEAD" when "--head" is given in the codepath that handles "--verify", so that all valid full refnames including HEAD are passed to the same output machinery.
--- builtin/show-ref.c | 3 ++- t/t1403-show-ref.sh | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)
builtin/show-ref.c +2 -1
@@ -202,7 +202,8 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)while (*pattern) {struct object_id oid; -if (starts_with (*pattern,"refs/" ) && +if ((starts_with (*pattern,"refs/" ) || + (show_head && !strcmp (*pattern,"HEAD" ))) && !read_ref (*pattern, oid.hash)) {if (!quiet)show_one (*pattern, &oid);
t/t1403-show-ref.sh +14 -0
@@ -164,4 +164,18 @@ test_expect_success 'show-ref --heads, --tags, --head, pattern' ' test_cmp expect actual ' +test_expect_success 'show-ref --verify --head' ' + echo $(git rev-parse HEAD) HEAD >expect && + git show-ref --verify --head HEAD >actual && + test_cmp expect actual && + + >expect && + + git show-ref --verify --head -q HEAD >actual && + test_cmp expect actual && + + test_must_fail git show-ref --verify --head -q A >actual && + test_cmp expect actual +' + test_done
Signature · 1 line
2.11.0
PATCH v2 2/4 show-ref: Allow -d to work with --verify
In reply to Vladimir PanteleevUse the same output machinery when --verify is absent or present, which allows tag dereferencing (-d) to work with --verify. This is useful when the user wishes to avoid the costly iteration of refs.
--- builtin/show-ref.c | 3 +-- t/t1403-show-ref.sh | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-)
builtin/show-ref.c +1 -2
@@ -205,8 +205,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)if ((starts_with (*pattern,"refs/" ) || (show_head && !strcmp (*pattern,"HEAD" ))) && !read_ref (*pattern, oid.hash)) { -if (!quiet) -show_one (*pattern, &oid); +show_ref (*pattern, &oid,0 ,NULL ); }else if (!quiet)die ("'%s' - not a valid ref" , *pattern);
t/t1403-show-ref.sh +9 -0
@@ -97,6 +97,9 @@ test_expect_success 'show-ref -d' ' git show-ref -d refs/tags/A refs/tags/C >actual && test_cmp expect actual && + git show-ref --verify -d refs/tags/A refs/tags/C >actual && + test_cmp expect actual && + echo $(git rev-parse refs/heads/master) refs/heads/master >expect && git show-ref -d master >actual && test_cmp expect actual &&
@@ -116,6 +119,12 @@ test_expect_success 'show-ref -d' ' test_cmp expect actual && test_must_fail git show-ref -d --verify heads/master >actual && + test_cmp expect actual && + + test_must_fail git show-ref --verify -d A C >actual && + test_cmp expect actual && + + test_must_fail git show-ref --verify -d tags/A tags/C >actual && test_cmp expect actual '
Signature · 1 line
2.11.0
PATCH v2 3/4 show-ref: Optimize show_ref a bit
In reply to Vladimir PanteleevThe inner `if (verify)' check was not being used before the preceding commit, as show_ref was never being called from a code path where verify was non-zero.
Adding a `!verify' check to the outer if skips an unnecessary string comparison when verify is non-zero, and show_ref is already called with a reference exactly matching pattern.
--- builtin/show-ref.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
builtin/show-ref.c +1 -4
@@ -43,7 +43,7 @@ static int show_ref(const char *refname, const struct object_id *oid,if (!match)return 0 ; } -if (pattern) { +if (pattern && !verify) {int reflen =strlen (refname);const char **p = pattern, *m;while ((m = *p++) !=NULL ) {
@@ -54,9 +54,6 @@ static int show_ref(const char *refname, const struct object_id *oid,continue ;if (len == reflen) goto match; -/* "--verify" requires an exact match */ -if (verify) -continue ;if (refname[reflen - len -1 ] =='/' ) goto match; }
Signature · 1 line
2.11.0
Re: [PATCH v2 3/4] show-ref: Optimize show_ref a bit
Quoted 27 lines · Vladimir Panteleev
The inner `if (verify)' check was not being used before the preceding commit, as
show_refwas never being called from a code path where verify was non-zero.Adding a `!verify' check to the outer if skips an unnecessary string comparison when verify is non-zero, and
show_refis already called with a reference exactly matching pattern.Signed-off-by Vladimir Panteleev <[email protected]>--- builtin/show-ref.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)builtin/show-ref.c +1
-1@@ -43,7 +43,7 @@ static int show_ref(const char *refname, const struct object_id *oid,if (!match)return 0 ; } -if (pattern) { +if (pattern && !verify) {int reflen =strlen (refname);const char **p = pattern, *m;while ((m = *p++) !=NULL ) {
builtin/show-ref.c static int show_ref(const char *refname, const struct object_id *oid, · line 59
Quoted 9 lines
builtin/show-ref.c +0
-3@@ -54,9 +54,6 @@ static int show_ref(const char *refname, const struct object_id *oid,continue ;if (len == reflen) goto match; -/* "--verify" requires an exact match */ -if (verify) -continue ;if (refname[reflen - len -1 ] =='/' ) goto match; }
Having to do this change probably is an indication that the division of labour between show_ref() and show_one() up to this step needs to be rethought.
Conceptually, "git show-ref" works in two ways:
* When in --verify mode, the end user gives which refnames to consider showing.
* Otherwise the end user gives pattern and the command infers which refnames to consider showing using the pattern.
And for the refnames that are considered for showing, we may do various things, like -d to deref and --quiet to be silent. We want this actual "output" step to be the same between two modes of operation.
So a better division of labour would be:
* Make show_ref() about "using pattern, enumerate what refs to show" and call show_one().
* Update show_one() and teach _it_ to handle quiet and deref_tags.
* From cmd_show_ref(), in --verify mode, make sure the ref exists and call show_one(), because we do not do the "using pattern, enumerate what refs to show" at all.
And from that point of view, I think 4/4 is going in the wrong direction.
I also think that "git show-ref --verify HEAD" should work; it is OK that the command accepts "--head" in that case, but when in --verify mode, the end user gives which refnames to consider showing, and HEAD given from the command line is a signal enough that that psuedo-ref is what the end user wants to be shown. "--head" is about not filtering in "enumerate the ones that match the given patterns" mode, and it feels unnecessary to require it in "--verify" mode.
Thanks.
Re: [PATCH v2 3/4] show-ref: Optimize show_ref a bit
Quoted 9 lines · Junio C Hamano
Having to do this change probably is an indication that the division of labour between
show_ref()andshow_one()up to this step needs to be rethought.Conceptually, "git show-ref" works in two ways:
* When in --verify mode, the end user gives which refnames to consider showing.
Quoted 7 lines
* Otherwise the end user gives pattern and the command infers which refnames to consider showing using the pattern.
And for the refnames that are considered for showing, we may do various things, like -d to deref and --quiet to be silent. We want this actual "output" step to be the same between two modes of operation.
... also "error out if the named object did not exist" can be part of this, which means ...
Quoted 6 lines
So a better division of labour would be:
* Make
show_ref()about "using pattern, enumerate what refs to show" and callshow_one().* Update
show_one()and teach _it_ to handle quiet andderef_tags.
... "if (!has_sha1_file(oid->hash)) die()" in show_ref() would probably want to be part of this update.
PATCH v2 4/4 show-ref: Inline show_one
In reply to Vladimir PanteleevAs the small function is now only called from a single place, there is no point in keeping it as a separate function any longer.
--- builtin/show-ref.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
builtin/show-ref.c +5 -10
@@ -17,15 +17,6 @@ static int deref_tags, show_head, tags_only, heads_only, found_match, verify,static const char **pattern;static const char *exclude_existing_arg; -static void show_one (const char *refname,const struct object_id *oid) -{ -const char *hex =find_unique_abbrev (oid->hash, abbrev); -if (hash_only) -printf ("%s\n" , hex); -else -printf ("%s %s\n" , hex, refname); -} -static int show_ref (const char *refname,const struct object_id *oid,int flag,void *cbdata) {
@@ -74,7 +65,11 @@ static int show_ref(const char *refname, const struct object_id *oid,if (quiet)return 0 ; -show_one (refname, oid); + hex =find_unique_abbrev (oid->hash, abbrev); +if (hash_only) +printf ("%s\n" , hex); +else +printf ("%s %s\n" , hex, refname);if (!deref_tags)return 0 ;
Signature · 1 line
2.11.0