@@ -72,9 +72,6 @@ static const char *const reflog_usage[] = {
72
72
NULL
73
73
};
74
74
75
- static timestamp_t default_reflog_expire ;
76
- static timestamp_t default_reflog_expire_unreachable ;
77
-
78
75
struct worktree_reflogs {
79
76
struct worktree * worktree ;
80
77
struct string_list reflogs ;
@@ -100,147 +97,35 @@ static int collect_reflog(const char *ref, void *cb_data)
100
97
return 0 ;
101
98
}
102
99
103
- static struct reflog_expire_cfg {
104
- struct reflog_expire_cfg * next ;
105
- timestamp_t expire_total ;
106
- timestamp_t expire_unreachable ;
107
- char pattern [FLEX_ARRAY ];
108
- } * reflog_expire_cfg , * * reflog_expire_cfg_tail ;
109
-
110
- static struct reflog_expire_cfg * find_cfg_ent (const char * pattern , size_t len )
111
- {
112
- struct reflog_expire_cfg * ent ;
113
-
114
- if (!reflog_expire_cfg_tail )
115
- reflog_expire_cfg_tail = & reflog_expire_cfg ;
116
-
117
- for (ent = reflog_expire_cfg ; ent ; ent = ent -> next )
118
- if (!xstrncmpz (ent -> pattern , pattern , len ))
119
- return ent ;
120
-
121
- FLEX_ALLOC_MEM (ent , pattern , pattern , len );
122
- * reflog_expire_cfg_tail = ent ;
123
- reflog_expire_cfg_tail = & (ent -> next );
124
- return ent ;
125
- }
126
-
127
- /* expiry timer slot */
128
- #define EXPIRE_TOTAL 01
129
- #define EXPIRE_UNREACH 02
130
-
131
- static int reflog_expire_config (const char * var , const char * value ,
132
- const struct config_context * ctx , void * cb )
133
- {
134
- const char * pattern , * key ;
135
- size_t pattern_len ;
136
- timestamp_t expire ;
137
- int slot ;
138
- struct reflog_expire_cfg * ent ;
139
-
140
- if (parse_config_key (var , "gc" , & pattern , & pattern_len , & key ) < 0 )
141
- return git_default_config (var , value , ctx , cb );
142
-
143
- if (!strcmp (key , "reflogexpire" )) {
144
- slot = EXPIRE_TOTAL ;
145
- if (git_config_expiry_date (& expire , var , value ))
146
- return -1 ;
147
- } else if (!strcmp (key , "reflogexpireunreachable" )) {
148
- slot = EXPIRE_UNREACH ;
149
- if (git_config_expiry_date (& expire , var , value ))
150
- return -1 ;
151
- } else
152
- return git_default_config (var , value , ctx , cb );
153
-
154
- if (!pattern ) {
155
- switch (slot ) {
156
- case EXPIRE_TOTAL :
157
- default_reflog_expire = expire ;
158
- break ;
159
- case EXPIRE_UNREACH :
160
- default_reflog_expire_unreachable = expire ;
161
- break ;
162
- }
163
- return 0 ;
164
- }
165
-
166
- ent = find_cfg_ent (pattern , pattern_len );
167
- if (!ent )
168
- return -1 ;
169
- switch (slot ) {
170
- case EXPIRE_TOTAL :
171
- ent -> expire_total = expire ;
172
- break ;
173
- case EXPIRE_UNREACH :
174
- ent -> expire_unreachable = expire ;
175
- break ;
176
- }
177
- return 0 ;
178
- }
179
-
180
- static void set_reflog_expiry_param (struct cmd_reflog_expire_cb * cb , const char * ref )
181
- {
182
- struct reflog_expire_cfg * ent ;
183
-
184
- if (cb -> explicit_expiry == (EXPIRE_TOTAL |EXPIRE_UNREACH ))
185
- return ; /* both given explicitly -- nothing to tweak */
186
-
187
- for (ent = reflog_expire_cfg ; ent ; ent = ent -> next ) {
188
- if (!wildmatch (ent -> pattern , ref , 0 )) {
189
- if (!(cb -> explicit_expiry & EXPIRE_TOTAL ))
190
- cb -> expire_total = ent -> expire_total ;
191
- if (!(cb -> explicit_expiry & EXPIRE_UNREACH ))
192
- cb -> expire_unreachable = ent -> expire_unreachable ;
193
- return ;
194
- }
195
- }
196
-
197
- /*
198
- * If unconfigured, make stash never expire
199
- */
200
- if (!strcmp (ref , "refs/stash" )) {
201
- if (!(cb -> explicit_expiry & EXPIRE_TOTAL ))
202
- cb -> expire_total = 0 ;
203
- if (!(cb -> explicit_expiry & EXPIRE_UNREACH ))
204
- cb -> expire_unreachable = 0 ;
205
- return ;
206
- }
207
-
208
- /* Nothing matched -- use the default value */
209
- if (!(cb -> explicit_expiry & EXPIRE_TOTAL ))
210
- cb -> expire_total = default_reflog_expire ;
211
- if (!(cb -> explicit_expiry & EXPIRE_UNREACH ))
212
- cb -> expire_unreachable = default_reflog_expire_unreachable ;
213
- }
214
-
215
100
static int expire_unreachable_callback (const struct option * opt ,
216
101
const char * arg ,
217
102
int unset )
218
103
{
219
- struct cmd_reflog_expire_cb * cmd = opt -> value ;
104
+ struct reflog_expire_options * opts = opt -> value ;
220
105
221
106
BUG_ON_OPT_NEG (unset );
222
107
223
- if (parse_expiry_date (arg , & cmd -> expire_unreachable ))
108
+ if (parse_expiry_date (arg , & opts -> expire_unreachable ))
224
109
die (_ ("invalid timestamp '%s' given to '--%s'" ),
225
110
arg , opt -> long_name );
226
111
227
- cmd -> explicit_expiry |= EXPIRE_UNREACH ;
112
+ opts -> explicit_expiry |= REFLOG_EXPIRE_UNREACH ;
228
113
return 0 ;
229
114
}
230
115
231
116
static int expire_total_callback (const struct option * opt ,
232
117
const char * arg ,
233
118
int unset )
234
119
{
235
- struct cmd_reflog_expire_cb * cmd = opt -> value ;
120
+ struct reflog_expire_options * opts = opt -> value ;
236
121
237
122
BUG_ON_OPT_NEG (unset );
238
123
239
- if (parse_expiry_date (arg , & cmd -> expire_total ))
124
+ if (parse_expiry_date (arg , & opts -> expire_total ))
240
125
die (_ ("invalid timestamp '%s' given to '--%s'" ),
241
126
arg , opt -> long_name );
242
127
243
- cmd -> explicit_expiry |= EXPIRE_TOTAL ;
128
+ opts -> explicit_expiry |= REFLOG_EXPIRE_TOTAL ;
244
129
return 0 ;
245
130
}
246
131
@@ -285,8 +170,8 @@ static int cmd_reflog_list(int argc, const char **argv, const char *prefix,
285
170
static int cmd_reflog_expire (int argc , const char * * argv , const char * prefix ,
286
171
struct repository * repo UNUSED )
287
172
{
288
- struct cmd_reflog_expire_cb cmd = { 0 };
289
173
timestamp_t now = time (NULL );
174
+ struct reflog_expire_options opts = REFLOG_EXPIRE_OPTIONS_INIT (now );
290
175
int i , status , do_all , single_worktree = 0 ;
291
176
unsigned int flags = 0 ;
292
177
int verbose = 0 ;
@@ -301,33 +186,27 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
301
186
N_ ("update the reference to the value of the top reflog entry" ),
302
187
EXPIRE_REFLOGS_UPDATE_REF ),
303
188
OPT_BOOL (0 , "verbose" , & verbose , N_ ("print extra information on screen" )),
304
- OPT_CALLBACK_F (0 , "expire" , & cmd , N_ ("timestamp" ),
189
+ OPT_CALLBACK_F (0 , "expire" , & opts , N_ ("timestamp" ),
305
190
N_ ("prune entries older than the specified time" ),
306
191
PARSE_OPT_NONEG ,
307
192
expire_total_callback ),
308
- OPT_CALLBACK_F (0 , "expire-unreachable" , & cmd , N_ ("timestamp" ),
193
+ OPT_CALLBACK_F (0 , "expire-unreachable" , & opts , N_ ("timestamp" ),
309
194
N_ ("prune entries older than <time> that are not reachable from the current tip of the branch" ),
310
195
PARSE_OPT_NONEG ,
311
196
expire_unreachable_callback ),
312
- OPT_BOOL (0 , "stale-fix" , & cmd .stalefix ,
197
+ OPT_BOOL (0 , "stale-fix" , & opts .stalefix ,
313
198
N_ ("prune any reflog entries that point to broken commits" )),
314
199
OPT_BOOL (0 , "all" , & do_all , N_ ("process the reflogs of all references" )),
315
200
OPT_BOOL (0 , "single-worktree" , & single_worktree ,
316
201
N_ ("limits processing to reflogs from the current worktree only" )),
317
202
OPT_END ()
318
203
};
319
204
320
- default_reflog_expire_unreachable = now - 30 * 24 * 3600 ;
321
- default_reflog_expire = now - 90 * 24 * 3600 ;
322
- git_config (reflog_expire_config , NULL );
205
+ git_config (reflog_expire_config , & opts );
323
206
324
207
save_commit_buffer = 0 ;
325
208
do_all = status = 0 ;
326
209
327
- cmd .explicit_expiry = 0 ;
328
- cmd .expire_total = default_reflog_expire ;
329
- cmd .expire_unreachable = default_reflog_expire_unreachable ;
330
-
331
210
argc = parse_options (argc , argv , prefix , options , reflog_expire_usage , 0 );
332
211
333
212
if (verbose )
@@ -338,7 +217,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
338
217
* even in older repository. We cannot trust what's reachable
339
218
* from reflog if the repository was pruned with older git.
340
219
*/
341
- if (cmd .stalefix ) {
220
+ if (opts .stalefix ) {
342
221
struct rev_info revs ;
343
222
344
223
repo_init_revisions (the_repository , & revs , prefix );
@@ -372,11 +251,11 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
372
251
373
252
for_each_string_list_item (item , & collected .reflogs ) {
374
253
struct expire_reflog_policy_cb cb = {
375
- .cmd = cmd ,
254
+ .opts = opts ,
376
255
.dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN ),
377
256
};
378
257
379
- set_reflog_expiry_param (& cb .cmd , item -> string );
258
+ reflog_expire_options_set_refname (& cb .opts , item -> string );
380
259
status |= refs_reflog_expire (get_main_ref_store (the_repository ),
381
260
item -> string , flags ,
382
261
reflog_expiry_prepare ,
@@ -389,13 +268,13 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
389
268
390
269
for (i = 0 ; i < argc ; i ++ ) {
391
270
char * ref ;
392
- struct expire_reflog_policy_cb cb = { .cmd = cmd };
271
+ struct expire_reflog_policy_cb cb = { .opts = opts };
393
272
394
273
if (!repo_dwim_log (the_repository , argv [i ], strlen (argv [i ]), NULL , & ref )) {
395
274
status |= error (_ ("reflog could not be found: '%s'" ), argv [i ]);
396
275
continue ;
397
276
}
398
- set_reflog_expiry_param (& cb .cmd , ref );
277
+ reflog_expire_options_set_refname (& cb .opts , ref );
399
278
status |= refs_reflog_expire (get_main_ref_store (the_repository ),
400
279
ref , flags ,
401
280
reflog_expiry_prepare ,
0 commit comments