diff options
author | Russell Belfer <rb@github.com> | 2013-03-15 16:39:00 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-03-15 16:39:00 -0700 |
commit | 5540d9477ed143707435324e785336d254b12e47 (patch) | |
tree | 4ffaba1990a82cc51ee5169d7d2913ecceef4df2 /src | |
parent | a5f6138407efb6d8866fe8de5aac13454aefcd82 (diff) | |
download | libgit2-5540d9477ed143707435324e785336d254b12e47.tar.gz |
Implement global/system file search paths
The goal of this work is to expose the search logic for "global",
"system", and "xdg" files through the git_libgit2_opts() interface.
Behind the scenes, I changed the logic for finding files to have a
notion of a git_strarray that represents a search path and to store
a separate search path for each of the three tiers of config file.
For each tier, I implemented a function to initialize it to default
values (generally based on environment variables), and then general
interfaces to get it, set it, reset it, and prepend new directories
to it.
Next, I exposed these interfaces through the git_libgit2_opts
interface, reusing the GIT_CONFIG_LEVEL_SYSTEM, etc., constants
for the user to control which search path they were modifying.
There are alternative designs for the opts interface / argument
ordering, so I'm putting this phase out for discussion.
Additionally, I ended up doing a little bit of clean up regarding
attr.h and attr_file.h, adding a new attrcache.h so the other two
files wouldn't have to be included in so many places.
Diffstat (limited to 'src')
-rw-r--r-- | src/attr.c | 43 | ||||
-rw-r--r-- | src/attr.h | 20 | ||||
-rw-r--r-- | src/attr_file.c | 1 | ||||
-rw-r--r-- | src/attr_file.h | 1 | ||||
-rw-r--r-- | src/attrcache.h | 24 | ||||
-rw-r--r-- | src/common.h | 6 | ||||
-rw-r--r-- | src/config.c | 88 | ||||
-rw-r--r-- | src/config.h | 7 | ||||
-rw-r--r-- | src/fileops.c | 161 | ||||
-rw-r--r-- | src/fileops.h | 46 | ||||
-rw-r--r-- | src/ignore.c | 3 | ||||
-rw-r--r-- | src/ignore.h | 5 | ||||
-rw-r--r-- | src/repository.h | 2 | ||||
-rw-r--r-- | src/util.c | 106 | ||||
-rw-r--r-- | src/win32/findfile.c | 208 | ||||
-rw-r--r-- | src/win32/findfile.h | 12 |
16 files changed, 508 insertions, 225 deletions
diff --git a/src/attr.c b/src/attr.c index 9c88771e3..979fecc14 100644 --- a/src/attr.c +++ b/src/attr.c @@ -1,6 +1,8 @@ #include "repository.h" #include "fileops.h" #include "config.h" +#include "attr.h" +#include "ignore.h" #include "git2/oid.h" #include <ctype.h> @@ -593,17 +595,28 @@ static int collect_attr_files( return error; } -static char *try_global_default(const char *relpath) +static int attr_cache__lookup_path( + const char **out, git_config *cfg, const char *key, const char *fallback) { - git_buf dflt = GIT_BUF_INIT; - char *rval = NULL; + git_buf buf = GIT_BUF_INIT; + int error; - if (!git_futils_find_global_file(&dflt, relpath)) - rval = git_buf_detach(&dflt); + if (!(error = git_config_get_string(out, cfg, key))) + return 0; - git_buf_free(&dflt); + if (error == GIT_ENOTFOUND) { + giterr_clear(); + error = 0; - return rval; + if (!git_futils_find_xdg_file(&buf, fallback)) + *out = git_buf_detach(&buf); + else + *out = NULL; + + git_buf_free(&buf); + } + + return error; } int git_attr_cache__init(git_repository *repo) @@ -619,19 +632,15 @@ int git_attr_cache__init(git_repository *repo) if (git_repository_config__weakptr(&cfg, repo) < 0) return -1; - ret = git_config_get_string(&cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG); - if (ret < 0 && ret != GIT_ENOTFOUND) + ret = attr_cache__lookup_path( + &cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG); + if (ret < 0) return ret; - if (ret == GIT_ENOTFOUND) - cache->cfg_attr_file = try_global_default(GIT_ATTR_CONFIG_DEFAULT); - ret = git_config_get_string(&cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG); - if (ret < 0 && ret != GIT_ENOTFOUND) + ret = attr_cache__lookup_path( + &cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG); + if (ret < 0) return ret; - if (ret == GIT_ENOTFOUND) - cache->cfg_excl_file = try_global_default(GIT_IGNORE_CONFIG_DEFAULT); - - giterr_clear(); /* allocate hashtable for attribute and ignore file contents */ if (cache->files == NULL) { diff --git a/src/attr.h b/src/attr.h index 0fc33089b..19c979bcd 100644 --- a/src/attr.h +++ b/src/attr.h @@ -8,27 +8,13 @@ #define INCLUDE_attr_h__ #include "attr_file.h" -#include "strmap.h" - -#define GIT_ATTR_CONFIG "core.attributesfile" -#define GIT_ATTR_CONFIG_DEFAULT ".config/git/attributes" -#define GIT_IGNORE_CONFIG "core.excludesfile" -#define GIT_IGNORE_CONFIG_DEFAULT ".config/git/ignore" - -typedef struct { - int initialized; - git_pool pool; - git_strmap *files; /* hash path to git_attr_file of rules */ - git_strmap *macros; /* hash name to vector<git_attr_assignment> */ - const char *cfg_attr_file; /* cached value of core.attributesfile */ - const char *cfg_excl_file; /* cached value of core.excludesfile */ -} git_attr_cache; + +#define GIT_ATTR_CONFIG "core.attributesfile" +#define GIT_IGNORE_CONFIG "core.excludesfile" typedef int (*git_attr_file_parser)( git_repository *, void *, const char *, git_attr_file *); -extern int git_attr_cache__init(git_repository *repo); - extern int git_attr_cache__insert_macro( git_repository *repo, git_attr_rule *macro); diff --git a/src/attr_file.c b/src/attr_file.c index 628cb1544..74bd2133f 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -1,6 +1,7 @@ #include "common.h" #include "repository.h" #include "filebuf.h" +#include "attr.h" #include "git2/blob.h" #include "git2/tree.h" #include <ctype.h> diff --git a/src/attr_file.h b/src/attr_file.h index 8dc8303f7..2cc8546a2 100644 --- a/src/attr_file.h +++ b/src/attr_file.h @@ -17,6 +17,7 @@ #define GIT_ATTR_FILE ".gitattributes" #define GIT_ATTR_FILE_INREPO "info/attributes" #define GIT_ATTR_FILE_SYSTEM "gitattributes" +#define GIT_ATTR_FILE_XDG "attributes" #define GIT_ATTR_FNMATCH_NEGATIVE (1U << 0) #define GIT_ATTR_FNMATCH_DIRECTORY (1U << 1) diff --git a/src/attrcache.h b/src/attrcache.h new file mode 100644 index 000000000..12cec4bfb --- /dev/null +++ b/src/attrcache.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_attrcache_h__ +#define INCLUDE_attrcache_h__ + +#include "pool.h" +#include "strmap.h" + +typedef struct { + int initialized; + git_pool pool; + git_strmap *files; /* hash path to git_attr_file of rules */ + git_strmap *macros; /* hash name to vector<git_attr_assignment> */ + const char *cfg_attr_file; /* cached value of core.attributesfile */ + const char *cfg_excl_file; /* cached value of core.excludesfile */ +} git_attr_cache; + +extern int git_attr_cache__init(git_repository *repo); + +#endif diff --git a/src/common.h b/src/common.h index e3a9e1984..235da0412 100644 --- a/src/common.h +++ b/src/common.h @@ -56,6 +56,12 @@ #define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; } /** + * Check a return value and propogate result if non-zero. + */ +#define GITERR_CHECK_ERROR(code) \ + do { int _err = (code); if (_err < 0) return _err; } while (0) + +/** * Set the error message for this thread, formatting as needed. */ void giterr_set(int error_class, const char *string, ...); diff --git a/src/config.c b/src/config.c index d6aa3078c..85db0ab3c 100644 --- a/src/config.c +++ b/src/config.c @@ -510,62 +510,48 @@ int git_config_set_multivar(git_config *cfg, const char *name, const char *regex return file->set_multivar(file, name, regexp, value); } -int git_config_find_global_r(git_buf *path) +static int git_config__find_file_to_path( + char *out, size_t outlen, int (*find)(git_buf *buf)) { - int error = git_futils_find_global_file(path, GIT_CONFIG_FILENAME); + int error = 0; + git_buf path = GIT_BUF_INIT; + + if ((error = find(&path)) < 0) + goto done; + + if (path.size >= outlen) { + giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); + error = -1; + goto done; + } + git_buf_copy_cstr(out, outlen, &path); + +done: + git_buf_free(&path); return error; } -int git_config_find_xdg_r(git_buf *path) +int git_config_find_global_r(git_buf *path) { - int error = git_futils_find_global_file(path, GIT_CONFIG_FILENAME_ALT); - - return error; + return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL); } int git_config_find_global(char *global_config_path, size_t length) { - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_global_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } + return git_config__find_file_to_path( + global_config_path, length, git_config_find_global_r); +} - git_buf_copy_cstr(global_config_path, length, &path); - git_buf_free(&path); - return 0; +int git_config_find_xdg_r(git_buf *path) +{ + return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG); } int git_config_find_xdg(char *xdg_config_path, size_t length) { - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_xdg_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } - - git_buf_copy_cstr(xdg_config_path, length, &path); - git_buf_free(&path); - return 0; + return git_config__find_file_to_path( + xdg_config_path, length, git_config_find_xdg_r); } int git_config_find_system_r(git_buf *path) @@ -575,24 +561,8 @@ int git_config_find_system_r(git_buf *path) int git_config_find_system(char *system_config_path, size_t length) { - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_system_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } - - git_buf_copy_cstr(system_config_path, length, &path); - git_buf_free(&path); - return 0; + return git_config__find_file_to_path( + system_config_path, length, git_config_find_system_r); } int git_config_open_default(git_config **out) diff --git a/src/config.h b/src/config.h index db5ebb3b7..c43e47e82 100644 --- a/src/config.h +++ b/src/config.h @@ -12,10 +12,11 @@ #include "vector.h" #include "repository.h" -#define GIT_CONFIG_FILENAME ".gitconfig" -#define GIT_CONFIG_FILENAME_ALT ".config/git/config" -#define GIT_CONFIG_FILENAME_INREPO "config" #define GIT_CONFIG_FILENAME_SYSTEM "gitconfig" +#define GIT_CONFIG_FILENAME_GLOBAL ".gitconfig" +#define GIT_CONFIG_FILENAME_XDG "config" + +#define GIT_CONFIG_FILENAME_INREPO "config" #define GIT_CONFIG_FILE_MODE 0666 struct git_config { diff --git a/src/fileops.c b/src/fileops.c index c1824e812..4ed46ce85 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -558,75 +558,136 @@ clean_up: return error; } -int git_futils_find_system_file(git_buf *path, const char *filename) + +static int git_futils_guess_system_dirs(git_strarray *out) { #ifdef GIT_WIN32 - // try to find git.exe/git.cmd on path - if (!win32_find_system_file_using_path(path, filename)) - return 0; + return win32_find_system_dirs(out); +#else + return git_strarray_set(out, 1, "/etc"); +#endif +} - // try to find msysgit installation path using registry - if (!win32_find_system_file_using_registry(path, filename)) - return 0; +static int git_futils_guess_global_dirs(git_strarray *out) +{ +#ifdef GIT_WIN32 + return win32_find_global_dirs(out); #else - if (git_buf_joinpath(path, "/etc", filename) < 0) - return -1; + return git_strarray_set(out, 1, getenv("HOME")); +#endif +} - if (git_path_exists(path->ptr) == true) - return 0; +static int git_futils_guess_xdg_dirs(git_strarray *out) +{ +#ifdef GIT_WIN32 + return win32_find_xdg_dirs(out); +#else + int error = 0; + git_buf xdg = GIT_BUF_INIT; + const char *env = NULL; + + if ((env = getenv("XDG_CONFIG_HOME")) != NULL) + git_buf_joinpath(&xdg, env, "git"); + else if ((env = getenv("HOME")) != NULL) + git_buf_joinpath(&xdg, env, ".config/git"); + + error = git_strarray_set(out, 1, xdg.ptr); + git_buf_free(&xdg); + + return error; #endif +} - git_buf_clear(path); - giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); - return GIT_ENOTFOUND; +typedef int (*git_futils_dirs_guess_cb)(git_strarray *out); + +static git_strarray git_futils__dirs[GIT_FUTILS_DIR__MAX]; +static git_futils_dirs_guess_cb git_futils__dir_guess[GIT_FUTILS_DIR__MAX] = { + git_futils_guess_system_dirs, + git_futils_guess_global_dirs, + git_futils_guess_xdg_dirs, +}; + +static int git_futils_check_selector(git_futils_dir_t which) +{ + if (which < GIT_FUTILS_DIR__MAX) + return 0; + giterr_set(GITERR_INVALID, "config directory selector out of range"); + return -1; } -int git_futils_find_global_file(git_buf *path, const char *filename) +int git_futils_dirs_get(const git_strarray **out, git_futils_dir_t which) { -#ifdef GIT_WIN32 - struct win32_path root; - static const wchar_t *tmpls[4] = { - L"%HOME%\\", - L"%HOMEDRIVE%%HOMEPATH%\\", - L"%USERPROFILE%\\", - NULL, - }; - const wchar_t **tmpl; - - for (tmpl = tmpls; *tmpl != NULL; tmpl++) { - /* try to expand environment variable, skipping if not set */ - if (win32_expand_path(&root, *tmpl) != 0 || root.path[0] == L'%') - continue; - - /* try to look up file under path */ - if (!win32_find_file(path, &root, filename)) - return 0; + if (!out) { + giterr_set(GITERR_INVALID, "Output git_strarray not provided"); + return -1; } - giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); - git_buf_clear(path); + *out = NULL; - return GIT_ENOTFOUND; -#else - const char *home = getenv("HOME"); + GITERR_CHECK_ERROR(git_futils_check_selector(which)); - if (home == NULL) { - giterr_set(GITERR_OS, "Global file lookup failed. " - "Cannot locate the user's home directory"); - return GIT_ENOTFOUND; + if (!git_futils__dirs[which].count) { + int error = git_futils__dir_guess[which](&git_futils__dirs[which]); + if (error < 0) + return error; } - if (git_buf_joinpath(path, home, filename) < 0) - return -1; + *out = &git_futils__dirs[which]; + return 0; +} - if (git_path_exists(path->ptr) == false) { - giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); - git_buf_clear(path); - return GIT_ENOTFOUND; +int git_futils_dirs_set( + git_futils_dir_t which, const git_strarray *dirs, bool replace) +{ + GITERR_CHECK_ERROR(git_futils_check_selector(which)); + + if (replace) + git_strarray_free(&git_futils__dirs[which]); + + /* init with defaults if it hasn't been done yet, but ignore error */ + else if (!git_futils__dirs[which].count) + git_futils__dir_guess[which](&git_futils__dirs[which]); + + return git_strarray_prepend(&git_futils__dirs[which], dirs); +} + +static int git_futils_find_in_dirlist( + git_buf *path, const char *name, git_futils_dir_t which, const char *label) +{ + size_t i; + const git_strarray *syspaths; + + GITERR_CHECK_ERROR(git_futils_dirs_get(&syspaths, which)); + + for (i = 0; i < syspaths->count; ++i) { + GITERR_CHECK_ERROR( + git_buf_joinpath(path, syspaths->strings[i], name)); + + if (git_path_exists(path->ptr)) + return 0; } - return 0; -#endif + git_buf_clear(path); + giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name); + return GIT_ENOTFOUND; +} + +int git_futils_find_system_file(git_buf *path, const char *filename) +{ + return git_futils_find_in_dirlist( + path, filename, GIT_FUTILS_DIR_SYSTEM, "system"); +} + +int git_futils_find_global_file(git_buf *path, const char *filename) +{ + return git_futils_find_in_dirlist( + path, filename, GIT_FUTILS_DIR_GLOBAL, "global"); +} + +int git_futils_find_xdg_file(git_buf *path, const char *filename) +{ + return git_futils_find_in_dirlist( + path, filename, GIT_FUTILS_DIR_XDG, "global/xdg"); } int git_futils_fake_symlink(const char *old, const char *new) diff --git a/src/fileops.h b/src/fileops.h index 7ba99d3d9..57ab91837 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -276,25 +276,55 @@ extern void git_futils_mmap_free(git_map *map); * * @param pathbuf buffer to write the full path into * @param filename name of file to find in the home directory - * @return - * - 0 if found; - * - GIT_ENOTFOUND if not found; - * - -1 on an unspecified OS related error. + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error */ extern int git_futils_find_global_file(git_buf *path, const char *filename); /** + * Find an "XDG" file (i.e. one in user's XDG config path). + * + * @param pathbuf buffer to write the full path into + * @param filename name of file to find in the home directory + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error + */ +extern int git_futils_find_xdg_file(git_buf *path, const char *filename); + +/** * Find a "system" file (i.e. one shared for all users of the system). * * @param pathbuf buffer to write the full path into * @param filename name of file to find in the home directory - * @return - * - 0 if found; - * - GIT_ENOTFOUND if not found; - * - -1 on an unspecified OS related error. + * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error */ extern int git_futils_find_system_file(git_buf *path, const char *filename); +typedef enum { + GIT_FUTILS_DIR_SYSTEM = 0, + GIT_FUTILS_DIR_GLOBAL = 1, + GIT_FUTILS_DIR_XDG = 2, + GIT_FUTILS_DIR__MAX = 3, +} git_futils_dir_t; + +/** + * Get the strarray of search paths for global/system files + * + * @param out git_strarray of search paths + * @param which which list of paths to return + * @return 0 on success, <0 on failure (allocation error) + */ +extern int git_futils_dirs_get( + const git_strarray **out, git_futils_dir_t which); + +/** + * Set or prepend strarray of search paths for global/system files + * + * @param which which list of paths to modify + * @param dirs new list of search paths + * @param replace true to replace old, false to prepend to old + * @return 0 on success, <0 on failure (allocation error) + */ +extern int git_futils_dirs_set( + git_futils_dir_t which, const git_strarray *dirs, bool replace); /** * Create a "fake" symlink (text file containing the target path). diff --git a/src/ignore.c b/src/ignore.c index 5edc5b65b..17779522c 100644 --- a/src/ignore.c +++ b/src/ignore.c @@ -1,11 +1,10 @@ #include "git2/ignore.h" #include "ignore.h" +#include "attr.h" #include "path.h" #include "config.h" #define GIT_IGNORE_INTERNAL "[internal]exclude" -#define GIT_IGNORE_FILE_INREPO "info/exclude" -#define GIT_IGNORE_FILE ".gitignore" #define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n" diff --git a/src/ignore.h b/src/ignore.h index 5a15afcca..5af8e8e7d 100644 --- a/src/ignore.h +++ b/src/ignore.h @@ -9,6 +9,11 @@ #include "repository.h" #include "vector.h" +#include "attr_file.h" + +#define GIT_IGNORE_FILE ".gitignore" +#define GIT_IGNORE_FILE_INREPO "info/exclude" +#define GIT_IGNORE_FILE_XDG "ignore" /* The git_ignores structure maintains three sets of ignores: * - internal ignores diff --git a/src/repository.h b/src/repository.h index ebd797cc1..cc2f8c2b8 100644 --- a/src/repository.h +++ b/src/repository.h @@ -19,7 +19,7 @@ #include "buffer.h" #include "odb.h" #include "object.h" -#include "attr.h" +#include "attrcache.h" #include "strmap.h" #include "refdb.h" diff --git a/src/util.c b/src/util.c index 885978046..f481646f7 100644 --- a/src/util.c +++ b/src/util.c @@ -10,6 +10,7 @@ #include <stdio.h> #include <ctype.h> #include "posix.h" +#include "fileops.h" #ifdef _MSC_VER # include <Shlwapi.h> @@ -38,13 +39,30 @@ int git_libgit2_capabilities() extern size_t git_mwindow__window_size; extern size_t git_mwindow__mapped_limit; -void git_libgit2_opts(int key, ...) +static int convert_config_level_to_futils_dir(int config_level) { + int val = -1; + + switch (config_level) { + case GIT_CONFIG_LEVEL_SYSTEM: val = GIT_FUTILS_DIR_SYSTEM; break; + case GIT_CONFIG_LEVEL_XDG: val = GIT_FUTILS_DIR_XDG; break; + case GIT_CONFIG_LEVEL_GLOBAL: val = GIT_FUTILS_DIR_GLOBAL; break; + default: + giterr_set( + GITERR_INVALID, "Invalid config path selector %d", config_level); + } + + return val; +} + +int git_libgit2_opts(int key, ...) +{ + int error = 0; va_list ap; va_start(ap, key); - switch(key) { + switch (key) { case GIT_OPT_SET_MWINDOW_SIZE: git_mwindow__window_size = va_arg(ap, size_t); break; @@ -60,9 +78,31 @@ void git_libgit2_opts(int key, ...) case GIT_OPT_GET_MWINDOW_MAPPED_LIMIT: *(va_arg(ap, size_t *)) = git_mwindow__mapped_limit; break; + + case GIT_OPT_GET_SEARCH_PATH: + { + const git_strarray **out = va_arg(ap, const git_strarray **); + int which = convert_config_level_to_futils_dir(va_arg(ap, int)); + + error = (which < 0) ? which : git_futils_dirs_get(out, which); + break; + } + + case GIT_OPT_SET_SEARCH_PATH: + case GIT_OPT_PREPEND_SEARCH_PATH: + { + int which = convert_config_level_to_futils_dir(va_arg(ap, int)); + const git_strarray *dirs = va_arg(ap, git_strarray *); + + error = (which < 0) ? which : git_futils_dirs_set( + which, dirs, key == GIT_OPT_SET_SEARCH_PATH); + break; + } } va_end(ap); + + return error; } void git_strarray_free(git_strarray *array) @@ -72,34 +112,84 @@ void git_strarray_free(git_strarray *array) git__free(array->strings[i]); git__free(array->strings); + + memset(array, 0, sizeof(*array)); } int git_strarray_copy(git_strarray *tgt, const git_strarray *src) { + assert(tgt && src); + + memset(tgt, 0, sizeof(*tgt)); + return git_strarray_prepend(tgt, src); +} + +int git_strarray_set(git_strarray *tgt, size_t count, ...) +{ size_t i; + va_list ap; - assert(tgt && src); + assert(tgt); memset(tgt, 0, sizeof(*tgt)); - if (!src->count) + if (!count) return 0; - tgt->strings = git__calloc(src->count, sizeof(char *)); + tgt->strings = git__calloc(count, sizeof(char *)); GITERR_CHECK_ALLOC(tgt->strings); - for (i = 0; i < src->count; ++i) { - tgt->strings[tgt->count] = git__strdup(src->strings[i]); + va_start(ap, count); + for (i = 0; i < count; ++i) { + const char *str = va_arg(ap, const char *); + if (!str) + continue; + tgt->strings[tgt->count] = git__strdup(str); if (!tgt->strings[tgt->count]) { git_strarray_free(tgt); - memset(tgt, 0, sizeof(*tgt)); + va_end(ap); return -1; } tgt->count++; } + va_end(ap); + + return 0; +} + +int git_strarray_prepend(git_strarray *tgt, const git_strarray *src) +{ + size_t i; + git_strarray merge; + + if (!src || !src->count) + return 0; + + merge.count = 0; + merge.strings = git__calloc(tgt->count + src->count, sizeof(char *)); + GITERR_CHECK_ALLOC(merge.strings); + + for (i = 0; i < src->count; ++i) { + if (!src->strings[i]) + continue; + + merge.strings[merge.count] = git__strdup(src->strings[i]); + if (!merge.strings[merge.count]) { + git_strarray_free(&merge); + return -1; + } + + merge.count++; + } + + for (i = 0; i < tgt->count; ++i) + if (tgt->strings[i]) + merge.strings[merge.count++] = tgt->strings[i]; + git__free(tgt->strings); + memcpy(tgt, &merge, sizeof(merge)); return 0; } diff --git a/src/win32/findfile.c b/src/win32/findfile.c index 6fc7c7513..d3351b136 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -10,6 +10,7 @@ #include "findfile.h" #define REG_MSYSGIT_INSTALL_LOCAL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" + #ifndef _WIN64 #define REG_MSYSGIT_INSTALL REG_MSYSGIT_INSTALL_LOCAL #else @@ -22,11 +23,21 @@ int win32_expand_path(struct win32_path *s_root, const wchar_t *templ) return s_root->len ? 0 : -1; } -int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename) +static int win32_path_utf16_to_8(git_buf *path_utf8, const wchar_t *path_utf16) +{ + char temp_utf8[GIT_PATH_MAX]; + + git__utf16_to_8(temp_utf8, path_utf16); + git_path_mkposix(temp_utf8); + + return git_buf_sets(path_utf8, temp_utf8); +} + +int win32_find_file( + git_buf *path, const struct win32_path *root, const char *filename) { size_t len, alloc_len; wchar_t *file_utf16 = NULL; - char file_utf8[GIT_PATH_MAX]; if (!root || !filename || (len = strlen(filename)) == 0) return GIT_ENOTFOUND; @@ -50,15 +61,13 @@ int win32_find_file(git_buf *path, const struct win32_path *root, const char *fi return GIT_ENOTFOUND; } - git__utf16_to_8(file_utf8, file_utf16); - git_path_mkposix(file_utf8); - git_buf_sets(path, file_utf8); - + win32_path_utf16_to_8(path, file_utf16); git__free(file_utf16); + return 0; } -wchar_t* win32_nextpath(wchar_t *path, wchar_t *buf, size_t buflen) +static wchar_t* win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen) { wchar_t term, *base = path; @@ -77,80 +86,169 @@ wchar_t* win32_nextpath(wchar_t *path, wchar_t *buf, size_t buflen) return (path != base) ? path : NULL; } -int win32_find_system_file_using_path(git_buf *path, const char *filename) +static int win32_find_git_in_path(git_buf *buf, const wchar_t *gitexe) { - wchar_t * env = NULL; + wchar_t *env = _wgetenv(L"PATH"), lastch; struct win32_path root; + size_t gitexe_len = wcslen(gitexe); - env = _wgetenv(L"PATH"); if (!env) return -1; - // search in all paths defined in PATH - while ((env = win32_nextpath(env, root.path, MAX_PATH - 1)) != NULL && *root.path) - { - wchar_t * pfin = root.path + wcslen(root.path) - 1; // last char of the current path entry + while ((env = win32_walkpath(env, root.path, MAX_PATH-1)) && *root.path) { + root.len = (DWORD)wcslen(root.path); + lastch = root.path[root.len - 1]; + + /* ensure trailing slash (MAX_PATH-1 to walkpath guarantees space) */ + if (lastch != L'/' && lastch != L'\\') { + root.path[root.len++] = L'\\'; + root.path[root.len] = L'\0'; + } - // ensure trailing slash - if (*pfin != L'/' && *pfin != L'\\') - wcscpy(++pfin, L"\\"); // we have enough space left, MAX_PATH - 1 is used in nextpath above + if (root.len + gitexe_len >= MAX_PATH) + continue; + wcscpy(&root.path[root.len], gitexe); - root.len = (DWORD)wcslen(root.path) + 1; + if (_waccess(root.path, F_OK) == 0 && root.len > 5) { + /* replace "bin\\" or "cmd\\" with "etc\\" */ + wcscpy(&root.path[root.len - 4], L"etc\\"); - if (win32_find_file(path, &root, "git.cmd") == 0 || win32_find_file(path, &root, "git.exe") == 0) { - // we found the cmd or bin directory of a git installaton - if (root.len > 5) { - wcscpy(root.path + wcslen(root.path) - 4, L"etc\\"); - if (win32_find_file(path, &root, filename) == 0) - return 0; - } + win32_path_utf16_to_8(buf, root.path); + return 0; } } - + return GIT_ENOTFOUND; } -int win32_find_system_file_using_registry(git_buf *path, const char *filename) +static int win32_find_git_in_registry( + git_buf *buf, const HKEY hieve, const wchar_t *key) { - struct win32_path root; + HKEY hKey; + DWORD dwType = REG_SZ; + struct win32_path path16; + + assert(buf); - if (win32_find_msysgit_in_registry(&root, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL)) { - if (win32_find_msysgit_in_registry(&root, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) { - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory"); - return -1; + path16.len = 0; + + if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { + if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, + (LPBYTE)&path16.path, &path16.len) == ERROR_SUCCESS) + { + /* InstallLocation points to the root of the git directory */ + + if (path16.len + 4 > MAX_PATH) { /* 4 = wcslen(L"etc\\") */ + giterr_set(GITERR_OS, "Cannot locate git - path too long"); + return -1; + } + + wcscat(path16.path, L"etc\\"); + path16.len += 4; + + win32_path_utf16_to_8(buf, path16.path); } - } - if (win32_find_file(path, &root, filename) < 0) { - giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); - git_buf_clear(path); - return GIT_ENOTFOUND; + RegCloseKey(hKey); } - return 0; + return path16.len ? 0 : GIT_ENOTFOUND; } -int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key) +static int win32_copy_to_strarray( + git_strarray *out, size_t count, char **strings) { - HKEY hKey; - DWORD dwType = REG_SZ; - DWORD dwSize = MAX_PATH; + size_t i, realcount; - assert(root); + if (!count) + return 0; - root->len = 0; - if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { - if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)&root->path, &dwSize) == ERROR_SUCCESS) { - // InstallLocation points to the root of the msysgit directory - if (dwSize + 4 > MAX_PATH) {// 4 = wcslen(L"etc\\") - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long"); - return -1; - } - wcscat(root->path, L"etc\\"); - root->len = (DWORD)wcslen(root->path) + 1; + for (i = 0, realcount = 0; i < count; ++i) + if (strings[i]) realcount++; + + out->strings = git__calloc(realcount, sizeof(char *)); + GITERR_CHECK_ALLOC(out->strings); + + for (i = 0, out->count = 0; i < count; ++i) + if (strings[i]) + out->strings[out->count++] = strings[i]; + + return 0; +} + +static int win32_find_existing_dirs( + git_strarray *out, const wchar_t *tmpl[], char *temp[]) +{ + struct win32_path path16; + git_buf buf = GIT_BUF_INIT; + size_t count; + + for (count = 0; *tmpl != NULL; tmpl++) { + if (!win32_expand_path(&path16, *tmpl) && + path16.path[0] != L'%' && + !_waccess(path16.path, F_OK)) + { + win32_path_utf16_to_8(&buf, path16.path); + temp[count++] = git_buf_detach(&buf); } } - RegCloseKey(hKey); - return root->len ? 0 : GIT_ENOTFOUND; + return win32_copy_to_strarray(out, count, temp); } + +int win32_find_system_dirs(git_strarray *out) +{ + char *strings[4]; + size_t count = 0; + git_buf buf = GIT_BUF_INIT; + + memset(out, 0, sizeof(*out)); + + /* directories where git.exe & git.cmd are found */ + if (!win32_find_git_in_path(&buf, L"git.exe")) + strings[count++] = git_buf_detach(&buf); + + if (!win32_find_git_in_path(&buf, L"git.cmd")) + strings[count++] = git_buf_detach(&buf); + + /* directories where git is installed according to registry */ + if (!win32_find_git_in_registry( + &buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL)) + strings[count++] = git_buf_detach(&buf); + + if (!win32_find_git_in_registry( + &buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) + strings[count++] = git_buf_detach(&buf); + + return win32_copy_to_strarray(out, count, strings); +} + +int win32_find_global_dirs(git_strarray *out) +{ + char *temp[3]; + static const wchar_t *global_tmpls[4] = { + L"%HOME%\\", + L"%HOMEDRIVE%%HOMEPATH%\\", + L"%USERPROFILE%\\", + NULL, + }; + + return win32_find_existing_dirs(out, global_tmpls, temp); +} + +int win32_find_xdg_dirs(git_strarray *out) +{ + char *temp[6]; + static const wchar_t *global_tmpls[7] = { + L"%XDG_CONFIG_HOME%\\git", + L"%APPDATA%\\git", + L"%LOCALAPPDATA%\\git", + L"%HOME%\\.config\\git", + L"%HOMEDRIVE%%HOMEPATH%\\.config\\git", + L"%USERPROFILE%\\.config\\git", + NULL, + }; + + return win32_find_existing_dirs(out, global_tmpls, temp); +} + diff --git a/src/win32/findfile.h b/src/win32/findfile.h index 47fe71596..f8a13c3e3 100644 --- a/src/win32/findfile.h +++ b/src/win32/findfile.h @@ -13,12 +13,14 @@ struct win32_path { DWORD len; }; -int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); +extern int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); -int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename); -int win32_find_system_file_using_path(git_buf *path, const char *filename); -int win32_find_system_file_using_registry(git_buf *path, const char *filename); -int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key); +extern int win32_find_file( + git_buf *path, const struct win32_path *root, const char *filename); + +extern int win32_find_system_dirs(git_strarray *out); +extern int win32_find_global_dirs(git_strarray *out); +extern int win32_find_xdg_dirs(git_strarray *out); #endif |