Skip to content

Commit d20fc19

Browse files
pks-tgitster
authored andcommitted
builtin/reflog: make functions regarding reflog_expire_options public
Make functions that are required to manage `reflog_expire_options` available elsewhere by moving them into "reflog.c" and exposing them in the corresponding header. The functions will be used in a subsequent commit. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 964f364 commit d20fc19

File tree

3 files changed

+128
-111
lines changed

3 files changed

+128
-111
lines changed

builtin/reflog.c

Lines changed: 4 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -88,113 +88,6 @@ static int collect_reflog(const char *ref, void *cb_data)
8888
return 0;
8989
}
9090

91-
static struct reflog_expire_entry_option *find_cfg_ent(struct reflog_expire_options *opts,
92-
const char *pattern, size_t len)
93-
{
94-
struct reflog_expire_entry_option *ent;
95-
96-
if (!opts->entries_tail)
97-
opts->entries_tail = &opts->entries;
98-
99-
for (ent = opts->entries; ent; ent = ent->next)
100-
if (!xstrncmpz(ent->pattern, pattern, len))
101-
return ent;
102-
103-
FLEX_ALLOC_MEM(ent, pattern, pattern, len);
104-
*opts->entries_tail = ent;
105-
opts->entries_tail = &(ent->next);
106-
return ent;
107-
}
108-
109-
/* expiry timer slot */
110-
#define EXPIRE_TOTAL 01
111-
#define EXPIRE_UNREACH 02
112-
113-
static int reflog_expire_config(const char *var, const char *value,
114-
const struct config_context *ctx, void *cb)
115-
{
116-
struct reflog_expire_options *opts = cb;
117-
const char *pattern, *key;
118-
size_t pattern_len;
119-
timestamp_t expire;
120-
int slot;
121-
struct reflog_expire_entry_option *ent;
122-
123-
if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
124-
return git_default_config(var, value, ctx, cb);
125-
126-
if (!strcmp(key, "reflogexpire")) {
127-
slot = EXPIRE_TOTAL;
128-
if (git_config_expiry_date(&expire, var, value))
129-
return -1;
130-
} else if (!strcmp(key, "reflogexpireunreachable")) {
131-
slot = EXPIRE_UNREACH;
132-
if (git_config_expiry_date(&expire, var, value))
133-
return -1;
134-
} else
135-
return git_default_config(var, value, ctx, cb);
136-
137-
if (!pattern) {
138-
switch (slot) {
139-
case EXPIRE_TOTAL:
140-
opts->default_expire_total = expire;
141-
break;
142-
case EXPIRE_UNREACH:
143-
opts->default_expire_unreachable = expire;
144-
break;
145-
}
146-
return 0;
147-
}
148-
149-
ent = find_cfg_ent(opts, pattern, pattern_len);
150-
if (!ent)
151-
return -1;
152-
switch (slot) {
153-
case EXPIRE_TOTAL:
154-
ent->expire_total = expire;
155-
break;
156-
case EXPIRE_UNREACH:
157-
ent->expire_unreachable = expire;
158-
break;
159-
}
160-
return 0;
161-
}
162-
163-
static void set_reflog_expiry_param(struct reflog_expire_options *cb, const char *ref)
164-
{
165-
struct reflog_expire_entry_option *ent;
166-
167-
if (cb->explicit_expiry == (EXPIRE_TOTAL|EXPIRE_UNREACH))
168-
return; /* both given explicitly -- nothing to tweak */
169-
170-
for (ent = cb->entries; ent; ent = ent->next) {
171-
if (!wildmatch(ent->pattern, ref, 0)) {
172-
if (!(cb->explicit_expiry & EXPIRE_TOTAL))
173-
cb->expire_total = ent->expire_total;
174-
if (!(cb->explicit_expiry & EXPIRE_UNREACH))
175-
cb->expire_unreachable = ent->expire_unreachable;
176-
return;
177-
}
178-
}
179-
180-
/*
181-
* If unconfigured, make stash never expire
182-
*/
183-
if (!strcmp(ref, "refs/stash")) {
184-
if (!(cb->explicit_expiry & EXPIRE_TOTAL))
185-
cb->expire_total = 0;
186-
if (!(cb->explicit_expiry & EXPIRE_UNREACH))
187-
cb->expire_unreachable = 0;
188-
return;
189-
}
190-
191-
/* Nothing matched -- use the default value */
192-
if (!(cb->explicit_expiry & EXPIRE_TOTAL))
193-
cb->expire_total = cb->default_expire_total;
194-
if (!(cb->explicit_expiry & EXPIRE_UNREACH))
195-
cb->expire_unreachable = cb->default_expire_unreachable;
196-
}
197-
19891
static int expire_unreachable_callback(const struct option *opt,
19992
const char *arg,
20093
int unset)
@@ -207,7 +100,7 @@ static int expire_unreachable_callback(const struct option *opt,
207100
die(_("invalid timestamp '%s' given to '--%s'"),
208101
arg, opt->long_name);
209102

210-
opts->explicit_expiry |= EXPIRE_UNREACH;
103+
opts->explicit_expiry |= REFLOG_EXPIRE_UNREACH;
211104
return 0;
212105
}
213106

