{"thread":{"id":"45394","subject":"[PATCH] Makefile: escape ' in bin-wrappers rule","startedAt":"2017-03-16T06:17:46Z","lastAt":"2017-03-16T11:25:01Z","messageCount":2,"participants":["Devin Lehmacher","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"314213","messageId":"20170316061738.26760-1-lehmacdj@gmail.com","threadId":"45394","inReplyTo":null,"subject":"[PATCH] Makefile: escape ' in bin-wrappers rule","fromName":"Devin Lehmacher","fromEmail":"lehmacdj@gmail.com","sentAt":"2017-03-16T06:17:38Z","receivedAt":"2017-03-16T06:17:46Z","isPatch":true,"sender":{"key":"lehmacdj@gmail.com","avatar":"https://avatars.githubusercontent.com/u/13156983?v=4"},"body":"If the pwd contains a ' escape it as '\\'' so that git will not fail\nwhile building on a path containing '.\n---\n Makefile | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/Makefile b/Makefile\nindex ed68700ac..5cf5d8537 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -2276,7 +2276,7 @@ all:: $(NO_INSTALL)\n bin-wrappers/%: wrap-for-bin.sh\n \t@mkdir -p bin-wrappers\n \t$(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \\\n-\t     -e 's|@@BUILD_DIR@@|$(shell pwd)|' \\\n+\t     -e 's|@@BUILD_DIR@@|$(shell pwd | sed -e \"s/'/'\\\\\\''/g\")|' \\\n \t     -e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%,$(@F))|' < $< > $@ && \\\n \tchmod +x $@\n \n-- \n2.11.0\n\n"},{"id":"314216","messageId":"20170316112447.meosrjddumkjrfiu@sigill.intra.peff.net","threadId":"45394","inReplyTo":"20170316061738.26760-1-lehmacdj@gmail.com","subject":"Re: [PATCH] Makefile: escape ' in bin-wrappers rule","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-16T11:24:47Z","receivedAt":"2017-03-16T11:25:01Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Mar 16, 2017 at 02:17:38AM -0400, Devin Lehmacher wrote:\n\n> If the pwd contains a ' escape it as '\\'' so that git will not fail\n> while building on a path containing '.\n\nI think this only fixes half the problem...\n\n> diff --git a/Makefile b/Makefile\n> index ed68700ac..5cf5d8537 100644\n> --- a/Makefile\n> +++ b/Makefile\n> @@ -2276,7 +2276,7 @@ all:: $(NO_INSTALL)\n>  bin-wrappers/%: wrap-for-bin.sh\n>  \t@mkdir -p bin-wrappers\n>  \t$(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \\\n> -\t     -e 's|@@BUILD_DIR@@|$(shell pwd)|' \\\n> +\t     -e 's|@@BUILD_DIR@@|$(shell pwd | sed -e \"s/'/'\\\\\\''/g\")|' \\\n\nThis escapes the single-quote against the shell run by make. But it does\na blind text substitution inside the wrap-for-bin script itself, which\nmeans it's subject to another round of expansion. E.g.:\n\n  $ git clone git foo\\'git\n  $ cd foo\\'git\n  $ make bin-wrappers/git\n  GIT_VERSION = 2.12.0.623.g86ec6c963\n  /bin/sh: 4: Syntax error: Unterminated quoted string\n  Makefile:2296: recipe for target 'bin-wrappers/git' failed\n  make: *** [bin-wrappers/git] Error 2\n\n  $ git am /your/patch\n  $ make bin-wrappers/git\n  GIT_VERSION = 2.12.0.624.gc9787fbf0\n      GEN bin-wrappers/git\n  $ bin-wrappers/git\n  bin-wrappers/git: 16: bin-wrappers/git: Syntax error: \"fi\" unexpected\n\nSo you'd need an extra layer of quoting. And then to top it all off,\npassing backslashes through sed requires _another_ layer of quoting.\nSomething like this:\n\ndiff --git a/Makefile b/Makefile\nindex 17e381f0c..9c1357a77 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -2292,10 +2292,15 @@ test_bindir_programs := $(patsubst %,bin-wrappers/%,$(BINDIR_PROGRAMS_NEED_X) $(\n all:: $(TEST_PROGRAMS) $(test_bindir_programs)\n all:: $(NO_INSTALL)\n \n+PWD = $(shell pwd)\n+PWD_SQ = $(subst ','\\'',$(PWD))\n+PWD_SQ_SED = $(subst \\,\\\\,$(PWD_SQ))\n+PWD_SQ_SED_SQ = $(subst ','\\'',$(PWD_SQ_SED))\n+\n bin-wrappers/%: wrap-for-bin.sh\n \t@mkdir -p bin-wrappers\n \t$(QUIET_GEN)sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \\\n-\t     -e 's|@@BUILD_DIR@@|$(shell pwd)|' \\\n+\t     -e 's|@@BUILD_DIR@@|$(PWD_SQ_SED_SQ)|' \\\n \t     -e 's|@@PROG@@|$(patsubst test-%,t/helper/test-%,$(@F))|' < $< > $@ && \\\n \tchmod +x $@\n \n\nThat at least gets me as far as building a runnable git. I wouldn't be\nsurprised if there are other gotchas, though. Running \"make test\" dies\nin t0060 due to similar substitutions.\n\nI guess if somebody is really determined they can try to fix every spot.\nBut at some point I wonder if the answer is \"if it hurts, don't do it\".\n\n-Peff\n"}]}