Commit a1023a43 by Carlos Martín Nieto Committed by GitHub

Merge pull request #4179 from libgit2/ethomson/expand_tilde

Introduce home directory expansion function for config files, attribute files
parents 9b1260d3 e65b5e96
...@@ -290,14 +290,16 @@ static int attr_cache__lookup_path( ...@@ -290,14 +290,16 @@ static int attr_cache__lookup_path(
const char *cfgval = entry->value; const char *cfgval = entry->value;
/* expand leading ~/ as needed */ /* expand leading ~/ as needed */
if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' && if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') {
!git_sysdir_find_global_file(&buf, &cfgval[2])) if (! (error = git_sysdir_expand_global_file(&buf, &cfgval[2])))
*out = git_buf_detach(&buf); *out = git_buf_detach(&buf);
else if (cfgval) } else if (cfgval) {
*out = git__strdup(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); *out = git_buf_detach(&buf);
}
git_config_entry_free(entry); git_config_entry_free(entry);
git_buf_free(&buf); git_buf_free(&buf);
......
...@@ -1358,9 +1358,6 @@ fail_parse: ...@@ -1358,9 +1358,6 @@ fail_parse:
int git_config_parse_path(git_buf *out, const char *value) int git_config_parse_path(git_buf *out, const char *value)
{ {
int error = 0;
const git_buf *home;
assert(out && value); assert(out && value);
git_buf_sanitize(out); git_buf_sanitize(out);
...@@ -1371,16 +1368,7 @@ int git_config_parse_path(git_buf *out, const char *value) ...@@ -1371,16 +1368,7 @@ int git_config_parse_path(git_buf *out, const char *value)
return -1; return -1;
} }
if ((error = git_sysdir_get(&home, GIT_SYSDIR_GLOBAL)) < 0) return git_sysdir_expand_global_file(out, value[1] ? &value[2] : NULL);
return error;
git_buf_sets(out, home->ptr);
git_buf_puts(out, value + 1);
if (git_buf_oom(out))
return -1;
return 0;
} }
return git_buf_sets(out, value); return git_buf_sets(out, value);
......
...@@ -1256,7 +1256,7 @@ static int included_path(git_buf *out, const char *dir, const char *path) ...@@ -1256,7 +1256,7 @@ static int included_path(git_buf *out, const char *dir, const char *path)
{ {
/* From the user's home */ /* From the user's home */
if (path[0] == '~' && path[1] == '/') if (path[0] == '~' && path[1] == '/')
return git_sysdir_find_global_file(out, &path[1]); return git_sysdir_expand_global_file(out, &path[1]);
return git_path_join_unrooted(out, path, dir, NULL); return git_path_join_unrooted(out, path, dir, NULL);
} }
......
...@@ -275,3 +275,14 @@ int git_sysdir_find_template_dir(git_buf *path) ...@@ -275,3 +275,14 @@ int git_sysdir_find_template_dir(git_buf *path)
path, NULL, GIT_SYSDIR_TEMPLATE, "template"); path, NULL, GIT_SYSDIR_TEMPLATE, "template");
} }
int git_sysdir_expand_global_file(git_buf *path, const char *filename)
{
int error;
if ((error = git_sysdir_find_global_file(path, NULL)) == 0) {
if (filename)
error = git_buf_joinpath(path, path->ptr, filename);
}
return error;
}
...@@ -55,6 +55,18 @@ extern int git_sysdir_find_programdata_file(git_buf *path, const char *filename) ...@@ -55,6 +55,18 @@ extern int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
*/ */
extern int git_sysdir_find_template_dir(git_buf *path); extern int git_sysdir_find_template_dir(git_buf *path);
/**
* Expand the name of a "global" file (i.e. one in a user's home
* directory). Unlike `find_global_file` (above), this makes no
* attempt to check for the existence of the file, and is useful if
* you want the full path regardless of existence.
*
* @param path buffer to write the full path into
* @param filename name of file in the home directory
* @return 0 on success or -1 on error
*/
extern int git_sysdir_expand_global_file(git_buf *path, const char *filename);
typedef enum { typedef enum {
GIT_SYSDIR_SYSTEM = 0, GIT_SYSDIR_SYSTEM = 0,
GIT_SYSDIR_GLOBAL = 1, GIT_SYSDIR_GLOBAL = 1,
......
...@@ -108,6 +108,26 @@ void test_config_include__missing(void) ...@@ -108,6 +108,26 @@ void test_config_include__missing(void)
git_config_free(cfg); git_config_free(cfg);
} }
void test_config_include__missing_homedir(void)
{
git_config *cfg;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz");
giterr_clear();
cl_git_pass(git_config_open_ondisk(&cfg, "including"));
cl_assert(giterr_last() == NULL);
cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
cl_assert_equal_s("baz", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg);
cl_sandbox_set_search_path_defaults();
}
#define replicate10(s) s s s s s s s s s s #define replicate10(s) s s s s s s s s s s
void test_config_include__depth2(void) void test_config_include__depth2(void)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment