treewide: include parse-options.h in source files
[git/debian.git] / builtin / reflog.c
blob9b000bb6bcaf0fe8f243e01b236bdeb341b1ca6e
1 #include "builtin.h"
2 #include "config.h"
3 #include "revision.h"
4 #include "reachable.h"
5 #include "worktree.h"
6 #include "reflog.h"
7 #include "parse-options.h"
9 #define BUILTIN_REFLOG_SHOW_USAGE \
10 N_("git reflog [show] [<log-options>] [<ref>]")
12 #define BUILTIN_REFLOG_EXPIRE_USAGE \
13 N_("git reflog expire [--expire=<time>] [--expire-unreachable=<time>]\n" \
14 " [--rewrite] [--updateref] [--stale-fix]\n" \
15 " [--dry-run | -n] [--verbose] [--all [--single-worktree] | <refs>...]")
17 #define BUILTIN_REFLOG_DELETE_USAGE \
18 N_("git reflog delete [--rewrite] [--updateref]\n" \
19 " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
21 #define BUILTIN_REFLOG_EXISTS_USAGE \
22 N_("git reflog exists <ref>")
24 static const char *const reflog_show_usage[] = {
25 BUILTIN_REFLOG_SHOW_USAGE,
26 NULL,
29 static const char *const reflog_expire_usage[] = {
30 BUILTIN_REFLOG_EXPIRE_USAGE,
31 NULL
34 static const char *const reflog_delete_usage[] = {
35 BUILTIN_REFLOG_DELETE_USAGE,
36 NULL
39 static const char *const reflog_exists_usage[] = {
40 BUILTIN_REFLOG_EXISTS_USAGE,
41 NULL,
44 static const char *const reflog_usage[] = {
45 BUILTIN_REFLOG_SHOW_USAGE,
46 BUILTIN_REFLOG_EXPIRE_USAGE,
47 BUILTIN_REFLOG_DELETE_USAGE,
48 BUILTIN_REFLOG_EXISTS_USAGE,
49 NULL
52 static timestamp_t default_reflog_expire;
53 static timestamp_t default_reflog_expire_unreachable;
55 struct worktree_reflogs {
56 struct worktree *worktree;
57 struct string_list reflogs;
60 static int collect_reflog(const char *ref, const struct object_id *oid UNUSED,
61 int flags UNUSED, void *cb_data)
63 struct worktree_reflogs *cb = cb_data;
64 struct worktree *worktree = cb->worktree;
65 struct strbuf newref = STRBUF_INIT;
68 * Avoid collecting the same shared ref multiple times because
69 * they are available via all worktrees.
71 if (!worktree->is_current &&
72 parse_worktree_ref(ref, NULL, NULL, NULL) == REF_WORKTREE_SHARED)
73 return 0;
75 strbuf_worktree_ref(worktree, &newref, ref);
76 string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
78 return 0;
81 static struct reflog_expire_cfg {
82 struct reflog_expire_cfg *next;
83 timestamp_t expire_total;
84 timestamp_t expire_unreachable;
85 char pattern[FLEX_ARRAY];
86 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
88 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
90 struct reflog_expire_cfg *ent;
92 if (!reflog_expire_cfg_tail)
93 reflog_expire_cfg_tail = &reflog_expire_cfg;
95 for (ent = reflog_expire_cfg; ent; ent = ent->next)
96 if (!strncmp(ent->pattern, pattern, len) &&
97 ent->pattern[len] == '\0')
98 return ent;
100 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
101 *reflog_expire_cfg_tail = ent;
102 reflog_expire_cfg_tail = &(ent->next);
103 return ent;
106 /* expiry timer slot */
107 #define EXPIRE_TOTAL 01
108 #define EXPIRE_UNREACH 02
110 static int reflog_expire_config(const char *var, const char *value, void *cb)
112 const char *pattern, *key;
113 size_t pattern_len;
114 timestamp_t expire;
115 int slot;
116 struct reflog_expire_cfg *ent;
118 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
119 return git_default_config(var, value, cb);
121 if (!strcmp(key, "reflogexpire")) {
122 slot = EXPIRE_TOTAL;
123 if (git_config_expiry_date(&expire, var, value))
124 return -1;
125 } else if (!strcmp(key, "reflogexpireunreachable")) {
126 slot = EXPIRE_UNREACH;
127 if (git_config_expiry_date(&expire, var, value))
128 return -1;
129 } else
130 return git_default_config(var, value, cb);
132 if (!pattern) {
133 switch (slot) {
134 case EXPIRE_TOTAL:
135 default_reflog_expire = expire;
136 break;
137 case EXPIRE_UNREACH:
138 default_reflog_expire_unreachable = expire;
139 break;
141 return 0;
144 ent = find_cfg_ent(pattern, pattern_len);
145 if (!ent)
146 return -1;
147 switch (slot) {
148 case EXPIRE_TOTAL:
149 ent->expire_total = expire;
150 break;
151 case EXPIRE_UNREACH:
152 ent->expire_unreachable = expire;
153 break;
155 return 0;
158 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, const char *ref)
160 struct reflog_expire_cfg *ent;
162 if (cb->explicit_expiry == (EXPIRE_TOTAL|EXPIRE_UNREACH))
163 return; /* both given explicitly -- nothing to tweak */
165 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
166 if (!wildmatch(ent->pattern, ref, 0)) {
167 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
168 cb->expire_total = ent->expire_total;
169 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
170 cb->expire_unreachable = ent->expire_unreachable;
171 return;
176 * If unconfigured, make stash never expire
178 if (!strcmp(ref, "refs/stash")) {
179 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
180 cb->expire_total = 0;
181 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
182 cb->expire_unreachable = 0;
183 return;
186 /* Nothing matched -- use the default value */
187 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
188 cb->expire_total = default_reflog_expire;
189 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
190 cb->expire_unreachable = default_reflog_expire_unreachable;
193 static int expire_unreachable_callback(const struct option *opt,
194 const char *arg,
195 int unset)
197 struct cmd_reflog_expire_cb *cmd = opt->value;
199 BUG_ON_OPT_NEG(unset);
201 if (parse_expiry_date(arg, &cmd->expire_unreachable))
202 die(_("invalid timestamp '%s' given to '--%s'"),
203 arg, opt->long_name);
205 cmd->explicit_expiry |= EXPIRE_UNREACH;
206 return 0;
209 static int expire_total_callback(const struct option *opt,
210 const char *arg,
211 int unset)
213 struct cmd_reflog_expire_cb *cmd = opt->value;
215 BUG_ON_OPT_NEG(unset);
217 if (parse_expiry_date(arg, &cmd->expire_total))
218 die(_("invalid timestamp '%s' given to '--%s'"),
219 arg, opt->long_name);
221 cmd->explicit_expiry |= EXPIRE_TOTAL;
222 return 0;
225 static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
227 struct option options[] = {
228 OPT_END()
231 parse_options(argc, argv, prefix, options, reflog_show_usage,
232 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
233 PARSE_OPT_KEEP_UNKNOWN_OPT);
235 return cmd_log_reflog(argc, argv, prefix);
238 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
240 struct cmd_reflog_expire_cb cmd = { 0 };
241 timestamp_t now = time(NULL);
242 int i, status, do_all, all_worktrees = 1;
243 unsigned int flags = 0;
244 int verbose = 0;
245 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
246 const struct option options[] = {
247 OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
248 EXPIRE_REFLOGS_DRY_RUN),
249 OPT_BIT(0, "rewrite", &flags,
250 N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"),
251 EXPIRE_REFLOGS_REWRITE),
252 OPT_BIT(0, "updateref", &flags,
253 N_("update the reference to the value of the top reflog entry"),
254 EXPIRE_REFLOGS_UPDATE_REF),
255 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
256 OPT_CALLBACK_F(0, "expire", &cmd, N_("timestamp"),
257 N_("prune entries older than the specified time"),
258 PARSE_OPT_NONEG,
259 expire_total_callback),
260 OPT_CALLBACK_F(0, "expire-unreachable", &cmd, N_("timestamp"),
261 N_("prune entries older than <time> that are not reachable from the current tip of the branch"),
262 PARSE_OPT_NONEG,
263 expire_unreachable_callback),
264 OPT_BOOL(0, "stale-fix", &cmd.stalefix,
265 N_("prune any reflog entries that point to broken commits")),
266 OPT_BOOL(0, "all", &do_all, N_("process the reflogs of all references")),
267 OPT_BOOL(1, "single-worktree", &all_worktrees,
268 N_("limits processing to reflogs from the current worktree only")),
269 OPT_END()
272 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
273 default_reflog_expire = now - 90 * 24 * 3600;
274 git_config(reflog_expire_config, NULL);
276 save_commit_buffer = 0;
277 do_all = status = 0;
279 cmd.explicit_expiry = 0;
280 cmd.expire_total = default_reflog_expire;
281 cmd.expire_unreachable = default_reflog_expire_unreachable;
283 argc = parse_options(argc, argv, prefix, options, reflog_expire_usage, 0);
285 if (verbose)
286 should_prune_fn = should_expire_reflog_ent_verbose;
289 * We can trust the commits and objects reachable from refs
290 * even in older repository. We cannot trust what's reachable
291 * from reflog if the repository was pruned with older git.
293 if (cmd.stalefix) {
294 struct rev_info revs;
296 repo_init_revisions(the_repository, &revs, prefix);
297 revs.do_not_die_on_missing_tree = 1;
298 revs.ignore_missing = 1;
299 revs.ignore_missing_links = 1;
300 if (verbose)
301 printf(_("Marking reachable objects..."));
302 mark_reachable_objects(&revs, 0, 0, NULL);
303 release_revisions(&revs);
304 if (verbose)
305 putchar('\n');
308 if (do_all) {
309 struct worktree_reflogs collected = {
310 .reflogs = STRING_LIST_INIT_DUP,
312 struct string_list_item *item;
313 struct worktree **worktrees, **p;
315 worktrees = get_worktrees();
316 for (p = worktrees; *p; p++) {
317 if (!all_worktrees && !(*p)->is_current)
318 continue;
319 collected.worktree = *p;
320 refs_for_each_reflog(get_worktree_ref_store(*p),
321 collect_reflog, &collected);
323 free_worktrees(worktrees);
325 for_each_string_list_item(item, &collected.reflogs) {
326 struct expire_reflog_policy_cb cb = {
327 .cmd = cmd,
328 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
331 set_reflog_expiry_param(&cb.cmd, item->string);
332 status |= reflog_expire(item->string, flags,
333 reflog_expiry_prepare,
334 should_prune_fn,
335 reflog_expiry_cleanup,
336 &cb);
338 string_list_clear(&collected.reflogs, 0);
341 for (i = 0; i < argc; i++) {
342 char *ref;
343 struct expire_reflog_policy_cb cb = { .cmd = cmd };
345 if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
346 status |= error(_("%s points nowhere!"), argv[i]);
347 continue;
349 set_reflog_expiry_param(&cb.cmd, ref);
350 status |= reflog_expire(ref, flags,
351 reflog_expiry_prepare,
352 should_prune_fn,
353 reflog_expiry_cleanup,
354 &cb);
355 free(ref);
357 return status;
360 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
362 int i, status = 0;
363 unsigned int flags = 0;
364 int verbose = 0;
366 const struct option options[] = {
367 OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
368 EXPIRE_REFLOGS_DRY_RUN),
369 OPT_BIT(0, "rewrite", &flags,
370 N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"),
371 EXPIRE_REFLOGS_REWRITE),
372 OPT_BIT(0, "updateref", &flags,
373 N_("update the reference to the value of the top reflog entry"),
374 EXPIRE_REFLOGS_UPDATE_REF),
375 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
376 OPT_END()
379 argc = parse_options(argc, argv, prefix, options, reflog_delete_usage, 0);
381 if (argc < 1)
382 return error(_("no reflog specified to delete"));
384 for (i = 0; i < argc; i++)
385 status |= reflog_delete(argv[i], flags, verbose);
387 return status;
390 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
392 struct option options[] = {
393 OPT_END()
395 const char *refname;
397 argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
399 if (!argc)
400 usage_with_options(reflog_exists_usage, options);
402 refname = argv[0];
403 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
404 die(_("invalid ref format: %s"), refname);
405 return !reflog_exists(refname);
409 * main "reflog"
412 int cmd_reflog(int argc, const char **argv, const char *prefix)
414 parse_opt_subcommand_fn *fn = NULL;
415 struct option options[] = {
416 OPT_SUBCOMMAND("show", &fn, cmd_reflog_show),
417 OPT_SUBCOMMAND("expire", &fn, cmd_reflog_expire),
418 OPT_SUBCOMMAND("delete", &fn, cmd_reflog_delete),
419 OPT_SUBCOMMAND("exists", &fn, cmd_reflog_exists),
420 OPT_END()
423 argc = parse_options(argc, argv, prefix, options, reflog_usage,
424 PARSE_OPT_SUBCOMMAND_OPTIONAL |
425 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
426 PARSE_OPT_KEEP_UNKNOWN_OPT);
427 if (fn)
428 return fn(argc - 1, argv + 1, prefix);
429 else
430 return cmd_log_reflog(argc, argv, prefix);