git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 3/2] setup: use a single return path in setup_git_directory*()
Date: Sat, 20 Jul 2024 15:09:14 -0700	[thread overview]
Message-ID: <20240720220915.2933266-4-gitster@pobox.com> (raw)
In-Reply-To: <20240720220915.2933266-1-gitster@pobox.com>

[Do not use. For illustration purposes only]

Instead of sprinkling "return" all over the place, use the "assign
to the result variable and then jump to the single label set up to
leave the function" pattern, so that we can clean up any extra
resource allocated before returning at a single place.

No functional change is intended with this step, but it will be used
soon.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 setup.c | 50 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 18 deletions(-)

diff --git a/setup.c b/setup.c
index 29304d7452..29e23a905c 100644
--- a/setup.c
+++ b/setup.c
@@ -1416,6 +1416,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
 	int ceil_offset = -1, min_offset = offset_1st_component(dir->buf);
 	dev_t current_device = 0;
 	int one_filesystem = 1;
+	enum discovery_result result;
 
 	/*
 	 * If GIT_DIR is set explicitly, we're not going
@@ -1425,7 +1426,8 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
 	gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
 	if (gitdirenv) {
 		strbuf_addstr(gitdir, gitdirenv);
-		return GIT_DIR_EXPLICIT;
+		result = GIT_DIR_EXPLICIT;
+		goto cleanup_and_return;
 	}
 
 	if (env_ceiling_dirs) {
@@ -1479,8 +1481,10 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
 					gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
 					gitdir_path = xstrdup(dir->buf);
 				}
-			} else if (error_code != READ_GITFILE_ERR_STAT_FAILED)
-				return GIT_DIR_INVALID_GITFILE;
+			} else if (error_code != READ_GITFILE_ERR_STAT_FAILED) {
+				result = GIT_DIR_INVALID_GITFILE;
+				goto cleanup_and_return;
+			}
 		} else
 			gitfile = xstrdup(dir->buf);
 		/*
@@ -1491,16 +1495,15 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
 		 */
 		strbuf_setlen(dir, offset);
 		if (gitdirenv) {
-			enum discovery_result ret;
 			const char *gitdir_candidate =
 				gitdir_path ? gitdir_path : gitdirenv;
 
 			if (ensure_valid_ownership(gitfile, dir->buf,
 						   gitdir_candidate, report)) {
 				strbuf_addstr(gitdir, gitdirenv);
-				ret = GIT_DIR_DISCOVERED;
+				result = GIT_DIR_DISCOVERED;
 			} else
-				ret = GIT_DIR_INVALID_OWNERSHIP;
+				result = GIT_DIR_INVALID_OWNERSHIP;
 
 			/*
 			 * Earlier, during discovery, we might have allocated
@@ -1514,8 +1517,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
 			 */
 			free(gitdir_path);
 			free(gitfile);
-
-			return ret;
+			goto cleanup_and_return;
 		}
 
 		if (is_git_directory(dir->buf)) {
@@ -1523,25 +1525,37 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
 			if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT &&
 			    !is_implicit_bare_repo(dir->buf))
 				return GIT_DIR_DISALLOWED_BARE;
-			if (!ensure_valid_ownership(NULL, NULL, dir->buf, report))
-				return GIT_DIR_INVALID_OWNERSHIP;
-			strbuf_addstr(gitdir, ".");
-			return GIT_DIR_BARE;
+			if (!ensure_valid_ownership(NULL, NULL, dir->buf, report)) {
+				result = GIT_DIR_INVALID_OWNERSHIP;
+			} else {
+				strbuf_addstr(gitdir, ".");
+				result = GIT_DIR_BARE;
+			}
+			goto cleanup_and_return;
 		}
 
-		if (offset <= min_offset)
-			return GIT_DIR_HIT_CEILING;
+		if (offset <= min_offset) {
+			result = GIT_DIR_HIT_CEILING;
+			goto cleanup_and_return;
+		}
 
 		while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset]))
 			; /* continue */
