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

[PATCH 3/6] checkout-index: handle "--no-prefix" option

From
Jeff King <peff@peff.net>
Date
Jan 31, 2016, 11:26 UTC
Message-ID
<20160131112615.GC5116@sigill.intra.peff.net>
In-Reply-To
<20160131112215.GA4589@sigill.intra.peff.net>

We use a custom callback to parse "--prefix", but it does not handle the "unset" case. As a result, passing "--no-prefix" will cause a segfault.

We can fix this by switching it to an OPT_STRING, which makes "--no-prefix" counteract a previous "--prefix". Note that this assigns NULL, so we bump our default-case initialization to lower in the main function.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/checkout-index.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
diff --git a/builtin/checkout-index.c b/builtin/checkout-index.c
index 3b913d1..43bedde 100644
--- a/builtin/checkout-index.c
+++ b/builtin/checkout-index.c
@@ -142,14 +142,6 @@ static int option_parse_u(const struct option *opt,
 	return 0;
 }
 
-static int option_parse_prefix(const struct option *opt,
-			       const char *arg, int unset)
-{
-	state.base_dir = arg;
-	state.base_dir_len = strlen(arg);
-	return 0;
-}
-
 static int option_parse_stage(const struct option *opt,
 			      const char *arg, int unset)
 {
@@ -191,9 +183,8 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 			N_("read list of paths from the standard input")),
 		OPT_BOOL(0, "temp", &to_tempfile,
 			N_("write the content to temporary files")),
-		OPT_CALLBACK(0, "prefix", NULL, N_("string"),
-			N_("when creating files, prepend <string>"),
-			option_parse_prefix),
+		OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
+			N_("when creating files, prepend <string>")),
 		OPT_CALLBACK(0, "stage", NULL, NULL,
 			N_("copy out the files from named stage"),
 			option_parse_stage),
@@ -204,7 +195,6 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 		usage_with_options(builtin_checkout_index_usage,
 				   builtin_checkout_index_options);
 	git_config(git_default_config, NULL);
-	state.base_dir = "";
 	prefix_length = prefix ? strlen(prefix) : 0;
 
 	if (read_cache() < 0) {
@@ -217,6 +207,10 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
 	state.quiet = quiet;
 	state.not_new = not_new;
 
+	if (!state.base_dir)
+		state.base_dir = "";
+	state.base_dir_len = strlen(state.base_dir);
+
 	if (state.base_dir_len || to_tempfile) {
 		/* when --prefix is specified we do not
 		 * want to update cache.
-- 
2.7.0.489.g6faad84
Previous: Jeff KingNext: Jeff King
Message 7 of 19 in “post-strbuf_getline cleanups”
  1. 0/6 post-strbuf_getline cleanupsJeff King, Jan 31, 2016
  2. 1/6 give "nbuf" strbuf a more meaningful nameJeff King, Jan 31, 2016
  3. Johannes SchindelinJan 31, 2016
  4. Jeff KingJan 31, 2016
  5. Johannes SchindelinJan 31, 2016
  6. 2/6 checkout-index: simplify "-z" option parsingJeff King, Jan 31, 2016
  7. 3/6 checkout-index: handle "--no-prefix" optionJeff King, Jan 31, 2016
  8. 4/6 checkout-index: handle "--no-index" optionJeff King, Jan 31, 2016
  9. Junio C HamanoFeb 1, 2016
  10. Jeff KingFeb 1, 2016
  11. 5/6 checkout-index: disallow "--no-stage" optionJeff King, Jan 31, 2016
  12. Junio C HamanoFeb 1, 2016
  13. Jeff KingFeb 1, 2016
  14. 6/6 apply, ls-files: simplify "-z" parsingJeff King, Jan 31, 2016
  15. Johannes SchindelinJan 31, 2016
  16. Junio C HamanoFeb 1, 2016
  17. Junio C HamanoFeb 1, 2016
  18. Jeff KingFeb 2, 2016
  19. Junio C HamanoFeb 1, 2016

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.