Skip to content

Commit dae696a

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Allow debugging unsafe directories' ownership
When Git refuses to use an existing repository because it is owned by someone else than the current user, it can be a bit tricky on Windows to figure out what is going on. Let's help with that by offering some more information via the environment variable `GIT_TEST_DEBUG_UNSAFE_DIRECTORIES`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ff841f1 commit dae696a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Documentation/config/safe.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ which id the original user has.
4040
If that is not what you would prefer and want git to only trust
4141
repositories that are owned by root instead, then you can remove
4242
the `SUDO_UID` variable from root's environment before invoking git.
43+
+
44+
Due to the permission model on Windows where ACLs are used instead of
45+
Unix' simpler permission model, it can be a bit tricky to figure out why
46+
a directory is considered unsafe. To help with this, Git will provide
47+
more detailed information when the environment variable
48+
`GIT_TEST_DEBUG_UNSAFE_DIRECTORIES` is set to `true`.

compat/mingw.c

Lines changed: 21 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 <aclapi.h>
4+
#include <sddl.h>
45
#include <conio.h>
56
#include <wchar.h>
67
#include "../strbuf.h"
@@ -2717,6 +2718,26 @@ int is_path_owned_by_current_sid(const char *path)
27172718
IsValidSid(current_user_sid) &&
27182719
EqualSid(sid, current_user_sid))
27192720
result = 1;
2721+
else if (git_env_bool("GIT_TEST_DEBUG_UNSAFE_DIRECTORIES", 0)) {
2722+
LPSTR str1, str2, to_free1 = NULL, to_free2 = NULL;
2723+
2724+
if (ConvertSidToStringSidA(sid, &str1))
2725+
to_free1 = str1;
2726+
else
2727+
str1 = "(inconvertible)";
2728+
2729+
if (!current_user_sid)
2730+
str2 = "(none)";
2731+
else if (!IsValidSid(current_user_sid))
2732+
str2 = "(invalid)";
2733+
else if (ConvertSidToStringSidA(current_user_sid, &str2))
2734+
to_free2 = str2;
2735+
else
2736+
str2 = "(inconvertible)";
2737+
warning("'%s' is owned by:\n\t'%s'\nbut the current user is:\n\t'%s'", path, str1, str2);
2738+
LocalFree(to_free1);
2739+
LocalFree(to_free2);
2740+
}
27202741
}
27212742

27222743
/*

setup.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,13 +1433,23 @@ const char *setup_git_directory_gently(int *nongit_ok)
14331433
case GIT_DIR_INVALID_OWNERSHIP:
14341434
if (!nongit_ok) {
14351435
struct strbuf quoted = STRBUF_INIT;
1436+
struct strbuf hint = STRBUF_INIT;
1437+
1438+
#ifdef __MINGW32__
1439+
if (!git_env_bool("GIT_TEST_DEBUG_UNSAFE_DIRECTORIES", 0))
1440+
strbuf_addstr(&hint,
1441+
_("\n\nSet the environment variable "
1442+
"GIT_TEST_DEBUG_UNSAFE_DIRECTORIES=true "
1443+
"and run\n"
1444+
"again for more information."));
1445+
#endif
14361446

14371447
sq_quote_buf_pretty(&quoted, dir.buf);
14381448
die(_("detected dubious ownership in repository at '%s'\n"
14391449
"To add an exception for this directory, call:\n"
14401450
"\n"
1441-
"\tgit config --global --add safe.directory %s"),
1442-
dir.buf, quoted.buf);
1451+
"\tgit config --global --add safe.directory %s%s"),
1452+
dir.buf, quoted.buf, hint.buf);
14431453
}
14441454
*nongit_ok = 1;
14451455
break;

0 commit comments

Comments
 (0)