git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] rev-parse --parseopt: fix handling of optional arguments

From
Nicolas Vigier <boklm@mars-attacks.org>
Date
Oct 15, 2013, 12:00 UTC
Message-ID
<1381838425-18244-1-git-send-email-boklm@mars-attacks.org>

git rev-parse --parseopt does not allow us to see the difference between an option with an optional argument starting with a dash, and an option with an unset optional argument followed by an other option.

If I use this script :
  $ cat /tmp/opt.sh
  #!/bin/sh
  OPTIONS_SPEC="\
  git [options]
  --
  q,quiet         be quiet
  S,gpg-sign?     GPG-sign commit"
  echo "$OPTIONS_SPEC" | git rev-parse --parseopt $parseopt_extra -- "$@"
Then the following two commands give us the same result :
  $ /tmp/opt.sh -S -q
  set -- -S -q --
  $ /tmp/opt.sh -S-q
  set -- -S '-q' --
We cannot know if '-q' is an argument to '-S' or a new option.

With this patch, rev-parse --parseopt will always give an argument to optional options, as an empty string if the argument is unset.

The same two commands now give us :
  $ /tmp/opt.sh -S -q
  set -- -S '' -q --
  $ /tmp/opt.sh -S-q
  set -- -S '-q' --
We can now see if '-q' is an argument to '-S' or an other option.
Also adding two tests in t1502.

There does not seem to be any shell script git command included in git sources tree that is currently using optional arguments and could be affected by this change.

Signed-off-by: Nicolas Vigier <boklm@mars-attacks.org>
---
 builtin/rev-parse.c           |  3 +++
 t/t1502-rev-parse-parseopt.sh | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index de894c7..25e8c74 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -327,6 +327,9 @@ static int parseopt_dump(const struct option *o, const char *arg, int unset)
 	if (arg) {
 		strbuf_addch(parsed, ' ');
 		sq_quote_buf(parsed, arg);
+	} else if (o->flags & PARSE_OPT_OPTARG) {
+		const char empty_arg[] = " ''";
+		strbuf_add(parsed, empty_arg, strlen(empty_arg));
 	}
 	return 0;
 }
diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh
index 13c88c9..abe7c2f 100755
--- a/t/t1502-rev-parse-parseopt.sh
+++ b/t/t1502-rev-parse-parseopt.sh
@@ -99,4 +99,22 @@ test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option withou
 	test_cmp expect output
 '
 
+cat > expect <<EOF
+set -- -C '' --foo --
+EOF
+
+test_expect_success 'test --parseopt -C --foo' '
+	git rev-parse --parseopt -- -C --foo <optionspec >output &&
+	test_cmp expect output
+'
+
+cat > expect <<EOF
+set -- -C '--foo' --
+EOF
+
+test_expect_success 'test --parseopt -C--foo' '
+	git rev-parse --parseopt -- -C--foo <optionspec >output &&
+	test_cmp expect output
+'
+
 test_done
-- 
1.8.4
Next: Junio C Hamano
Message 1 of 25 in “rev-parse --parseopt: fix handling of optional arguments”
  1. rev-parse --parseopt: fix handling of optional argumentsNicolas Vigier, Oct 15, 2013
  2. Junio C HamanoOct 15, 2013
  3. Nicolas VigierOct 15, 2013
  4. Jonathan NiederOct 15, 2013
  5. Junio C HamanoOct 15, 2013
  6. Jonathan NiederOct 15, 2013
  7. Johannes SixtOct 16, 2013
  8. Jeff KingOct 16, 2013
  9. Junio C HamanoOct 16, 2013
  10. Jeff KingOct 16, 2013
  11. Nicolas VigierOct 16, 2013
  12. Nicolas VigierOct 16, 2013
  13. Jonathan NiederOct 16, 2013
  14. rev-parse --parseopt: add the --sticked-long modeNicolas Vigier, Oct 25, 2013
  15. Junio C HamanoOct 25, 2013
  16. Nicolas VigierOct 25, 2013
  17. Junio C HamanoOct 25, 2013
  18. Philip OakleyOct 26, 2013
  19. Junio C HamanoOct 28, 2013
  20. sticked -> stuckNicolas Vigier, Oct 31, 2013
  21. 1/2 Use the word 'stuck' instead of 'sticked'Nicolas Vigier, Oct 31, 2013
  22. Junio C HamanoOct 31, 2013
  23. 2/2 rev-parse --parseopt: add the --stuck-long modeNicolas Vigier, Oct 31, 2013
  24. Michael HaggertyOct 27, 2013
  25. Nicolas VigierOct 15, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.