Skip to contentSkip to conversation

for-each-ref: `:short` format for `refname`

PATCH Superseded Bert Wesarg 4 participants 26 messages Jun 15, 2016 1 message not in the archive Documentation

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 for-each-ref: `:short` format for `refname`

This strips from the refname the common directory prefix with the matched pattern.

This is particular usefull for bash completion, to get refs without refs/heads or refs/tags.

---
 Documentation/git-for-each-ref.txt |    5 ++
 builtin-for-each-ref.c             |   74 +++++++++++++++++++++++++++++++----
 t/t6300-for-each-ref.sh            |   61 +++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 9 deletions(-)
Documentation/git-for-each-ref.txt +5 -0
index eae6c0e..deeae79 100644
@@ -74,6 +74,11 @@ For all objects, the following names can be used:
 
 refname::
 	The name of the ref (the part after $GIT_DIR/).
+	For a short name of the ref append `:short`. This will strip
+	the common directory prefix with the pattern which matches this ref.
+	I.e. for a the pattern `refs/heads` you get `master`, or for
+	`refs/tags/v1.5.[01].*` you get `v1.5.[01].*`.
+	This is particular usefull for bash completion.
 
 objecttype::
 	The type of the object (`blob`, `tree`, `commit`, `tag`).
builtin-for-each-ref.c +65 -9

105 lines not on this page · raw message

t/t6300-for-each-ref.sh +61 -0

67 lines not on this page · raw message

Signature · 1 line
tg: (445cac1..) t/for-each-ref-refshort (depends on: master)
#2 lore raw

Hi,

Documentation/git-for-each-ref.txt For all objects, the following names can be used: · line 81

Quoted 13 lines · Bert Wesarg

This strips from the refname the common directory prefix with the matched pattern.

This is particular usefull for bash completion, to get refs without refs/heads or refs/tags.

refname::

Documentation/git-for-each-ref.txt +5 -0
 	The name of the ref (the part after $GIT_DIR/).
+	For a short name of the ref append `:short`. This will strip
+	the common directory prefix with the pattern which matches this ref.
+	I.e. for a the pattern `refs/heads` you get `master`, or for
+	`refs/tags/v1.5.[01].*` you get `v1.5.[01].*`.
+	This is particular usefull for bash completion.

Should this last sentence really belong to the documentation?

Furthermore, I think ':strip' better describes what this format actually does. Even you have used the word 'strip' in the commit message and in the documentation as well.

As far as bash completion is concerned, I'm for it, as it does exactly what the completion script needs to perform better, it doesn't have those conceptual issues 'refbasename' has, and it's only a tad slower than 'refbasename'.

However, if we consider possible use cases other than bash completion, I don't know which one is more useful. For example, if you have two branches 'foo/bar' and 'foo/baz', then 'git merge $(git for-each-ref --format=%(refbasename) refs/heads/foo)' will work as expected, but 'refname:short' not, as it will output only 'bar' and 'baz' which 'git merge' can not find.

Best,
Gábor

#3 lore raw
Quoted 9 lines · SZEDER Gábor

Hi,

Quoted 5 lines · Bert Wesarg

This strips from the refname the common directory prefix with the matched pattern.

This is particular usefull for bash completion, to get refs without refs/heads or refs/tags.

Quoted 8 lines

Documentation/git-for-each-ref.txt For all objects, the following names can be used: · line 81

Quoted 7 lines

refname::
The name of the ref (the part after $GIT_DIR/).

Documentation/git-for-each-ref.txt +5 -0
+     For a short name of the ref append `:short`. This will strip
+     the common directory prefix with the pattern which matches this ref.
+     I.e. for a the pattern `refs/heads` you get `master`, or for
+     `refs/tags/v1.5.[01].*` you get `v1.5.[01].*`.
+     This is particular usefull for bash completion.

Should this last sentence really belong to the documentation?

At least it is not the only example in the documentation.

Quoted 4 lines

Furthermore, I think ':strip' better describes what this format actually does. Even you have used the word 'strip' in the commit message and in the documentation as well.

True, I'm ok with this proposal.

Quoted 7 lines

As far as bash completion is concerned, I'm for it, as it does exactly what the completion script needs to perform better, it doesn't have those conceptual issues 'refbasename' has, and it's only a tad slower than 'refbasename'.

Quoted 6 lines

However, if we consider possible use cases other than bash completion, I don't know which one is more useful. For example, if you have two branches 'foo/bar' and 'foo/baz', then 'git merge $(git for-each-ref --format=%(refbasename) refs/heads/foo)' will work as expected, but 'refname:short' not, as it will output only 'bar' and 'baz' which 'git merge' can not find.

Yeah, thats an disadvantage and I thought about this, too. But I have no particular opinion about it.

Regards,
Bert

Quoted 2 lines

Best,
Gábor

#4 lore raw
Quoted 9 lines · Bert Wesarg
Quoted 6 lines · SZEDER Gábor

However, if we consider possible use cases other than bash completion, I don't know which one is more useful. For example, if you have two branches 'foo/bar' and 'foo/baz', then 'git merge $(git for-each-ref --format=%(refbasename) refs/heads/foo)' will work as expected, but 'refname:short' not, as it will output only 'bar' and 'baz' which 'git merge' can not find.

Yeah, thats an disadvantage and I thought about this, too. But I have no particular opinion about it.

Ok, I have a new idea, which could be made all happy:

IMHO the goal of this new format for refname should be, that it can be used as an ref on the command line. This isn't given with my current :short proposal (which I call :strip as of now), as Gábor showed. What we need is the reverse of what happened with refnames given on the command line to commands like checkout/merge/... The only thing that comes near to this is this from refs.c:

    const char *ref_rev_parse_rules[] = {
            "%.*s",
            "refs/%.*s",
            "refs/tags/%.*s",
            "refs/heads/%.*s",
            "refs/remotes/%.*s",
            "refs/remotes/%.*s/HEAD",
            NULL
    };

Which doesn't look very useful, because every ref from for_each_ref would match rule one. So we probably need to try the reverse of this list. Now my knowledge from git internals is really low, I don't know if this is sane. I know that this can't be bijective but at least the bash completion would be happy with this idea.

Comments, thoughts, brown paper bags...

Thanks,
Bert

#5 lore raw
Quoted 17 lines · Bert Wesarg

IMHO the goal of this new format for refname should be, that it can be used as an ref on the command line. This isn't given with my current :short proposal (which I call :strip as of now), as Gábor showed. What we need is the reverse of what happened with refnames given on the command line to commands like checkout/merge/... The only thing that comes near to this is this from refs.c:

    const char *ref_rev_parse_rules[] = {
            "%.*s",
            "refs/%.*s",
            "refs/tags/%.*s",
            "refs/heads/%.*s",
            "refs/remotes/%.*s",
            "refs/remotes/%.*s/HEAD",
            NULL
    };
Quoted 3 lines

Which doesn't look very useful, because every ref from for_each_ref would match rule one. So we probably need to try the reverse of this list.

Yup. If you search the list backwards and extract the part of the ref that matches %.*s you'll get a name that other tools can find, and which is the shortest name possible.

You can still get ambiguous names. Avoiding them requires going through all refs and building their short forms, then using the full ref name for any ref which had more than one name shorten to the same string. Ugly, but implementable, and probably something that should be considered.

Signature · 1 line
Shawn.
#6 lore raw
Quoted 26 lines · Shawn O. Pearce
Quoted 17 lines · Bert Wesarg

IMHO the goal of this new format for refname should be, that it can be used as an ref on the command line. This isn't given with my current :short proposal (which I call :strip as of now), as Gábor showed. What we need is the reverse of what happened with refnames given on the command line to commands like checkout/merge/... The only thing that comes near to this is this from refs.c:

    const char *ref_rev_parse_rules[] = {
            "%.*s",
            "refs/%.*s",
            "refs/tags/%.*s",
            "refs/heads/%.*s",
            "refs/remotes/%.*s",
            "refs/remotes/%.*s/HEAD",
            NULL
    };
Quoted 3 lines

Which doesn't look very useful, because every ref from for_each_ref would match rule one. So we probably need to try the reverse of this list.

Yup. If you search the list backwards and extract the part of the ref that matches %.*s you'll get a name that other tools can find, and which is the shortest name possible.

Quoted 5 lines

You can still get ambiguous names. Avoiding them requires going through all refs and building their short forms, then using the full ref name for any ref which had more than one name shorten to the same string. Ugly, but implementable, and probably something that should be considered.

What about: try the list backwards until the first match, than try the matched part (this what %.*s matched) with the forward list, if both give the same pattern, its not disambiguous. If not try the next pattern backwards.

Its quadratic in the number of patterns, but this is maybe smaller than scanning all refs (which may include a sort phase).

Bert

Quoted 3 lines

--
Shawn.

#7 lore raw
Quoted 8 lines · Bert Wesarg

On Mon, Sep 1, 2008 at 21:10, Shawn O. Pearce <[email protected]> wrote:
...

Quoted 5 lines

You can still get ambiguous names. Avoiding them requires going through all refs and building their short forms, then using the full ref name for any ref which had more than one name shorten to the same string. Ugly, but implementable, and probably something that should be considered.

Quoted 4 lines

What about: try the list backwards until the first match, than try the matched part (this what %.*s matched) with the forward list, if both give the same pattern, its not disambiguous. If not try the next pattern backwards.

How does it catch the case where you have both 'xyzzy' branch and 'xyzzy' tag, which is the point of disambiguation issue Shawn raised?

#8 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Bert Wesarg

On Mon, Sep 1, 2008 at 21:10, Shawn O. Pearce <[email protected]> wrote:
...

Quoted 5 lines

You can still get ambiguous names. Avoiding them requires going through all refs and building their short forms, then using the full ref name for any ref which had more than one name shorten to the same string. Ugly, but implementable, and probably something that should be considered.

Quoted 7 lines
Quoted 4 lines

What about: try the list backwards until the first match, than try the matched part (this what %.*s matched) with the forward list, if both give the same pattern, its not disambiguous. If not try the next pattern backwards.

How does it catch the case where you have both 'xyzzy' branch and 'xyzzy' tag, which is the point of disambiguation issue Shawn raised?

Right.

#9 lore raw
Quoted 11 lines · Bert Wesarg
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Bert Wesarg

On Mon, Sep 1, 2008 at 21:10, Shawn O. Pearce <[email protected]> wrote:
...

Quoted 5 lines

You can still get ambiguous names. Avoiding them requires going through all refs and building their short forms, then using the full ref name for any ref which had more than one name shorten to the same string. Ugly, but implementable, and probably something that should be considered.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

What about: try the list backwards until the first match, than try the matched part (this what %.*s matched) with the forward list, if both give the same pattern, its not disambiguous. If not try the next pattern backwards.

How does it catch the case where you have both 'xyzzy' branch and 'xyzzy' tag, which is the point of disambiguation issue Shawn raised?

Right.

I was wrong:

given these two refs:

refs/heads/xyzzy
refs/tags/xyzzy

first try to shorten "refs/heads/xyzzy":

first (from the end) matched pattern is "refs/heads/%.*s" with
"xyzzy" as result

but resolved ref for "xyzzy" is "refs/tags/xyzzy" => continue

next matched pattern is "%.*s" with "refs/heads/xyzzy" as result

end result is therefore: "refs/heads/xyzzy"

second try to shorten "refs/tags/xyzzy":

first (from the end) matched pattern is "refs/tags/%.*s" with
"xyzzy" as result

resolved ref for "xyzzy" is "refs/tags/xyzzy" => end

end result is therefore: "xyzzy"

the output would be:

refs/heads/xyzzy
xyzzy

The question is now, if this is usable for bash completion? Current bash completion would handle this case wrong, because you get two xyzzy.

Bert

#10 lore raw
Quoted 26 lines · Bert Wesarg

given these two refs:

refs/heads/xyzzy
refs/tags/xyzzy

first try to shorten "refs/heads/xyzzy":

first (from the end) matched pattern is "refs/heads/%.*s" with
"xyzzy" as result

but resolved ref for "xyzzy" is "refs/tags/xyzzy" => continue

next matched pattern is "%.*s" with "refs/heads/xyzzy" as result

end result is therefore: "refs/heads/xyzzy"

second try to shorten "refs/tags/xyzzy":

first (from the end) matched pattern is "refs/tags/%.*s" with
"xyzzy" as result

resolved ref for "xyzzy" is "refs/tags/xyzzy" => end

end result is therefore: "xyzzy"

Quoted 8 lines

the output would be:

refs/heads/xyzzy
xyzzy

The question is now, if this is usable for bash completion? Current bash completion would handle this case wrong, because you get two xyzzy.

I think this is reasonable. Its better than what we have today, which is ambiguous completion. So this looks reasoanble to me. Usually people don't have ambiguous names, but it happens. I've been known to do something stupid like this:

  git checkout -b v1.0 v1.0
  git reset --hard v1.2
  git log v1.0
  # wtf?!?!!?!

;-)

Signature · 1 line
Shawn.
#11 lore raw

PATCH v2 1/1 for-each-ref: `:short` format for `refname`

Tries to shorten the refname to a non-ambiguous name. I.e. the full and the short refname points to the same object.

---
 Documentation/git-for-each-ref.txt |    2 +
 builtin-for-each-ref.c             |  133 ++++++++++++++++++++++++++++++++++--
 t/t6300-for-each-ref.sh            |   31 ++++++++
 3 files changed, 159 insertions(+), 7 deletions(-)
Documentation/git-for-each-ref.txt +2 -0
index eae6c0e..89158d9 100644
@@ -74,6 +74,8 @@ For all objects, the following names can be used:
 
 refname::
 	The name of the ref (the part after $GIT_DIR/).
