@@ -72,9 +72,6 @@ static const char *const reflog_usage[] = {
7272 NULL
7373};
7474
75- static timestamp_t default_reflog_expire ;
76- static timestamp_t default_reflog_expire_unreachable ;
77-
7875struct worktree_reflogs {
7976 struct worktree * worktree ;
8077 struct string_list reflogs ;
@@ -100,147 +97,35 @@ static int collect_reflog(const char *ref, void *cb_data)
10097 return 0 ;
10198}
10299
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-
215100static int expire_unreachable_callback (const struct option * opt ,
216101 const char * arg ,
217102 int unset )
218103{
219- struct cmd_reflog_expire_cb * cmd = opt -> value ;
104+ struct reflog_expire_options * opts = opt -> value ;
220105
221106 BUG_ON_OPT_NEG (unset );
222107
223- if (parse_expiry_date (arg , & cmd -> expire_unreachable ))
108+ if (parse_expiry_date (arg , & opts -> expire_unreachable ))
224109 die (_ ("invalid timestamp '%s' given to '--%s'" ),
225110 arg , opt -> long_name );
226111
227- cmd -> explicit_expiry |= EXPIRE_UNREACH ;
112+ opts -> explicit_expiry |= REFLOG_EXPIRE_UNREACH ;
228113 return 0 ;
229114}
230115
231116static int expire_total_callback (const struct option * opt ,
232117 const char * arg ,
233118 int unset )
234119{
235- struct cmd_reflog_expire_cb * cmd = opt -> value ;
120+ struct reflog_expire_options * opts = opt -> value ;
236121
237122 BUG_ON_OPT_NEG (unset );
238123
239- if (parse_expiry_date (arg , & cmd -> expire_total ))
124+ if (parse_expiry_date (arg , & opts -> expire_total ))
240125 die (_ ("invalid timestamp '%s' given to '--%s'" ),
241126 arg , opt -> long_name );
242127
243- cmd -> explicit_expiry |= EXPIRE_TOTAL ;
128+ opts -> explicit_expiry |= REFLOG_EXPIRE_TOTAL ;
244129 return 0 ;
245130}
246131
@@ -285,8 +170,8 @@ static int cmd_reflog_list(int argc, const char **argv, const char *prefix,
285170static int cmd_reflog_expire (int argc , const char * * argv , const char * prefix ,
286171 struct repository * repo UNUSED )
287172{
288- struct cmd_reflog_expire_cb cmd = { 0 };
289173 timestamp_t now = time (NULL );
174+ struct reflog_expire_options opts = REFLOG_EXPIRE_OPTIONS_INIT (now );
290175 int i , status , do_all , single_worktree = 0 ;
291176 unsigned int flags = 0 ;
292177 int verbose = 0 ;
@@ -301,33 +186,27 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
301186 N_ ("update the reference to the value of the top reflog entry" ),
302187 EXPIRE_REFLOGS_UPDATE_REF ),
303188 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" ),
305190 N_ ("prune entries older than the specified time" ),
306191 PARSE_OPT_NONEG ,
307192 expire_total_callback ),
308- OPT_CALLBACK_F (0 , "expire-unreachable" , & cmd , N_ ("timestamp" ),
193+ OPT_CALLBACK_F (0 , "expire-unreachable" , & opts , N_ ("timestamp" ),
309194 N_ ("prune entries older than <time> that are not reachable from the current tip of the branch" ),
310195 PARSE_OPT_NONEG ,
311196 expire_unreachable_callback ),
312- OPT_BOOL (0 , "stale-fix" , & cmd .stalefix ,
197+ OPT_BOOL (0 , "stale-fix" , & opts .stalefix ,
313198 N_ ("prune any reflog entries that point to broken commits" )),
314199 OPT_BOOL (0 , "all" , & do_all , N_ ("process the reflogs of all references" )),
315200 OPT_BOOL (0 , "single-worktree" , & single_worktree ,
316201 N_ ("limits processing to reflogs from the current worktree only" )),
317202 OPT_END ()
318203 };
319204
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 );
323206
324207 save_commit_buffer = 0 ;
325208 do_all = status = 0 ;
326209
327- cmd .explicit_expiry = 0 ;
328- cmd .expire_total = default_reflog_expire ;
329- cmd .expire_unreachable = default_reflog_expire_unreachable ;
330-
331210 argc = parse_options (argc , argv , prefix , options , reflog_expire_usage , 0 );
332211
333212 if (verbose )
@@ -338,7 +217,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
338217 * even in older repository. We cannot trust what's reachable
339218 * from reflog if the repository was pruned with older git.
340219 */
341- if (cmd .stalefix ) {
220+ if (opts .stalefix ) {
342221 struct rev_info revs ;
343222
344223 repo_init_revisions (the_repository , & revs , prefix );
@@ -372,11 +251,11 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
372251
373252 for_each_string_list_item (item , & collected .reflogs ) {
374253 struct expire_reflog_policy_cb cb = {
375- .cmd = cmd ,
254+ .opts = opts ,
376255 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN ),
377256 };
378257
379- set_reflog_expiry_param (& cb .cmd , item -> string );
258+ reflog_expire_options_set_refname (& cb .opts , item -> string );
380259 status |= refs_reflog_expire (get_main_ref_store (the_repository ),
381260 item -> string , flags ,
382261 reflog_expiry_prepare ,
@@ -389,13 +268,13 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
389268
390269 for (i = 0 ; i < argc ; i ++ ) {
391270 char * ref ;
392- struct expire_reflog_policy_cb cb = { .cmd = cmd };
271+ struct expire_reflog_policy_cb cb = { .opts = opts };
393272
394273 if (!repo_dwim_log (the_repository , argv [i ], strlen (argv [i ]), NULL , & ref )) {
395274 status |= error (_ ("reflog could not be found: '%s'" ), argv [i ]);
396275 continue ;
397276 }
398- set_reflog_expiry_param (& cb .cmd , ref );
277+ reflog_expire_options_set_refname (& cb .opts , ref );
399278 status |= refs_reflog_expire (get_main_ref_store (the_repository ),
400279 ref , flags ,
401280 reflog_expiry_prepare ,
0 commit comments