Skip to content

Commit 8c9c051

Browse files
pks-tdscho
authored andcommitted
setup.c: introduce die_upon_dubious_ownership()
Introduce a new function `die_upon_dubious_ownership()` that uses `ensure_valid_ownership()` to verify whether a repositroy is safe for use, and causes Git to die in case it is not. This function will be used in a subsequent commit. Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent d1bb66a commit 8c9c051

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

cache.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,18 @@ void set_git_work_tree(const char *tree);
606606

607607
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
608608

609+
/*
610+
* Check if a repository is safe and die if it is not, by verifying the
611+
* ownership of the worktree (if any), the git directory, and the gitfile (if
612+
* any).
613+
*
614+
* Exemptions for known-safe repositories can be added via `safe.directory`
615+
* config settings; for non-bare repositories, their worktree needs to be
616+
* added, for bare ones their git directory.
617+
*/
618+
void die_upon_dubious_ownership(const char *gitfile, const char *worktree,
619+
const char *gitdir);
620+
609621
void setup_work_tree(void);
610622
/*
611623
* Find the commondir and gitdir of the repository that contains the current

setup.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,27 @@ static int ensure_valid_ownership(const char *gitfile,
11651165
return data.is_safe;
11661166
}
11671167

1168+
void die_upon_dubious_ownership(const char *gitfile, const char *worktree,
1169+
const char *gitdir)
1170+
{
1171+
struct strbuf report = STRBUF_INIT, quoted = STRBUF_INIT;
1172+
const char *path;
1173+
1174+
if (ensure_valid_ownership(gitfile, worktree, gitdir, &report))
1175+
return;
1176+
1177+
strbuf_complete(&report, '\n');
1178+
path = gitfile ? gitfile : gitdir;
1179+
sq_quote_buf_pretty(&quoted, path);
1180+
1181+
die(_("detected dubious ownership in repository at '%s'\n"
1182+
"%s"
1183+
"To add an exception for this directory, call:\n"
1184+
"\n"
1185+
"\tgit config --global --add safe.directory %s"),
1186+
path, report.buf, quoted.buf);
1187+
}
1188+
11681189
static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
11691190
{
11701191
enum allowed_bare_repo *allowed_bare_repo = d;

0 commit comments

Comments
 (0)