Subject: Re: [PATCH 05/23] fsmonitor-settings: bare repos are incompatible with FSMonitor

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Fri, 25 Feb 2022 21:42:09 +0100

On Tue, Feb 15 2022, Jeff Hostetler via GitGitGadget wrote:

> +static void create_reason_message(struct repository *r,
> +				  struct strbuf *buf_reason)
> +{
> +	struct fsmonitor_settings *s = r->settings.fsmonitor;
> +
> +	switch (s->reason) {
> +	case FSMONITOR_REASON_ZERO:
> +		return;
> +
> +	case FSMONITOR_REASON_BARE:
> +		strbuf_addstr(buf_reason,
> +			      _("bare repos are incompatible with fsmonitor"));
> +		return;
> +
> +	default:
> +		BUG("Unhandled case in create_reason_message '%d'", s->reason);
> +	}
> +}
> +
> +enum fsmonitor_reason fsm_settings__get_reason(struct repository *r,
> +					       struct strbuf *buf_reason)
> +{
> +	lookup_fsmonitor_settings(r);
> +
> +	strbuf_reset(buf_reason);
> +	if (r->settings.fsmonitor->mode == FSMONITOR_MODE_INCOMPATIBLE)
> +		create_reason_message(r, buf_reason);
> +
> +	return r->settings.fsmonitor->reason;
> +}

This API (just looking at one small bit discussed because related bits
conflict with another series) seems to require a lot of ceremony just to
get a const char * error.

I tried this on top of "seen", and the parts I compile on Linux (so not
the fsmonitor--daemon.c) were happy with it.

 builtin/fsmonitor--daemon.c | 14 +++++++-------
 builtin/update-index.c      |  9 ++++-----
 fsmonitor-settings.c        | 47 +++++++++++++++------------------------------
 fsmonitor-settings.h        |  5 +++--
 4 files changed, 29 insertions(+), 46 deletions(-)

diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 591433e897d..7ad7bc718b3 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -1496,7 +1496,7 @@ int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix)
 {
 	const char *subcmd;
 	int free_console = 0;
-
+	enum fsmonitor_mode fsm_mode;
 	struct option options[] = {
 		OPT_BOOL(0, "free-console", &free_console, N_("free console")),
 		OPT_INTEGER(0, "ipc-threads",
@@ -1524,12 +1524,12 @@ int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix)
 	prepare_repo_settings(the_repository);
 	fsm_settings__set_ipc(the_repository);
 
-	if (fsm_settings__get_mode(the_repository) == FSMONITOR_MODE_INCOMPATIBLE) {
-		struct strbuf buf_reason = STRBUF_INIT;
-		fsm_settings__get_reason(the_repository, &buf_reason);
-		error("%s '%s'", buf_reason.buf, xgetcwd());
-		strbuf_release(&buf_reason);
-		return -1;
+	fsm_mode = fsm_settings__get_mode(the_repository);
+	if (fsm_mode == FSMONITOR_MODE_INCOMPATIBLE) {
+		enum fsmonitor_reason fsm_reason = fsm_settings__get_reason(the_repository);
+		const char *reason = fsm_settings_incompatible_reason_msg(fsm_mode, fsm_reason);
+
+		return error("%s", reason ? reason : "???");
 	}
 
 	if (!strcmp(subcmd, "start"))
diff --git a/builtin/update-index.c b/builtin/update-index.c
index 61b0b98ccaf..f8f638d33d9 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -1237,13 +1237,12 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 
 	if (fsmonitor > 0) {
 		enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
+		enum fsmonitor_reason fsm_reason = fsm_settings__get_reason(r);
 
 		if (fsm_mode == FSMONITOR_MODE_INCOMPATIBLE) {
-			struct strbuf buf_reason = STRBUF_INIT;
-			fsm_settings__get_reason(r, &buf_reason);
-			error("%s", buf_reason.buf);
-			strbuf_release(&buf_reason);
-			return -1;
+			const char *reason = fsm_settings_incompatible_reason_msg(fsm_mode, fsm_reason);
+
+			return error("%s", reason ? reason : "???");
 		}
 
 		if (fsm_mode == FSMONITOR_MODE_DISABLED) {
diff --git a/fsmonitor-settings.c b/fsmonitor-settings.c
index de69ace246a..e37b342aa2b 100644
--- a/fsmonitor-settings.c
+++ b/fsmonitor-settings.c
@@ -103,6 +103,13 @@ enum fsmonitor_mode fsm_settings__get_mode(struct repository *r)
 	return r->settings.fsmonitor->mode;
 }
 
+enum fsmonitor_reason fsm_settings__get_reason(struct repository *r)
+{
+	lookup_fsmonitor_settings(r);
+
+	return r->settings.fsmonitor->reason;
+}
+
 const char *fsm_settings__get_hook_path(struct repository *r)
 {
 	lookup_fsmonitor_settings(r);
@@ -142,43 +149,19 @@ void fsm_settings__set_disabled(struct repository *r)
 	FREE_AND_NULL(r->settings.fsmonitor->hook_path);
 }
 
-static void create_reason_message(struct repository *r,
-				  struct strbuf *buf_reason)
+const char *fsm_settings_incompatible_reason_msg(enum fsmonitor_mode mode,
+						 enum fsmonitor_reason reason)
 {
-	struct fsmonitor_settings *s = r->settings.fsmonitor;
+	assert(mode == FSMONITOR_MODE_INCOMPATIBLE);
 
-	switch (s->reason) {
+	switch (reason) {
 	case FSMONITOR_REASON_ZERO:
-		return;
-
+		return NULL;
 	case FSMONITOR_REASON_BARE:
-		strbuf_addstr(buf_reason,
-			      _("bare repos are incompatible with fsmonitor"));
-		return;
-
+		return _("bare repos are incompatible with fsmonitor");
 	case FSMONITOR_REASON_VIRTUAL:
-		strbuf_addstr(buf_reason,
-			      _("virtual repos are incompatible with fsmonitor"));
-		return;
-
+		return _("virtual repos are incompatible with fsmonitor");
 	case FSMONITOR_REASON_REMOTE:
-		strbuf_addstr(buf_reason,
-			      _("remote repos are incompatible with fsmonitor"));
-		return;
-
-	default:
-		BUG("Unhandled case in create_reason_message '%d'", s->reason);
+		return _("remote repos are incompatible with fsmonitor");
 	}
 }
-
-enum fsmonitor_reason fsm_settings__get_reason(struct repository *r,
-					       struct strbuf *buf_reason)
-{
-	lookup_fsmonitor_settings(r);
-
-	strbuf_reset(buf_reason);
-	if (r->settings.fsmonitor->mode == FSMONITOR_MODE_INCOMPATIBLE)
-		create_reason_message(r, buf_reason);
-
-	return r->settings.fsmonitor->reason;
-}
diff --git a/fsmonitor-settings.h b/fsmonitor-settings.h
index fca25887c0f..81be1ef1801 100644
--- a/fsmonitor-settings.h
+++ b/fsmonitor-settings.h
@@ -25,9 +25,10 @@ void fsm_settings__set_hook(struct repository *r, const char *path);
 void fsm_settings__set_disabled(struct repository *r);
 
 enum fsmonitor_mode fsm_settings__get_mode(struct repository *r);
+enum fsmonitor_reason fsm_settings__get_reason(struct repository *r);
 const char *fsm_settings__get_hook_path(struct repository *r);
-enum fsmonitor_reason fsm_settings__get_reason(struct repository *r,
-					       struct strbuf *buf_reason);
+const char *fsm_settings_incompatible_reason_msg(enum fsmonitor_mode mode,
+						 enum fsmonitor_reason reason);
 
 struct fsmonitor_settings;