Skip to content

Commit 3e1e762

Browse files
committed
Merge branch 'jc/prune-all'
We used the approxidate() parser for "--expire=<timestamp>" options of various commands, but it is better to treat --expire=all and --expire=now a bit more specially than using the current timestamp. Update "git gc" and "git reflog" with a new parsing function for expiry dates. * jc/prune-all: prune: introduce OPT_EXPIRY_DATE() and use it api-parse-options.txt: document "no-" for non-boolean options git-gc.txt, git-reflog.txt: document new expiry options date.c: add parse_expiry_date()
2 parents 305e19b + 27ec394 commit 3e1e762

File tree

9 files changed

+58
-13
lines changed

9 files changed

+58
-13
lines changed

Documentation/git-gc.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ automatic consolidation of packs.
6262

6363
--prune=<date>::
6464
Prune loose objects older than date (default is 2 weeks ago,
65-
overridable by the config variable `gc.pruneExpire`). This
66-
option is on by default.
65+
overridable by the config variable `gc.pruneExpire`).
66+
--prune=all prunes loose objects regardless of their age.
67+
--prune is on by default.
6768

6869
--no-prune::
6970
Do not prune any loose objects.

Documentation/git-reflog.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ them.
6767
--expire=<time>::
6868
Entries older than this time are pruned. Without the
6969
option it is taken from configuration `gc.reflogExpire`,
70-
which in turn defaults to 90 days.
70+
which in turn defaults to 90 days. --expire=all prunes
71+
entries regardless of their age; --expire=never turns off
72+
pruning of reachable entries (but see --expire-unreachable).
7173

7274
--expire-unreachable=<time>::
7375
Entries older than this time and not reachable from
7476
the current tip of the branch are pruned. Without the
7577
option it is taken from configuration
7678
`gc.reflogExpireUnreachable`, which in turn defaults to
77-
30 days.
79+
30 days. --expire-unreachable=all prunes unreachable
80+
entries regardless of their age; --expire-unreachable=never
81+
turns off early pruning of unreachable entries (but see
82+
--expire).
7883

7984
--all::
8085
Instead of listing <refs> explicitly, prune all refs.

Documentation/technical/api-parse-options.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The parse-options API allows:
4141
* Boolean long options can be 'negated' (or 'unset') by prepending
4242
`no-`, e.g. `--no-abbrev` instead of `--abbrev`. Conversely,
4343
options that begin with `no-` can be 'negated' by removing it.
44+
Other long options can be unset (e.g., set string to NULL, set
45+
integer to 0) by prepending `no-`.
4446

4547
* Options and non-option arguments can clearly be separated using the `--`
4648
option, e.g. `-a -b --option -- --this-is-a-file` indicates that
@@ -174,6 +176,10 @@ There are some macros to easily define options:
174176
Introduce an option with date argument, see `approxidate()`.
175177
The timestamp is put into `int_var`.
176178

179+
`OPT_EXPIRY_DATE(short, long, &int_var, description)`::
180+
Introduce an option with expiry date argument, see `parse_expiry_date()`.
181+
The timestamp is put into `int_var`.
182+
177183
`OPT_CALLBACK(short, long, &var, arg_str, description, func_ptr)`::
178184
Introduce an option with argument.
179185
The argument will be fed into the function given by `func_ptr`

builtin/prune.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
132132
OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
133133
OPT__VERBOSE(&verbose, N_("report pruned objects")),
134134
OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
135-
OPT_DATE(0, "expire", &expire,
136-
N_("expire objects older than <time>")),
135+
OPT_EXPIRY_DATE(0, "expire", &expire,
136+
N_("expire objects older than <time>")),
137137
OPT_END()
138138
};
139139
char *s;

