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

Re: [PATCH] Expand ~ and ~user in core.excludesfile, commit.template

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 17, 2009, 06:49 UTC
Message-ID
<7vy6m5hci0.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7v3a4enkzt.fsf@alter.siamese.dyndns.org>

I'd like to squash this to your patch, based on the earlier review comments.

I didn't mention about the first hunk; it is just a style. An opening brace that begins a function body comes at column 1.

Also first_slash and to_copy are made const pointers, as they do not have to touch the region of memory they point to (otherwise you cannot assign path to to_copy without getting warned).

 config.c |    3 ++-
 path.c   |   32 ++++++++++++++------------------
 2 files changed, 16 insertions(+), 19 deletions(-)
diff --git a/config.c b/config.c
index 0fcc4ce..b3d1ff4 100644
--- a/config.c
+++ b/config.c
@@ -351,7 +351,8 @@ int git_config_string(const char **dest, const char *var, const char *value)
 	return 0;
 }
 
-int git_config_pathname(const char **dest, const char *var, const char *value) {
+int git_config_pathname(const char **dest, const char *var, const char *value)
+{
 	if (!value)
 		return config_error_nonbool(var);
 	*dest = expand_user_path(value);
diff --git a/path.c b/path.c
index 009c8e0..2470f78 100644
--- a/path.c
+++ b/path.c
@@ -208,11 +208,8 @@ int validate_headref(const char *path)
 	return -1;
 }
 
-static inline struct passwd *getpw_str(const char *username, size_t len)
+static struct passwd *getpw_str(const char *username, size_t len)
 {
-	if (len == 0)
-		return getpwuid(getuid());
-
 	struct passwd *pw;
 	char *username_z = xmalloc(len + 1);
 	memcpy(username_z, username, len);
@@ -223,18 +220,18 @@ static inline struct passwd *getpw_str(const char *username, size_t len)
 }
 
 /*
- * Return a string with ~ and ~user expanded via getpw*.  If buf != NULL, then
- * it is a newly allocated string. Returns NULL on getpw failure or if
- * path is NULL.
+ * Return a string with ~ and ~user expanded via getpw*.  If buf != NULL,
+ * then it is a newly allocated string. Returns NULL on getpw failure or
+ * if path is NULL.
  */
 char *expand_user_path(const char *path)
 {
 	struct strbuf user_path = STRBUF_INIT;
-	char * first_slash = strchrnul(path, '/');
-	char * to_copy;
+	const char *first_slash = strchrnul(path, '/');
+	const char *to_copy = path;
+
 	if (path == NULL)
 		goto return_null;
-
 	if (path[0] == '~') {
 		const char *username = path + 1;
 		size_t username_len = first_slash - username;
@@ -243,8 +240,6 @@ char *expand_user_path(const char *path)
 			goto return_null;
 		strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
 		to_copy = first_slash;
-	} else if (path[0] != '/') {
-		to_copy = path;
 	}
 	strbuf_add(&user_path, to_copy, strlen(to_copy));
 	return strbuf_detach(&user_path, NULL);
@@ -300,14 +295,15 @@ char *enter_repo(char *path, int strict)
 		if (path[0] == '~') {
 			char *newpath = expand_user_path(path);
 			if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
-				if (path != newpath)
-					free(newpath);
+				free(newpath);
 				return NULL;
 			}
-			/* Copy back into the static buffer. A pity
-			   since newpath was not bounded, but other
-			   branches of the if are limited by PATH_MAX
-			   anyway. */
+			/*
+			 * Copy back into the static buffer. A pity
+			 * since newpath was not bounded, but other
+			 * branches of the if are limited by PATH_MAX
+			 * anyway.
+			 */
 			strcpy(used_path, newpath); free(newpath);
 			strcpy(validated_path, path);
 			path = used_path;
-- 
1.6.5.3.283.g4b054
Previous: Junio C HamanoNext: Matthieu Moy
Message 3 of 24 in “Expand ~ and ~user in core.excludesfile, commit.template”
  1. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 16, 2009
  2. Junio C HamanoNov 16, 2009
  3. Junio C HamanoNov 17, 2009
  4. Matthieu MoyNov 17, 2009
  5. Jakub NarebskiNov 16, 2009
  6. Junio C HamanoNov 17, 2009
  7. Matthieu MoyNov 17, 2009
  8. Jakub NarebskiNov 17, 2009
  9. Jakub NarebskiNov 17, 2009
  10. Jeff KingNov 17, 2009
  11. Mike HommeyNov 17, 2009
  12. Andreas SchwabNov 17, 2009
  13. Junio C HamanoNov 17, 2009
  14. Andreas SchwabNov 18, 2009
  15. Matthieu MoyNov 18, 2009
  16. Matthieu MoyNov 17, 2009
  17. Jeff KingNov 17, 2009
  18. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 17, 2009
  19. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 18, 2009
  20. Expand ~ and ~user in core.excludesfile, commit.templateMatthieu Moy, Nov 18, 2009
  21. expand_user_path: expand ~ to $HOME, not to the actual homedir.Matthieu Moy, Nov 19, 2009
  22. Jeff KingNov 19, 2009
  23. Matthieu MoyNov 19, 2009
  24. Junio C HamanoNov 19, 2009

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.