From 5540d9477ed143707435324e785336d254b12e47 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Fri, 15 Mar 2013 16:39:00 -0700 Subject: [PATCH 1/3] 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. --- include/git2/common.h | 46 +++++++-- include/git2/strarray.h | 30 +++++- src/attr.c | 43 +++++---- src/attr.h | 18 +--- src/attr_file.c | 1 + src/attr_file.h | 1 + src/attrcache.h | 24 +++++ src/common.h | 6 ++ src/config.c | 88 ++++++----------- src/config.h | 7 +- src/fileops.c | 175 ++++++++++++++++++++++----------- src/fileops.h | 46 +++++++-- src/ignore.c | 3 +- src/ignore.h | 5 + src/repository.h | 2 +- src/util.c | 108 +++++++++++++++++++-- src/win32/findfile.c | 208 +++++++++++++++++++++++++++++----------- src/win32/findfile.h | 12 ++- tests-clar/core/env.c | 104 ++++++++++++++++++-- 19 files changed, 679 insertions(+), 248 deletions(-) create mode 100644 src/attrcache.h diff --git a/include/git2/common.h b/include/git2/common.h index 7066d5ea3..137ffa0a4 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -128,7 +128,10 @@ enum { GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE, GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, - GIT_OPT_SET_MWINDOW_MAPPED_LIMIT + GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, + GIT_OPT_GET_SEARCH_PATH, + GIT_OPT_SET_SEARCH_PATH, + GIT_OPT_PREPEND_SEARCH_PATH, }; /** @@ -136,17 +139,44 @@ enum { * * Available options: * - * opts(GIT_OPT_MWINDOW_SIZE, size_t): - * set the maximum mmap window size + * opts(GIT_OPT_GET_MWINDOW_SIZE, size_t *): + * Get the maximum mmap window size * - * opts(GIT_OPT_MWINDOW_MAPPED_LIMIT, size_t): - * set the maximum amount of memory that can be mapped at any time + * opts(GIT_OPT_SET_MWINDOW_SIZE, size_t): + * Set the maximum mmap window size + * + * opts(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, size_t *): + * Get the maximum memory that will be mapped in total by the library + * + * opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, size_t): + * Set the maximum amount of memory that can be mapped at any time * by the library * - * @param option Option key - * @param ... value to set the option + * opts(GIT_OPT_GET_SEARCH_PATH, const git_strarray **, int level) + * Get a strarray of the search path for a given level of config + * data. "level" must be one of GIT_CONFIG_LEVEL_SYSTEM, + * GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG. The search + * path applies to shared attributes and ignore files, too. + * + * opts(GIT_OPT_SET_SEARCH_PATH, int level, const git_strarray *) + * Set the search path for a given level of config data. Passing + * NULL for the git_strarray pointer resets the search path to the + * default (which is generally based on environment variables). + * "level" must be one of GIT_CONFIG_LEVEL_SYSTEM, + * GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG. The search + * path applies to shared attributes and ignore files, too. + * + * opts(GIT_OPT_PREPEND_SEARCH_PATH, int level, const git_strarray *) + * Prepend new directories to the search path for a given level of + * config data. "level" must be one of GIT_CONFIG_LEVEL_SYSTEM, + * GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG. The search + * path applies to shared attributes and ignore files, too. + * + * @param option Option key + * @param ... value to set the option + * @return 0 on success, <0 on failure */ -GIT_EXTERN(void) git_libgit2_opts(int option, ...); +GIT_EXTERN(int) git_libgit2_opts(int option, ...); /** @} */ GIT_END_DECL diff --git a/include/git2/strarray.h b/include/git2/strarray.h index 6ea570c14..df34a5b88 100644 --- a/include/git2/strarray.h +++ b/include/git2/strarray.h @@ -43,8 +43,8 @@ GIT_EXTERN(void) git_strarray_free(git_strarray *array); /** * Copy a string array object from source to target. * - * Note: target is overwritten and hence should be empty, - * otherwise its contents are leaked. + * Note: target is overwritten and hence should be empty, otherwise its + * contents are leaked. Call git_strarray_free() if necessary. * * @param tgt target * @param src source @@ -52,6 +52,32 @@ GIT_EXTERN(void) git_strarray_free(git_strarray *array); */ GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src); +/** + * Initialize a string array from a list of strings + * + * Note: target is overwritten and hence should be empty, otherwise its + * contents are leaked. Call git_strarray_free() if necessary. + * + * @param tgt target + * @param count number of strings to follow + * @return 0 on success, <0 on allocation failure + */ +GIT_EXTERN(int) git_strarray_set(git_strarray *tgt, size_t count, ...); + +/** + * Insert a strarray into the beginning of another + * + * In this case, tgt is an existing (initialized) strarray and the result + * will be reallocated with all the strings in src inserted before all of + * the existing strings in tgt. Strings in src will be strdup'ed, so + * you should still `git_strarray_free()` src when you are done with it. + * + * @param tgt strarray to update + * @param src strarray to copy from + * @return 0 on success, <0 on allocation failure (tgt will be unchanged) + */ +GIT_EXTERN(int) git_strarray_prepend(git_strarray *tgt, const git_strarray *src); + /** @} */ GIT_END_DECL 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 @@ -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 */ - 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 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 */ + 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 @@ -55,6 +55,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. */ 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); + return git_config__find_file_to_path( + global_config_path, length, git_config_find_global_r); +} - 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(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; - - // try to find msysgit installation path using registry - if (!win32_find_system_file_using_registry(path, filename)) - return 0; + return win32_find_system_dirs(out); #else - if (git_buf_joinpath(path, "/etc", filename) < 0) - return -1; - - if (git_path_exists(path->ptr) == true) - return 0; + return git_strarray_set(out, 1, "/etc"); #endif +} + +static int git_futils_guess_global_dirs(git_strarray *out) +{ +#ifdef GIT_WIN32 + return win32_find_global_dirs(out); +#else + return git_strarray_set(out, 1, getenv("HOME")); +#endif +} + +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 +} + +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_dirs_get(const git_strarray **out, git_futils_dir_t which) +{ + if (!out) { + giterr_set(GITERR_INVALID, "Output git_strarray not provided"); + return -1; + } + + *out = NULL; + + GITERR_CHECK_ERROR(git_futils_check_selector(which)); + + if (!git_futils__dirs[which].count) { + int error = git_futils__dir_guess[which](&git_futils__dirs[which]); + if (error < 0) + return error; + } + + *out = &git_futils__dirs[which]; + return 0; +} + +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; + } git_buf_clear(path); - giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); + 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) { -#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; + return git_futils_find_in_dirlist( + path, filename, GIT_FUTILS_DIR_GLOBAL, "global"); +} - 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; - } - - giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename); - git_buf_clear(path); - - return GIT_ENOTFOUND; -#else - const char *home = getenv("HOME"); - - if (home == NULL) { - giterr_set(GITERR_OS, "Global file lookup failed. " - "Cannot locate the user's home directory"); - return GIT_ENOTFOUND; - } - - if (git_buf_joinpath(path, home, filename) < 0) - return -1; - - 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; - } - - return 0; -#endif +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 #include #include "posix.h" +#include "fileops.h" #ifdef _MSC_VER # include @@ -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,37 +112,87 @@ 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) { - size_t i; - assert(tgt && src); memset(tgt, 0, sizeof(*tgt)); + return git_strarray_prepend(tgt, src); +} - if (!src->count) +int git_strarray_set(git_strarray *tgt, size_t count, ...) +{ + size_t i; + va_list ap; + + assert(tgt); + + memset(tgt, 0, sizeof(*tgt)); + + 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; +} + int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base) { const char *p; 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 - if (*pfin != L'/' && *pfin != L'\\') - wcscpy(++pfin, L"\\"); // we have enough space left, MAX_PATH - 1 is used in nextpath above + /* 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'; + } - root.len = (DWORD)wcslen(root.path) + 1; + if (root.len + gitexe_len >= MAX_PATH) + continue; + wcscpy(&root.path[root.len], gitexe); - 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; - } + if (_waccess(root.path, F_OK) == 0 && root.len > 5) { + /* replace "bin\\" or "cmd\\" with "etc\\" */ + wcscpy(&root.path[root.len - 4], L"etc\\"); + + 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; - 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; + assert(buf); + + 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); } + + RegCloseKey(hKey); } - 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; - } + return path16.len ? 0 : GIT_ENOTFOUND; +} + +static int win32_copy_to_strarray( + git_strarray *out, size_t count, char **strings) +{ + size_t i, realcount; + + if (!count) + return 0; + + 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; } -int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key) +static int win32_find_existing_dirs( + git_strarray *out, const wchar_t *tmpl[], char *temp[]) { - HKEY hKey; - DWORD dwType = REG_SZ; - DWORD dwSize = MAX_PATH; + struct win32_path path16; + git_buf buf = GIT_BUF_INIT; + size_t count; - assert(root); - - 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 (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 diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c index 2f5e91f71..7c36f3998 100644 --- a/tests-clar/core/env.c +++ b/tests-clar/core/env.c @@ -53,6 +53,10 @@ void test_core_env__cleanup(void) if (**val != '\0') (void)p_rmdir(*val); } + + /* reset search paths to default */ + git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL, true); + git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL, true); } static void setenv_and_check(const char *name, const char *value) @@ -67,17 +71,26 @@ static void setenv_and_check(const char *name, const char *value) #endif } +static void reset_global_search_path(void) +{ + cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL, true)); +} + +static void reset_system_search_path(void) +{ + cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL, true)); +} + void test_core_env__0(void) { git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; char testfile[16], tidx = '0'; char **val; + const char *testname = "testfile"; + size_t testlen = strlen(testname); - memset(testfile, 0, sizeof(testfile)); - cl_assert_equal_s("", testfile); - - memcpy(testfile, "testfile", 8); - cl_assert_equal_s("testfile", testfile); + strncpy(testfile, testname, sizeof(testfile)); + cl_assert_equal_s(testname, testfile); for (val = home_values; *val != NULL; val++) { @@ -96,7 +109,7 @@ void test_core_env__0(void) * an environment variable set from a previous iteration won't * accidentally make this test pass... */ - testfile[8] = tidx++; + testfile[testlen] = tidx++; cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); cl_git_mkfile(path.ptr, "find me"); git_buf_rtruncate_at_char(&path, '/'); @@ -105,9 +118,13 @@ void test_core_env__0(void) GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); setenv_and_check("HOME", path.ptr); + reset_global_search_path(); + cl_git_pass(git_futils_find_global_file(&found, testfile)); cl_setenv("HOME", env_save[0]); + reset_global_search_path(); + cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); @@ -115,6 +132,7 @@ void test_core_env__0(void) setenv_and_check("HOMEDRIVE", NULL); setenv_and_check("HOMEPATH", NULL); setenv_and_check("USERPROFILE", path.ptr); + reset_global_search_path(); cl_git_pass(git_futils_find_global_file(&found, testfile)); @@ -124,6 +142,7 @@ void test_core_env__0(void) if (root >= 0) { setenv_and_check("USERPROFILE", NULL); + reset_global_search_path(); cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); @@ -133,6 +152,7 @@ void test_core_env__0(void) setenv_and_check("HOMEDRIVE", path.ptr); path.ptr[root] = old; setenv_and_check("HOMEPATH", &path.ptr[root]); + reset_global_search_path(); cl_git_pass(git_futils_find_global_file(&found, testfile)); } @@ -146,6 +166,7 @@ void test_core_env__0(void) git_buf_free(&found); } + void test_core_env__1(void) { git_buf path = GIT_BUF_INIT; @@ -158,6 +179,7 @@ void test_core_env__1(void) cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist")); cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist")); #endif + reset_global_search_path(); cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); @@ -167,6 +189,8 @@ void test_core_env__1(void) cl_git_pass(cl_setenv("HOMEPATH", NULL)); cl_git_pass(cl_setenv("USERPROFILE", NULL)); #endif + reset_global_search_path(); + reset_system_search_path(); cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); @@ -176,9 +200,77 @@ void test_core_env__1(void) #ifdef GIT_WIN32 cl_git_pass(cl_setenv("PROGRAMFILES", NULL)); + reset_system_search_path(); + cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile")); #endif git_buf_free(&path); } + +void test_core_env__2(void) +{ + git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; + char testfile[16], tidx = '0'; + char **val; + const char *testname = "alternate"; + size_t testlen = strlen(testname); + git_strarray arr; + + strncpy(testfile, testname, sizeof(testfile)); + cl_assert_equal_s(testname, testfile); + + for (val = home_values; *val != NULL; val++) { + + /* if we can't make the directory, let's just assume + * we are on a filesystem that doesn't support the + * characters in question and skip this test... + */ + if (p_mkdir(*val, 0777) != 0) { + *val = ""; /* mark as not created */ + continue; + } + + cl_git_pass(git_path_prettify(&path, *val, NULL)); + + /* vary testfile name in each directory so accidentally leaving + * an environment variable set from a previous iteration won't + * accidentally make this test pass... + */ + testfile[testlen] = tidx++; + cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); + cl_git_mkfile(path.ptr, "find me"); + git_buf_rtruncate_at_char(&path, '/'); + + arr.count = 1; + arr.strings = &path.ptr; + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &arr)); + + cl_git_pass(git_futils_find_global_file(&found, testfile)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_PREPEND_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &arr)); + + cl_git_pass(git_futils_find_global_file(&found, testfile)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); + + (void)p_rmdir(*val); + } + + git_buf_free(&path); + git_buf_free(&found); +} From 41954a49c12a72eda3b3fe02c2752f6831b5dbf9 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 18 Mar 2013 14:19:35 -0700 Subject: [PATCH 2/3] Switch search paths to classic delimited strings This switches the APIs for setting and getting the global/system search paths from using git_strarray to using a simple string with GIT_PATH_LIST_SEPARATOR delimited paths, just as the environment PATH variable would contain. This makes it simpler to get and set the value. I also added code to expand "$PATH" when setting a new value to embed the old value of the path. This means that I no longer require separate actions to PREPEND to the value. --- include/git2/common.h | 33 +++++----- src/config.c | 2 +- src/fileops.c | 126 ++++++++++++++++++++++++++------------ src/fileops.h | 39 ++++++++---- src/global.c | 9 ++- src/util.c | 24 +++----- src/win32/findfile.c | 68 ++++++++------------ src/win32/findfile.h | 6 +- tests-clar/clar_libgit2.c | 2 +- tests-clar/core/env.c | 91 +++++++++++++++++++-------- 10 files changed, 242 insertions(+), 158 deletions(-) diff --git a/include/git2/common.h b/include/git2/common.h index 137ffa0a4..b8c3e42ce 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -131,7 +131,6 @@ enum { GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH, - GIT_OPT_PREPEND_SEARCH_PATH, }; /** @@ -152,25 +151,21 @@ enum { * Set the maximum amount of memory that can be mapped at any time * by the library * - * opts(GIT_OPT_GET_SEARCH_PATH, const git_strarray **, int level) - * Get a strarray of the search path for a given level of config - * data. "level" must be one of GIT_CONFIG_LEVEL_SYSTEM, - * GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG. The search - * path applies to shared attributes and ignore files, too. + * opts(GIT_OPT_GET_SEARCH_PATH, int level, char *out, size_t len) + * Get the search path for a given level of config data. "level" must + * be one of GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_GLOBAL, or + * GIT_CONFIG_LEVEL_XDG. The search path is written to the `out` + * buffer up to size `len`. Returns GIT_EBUFS if buffer is too small. * - * opts(GIT_OPT_SET_SEARCH_PATH, int level, const git_strarray *) - * Set the search path for a given level of config data. Passing - * NULL for the git_strarray pointer resets the search path to the - * default (which is generally based on environment variables). - * "level" must be one of GIT_CONFIG_LEVEL_SYSTEM, - * GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG. The search - * path applies to shared attributes and ignore files, too. - * - * opts(GIT_OPT_PREPEND_SEARCH_PATH, int level, const git_strarray *) - * Prepend new directories to the search path for a given level of - * config data. "level" must be one of GIT_CONFIG_LEVEL_SYSTEM, - * GIT_CONFIG_LEVEL_GLOBAL, or GIT_CONFIG_LEVEL_XDG. The search - * path applies to shared attributes and ignore files, too. + * opts(GIT_OPT_SET_SEARCH_PATH, int level, const char *path) + * Set the search path for a level of config data. The search path + * applied to shared attributes and ignore files, too. + * - `path` lists directories delimited by GIT_PATH_LIST_SEPARATOR. + * Pass NULL to reset to the default (generally based on environment + * variables). Use magic path `$PATH` to include the old value + * of the path (if you want to prepend or append, for instance). + * - `level` must be GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_GLOBAL, + * or GIT_CONFIG_LEVEL_XDG. * * @param option Option key * @param ... value to set the option diff --git a/src/config.c b/src/config.c index 85db0ab3c..8be5da6b7 100644 --- a/src/config.c +++ b/src/config.c @@ -521,7 +521,7 @@ static int git_config__find_file_to_path( if (path.size >= outlen) { giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); - error = -1; + error = GIT_EBUFS; goto done; } diff --git a/src/fileops.c b/src/fileops.c index 4ed46ce85..9700eed3c 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -559,48 +559,46 @@ clean_up: } -static int git_futils_guess_system_dirs(git_strarray *out) +static int git_futils_guess_system_dirs(git_buf *out) { #ifdef GIT_WIN32 return win32_find_system_dirs(out); #else - return git_strarray_set(out, 1, "/etc"); + return git_buf_sets(out, "/etc"); #endif } -static int git_futils_guess_global_dirs(git_strarray *out) +static int git_futils_guess_global_dirs(git_buf *out) { #ifdef GIT_WIN32 return win32_find_global_dirs(out); #else - return git_strarray_set(out, 1, getenv("HOME")); + return git_buf_sets(out, getenv("HOME")); #endif } -static int git_futils_guess_xdg_dirs(git_strarray *out) +static int git_futils_guess_xdg_dirs(git_buf *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"); + return git_buf_joinpath(out, env, "git"); else if ((env = getenv("HOME")) != NULL) - git_buf_joinpath(&xdg, env, ".config/git"); + return git_buf_joinpath(out, env, ".config/git"); - error = git_strarray_set(out, 1, xdg.ptr); - git_buf_free(&xdg); - - return error; + git_buf_clear(out); + return 0; #endif } -typedef int (*git_futils_dirs_guess_cb)(git_strarray *out); +typedef int (*git_futils_dirs_guess_cb)(git_buf *out); + +static git_buf git_futils__dirs[GIT_FUTILS_DIR__MAX] = + { GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT }; -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, @@ -615,53 +613,105 @@ static int git_futils_check_selector(git_futils_dir_t which) return -1; } -int git_futils_dirs_get(const git_strarray **out, git_futils_dir_t which) +int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which) { - if (!out) { - giterr_set(GITERR_INVALID, "Output git_strarray not provided"); - return -1; - } + assert(out); *out = NULL; GITERR_CHECK_ERROR(git_futils_check_selector(which)); - 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_len(&git_futils__dirs[which])) + GITERR_CHECK_ERROR( + git_futils__dir_guess[which](&git_futils__dirs[which])); *out = &git_futils__dirs[which]; return 0; } -int git_futils_dirs_set( - git_futils_dir_t which, const git_strarray *dirs, bool replace) +int git_futils_dirs_get_str(char *out, size_t outlen, git_futils_dir_t which) { + const git_buf *path = NULL; + + GITERR_CHECK_ERROR(git_futils_check_selector(which)); + GITERR_CHECK_ERROR(git_futils_dirs_get(&path, which)); + + if (!out || path->size >= outlen) { + giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); + return GIT_EBUFS; + } + + git_buf_copy_cstr(out, outlen, path); + return 0; +} + +#define PATH_MAGIC "$PATH" + +int git_futils_dirs_set(git_futils_dir_t which, const char *search_path) +{ + const char *expand_path = NULL; + git_buf merge = GIT_BUF_INIT; + GITERR_CHECK_ERROR(git_futils_check_selector(which)); - if (replace) - git_strarray_free(&git_futils__dirs[which]); + if (search_path != NULL) + expand_path = strstr(search_path, PATH_MAGIC); - /* init with defaults if it hasn't been done yet, but ignore error */ - else if (!git_futils__dirs[which].count) + /* init with default if not yet done and needed (ignoring error) */ + if ((!search_path || expand_path) && + !git_buf_len(&git_futils__dirs[which])) git_futils__dir_guess[which](&git_futils__dirs[which]); - return git_strarray_prepend(&git_futils__dirs[which], dirs); + /* if $PATH is not referenced, then just set the path */ + if (!expand_path) + return git_buf_sets(&git_futils__dirs[which], search_path); + + /* otherwise set to join(before $PATH, old value, after $PATH) */ + if (expand_path > search_path) + git_buf_set(&merge, search_path, expand_path - search_path); + + if (git_buf_len(&git_futils__dirs[which])) + git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, + merge.ptr, git_futils__dirs[which].ptr); + + expand_path += strlen(PATH_MAGIC); + if (*expand_path) + git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path); + + git_buf_swap(&git_futils__dirs[which], &merge); + git_buf_free(&merge); + + return git_buf_oom(&git_futils__dirs[which]) ? -1 : 0; +} + +void git_futils_dirs_free(void) +{ + int i; + for (i = 0; i < GIT_FUTILS_DIR__MAX; ++i) + git_buf_free(&git_futils__dirs[i]); } 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; + size_t len; + const char *scan, *next = NULL; + const git_buf *syspath; - GITERR_CHECK_ERROR(git_futils_dirs_get(&syspaths, which)); + GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which)); - for (i = 0; i < syspaths->count; ++i) { - GITERR_CHECK_ERROR( - git_buf_joinpath(path, syspaths->strings[i], name)); + for (scan = git_buf_cstr(syspath); scan; scan = next) { + for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR); + next && next > scan && next[-1] == '\\'; + next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR)) + /* find unescaped separator or end of string */; + + len = next ? (size_t)(next++ - scan) : strlen(scan); + if (!len) + continue; + + GITERR_CHECK_ERROR(git_buf_set(path, scan, len)); + GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name)); if (git_path_exists(path->ptr)) return 0; diff --git a/src/fileops.h b/src/fileops.h index 57ab91837..627a6923d 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -306,25 +306,42 @@ typedef enum { } git_futils_dir_t; /** - * Get the strarray of search paths for global/system files + * Get the search path for global/system/xdg files * - * @param out git_strarray of search paths + * @param out pointer to git_buf containing search path * @param which which list of paths to return - * @return 0 on success, <0 on failure (allocation error) + * @return 0 on success, <0 on failure */ -extern int git_futils_dirs_get( - const git_strarray **out, git_futils_dir_t which); +extern int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which); /** - * Set or prepend strarray of search paths for global/system files + * Get search path into a preallocated buffer * - * @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 + * @param out String buffer to write into + * @param outlen Size of string buffer + * @param which Which search path to return + * @return 0 on success, GIT_EBUFS if out is too small, <0 on other failure + */ + +extern int git_futils_dirs_get_str( + char *out, size_t outlen, git_futils_dir_t which); + +/** + * Set search paths for global/system/xdg files + * + * The first occurrence of the magic string "$PATH" in the new value will + * be replaced with the old value of the search path. + * + * @param which Which search path to modify + * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR) * @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); +extern int git_futils_dirs_set(git_futils_dir_t which, const char *paths); + +/** + * Release / reset all search paths + */ +extern void git_futils_dirs_free(void); /** * Create a "fake" symlink (text file containing the target path). diff --git a/src/global.c b/src/global.c index 4d37fa1d2..b7fd8e257 100644 --- a/src/global.c +++ b/src/global.c @@ -7,7 +7,8 @@ #include "common.h" #include "global.h" #include "hash.h" -#include "git2/threads.h" +#include "fileops.h" +#include "git2/threads.h" #include "thread-utils.h" @@ -82,6 +83,7 @@ void git_threads_shutdown(void) /* Shut down any subsystems that have global state */ git_hash_global_shutdown(); + git_futils_dirs_free(); } git_global_st *git__global_state(void) @@ -139,6 +141,7 @@ void git_threads_shutdown(void) /* Shut down any subsystems that have global state */ git_hash_global_shutdown(); + git_futils_dirs_free(); } git_global_st *git__global_state(void) @@ -171,7 +174,9 @@ int git_threads_init(void) void git_threads_shutdown(void) { - /* noop */ + /* Shut down any subsystems that have global state */ + git_hash_global_shutdown(); + git_futils_dirs_free(); } git_global_st *git__global_state(void) diff --git a/src/util.c b/src/util.c index f481646f7..ba867b440 100644 --- a/src/util.c +++ b/src/util.c @@ -39,7 +39,7 @@ int git_libgit2_capabilities() extern size_t git_mwindow__window_size; extern size_t git_mwindow__mapped_limit; -static int convert_config_level_to_futils_dir(int config_level) +static int config_level_to_futils_dir(int config_level) { int val = -1; @@ -80,24 +80,18 @@ int git_libgit2_opts(int key, ...) 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)); + if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0) { + char *out = va_arg(ap, char *); + size_t outlen = va_arg(ap, size_t); - error = (which < 0) ? which : git_futils_dirs_get(out, which); - break; + error = git_futils_dirs_get_str(out, outlen, error); } + 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; - } + if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0) + error = git_futils_dirs_set(error, va_arg(ap, const char *)); + break; } va_end(ap); diff --git a/src/win32/findfile.c b/src/win32/findfile.c index d3351b136..6cdea8541 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -155,75 +155,59 @@ static int win32_find_git_in_registry( return path16.len ? 0 : GIT_ENOTFOUND; } -static int win32_copy_to_strarray( - git_strarray *out, size_t count, char **strings) -{ - size_t i, realcount; - - if (!count) - return 0; - - 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[]) + git_buf *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++) { + git_buf_clear(out); + + for (; *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); + + if (buf.size) + git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr); } } - return win32_copy_to_strarray(out, count, temp); + git_buf_free(&buf); + + return (git_buf_oom(out) ? -1 : 0); } -int win32_find_system_dirs(git_strarray *out) +int win32_find_system_dirs(git_buf *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.exe") && buf.size) + git_buf_set(out, buf.ptr, buf.size); + else + git_buf_clear(out); - if (!win32_find_git_in_path(&buf, L"git.cmd")) - strings[count++] = git_buf_detach(&buf); + if (!win32_find_git_in_path(&buf, L"git.cmd") && buf.size) + git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr); /* 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); + &buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL) && buf.size) + git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr); if (!win32_find_git_in_registry( - &buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) - strings[count++] = git_buf_detach(&buf); + &buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL) && buf.size) + git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr); - return win32_copy_to_strarray(out, count, strings); + git_buf_free(&buf); + + return (git_buf_oom(out) ? -1 : 0); } -int win32_find_global_dirs(git_strarray *out) +int win32_find_global_dirs(git_buf *out) { char *temp[3]; static const wchar_t *global_tmpls[4] = { @@ -236,7 +220,7 @@ int win32_find_global_dirs(git_strarray *out) return win32_find_existing_dirs(out, global_tmpls, temp); } -int win32_find_xdg_dirs(git_strarray *out) +int win32_find_xdg_dirs(git_buf *out) { char *temp[6]; static const wchar_t *global_tmpls[7] = { diff --git a/src/win32/findfile.h b/src/win32/findfile.h index f8a13c3e3..300bd168d 100644 --- a/src/win32/findfile.h +++ b/src/win32/findfile.h @@ -18,9 +18,9 @@ extern int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); 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); +extern int win32_find_system_dirs(git_buf *out); +extern int win32_find_global_dirs(git_buf *out); +extern int win32_find_xdg_dirs(git_buf *out); #endif diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c index 698aa90f0..8033cdc3e 100644 --- a/tests-clar/clar_libgit2.c +++ b/tests-clar/clar_libgit2.c @@ -136,7 +136,7 @@ int cl_rename(const char *source, const char *dest) #include char *cl_getenv(const char *name) { - return getenv(name); + return getenv(name); } int cl_setenv(const char *name, const char *value) diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c index 7c36f3998..d684f4ca0 100644 --- a/tests-clar/core/env.c +++ b/tests-clar/core/env.c @@ -29,8 +29,24 @@ static char *home_values[] = { void test_core_env__initialize(void) { int i; - for (i = 0; i < NUM_VARS; ++i) - env_save[i] = cl_getenv(env_vars[i]); + for (i = 0; i < NUM_VARS; ++i) { + const char *original = cl_getenv(env_vars[i]); +#ifdef GIT_WIN32 + env_save[i] = original; +#else + env_save[i] = original ? git__strdup(original) : NULL; +#endif + } +} + +static void reset_global_search_path(void) +{ + cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL)); +} + +static void reset_system_search_path(void) +{ + cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL)); } void test_core_env__cleanup(void) @@ -40,9 +56,7 @@ void test_core_env__cleanup(void) for (i = 0; i < NUM_VARS; ++i) { cl_setenv(env_vars[i], env_save[i]); -#ifdef GIT_WIN32 git__free(env_save[i]); -#endif env_save[i] = NULL; } @@ -55,8 +69,8 @@ void test_core_env__cleanup(void) } /* reset search paths to default */ - git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL, true); - git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL, true); + reset_global_search_path(); + reset_system_search_path(); } static void setenv_and_check(const char *name, const char *value) @@ -64,21 +78,10 @@ static void setenv_and_check(const char *name, const char *value) char *check; cl_git_pass(cl_setenv(name, value)); + check = cl_getenv(name); cl_assert_equal_s(value, check); -#ifdef GIT_WIN32 git__free(check); -#endif -} - -static void reset_global_search_path(void) -{ - cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL, true)); -} - -static void reset_system_search_path(void) -{ - cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL, true)); } void test_core_env__0(void) @@ -216,7 +219,7 @@ void test_core_env__2(void) char **val; const char *testname = "alternate"; size_t testlen = strlen(testname); - git_strarray arr; + char out[GIT_PATH_MAX]; strncpy(testfile, testname, sizeof(testfile)); cl_assert_equal_s(testname, testfile); @@ -227,7 +230,7 @@ void test_core_env__2(void) * we are on a filesystem that doesn't support the * characters in question and skip this test... */ - if (p_mkdir(*val, 0777) != 0) { + if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) { *val = ""; /* mark as not created */ continue; } @@ -243,30 +246,66 @@ void test_core_env__2(void) cl_git_mkfile(path.ptr, "find me"); git_buf_rtruncate_at_char(&path, '/'); - arr.count = 1; - arr.strings = &path.ptr; - + /* default should be NOTFOUND */ cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + /* set search path */ cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &arr)); + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); + cl_assert_equal_s(out, path.ptr); cl_git_pass(git_futils_find_global_file(&found, testfile)); + /* reset */ cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); - cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + /* try prepend behavior */ + cl_git_pass(git_buf_putc(&path, GIT_PATH_LIST_SEPARATOR)); + cl_git_pass(git_buf_puts(&path, "$PATH")); + cl_git_pass(git_libgit2_opts( - GIT_OPT_PREPEND_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &arr)); + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); + + git_buf_rtruncate_at_char(&path, GIT_PATH_LIST_SEPARATOR); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); + cl_assert(git__prefixcmp(out, path.ptr) == 0); cl_git_pass(git_futils_find_global_file(&found, testfile)); + /* reset */ cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + + /* try append behavior */ + cl_git_pass(git_buf_join( + &found, GIT_PATH_LIST_SEPARATOR, "$PATH", path.ptr)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, found.ptr)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); + cl_assert(git__suffixcmp(out, path.ptr) == 0); + + cl_git_pass(git_futils_find_global_file(&found, testfile)); + + /* reset */ + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); + + cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); + (void)p_unlink(path.ptr); (void)p_rmdir(*val); } From 324602514fec5ba04fa236c67d633f9b18ad9845 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 18 Mar 2013 15:54:35 -0700 Subject: [PATCH 3/3] Fixes and cleanups Get rid of some dead code, tighten things up a bit, and fix a bug with core::env test. --- include/git2/strarray.h | 26 ---------- src/fileops.c | 6 +-- src/util.c | 64 ++++-------------------- src/win32/findfile.c | 20 ++++---- src/win32/findfile.h | 15 +++--- tests-clar/core/env.c | 106 ++++++++++++++++++---------------------- 6 files changed, 77 insertions(+), 160 deletions(-) diff --git a/include/git2/strarray.h b/include/git2/strarray.h index df34a5b88..d338eb7ad 100644 --- a/include/git2/strarray.h +++ b/include/git2/strarray.h @@ -52,32 +52,6 @@ GIT_EXTERN(void) git_strarray_free(git_strarray *array); */ GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src); -/** - * Initialize a string array from a list of strings - * - * Note: target is overwritten and hence should be empty, otherwise its - * contents are leaked. Call git_strarray_free() if necessary. - * - * @param tgt target - * @param count number of strings to follow - * @return 0 on success, <0 on allocation failure - */ -GIT_EXTERN(int) git_strarray_set(git_strarray *tgt, size_t count, ...); - -/** - * Insert a strarray into the beginning of another - * - * In this case, tgt is an existing (initialized) strarray and the result - * will be reallocated with all the strings in src inserted before all of - * the existing strings in tgt. Strings in src will be strdup'ed, so - * you should still `git_strarray_free()` src when you are done with it. - * - * @param tgt strarray to update - * @param src strarray to copy from - * @return 0 on success, <0 on allocation failure (tgt will be unchanged) - */ -GIT_EXTERN(int) git_strarray_prepend(git_strarray *tgt, const git_strarray *src); - /** @} */ GIT_END_DECL diff --git a/src/fileops.c b/src/fileops.c index 9700eed3c..fc9fca022 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -562,7 +562,7 @@ clean_up: static int git_futils_guess_system_dirs(git_buf *out) { #ifdef GIT_WIN32 - return win32_find_system_dirs(out); + return git_win32__find_system_dirs(out); #else return git_buf_sets(out, "/etc"); #endif @@ -571,7 +571,7 @@ static int git_futils_guess_system_dirs(git_buf *out) static int git_futils_guess_global_dirs(git_buf *out) { #ifdef GIT_WIN32 - return win32_find_global_dirs(out); + return git_win32__find_global_dirs(out); #else return git_buf_sets(out, getenv("HOME")); #endif @@ -580,7 +580,7 @@ static int git_futils_guess_global_dirs(git_buf *out) static int git_futils_guess_xdg_dirs(git_buf *out) { #ifdef GIT_WIN32 - return win32_find_xdg_dirs(out); + return git_win32__find_xdg_dirs(out); #else const char *env = NULL; diff --git a/src/util.c b/src/util.c index ba867b440..f5b4a1d68 100644 --- a/src/util.c +++ b/src/util.c @@ -112,78 +112,32 @@ void git_strarray_free(git_strarray *array) int git_strarray_copy(git_strarray *tgt, const git_strarray *src) { + size_t i; + 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); - - memset(tgt, 0, sizeof(*tgt)); - - if (!count) + if (!src->count) return 0; - tgt->strings = git__calloc(count, sizeof(char *)); + tgt->strings = git__calloc(src->count, sizeof(char *)); GITERR_CHECK_ALLOC(tgt->strings); - 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); - 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); + tgt->strings[tgt->count] = git__strdup(src->strings[i]); + if (!tgt->strings[tgt->count]) { + git_strarray_free(tgt); + memset(tgt, 0, sizeof(*tgt)); return -1; } - merge.count++; + tgt->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 6cdea8541..bc36b6b45 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -17,7 +17,7 @@ #define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" #endif -int win32_expand_path(struct win32_path *s_root, const wchar_t *templ) +int git_win32__expand_path(struct git_win32__path *s_root, const wchar_t *templ) { s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH); return s_root->len ? 0 : -1; @@ -33,8 +33,8 @@ static int win32_path_utf16_to_8(git_buf *path_utf8, const wchar_t *path_utf16) return git_buf_sets(path_utf8, temp_utf8); } -int win32_find_file( - git_buf *path, const struct win32_path *root, const char *filename) +int git_win32__find_file( + git_buf *path, const struct git_win32__path *root, const char *filename) { size_t len, alloc_len; wchar_t *file_utf16 = NULL; @@ -89,7 +89,7 @@ static wchar_t* win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen) static int win32_find_git_in_path(git_buf *buf, const wchar_t *gitexe) { wchar_t *env = _wgetenv(L"PATH"), lastch; - struct win32_path root; + struct git_win32__path root; size_t gitexe_len = wcslen(gitexe); if (!env) @@ -126,7 +126,7 @@ static int win32_find_git_in_registry( { HKEY hKey; DWORD dwType = REG_SZ; - struct win32_path path16; + struct git_win32__path path16; assert(buf); @@ -158,13 +158,13 @@ static int win32_find_git_in_registry( static int win32_find_existing_dirs( git_buf *out, const wchar_t *tmpl[], char *temp[]) { - struct win32_path path16; + struct git_win32__path path16; git_buf buf = GIT_BUF_INIT; git_buf_clear(out); for (; *tmpl != NULL; tmpl++) { - if (!win32_expand_path(&path16, *tmpl) && + if (!git_win32__expand_path(&path16, *tmpl) && path16.path[0] != L'%' && !_waccess(path16.path, F_OK)) { @@ -180,7 +180,7 @@ static int win32_find_existing_dirs( return (git_buf_oom(out) ? -1 : 0); } -int win32_find_system_dirs(git_buf *out) +int git_win32__find_system_dirs(git_buf *out) { git_buf buf = GIT_BUF_INIT; @@ -207,7 +207,7 @@ int win32_find_system_dirs(git_buf *out) return (git_buf_oom(out) ? -1 : 0); } -int win32_find_global_dirs(git_buf *out) +int git_win32__find_global_dirs(git_buf *out) { char *temp[3]; static const wchar_t *global_tmpls[4] = { @@ -220,7 +220,7 @@ int win32_find_global_dirs(git_buf *out) return win32_find_existing_dirs(out, global_tmpls, temp); } -int win32_find_xdg_dirs(git_buf *out) +int git_win32__find_xdg_dirs(git_buf *out) { char *temp[6]; static const wchar_t *global_tmpls[7] = { diff --git a/src/win32/findfile.h b/src/win32/findfile.h index 300bd168d..fc79e1b72 100644 --- a/src/win32/findfile.h +++ b/src/win32/findfile.h @@ -8,19 +8,20 @@ #ifndef INCLUDE_git_findfile_h__ #define INCLUDE_git_findfile_h__ -struct win32_path { +struct git_win32__path { wchar_t path[MAX_PATH]; DWORD len; }; -extern int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); +extern int git_win32__expand_path( + struct git_win32__path *s_root, const wchar_t *templ); -extern int win32_find_file( - git_buf *path, const struct win32_path *root, const char *filename); +extern int git_win32__find_file( + git_buf *path, const struct git_win32__path *root, const char *filename); -extern int win32_find_system_dirs(git_buf *out); -extern int win32_find_global_dirs(git_buf *out); -extern int win32_find_xdg_dirs(git_buf *out); +extern int git_win32__find_system_dirs(git_buf *out); +extern int git_win32__find_global_dirs(git_buf *out); +extern int git_win32__find_xdg_dirs(git_buf *out); #endif diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c index d684f4ca0..0fa6472d7 100644 --- a/tests-clar/core/env.c +++ b/tests-clar/core/env.c @@ -32,7 +32,7 @@ void test_core_env__initialize(void) for (i = 0; i < NUM_VARS; ++i) { const char *original = cl_getenv(env_vars[i]); #ifdef GIT_WIN32 - env_save[i] = original; + env_save[i] = (char *)original; #else env_save[i] = original ? git__strdup(original) : NULL; #endif @@ -81,7 +81,9 @@ static void setenv_and_check(const char *name, const char *value) check = cl_getenv(name); cl_assert_equal_s(value, check); +#ifdef GIT_WIN32 git__free(check); +#endif } void test_core_env__0(void) @@ -212,6 +214,43 @@ void test_core_env__1(void) git_buf_free(&path); } +static void check_global_searchpath( + const char *path, int position, const char *file, git_buf *temp) +{ + char out[GIT_PATH_MAX]; + + /* build and set new path */ + if (position < 0) + cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH")); + else if (position > 0) + cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path)); + else + cl_git_pass(git_buf_sets(temp, path)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr)); + + /* get path and make sure $PATH expansion worked */ + cl_git_pass(git_libgit2_opts( + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); + + if (position < 0) + cl_assert(git__prefixcmp(out, path) == 0); + else if (position > 0) + cl_assert(git__suffixcmp(out, path) == 0); + else + cl_assert_equal_s(out, path); + + /* find file using new path */ + cl_git_pass(git_futils_find_global_file(temp, file)); + + /* reset path and confirm file not found */ + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(temp, file)); +} + void test_core_env__2(void) { git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; @@ -219,7 +258,6 @@ void test_core_env__2(void) char **val; const char *testname = "alternate"; size_t testlen = strlen(testname); - char out[GIT_PATH_MAX]; strncpy(testfile, testname, sizeof(testfile)); cl_assert_equal_s(testname, testfile); @@ -237,9 +275,8 @@ void test_core_env__2(void) cl_git_pass(git_path_prettify(&path, *val, NULL)); - /* vary testfile name in each directory so accidentally leaving - * an environment variable set from a previous iteration won't - * accidentally make this test pass... + /* vary testfile name so any sloppiness is resetting variables or + * deleting files won't accidentally make a test pass. */ testfile[testlen] = tidx++; cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); @@ -250,63 +287,14 @@ void test_core_env__2(void) cl_assert_equal_i( GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); - /* set search path */ - cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); - - cl_git_pass(git_libgit2_opts( - GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); - cl_assert_equal_s(out, path.ptr); - - cl_git_pass(git_futils_find_global_file(&found, testfile)); - - /* reset */ - cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); - - /* try prepend behavior */ - cl_git_pass(git_buf_putc(&path, GIT_PATH_LIST_SEPARATOR)); - cl_git_pass(git_buf_puts(&path, "$PATH")); - - cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); - - git_buf_rtruncate_at_char(&path, GIT_PATH_LIST_SEPARATOR); - - cl_git_pass(git_libgit2_opts( - GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); - cl_assert(git__prefixcmp(out, path.ptr) == 0); - - cl_git_pass(git_futils_find_global_file(&found, testfile)); - - /* reset */ - cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); - cl_assert_equal_i( - GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); - - /* try append behavior */ - cl_git_pass(git_buf_join( - &found, GIT_PATH_LIST_SEPARATOR, "$PATH", path.ptr)); - - cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, found.ptr)); - - cl_git_pass(git_libgit2_opts( - GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); - cl_assert(git__suffixcmp(out, path.ptr) == 0); - - cl_git_pass(git_futils_find_global_file(&found, testfile)); - - /* reset */ - cl_git_pass(git_libgit2_opts( - GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); + /* try plain, append $PATH, and prepend $PATH */ + check_global_searchpath(path.ptr, 0, testfile, &found); + check_global_searchpath(path.ptr, -1, testfile, &found); + check_global_searchpath(path.ptr, 1, testfile, &found); + /* cleanup */ cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); (void)p_unlink(path.ptr); - (void)p_rmdir(*val); }