Skip to content

Commit 4fead63

Browse files
authored
Merge pull request libgit2#6180 from libgit2/ethomson/win32_findfile_fixes
win32: update git for windows compatibility
2 parents 258df9c + 475c6eb commit 4fead63

File tree

10 files changed

+796
-107
lines changed

10 files changed

+796
-107
lines changed

src/fs_path.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,3 +1851,59 @@ int git_fs_path_validate_system_file_ownership(const char *path)
18511851
return ret;
18521852
#endif
18531853
}
1854+
1855+
int git_fs_path_find_executable(git_str *fullpath, const char *executable)
1856+
{
1857+
#ifdef GIT_WIN32
1858+
git_win32_path fullpath_w, executable_w;
1859+
int error;
1860+
1861+
if (git__utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0)
1862+
return -1;
1863+
1864+
error = git_win32_path_find_executable(fullpath_w, executable_w);
1865+
1866+
if (error == 0)
1867+
error = git_str_put_w(fullpath, fullpath_w, wcslen(fullpath_w));
1868+
1869+
return error;
1870+
#else
1871+
git_str path = GIT_STR_INIT;
1872+
const char *current_dir, *term;
1873+
bool found = false;
1874+
1875+
if (git__getenv(&path, "PATH") < 0)
1876+
return -1;
1877+
1878+
current_dir = path.ptr;
1879+
1880+
while (*current_dir) {
1881+
if (! (term = strchr(current_dir, GIT_PATH_LIST_SEPARATOR)))
1882+
term = strchr(current_dir, '\0');
1883+
1884+
git_str_clear(fullpath);
1885+
if (git_str_put(fullpath, current_dir, (term - current_dir)) < 0 ||
1886+
git_str_putc(fullpath, '/') < 0 ||
1887+
git_str_puts(fullpath, executable) < 0)
1888+
return -1;
1889+
1890+
if (git_fs_path_isfile(fullpath->ptr)) {
1891+
found = true;
1892+
break;
1893+
}
1894+
1895+
current_dir = term;
1896+
1897+
while (*current_dir == GIT_PATH_LIST_SEPARATOR)
1898+
current_dir++;
1899+
}
1900+
1901+
git_str_dispose(&path);
1902+
1903+
if (found)
1904+
return 0;
1905+
1906+
git_str_clear(fullpath);
1907+
return GIT_ENOTFOUND;
1908+
#endif
1909+
}

src/fs_path.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,4 +743,10 @@ bool git_fs_path_supports_symlinks(const char *dir);
743743
*/
744744
int git_fs_path_validate_system_file_ownership(const char *path);
745745

746+
/**
747+
* Search the current PATH for the given executable, returning the full
748+
* path if it is found.
749+
*/
750+
int git_fs_path_find_executable(git_str *fullpath, const char *executable);
751+
746752
#endif

src/sysdir.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int git_sysdir_guess_programdata_dirs(git_str *out)
3131
static int git_sysdir_guess_system_dirs(git_str *out)
3232
{
3333
#ifdef GIT_WIN32
34-
return git_win32__find_system_dirs(out, L"etc\\");
34+
return git_win32__find_system_dirs(out, "etc");
3535
#else
3636
return git_str_sets(out, "/etc");
3737
#endif
@@ -154,7 +154,7 @@ static int git_sysdir_guess_xdg_dirs(git_str *out)
154154
static int git_sysdir_guess_template_dirs(git_str *out)
155155
{
156156
#ifdef GIT_WIN32
157-
return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
157+
return git_win32__find_system_dirs(out, "share/git-core/templates");
158158
#else
159159
return git_str_sets(out, "/usr/share/git-core/templates");
160160
#endif
@@ -189,9 +189,25 @@ int git_sysdir_global_init(void)
189189
for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
190190
error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
191191

192+
if (error)
193+
return error;
194+
192195
return git_runtime_shutdown_register(git_sysdir_global_shutdown);
193196
}
194197

198+
int git_sysdir_reset(void)
199+
{
200+
size_t i;
201+
int error = 0;
202+
203+
for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); ++i) {
204+
git_str_dispose(&git_sysdir__dirs[i].buf);
205+
error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
206+
}
207+
208+
return error;
209+
}
210+
195211
static int git_sysdir_check_selector(git_sysdir_t which)
196212
{
197213
if (which < ARRAY_SIZE(git_sysdir__dirs))

src/sysdir.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,9 @@ extern int git_sysdir_get(const git_str **out, git_sysdir_t which);
105105
*/
106106
extern int git_sysdir_set(git_sysdir_t which, const char *paths);
107107

108+
/**
109+
* Reset search paths for global/system/xdg files.
110+
*/
111+
extern int git_sysdir_reset(void);
112+
108113
#endif

0 commit comments

Comments
 (0)