Skip to content

Commit 17d3883

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d51e1df commit 17d3883

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2673,7 +2673,7 @@ static PSID get_current_user_sid(void)
26732673
return result;
26742674
}
26752675

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)
26772677
{
26782678
WCHAR wpath[MAX_PATH];
26792679
PSID sid = NULL;

compat/mingw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ char *mingw_query_user_email(void);
463463
* Verifies that the specified path is owned by the user running the
464464
* current process.
465465
*/
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);
467467
#define is_path_owned_by_current_user is_path_owned_by_current_sid
468468

469469
/**

git-compat-util.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <crtdbg.h>
2424
#endif
2525

26+
struct strbuf;
27+
28+
2629
#define _FILE_OFFSET_BITS 64
2730

2831

@@ -487,7 +490,7 @@ static inline void extract_id_from_env(const char *env, uid_t *id)
487490
}
488491
}
489492

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)
491494
{
492495
struct stat st;
493496
uid_t euid;

setup.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,16 +1138,17 @@ static int safe_directory_cb(const char *key, const char *value, void *d)
11381138
* added, for bare ones their git directory.
11391139
*/
11401140
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)
11421143
{
11431144
struct safe_directory_data data = {
11441145
.path = worktree ? worktree : gitdir
11451146
};
11461147

11471148
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)))
11511152
return 1;
11521153

11531154
/*
@@ -1187,6 +1188,7 @@ enum discovery_result {
11871188
*/
11881189
static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
11891190
struct strbuf *gitdir,
1191+
struct strbuf *report,
11901192
int die_on_error)
11911193
{
11921194
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,
12751277
gitdir_path ? gitdir_path : gitdirenv;
12761278

12771279
if (ensure_valid_ownership(gitfile, dir->buf,
1278-
gitdir_candidate)) {
1280+
gitdir_candidate, report)) {
12791281
strbuf_addstr(gitdir, gitdirenv);
12801282
ret = GIT_DIR_DISCOVERED;
12811283
} else
@@ -1298,7 +1300,7 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
12981300
}
12991301

13001302
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))
13021304
return GIT_DIR_INVALID_OWNERSHIP;
13031305
strbuf_addstr(gitdir, ".");
13041306
return GIT_DIR_BARE;
@@ -1331,7 +1333,7 @@ int discover_git_directory(struct strbuf *commondir,
13311333
return -1;
13321334

13331335
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) {
13351337
strbuf_release(&dir);
13361338
return -1;
13371339
}
@@ -1378,7 +1380,7 @@ int discover_git_directory(struct strbuf *commondir,
13781380
const char *setup_git_directory_gently(int *nongit_ok)
13791381
{
13801382
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;
13821384
const char *prefix = NULL;
13831385
struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
13841386

@@ -1403,7 +1405,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
14031405
die_errno(_("Unable to read current working directory"));
14041406
strbuf_addbuf(&dir, &cwd);
14051407

1406-
switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) {
1408+
switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) {
14071409
case GIT_DIR_EXPLICIT:
14081410
prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok);
14091411
break;
@@ -1435,12 +1437,14 @@ const char *setup_git_directory_gently(int *nongit_ok)
14351437
if (!nongit_ok) {
14361438
struct strbuf quoted = STRBUF_INIT;
14371439

1440+
strbuf_complete(&report, '\n');
14381441
sq_quote_buf_pretty(&quoted, dir.buf);
14391442
die(_("detected dubious ownership in repository at '%s'\n"
1443+
"%s"
14401444
"To add an exception for this directory, call:\n"
14411445
"\n"
14421446
"\tgit config --global --add safe.directory %s"),
1443-
dir.buf, quoted.buf);
1447+
dir.buf, report.buf, quoted.buf);
14441448
}
14451449
*nongit_ok = 1;
14461450
break;
@@ -1519,6 +1523,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
15191523

15201524
strbuf_release(&dir);
15211525
strbuf_release(&gitdir);
1526+
strbuf_release(&report);
15221527
clear_repository_format(&repo_fmt);
15231528

15241529
return prefix;

0 commit comments

Comments
 (0)