builtin/reflog.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,9 @@ static int parse_expire_cfg_value(const char *var, const char *value, unsigned l
496496
{
497497
if (!value)
498498
return config_error_nonbool(var);
499-
if (!strcmp(value, "never") || !strcmp(value, "false")) {
500-
*expire = 0;
501-
return 0;
502-
}
503-
*expire = approxidate(value);
499+
if (parse_expiry_date(value, expire))
500+
return error(_("%s' for '%s' is not a valid timestamp"),
501+
value, var);
504502
return 0;
505503
}
506504

@@ -614,11 +612,13 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
614612
if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
615613
cb.dry_run = 1;
616614
else if (!prefixcmp(arg, "--expire=")) {
617-
cb.expire_total = approxidate(arg + 9);
615+
if (parse_expiry_date(arg + 9, &cb.expire_total))
616+
die(_("'%s' is not a valid timestamp"), arg);
618617
explicit_expiry |= EXPIRE_TOTAL;
619618
}
620619
else if (!prefixcmp(arg, "--expire-unreachable=")) {
621-
cb.expire_unreachable = approxidate(arg + 21);
620+
if (parse_expiry_date(arg + 21, &cb.expire_unreachable))
621+
die(_("'%s' is not a valid timestamp"), arg);
622622
explicit_expiry |= EXPIRE_UNREACH;
623623
}
624624
else if (!strcmp(arg, "--stale-fix"))

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ void show_date_relative(unsigned long time, int tz, const struct timeval *now,
910910
struct strbuf *timebuf);
911911
int parse_date(const char *date, char *buf, int bufsize);
912912
int parse_date_basic(const char *date, unsigned long *timestamp, int *offset);
913+
int parse_expiry_date(const char *date, unsigned long *timestamp);
913914
void datestamp(char *buf, int bufsize);
914915
#define approxidate(s) approxidate_careful((s), NULL)
915916
unsigned long approxidate_careful(const char *, int *);

date.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,28 @@ int parse_date_basic(const char *date, unsigned long *timestamp, int *offset)
711711
return 0; /* success */
712712
}
713713

714+
int parse_expiry_date(const char *date, unsigned long *timestamp)
715+
{
716+
int errors = 0;
717+
718+
if (!strcmp(date, "never") || !strcmp(date, "false"))
719+
*timestamp = 0;
720+
else if (!strcmp(date, "all") || !strcmp(date, "now"))
721+
/*
722+
* We take over "now" here, which usually translates
723+
* to the current timestamp. This is because the user
724+
* really means to expire everything she has done in
725+
* the past, and by definition reflogs are the record
726+
* of the past, and there is nothing from the future
727+
* to be kept.
728+
*/
729+
*timestamp = ULONG_MAX;
730+
else
731+
*timestamp = approxidate_careful(date, &errors);
732+
733+
return errors;
734+
}
735+
714736
int parse_date(const char *date, char *result, int maxlen)
715737
{
716738
unsigned long timestamp;

parse-options-cb.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
3333
return 0;
3434
}
3535

36+
int parse_opt_expiry_date_cb(const struct option *opt, const char *arg,
37+
int unset)
38+
{
39+
return parse_expiry_date(arg, (unsigned long *)opt->value);
40+
}
41+
3642
int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
3743
int unset)
3844
{

parse-options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ struct option {
140140
#define OPT_DATE(s, l, v, h) \
141141
{ OPTION_CALLBACK, (s), (l), (v), N_("time"),(h), 0, \
142142
parse_opt_approxidate_cb }
143+
#define OPT_EXPIRY_DATE(s, l, v, h) \
144+
{ OPTION_CALLBACK, (s), (l), (v), N_("expiry date"),(h), 0, \
145+
parse_opt_expiry_date_cb }
143146
#define OPT_CALLBACK(s, l, v, a, h, f) \
144147
{ OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
145148
#define OPT_NUMBER_CALLBACK(v, h, f) \
@@ -219,6 +222,7 @@ extern int parse_options_concat(struct option *dst, size_t, struct option *src);
219222
/*----- some often used options -----*/
220223
extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
221224
extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
225+
extern int parse_opt_expiry_date_cb(const struct option *, const char *, int);
222226
extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
223227
extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
224228
extern int parse_opt_with_commit(const struct option *, const char *, int);

0 commit comments

Comments
 (0)