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>
Patrick Steinhardt committed
Apr 15, 2024 at 13:30 UTC
8c9c051bef3db0fe267f3fb6a1dab293c5f23b38
2 files changed
+33
cache.h
+12
@@ -606,6 +606,18 @@ void set_git_work_tree(const char *tree);
606
607
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
608
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
+
621
void setup_work_tree(void);
622
/*
623
* Find the commondir and gitdir of the repository that contains the current
setup.c
+21
@@ -1165,6 +1165,27 @@ static int ensure_valid_ownership(const char *gitfile,
1165
return data.is_safe;
1166
}
1167
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("ed, 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
+
1189
static int allowed_bare_repo_cb(const char *key, const char *value, void *d)
1190
{
1191
enum allowed_bare_repo *allowed_bare_repo = d;