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

[PATCH 1/3] rev-parse: use skip_prefix when parsing options

From
Jeff King <peff@peff.net>
Date
Mar 15, 2017, 20:05 UTC
Message-ID
<20170315200535.rdno2keqnynfzmyj@sigill.intra.peff.net>
In-Reply-To
<20170315200521.qwcm5tqcdzkbwp3x@sigill.intra.peff.net>

Using skip_prefix lets us avoid manually-counted offsets into the argument string. This patch converts the simple and obvious cases.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/rev-parse.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index e08677e55..836ccf00e 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -722,8 +722,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				for_each_ref(show_reference, NULL);
 				continue;
 			}
-			if (starts_with(arg, "--disambiguate=")) {
-				for_each_abbrev(arg + 15, show_abbrev, NULL);
+			if (skip_prefix(arg, "--disambiguate=", &arg)) {
+				for_each_abbrev(arg, show_abbrev, NULL);
 				continue;
 			}
 			if (!strcmp(arg, "--bisect")) {
@@ -731,8 +731,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				for_each_ref_in("refs/bisect/good", anti_reference, NULL);
 				continue;
 			}
-			if (starts_with(arg, "--branches=")) {
-				for_each_glob_ref_in(show_reference, arg + 11,
+			if (skip_prefix(arg, "--branches=", &arg)) {
+				for_each_glob_ref_in(show_reference, arg,
 					"refs/heads/", NULL);
 				clear_ref_exclusion(&ref_excludes);
 				continue;
@@ -742,8 +742,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				clear_ref_exclusion(&ref_excludes);
 				continue;
 			}
-			if (starts_with(arg, "--tags=")) {
-				for_each_glob_ref_in(show_reference, arg + 7,
+			if (skip_prefix(arg, "--tags=", &arg)) {
+				for_each_glob_ref_in(show_reference, arg,
 					"refs/tags/", NULL);
 				clear_ref_exclusion(&ref_excludes);
 				continue;
@@ -753,13 +753,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				clear_ref_exclusion(&ref_excludes);
 				continue;
 			}
-			if (starts_with(arg, "--glob=")) {
-				for_each_glob_ref(show_reference, arg + 7, NULL);
+			if (skip_prefix(arg, "--glob=", &arg)) {
+				for_each_glob_ref(show_reference, arg, NULL);
 				clear_ref_exclusion(&ref_excludes);
 				continue;
 			}
-			if (starts_with(arg, "--remotes=")) {
-				for_each_glob_ref_in(show_reference, arg + 10,
+			if (skip_prefix(arg, "--remotes=", &arg)) {
+				for_each_glob_ref_in(show_reference, arg,
 					"refs/remotes/", NULL);
 				clear_ref_exclusion(&ref_excludes);
 				continue;
@@ -769,8 +769,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				clear_ref_exclusion(&ref_excludes);
 				continue;
 			}
-			if (starts_with(arg, "--exclude=")) {
-				add_ref_exclusion(&ref_excludes, arg + 10);
+			if (skip_prefix(arg, "--exclude=", &arg)) {
+				add_ref_exclusion(&ref_excludes, arg);
 				continue;
 			}
 			if (!strcmp(arg, "--show-toplevel")) {
@@ -865,20 +865,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				}
 				continue;
 			}
-			if (starts_with(arg, "--since=")) {
-				show_datestring("--max-age=", arg+8);
+			if (skip_prefix(arg, "--since=", &arg)) {
+				show_datestring("--max-age=", arg);
 				continue;
 			}
-			if (starts_with(arg, "--after=")) {
-				show_datestring("--max-age=", arg+8);
+			if (skip_prefix(arg, "--after=", &arg)) {
+				show_datestring("--max-age=", arg);
 				continue;
 			}
-			if (starts_with(arg, "--before=")) {
-				show_datestring("--min-age=", arg+9);
+			if (skip_prefix(arg, "--before=", &arg)) {
+				show_datestring("--min-age=", arg);
 				continue;
 			}
-			if (starts_with(arg, "--until=")) {
-				show_datestring("--min-age=", arg+8);
+			if (skip_prefix(arg, "--until=", &arg)) {
+				show_datestring("--min-age=", arg);
 				continue;
 			}
 			if (show_flag(arg) && verify)
-- 
2.12.0.613.g6e7c52a0d
Previous: Jeff King
Message 6 of 6 in “minor rev-parse option-parsing cleanups”
  1. 0/3 minor rev-parse option-parsing cleanupsJeff King, Mar 15, 2017
  2. 2/3 rev-parse: add helper for parsing "--foo/--foo="Jeff King, Mar 15, 2017
  3. 3/3 rev-parse: simplify parsing of ref optionsJeff King, Mar 15, 2017
  4. Junio C HamanoMar 15, 2017
  5. Jeff KingMar 15, 2017
  6. 1/3 rev-parse: use skip_prefix when parsing optionsJeff King, Mar 15, 2017

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.