refs: stop modifying global `log_all_ref_updates` variable
In refs-related code we modify the global `log_all_ref_updates` variable, which is done because `should_autocreate_reflog()` does not accept passing an `enum log_refs_config` but instead accesses the global variable. Adapt its interface such that the value is provided by the caller, which allows us to compute the proper value locally without having to modify global state. This change requires us to move the enum to "repo-settings.h", or otherwise we get compilation errors due to include cycles. We're about to fully move this setting into the repo-settings subsystem anyway, so this is fine. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Sep 12, 2024 at 13:30 UTC
9a20b889e8703482162d9d1487b876be42564a78
7 files changed
+36
-26
builtin/checkout.c
+2
-1
@@ -23,6 +23,7 @@
23
#include "read-cache.h"
24
#include "refs.h"
25
#include "remote.h"
26
+#include "repo-settings.h"
27
#include "resolve-undo.h"
28
#include "revision.h"
29
#include "setup.h"
@@ -954,7 +955,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
955
956
refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
957
if (opts->new_branch_log &&
957
- !should_autocreate_reflog(refname)) {
958
+ !should_autocreate_reflog(log_all_ref_updates, refname)) {
959
int ret;
960
struct strbuf err = STRBUF_INIT;
961
environment.h
+2
-6
@@ -1,6 +1,8 @@
1
#ifndef ENVIRONMENT_H
2
#define ENVIRONMENT_H
3
4
+#include "repo-settings.h"
5
+
6
/* Double-check local_repo_env below if you add to this list. */
7
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
8
#define GIT_COMMON_DIR_ENVIRONMENT "GIT_COMMON_DIR"
@@ -179,12 +181,6 @@ extern int core_apply_sparse_checkout;
181
extern int core_sparse_checkout_cone;
182
extern int sparse_expect_files_outside_of_patterns;
183
182
-enum log_refs_config {
183
- LOG_REFS_UNSET = -1,
184
- LOG_REFS_NONE = 0,
185
- LOG_REFS_NORMAL,
186
- LOG_REFS_ALWAYS
187
-};
184
extern enum log_refs_config log_all_ref_updates;
185
186
enum rebase_setup_type {
refs.c
+3
-2
@@ -24,7 +24,7 @@
24
#include "submodule.h"
25
#include "worktree.h"
26
#include "strvec.h"
27
-#include "repository.h"
27
+#include "repo-settings.h"
28
#include "setup.h"
29
#include "sigchain.h"
30
#include "date.h"
@@ -958,7 +958,8 @@ static char *normalize_reflog_message(const char *msg)
958
return strbuf_detach(&sb, NULL);
959
}
960
961
-int should_autocreate_reflog(const char *refname)
961
+int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,
962
+ const char *refname)
963
{
964
switch (log_all_ref_updates) {
965
case LOG_REFS_ALWAYS:
refs.h
+3
-1
@@ -3,6 +3,7 @@
3
4
#include "commit.h"
5
#include "repository.h"
6
+#include "repo-settings.h"
7
8
struct fsck_options;
9
struct object_id;
@@ -111,7 +112,8 @@ int refs_verify_refname_available(struct ref_store *refs,
112
113
int refs_ref_exists(struct ref_store *refs, const char *refname);
114
114
-int should_autocreate_reflog(const char *refname);
115
+int should_autocreate_reflog(enum log_refs_config log_all_ref_updates,
116
+ const char *refname);
117
118
int is_branch(const char *refname);
119
refs/files-backend.c
+12
-11
@@ -8,6 +8,7 @@
8
#include "../hex.h"
9
#include "../fsck.h"
10
#include "../refs.h"
11
+#include "../repo-settings.h"
12
#include "refs-internal.h"
13
#include "ref-cache.h"
14
#include "packed-backend.h"
@@ -1443,6 +1444,7 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,
1444
static int commit_ref_update(struct files_ref_store *refs,
1445
struct ref_lock *lock,
1446
const struct object_id *oid, const char *logmsg,
1447
+ int flags,
1448
struct strbuf *err);
1449
1450
/*
@@ -1586,7 +1588,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
1588
oidcpy(&lock->old_oid, &orig_oid);
1589
1590
if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
1589
- commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
1591
+ commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {
1592
error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1593
strbuf_release(&err);
1594
goto rollback;
@@ -1603,14 +1605,11 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
1605
goto rollbacklog;
1606
}
1607
1606
- flag = log_all_ref_updates;
1607
- log_all_ref_updates = LOG_REFS_NONE;
1608
if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
1609
- commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
1609
+ commit_ref_update(refs, lock, &orig_oid, NULL, REF_SKIP_CREATE_REFLOG, &err)) {
1610
error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1611
strbuf_release(&err);
1612
}
1613
- log_all_ref_updates = flag;
1613
1614
rollbacklog:
1615
if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
@@ -1705,13 +1704,17 @@ static int log_ref_setup(struct files_ref_store *refs,
1704
const char *refname, int force_create,
1705
int *logfd, struct strbuf *err)
1706
{
1707
+ enum log_refs_config log_refs_cfg = log_all_ref_updates;
1708
struct strbuf logfile_sb = STRBUF_INIT;
1709
char *logfile;
1710
1711
+ if (log_refs_cfg == LOG_REFS_UNSET)
1712
+ log_refs_cfg = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1713
+
1714
files_reflog_path(refs, &logfile_sb, refname);
1715
logfile = strbuf_detach(&logfile_sb, NULL);
1716
1714
- if (force_create || should_autocreate_reflog(refname)) {
1717
+ if (force_create || should_autocreate_reflog(log_refs_cfg, refname)) {
1718
if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1719
if (errno == ENOENT)
1720
strbuf_addf(err, "unable to create directory for '%s': "
@@ -1800,9 +1803,6 @@ static int files_log_ref_write(struct files_ref_store *refs,
1803
if (flags & REF_SKIP_CREATE_REFLOG)
1804
return 0;
1805
1803
- if (log_all_ref_updates == LOG_REFS_UNSET)
1804
- log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1805
-
1806
result = log_ref_setup(refs, refname,
1807
flags & REF_FORCE_CREATE_REFLOG,
1808
&logfd, err);
@@ -1891,6 +1891,7 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,
1891
static int commit_ref_update(struct files_ref_store *refs,
1892
struct ref_lock *lock,
1893
const struct object_id *oid, const char *logmsg,
1894
+ int flags,
1895
struct strbuf *err)
1896
{
1897
files_assert_main_repository(refs, "commit_ref_update");
@@ -1898,7 +1899,7 @@ static int commit_ref_update(struct files_ref_store *refs,
1899
clear_loose_ref_cache(refs);
1900
if (files_log_ref_write(refs, lock->ref_name,
1901
&lock->old_oid, oid,
1901
- logmsg, 0, err)) {
1902
+ logmsg, flags, err)) {
1903
char *old_msg = strbuf_detach(err, NULL);
1904
strbuf_addf(err, "cannot update the ref '%s': %s",
1905
lock->ref_name, old_msg);
@@ -1931,7 +1932,7 @@ static int commit_ref_update(struct files_ref_store *refs,
1932
struct strbuf log_err = STRBUF_INIT;
1933
if (files_log_ref_write(refs, "HEAD",
1934
&lock->old_oid, oid,
1934
- logmsg, 0, &log_err)) {
1935
+ logmsg, flags, &log_err)) {
1936
error("%s", log_err.buf);
1937
strbuf_release(&log_err);
1938
}
refs/reftable-backend.c
+7
-5
@@ -19,6 +19,7 @@
19
#include "../reftable/reftable-record.h"
20
#include "../reftable/reftable-error.h"
21
#include "../reftable/reftable-iterator.h"
22
+#include "../repo-settings.h"
23
#include "../setup.h"
24
#include "../strmap.h"
25
#include "parse.h"
@@ -158,20 +159,21 @@ static struct reftable_stack *stack_for(struct reftable_ref_store *store,
159
160
static int should_write_log(struct ref_store *refs, const char *refname)
161
{
161
- if (log_all_ref_updates == LOG_REFS_UNSET)
162
- log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
162
+ enum log_refs_config log_refs_cfg = log_all_ref_updates;
163
+ if (log_refs_cfg == LOG_REFS_UNSET)
164
+ log_refs_cfg = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
165
164
- switch (log_all_ref_updates) {
166
+ switch (log_refs_cfg) {
167
case LOG_REFS_NONE:
168
return refs_reflog_exists(refs, refname);
169
case LOG_REFS_ALWAYS:
170
return 1;
171
case LOG_REFS_NORMAL:
170
- if (should_autocreate_reflog(refname))
172
+ if (should_autocreate_reflog(log_refs_cfg, refname))
173
return 1;
174
return refs_reflog_exists(refs, refname);
175
default:
174
- BUG("unhandled core.logAllRefUpdates value %d", log_all_ref_updates);
176
+ BUG("unhandled core.logAllRefUpdates value %d", log_refs_cfg);
177
}
178
}
179
repo-settings.h
+7
@@ -16,6 +16,13 @@ enum fetch_negotiation_setting {
16
FETCH_NEGOTIATION_NOOP,
17
};
18
19
+enum log_refs_config {
20
+ LOG_REFS_UNSET = -1,
21
+ LOG_REFS_NONE = 0,
22
+ LOG_REFS_NORMAL,
23
+ LOG_REFS_ALWAYS
24
+};
25
+
26
struct repo_settings {
27
int initialized;
28