mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-28 11:52:46 +00:00
Merge pull request #1417 from arrbee/opts-for-paths
Implement opts interface for global/system file search paths
This commit is contained in:
commit
5b27bf7e5b
@ -128,7 +128,9 @@ 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,
|
||||
};
|
||||
|
||||
/**
|
||||
@ -136,17 +138,40 @@ 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, 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 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
|
||||
* @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
|
||||
|
43
src/attr.c
43
src/attr.c
@ -1,6 +1,8 @@
|
||||
#include "repository.h"
|
||||
#include "fileops.h"
|
||||
#include "config.h"
|
||||
#include "attr.h"
|
||||
#include "ignore.h"
|
||||
#include "git2/oid.h"
|
||||
#include <ctype.h>
|
||||
|
||||
@ -593,17 +595,28 @@ static int collect_attr_files(
|
||||
return error;
|
||||
}
|
||||
|
||||
static char *try_global_default(const char *relpath)
|
||||
static int attr_cache__lookup_path(
|
||||
const char **out, git_config *cfg, const char *key, const char *fallback)
|
||||
{
|
||||
git_buf dflt = GIT_BUF_INIT;
|
||||
char *rval = NULL;
|
||||
git_buf buf = GIT_BUF_INIT;
|
||||
int error;
|
||||
|
||||
if (!git_futils_find_global_file(&dflt, relpath))
|
||||
rval = git_buf_detach(&dflt);
|
||||
if (!(error = git_config_get_string(out, cfg, key)))
|
||||
return 0;
|
||||
|
||||
git_buf_free(&dflt);
|
||||
if (error == GIT_ENOTFOUND) {
|
||||
giterr_clear();
|
||||
error = 0;
|
||||
|
||||
return rval;
|
||||
if (!git_futils_find_xdg_file(&buf, fallback))
|
||||
*out = git_buf_detach(&buf);
|
||||
else
|
||||
*out = NULL;
|
||||
|
||||
git_buf_free(&buf);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
int git_attr_cache__init(git_repository *repo)
|
||||
@ -619,19 +632,15 @@ int git_attr_cache__init(git_repository *repo)
|
||||
if (git_repository_config__weakptr(&cfg, repo) < 0)
|
||||
return -1;
|
||||
|
||||
ret = git_config_get_string(&cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG);
|
||||
if (ret < 0 && ret != GIT_ENOTFOUND)
|
||||
ret = attr_cache__lookup_path(
|
||||
&cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
if (ret == GIT_ENOTFOUND)
|
||||
cache->cfg_attr_file = try_global_default(GIT_ATTR_CONFIG_DEFAULT);
|
||||
|
||||
ret = git_config_get_string(&cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG);
|
||||
if (ret < 0 && ret != GIT_ENOTFOUND)
|
||||
ret = attr_cache__lookup_path(
|
||||
&cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
if (ret == GIT_ENOTFOUND)
|
||||
cache->cfg_excl_file = try_global_default(GIT_IGNORE_CONFIG_DEFAULT);
|
||||
|
||||
giterr_clear();
|
||||
|
||||
/* allocate hashtable for attribute and ignore file contents */
|
||||
if (cache->files == NULL) {
|
||||
|
18
src/attr.h
18
src/attr.h
@ -8,27 +8,13 @@
|
||||
#define INCLUDE_attr_h__
|
||||
|
||||
#include "attr_file.h"
|
||||
#include "strmap.h"
|
||||
|
||||
#define GIT_ATTR_CONFIG "core.attributesfile"
|
||||
#define GIT_ATTR_CONFIG_DEFAULT ".config/git/attributes"
|
||||
#define GIT_IGNORE_CONFIG "core.excludesfile"
|
||||
#define GIT_IGNORE_CONFIG_DEFAULT ".config/git/ignore"
|
||||
|
||||
typedef struct {
|
||||
int initialized;
|
||||
git_pool pool;
|
||||
git_strmap *files; /* hash path to git_attr_file of rules */
|
||||
git_strmap *macros; /* hash name to vector<git_attr_assignment> */
|
||||
const char *cfg_attr_file; /* cached value of core.attributesfile */
|
||||
const char *cfg_excl_file; /* cached value of core.excludesfile */
|
||||
} git_attr_cache;
|
||||
#define GIT_ATTR_CONFIG "core.attributesfile"
|
||||
#define GIT_IGNORE_CONFIG "core.excludesfile"
|
||||
|
||||
typedef int (*git_attr_file_parser)(
|
||||
git_repository *, void *, const char *, git_attr_file *);
|
||||
|
||||
extern int git_attr_cache__init(git_repository *repo);
|
||||
|
||||
extern int git_attr_cache__insert_macro(
|
||||
git_repository *repo, git_attr_rule *macro);
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "common.h"
|
||||
#include "repository.h"
|
||||
#include "filebuf.h"
|
||||
#include "attr.h"
|
||||
#include "git2/blob.h"
|
||||
#include "git2/tree.h"
|
||||
#include <ctype.h>
|
||||
|
@ -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)
|
||||
|
24
src/attrcache.h
Normal file
24
src/attrcache.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) the libgit2 contributors. All rights reserved.
|
||||
*
|
||||
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
||||
* a Linking Exception. For full terms see the included COPYING file.
|
||||
*/
|
||||
#ifndef INCLUDE_attrcache_h__
|
||||
#define INCLUDE_attrcache_h__
|
||||
|
||||
#include "pool.h"
|
||||
#include "strmap.h"
|
||||
|
||||
typedef struct {
|
||||
int initialized;
|
||||
git_pool pool;
|
||||
git_strmap *files; /* hash path to git_attr_file of rules */
|
||||
git_strmap *macros; /* hash name to vector<git_attr_assignment> */
|
||||
const char *cfg_attr_file; /* cached value of core.attributesfile */
|
||||
const char *cfg_excl_file; /* cached value of core.excludesfile */
|
||||
} git_attr_cache;
|
||||
|
||||
extern int git_attr_cache__init(git_repository *repo);
|
||||
|
||||
#endif
|
@ -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.
|
||||
*/
|
||||
|
88
src/config.c
88
src/config.c
@ -515,62 +515,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 = GIT_EBUFS;
|
||||
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)
|
||||
@ -580,24 +566,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 {
|
||||
|
225
src/fileops.c
225
src/fileops.c
@ -558,75 +558,186 @@ clean_up:
|
||||
return error;
|
||||
}
|
||||
|
||||
int git_futils_find_system_file(git_buf *path, const char *filename)
|
||||
|
||||
static int git_futils_guess_system_dirs(git_buf *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 git_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_buf_sets(out, "/etc");
|
||||
#endif
|
||||
}
|
||||
|
||||
static int git_futils_guess_global_dirs(git_buf *out)
|
||||
{
|
||||
#ifdef GIT_WIN32
|
||||
return git_win32__find_global_dirs(out);
|
||||
#else
|
||||
return git_buf_sets(out, getenv("HOME"));
|
||||
#endif
|
||||
}
|
||||
|
||||
static int git_futils_guess_xdg_dirs(git_buf *out)
|
||||
{
|
||||
#ifdef GIT_WIN32
|
||||
return git_win32__find_xdg_dirs(out);
|
||||
#else
|
||||
const char *env = NULL;
|
||||
|
||||
if ((env = getenv("XDG_CONFIG_HOME")) != NULL)
|
||||
return git_buf_joinpath(out, env, "git");
|
||||
else if ((env = getenv("HOME")) != NULL)
|
||||
return git_buf_joinpath(out, env, ".config/git");
|
||||
|
||||
git_buf_clear(out);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
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_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_buf **out, git_futils_dir_t which)
|
||||
{
|
||||
assert(out);
|
||||
|
||||
*out = NULL;
|
||||
|
||||
GITERR_CHECK_ERROR(git_futils_check_selector(which));
|
||||
|
||||
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_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 (search_path != NULL)
|
||||
expand_path = strstr(search_path, PATH_MAGIC);
|
||||
|
||||
/* 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]);
|
||||
|
||||
/* 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 len;
|
||||
const char *scan, *next = NULL;
|
||||
const git_buf *syspath;
|
||||
|
||||
GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -276,25 +276,72 @@ 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 search path for global/system/xdg files
|
||||
*
|
||||
* @param out pointer to git_buf containing search path
|
||||
* @param which which list of paths to return
|
||||
* @return 0 on success, <0 on failure
|
||||
*/
|
||||
extern int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which);
|
||||
|
||||
/**
|
||||
* Get search path into a preallocated buffer
|
||||
*
|
||||
* @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 char *paths);
|
||||
|
||||
/**
|
||||
* Release / reset all search paths
|
||||
*/
|
||||
extern void git_futils_dirs_free(void);
|
||||
|
||||
/**
|
||||
* Create a "fake" symlink (text file containing the target path).
|
||||
|
@ -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)
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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"
|
||||
|
||||
|
44
src/util.c
44
src/util.c
@ -10,6 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include "posix.h"
|
||||
#include "fileops.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# include <Shlwapi.h>
|
||||
@ -38,13 +39,30 @@ int git_libgit2_capabilities()
|
||||
extern size_t git_mwindow__window_size;
|
||||
extern size_t git_mwindow__mapped_limit;
|
||||
|
||||
void git_libgit2_opts(int key, ...)
|
||||
static int 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,25 @@ 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:
|
||||
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 = git_futils_dirs_get_str(out, outlen, error);
|
||||
}
|
||||
break;
|
||||
|
||||
case GIT_OPT_SET_SEARCH_PATH:
|
||||
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);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void git_strarray_free(git_strarray *array)
|
||||
@ -72,6 +106,8 @@ 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)
|
||||
@ -89,8 +125,10 @@ int git_strarray_copy(git_strarray *tgt, const git_strarray *src)
|
||||
GITERR_CHECK_ALLOC(tgt->strings);
|
||||
|
||||
for (i = 0; i < src->count; ++i) {
|
||||
tgt->strings[tgt->count] = git__strdup(src->strings[i]);
|
||||
if (!src->strings[i])
|
||||
continue;
|
||||
|
||||
tgt->strings[tgt->count] = git__strdup(src->strings[i]);
|
||||
if (!tgt->strings[tgt->count]) {
|
||||
git_strarray_free(tgt);
|
||||
memset(tgt, 0, sizeof(*tgt));
|
||||
|
@ -10,23 +10,34 @@
|
||||
#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
|
||||
#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;
|
||||
}
|
||||
|
||||
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 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;
|
||||
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,153 @@ 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;
|
||||
struct win32_path root;
|
||||
wchar_t *env = _wgetenv(L"PATH"), lastch;
|
||||
struct git_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)
|
||||
{
|
||||
struct win32_path root;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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 0;
|
||||
}
|
||||
|
||||
int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key)
|
||||
static int win32_find_git_in_registry(
|
||||
git_buf *buf, const HKEY hieve, const wchar_t *key)
|
||||
{
|
||||
HKEY hKey;
|
||||
DWORD dwType = REG_SZ;
|
||||
DWORD dwSize = MAX_PATH;
|
||||
struct git_win32__path path16;
|
||||
|
||||
assert(root);
|
||||
assert(buf);
|
||||
|
||||
path16.len = 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");
|
||||
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(root->path, L"etc\\");
|
||||
root->len = (DWORD)wcslen(root->path) + 1;
|
||||
|
||||
wcscat(path16.path, L"etc\\");
|
||||
path16.len += 4;
|
||||
|
||||
win32_path_utf16_to_8(buf, path16.path);
|
||||
}
|
||||
|
||||
RegCloseKey(hKey);
|
||||
}
|
||||
|
||||
return path16.len ? 0 : GIT_ENOTFOUND;
|
||||
}
|
||||
|
||||
static int win32_find_existing_dirs(
|
||||
git_buf *out, const wchar_t *tmpl[], char *temp[])
|
||||
{
|
||||
struct git_win32__path path16;
|
||||
git_buf buf = GIT_BUF_INIT;
|
||||
|
||||
git_buf_clear(out);
|
||||
|
||||
for (; *tmpl != NULL; tmpl++) {
|
||||
if (!git_win32__expand_path(&path16, *tmpl) &&
|
||||
path16.path[0] != L'%' &&
|
||||
!_waccess(path16.path, F_OK))
|
||||
{
|
||||
win32_path_utf16_to_8(&buf, path16.path);
|
||||
|
||||
if (buf.size)
|
||||
git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
|
||||
}
|
||||
}
|
||||
RegCloseKey(hKey);
|
||||
|
||||
return root->len ? 0 : GIT_ENOTFOUND;
|
||||
git_buf_free(&buf);
|
||||
|
||||
return (git_buf_oom(out) ? -1 : 0);
|
||||
}
|
||||
|
||||
int git_win32__find_system_dirs(git_buf *out)
|
||||
{
|
||||
git_buf buf = GIT_BUF_INIT;
|
||||
|
||||
/* directories where git.exe & git.cmd are found */
|
||||
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") && 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) && 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) && buf.size)
|
||||
git_buf_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
|
||||
|
||||
git_buf_free(&buf);
|
||||
|
||||
return (git_buf_oom(out) ? -1 : 0);
|
||||
}
|
||||
|
||||
int git_win32__find_global_dirs(git_buf *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 git_win32__find_xdg_dirs(git_buf *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);
|
||||
}
|
||||
|
||||
|
@ -8,17 +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;
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
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 git_win32__find_file(
|
||||
git_buf *path, const struct git_win32__path *root, const char *filename);
|
||||
|
||||
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
|
||||
|
||||
|
@ -136,7 +136,7 @@ int cl_rename(const char *source, const char *dest)
|
||||
#include <stdlib.h>
|
||||
char *cl_getenv(const char *name)
|
||||
{
|
||||
return getenv(name);
|
||||
return getenv(name);
|
||||
}
|
||||
|
||||
int cl_setenv(const char *name, const char *value)
|
||||
|
@ -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] = (char *)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;
|
||||
}
|
||||
|
||||
@ -53,6 +67,10 @@ void test_core_env__cleanup(void)
|
||||
if (**val != '\0')
|
||||
(void)p_rmdir(*val);
|
||||
}
|
||||
|
||||
/* reset search paths to default */
|
||||
reset_global_search_path();
|
||||
reset_system_search_path();
|
||||
}
|
||||
|
||||
static void setenv_and_check(const char *name, const char *value)
|
||||
@ -60,6 +78,7 @@ 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
|
||||
@ -72,12 +91,11 @@ 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 +114,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 +123,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 +137,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 +147,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 +157,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 +171,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 +184,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 +194,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 +205,99 @@ 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);
|
||||
}
|
||||
|
||||
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;
|
||||
char testfile[16], tidx = '0';
|
||||
char **val;
|
||||
const char *testname = "alternate";
|
||||
size_t testlen = strlen(testname);
|
||||
|
||||
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 && errno != EEXIST) {
|
||||
*val = ""; /* mark as not created */
|
||||
continue;
|
||||
}
|
||||
|
||||
cl_git_pass(git_path_prettify(&path, *val, NULL));
|
||||
|
||||
/* 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));
|
||||
cl_git_mkfile(path.ptr, "find me");
|
||||
git_buf_rtruncate_at_char(&path, '/');
|
||||
|
||||
/* default should be NOTFOUND */
|
||||
cl_assert_equal_i(
|
||||
GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
git_buf_free(&path);
|
||||
git_buf_free(&found);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user