"git log .." thinks ".." is ambiguous
Either end of revision range operator can be omitted to default to HEAD, as in "origin.." (what did I do since I forked) or "..origin" (what did they do since I forked). But this resulted in ".." to be interpreted as an empty range "HEAD..HEAD", and worse yet, because ".." does exist on the filesystem, we get this annoying output:
$ cd Documentation/howto $ git log .. ;# give me recent commits that touch Documentation/ area. fatal: ambiguous argument '..': both revision and filename Use '--' to separate filenames from revisions
Surely we could say "git log .. --", but we shouldn't have to.
---
* I don't have very strong feeling about this, but just got annoyed enough to write this patch. Should apply cleanly all the way down to at least 1.6.0 if not earlier.
revision.c | 16 ++++++++++++++-- 1 files changed, 14 insertions(+), 2 deletions(-)
revision.c +14 -2
@@ -905,14 +905,26 @@ int handle_revision_arg(const char *arg, struct rev_info *revs,const char *this = arg;int symmetric = *next =='.' ;unsigned int flags_exclude = flags ^UNINTERESTING ; +static const char head_by_default[] ="HEAD" ; *dotdot =0 ; next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; +if (this == head_by_default && next == head_by_default && + !symmetric) { +/* +* Just ".."? That is not a range but the +* pathspec for the parent directory. +*/ +if (!cant_be_filename) { + *dotdot ='.' ; +return -1 ; + } + }if (!get_sha1 (this, from_sha1) && !get_sha1 (next, sha1)) {struct commit *a, *b;
Quoted 6 lines · Junio C Hamano
Either end of revision range operator can be omitted to default to HEAD, as in "origin.." (what did I do since I forked) or "..origin" (what did they do since I forked). But this resulted in ".." to be interpreted as an empty range "HEAD..HEAD", and worse yet, because ".." does exist on the filesystem, we get this annoying output:
Quoted 6 lines
$ cd Documentation/howto $ git log .. ;# give me recent commits that touch Documentation/ area. fatal: ambiguous argument '..': both revision and filename Use '--' to separate filenames from revisionsSurely we could say "git log .. --", but we shouldn't have to.
I got slightly confused reading this, because I thought at first you wanted ".." to be some kind of magic rev specifier. In particular, your last line should say:
git log -- ..
no?
I finally figured it out by reading your example carefully.
As far as the patch goes, I think it is the right thing to do. We should probably disallow ".." as a revision (because it is nonsensical to ask for, just as it would be to ask for a non-existent ref). And then the only sane thing is to consider it as a path.
I didn't check, but what does your patch do with:
$ cd "`git rev-parse --show-toplevel`" $ git log ..
?
-Peff
Quoted 4 lines · Jeff King
I didn't check, but what does your patch do with:
$ cd "`git rev-parse --show-toplevel`" $ git log ..
Nevermind, it properly complains "'..' is outside repository"; of course no new code is needed because we could already do "git log ../..".
I do notice that gitk doesn't work, though. Doing "gitk .." produces an "ambiguous argument" error. It looks like "git rev-parse .." still treats it as HEAD..HEAD, which may be the source of the error.
-Peff
Quoted 15 lines · Jeff King
Quoted 6 lines · Junio C Hamano
Either end of revision range operator can be omitted to default to HEAD, as in "origin.." (what did I do since I forked) or "..origin" (what did they do since I forked). But this resulted in ".." to be interpreted as an empty range "HEAD..HEAD", and worse yet, because ".." does exist on the filesystem, we get this annoying output:
Quoted 6 lines
$ cd Documentation/howto $ git log .. ;# give me recent commits that touch Documentation/ area. fatal: ambiguous argument '..': both revision and filename Use '--' to separate filenames from revisionsSurely we could say "git log .. --", but we shouldn't have to.
Quoted 7 lines
I got slightly confused reading this, because I thought at first you wanted ".." to be some kind of magic rev specifier. In particular, your last line should say:
git log -- ..
no?
Yeah, sorry about that. Or "git log ../".
And a matching "rev-parse" update should look like this. I wonder if we could share some code between the two, though...
Documentation/revisions.txt | 7 +++++++ builtin/rev-parse.c | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-)
Documentation/revisions.txt +7 -0
@@ -213,6 +213,13 @@ of 'r1' and 'r2' and is defined as It is the set of commits that are reachable from either one of 'r1' or 'r2' but not from both. +In these two shorthands, you can omit one end and let it default to HEAD. +For example, 'origin..' is a shorthand for 'origin..HEAD' and asks "What +did I do since I forked from the origin branch?" Similarly, '..origin' +is a shorthand for 'HEAD..origin' and asks "What did the origin do since +I forked from them?" Note that you cannot omit both ends. '..' is not +an empty range that is both reachable and unreachable from HEAD. + Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The 'r1{caret}@' notation means all parents of 'r1'. 'r1{caret}!' includes commit 'r1' but excludes
builtin/rev-parse.c +14 -2
@@ -223,6 +223,7 @@ static int try_difference(const char *arg)const char *next;const char *this;int symmetric; +static const char head_by_default[] ="HEAD" ;if (!(dotdot =strstr (arg,".." )))return 0 ;
@@ -234,9 +235,20 @@ static int try_difference(const char *arg) next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; + +if (this == head_by_default && next == head_by_default && + !symmetric) { +/* +* Just ".."? That is not a range but the +* pathspec for the parent directory. +*/ + *dotdot ='.' ; +return 0 ; + } +if (!get_sha1 (this, sha1) && !get_sha1 (next, end)) {show_rev (NORMAL , end, next);show_rev (symmetric ?NORMAL :REVERSED , sha1, this);
Quoted 6 lines · Junio C Hamano
And a matching "rev-parse" update should look like this. I wonder if we could share some code between the two, though...
Documentation/revisions.txt | 7 +++++++ builtin/rev-parse.c | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-)
The doc update looks good to me. The duplication in rev-parse is pretty gross, but that is certainly not a problem you are introducing. It would be nice to clean it up, but I don't know how painful that would be. It seems like rev-parse should just be calling handle_revision_arg, but I'm sure there are some nasty corner cases. IOW, I am not volunteering. :)
-Peff
PATCH v2 1/1 specifying ranges: we did not mean to make ".." an empty set
In reply to Junio C HamanoEither end of revision range operator can be omitted to default to HEAD, as in "origin.." (what did I do since I forked) or "..origin" (what did they do since I forked). But the current parser interprets ".." as an empty range "HEAD..HEAD", and worse yet, because ".." does exist on the filesystem, we get this annoying output:
$ cd Documentation/howto $ git log .. ;# give me recent commits that touch Documentation/ area. fatal: ambiguous argument '..': both revision and filename Use '--' to separate filenames from revisions
Surely we could say "git log ../" or even "git log -- .." to disambiguate, but we shouldn't have to.
---
* With docs and tests. In an early version, I forgot to restore the revision string that is temporarily NUL terminated with *dotdot = '.', which resulted in "git log .." from Doc/howto to show not just the log of Doc but show everything. The new test in t4202 tries to catch that rather common mistake as well.
Documentation/revisions.txt | 7 +++++++ builtin/rev-parse.c | 16 ++++++++++++++-- revision.c | 16 ++++++++++++++-- t/t1506-rev-parse-diagnosis.sh | 14 ++++++++++++++ t/t4202-log.sh | 7 +++++++ 5 files changed, 56 insertions(+), 4 deletions(-)
Documentation/revisions.txt +7 -0
@@ -184,6 +184,13 @@ of `r1` and `r2` and is defined as It is the set of commits that are reachable from either one of `r1` or `r2` but not from both. +In these two shorthands, you can omit one end and let it default to HEAD. +For example, 'origin..' is a shorthand for 'origin..HEAD' and asks "What +did I do since I forked from the origin branch?" Similarly, '..origin' +is a shorthand for 'HEAD..origin' and asks "What did the origin do since +I forked from them?" Note that you cannot omit both ends. '..' is not +an empty range that is both reachable and unreachable from HEAD. + Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The `r1{caret}@` notation means all parents of `r1`. `r1{caret}!` includes commit `r1` but excludes
builtin/rev-parse.c +14 -2
@@ -219,6 +219,7 @@ static int try_difference(const char *arg)const char *next;const char *this;int symmetric; +static const char head_by_default[] ="HEAD" ;if (!(dotdot =strstr (arg,".." )))return 0 ;
@@ -230,9 +231,20 @@ static int try_difference(const char *arg) next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; + +if (this == head_by_default && next == head_by_default && + !symmetric) { +/* +* Just ".."? That is not a range but the +* pathspec for the parent directory. +*/ + *dotdot ='.' ; +return 0 ; + } +if (!get_sha1 (this, sha1) && !get_sha1 (next, end)) {show_rev (NORMAL , end, next);show_rev (symmetric ?NORMAL :REVERSED , sha1, this);
revision.c +14 -2
@@ -1004,14 +1004,26 @@ int handle_revision_arg(const char *arg, struct rev_info *revs,const char *this = arg;int symmetric = *next =='.' ;unsigned int flags_exclude = flags ^UNINTERESTING ; +static const char head_by_default[] ="HEAD" ; *dotdot =0 ; next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; +if (this == head_by_default && next == head_by_default && + !symmetric) { +/* +* Just ".."? That is not a range but the +* pathspec for the parent directory. +*/ +if (!cant_be_filename) { + *dotdot ='.' ; +return -1 ; + } + }if (!get_sha1 (this, from_sha1) && !get_sha1 (next, sha1)) {struct commit *a, *b;
t/t1506-rev-parse-diagnosis.sh +14 -0
@@ -75,4 +75,18 @@ test_expect_success 'invalid @{n} reference' ' grep "fatal: Log for [^ ]* only has [0-9][0-9]* entries." error ' +test_expect_success 'dotdot is not an empty set' ' + ( H=$(git rev-parse HEAD) && echo $H ; echo ^$H ) >expect && + + git rev-parse HEAD.. >actual && + test_cmp expect actual && + + git rev-parse ..HEAD >actual && + test_cmp expect actual && + + echo .. >expect && + git rev-parse .. >actual && + test_cmp expect actual +' + test_done
t/t4202-log.sh +7 -0
@@ -463,4 +463,11 @@ test_expect_success 'show added path under "--follow -M"' ' ) ' +test_expect_success 'dotdot is a parent directory' ' + mkdir -p a/b && + ( echo sixth && echo fifth ) >expect && + ( cd a/b && git log --format=%s .. ) >actual && + test_cmp expect actual +' + test_done
Signature · 1 line
1.7.5.253.g2243e
Re: [PATCH v2] specifying ranges: we did not mean to make ".." an empty set
Quoted 11 lines · Junio C Hamano
Either end of revision range operator can be omitted to default to HEAD, as in "origin.." (what did I do since I forked) or "..origin" (what did they do since I forked). But the current parser interprets ".." as an empty range "HEAD..HEAD", and worse yet, because ".." does exist on the filesystem, we get this annoying output:
$ cd Documentation/howto $ git log .. ;# give me recent commits that touch Documentation/ area. fatal: ambiguous argument '..': both revision and filename Use '--' to separate filenames from revisions
Quoted 5 lines
Surely we could say "git log ../" or even "git log -- .." to disambiguate, but we shouldn't have to.
Helped-by Jeff King <[email protected]>Signed-off-by Junio C Hamano <[email protected]>
Looks good to me.
Doing "..." is still allowed, but will never produce any useful results. I don't know if it is worth disallowing it to catch errors. I am tempted to say it should be magic for "@{u}...HEAD", but I think just "..." is getting unreadably magical. "@{u}...HEAD" is already pretty concise and is much more readable.
-Peff
Re: [PATCH v2] specifying ranges: we did not mean to make ".." an empty set
Jeff King venit, vidit, dixit 02.05.2011 23:01:
Quoted 13 lines · Junio C Hamano
Quoted 12 lines
Either end of revision range operator can be omitted to default to HEAD, as in "origin.." (what did I do since I forked) or "..origin" (what did they do since I forked). But the current parser interprets ".." as an empty range "HEAD..HEAD", and worse yet, because ".." does exist on the filesystem, we get this annoying output:
$ cd Documentation/howto $ git log .. ;# give me recent commits that touch Documentation/ area. fatal: ambiguous argument '..': both revision and filename Use '--' to separate filenames from revisions
Quoted 7 lines
Quoted 5 lines
Surely we could say "git log ../" or even "git log -- .." to disambiguate, but we shouldn't have to.
Helped-by Jeff King <[email protected]>Signed-off-by Junio C Hamano <[email protected]>Looks good to me.
I'm sorry but I don't like this at all, because:
Quoted 5 lines
Doing "..." is still allowed, but will never produce any useful results. I don't know if it is worth disallowing it to catch errors. I am tempted to say it should be magic for "@{u}...HEAD", but I think just "..." is getting unreadably magical. "@{u}...HEAD" is already pretty concise and is much more readable.
We need to disambiguate any pathspec with "--" which could be a revision parameter. Therefore I find it very unnatural to disambiguate ".." to a pathspec automatically (and have "..." error out). "../" is really simple enough to type.
For "..", we should either catch the error or, better, have ".." default to "@{u}..HEAD" and do the same for "...". Those are used much more often, and this goes with "try to parse as a rev first, then pathspec".
Michael
Re: [PATCH v2] specifying ranges: we did not mean to make ".." an empty set
Quoted 13 lines · Michael J Gruber
Quoted 4 lines
Quoted 2 lines
Helped-by Jeff King <[email protected]>Signed-off-by Junio C Hamano <[email protected]>Looks good to me.
I'm sorry but I don't like this at all, because:
Quoted 5 lines
Doing "..." is still allowed, but will never produce any useful results. I don't know if it is worth disallowing it to catch errors. I am tempted to say it should be magic for "@{u}...HEAD", but I think just "..." is getting unreadably magical. "@{u}...HEAD" is already pretty concise and is much more readable.
Quoted 4 lines
We need to disambiguate any pathspec with "--" which could be a revision parameter. Therefore I find it very unnatural to disambiguate ".." to a pathspec automatically (and have "..." error out). "../" is really simple enough to type.
If you are comfortable typing "../", why do you even care? It would be a different story if the patch made ".." error out and forbade to be used as an empty range even when you disambiguated, i.e. "git log .. --", but that is not what we are doing.
And we do not even special case "...". Between the two potential requests of asking for an empty revision range and asking for a pathspec "...", both are just as unlikely.
Contrast that with ".." and realize that is very different. It is infinitely more likely that the user meant the immediate parent directory than an empty revision range.
Re: [PATCH v2] specifying ranges: we did not mean to make ".." an empty set
Junio C Hamano venit, vidit, dixit 03.05.2011 19:38:
Quoted 15 lines
Quoted 13 lines · Michael J Gruber
Quoted 4 lines
Quoted 2 lines
Helped-by Jeff King <[email protected]>Signed-off-by Junio C Hamano <[email protected]>Looks good to me.
I'm sorry but I don't like this at all, because:
Quoted 5 lines
Doing "..." is still allowed, but will never produce any useful results. I don't know if it is worth disallowing it to catch errors. I am tempted to say it should be magic for "@{u}...HEAD", but I think just "..." is getting unreadably magical. "@{u}...HEAD" is already pretty concise and is much more readable.
Quoted 6 lines
Quoted 4 lines
We need to disambiguate any pathspec with "--" which could be a revision parameter. Therefore I find it very unnatural to disambiguate ".." to a pathspec automatically (and have "..." error out). "../" is really simple enough to type.
If you are comfortable typing "../", why do you even care? It would be a
I care about:
- sane defaults
- sane arguments
Quoted 11 lines
different story if the patch made ".." error out and forbade to be used as an empty range even when you disambiguated, i.e. "git log .. --", but that is not what we are doing.
And we do not even special case "...". Between the two potential requests of asking for an empty revision range and asking for a pathspec "...", both are just as unlikely.
Contrast that with ".." and realize that is very different. It is infinitely more likely that the user meant the immediate parent directory than an empty revision range.
and that is a straw man argument. I suggested "@{u}..HEAD" for "..", because I consider that much more useful. "Infinitely more likely" is obviously true and obviously pointless when you compare with something of zero likelihood (and nonsense otherwise). I have no problem accepting a majority vote or sane arguments, but not something like this, sorry.
Michael
Re: [PATCH v2] specifying ranges: we did not mean to make ".." an empty set
Quoted 6 lines · Michael J Gruber
Quoted 3 lines
Contrast that with ".." and realize that is very different. It is infinitely more likely that the user meant the immediate parent directory than an empty revision range.
and that is a straw man argument. I suggested "@{u}..HEAD" for "..", because I consider that much more useful.
But the thing is,
Either of @{u}.. or ..@{u} may be an often used range depending on who you are and what you want to find. But reusing ".." for that purpose would be a very steep uphill battle for obvious reasons that I can count at least three:
* How can a user remember ".." stands for whichever of @{u}.. or ..@{u} you happen to pick?
* Can you guarantee that there will never be anything _more_ common and useful than "@{u}.." that deserves to use ".." as a shorthand? I can't.
* Doing anything other than an empty range for ".." is a bit _too_ magical for my taste. After all, if $A.. means $A..HEAD and ..$B means HEAD..$B, giving an empty string to $A or $B should yield nothing other than HEAD..HEAD, unless you want to confuse the user.
I think your argument is that I wouldn't have felt the annoyance if ".." meant a vastly more useful range that is totally different from the current semantics, because I would have understood that ".." could be both a useful range and a useful pathspec, and there won't be "Stipid git! I know .. could be an empty range but it should be obvious to you that I didn't mean that interpretation with no practical value. Just take it as a parent directory pathspec!".
You could achieve that by time-travelling to a past where no git user were present and inplement "$A..$B" to default in that way from day one, and if I were living in such a world, I would certainly agree with you.
But that is not the world we live in.
In other words, both of us agree with the statement: "What .. means today has no practical value". But I do not think that the conclusion that follows that statement should be "so let's change its semantics and make it do something useful". Changing it to anything magical breaks consistency in a big way, than keeping this degerated case as such.
As I said in the beginning of the thread, this was a mere annoyance, and I wouldn't be unhappy to drop this change. I will have to type "../" myself when I mean "parent directory", but that is a minor annoyance. But it also may turn out to be a disaster that everybody else needs to teach new people to do so.
As to changing the semantics of "..", I am moderately against it, but I consider that is a separate topic. I am not opposed to giving a range that is common and useful (be it @{u}.., ..@{u}, or anything else) a short-hand, but that short-hand should not be "..".
Re: [PATCH v2] specifying ranges: we did not mean to make ".." an empty set
Quoted 4 lines · Junio C Hamano
As to changing the semantics of "..", I am moderately against it, but I consider that is a separate topic. I am not opposed to giving a range that is common and useful (be it @{u}.., ..@{u}, or anything else) a short-hand, but that short-hand should not be "..".
After re-reading 1654a46 (specifying ranges: we did not mean to make ".." an empty set, 2011-05-02), I found that I was contradicting myself in the patch.
The last part of the documantation update in that patch is clearly wrong. The patch does not change what ".." means. The only thing it does is to widen the heuristics that is used to disambiguate when the input could be ambiguous.
Documentation/revisions.txt | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
Documentation/revisions.txt +2 -2
@@ -187,10 +187,10 @@ It is the set of commits that are reachable from either one of In these two shorthands, you can omit one end and let it default to HEAD. For example, 'origin..' is a shorthand for 'origin..HEAD' and asks "What did I do since I forked from the origin branch?" Similarly, '..origin' is a shorthand for 'HEAD..origin' and asks "What did the origin do since -I forked from them?" Note that you cannot omit both ends. '..' is not -an empty range that is both reachable and unreachable from HEAD. +I forked from them?" Note that '..' would mean 'HEAD..HEAD' which is an +empty range that is both reachable and unreachable from HEAD. Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The `r1{caret}@` notation means all parents of `r1`. `r1{caret}!` includes commit `r1` but excludes
Re: [PATCH v2] specifying ranges: we did not mean to make ".." an empty set
In reply to Jeff KingQuoted 2 lines · Jeff King
.... "@{u}...HEAD" is already pretty concise and
is much more readable.
Especially since you can ust say "@{u}..." without HEAD.
Quoted 10 lines · Junio C Hamano
Either end of revision range operator can be omitted to default to HEAD, as in "origin.." (what did I do since I forked) or "..origin" (what did they do since I forked). But this resulted in ".." to be interpreted as an empty range "HEAD..HEAD", and worse yet, because ".." does exist on the filesystem, we get this annoying output:
$ cd Documentation/howto $ git log .. ;# give me recent commits that touch Documentation/ area. fatal: ambiguous argument '..': both revision and filename Use '--' to separate filenames from revisions
Perhaps it's because I still don't use git quite enough yet... but I don't find the above annoying. *shrug*
-John
Quoted 12 lines · Junio C Hamano
revision.c +1
-0@@ -905,14 +905,26 @@ int handle_revision_arg(const char *arg,struct rev_info *revs, const char *this = arg;int symmetric = *next =='.' ;unsigned int flags_exclude = flags ^UNINTERESTING ; +static const char head_by_default[] ="HEAD" ; *dotdot =0 ;
revision.c int handle_revision_arg(const char *arg, · line 919
Quoted 10 lines
revision.c +4
-2next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; +if (this == head_by_default && next == head_by_default && + !symmetric) {
Is there a reason not to write
const char *head_by_default = "HEAD";
or even
const char *const head_by_default = "HEAD";
instead? Loading a static array and checking an init flag is a pessimization versus just pointing into a read-only segment.
Josh
Joshua Juran venit, vidit, dixit 03.05.2011 03:57:
Quoted 27 lines · Junio C Hamano
Quoted 13 lines
revision.c +1
-0@@ -905,14 +905,26 @@ int handle_revision_arg(const char *arg,struct rev_info *revs, const char *this = arg;int symmetric = *next =='.' ;unsigned int flags_exclude = flags ^UNINTERESTING ; +static const char head_by_default[] ="HEAD" ; *dotdot =0 ;revision.c int handle_revision_arg(const char *arg, · line 919
Quoted 10 lines
revision.c +4
-2next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; +if (this == head_by_default && next == head_by_default && + !symmetric) {Is there a reason not to write
Quoted 8 lines
const char *head_by_default = "HEAD";or even
const char *const head_by_default = "HEAD";instead? Loading a static array and checking an init flag is a pessimization versus just pointing into a read-only segment.
Because of the comparisons later on: this == "HEAD" is not the same.
Michael
Quoted 30 lines · Michael J Gruber
Joshua Juran venit, vidit, dixit 03.05.2011 03:57:
Quoted 29 lines · Junio C Hamano
Quoted 13 lines
revision.c +1
-0@@ -905,14 +905,26 @@ int handle_revision_arg(const char *arg,struct rev_info *revs, const char *this = arg;int symmetric = *next =='.' ;unsigned int flags_exclude = flags ^UNINTERESTING ; +static const char head_by_default[] ="HEAD" ; *dotdot =0 ;revision.c int handle_revision_arg(const char *arg, · line 919
Quoted 10 lines
revision.c +4
-2next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; +if (this == head_by_default && next == head_by_default && + !symmetric) {Is there a reason not to write
const char *head_by_default = "HEAD";
Quoted 8 lines
Quoted 6 lines
or even
const char *const head_by_default = "HEAD";instead? Loading a static array and checking an init flag is a pessimization versus just pointing into a read-only segment.
Because of the comparisons later on: this == "HEAD" is not the same.
Sorry if I was unclear. I meant to replace only the 'static const char head_by_default[] = "HEAD";' line and leave the rest of the patch unchanged.
My recollection is that Metrowerks C will statically allocate read-write storage (duplicating the read-only copy of the string) and copy the string into it at runtime. It only copies the string once, which is ensured by checking an internally generated flag (also statically allocated) every time the scope containing the declaration is executed. This is the pessimization I speak of.
By contrast, my suggestion allocates a single pointer on the stack regardless of compiler optimization.
Josh
Joshua Juran venit, vidit, dixit 03.05.2011 10:17:
Quoted 52 lines · Michael J Gruber
Quoted 31 lines
Joshua Juran venit, vidit, dixit 03.05.2011 03:57:
Quoted 29 lines · Junio C Hamano
Quoted 13 lines
revision.c +1
-0@@ -905,14 +905,26 @@ int handle_revision_arg(const char *arg,struct rev_info *revs, const char *this = arg;int symmetric = *next =='.' ;unsigned int flags_exclude = flags ^UNINTERESTING ; +static const char head_by_default[] ="HEAD" ; *dotdot =0 ;revision.c int handle_revision_arg(const char *arg, · line 919
Quoted 10 lines
revision.c +4
-2next += symmetric;if (!*next) - next ="HEAD" ; + next = head_by_default;if (dotdot == arg) - this ="HEAD" ; + this = head_by_default; +if (this == head_by_default && next == head_by_default && + !symmetric) {Is there a reason not to write
const char *head_by_default = "HEAD";Quoted 8 lines
Quoted 6 lines
or even
const char *const head_by_default = "HEAD";instead? Loading a static array and checking an init flag is a pessimization versus just pointing into a read-only segment.
Because of the comparisons later on: this == "HEAD" is not the same.
Sorry if I was unclear. I meant to replace only the 'static const char head_by_default[] = "HEAD";' line and leave the rest of the patch unchanged.
My recollection is that Metrowerks C will statically allocate read- write storage (duplicating the read-only copy of the string) and copy the string into it at runtime. It only copies the string once, which is ensured by checking an internally generated flag (also statically allocated) every time the scope containing the declaration is executed. This is the pessimization I speak of.
Quoted 2 lines
By contrast, my suggestion allocates a single pointer on the stack regardless of compiler optimization.
I see, sorry, I misread your suggestion. Junio will have to answer for himself then ;)
Michael
Quoted 6 lines · Joshua Juran
My recollection is that Metrowerks C will statically allocate read- write storage (duplicating the read-only copy of the string) and copy the string into it at runtime. It only copies the string once, which is ensured by checking an internally generated flag (also statically allocated) every time the scope containing the declaration is executed. This is the pessimization I speak of.
That's a stupid compiler, isn't it? Doesn't it pay attention to the fact that the static char string in the function scope also says "const"?
A sane compiler would store { 'H' 'E' 'A' 'D' '\0' } five bytes in a data segment (preferrably ro), give it a label and turn its assignment to and comparison with "next" and "this" to a reference to that label address, and all can be resolved by the linker. Your suggestion will give a compiler an excuse to allocate an extra pointer variable to hold that address in addition to that, espcially if you do not say const twice.
Of course the extra pointer could be optimized away if you follow the data and if the compiler does so we would get the same code.