setup: prepare for more detailed "dubious ownership" messages

When verifying the ownership of the Git directory, we sometimes would like to say a bit more about it, e.g. when using a platform-dependent code path (think: Windows has the permission model that is so different from Unix'), but only when it is a appropriate to actually say something. To allow for that, collect that information and hand it back to the caller (whose responsibility it is to show it or not). Note: We do not actually fill in any platform-dependent information yet, this commit just adds the infrastructure to be able to do so. Based-on-an-idea-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Aug 8, 2022 at 13:27 UTC 17d3883fe9c88b823002ad9fafb42313ddc3d3d5
4 files changed +21 -13
compat/mingw.c
+1 -1
@@ -2673,7 +2673,7 @@ static PSID get_current_user_sid(void)
2673 return result;
2674 }
2675
2676 -int is_path_owned_by_current_sid(const char *path)
2676 +int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
2677 {
2678 WCHAR wpath[MAX_PATH];
2679 PSID sid = NULL;
compat/mingw.h
+1 -1
@@ -463,7 +463,7 @@ char *mingw_query_user_email(void);
463 * Verifies that the specified path is owned by the user running the
464 * current process.
465 */
466 -int is_path_owned_by_current_sid(const char *path);
466 +int is_path_owned_by_current_sid(const char *path, struct strbuf *report);
467 #define is_path_owned_by_current_user is_path_owned_by_current_sid
468
469 /**
git-compat-util.h
+4 -1
@@ -23,6 +23,9 @@
23 #include <crtdbg.h>
24 #endif
25
26 +struct strbuf;
27 +
28 +
29 #define _FILE_OFFSET_BITS 64
30
31
@@ -487,7 +490,7 @@ static inline void extract_id_from_env(const char *env, uid_t *id)
490 }
491 }
492
490 -static inline int is_path_owned_by_current_uid(const char *path)
493 +static inline int is_path_owned_by_current_uid(const char *path, struct strbuf *report)
494 {
495 struct stat st;
496 uid_t euid;
setup.c
+15 -10
@@ -1138,16 +1138,17 @@ static int safe_directory_cb(const char *key, const char *value, void *d)
1138 * added, for bare ones their git directory.
1139 */
1140 static int ensure_valid_ownership(const char *gitfile,
1141 - const char *worktree, const char *gitdir)
1141 + const char *worktree, const char *gitdir,
1142 + struct strbuf *report)
1143 {
1144 struct safe_directory_data data = {
1145 .path = worktree ? worktree : gitdir
1146 };
1147
1148 if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
1148 - (!gitfile || is_path_owned_by_current_user(gitfile)) &&
1149 - (!worktree || is_path_owned_by_current_user(worktree)) &&
1150 - (!gitdir || is_path_owned_by_current_user(gitdir)))
1149 + (!gitfile || is_path_owned_by_current_user(gitfile, report)) &&
1150 + (!worktree || is_path_owned_by_current_user(worktree, report)) &&
1151 + (!gitdir || is_path_owned_by_current_user(gitdir, report)))
1152 return 1;
1153
1154 /*
@@ -1187,6 +1188,7 @@ enum discovery_result {
1188 */
1189 static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
1190 struct strbuf *gitdir,
1191 + struct strbuf *report,
1192 int die_on_error)
1193 {
1194 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
@@ -1275,7 +1277,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
1277 gitdir_path ? gitdir_path : gitdirenv;
1278
1279 if (ensure_valid_ownership(gitfile, dir->buf,
1278 - gitdir_candidate)) {
1280 + gitdir_candidate, report)) {
1281 strbuf_addstr(gitdir, gitdirenv);
1282 ret = GIT_DIR_DISCOVERED;
1283 } else
@@ -1298,7 +1300,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
1300 }
1301
1302 if (is_git_directory(dir->buf)) {
1301 - if (!ensure_valid_ownership(NULL, NULL, dir->buf))
1303 + if (!ensure_valid_ownership(NULL, NULL, dir->buf, report))
1304 return GIT_DIR_INVALID_OWNERSHIP;
1305 strbuf_addstr(gitdir, ".");
1306 return GIT_DIR_BARE;
@@ -1331,7 +1333,7 @@ int discover_git_directory(struct strbuf *commondir,
1333 return -1;
1334
1335 cwd_len = dir.len;
1334 - if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
1336 + if (setup_git_directory_gently_1(&dir, gitdir, NULL, 0) <= 0) {
1337 strbuf_release(&dir);
1338 return -1;
1339 }
@@ -1378,7 +1380,7 @@ int discover_git_directory(struct strbuf *commondir,
1380 const char *setup_git_directory_gently(int *nongit_ok)
1381 {
1382 static struct strbuf cwd = STRBUF_INIT;
1381 - struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
1383 + struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
1384 const char *prefix = NULL;
1385 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
1386
@@ -1403,7 +1405,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
1405 die_errno(_("Unable to read current working directory"));
1406 strbuf_addbuf(&dir, &cwd);
1407
1406 - switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) {
1408 + switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) {
1409 case GIT_DIR_EXPLICIT:
1410 prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok);
1411 break;
@@ -1435,12 +1437,14 @@ const char *setup_git_directory_gently(int *nongit_ok)
1437 if (!nongit_ok) {
1438 struct strbuf quoted = STRBUF_INIT;
1439
1440 + strbuf_complete(&report, '\n');
1441 sq_quote_buf_pretty(&quoted, dir.buf);
1442 die(_("detected dubious ownership in repository at '%s'\n"
1443 + "%s"
1444 "To add an exception for this directory, call:\n"
1445 "\n"
1446 "\tgit config --global --add safe.directory %s"),
1443 - dir.buf, quoted.buf);
1447 + dir.buf, report.buf, quoted.buf);
1448 }
1449 *nongit_ok = 1;
1450 break;
@@ -1519,6 +1523,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
1523
1524 strbuf_release(&dir);
1525 strbuf_release(&gitdir);
1526 + strbuf_release(&report);
1527 clear_repository_format(&repo_fmt);
1528
1529 return prefix;