@@ -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 */
11401140static 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 */
11881189static 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,
13781380const 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