git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Rubén Justo" <rjusto@gmail.com>
To: Git List <git@vger.kernel.org>
Subject: [PATCH 1/4] path.c: introduce strbuf_interpolate_path
Date: Sat, 6 Apr 2024 16:31:56 +0200	[thread overview]
Message-ID: <eb40f012-e910-49a3-9e3f-2dbab2b169a6@gmail.com> (raw)
In-Reply-To: <f4af1e88-5bd9-4b3c-8691-84dbf0ca3ee2@gmail.com>

Factorize interpolate_path to have a similar function that uses a
strbuf, instead of allocating a new string, to return the interpolated
path.

It will allow us to avoid some allocs and also some frees, which we will
take advantage of in the next commits.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 path.c | 20 ++++++++++++++------
 path.h |  1 +
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/path.c b/path.c
index 8bb223c92c..7a1a1a9bc0 100644
--- a/path.c
+++ b/path.c
@@ -737,8 +737,16 @@ static struct passwd *getpw_str(const char *username, size_t len)
 char *interpolate_path(const char *path, int real_home)
 {
 	struct strbuf user_path = STRBUF_INIT;
+
+	return strbuf_interpolate_path(path, real_home, &user_path);
+}
+
+char *strbuf_interpolate_path(const char *path, int real_home, struct strbuf* dst)
+{
 	const char *to_copy = path;
 
+	strbuf_reset(dst);
+
 	if (!path)
 		goto return_null;
 
@@ -754,9 +762,9 @@ char *interpolate_path(const char *path, int real_home)
 			if (!home)
 				goto return_null;
 			if (real_home)
-				strbuf_add_real_path(&user_path, home);
+				strbuf_add_real_path(dst, home);
 			else
-				strbuf_addstr(&user_path, home);
+				strbuf_addstr(dst, home);
 #ifdef GIT_WINDOWS_NATIVE
 			convert_slashes(user_path.buf);
 #endif
@@ -764,14 +772,14 @@ char *interpolate_path(const char *path, int real_home)
 			struct passwd *pw = getpw_str(username, username_len);
 			if (!pw)
 				goto return_null;
-			strbuf_addstr(&user_path, pw->pw_dir);
+			strbuf_addstr(dst, pw->pw_dir);
 		}
 		to_copy = first_slash;
 	}
-	strbuf_addstr(&user_path, to_copy);
-	return strbuf_detach(&user_path, NULL);
+	strbuf_addstr(dst, to_copy);
+	return dst->buf;
 return_null:
-	strbuf_release(&user_path);
+	strbuf_release(dst);
 	return NULL;
 }
 
diff --git a/path.h b/path.h
index e053effef2..da7e5384a3 100644
--- a/path.h
+++ b/path.h
@@ -185,6 +185,7 @@ int calc_shared_perm(int mode);
 int adjust_shared_perm(const char *path);
 
 char *interpolate_path(const char *path, int real_home);
+char *strbuf_interpolate_path(const char *path, int real_home, struct strbuf *dst);
 const char *enter_repo(const char *path, int strict);
 const char *remove_leading_path(const char *in, const char *prefix);
 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
-- 
2.44.0.697.g9b33b46f29


  reply	other threads:[~2024-04-06 14:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-06 14:29 [PATCH 0/4] fix a leak with excludes_file Rubén Justo
2024-04-06 14:31 ` Rubén Justo [this message]
2024-04-06 14:32 ` [PATCH 2/4] config.c: introduce git_config_strbuf_pathname Rubén Justo
2024-04-06 14:32 ` [PATCH 3/4] environment.c: convert excludes_file to struct strbuf Rubén Justo
2024-04-06 14:32 ` [PATCH 4/4] t7300: mark as leak-free Rubén Justo
2024-04-06 17:53 ` [PATCH 0/4] fix a leak with excludes_file Junio C Hamano
2024-04-07 17:48   ` Rubén Justo
2024-04-08 17:36     ` Junio C Hamano
2024-04-08 19:33       ` Rubén Justo

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=eb40f012-e910-49a3-9e3f-2dbab2b169a6@gmail.com \
    --to=rjusto@gmail.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).