Merge pull request #3475 from libgit2/cmn/programdata-config config: add a ProgramData level
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
diff --git a/include/git2/config.h b/include/git2/config.h
index 56b5431..d0f1ba1 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -29,25 +29,28 @@ GIT_BEGIN_DECL
* priority levels as well.
*/
typedef enum {
+ /** System-wide on Windows, for compatibility with portable git */
+ GIT_CONFIG_LEVEL_PROGRAMDATA = 1,
+
/** System-wide configuration file; /etc/gitconfig on Linux systems */
- GIT_CONFIG_LEVEL_SYSTEM = 1,
+ GIT_CONFIG_LEVEL_SYSTEM = 2,
/** XDG compatible configuration file; typically ~/.config/git/config */
- GIT_CONFIG_LEVEL_XDG = 2,
+ GIT_CONFIG_LEVEL_XDG = 3,
/** User-specific configuration file (also called Global configuration
* file); typically ~/.gitconfig
*/
- GIT_CONFIG_LEVEL_GLOBAL = 3,
+ GIT_CONFIG_LEVEL_GLOBAL = 4,
/** Repository specific configuration file; $WORK_DIR/.git/config on
* non-bare repos
*/
- GIT_CONFIG_LEVEL_LOCAL = 4,
+ GIT_CONFIG_LEVEL_LOCAL = 5,
/** Application specific configuration file; freely defined by applications
*/
- GIT_CONFIG_LEVEL_APP = 5,
+ GIT_CONFIG_LEVEL_APP = 6,
/** Represents the highest level available config file (i.e. the most
* specific config file available that actually is loaded)
@@ -142,6 +145,17 @@ GIT_EXTERN(int) git_config_find_xdg(git_buf *out);
GIT_EXTERN(int) git_config_find_system(git_buf *out);
/**
+ * Locate the path to the configuration file in ProgramData
+ *
+ * Look for the file in %PROGRAMDATA%\Git\config used by portable git.
+ *
+ * @param out Pointer to a user-allocated git_buf in which to store the path
+ * @return 0 if a ProgramData configuration file has been
+ * found. Its path will be stored in `out`.
+ */
+GIT_EXTERN(int) git_config_find_programdata(git_buf *out);
+
+/**
* Open the global, XDG and system configuration files
*
* Utility wrapper that finds the global, XDG and system configuration files
diff --git a/src/config.c b/src/config.c
index f0b2c3a..f4d4cb2 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1086,6 +1086,12 @@ int git_config_find_system(git_buf *path)
return git_sysdir_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
}
+int git_config_find_programdata(git_buf *path)
+{
+ git_buf_sanitize(path);
+ return git_sysdir_find_programdata_file(path, GIT_CONFIG_FILENAME_PROGRAMDATA);
+}
+
int git_config__global_location(git_buf *buf)
{
const git_buf *paths;
@@ -1133,6 +1139,10 @@ int git_config_open_default(git_config **out)
error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_SYSTEM, 0);
+ if (!error && !git_config_find_programdata(&buf))
+ error = git_config_add_file_ondisk(cfg, buf.ptr,
+ GIT_CONFIG_LEVEL_PROGRAMDATA, 0);
+
git_buf_free(&buf);
if (error) {
diff --git a/src/config.h b/src/config.h
index ba74533..00c12b5 100644
--- a/src/config.h
+++ b/src/config.h
@@ -12,6 +12,7 @@
#include "vector.h"
#include "repository.h"
+#define GIT_CONFIG_FILENAME_PROGRAMDATA "config"
#define GIT_CONFIG_FILENAME_SYSTEM "gitconfig"
#define GIT_CONFIG_FILENAME_GLOBAL ".gitconfig"
#define GIT_CONFIG_FILENAME_XDG "config"
diff --git a/src/repository.c b/src/repository.c
index 77145cf..38d1869 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -585,7 +585,8 @@ static int load_config(
git_repository *repo,
const char *global_config_path,
const char *xdg_config_path,
- const char *system_config_path)
+ const char *system_config_path,
+ const char *programdata_path)
{
int error;
git_buf config_path = GIT_BUF_INIT;
@@ -626,6 +627,12 @@ static int load_config(
error != GIT_ENOTFOUND)
goto on_error;
+ if (programdata_path != NULL &&
+ (error = git_config_add_file_ondisk(
+ cfg, programdata_path, GIT_CONFIG_LEVEL_PROGRAMDATA, 0)) < 0 &&
+ error != GIT_ENOTFOUND)
+ goto on_error;
+
giterr_clear(); /* clear any lingering ENOTFOUND errors */
*out = cfg;
@@ -651,11 +658,13 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
git_buf global_buf = GIT_BUF_INIT;
git_buf xdg_buf = GIT_BUF_INIT;
git_buf system_buf = GIT_BUF_INIT;
+ git_buf programdata_buf = GIT_BUF_INIT;
git_config *config;
git_config_find_global(&global_buf);
git_config_find_xdg(&xdg_buf);
git_config_find_system(&system_buf);
+ git_config_find_programdata(&programdata_buf);
/* If there is no global file, open a backend for it anyway */
if (git_buf_len(&global_buf) == 0)
@@ -665,7 +674,8 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
&config, repo,
path_unless_empty(&global_buf),
path_unless_empty(&xdg_buf),
- path_unless_empty(&system_buf));
+ path_unless_empty(&system_buf),
+ path_unless_empty(&programdata_buf));
if (!error) {
GIT_REFCOUNT_OWN(config, repo);
diff --git a/src/sysdir.c b/src/sysdir.c
index 2795de4..bf53d83 100644
--- a/src/sysdir.c
+++ b/src/sysdir.c
@@ -15,6 +15,16 @@
#include "win32/findfile.h"
#endif
+static int git_sysdir_guess_programdata_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+ return git_win32__find_programdata_dirs(out);
+#else
+ git_buf_clear(out);
+ return 0;
+#endif
+}
+
static int git_sysdir_guess_system_dirs(git_buf *out)
{
#ifdef GIT_WIN32
@@ -76,12 +86,13 @@ static int git_sysdir_guess_template_dirs(git_buf *out)
typedef int (*git_sysdir_guess_cb)(git_buf *out);
static git_buf git_sysdir__dirs[GIT_SYSDIR__MAX] =
- { GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
+ { GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
static git_sysdir_guess_cb git_sysdir__dir_guess[GIT_SYSDIR__MAX] = {
git_sysdir_guess_system_dirs,
git_sysdir_guess_global_dirs,
git_sysdir_guess_xdg_dirs,
+ git_sysdir_guess_programdata_dirs,
git_sysdir_guess_template_dirs,
};
@@ -258,6 +269,12 @@ int git_sysdir_find_xdg_file(git_buf *path, const char *filename)
path, filename, GIT_SYSDIR_XDG, "global/xdg");
}
+int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
+{
+ return git_sysdir_find_in_dirlist(
+ path, filename, GIT_SYSDIR_PROGRAMDATA, "ProgramData");
+}
+
int git_sysdir_find_template_dir(git_buf *path)
{
return git_sysdir_find_in_dirlist(
diff --git a/src/sysdir.h b/src/sysdir.h
index f1bbf0b..12874fc 100644
--- a/src/sysdir.h
+++ b/src/sysdir.h
@@ -39,6 +39,15 @@ extern int git_sysdir_find_xdg_file(git_buf *path, const char *filename);
extern int git_sysdir_find_system_file(git_buf *path, const char *filename);
/**
+ * Find a "ProgramData" file (i.e. one in %PROGRAMDATA%)
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file to find in the ProgramData directory
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_programdata_file(git_buf *path, const char *filename);
+
+/**
* Find template directory.
*
* @param path buffer to write the full path into
@@ -50,8 +59,9 @@ typedef enum {
GIT_SYSDIR_SYSTEM = 0,
GIT_SYSDIR_GLOBAL = 1,
GIT_SYSDIR_XDG = 2,
- GIT_SYSDIR_TEMPLATE = 3,
- GIT_SYSDIR__MAX = 4,
+ GIT_SYSDIR_PROGRAMDATA = 3,
+ GIT_SYSDIR_TEMPLATE = 4,
+ GIT_SYSDIR__MAX = 5,
} git_sysdir_t;
/**
diff --git a/src/win32/findfile.c b/src/win32/findfile.c
index de27dd0..58c2227 100644
--- a/src/win32/findfile.c
+++ b/src/win32/findfile.c
@@ -215,3 +215,13 @@ int git_win32__find_xdg_dirs(git_buf *out)
return win32_find_existing_dirs(out, global_tmpls);
}
+
+int git_win32__find_programdata_dirs(git_buf *out)
+{
+ static const wchar_t *programdata_tmpls[2] = {
+ L"%PROGRAMDATA%\\Git",
+ NULL,
+ };
+
+ return win32_find_existing_dirs(out, programdata_tmpls);
+}
diff --git a/src/win32/findfile.h b/src/win32/findfile.h
index a50319b..3d5fff4 100644
--- a/src/win32/findfile.h
+++ b/src/win32/findfile.h
@@ -11,6 +11,7 @@
extern int git_win32__find_system_dirs(git_buf *out, const wchar_t *subpath);
extern int git_win32__find_global_dirs(git_buf *out);
extern int git_win32__find_xdg_dirs(git_buf *out);
+extern int git_win32__find_programdata_dirs(git_buf *out);
#endif
diff --git a/tests/config/global.c b/tests/config/global.c
index b5e83fe..1336ef6 100644
--- a/tests/config/global.c
+++ b/tests/config/global.c
@@ -65,3 +65,43 @@ void test_config_global__open_xdg(void)
git_config_free(xdg);
git_config_free(cfg);
}
+
+void test_config_global__open_programdata(void)
+{
+ char *programdata;
+ git_config *cfg;
+ git_repository *repo;
+ git_buf config_path = GIT_BUF_INIT;
+ git_buf var_contents = GIT_BUF_INIT;
+
+ if (!cl_getenv("GITTEST_INVASIVE_FS_STRUCTURE"))
+ cl_skip();
+
+ programdata = cl_getenv("PROGRAMDATA");
+ cl_git_pass(git_buf_printf(&config_path, "%s/Git", programdata));
+ cl_git_pass(p_mkdir(config_path.ptr, 0777));
+ cl_git_pass(git_buf_puts(&config_path, "/config"));
+
+ cl_git_pass(git_config_open_ondisk(&cfg, config_path.ptr));
+ cl_git_pass(git_config_set_string(cfg, "programdata.var", "even higher level"));
+
+ git_buf_free(&config_path);
+ git_config_free(cfg);
+
+ git_config_open_default(&cfg);
+ cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
+ cl_assert_equal_s("even higher level", var_contents.ptr);
+
+ git_config_free(cfg);
+ git_buf_free(&var_contents);
+
+ cl_git_pass(git_repository_init(&repo, "./foo.git", true));
+ cl_git_pass(git_repository_config(&cfg, repo));
+ cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
+ cl_assert_equal_s("even higher level", var_contents.ptr);
+
+ git_config_free(cfg);
+ git_buf_free(&var_contents);
+ git_repository_free(repo);
+ cl_fixture_cleanup("./foo.git");
+}