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 <Johannes.Schindelin@gmx.de>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Patrick Steinhardt
2024-04-15 13:30:36 +02:00
committed by Johannes Schindelin
parent d1bb66a546
commit 8c9c051bef
2 changed files with 33 additions and 0 deletions

12
cache.h
View File

@ -606,6 +606,18 @@ void set_git_work_tree(const char *tree);
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
/*
* Check if a repository is safe and die if it is not, by verifying the
* ownership of the worktree (if any), the git directory, and the gitfile (if
* any).
*
* Exemptions for known-safe repositories can be added via `safe.directory`
* config settings; for non-bare repositories, their worktree needs to be
* added, for bare ones their git directory.
*/
void die_upon_dubious_ownership(const char *gitfile, const char *worktree,
const char *gitdir);
void setup_work_tree(void);
/*
* Find the commondir and gitdir of the repository that contains the current

21
setup.c
View File

@ -1165,6 +1165,27 @@ static int ensure_valid_ownership(const char *gitfile,
return data.is_safe;
}
void die_upon_dubious_ownership(const char *gitfile, const char *worktree,
const char *gitdir)
{
struct strbuf report = STRBUF_INIT, quoted = STRBUF_INIT;
const char *path;
if (ensure_valid_ownership(gitfile, worktree, gitdir, &report))
return;
strbuf_complete(&report, '\n');
path = gitfile ? gitfile : gitdir;
sq_quote_buf_pretty(&quoted, path);
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"),
path, report.buf, quoted.buf);
}
static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
{
enum allowed_bare_repo *allowed_bare_repo = d;