Skip to contentSkip to conversation

rev-parse: allow --flags to output rev-parse-like flags

PATCH In review Jon Seymour 2 participants 8 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

v4 cover rev-parse: allow --flags to output rev-parse-like flags

This series allows git rev-parse --flags to output remaining flag-like arguments even if such arguments are valid options to git rev-parse itself.

Previously:

  $ git rev-parse --flags -q -X --no-flags -- Y -Z
  -X
  $

Now:

  $ git rev-parse --flags -q -X --no-flags -- Y -Z
  -q -X --no-flags
  $

This series also changes the interpretation of --flags so that specification of this option implies --no-revs.

Previously:

  $ git rev-parse --flags HEAD
  HEAD
  $

Now:

  $ git rev-parse --flags HEAD
  $

Reviewers attention is drawn to the following behaviour which, while unchanged from current behaviour, differs from the behaviour specified by the documentation. Specifically:

  $ git rev-parse -X --no-flags
  -X
  $ git rev-parse --no-flags -X
  $

In other words, a git rev-parse option only ever affects the interpretation of succeeding options, never preceding options.

Aevar's feedback on v2 of this series has been incorporated.

Jon Seymour (4):
rev-parse: stop interpreting flags as options to rev-parse once
--flags is specified
rev-parse: add tests for git rev-parse --flags.
rev-parse: update documentation of --flags and --no-flags options
rev-parse: make --flags imply --no-revs for remaining arguments.

 Documentation/git-rev-parse.txt |   12 ++++
 builtin/rev-parse.c             |   14 +++++-
 t/t1510-rev-parse-flags.sh      |  109 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 1 deletions(-)
 create mode 100755 t/t1510-rev-parse-flags.sh
Signature · 1 line
1.7.3.3.gc4c52.dirty
#1 lore raw

PATCH v4 1/4 rev-parse: stop interpreting flags as options to rev-parse once --flags is specified

Current git rev-parse behaviour makes --flags hard to use if the remaining arguments to git rev-parse contain an option that would otherwise be interpreted as an option by git rev-parse itself.

So, for example:

  $> git rev-parse --flags -q -X
  -X

Normally one might expect to use -- to prevent -q being interpreted:

  $> git rev-parse --flags -- -q -X
  -q -X

But we can't really use -- in this way, because commands that use git rev-parse might reasonably expect:

  $> git rev-parse --flags -Y -- -q -X
  -Y

That is, -Y to be regarded as a flag but everything after -- to be uninterpreted.

This proposed change modifies git rev-parse so that git rev-parse stops interpreting flag arguments as options to git rev-parse once --flags is interpreted. We also exit early once -- is found.

---
 builtin/rev-parse.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
builtin/rev-parse.c +8 -0
index a5a1c86..2ad269a 100644
@@ -497,8 +497,16 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				/* Pass on the "--" if we show anything but files.. */
 				if (filter & (DO_FLAGS | DO_REVS))
 					show_file(arg);
+				if (!(filter & DO_NONFLAGS)) {
+					return 0;
+				}
 				continue;
 			}
