Skip to content

Commit 6f3fbed

Browse files
pks-tgitster
authored andcommitted
environment: move access to "core.hooksPath" into repo settings
The "core.hooksPath" setting is stored in a global variable and populated via the `git_default_core_config`. This may cause issues in the case where one is handling multiple different repositories in a single process with different values for that config key, as we may or may not see the correct value in that case. Furthermore, global state blocks our path towards libification. Refactor the code so that we instead store the value in `struct repo_settings`. The value is computed as-needed and cached. The result should be functionally the same as there aren't ever any code paths where we'd execute hooks outside the context of a repository. Note that this requires us to change the passed-in repository in the `repo_git_path()` family of functions to be non-constant, as we call `adjust_git_path()` there. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b411ed6 commit 6f3fbed

File tree

7 files changed

+23
-17
lines changed

7 files changed

+23
-17
lines changed

config.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,11 +1436,6 @@ static int git_default_core_config(const char *var, const char *value,
14361436
return git_config_pathname(&git_attributes_file, var, value);
14371437
}
14381438

1439-
if (!strcmp(var, "core.hookspath")) {
1440-
FREE_AND_NULL(git_hooks_path);
1441-
return git_config_pathname(&git_hooks_path, var, value);
1442-
}
1443-
14441439
if (!strcmp(var, "core.bare")) {
14451440
is_bare_repository_cfg = git_config_bool(var, value);
14461441
return 0;

environment.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ char *git_log_output_encoding;
4242
char *apply_default_whitespace;
4343
char *apply_default_ignorewhitespace;
4444
char *git_attributes_file;
45-
char *git_hooks_path;
4645
int zlib_compression_level = Z_BEST_SPEED;
4746
int pack_compression_level = Z_DEFAULT_COMPRESSION;
4847
int fsync_object_files = -1;

environment.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ extern int warn_on_object_refname_ambiguity;
160160
extern char *apply_default_whitespace;
161161
extern char *apply_default_ignorewhitespace;
162162
extern char *git_attributes_file;
163-
extern char *git_hooks_path;
164163
extern int zlib_compression_level;
165164
extern int pack_compression_level;
166165
extern size_t packed_git_window_size;

path.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,11 @@ void report_linked_checkout_garbage(struct repository *r)
387387
strbuf_release(&sb);
388388
}
389389

390-
static void adjust_git_path(const struct repository *repo,
390+
static void adjust_git_path(struct repository *repo,
391391
struct strbuf *buf, int git_dir_len)
392392
{
393393
const char *base = buf->buf + git_dir_len;
394+
394395
if (is_dir_file(base, "info", "grafts"))
395396
strbuf_splice(buf, 0, buf->len,
396397
repo->graft_file, strlen(repo->graft_file));
@@ -399,8 +400,8 @@ static void adjust_git_path(const struct repository *repo,
399400
repo->index_file, strlen(repo->index_file));
400401
else if (dir_prefix(base, "objects"))
401402
replace_dir(buf, git_dir_len + 7, repo->objects->odb->path);
402-
else if (git_hooks_path && dir_prefix(base, "hooks"))
403-
replace_dir(buf, git_dir_len + 5, git_hooks_path);
403+
else if (repo_settings_get_hooks_path(repo) && dir_prefix(base, "hooks"))
404+
replace_dir(buf, git_dir_len + 5, repo_settings_get_hooks_path(repo));
404405
else if (repo->different_commondir)
405406
update_common_dir(buf, git_dir_len, repo->commondir);
406407
}
@@ -417,7 +418,7 @@ static void strbuf_worktree_gitdir(struct strbuf *buf,
417418
repo_common_path_append(repo, buf, "worktrees/%s", wt->id);
418419
}
419420

420-
static void repo_git_pathv(const struct repository *repo,
421+
static void repo_git_pathv(struct repository *repo,
421422
const struct worktree *wt, struct strbuf *buf,
422423
const char *fmt, va_list args)
423424
{
@@ -432,7 +433,7 @@ static void repo_git_pathv(const struct repository *repo,
432433
strbuf_cleanup_path(buf);
433434
}
434435

435-
char *repo_git_path(const struct repository *repo,
436+
char *repo_git_path(struct repository *repo,
436437
const char *fmt, ...)
437438
{
438439
struct strbuf path = STRBUF_INIT;
@@ -443,7 +444,7 @@ char *repo_git_path(const struct repository *repo,
443444
return strbuf_detach(&path, NULL);
444445
}
445446

446-
const char *repo_git_path_append(const struct repository *repo,
447+
const char *repo_git_path_append(struct repository *repo,
447448
struct strbuf *sb,
448449
const char *fmt, ...)
449450
{
@@ -454,7 +455,7 @@ const char *repo_git_path_append(const struct repository *repo,
454455
return sb->buf;
455456
}
456457

457-
const char *repo_git_path_replace(const struct repository *repo,
458+
const char *repo_git_path_replace(struct repository *repo,
458459
struct strbuf *sb,
459460
const char *fmt, ...)
460461
{

path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ const char *repo_common_path_replace(const struct repository *repo,
5252
* For an exhaustive list of the adjustments made look at `common_list` and
5353
* `adjust_git_path` in path.c.
5454
*/
55-
char *repo_git_path(const struct repository *repo,
55+
char *repo_git_path(struct repository *repo,
5656
const char *fmt, ...)
5757
__attribute__((format (printf, 2, 3)));
58-
const char *repo_git_path_append(const struct repository *repo,
58+
const char *repo_git_path_append(struct repository *repo,
5959
struct strbuf *sb,
6060
const char *fmt, ...)
6161
__attribute__((format (printf, 3, 4)));
62-
const char *repo_git_path_replace(const struct repository *repo,
62+
const char *repo_git_path_replace(struct repository *repo,
6363
struct strbuf *sb,
6464
const char *fmt, ...)
6565
__attribute__((format (printf, 3, 4)));

repo-settings.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ void repo_settings_clear(struct repository *r)
146146
{
147147
struct repo_settings empty = REPO_SETTINGS_INIT;
148148
FREE_AND_NULL(r->settings.fsmonitor);
149+
FREE_AND_NULL(r->settings.hooks_path);
149150
r->settings = empty;
150151
}
151152

@@ -173,3 +174,10 @@ int repo_settings_get_warn_ambiguous_refs(struct repository *repo)
173174
&repo->settings.warn_ambiguous_refs, 1);
174175
return repo->settings.warn_ambiguous_refs;
175176
}
177+
178+
const char *repo_settings_get_hooks_path(struct repository *repo)
179+
{
180+
if (!repo->settings.hooks_path)
181+
repo_config_get_pathname(repo, "core.hookspath", &repo->settings.hooks_path);
182+
return repo->settings.hooks_path;
183+
}

repo-settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ struct repo_settings {
6161
size_t delta_base_cache_limit;
6262
size_t packed_git_window_size;
6363
size_t packed_git_limit;
64+
65+
char *hooks_path;
6466
};
6567
#define REPO_SETTINGS_INIT { \
6668
.index_version = -1, \
@@ -79,5 +81,7 @@ void repo_settings_clear(struct repository *r);
7981
enum log_refs_config repo_settings_get_log_all_ref_updates(struct repository *repo);
8082
/* Read the value for "core.warnAmbiguousRefs". */
8183
int repo_settings_get_warn_ambiguous_refs(struct repository *repo);
84+
/* Read the value for "core.hooksPath". */
85+
const char *repo_settings_get_hooks_path(struct repository *repo);
8286

8387
#endif /* REPO_SETTINGS_H */

0 commit comments

Comments
 (0)