Commit 5540d947 by Russell Belfer

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.
parent a5f61384
......@@ -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
......
......@@ -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
......
#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) {
......
......@@ -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);
......
#include "common.h"
#include "repository.h"
#include "filebuf.h"
#include "attr.h"
#include "git2/blob.h"
#include "git2/tree.h"
#include <ctype.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)
......
/*
* 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
......@@ -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, ...);
......
......@@ -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)
......
......@@ -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 {
......
......@@ -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)
......
......@@ -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).
......
#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"
......
......@@ -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
......
......@@ -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"
......
......@@ -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;
}
......
......@@ -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);
}
......@@ -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
......@@ -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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment