{"thread":{"id":"176","subject":"[PATCH] gittrack.sh accepts invalid branch names","startedAt":"2005-04-20T19:48:30Z","lastAt":"2005-04-21T01:28:51Z","messageCount":4,"participants":["Pavel Roskin","Petr Baudis","Paul Jackson"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"1021","messageId":"1114026510.15186.15.camel@dv","threadId":"176","inReplyTo":null,"subject":"[PATCH] gittrack.sh accepts invalid branch names","fromName":"Pavel Roskin","fromEmail":"proski@gnu.org","sentAt":"2005-04-20T19:48:30Z","receivedAt":"2005-04-20T19:48:30Z","isPatch":true,"sender":{"key":"proski@gnu.org","avatar":null},"body":"Hello, Petr and everybody!\n\ngittrack.sh allows abbreviated branch names, e.g. it's possible to run\n\"git track lin\" when there is a branch called \"linus\".\n\nI believe it's a bug, not a feature.  Please look at this line from\ngittrack.sh:\n\ngrep -q $(echo -e \"^$name\\t\" | sed 's/\\./\\\\./g') .git/remotes\n\nThe result of command expansion is subjected to word splitting, which\nmeans the trailing tab is removed as a space.  So grep doesn't see the\ntab.\n\nThe way to avoid word splitting would be to quote \"$()\", but it would\nmake the shell code too hairy.  I'm not even sure all shells would\ninterpret \"$(\"$name\")\" correctly.\n\nSo I decided to use tab directly in the sed expression.  I cannot think\nof any portable way to avoid grep completely (\"q\" is a GNU sed\nextension, and we want to support BSD, I think), so it's still there,\nlooking for any output from sed.\n\nSigned-off-by: Pavel Roskin <proski@gnu.org>\n\n--- a/gittrack.sh\n+++ b/gittrack.sh\n@@ -35,7 +35,7 @@ die () {\n mkdir -p .git/heads\n \n if [ \"$name\" ]; then\n-\tgrep -q $(echo -e \"^$name\\t\" | sed 's/\\./\\\\./g') .git/remotes || \\\n+\tsed -ne \"/^$name\\t/p\" .git/remotes | grep -q . || \\\n \t\t[ -s \".git/heads/$name\" ] || \\\n \t\tdie \"unknown branch \\\"$name\\\"\"\n \n\n-- \nRegards,\nPavel Roskin\n\n"},{"id":"1058","messageId":"20050420232110.GA12962@pasky.ji.cz","threadId":"176","inReplyTo":"1114026510.15186.15.camel@dv","subject":"Re: [PATCH] gittrack.sh accepts invalid branch names","fromName":"Petr Baudis","fromEmail":"pasky@ucw.cz","sentAt":"2005-04-20T23:21:10Z","receivedAt":"2005-04-20T23:21:10Z","isPatch":true,"sender":{"key":"pasky@ucw.cz","avatar":"https://avatars.githubusercontent.com/u/18439?v=4"},"body":"Dear diary, on Wed, Apr 20, 2005 at 09:48:30PM CEST, I got a letter\nwhere Pavel Roskin <proski@gnu.org> told me that...\n> --- a/gittrack.sh\n> +++ b/gittrack.sh\n> @@ -35,7 +35,7 @@ die () {\n>  mkdir -p .git/heads\n>  \n>  if [ \"$name\" ]; then\n> -\tgrep -q $(echo -e \"^$name\\t\" | sed 's/\\./\\\\./g') .git/remotes || \\\n> +\tsed -ne \"/^$name\\t/p\" .git/remotes | grep -q . || \\\n>  \t\t[ -s \".git/heads/$name\" ] || \\\n>  \t\tdie \"unknown branch \\\"$name\\\"\"\n\nThis fixes the acceptance, but not the choice.\n\nWhat does the grep -q . exactly do? Just sets error code based on\nwhether the sed output is non-empty? What about [] instead?\n\n-- \n\t\t\t\tPetr \"Pasky\" Baudis\nStuff: http://pasky.or.cz/\nC++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor\n"},{"id":"1059","messageId":"20050420162234.3ccbf23e.pj@sgi.com","threadId":"176","inReplyTo":"1114026510.15186.15.camel@dv","subject":"Re: [PATCH] gittrack.sh accepts invalid branch names","fromName":"Paul Jackson","fromEmail":"pj@sgi.com","sentAt":"2005-04-20T23:22:34Z","receivedAt":"2005-04-20T23:22:34Z","isPatch":true,"sender":{"key":"pj@sgi.com","avatar":null},"body":"Pavel wrote:\n> \tsed -ne \"/^$name\\t/p\" .git/remotes | grep -q .\n\nConsider using the following to look for a match of $name with\nthe first tab separated field of the remotes file (and to avoid\nusing 'grep -q', which is not in all grep's, so far as I know):\n\n\tcut -f1 .git/remotes | grep -Fx \"$name\" >/dev/null\n\n-- \n                  I won't rest till it's the best ...\n                  Programmer, Linux Scalability\n                  Paul Jackson <pj@engr.sgi.com> 1.650.933.1373, 1.925.600.0401\n"},{"id":"1076","messageId":"1114046931.20044.11.camel@dv","threadId":"176","inReplyTo":"20050420232110.GA12962@pasky.ji.cz","subject":"Re: [PATCH] gittrack.sh accepts invalid branch names","fromName":"Pavel Roskin","fromEmail":"proski@gnu.org","sentAt":"2005-04-21T01:28:51Z","receivedAt":"2005-04-21T01:28:51Z","isPatch":true,"sender":{"key":"proski@gnu.org","avatar":null},"body":"Hi, Petr!\n\nOn Thu, 2005-04-21 at 01:21 +0200, Petr Baudis wrote:\n> Dear diary, on Wed, Apr 20, 2005 at 09:48:30PM CEST, I got a letter\n> where Pavel Roskin <proski@gnu.org> told me that...\n> > --- a/gittrack.sh\n> > +++ b/gittrack.sh\n> > @@ -35,7 +35,7 @@ die () {\n> >  mkdir -p .git/heads\n> >  \n> >  if [ \"$name\" ]; then\n> > -\tgrep -q $(echo -e \"^$name\\t\" | sed 's/\\./\\\\./g') .git/remotes || \\\n> > +\tsed -ne \"/^$name\\t/p\" .git/remotes | grep -q . || \\\n> >  \t\t[ -s \".git/heads/$name\" ] || \\\n> >  \t\tdie \"unknown branch \\\"$name\\\"\"\n> \n> This fixes the acceptance, but not the choice.\n> \n> What does the grep -q . exactly do? Just sets error code based on\n> whether the sed output is non-empty?\n\nYes.\n\n>  What about [] instead?\n\nYou'll need another pair of quotes for that:\n\n[ \"$(sed -ne \"/^$name\\t/p\" .git/remotes)\" ]; echo $?\n\nIf I remember correctly from my Autoconf hacking experience, not all\nshells like mixing quotes and command substitution, and even bash\ntreated this differently in different versions.  I can do more research,\nbut it seems just too fragile to me.\n\nAnother thing I remember is that \"case\" would not need quotes.  For some\nhistoric reasons, the expression between \"case\" and \"in\" is subjected to\ncommand substitution, but not word expansion.\n\nSo the patch becomes:\n\n--- a/gittrack.sh\n+++ b/gittrack.sh\n@@ -35,9 +35,11 @@ die () {\n mkdir -p .git/heads\n \n if [ \"$name\" ]; then\n-\tgrep -q $(echo -e \"^$name\\t\" | sed 's/\\./\\\\./g') .git/remotes || \\\n+\tcase x$(sed -ne \"/^$name\\t/p\" .git/remotes) in\n+\tx)\n \t\t[ -s \".git/heads/$name\" ] || \\\n-\t\tdie \"unknown branch \\\"$name\\\"\"\n+\t\tdie \"unknown branch \\\"$name\\\"\" ;;\n+\tesac\n \n \techo $name >.git/tracking\n \nLooks rather ugly for my taste, but just in case:\nSigned-off-by: Pavel Roskin <proski@gnu.org>\n\nBy the way, please check all references to .git/remotes - this bug is\nnot specific to gittrack.sh.\n\n\n-- \nRegards,\nPavel Roskin\n\n"}]}