+			if (!(filter & DO_NONFLAGS)) {
+				/* once we see --flags, we stop interpreting other flags */
+				 show_flag(arg);
+				 continue;
+			}
 			if (!strcmp(arg, "--default")) {
 				def = argv[i+1];
 				i++;
Signature · 1 line
1.7.3.3.gc4c52.dirty
#2 lore raw

PATCH v4 2/4 rev-parse: add tests for git rev-parse --flags.

In reply to Jon Seymour
---
 t/t1510-rev-parse-flags.sh |  109 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100755 t/t1510-rev-parse-flags.sh
t/t1510-rev-parse-flags.sh new file +109 -0
new file mode 100755
index 0000000..53002df
@@ -0,0 +1,109 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Jon Seymour
+#
+
+test_description='test git rev-parse --flags'
+. ./test-lib.sh
+
+test_commit "A"
+
+test_expect_success 'git rev-parse --flags -> ""' \
+'
+	: >expected &&
+	git rev-parse --flags >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags X -> ""' \
+'
+	: >expected &&
+	git rev-parse --flags X >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --no-revs --flags HEAD -> ""' \
+'
+	: >expected &&
+	git rev-parse --no-revs --flags HEAD >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --symbolic --flags HEAD -> "HEAD"' \
+'
+	echo HEAD > expected &&
+	git rev-parse --symbolic --flags HEAD >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags -- -> ""' \
+'
+	: >expected &&
+	git rev-parse --flags -- >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags -- X -> ""' \
+'
+	: >expected &&
+	git rev-parse --flags -- X >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags -- -X -> ""' \
+'
+	: >expected &&
+	git rev-parse --flags -- -X >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags -- -q --> ""' \
+'
+	: >expected &&
+	git rev-parse --flags -- -q >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags -X -> "-X"' \
+'
+	echo -X > expected &&
+	git rev-parse --flags -X >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags -q -> "-q"' \
+'
+	echo -q > expected &&
+	git rev-parse --flags -q >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags -X -- Y -Z -> "-X"' \
+'
+	echo -X > expected &&
+	git rev-parse --flags -X -- Y -Z >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --flags --no-flags -> "--no-flags"' \
+'
+	echo --no-flags > expected &&
+	git rev-parse --flags --no-flags >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --no-flags --flags -X -> ""' \
+'
+	: >expected &&
+	git rev-parse --no-flags --flags -X >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git rev-parse --symbolic --no-flags --flags HEAD -> "HEAD"' \
+'
+	echo HEAD >expected &&
+	git rev-parse --symbolic --no-flags --flags HEAD >actual &&
+	test_cmp expected actual
+'
+
+test_done
Signature · 1 line
1.7.3.3.gc4c52.dirty
#3 lore raw

Re: [PATCH v4 2/4] rev-parse: add tests for git rev-parse --flags.

Quoted 1 line · Jon Seymour

+       : >expected &&

We've been dropping the ":>foo" style in favor of ">foo" in other tests. There's no need for the ":".

Quoted 4 lines

+       echo -X > expected &&
+       echo -q > expected &&
+       echo -X > expected &&
+       echo --no-flags > expected &&

Maybe some echo implementations don't like flag-like params, and we need:

    printf "%s\n" "-X"

here. I don't know whether that's the case.

#4 lore raw

Re: [PATCH v4 2/4] rev-parse: add tests for git rev-parse --flags.

Quoted 12 lines · Ævar Arnfjörð Bjarmason
Quoted 1 line · Jon Seymour

+       : >expected &&

We've been dropping the ":>foo" style in favor of ">foo" in other tests. There's no need for the ":".

Quoted 4 lines

+       echo -X > expected &&
+       echo -q > expected &&
+       echo -X > expected &&
+       echo --no-flags > expected &&

Quoted 6 lines

Maybe some echo implementations don't like flag-like params, and we need:

   printf "%s\n" "-X"

here. I don't know whether that's the case.

Thanks for the review.

My latest revision incorporates this feedback, although I didn't use extra quotes around -X. (e.g. I wrote printf "%s\n" -X)

jon.

#5 lore raw

PATCH v4 3/4 rev-parse: update documentation of --flags and --no-flags options

In reply to Jon Seymour
---
 Documentation/git-rev-parse.txt |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
Documentation/git-rev-parse.txt +11 -1
index 341ca90..f5e6637 100644
@@ -49,10 +49,20 @@ OPTIONS
 	'git rev-list' command.
 
 --flags::
-	Do not output non-flag parameters.
+	Do not output non-flag parameters which are not also revisions.
+	+
+	If specified, this option causes 'git rev-parse' to stop
+	interpreting remaining arguments as options for its own
+	consumption. As such, this option should be specified
+	after all other options that 'git rev-parse' is expected
+	to interpret.
 
 --no-flags::
 	Do not output flag parameters.
+	+
+	If both `--flags` and `--no-flags` are specified, the first
+	option specified wins and the other option is treated like
+	a non-option argument.
 
 --default <arg>::
 	If there is no parameter given by the user, use `<arg>`
Signature · 1 line
1.7.3.3.gc4c52.dirty
#6 lore raw

PATCH v4 4/4 rev-parse: make --flags imply --no-revs for remaining arguments.

In reply to Jon Seymour

This change ensures that git rev-parse --flags complies with its documentation, namely:

"Do not output non-flag parameters".

Previously:

   $ git rev-parse --flags HEAD
   <sha1 hash of HEAD>
   $

Now:

   $ git rev-parse --flags HEAD
   $
---
 Documentation/git-rev-parse.txt |   24 +++++++++++++-----------
 builtin/rev-parse.c             |    6 +++++-
 t/t1510-rev-parse-flags.sh      |    2 +-
 3 files changed, 19 insertions(+), 13 deletions(-)
Documentation/git-rev-parse.txt +13 -11
index f5e6637..f26fc7b 100644
@@ -49,20 +49,22 @@ OPTIONS
 	'git rev-list' command.
 
 --flags::
-	Do not output non-flag parameters which are not also revisions.
-	+
-	If specified, this option causes 'git rev-parse' to stop
-	interpreting remaining arguments as options for its own
-	consumption. As such, this option should be specified
-	after all other options that 'git rev-parse' is expected
-	to interpret.
+	Do not output non-flag parameters.
++
+If specified, this option causes 'git rev-parse' to stop
+interpreting remaining arguments as options for its own
+consumption. As such, this option should be specified
+after all other options that 'git rev-parse' is expected
+to interpret.
++
+If `--flags` is specified, `--no-revs` is implied.
 
 --no-flags::
 	Do not output flag parameters.
-	+
-	If both `--flags` and `--no-flags` are specified, the first
-	option specified wins and the other option is treated like
-	a non-option argument.
++
+If both `--flags` and `--no-flags` are specified, the first
+option specified wins and the other option is treated like
+a non-option argument.
 
 --default <arg>::
 	If there is no parameter given by the user, use `<arg>`
builtin/rev-parse.c +5 -1
index 2ad269a..84f9f07 100644
@@ -13,7 +13,7 @@
 #define DO_REVS		1
 #define DO_NOREV	2
 #define DO_FLAGS	4
-#define DO_NONFLAGS	8
+#define DO_NONFLAGS	(8|DO_REVS)
 static int filter = ~0;
 
 static const char *def;
@@ -521,6 +521,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--flags")) {
+				if (!(filter & DO_FLAGS)) {
+					/* prevent --flags being interpreted if --no-flags has been seen */
+					continue;
+				}
 				filter &= ~DO_NONFLAGS;
 				continue;
 			}
