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

[PATCH 1/2] status: unify parsing of --untracked= and status.showUntrackedFiles

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 13, 2024, 17:32 UTC
Message-ID
<20240313173214.962532-2-gitster@pobox.com>
In-Reply-To
<20240313173214.962532-1-gitster@pobox.com>

There are two code paths that take a string and parse it to enum untracked_status_type. Introduce a helper function and use it.

As these two places handle an error differently, add an additional invalid value to the enum, and have the caller of the helper handle the error condition, instead of dying or emitting error message from the helper.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/commit.c | 44 +++++++++++++++++++++++++++-----------------
 wt-status.h      |  3 ++-
 2 files changed, 29 insertions(+), 18 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index a91197245f..e61e459ca9 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1157,22 +1157,34 @@ static void handle_ignored_arg(struct wt_status *s)
 		die(_("Invalid ignored mode '%s'"), ignored_arg);
 }
 
-static void handle_untracked_files_arg(struct wt_status *s)
+static enum untracked_status_type parse_untracked_setting_name(const char *u)
 {
-	if (!untracked_files_arg)
-		; /* default already initialized */
-	else if (!strcmp(untracked_files_arg, "no"))
-		s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
-	else if (!strcmp(untracked_files_arg, "normal"))
-		s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
-	else if (!strcmp(untracked_files_arg, "all"))
-		s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
 	/*
 	 * Please update $__git_untracked_file_modes in
 	 * git-completion.bash when you add new options
 	 */
+	if (!strcmp(u, "no"))
+		return SHOW_NO_UNTRACKED_FILES;
+	else if (!strcmp(u, "normal"))
+		return SHOW_NORMAL_UNTRACKED_FILES;
+	else if (!strcmp(u, "all"))
+		return SHOW_ALL_UNTRACKED_FILES;
 	else
-		die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
+		return SHOW_UNTRACKED_FILES_ERROR;
+}
+
+static void handle_untracked_files_arg(struct wt_status *s)
+{
+	enum untracked_status_type u;
+
+	if (!untracked_files_arg)
+		return; /* default already initialized */
+
+	u = parse_untracked_setting_name(untracked_files_arg);
+	if (u == SHOW_UNTRACKED_FILES_ERROR)
+		die(_("Invalid untracked files mode '%s'"),
+		    untracked_files_arg);
+	s->show_untracked_files = u;
 }
 
 static const char *read_commit_message(const char *name)
@@ -1455,16 +1467,14 @@ static int git_status_config(const char *k, const char *v,
 		return 0;
 	}
 	if (!strcmp(k, "status.showuntrackedfiles")) {
+		enum untracked_status_type u;
+
 		if (!v)
 			return config_error_nonbool(k);
-		else if (!strcmp(v, "no"))
-			s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
-		else if (!strcmp(v, "normal"))
-			s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
-		else if (!strcmp(v, "all"))
-			s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
-		else
+		u = parse_untracked_setting_name(v);
+		if (u == SHOW_UNTRACKED_FILES_ERROR)
 			return error(_("Invalid untracked files mode '%s'"), v);
+		s->show_untracked_files = u;
 		return 0;
 	}
 	if (!strcmp(k, "diff.renamelimit")) {
diff --git a/wt-status.h b/wt-status.h
index 5e99ba4707..4e377ce62b 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -23,7 +23,8 @@ enum color_wt_status {
 };
 
 enum untracked_status_type {
-	SHOW_NO_UNTRACKED_FILES,
+	SHOW_UNTRACKED_FILES_ERROR = -1,
+	SHOW_NO_UNTRACKED_FILES = 0,
 	SHOW_NORMAL_UNTRACKED_FILES,
 	SHOW_ALL_UNTRACKED_FILES
 };
-- 
2.44.0-191-g945115026a
Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 4 in “status --untracked=no”
  1. 0/2 status --untracked=noJunio C Hamano, Mar 13, 2024
  2. 1/2 status: unify parsing of --untracked= and status.showUntrackedFilesJunio C Hamano, Mar 13, 2024
  3. 2/2 status: allow --untracked=false and friendsJunio C Hamano, Mar 13, 2024
  4. Kristoffer HaugsbakkMar 13, 2024

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.