+	For a non-ambiguous short name of the ref append `:short`.
+	I.e. both the full and short name will resolve to the same object.
 
 objecttype::
 	The type of the object (`blob`, `tree`, `commit`, `tag`).
builtin-for-each-ref.c +126 -7

145 lines not on this page · raw message

t/t6300-for-each-ref.sh +31 -0
index 8ced593..ad8d48e 100755
@@ -262,6 +262,37 @@ for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 	"
 done
 
+cat >expected <<\EOF
+master
+testtag
+EOF
+
+test_expect_success 'Check short refname format' '
+	(git for-each-ref --format="%(refname:short)" refs/heads &&
+	git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'Check for invalid refname format' '
+	test_must_fail git for-each-ref --format="%(refname:INVALID)"
+'
+
+cat >expected <<\EOF
+heads/master
+master
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs' '
+	git checkout -b newtag &&
+	echo "Using $datestamp" > one &&
+	git add one &&
+	git commit -m "Branch" &&
+	setdate_and_increment &&
+	git tag -m "Tagging at $datestamp" master &&
+	git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'an unusual tag with an incomplete line' '
 
 	git tag -m "bogo" bogo &&
Signature · 1 line
1.6.0
#12 lore raw
Quoted 2 lines · Bert Wesarg

Tries to shorten the refname to a non-ambiguous name. I.e. the full and the short refname points to the same object.

The definition of "ambiguity" here is wrong (see below).

builtin-for-each-ref.c static void grab_values(struct atom_value *val, int deref, struct object *obj, v · line 622

Quoted 12 lines
builtin-for-each-ref.c +12 -0
+	/* skip first rule, will always match */
+	for (i = 0; i < nr_rules - 1; i++) {
+		const char **p;
+		int short_name_len;
+
+		if (1 != sscanf(ref->refname, scanf_fmts[nr_rules - 1 - i],
+				short_name))
+			continue;
+
+		short_name_len = strlen(short_name);
+
+		/* check if full and short point to the same object

Style?

builtin-for-each-ref.c static void grab_values(struct atom_value *val, int deref, struct object *obj, v · line 624

Quoted 2 lines
builtin-for-each-ref.c +2 -0
+		 * by checking all rules in forward direction
+		 */

I think this part of the code is wrong, in that it talks about what object the ref points at. That is not what ref ambiguity is about.

Given a tag that points at a version 1.0.0 commit, this sequence will create:

	$ git tag foo v1.0.0^0
        $ git branch foo v1.0.0^0

ambiguous branch and tag whose names are both 'foo', even though they point at the same thing. The right API to use would be resolve_ref(), I think.

Other than that, it is well done.

Although I was initially a bit surprised by the size of the patch to implement something so (conceptually) simple, the code was easy and straightforward to follow.

Thanks.

#13 lore raw
Quoted 16 lines · Junio C Hamano
Quoted 2 lines · Bert Wesarg

Tries to shorten the refname to a non-ambiguous name. I.e. the full and the short refname points to the same object.

The definition of "ambiguity" here is wrong (see below).

builtin-for-each-ref.c static void grab_values(struct atom_value *val, int deref, struct object *obj, v · line 624

Quoted 2 lines
builtin-for-each-ref.c +2 -0
+              * by checking all rules in forward direction
+              */

I think this part of the code is wrong, in that it talks about what object the ref points at. That is not what ref ambiguity is about.

Given a tag that points at a version 1.0.0 commit, this sequence will create:

Quoted 6 lines
       $ git tag foo v1.0.0^0
       $ git branch foo v1.0.0^0

ambiguous branch and tag whose names are both 'foo', even though they point at the same thing. The right API to use would be resolve_ref(), I think.

I use resolve_ref():

int read_ref(const char *ref, unsigned char *sha1)

{
	if (resolve_ref(ref, sha1, 1, NULL))
		return 0;
	return -1;
}

Ok, I think I get your point: it doesn't matter if the full and short point to different objects, it's enough for ambiguity that the short name resolves to more than one ref.

New patch in reply.

Quoted 8 lines

Other than that, it is well done.

Although I was initially a bit surprised by the size of the patch to implement something so (conceptually) simple, the code was easy and straightforward to follow.

Thanks.

Your welcome.

Bert

Quoted 1 line
#14 lore raw

PATCH v3 1/1 for-each-ref: `:short` format for `refname`

Try to shorten the refname to a non-ambiguous name.

Changes in v3:
* don't compare sha1's, its ambiguous if the short name resovles to more than one ref
* use xstrdup()
* use indexes for the loops to clarify backward/forward direction

---
 Documentation/git-for-each-ref.txt |    1 +
 builtin-for-each-ref.c             |  138 ++++++++++++++++++++++++++++++++++--
 t/t6300-for-each-ref.sh            |   44 +++++++++++
 3 files changed, 176 insertions(+), 7 deletions(-)
Documentation/git-for-each-ref.txt +1 -0
index eae6c0e..4016f59 100644
@@ -74,6 +74,7 @@ For all objects, the following names can be used:
 
 refname::
 	The name of the ref (the part after $GIT_DIR/).
+	For a non-ambiguous short name of the ref append `:short`.
 
 objecttype::
 	The type of the object (`blob`, `tree`, `commit`, `tag`).
builtin-for-each-ref.c +131 -7

150 lines not on this page · raw message

t/t6300-for-each-ref.sh +44 -0

50 lines not on this page · raw message

Signature · 1 line
1.6.0
Acked-by on v3 1/1
#15 lore raw
Quoted 10 lines · Bert Wesarg

Try to shorten the refname to a non-ambiguous name.

Changes in v3:
* don't compare sha1's, its ambiguous if the short name resovles to more than one ref
* use xstrdup()
* use indexes for the loops to clarify backward/forward direction

Quoted 5 lines

---

Looks good. But the commit messages shouldn't have the "Changes in v3" section or probably the "Cc:" lines. The changes in v3 part usually goes below the ---.

I don't usually work on for-each-ref, but I'll toss an ack in anyway:

Signature · 1 line
Shawn.
#16 lore raw
Quoted 17 lines · Shawn O. Pearce
Quoted 10 lines · Bert Wesarg

Try to shorten the refname to a non-ambiguous name.

Changes in v3:
* don't compare sha1's, its ambiguous if the short name resovles to more than one ref
* use xstrdup()
* use indexes for the loops to clarify backward/forward direction

Quoted 5 lines

---

Quoted 3 lines

Looks good. But the commit messages shouldn't have the "Changes in v3" section or probably the "Cc:" lines. The changes in v3 part usually goes below the ---.

Ok, but shouldn't git format-patch detect that there is already a --- in the commit message, or is it illegal to have more than one ---?

And while we are at it, shouldn't git format-patch detect, that there is already a [*PATCH*]-prefix in the first line of the commit?

I always need to edit these two things after format-patch.

Bert

Quoted 4 lines

--
Shawn.

#17 lore raw
Quoted 18 lines · Bert Wesarg
Quoted 17 lines · Shawn O. Pearce
Quoted 15 lines · Bert Wesarg

Try to shorten the refname to a non-ambiguous name.

Changes in v3:
* don't compare sha1's, its ambiguous if the short name resovles to more than one ref
* use xstrdup()
* use indexes for the loops to clarify backward/forward direction

---

Quoted 8 lines
Quoted 3 lines

Looks good. But the commit messages shouldn't have the "Changes in v3" section or probably the "Cc:" lines. The changes in v3 part usually goes below the ---.

Ok, but shouldn't git format-patch detect that there is already a --- in the commit message, or is it illegal to have more than one ---?

And while we are at it, shouldn't git format-patch detect, that there is already a [*PATCH*]-prefix in the first line of the commit?

Ok, I was too fast, -k is obviously the right option for this.

Quoted 9 lines

I always need to edit these two things after format-patch.

Bert

Quoted 4 lines

--
Shawn.

#18 lore raw
In reply to Bert Wesarg
Quoted 8 lines · Bert Wesarg

Try to shorten the refname to a non-ambiguous name.

Changes in v3:
* don't compare sha1's, its ambiguous if the short name resovles to more than one ref
* use xstrdup()
* use indexes for the loops to clarify backward/forward direction

Please don't do "Changes in v3" in the commit log message; do so after "---" is very welcome as it gives people on the discussion _right now_ to understand the context of the change.

Also please do not omit discussion in the commit log message, to help people _not_ on the discussion right now.

Your earlier versions will not appear on the history anywhere, and v3 is the _only_ variant available to the people who need to understand what this change was made for and why the code to implement the change was done in the way the patch shows. Write your commit log message to help people who have "git show $this" and nothing else to figure these things out in year 2010.

Namely:

* Why "shortening" is a good thing? For what use case this new feature was invented for?

* What are the definitions of "shortening" and "non-ambiguous" in this feature's context?

In addition, for a complex feature with different possiblilities, it is a good practice to describe what alternatatives and pros-and-cons among them were considered while reaching this final version. This is to help people who would want to explore enhancement possibilities later; you beforehand warn them about dead ends you have already explored. For this particular patch/feature, I do not think it is necessary, though.

#19 lore raw

PATCH v4 1/1 for-each-ref: `:short` format for `refname`

Tries to shorten the refname to a non-ambiguous name.

Szeder Gábor noticed that the git bash completion takes a tremendous amount of time to strip leading components from heads and tags refs (i.e. refs/heads, refs/tags, ...). He proposed a new atom called 'refbasename' which removes at most two leading components from the ref name.

I myself, proposed a more dynamic solution, which strips off common leading components with the matched pattern.

But the current bash solution and both proposals suffer from one mayor problem: ambiguous refs.

A ref is ambiguous, if it resolves to more than one full refs. I.e. given the refs refs/heads/xyzzy and refs/tags/xyzzy. The (short) ref xyzzy can point to both refs.

( Note: Its irrelevant whether the referenced objects are the
same or not. )

This proposal solves this by checking for ambiguity of the shorten ref name.

The shortening is done with the same rules for resolving refs but in the reverse order. The short name is checked if it resolves to a different ref.

To continue the above example, the output would be like this:

heads/xyzzy
xyzzy

So, if you want just tags, xyzzy is not ambiguous, because it will resolve to a tag. If you need the heads you get a also a non-ambiguous short form of the ref.

To integrate this new format into the bash completion to get only non-ambiguous refs is beyond the scope of this patch.

---
Cc: [email protected]
Cc: [email protected]
Cc: Shawn O. Pearce <[email protected]>

 Documentation/git-for-each-ref.txt |    1 +
 builtin-for-each-ref.c             |  135 ++++++++++++++++++++++++++++++++++--
 t/t6300-for-each-ref.sh            |   44 ++++++++++++
 3 files changed, 173 insertions(+), 7 deletions(-)
Documentation/git-for-each-ref.txt +1 -0
index eae6c0e..4016f59 100644
@@ -74,6 +74,7 @@ For all objects, the following names can be used:
 
 refname::
 	The name of the ref (the part after $GIT_DIR/).
+	For a non-ambiguous short name of the ref append `:short`.
 
 objecttype::
 	The type of the object (`blob`, `tree`, `commit`, `tag`).
builtin-for-each-ref.c +128 -7

147 lines not on this page · raw message

t/t6300-for-each-ref.sh +44 -0

50 lines not on this page · raw message

Signature · 1 line
1.6.0
#20 lore raw
Quoted 10 lines · Bert Wesarg

...
To integrate this new format into the bash completion to get only non-ambiguous refs is beyond the scope of this patch.

---
Cc: [email protected]
Cc: [email protected]
Cc: Shawn O. Pearce <[email protected]>

Nice writeup of the history of this patch, if on tad-too-verbose side.

Quoted 24 lines
builtin-for-each-ref.c +18 -0
index 21e92bb..9b44092 100644
@@ -546,6 +546,107 @@ static void grab_values(struct atom_value *val, int deref, struct object *obj, v
+/*
+ * Shorten the refname to an non-ambiguous form
+ */
+static char *get_short_ref(struct refinfo *ref)
+{
...
+	/* skip first rule, it will always match */
+	for (i = nr_rules - 1; i > 0 ; --i) {
+		int j;
+		int short_name_len;
+
+		if (1 != sscanf(ref->refname, scanf_fmts[i], short_name))
+			continue;
+
+		short_name_len = strlen(short_name);
+
+		/*
+		 * check if the short name resolves to a valid ref,
+		 * but use only rules prior to the matched one

builtin-for-each-ref.c static void grab_values(struct atom_value *val, int deref, struct object *obj, v · line 641

Quoted 10 lines
builtin-for-each-ref.c +9 -0
+		 */
+		for (j = 0; j < i; j++) {
...
+		}
+		/*
+		 * short name is non-ambiguous if all previous rules
+		 * haven't resolved to a valid ref
+		 */
+		if (j == i)
+			return short_name;

Is this inner loop essentially the same as calling dwim_ref(), while temporarily turning warn_ambiguous_refs on, and checking for return value of one?

Quoted 8 lines
t/t6300-for-each-ref.sh +0 -0
index 8ced593..4f247dd 100755
@@ -262,6 +262,50 @@ for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
 	"
 done
 

t/t6300-for-each-ref.sh for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do · line 274

Quoted 10 lines
t/t6300-for-each-ref.sh +10 -0
+cat >expected <<\EOF
+master
+testtag
+EOF
+
+test_expect_success 'Check short refname format' '
+	(git for-each-ref --format="%(refname:short)" refs/heads &&
+	git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
+	test_cmp expected actual
+'

Not a complaint nor objection but mere curiosity. Why does this run two for-each-ref, not just one with two patterns?

t/t6300-for-each-ref.sh for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do · line 278

Quoted 3 lines
t/t6300-for-each-ref.sh +3 -0
+test_expect_success 'Check for invalid refname format' '
+	test_must_fail git for-each-ref --format="%(refname:INVALID)"
+'

Good.

Quoted 19 lines
t/t6300-for-each-ref.sh +19 -0
+cat >expected <<\EOF
+heads/master
+master
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs' '
+	git checkout -b newtag &&
+	echo "Using $datestamp" > one &&
+	git add one &&
+	git commit -m "Branch" &&
+	setdate_and_increment &&
+	git tag -m "Tagging at $datestamp" master &&
+	git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<\EOF
+heads/ambiguous
+ambiguous

t/t6300-for-each-ref.sh for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do · line 308

Quoted 10 lines
t/t6300-for-each-ref.sh +10 -0
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs II' '
+	git checkout master &&
+	git tag ambiguous testtag^0 &&
+	git branch ambiguous testtag^0 &&
+	git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
+	test_cmp expected actual
+'
+

Can we also try first creating a clone of some repo and run:

	for-each-ref --format="%(refname:short)" refs/remotes

I am unsure how "remotes/origin" when "refs/remotes/origin/HEAD" points at their 'master' branch behaves with your code, and/or how it should behave.

Other than that, nicely done.

#21 lore raw
Quoted 45 lines · Junio C Hamano
Quoted 10 lines · Bert Wesarg

...
To integrate this new format into the bash completion to get only non-ambiguous refs is beyond the scope of this patch.

---
Cc: [email protected]
Cc: [email protected]
Cc: Shawn O. Pearce <[email protected]>

Nice writeup of the history of this patch, if on tad-too-verbose side.

Quoted 30 lines
builtin-for-each-ref.c +23 -0
index 21e92bb..9b44092 100644
@@ -546,6 +546,107 @@ static void grab_values(struct atom_value *val, int deref, struct object *obj, v
+/*
+ * Shorten the refname to an non-ambiguous form
+ */
+static char *get_short_ref(struct refinfo *ref)
+{
...
+     /* skip first rule, it will always match */
+     for (i = nr_rules - 1; i > 0 ; --i) {
+             int j;
+             int short_name_len;
+
+             if (1 != sscanf(ref->refname, scanf_fmts[i], short_name))
+                     continue;
+
+             short_name_len = strlen(short_name);
+
+             /*
+              * check if the short name resolves to a valid ref,
+              * but use only rules prior to the matched one
+              */
+             for (j = 0; j < i; j++) {
...
+             }
+             /*
+              * short name is non-ambiguous if all previous rules
Quoted 8 lines

builtin-for-each-ref.c static void grab_values(struct atom_value *val, int deref, struct object *obj, v · line 641

Quoted 4 lines
builtin-for-each-ref.c +4 -0
+              * haven't resolved to a valid ref
+              */
+             if (j == i)
+                     return short_name;

Is this inner loop essentially the same as calling dwim_ref(), while temporarily turning warn_ambiguous_refs on, and checking for return value of one?

Not exactly.

Short version:

To follow my above example, with dwim_ref() we would get this:

heads/xyzzy
tags/xyzzy

Long version:

Currently we consider only rules prior to the matched rule (the rule which gives us the short name). That is, if any of these rules will also resolve to a valid ref, the short name is ambiguous, else its unambiguous. If we would consider subsequent rules past the matched one, we may find more valid refs for this short name. Because the current rule would match first if we try to resolve the short name, we don't have to check these rules. We get only a "ambiguous refs" warning.

I have no opinion if we want this 'strict unambiguousness'.

Quoted 9 lines
Quoted 9 lines
t/t6300-for-each-ref.sh +0 -0
index 8ced593..4f247dd 100755
@@ -262,6 +262,50 @@ for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
      "
 done
 
Quoted 13 lines

t/t6300-for-each-ref.sh for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do · line 274

Quoted 10 lines
t/t6300-for-each-ref.sh +10 -0
+cat >expected <<\EOF
+master
+testtag
+EOF
+
+test_expect_success 'Check short refname format' '
+     (git for-each-ref --format="%(refname:short)" refs/heads &&
+     git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
+     test_cmp expected actual
+'

Not a complaint nor objection but mere curiosity. Why does this run two for-each-ref, not just one with two patterns?

Its more a leftover from the :strip version, where the pattern was the point of interest.

Quoted 39 lines

t/t6300-for-each-ref.sh for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do · line 278

Quoted 4 lines
t/t6300-for-each-ref.sh +3 -0
+test_expect_success 'Check for invalid refname format' '
+     test_must_fail git for-each-ref --format="%(refname:INVALID)"
+'

Good.

Quoted 19 lines
t/t6300-for-each-ref.sh +19 -0
+cat >expected <<\EOF
+heads/master
+master
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs' '
+     git checkout -b newtag &&
+     echo "Using $datestamp" > one &&
+     git add one &&
+     git commit -m "Branch" &&
+     setdate_and_increment &&
+     git tag -m "Tagging at $datestamp" master &&
+     git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
+     test_cmp expected actual
+'
+
+cat >expected <<\EOF
+heads/ambiguous
+ambiguous

t/t6300-for-each-ref.sh for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do · line 308

Quoted 10 lines
t/t6300-for-each-ref.sh +10 -0
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs II' '
+     git checkout master &&
+     git tag ambiguous testtag^0 &&
+     git branch ambiguous testtag^0 &&
+     git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
+     test_cmp expected actual
+'
+

Can we also try first creating a clone of some repo and run:

Quoted 4 lines
       for-each-ref --format="%(refname:short)" refs/remotes

I am unsure how "remotes/origin" when "refs/remotes/origin/HEAD" points at their 'master' branch behaves with your code, and/or how it should behave.

I will look at this.

Quoted 4 lines

Other than that, nicely done.

Bert

#24 lore raw
In reply to Junio C Hamano
Quoted 5 lines · Junio C Hamano

Any followup on this topic since:

http://thread.gmane.org/gmane.comp.version-control.git/94478/focus=95041

I plan on looking at the refs/remote test today.

Any opinions, whether we want the 'strict' mode? i.e.:

for refs/heads/xyzzy and refs/tags/xyzzy:

loose mode (current implementation):

refs/heads/xyzzy => heads/xyzzy
refs/tags/xyzzy => xyzzy

there would be a ambiguous warning (if enabled) if you use xyzzy as a tag, but it resolves correctly to the tag.

strict mode:

refs/heads/xyzzy => heads/xyzzy
refs/tags/xyzzy => tags/xyzzy

will always produce a non-ambiguous short forms.

Bert

#25 lore raw
Quoted 12 lines · Bert Wesarg

Any opinions, whether we want the 'strict' mode? i.e.:

for refs/heads/xyzzy and refs/tags/xyzzy:

loose mode (current implementation):

refs/heads/xyzzy => heads/xyzzy
refs/tags/xyzzy => xyzzy

there would be a ambiguous warning (if enabled) if you use xyzzy as a tag, but it resolves correctly to the tag.

Quoted 6 lines

strict mode:

refs/heads/xyzzy => heads/xyzzy
refs/tags/xyzzy => tags/xyzzy

will always produce a non-ambiguous short forms.

I have no strong opinions either way, but if we want to pick only one, I suspect that the loose mode would be more appropriate for bash completion purposes exactly because:

(1) the shorter form would match the users' expectations, and;

(2) if it triggers ambiguity warning to use that result that matches users' expectations, it is a *good thing* --- it reminds the user that s/he is playing with fire _if_ the user is of careful type who enables the ambiguity warning.

Thinking about it from a different angle, it would make more sense to use loose mode if the user does not have ambiguity warning configured, and use strict mode if the warning is enabled. Then people who will get warnings from ambiguity will not get an ambiguous completion, and people who won't will get shorter but still unambiguous completion.

Which means, despite what I said earlier, now I have a mild preference to tie the choice to core.wawrnambigousrefs configuration.

#26 lore raw
Quoted 32 lines · Junio C Hamano
Quoted 12 lines · Bert Wesarg

Any opinions, whether we want the 'strict' mode? i.e.:

for refs/heads/xyzzy and refs/tags/xyzzy:

loose mode (current implementation):

refs/heads/xyzzy => heads/xyzzy
refs/tags/xyzzy => xyzzy

there would be a ambiguous warning (if enabled) if you use xyzzy as a tag, but it resolves correctly to the tag.

Quoted 6 lines

strict mode:

refs/heads/xyzzy => heads/xyzzy
refs/tags/xyzzy => tags/xyzzy

will always produce a non-ambiguous short forms.

I have no strong opinions either way, but if we want to pick only one, I suspect that the loose mode would be more appropriate for bash completion purposes exactly because:

(1) the shorter form would match the users' expectations, and;

(2) if it triggers ambiguity warning to use that result that matches users' expectations, it is a *good thing* --- it reminds the user that s/he is playing with fire _if_ the user is of careful type who enables the ambiguity warning.

Quoted 8 lines

Thinking about it from a different angle, it would make more sense to use loose mode if the user does not have ambiguity warning configured, and use strict mode if the warning is enabled. Then people who will get warnings from ambiguity will not get an ambiguous completion, and people who won't will get shorter but still unambiguous completion.

Which means, despite what I said earlier, now I have a mild preference to tie the choice to core.wawrnambigousrefs configuration.

A really nice idea. Await PATCH v5. I had to change my plans for today, unfortunately.

Bert

Quoted 1 line