Skip to content

Commit 864095f

Browse files
dscho마누엘
authored andcommitted
Windows: add support for a Windows-wide configuration
Between the libgit2 and the Git for Windows project, there has been a discussion how we could share Git configuration to avoid duplication (or worse: skew). Earlier, libgit2 was nice enough to just re-use Git for Windows' C:\Program Files (x86)\Git\etc\gitconfig but with the upcoming Git for Windows 2.x, there would be more paths to search, as we will have 64-bit and 32-bit versions, and the corresponding config files will be in %PROGRAMFILES%\Git\mingw64\etc and ...\mingw32\etc, respectively. Therefore we came to a consensus to use %PROGRAMDATA%\Git as the location for Git-specific files that are of wider interest than just Git for Windows. On XP, there is no %PROGRAMDATA%, therefore we need to use "%ALLUSERSPROFILE%\Application Data\Git\config" in those setups. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ee430c1 commit 864095f

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

compat/mingw.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "../git-compat-util.h"
22
#include "win32.h"
33
#include <conio.h>
4+
#include <shlobj.h>
45
#include <wchar.h>
56
#include "../strbuf.h"
67
#include "../run-command.h"
@@ -2371,3 +2372,20 @@ void mingw_startup()
23712372
/* init length of current directory for handle_long_path */
23722373
current_directory_len = GetCurrentDirectoryW(0, NULL);
23732374
}
2375+
2376+
const char *windows_wide_config(void)
2377+
{
2378+
static struct strbuf windows_wide = STRBUF_INIT;
2379+
if (!windows_wide.len) {
2380+
wchar_t wbuffer[MAX_PATH];
2381+
if (SHGetFolderPathW(NULL, CSIDL_COMMON_APPDATA, NULL,
2382+
SHGFP_TYPE_CURRENT, wbuffer) != S_OK)
2383+
strbuf_addch(&windows_wide, '\0');
2384+
else {
2385+
char buffer[MAX_PATH];
2386+
xwcstoutf(buffer, wbuffer, sizeof(buffer));
2387+
strbuf_addf(&windows_wide, "%s\\Git\\config", buffer);
2388+
}
2389+
}
2390+
return *windows_wide.buf ? windows_wide.buf : NULL;
2391+
}

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ static inline char *mingw_find_last_dir_sep(const char *path)
392392
int mingw_offset_1st_component(const char *path);
393393
#define offset_1st_component mingw_offset_1st_component
394394
#define PATH_SEP ';'
395+
extern const char *windows_wide_config(void);
396+
#define git_super_config windows_wide_config
395397
#ifndef __MINGW64_VERSION_MAJOR
396398
#define PRIuMAX "I64u"
397399
#define PRId64 "I64d"

config.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,11 +1204,18 @@ int git_config_system(void)
12041204
int git_config_early(config_fn_t fn, void *data, const char *repo_config)
12051205
{
12061206
int ret = 0, found = 0;
1207+
const char *super_config = git_super_config();
12071208
char *xdg_config = NULL;
12081209
char *user_config = NULL;
12091210

12101211
home_config_paths(&user_config, &xdg_config, "config");
12111212

1213+
if (super_config && git_config_system() &&
1214+
!access(super_config, R_OK)) {
1215+
ret += git_config_from_file(fn, super_config, data);
1216+
found += 1;
1217+
}
1218+
12121219
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
12131220
ret += git_config_from_file(fn, git_etc_gitconfig(),
12141221
data);

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ static inline char *git_find_last_dir_sep(const char *path)
306306
#define find_last_dir_sep git_find_last_dir_sep
307307
#endif
308308

309+
#ifndef git_super_config
310+
#define git_super_config() NULL
311+
#endif
312+
309313
#if defined(__HP_cc) && (__HP_cc >= 61000)
310314
#define NORETURN __attribute__((noreturn))
311315
#define NORETURN_PTR

0 commit comments

Comments
 (0)