@@ -223,7 +116,7 @@ static int expire_total_callback(const struct option *opt,
223116
die(_("invalid timestamp '%s' given to '--%s'"),
224117
arg, opt->long_name);
225118

226-
opts->explicit_expiry |= EXPIRE_TOTAL;
119+
opts->explicit_expiry |= REFLOG_EXPIRE_TOTAL;
227120
return 0;
228121
}
229122

@@ -353,7 +246,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
353246
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
354247
};
355248

356-
set_reflog_expiry_param(&cb.opts, item->string);
249+
reflog_expire_options_set_refname(&cb.opts, item->string);
357250
status |= refs_reflog_expire(get_main_ref_store(the_repository),
358251
item->string, flags,
359252
reflog_expiry_prepare,
@@ -372,7 +265,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
372265
status |= error(_("%s points nowhere!"), argv[i]);
373266
continue;
374267
}
375-
set_reflog_expiry_param(&cb.opts, ref);
268+
reflog_expire_options_set_refname(&cb.opts, ref);
376269
status |= refs_reflog_expire(get_main_ref_store(the_repository),
377270
ref, flags,
378271
reflog_expiry_prepare,

reflog.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,120 @@
22
#define DISABLE_SIGN_COMPARE_WARNINGS
33

44
#include "git-compat-util.h"
5+
#include "config.h"
56
#include "gettext.h"
67
#include "object-store-ll.h"
8+
#include "parse-options.h"
79
#include "reflog.h"
810
#include "refs.h"
911
#include "revision.h"
1012
#include "tree.h"
1113
#include "tree-walk.h"
14+
#include "wildmatch.h"
15+
16+
static struct reflog_expire_entry_option *find_cfg_ent(struct reflog_expire_options *opts,
17+
const char *pattern, size_t len)
18+
{
19+
struct reflog_expire_entry_option *ent;
20+
21+
if (!opts->entries_tail)
22+
opts->entries_tail = &opts->entries;
23+
24+
for (ent = opts->entries; ent; ent = ent->next)
25+
if (!xstrncmpz(ent->pattern, pattern, len))
26+
return ent;
27+
28+
FLEX_ALLOC_MEM(ent, pattern, pattern, len);
29+
*opts->entries_tail = ent;
30+
opts->entries_tail = &(ent->next);
31+
return ent;
32+
}
33+
34+
int reflog_expire_config(const char *var, const char *value,
35+
const struct config_context *ctx, void *cb)
36+
{
37+
struct reflog_expire_options *opts = cb;
38+
const char *pattern, *key;
39+
size_t pattern_len;
40+
timestamp_t expire;
41+
int slot;
42+
struct reflog_expire_entry_option *ent;
43+
44+
if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
45+
return git_default_config(var, value, ctx, cb);
46+
47+
if (!strcmp(key, "reflogexpire")) {
48+
slot = REFLOG_EXPIRE_TOTAL;
49+
if (git_config_expiry_date(&expire, var, value))
50+
return -1;
51+
} else if (!strcmp(key, "reflogexpireunreachable")) {
52+
slot = REFLOG_EXPIRE_UNREACH;
53+
if (git_config_expiry_date(&expire, var, value))
54+
return -1;
55+
} else
56+
return git_default_config(var, value, ctx, cb);
57+
58+
if (!pattern) {
59+
switch (slot) {
60+
case REFLOG_EXPIRE_TOTAL:
61+
opts->default_expire_total = expire;
62+
break;
63+
case REFLOG_EXPIRE_UNREACH:
64+
opts->default_expire_unreachable = expire;
65+
break;
66+
}
67+
return 0;
68+
}
69+
70+
ent = find_cfg_ent(opts, pattern, pattern_len);
71+
if (!ent)
72+
return -1;
73+
switch (slot) {
74+
case REFLOG_EXPIRE_TOTAL:
75+
ent->expire_total = expire;
76+
break;
77+
case REFLOG_EXPIRE_UNREACH:
78+
ent->expire_unreachable = expire;
79+
break;
80+
}
81+
return 0;
82+
}
83+
84+
void reflog_expire_options_set_refname(struct reflog_expire_options *cb,
85+
const char *ref)
86+
{
87+
struct reflog_expire_entry_option *ent;
88+
89+
if (cb->explicit_expiry == (REFLOG_EXPIRE_TOTAL|REFLOG_EXPIRE_UNREACH))
90+
return; /* both given explicitly -- nothing to tweak */
91+
92+
for (ent = cb->entries; ent; ent = ent->next) {
93+
if (!wildmatch(ent->pattern, ref, 0)) {
94+
if (!(cb->explicit_expiry & REFLOG_EXPIRE_TOTAL))
95+
cb->expire_total = ent->expire_total;
96+
if (!(cb->explicit_expiry & REFLOG_EXPIRE_UNREACH))
97+
cb->expire_unreachable = ent->expire_unreachable;
98+
return;
99+
}
100+
}
101+
102+
/*
103+
* If unconfigured, make stash never expire
104+
*/
105+
if (!strcmp(ref, "refs/stash")) {
106+
if (!(cb->explicit_expiry & REFLOG_EXPIRE_TOTAL))
107+
cb->expire_total = 0;
108+
if (!(cb->explicit_expiry & REFLOG_EXPIRE_UNREACH))
109+
cb->expire_unreachable = 0;
110+
return;
111+
}
112+
113+
/* Nothing matched -- use the default value */
114+
if (!(cb->explicit_expiry & REFLOG_EXPIRE_TOTAL))
115+
cb->expire_total = cb->default_expire_total;
116+
if (!(cb->explicit_expiry & REFLOG_EXPIRE_UNREACH))
117+
cb->expire_unreachable = cb->default_expire_unreachable;
118+
}
12119

13120
/* Remember to update object flag allocation in object.h */
14121
#define INCOMPLETE (1u<<10)

reflog.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define REFLOG_H
33
#include "refs.h"
44

5+
#define REFLOG_EXPIRE_TOTAL (1 << 0)
6+
#define REFLOG_EXPIRE_UNREACH (1 << 1)
7+
58
struct reflog_expire_entry_option {
69
struct reflog_expire_entry_option *next;
710
timestamp_t expire_total;
@@ -24,6 +27,20 @@ struct reflog_expire_options {
2427
.default_expire_unreachable = now - 90 * 24 * 3600, \
2528
}
2629

30+
/*
31+
* Parse the reflog expire configuration. This should be used with
32+
* `repo_config()`.
33+
*/
34+
int reflog_expire_config(const char *var, const char *value,
35+
const struct config_context *ctx, void *cb);
36+
37+
/*
38+
* Adapt the options so that they apply to the given refname. This applies any
39+
* per-reference reflog expiry configuration that may exist to the options.
40+
*/
41+
void reflog_expire_options_set_refname(struct reflog_expire_options *cb,
42+
const char *refname);
43+
2744
struct expire_reflog_policy_cb {
2845
enum {
2946
UE_NORMAL,

0 commit comments

Comments
 (0)