@@ -1119,14 +1119,32 @@ static int safe_directory_cb(const char *key, const char *value, void *d)
11191119 return 0 ;
11201120}
11211121
1122- static int ensure_valid_ownership (const char * path )
1122+ /*
1123+ * Check if a repository is safe, by verifying the ownership of the
1124+ * worktree (if any), the git directory, and the gitfile (if any).
1125+ *
1126+ * Exemptions for known-safe repositories can be added via `safe.directory`
1127+ * config settings; for non-bare repositories, their worktree needs to be
1128+ * added, for bare ones their git directory.
1129+ */
1130+ static int ensure_valid_ownership (const char * gitfile ,
1131+ const char * worktree , const char * gitdir )
11231132{
1124- struct safe_directory_data data = { .path = path };
1133+ struct safe_directory_data data = {
1134+ .path = worktree ? worktree : gitdir
1135+ };
11251136
11261137 if (!git_env_bool ("GIT_TEST_ASSUME_DIFFERENT_OWNER" , 0 ) &&
1127- is_path_owned_by_current_user (path ))
1138+ (!gitfile || is_path_owned_by_current_user (gitfile )) &&
1139+ (!worktree || is_path_owned_by_current_user (worktree )) &&
1140+ (!gitdir || is_path_owned_by_current_user (gitdir )))
11281141 return 1 ;
11291142
1143+ /*
1144+ * data.path is the "path" that identifies the repository and it is
1145+ * constant regardless of what failed above. data.is_safe should be
1146+ * initialized to false, and might be changed by the callback.
1147+ */
11301148 read_very_early_config (safe_directory_cb , & data );
11311149
11321150 return data .is_safe ;
@@ -1214,6 +1232,8 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
12141232 current_device = get_device_or_die (dir -> buf , NULL , 0 );
12151233 for (;;) {
12161234 int offset = dir -> len , error_code = 0 ;
1235+ char * gitdir_path = NULL ;
1236+ char * gitfile = NULL ;
12171237
12181238 if (offset > min_offset )
12191239 strbuf_addch (dir , '/' );
@@ -1224,21 +1244,50 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
12241244 if (die_on_error ||
12251245 error_code == READ_GITFILE_ERR_NOT_A_FILE ) {
12261246 /* NEEDSWORK: fail if .git is not file nor dir */
1227- if (is_git_directory (dir -> buf ))
1247+ if (is_git_directory (dir -> buf )) {
12281248 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT ;
1249+ gitdir_path = xstrdup (dir -> buf );
1250+ }
12291251 } else if (error_code != READ_GITFILE_ERR_STAT_FAILED )
12301252 return GIT_DIR_INVALID_GITFILE ;
1231- }
1253+ } else
1254+ gitfile = xstrdup (dir -> buf );
1255+ /*
1256+ * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT
1257+ * to check that directory for a repository.
1258+ * Now trim that tentative addition away, because we want to
1259+ * focus on the real directory we are in.
1260+ */
12321261 strbuf_setlen (dir , offset );
12331262 if (gitdirenv ) {
1234- if (!ensure_valid_ownership (dir -> buf ))
1235- return GIT_DIR_INVALID_OWNERSHIP ;
1236- strbuf_addstr (gitdir , gitdirenv );
1237- return GIT_DIR_DISCOVERED ;
1263+ enum discovery_result ret ;
1264+
1265+ if (ensure_valid_ownership (gitfile ,
1266+ dir -> buf ,
1267+ (gitdir_path ? gitdir_path : gitdirenv ))) {
1268+ strbuf_addstr (gitdir , gitdirenv );
1269+ ret = GIT_DIR_DISCOVERED ;
1270+ } else
1271+ ret = GIT_DIR_INVALID_OWNERSHIP ;
1272+
1273+ /*
1274+ * Earlier, during discovery, we might have allocated
1275+ * string copies for gitdir_path or gitfile so make
1276+ * sure we don't leak by freeing them now, before
1277+ * leaving the loop and function.
1278+ *
1279+ * Note: gitdirenv will be non-NULL whenever these are
1280+ * allocated, therefore we need not take care of releasing
1281+ * them outside of this conditional block.
1282+ */
1283+ free (gitdir_path );
1284+ free (gitfile );
1285+
1286+ return ret ;
12381287 }
12391288
12401289 if (is_git_directory (dir -> buf )) {
1241- if (!ensure_valid_ownership (dir -> buf ))
1290+ if (!ensure_valid_ownership (NULL , NULL , dir -> buf ))
12421291 return GIT_DIR_INVALID_OWNERSHIP ;
12431292 strbuf_addstr (gitdir , "." );
12441293 return GIT_DIR_BARE ;
@@ -1376,7 +1425,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
13761425 struct strbuf quoted = STRBUF_INIT ;
13771426
13781427 sq_quote_buf_pretty (& quoted , dir .buf );
1379- die (_ ("unsafe repository ( '%s' is owned by someone else) \n"
1428+ die (_ ("detected dubious ownership in repository at '%s'\n"
13801429 "To add an exception for this directory, call:\n"
13811430 "\n"
13821431 "\tgit config --global --add safe.directory %s" ),
0 commit comments