]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: prepare for more detailed "dubious ownership" messages
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 8 Aug 2022 13:27:47 +0000 (13:27 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Aug 2022 16:25:40 +0000 (09:25 -0700)
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>
compat/mingw.c
compat/mingw.h
git-compat-util.h
setup.c

index 2607de93af594034413e4512c4d7dd33fb133023..f12b7df16d91956c07ea99de8fbe52dbd3173f2a 100644 (file)
@@ -2673,7 +2673,7 @@ static PSID get_current_user_sid(void)
        return result;
 }
 
-int is_path_owned_by_current_sid(const char *path)
+int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
 {
        WCHAR wpath[MAX_PATH];
        PSID sid = NULL;
index a74da68f31321c18b25ed86299f7d84fa38a69aa..209cf7cebadd1746c8b79e1105bac103b8a72910 100644 (file)
@@ -463,7 +463,7 @@ char *mingw_query_user_email(void);
  * Verifies that the specified path is owned by the user running the
  * current process.
  */
-int is_path_owned_by_current_sid(const char *path);
+int is_path_owned_by_current_sid(const char *path, struct strbuf *report);
 #define is_path_owned_by_current_user is_path_owned_by_current_sid
 
 /**
index 58d7708296b614dbb6fd0bd9feb403d2a8c473ce..36a25ae252f24d23852c5cdd810936df1c23558f 100644 (file)
@@ -23,6 +23,9 @@
 #include <crtdbg.h>
 #endif
 
+struct strbuf;
+
+
 #define _FILE_OFFSET_BITS 64
 
 
@@ -487,7 +490,7 @@ static inline void extract_id_from_env(const char *env, uid_t *id)
        }
 }
 
-static inline int is_path_owned_by_current_uid(const char *path)
+static inline int is_path_owned_by_current_uid(const char *path, struct strbuf *report)
 {
        struct stat st;
        uid_t euid;
diff --git a/setup.c b/setup.c
index 0fdecec32e81852fe857f6f18ac620b543749685..ddcf6eb6035cd42709a46f84a71460a097ff9d9b 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -1138,16 +1138,17 @@ static int safe_directory_cb(const char *key, const char *value, void *d)
  * added, for bare ones their git directory.
  */
 static int ensure_valid_ownership(const char *gitfile,
-                                 const char *worktree, const char *gitdir)
+                                 const char *worktree, const char *gitdir,
+                                 struct strbuf *report)
 {
        struct safe_directory_data data = {
                .path = worktree ? worktree : gitdir
        };
 
        if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
-          (!gitfile || is_path_owned_by_current_user(gitfile)) &&
-          (!worktree || is_path_owned_by_current_user(worktree)) &&
-          (!gitdir || is_path_owned_by_current_user(gitdir)))
+           (!gitfile || is_path_owned_by_current_user(gitfile, report)) &&
+           (!worktree || is_path_owned_by_current_user(worktree, report)) &&
+           (!gitdir || is_path_owned_by_current_user(gitdir, report)))
                return 1;
 
        /*
@@ -1187,6 +1188,7 @@ enum discovery_result {
  */
 static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
                                                          struct strbuf *gitdir,
+                                                         struct strbuf *report,
                                                          int die_on_error)
 {
        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,
                                gitdir_path ? gitdir_path : gitdirenv;
 
                        if (ensure_valid_ownership(gitfile, dir->buf,
-                                                  gitdir_candidate)) {
+                                                  gitdir_candidate, report)) {
                                strbuf_addstr(gitdir, gitdirenv);
                                ret = GIT_DIR_DISCOVERED;
                        } else
@@ -1298,7 +1300,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
                }
 
                if (is_git_directory(dir->buf)) {
-                       if (!ensure_valid_ownership(NULL, NULL, dir->buf))
+                       if (!ensure_valid_ownership(NULL, NULL, dir->buf, report))
                                return GIT_DIR_INVALID_OWNERSHIP;
                        strbuf_addstr(gitdir, ".");
                        return GIT_DIR_BARE;
@@ -1331,7 +1333,7 @@ int discover_git_directory(struct strbuf *commondir,
                return -1;
 
        cwd_len = dir.len;
-       if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
+       if (setup_git_directory_gently_1(&dir, gitdir, NULL, 0) <= 0) {
                strbuf_release(&dir);
                return -1;
        }
@@ -1378,7 +1380,7 @@ int discover_git_directory(struct strbuf *commondir,
 const char *setup_git_directory_gently(int *nongit_ok)
 {
        static struct strbuf cwd = STRBUF_INIT;
-       struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
+       struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
        const char *prefix = NULL;
        struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
 
@@ -1403,7 +1405,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
                die_errno(_("Unable to read current working directory"));
        strbuf_addbuf(&dir, &cwd);
 
-       switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) {
+       switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) {
        case GIT_DIR_EXPLICIT:
                prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok);
                break;
@@ -1435,12 +1437,14 @@ const char *setup_git_directory_gently(int *nongit_ok)
                if (!nongit_ok) {
                        struct strbuf quoted = STRBUF_INIT;
 
+                       strbuf_complete(&report, '\n');
                        sq_quote_buf_pretty(&quoted, dir.buf);
                        die(_("detected dubious ownership in repository at '%s'\n"
+                             "%s"
                              "To add an exception for this directory, call:\n"
                              "\n"
                              "\tgit config --global --add safe.directory %s"),
-                           dir.buf, quoted.buf);
+                           dir.buf, report.buf, quoted.buf);
                }
                *nongit_ok = 1;
                break;
@@ -1519,6 +1523,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 
        strbuf_release(&dir);
        strbuf_release(&gitdir);
+       strbuf_release(&report);
        clear_repository_format(&repo_fmt);
 
        return prefix;