@@ -63,9 +63,6 @@ static const char *const reflog_usage[] = {
6363	NULL 
6464};
6565
66- static  timestamp_t  default_reflog_expire ;
67- static  timestamp_t  default_reflog_expire_unreachable ;
68- 
6966struct  worktree_reflogs  {
7067	struct  worktree  * worktree ;
7168	struct  string_list  reflogs ;
@@ -91,147 +88,35 @@ static int collect_reflog(const char *ref, void *cb_data)
9188	return  0 ;
9289}
9390
94- static  struct  reflog_expire_cfg  {
95- 	struct  reflog_expire_cfg  * next ;
96- 	timestamp_t  expire_total ;
97- 	timestamp_t  expire_unreachable ;
98- 	char  pattern [FLEX_ARRAY ];
99- } * reflog_expire_cfg , * * reflog_expire_cfg_tail ;
100- 
101- static  struct  reflog_expire_cfg  * find_cfg_ent (const  char  * pattern , size_t  len )
102- {
103- 	struct  reflog_expire_cfg  * ent ;
104- 
105- 	if  (!reflog_expire_cfg_tail )
106- 		reflog_expire_cfg_tail  =  & reflog_expire_cfg ;
107- 
108- 	for  (ent  =  reflog_expire_cfg ; ent ; ent  =  ent -> next )
109- 		if  (!xstrncmpz (ent -> pattern , pattern , len ))
110- 			return  ent ;
111- 
112- 	FLEX_ALLOC_MEM (ent , pattern , pattern , len );
113- 	* reflog_expire_cfg_tail  =  ent ;
114- 	reflog_expire_cfg_tail  =  & (ent -> next );
115- 	return  ent ;
116- }
117- 
118- /* expiry timer slot */ 
119- #define  EXPIRE_TOTAL    01
120- #define  EXPIRE_UNREACH  02
121- 
122- static  int  reflog_expire_config (const  char  * var , const  char  * value ,
123- 				const  struct  config_context  * ctx , void  * cb )
124- {
125- 	const  char  * pattern , * key ;
126- 	size_t  pattern_len ;
127- 	timestamp_t  expire ;
128- 	int  slot ;
129- 	struct  reflog_expire_cfg  * ent ;
130- 
131- 	if  (parse_config_key (var , "gc" , & pattern , & pattern_len , & key ) <  0 )
132- 		return  git_default_config (var , value , ctx , cb );
133- 
134- 	if  (!strcmp (key , "reflogexpire" )) {
135- 		slot  =  EXPIRE_TOTAL ;
136- 		if  (git_config_expiry_date (& expire , var , value ))
137- 			return  -1 ;
138- 	} else  if  (!strcmp (key , "reflogexpireunreachable" )) {
139- 		slot  =  EXPIRE_UNREACH ;
140- 		if  (git_config_expiry_date (& expire , var , value ))
141- 			return  -1 ;
142- 	} else 
143- 		return  git_default_config (var , value , ctx , cb );
144- 
145- 	if  (!pattern ) {
146- 		switch  (slot ) {
147- 		case  EXPIRE_TOTAL :
148- 			default_reflog_expire  =  expire ;
149- 			break ;
150- 		case  EXPIRE_UNREACH :
151- 			default_reflog_expire_unreachable  =  expire ;
152- 			break ;
153- 		}
154- 		return  0 ;
155- 	}
156- 
157- 	ent  =  find_cfg_ent (pattern , pattern_len );
158- 	if  (!ent )
159- 		return  -1 ;
160- 	switch  (slot ) {
161- 	case  EXPIRE_TOTAL :
162- 		ent -> expire_total  =  expire ;
163- 		break ;
164- 	case  EXPIRE_UNREACH :
165- 		ent -> expire_unreachable  =  expire ;
166- 		break ;
167- 	}
168- 	return  0 ;
169- }
170- 
171- static  void  set_reflog_expiry_param (struct  cmd_reflog_expire_cb  * cb , const  char  * ref )
172- {
173- 	struct  reflog_expire_cfg  * ent ;
174- 
175- 	if  (cb -> explicit_expiry  ==  (EXPIRE_TOTAL |EXPIRE_UNREACH ))
176- 		return ; /* both given explicitly -- nothing to tweak */ 
177- 
178- 	for  (ent  =  reflog_expire_cfg ; ent ; ent  =  ent -> next ) {
179- 		if  (!wildmatch (ent -> pattern , ref , 0 )) {
180- 			if  (!(cb -> explicit_expiry  &  EXPIRE_TOTAL ))
181- 				cb -> expire_total  =  ent -> expire_total ;
182- 			if  (!(cb -> explicit_expiry  &  EXPIRE_UNREACH ))
183- 				cb -> expire_unreachable  =  ent -> expire_unreachable ;
184- 			return ;
185- 		}
186- 	}
187- 
188- 	/* 
189- 	 * If unconfigured, make stash never expire 
190- 	 */ 
191- 	if  (!strcmp (ref , "refs/stash" )) {
192- 		if  (!(cb -> explicit_expiry  &  EXPIRE_TOTAL ))
193- 			cb -> expire_total  =  0 ;
194- 		if  (!(cb -> explicit_expiry  &  EXPIRE_UNREACH ))
195- 			cb -> expire_unreachable  =  0 ;
196- 		return ;
197- 	}
198- 
199- 	/* Nothing matched -- use the default value */ 
200- 	if  (!(cb -> explicit_expiry  &  EXPIRE_TOTAL ))
201- 		cb -> expire_total  =  default_reflog_expire ;
202- 	if  (!(cb -> explicit_expiry  &  EXPIRE_UNREACH ))
203- 		cb -> expire_unreachable  =  default_reflog_expire_unreachable ;
204- }
205- 
20691static  int  expire_unreachable_callback (const  struct  option  * opt ,
20792				 const  char  * arg ,
20893				 int  unset )
20994{
210- 	struct  cmd_reflog_expire_cb   * cmd  =  opt -> value ;
95+ 	struct  reflog_expire_options   * opts  =  opt -> value ;
21196
21297	BUG_ON_OPT_NEG (unset );
21398
214- 	if  (parse_expiry_date (arg , & cmd -> expire_unreachable ))
99+ 	if  (parse_expiry_date (arg , & opts -> expire_unreachable ))
215100		die (_ ("invalid timestamp '%s' given to '--%s'" ),
216101		    arg , opt -> long_name );
217102
218- 	cmd -> explicit_expiry  |= EXPIRE_UNREACH ;
103+ 	opts -> explicit_expiry  |= REFLOG_EXPIRE_UNREACH ;
219104	return  0 ;
220105}
221106
222107static  int  expire_total_callback (const  struct  option  * opt ,
223108				 const  char  * arg ,
224109				 int  unset )
225110{
226- 	struct  cmd_reflog_expire_cb   * cmd  =  opt -> value ;
111+ 	struct  reflog_expire_options   * opts  =  opt -> value ;
227112
228113	BUG_ON_OPT_NEG (unset );
229114
230- 	if  (parse_expiry_date (arg , & cmd -> expire_total ))
115+ 	if  (parse_expiry_date (arg , & opts -> expire_total ))
231116		die (_ ("invalid timestamp '%s' given to '--%s'" ),
232117		    arg , opt -> long_name );
233118
234- 	cmd -> explicit_expiry  |= EXPIRE_TOTAL ;
119+ 	opts -> explicit_expiry  |= REFLOG_EXPIRE_TOTAL ;
235120	return  0 ;
236121}
237122
@@ -276,8 +161,8 @@ static int cmd_reflog_list(int argc, const char **argv, const char *prefix,
276161static  int  cmd_reflog_expire (int  argc , const  char  * * argv , const  char  * prefix ,
277162			     struct  repository  * repo  UNUSED )
278163{
279- 	struct  cmd_reflog_expire_cb  cmd  =  { 0  };
280164	timestamp_t  now  =  time (NULL );
165+ 	struct  reflog_expire_options  opts  =  REFLOG_EXPIRE_OPTIONS_INIT (now );
281166	int  i , status , do_all , single_worktree  =  0 ;
282167	unsigned int   flags  =  0 ;
283168	int  verbose  =  0 ;
@@ -292,33 +177,27 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
292177			N_ ("update the reference to the value of the top reflog entry" ),
293178			EXPIRE_REFLOGS_UPDATE_REF ),
294179		OPT_BOOL (0 , "verbose" , & verbose , N_ ("print extra information on screen" )),
295- 		OPT_CALLBACK_F (0 , "expire" , & cmd , N_ ("timestamp" ),
180+ 		OPT_CALLBACK_F (0 , "expire" , & opts , N_ ("timestamp" ),
296181			       N_ ("prune entries older than the specified time" ),
297182			       PARSE_OPT_NONEG ,
298183			       expire_total_callback ),
299- 		OPT_CALLBACK_F (0 , "expire-unreachable" , & cmd , N_ ("timestamp" ),
184+ 		OPT_CALLBACK_F (0 , "expire-unreachable" , & opts , N_ ("timestamp" ),
300185			       N_ ("prune entries older than <time> that are not reachable from the current tip of the branch" ),
301186			       PARSE_OPT_NONEG ,
302187			       expire_unreachable_callback ),
303- 		OPT_BOOL (0 , "stale-fix" , & cmd .stalefix ,
188+ 		OPT_BOOL (0 , "stale-fix" , & opts .stalefix ,
304189			 N_ ("prune any reflog entries that point to broken commits" )),
305190		OPT_BOOL (0 , "all" , & do_all , N_ ("process the reflogs of all references" )),
306191		OPT_BOOL (0 , "single-worktree" , & single_worktree ,
307192			 N_ ("limits processing to reflogs from the current worktree only" )),
308193		OPT_END ()
309194	};
310195
311- 	default_reflog_expire_unreachable  =  now  -  30  *  24  *  3600 ;
312- 	default_reflog_expire  =  now  -  90  *  24  *  3600 ;
313- 	git_config (reflog_expire_config , NULL );
196+ 	git_config (reflog_expire_config , & opts );
314197
315198	save_commit_buffer  =  0 ;
316199	do_all  =  status  =  0 ;
317200
318- 	cmd .explicit_expiry  =  0 ;
319- 	cmd .expire_total  =  default_reflog_expire ;
320- 	cmd .expire_unreachable  =  default_reflog_expire_unreachable ;
321- 
322201	argc  =  parse_options (argc , argv , prefix , options , reflog_expire_usage , 0 );
323202
324203	if  (verbose )
@@ -329,7 +208,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
329208	 * even in older repository.  We cannot trust what's reachable 
330209	 * from reflog if the repository was pruned with older git. 
331210	 */ 
332- 	if  (cmd .stalefix ) {
211+ 	if  (opts .stalefix ) {
333212		struct  rev_info  revs ;
334213
335214		repo_init_revisions (the_repository , & revs , prefix );
@@ -363,11 +242,11 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
363242
364243		for_each_string_list_item (item , & collected .reflogs ) {
365244			struct  expire_reflog_policy_cb  cb  =  {
366- 				.cmd  =  cmd ,
245+ 				.opts  =  opts ,
367246				.dry_run  =  !!(flags  &  EXPIRE_REFLOGS_DRY_RUN ),
368247			};
369248
370- 			set_reflog_expiry_param (& cb .cmd ,  item -> string );
249+ 			reflog_expire_options_set_refname (& cb .opts ,  item -> string );
371250			status  |= refs_reflog_expire (get_main_ref_store (the_repository ),
372251						     item -> string , flags ,
373252						     reflog_expiry_prepare ,
@@ -380,13 +259,13 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
380259
381260	for  (i  =  0 ; i  <  argc ; i ++ ) {
382261		char  * ref ;
383- 		struct  expire_reflog_policy_cb  cb  =  { .cmd  =  cmd  };
262+ 		struct  expire_reflog_policy_cb  cb  =  { .opts  =  opts  };
384263
385264		if  (!repo_dwim_log (the_repository , argv [i ], strlen (argv [i ]), NULL , & ref )) {
386265			status  |= error (_ ("%s points nowhere!" ), argv [i ]);
387266			continue ;
388267		}
389- 		set_reflog_expiry_param (& cb .cmd , ref );
268+ 		reflog_expire_options_set_refname (& cb .opts , ref );
390269		status  |= refs_reflog_expire (get_main_ref_store (the_repository ),
391270					     ref , flags ,
392271					     reflog_expiry_prepare ,
0 commit comments