t/t1510-rev-parse-flags.sh +1 -1
index 53002df..4dd6228 100755
@@ -31,7 +31,7 @@ test_expect_success 'git rev-parse --no-revs --flags HEAD -> ""' \
 
 test_expect_success 'git rev-parse --symbolic --flags HEAD -> "HEAD"' \
 '
-	echo HEAD > expected &&
+	: > expected &&
 	git rev-parse --symbolic --flags HEAD >actual &&
 	test_cmp expected actual
 '
Signature · 1 line
1.7.3.3.gc4c52.dirty
#7 lore raw

Re: [PATCH v4 4/4] rev-parse: make --flags imply --no-revs for remaining arguments.

Oops - 4/4 breaks --no-revs.

Will issue v5 with guarding test shortly.

jon.

Quoted 99 lines · Jon Seymour

This change ensures that git rev-parse --flags complies with its documentation, namely:

"Do not output non-flag parameters".

Previously:

  $ git rev-parse --flags HEAD
  <sha1 hash of HEAD>
  $

Now:

  $ git rev-parse --flags HEAD
  $

---

Documentation/git-rev-parse.txt |   24 +++++++++++++-----------
builtin/rev-parse.c             |    6 +++++-
t/t1510-rev-parse-flags.sh      |    2 +-
3 files changed, 19 insertions(+), 13 deletions(-)
Documentation/git-rev-parse.txt +13 -14
index f5e6637..f26fc7b 100644
@@ -49,20 +49,22 @@ OPTIONS
   'git rev-list' command.
 
--flags::
-    Do not output non-flag parameters which are not also revisions.
-    +
-    If specified, this option causes 'git rev-parse' to stop
-    interpreting remaining arguments as options for its own
-    consumption. As such, this option should be specified
-    after all other options that 'git rev-parse' is expected
-    to interpret.
+    Do not output non-flag parameters.
++
+If specified, this option causes 'git rev-parse' to stop
+interpreting remaining arguments as options for its own
+consumption. As such, this option should be specified
+after all other options that 'git rev-parse' is expected
+to interpret.
++
+If `--flags` is specified, `--no-revs` is implied.
 
--no-flags::
   Do not output flag parameters.
-    +
-    If both `--flags` and `--no-flags` are specified, the first
-    option specified wins and the other option is treated like
-    a non-option argument.
++
+If both `--flags` and `--no-flags` are specified, the first
+option specified wins and the other option is treated like
+a non-option argument.
 
--default <arg>::
   If there is no parameter given by the user, use `<arg>`
builtin/rev-parse.c +5 -1
index 2ad269a..84f9f07 100644
@@ -13,7 +13,7 @@
 #define DO_REVS        1
 #define DO_NOREV    2
 #define DO_FLAGS    4
-#define DO_NONFLAGS    8
+#define DO_NONFLAGS    (8|DO_REVS)
 static int filter = ~0;
 
 static const char *def;
@@ -521,6 +521,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
               continue;
           }
           if (!strcmp(arg, "--flags")) {
+                if (!(filter & DO_FLAGS)) {
+                    /* prevent --flags being interpreted if --no-flags has been seen */
+                    continue;
+                }
               filter &= ~DO_NONFLAGS;
               continue;
           }
t/t1510-rev-parse-flags.sh +0 -0
index 53002df..4dd6228 100755
@@ -31,7 +31,7 @@ test_expect_success 'git rev-parse --no-revs --flags HEAD -> ""' \
 
 test_expect_success 'git rev-parse --symbolic --flags HEAD -> "HEAD"' \

'

t/t1510-rev-parse-flags.sh +1 -1
-    echo HEAD > expected &&
+    : > expected &&
   git rev-parse --symbolic --flags HEAD >actual &&
   test_cmp expected actual

'

Signature · 1 line
1.7.3.3.gc4c52.dirty