Commit 29aef94830edb6231a0737cfca35233f9a95463f

Edward Thomson 2017-03-23T11:59:06

config, attrcache: don't fallback to dirs literally named `~` The config and attrcache file reading code would attempt to load a file in a home directory by expanding the `~` and looking for the file, using `git_sysdir_find_global_file`. If the file was not found, the error handling would look for the literal path, eg `~/filename.txt`. Use the new `git_config_expand_global_file` instead, which allows us to get the path to the file separately, when the path is prefixed with `~/`, and fail with a not found error without falling back to looking for the literal path.

diff --git a/src/attrcache.c b/src/attrcache.c
index 4df14ee..5416189 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -290,14 +290,16 @@ static int attr_cache__lookup_path(
 		const char *cfgval = entry->value;
 
 		/* expand leading ~/ as needed */
-		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
-			!git_sysdir_find_global_file(&buf, &cfgval[2]))
-			*out = git_buf_detach(&buf);
-		else if (cfgval)
+		if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') {
+			if (! (error = git_sysdir_expand_global_file(&buf, &cfgval[2])))
+				*out = git_buf_detach(&buf);
+		} else if (cfgval) {
 			*out = git__strdup(cfgval);
+		}
 	}
-	else if (!git_sysdir_find_xdg_file(&buf, fallback))
+	else if (!git_sysdir_find_xdg_file(&buf, fallback)) {
 		*out = git_buf_detach(&buf);
+	}
 
 	git_config_entry_free(entry);
 	git_buf_free(&buf);
diff --git a/src/config_file.c b/src/config_file.c
index 6ef8066..5153f57 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1254,16 +1254,8 @@ static int strip_comments(char *line, int in_quotes)
 static int included_path(git_buf *out, const char *dir, const char *path)
 {
 	/* From the user's home */
-	int result;
-	if (path[0] == '~' && path[1] == '/') {
-		result = git_sysdir_find_global_file(out, &path[1]);
-		if (result == GIT_ENOTFOUND) {
-			git_buf_sets(out, &path[1]);
-			return 0;
-		}
-
-		return result;
-	}
+	if (path[0] == '~' && path[1] == '/')
+		return git_sysdir_expand_global_file(out, &path[1]);
 
 	return git_path_join_unrooted(out, path, dir, NULL);
 }