Skip to content

Commit 9a20b88

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 118fd1a commit 9a20b88

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

builtin/checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "read-cache.h"
2424
#include "refs.h"
2525
#include "remote.h"
26+
#include "repo-settings.h"
2627
#include "resolve-undo.h"
2728
#include "revision.h"
2829
#include "setup.h"
@@ -954,7 +955,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
954955

955956
refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
956957
if (opts->new_branch_log &&
957-
!should_autocreate_reflog(refname)) {
958+
!should_autocreate_reflog(log_all_ref_updates, refname)) {
958959
int ret;
959960
struct strbuf err = STRBUF_INIT;
960961

environment.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef ENVIRONMENT_H
22
#define ENVIRONMENT_H
33

4+
#include "repo-settings.h"
5+
46
/* Double-check local_repo_env below if you add to this list. */
57
#define GIT_DIR_ENVIRONMENT "GIT_DIR"
68
#define GIT_COMMON_DIR_ENVIRONMENT "GIT_COMMON_DIR"
@@ -179,12 +181,6 @@ extern int core_apply_sparse_checkout;
179181
extern int core_sparse_checkout_cone;
180182
extern int sparse_expect_files_outside_of_patterns;
181183

182-
enum log_refs_config {
183-
LOG_REFS_UNSET = -1,
184-
LOG_REFS_NONE = 0,
185-
LOG_REFS_NORMAL,
186-
LOG_REFS_ALWAYS
187-
};
188184
extern enum log_refs_config log_all_ref_updates;
189185

190186
enum rebase_setup_type {

refs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "submodule.h"
2525
#include "worktree.h"
2626
#include "strvec.h"
27-
#include "repository.h"
27+
#include "repo-settings.h"
2828
#include "setup.h"
2929
#include "sigchain.h"
3030
#include "date.h"
@@ -958,7 +958,8 @@ static char *normalize_reflog_message(const char *msg)
958958
return strbuf_detach(&sb, NULL);
959959
}
960960

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)
962963
{
963964
switch (log_all_ref_updates) {
964965
case LOG_REFS_ALWAYS:

refs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "commit.h"
55
#include "repository.h"
6+
#include "repo-settings.h"
67

78
struct fsck_options;
89
struct object_id;
@@ -111,7 +112,8 @@ int refs_verify_refname_available(struct ref_store *refs,
111112

112113
int refs_ref_exists(struct ref_store *refs, const char *refname);
113114

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);
115117

116118
int is_branch(const char *refname);
117119

refs/files-backend.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../hex.h"
99
#include "../fsck.h"
1010
#include "../refs.h"
11+
#include "../repo-settings.h"
1112
#include "refs-internal.h"
1213
#include "ref-cache.h"
1314
#include "packed-backend.h"
@@ -1443,6 +1444,7 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,
14431444
static int commit_ref_update(struct files_ref_store *refs,
14441445
struct ref_lock *lock,
14451446
const struct object_id *oid, const char *logmsg,
1447+
int flags,
14461448
struct strbuf *err);
14471449

14481450
/*
@@ -1586,7 +1588,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
15861588
oidcpy(&lock->old_oid, &orig_oid);
15871589

15881590
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)) {
15901592
error("unable to write current sha1 into %s: %s", newrefname, err.buf);
15911593
strbuf_release(&err);
15921594
goto rollback;
@@ -1603,14 +1605,11 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
16031605
goto rollbacklog;
16041606
}
16051607

1606-
flag = log_all_ref_updates;
1607-
log_all_ref_updates = LOG_REFS_NONE;
16081608
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)) {
16101610
error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
16111611
strbuf_release(&err);
16121612
}
1613-
log_all_ref_updates = flag;
16141613

16151614
rollbacklog:
16161615
if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
@@ -1705,13 +1704,17 @@ static int log_ref_setup(struct files_ref_store *refs,
17051704
const char *refname, int force_create,
17061705
int *logfd, struct strbuf *err)
17071706
{
1707+
enum log_refs_config log_refs_cfg = log_all_ref_updates;
17081708
struct strbuf logfile_sb = STRBUF_INIT;
17091709
char *logfile;
17101710

1711+
if (log_refs_cfg == LOG_REFS_UNSET)
1712+
log_refs_cfg = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1713+
17111714
files_reflog_path(refs, &logfile_sb, refname);
17121715
logfile = strbuf_detach(&logfile_sb, NULL);
17131716

1714-
if (force_create || should_autocreate_reflog(refname)) {
1717+
if (force_create || should_autocreate_reflog(log_refs_cfg, refname)) {
17151718
if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
17161719
if (errno == ENOENT)
17171720
strbuf_addf(err, "unable to create directory for '%s': "
@@ -1800,9 +1803,6 @@ static int files_log_ref_write(struct files_ref_store *refs,
18001803
if (flags & REF_SKIP_CREATE_REFLOG)
18011804
return 0;
18021805

1803-
if (log_all_ref_updates == LOG_REFS_UNSET)
1804-
log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
1805-
18061806
result = log_ref_setup(refs, refname,
18071807
flags & REF_FORCE_CREATE_REFLOG,
18081808
&logfd, err);
@@ -1891,14 +1891,15 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,
18911891
static int commit_ref_update(struct files_ref_store *refs,
18921892
struct ref_lock *lock,
18931893
const struct object_id *oid, const char *logmsg,
1894+
int flags,
18941895
struct strbuf *err)
18951896
{
18961897
files_assert_main_repository(refs, "commit_ref_update");
18971898

18981899
clear_loose_ref_cache(refs);
18991900
if (files_log_ref_write(refs, lock->ref_name,
19001901
&lock->old_oid, oid,
1901-
logmsg, 0, err)) {
1902+
logmsg, flags, err)) {
19021903
char *old_msg = strbuf_detach(err, NULL);
19031904
strbuf_addf(err, "cannot update the ref '%s': %s",
19041905
lock->ref_name, old_msg);
@@ -1931,7 +1932,7 @@ static int commit_ref_update(struct files_ref_store *refs,
19311932
struct strbuf log_err = STRBUF_INIT;
19321933
if (files_log_ref_write(refs, "HEAD",
19331934
&lock->old_oid, oid,
1934-
logmsg, 0, &log_err)) {
1935+
logmsg, flags, &log_err)) {
19351936
error("%s", log_err.buf);
19361937
strbuf_release(&log_err);
19371938
}

refs/reftable-backend.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../reftable/reftable-record.h"
2020
#include "../reftable/reftable-error.h"
2121
#include "../reftable/reftable-iterator.h"
22+
#include "../repo-settings.h"
2223
#include "../setup.h"
2324
#include "../strmap.h"
2425
#include "parse.h"
@@ -158,20 +159,21 @@ static struct reftable_stack *stack_for(struct reftable_ref_store *store,
158159

159160
static int should_write_log(struct ref_store *refs, const char *refname)
160161
{
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;
163165

164-
switch (log_all_ref_updates) {
166+
switch (log_refs_cfg) {
165167
case LOG_REFS_NONE:
166168
return refs_reflog_exists(refs, refname);
167169
case LOG_REFS_ALWAYS:
168170
return 1;
169171
case LOG_REFS_NORMAL:
170-
if (should_autocreate_reflog(refname))
172+
if (should_autocreate_reflog(log_refs_cfg, refname))
171173
return 1;
172174
return refs_reflog_exists(refs, refname);
173175
default:
174-
BUG("unhandled core.logAllRefUpdates value %d", log_all_ref_updates);
176+
BUG("unhandled core.logAllRefUpdates value %d", log_refs_cfg);
175177
}
176178
}
177179

repo-settings.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ enum fetch_negotiation_setting {
1616
FETCH_NEGOTIATION_NOOP,
1717
};
1818

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+
1926
struct repo_settings {
2027
int initialized;
2128

0 commit comments

Comments
 (0)