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

[PATCH 1/4] path.c: introduce strbuf_interpolate_path

From
Rubén Justo <rjusto@gmail.com>
Date
Apr 6, 2024, 14:31 UTC
Message-ID
<eb40f012-e910-49a3-9e3f-2dbab2b169a6@gmail.com>
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
Previous: Rubén JustoNext: Rubén Justo
Message 2 of 9 in “fix a leak with excludes_file”
  1. 0/4 fix a leak with excludes_fileRubén Justo, Apr 6, 2024
  2. 1/4 path.c: introduce strbuf_interpolate_pathRubén Justo, Apr 6, 2024
  3. 2/4 config.c: introduce git_config_strbuf_pathnameRubén Justo, Apr 6, 2024
  4. 3/4 environment.c: convert excludes_file to struct strbufRubén Justo, Apr 6, 2024
  5. 4/4 t7300: mark as leak-freeRubén Justo, Apr 6, 2024
  6. Junio C HamanoApr 6, 2024
  7. Rubén JustoApr 7, 2024
  8. Junio C HamanoApr 8, 2024
  9. Rubén JustoApr 8, 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.