-		if (offset <= ceil_offset)
-			return GIT_DIR_HIT_CEILING;
+		if (offset <= ceil_offset) {
+			result = GIT_DIR_HIT_CEILING;
+			goto cleanup_and_return;
+		}
 
 		strbuf_setlen(dir, offset > min_offset ?  offset : min_offset);
 		if (one_filesystem &&
-		    current_device != get_device_or_die(dir->buf, NULL, offset))
-			return GIT_DIR_HIT_MOUNT_POINT;
+		    current_device != get_device_or_die(dir->buf, NULL, offset)) {
+			result = GIT_DIR_HIT_MOUNT_POINT;
+			goto cleanup_and_return;
+		}
 	}
+
+cleanup_and_return:
+	return result;
 }
 
 enum discovery_result discover_git_directory_reason(struct strbuf *commondir,
-- 
2.46.0-rc1-48-g0900f1888e


  parent reply	other threads:[~2024-07-20 22:09 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-20 22:09 [PATCH 0/2] safe.directory clean-up Junio C Hamano
2024-07-20 22:09 ` [PATCH 1/2] safe.directory: normalize the checked path Junio C Hamano
2024-07-20 22:09 ` [PATCH 2/2] safe.directory: normalize the configured path Junio C Hamano
2024-07-20 22:09 ` Junio C Hamano [this message]
2024-07-20 22:09 ` [PATCH 4/2] setup: cache normalized safe.directory configuration Junio C Hamano
2024-07-23  2:18 ` [PATCH v2 0/3] safe.directory clean-up Junio C Hamano
2024-07-23  2:18   ` [PATCH v2 1/3] safe.directory: normalize the checked path Junio C Hamano
2024-07-23  2:18   ` [PATCH v2 2/3] safe.directory: normalize the configured path Junio C Hamano
2024-07-25  9:45     ` Phillip Wood
2024-07-25 16:11       ` Junio C Hamano
2024-08-14 13:20         ` Phillip Wood
2024-08-14 17:15           ` Junio C Hamano
2024-08-15  9:51             ` Phillip Wood
2024-08-15 14:43               ` Junio C Hamano
2024-07-26  5:02     ` Jeff King
2024-07-26 15:02       ` Junio C Hamano
2024-07-27 22:05         ` Jeff King
2024-07-23  2:19   ` [PATCH v2 3/3] safe.directory: setting safe.directory="." allows the "current" directory Junio C Hamano
2024-07-25  9:45     ` Phillip Wood
2024-07-25 16:12       ` Junio C Hamano
2024-07-25  9:45   ` [PATCH v2 0/3] safe.directory clean-up Phillip Wood
2024-07-25 16:14     ` Junio C Hamano
2024-07-30  1:10 ` [PATCH v3 " Junio C Hamano
2024-07-30  1:10   ` [PATCH v3 1/3] safe.directory: normalize the checked path Junio C Hamano
2024-07-30  1:10   ` [PATCH v3 2/3] safe.directory: normalize the configured path Junio C Hamano
2024-07-30  7:31     ` Jeff King
2024-07-30 16:03       ` Junio C Hamano
2024-07-30 20:08         ` Jeff King
2024-07-30  7:43     ` Jeff King
2024-07-30 16:22       ` Junio C Hamano
2024-07-30 17:56         ` safe.directory: preliminary clean-up Junio C Hamano
2024-07-30 20:13           ` Jeff King
2024-07-30 20:10         ` [PATCH v3 2/3] safe.directory: normalize the configured path Jeff King
2024-07-30  1:10   ` [PATCH v3 3/3] safe.directory: setting safe.directory="." allows the "current" directory Junio C Hamano
2024-07-30 18:43 ` [PATCH v4 0/4] safe.directory clean-up Junio C Hamano
2024-07-30 18:43   ` [PATCH v4 1/4] safe.directory: preliminary clean-up Junio C Hamano
2024-07-30 18:43   ` [PATCH v4 2/4] safe.directory: normalize the checked path Junio C Hamano
2024-07-30 18:43   ` [PATCH v4 3/4] safe.directory: normalize the configured path Junio C Hamano
2024-07-30 18:43   ` [PATCH v4 4/4] safe.directory: setting safe.directory="." allows the "current" directory Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240720220915.2933266-4-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).