diff --git a/src/unix/posix.h b/src/unix/posix.h index 5c74f0779..881e651f4 100644 --- a/src/unix/posix.h +++ b/src/unix/posix.h @@ -20,5 +20,6 @@ #define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a) #define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__) #define p_mkstemp(p) mkstemp(p) +#define p_setenv(n,v,o) setenv(n,v,o) #endif diff --git a/src/win32/posix.c b/src/win32/posix.c index 38e505e20..8038fdea2 100644 --- a/src/win32/posix.c +++ b/src/win32/posix.c @@ -252,3 +252,11 @@ int p_mkstemp(char *tmp_path) return p_creat(tmp_path, 0744); } + +int p_setenv(const char* name, const char* value, int overwrite) +{ + if (overwrite != 1) + return EINVAL; + + return (SetEnvironmentVariableA(name, value) == 0 ? GIT_EOSERR : GIT_SUCCESS); +} diff --git a/src/win32/posix.h b/src/win32/posix.h index db4ec19ec..58fd05007 100644 --- a/src/win32/posix.h +++ b/src/win32/posix.h @@ -32,5 +32,6 @@ extern char *p_realpath(const char *orig_path, char *buffer); extern int p_vsnprintf(char *buffer, size_t count, const char *format, va_list argptr); extern int p_snprintf(char *buffer, size_t count, const char *format, ...) GIT_FORMAT_PRINTF(3, 4); extern int p_mkstemp(char *tmp_path); +extern int p_setenv(const char* name, const char* value, int overwrite); #endif diff --git a/tests/t15-config.c b/tests/t15-config.c index fdfa092ef..d912abb8e 100644 --- a/tests/t15-config.c +++ b/tests/t15-config.c @@ -26,6 +26,7 @@ #include "test_helpers.h" #include +#include #include "filebuf.h" #define CONFIG_BASE TEST_RESOURCES "/config" @@ -217,7 +218,7 @@ BEGIN_TEST(config10, "a repo's config overrides the global config") char *old_home; old_home = git__strdup(getenv("HOME")); - setenv("HOME", CONFIG_BASE, 1); + p_setenv("HOME", CONFIG_BASE, 1); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, NULL)); @@ -226,7 +227,7 @@ BEGIN_TEST(config10, "a repo's config overrides the global config") git_config_free(cfg); git_repository_free(repo); - setenv("HOME", old_home, 1); + p_setenv("HOME", old_home, 1); free(old_home); END_TEST @@ -237,7 +238,7 @@ BEGIN_TEST(config11, "fall back to the global config") char *old_home; old_home = git__strdup(getenv("HOME")); - setenv("HOME", CONFIG_BASE, 1); + p_setenv("HOME", CONFIG_BASE, 1); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, NULL)); @@ -246,7 +247,7 @@ BEGIN_TEST(config11, "fall back to the global config") git_config_free(cfg); git_repository_free(repo); - setenv("HOME", old_home, 1); + p_setenv("HOME", old_home, 1); free(old_home); END_TEST diff --git a/tests/t16-remotes.c b/tests/t16-remotes.c index 6529f0ec4..af54f297d 100644 --- a/tests/t16-remotes.c +++ b/tests/t16-remotes.c @@ -26,6 +26,7 @@ #include "test_helpers.h" #include +#include BEGIN_TEST(remotes0, "remote parsing works") git_remote *remote; @@ -34,7 +35,7 @@ BEGIN_TEST(remotes0, "remote parsing works") char *old_home; old_home = git__strdup(getenv("HOME")); - setenv("HOME", "/dev/null", 1); + p_setenv("HOME", "/dev/null", 1); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, NULL)); @@ -46,7 +47,7 @@ BEGIN_TEST(remotes0, "remote parsing works") git_config_free(cfg); git_repository_free(repo); - setenv("HOME", old_home, 1); + p_setenv("HOME", old_home, 1); free(old_home); END_TEST @@ -58,7 +59,7 @@ BEGIN_TEST(refspec0, "remote with refspec works") char *old_home; old_home = git__strdup(getenv("HOME")); - setenv("HOME", "/dev/null", 1); + p_setenv("HOME", "/dev/null", 1); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, NULL)); @@ -71,7 +72,7 @@ BEGIN_TEST(refspec0, "remote with refspec works") git_config_free(cfg); git_repository_free(repo); - setenv("HOME", old_home, 1); + p_setenv("HOME", old_home, 1); free(old_home); END_TEST @@ -83,7 +84,7 @@ BEGIN_TEST(refspec1, "remote fnmatch works as expected") char *old_home; old_home = git__strdup(getenv("HOME")); - setenv("HOME", "/dev/null", 1); + p_setenv("HOME", "/dev/null", 1); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, NULL)); @@ -96,7 +97,7 @@ BEGIN_TEST(refspec1, "remote fnmatch works as expected") git_config_free(cfg); git_repository_free(repo); - setenv("HOME", old_home, 1); + p_setenv("HOME", old_home, 1); free(old_home); END_TEST @@ -109,7 +110,7 @@ BEGIN_TEST(refspec2, "refspec transform") char *old_home; old_home = git__strdup(getenv("HOME")); - setenv("HOME", "/dev/null", 1); + p_setenv("HOME", "/dev/null", 1); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, NULL)); @@ -122,7 +123,7 @@ BEGIN_TEST(refspec2, "refspec transform") git_config_free(cfg); git_repository_free(repo); - setenv("HOME", old_home, 1); + p_setenv("HOME", old_home, 1); free(old_home); END_TEST