Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / builtin / reflog.c
blob4dd297dce86e5212087ee1202d5b9dd1f754f9d1
1 #include "builtin.h"
2 #include "config.h"
3 #include "revision.h"
4 #include "reachable.h"
5 #include "worktree.h"
6 #include "reflog.h"
8 #define BUILTIN_REFLOG_SHOW_USAGE \
9 N_("git reflog [show] [<log-options>] [<ref>]")
11 #define BUILTIN_REFLOG_EXPIRE_USAGE \
12 N_("git reflog expire [--expire=<time>] [--expire-unreachable=<time>]\n" \
13 " [--rewrite] [--updateref] [--stale-fix]\n" \
14 " [--dry-run | -n] [--verbose] [--all [--single-worktree] | <refs>...]")
16 #define BUILTIN_REFLOG_DELETE_USAGE \
17 N_("git reflog delete [--rewrite] [--updateref]\n" \
18 " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
20 #define BUILTIN_REFLOG_EXISTS_USAGE \
21 N_("git reflog exists <ref>")
23 static const char *const reflog_show_usage[] = {
24 BUILTIN_REFLOG_SHOW_USAGE,
25 NULL,
28 static const char *const reflog_expire_usage[] = {
29 BUILTIN_REFLOG_EXPIRE_USAGE,
30 NULL
33 static const char *const reflog_delete_usage[] = {
34 BUILTIN_REFLOG_DELETE_USAGE,
35 NULL
38 static const char *const reflog_exists_usage[] = {
39 BUILTIN_REFLOG_EXISTS_USAGE,
40 NULL,
43 static const char *const reflog_usage[] = {
44 BUILTIN_REFLOG_SHOW_USAGE,
45 BUILTIN_REFLOG_EXPIRE_USAGE,
46 BUILTIN_REFLOG_DELETE_USAGE,
47 BUILTIN_REFLOG_EXISTS_USAGE,
48 NULL
51 static timestamp_t default_reflog_expire;
52 static timestamp_t default_reflog_expire_unreachable;
54 struct worktree_reflogs {
55 struct worktree *worktree;
56 struct string_list reflogs;
59 static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
61 struct worktree_reflogs *cb = cb_data;
62 struct worktree *worktree = cb->worktree;
63 struct strbuf newref = STRBUF_INIT;
66 * Avoid collecting the same shared ref multiple times because
67 * they are available via all worktrees.
69 if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
70 return 0;
72 strbuf_worktree_ref(worktree, &newref, ref);
73 string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
75 return 0;
78 static struct reflog_expire_cfg {
79 struct reflog_expire_cfg *next;
80 timestamp_t expire_total;
81 timestamp_t expire_unreachable;
82 char pattern[FLEX_ARRAY];
83 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
85 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
87 struct reflog_expire_cfg *ent;
89 if (!reflog_expire_cfg_tail)
90 reflog_expire_cfg_tail = &reflog_expire_cfg;
92 for (ent = reflog_expire_cfg; ent; ent = ent->next)
93 if (!strncmp(ent->pattern, pattern, len) &&
94 ent->pattern[len] == '\0')
95 return ent;
97 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
98 *reflog_expire_cfg_tail = ent;
99 reflog_expire_cfg_tail = &(ent->next);
100 return ent;
103 /* expiry timer slot */
104 #define EXPIRE_TOTAL 01
105 #define EXPIRE_UNREACH 02
107 static int reflog_expire_config(const char *var, const char *value, void *cb)
109 const char *pattern, *key;
110 size_t pattern_len;
111 timestamp_t expire;
112 int slot;
113 struct reflog_expire_cfg *ent;
115 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
116 return git_default_config(var, value, cb);
118 if (!strcmp(key, "reflogexpire")) {
119 slot = EXPIRE_TOTAL;
120 if (git_config_expiry_date(&expire, var, value))
121 return -1;
122 } else if (!strcmp(key, "reflogexpireunreachable")) {
123 slot = EXPIRE_UNREACH;
124 if (git_config_expiry_date(&expire, var, value))
125 return -1;
126 } else
127 return git_default_config(var, value, cb);
129 if (!pattern) {
130 switch (slot) {
131 case EXPIRE_TOTAL:
132 default_reflog_expire = expire;
133 break;
134 case EXPIRE_UNREACH:
135 default_reflog_expire_unreachable = expire;
136 break;
138 return 0;
141 ent = find_cfg_ent(pattern, pattern_len);
142 if (!ent)
143 return -1;
144 switch (slot) {
145 case EXPIRE_TOTAL:
146 ent->expire_total = expire;
147 break;
148 case EXPIRE_UNREACH:
149 ent->expire_unreachable = expire;
150 break;
152 return 0;
155 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, const char *ref)
157 struct reflog_expire_cfg *ent;
159 if (cb->explicit_expiry == (EXPIRE_TOTAL|EXPIRE_UNREACH))
160 return; /* both given explicitly -- nothing to tweak */
162 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
163 if (!wildmatch(ent->pattern, ref, 0)) {
164 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
165 cb->expire_total = ent->expire_total;
166 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
167 cb->expire_unreachable = ent->expire_unreachable;
168 return;
173 * If unconfigured, make stash never expire
175 if (!strcmp(ref, "refs/stash")) {
176 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
177 cb->expire_total = 0;
178 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
179 cb->expire_unreachable = 0;
180 return;
183 /* Nothing matched -- use the default value */
184 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
185 cb->expire_total = default_reflog_expire;
186 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
187 cb->expire_unreachable = default_reflog_expire_unreachable;
190 static int expire_unreachable_callback(const struct option *opt,
191 const char *arg,
192 int unset)
194 struct cmd_reflog_expire_cb *cmd = opt->value;
196 if (parse_expiry_date(arg, &cmd->expire_unreachable))
197 die(_("invalid timestamp '%s' given to '--%s'"),
198 arg, opt->long_name);
200 cmd->explicit_expiry |= EXPIRE_UNREACH;
201 return 0;
204 static int expire_total_callback(const struct option *opt,
205 const char *arg,
206 int unset)
208 struct cmd_reflog_expire_cb *cmd = opt->value;
210 if (parse_expiry_date(arg, &cmd->expire_total))
211 die(_("invalid timestamp '%s' given to '--%s'"),
212 arg, opt->long_name);
214 cmd->explicit_expiry |= EXPIRE_TOTAL;
215 return 0;
218 static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
220 struct option options[] = {
221 OPT_END()
224 parse_options(argc, argv, prefix, options, reflog_show_usage,
225 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
226 PARSE_OPT_KEEP_UNKNOWN);
228 return cmd_log_reflog(argc, argv, prefix);
231 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
233 struct cmd_reflog_expire_cb cmd = { 0 };
234 timestamp_t now = time(NULL);
235 int i, status, do_all, all_worktrees = 1;
236 unsigned int flags = 0;
237 int verbose = 0;
238 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
239 const struct option options[] = {
240 OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
241 EXPIRE_REFLOGS_DRY_RUN),
242 OPT_BIT(0, "rewrite", &flags,
243 N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"),
244 EXPIRE_REFLOGS_REWRITE),
245 OPT_BIT(0, "updateref", &flags,
246 N_("update the reference to the value of the top reflog entry"),
247 EXPIRE_REFLOGS_UPDATE_REF),
248 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
249 OPT_CALLBACK_F(0, "expire", &cmd, N_("timestamp"),
250 N_("prune entries older than the specified time"),
251 PARSE_OPT_NONEG,
252 expire_total_callback),
253 OPT_CALLBACK_F(0, "expire-unreachable", &cmd, N_("timestamp"),
254 N_("prune entries older than <time> that are not reachable from the current tip of the branch"),
255 PARSE_OPT_NONEG,
256 expire_unreachable_callback),
257 OPT_BOOL(0, "stale-fix", &cmd.stalefix,
258 N_("prune any reflog entries that point to broken commits")),
259 OPT_BOOL(0, "all", &do_all, N_("process the reflogs of all references")),
260 OPT_BOOL(1, "single-worktree", &all_worktrees,
261 N_("limits processing to reflogs from the current worktree only")),
262 OPT_END()
265 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
266 default_reflog_expire = now - 90 * 24 * 3600;
267 git_config(reflog_expire_config, NULL);
269 save_commit_buffer = 0;
270 do_all = status = 0;
272 cmd.explicit_expiry = 0;
273 cmd.expire_total = default_reflog_expire;
274 cmd.expire_unreachable = default_reflog_expire_unreachable;
276 argc = parse_options(argc, argv, prefix, options, reflog_expire_usage, 0);
278 if (verbose)
279 should_prune_fn = should_expire_reflog_ent_verbose;
282 * We can trust the commits and objects reachable from refs
283 * even in older repository. We cannot trust what's reachable
284 * from reflog if the repository was pruned with older git.
286 if (cmd.stalefix) {
287 struct rev_info revs;
289 repo_init_revisions(the_repository, &revs, prefix);
290 revs.do_not_die_on_missing_tree = 1;
291 revs.ignore_missing = 1;
292 revs.ignore_missing_links = 1;
293 if (verbose)
294 printf(_("Marking reachable objects..."));
295 mark_reachable_objects(&revs, 0, 0, NULL);
296 release_revisions(&revs);
297 if (verbose)
298 putchar('\n');
301 if (do_all) {
302 struct worktree_reflogs collected = {
303 .reflogs = STRING_LIST_INIT_DUP,
305 struct string_list_item *item;
306 struct worktree **worktrees, **p;
308 worktrees = get_worktrees();
309 for (p = worktrees; *p; p++) {
310 if (!all_worktrees && !(*p)->is_current)
311 continue;
312 collected.worktree = *p;
313 refs_for_each_reflog(get_worktree_ref_store(*p),
314 collect_reflog, &collected);
316 free_worktrees(worktrees);
318 for_each_string_list_item(item, &collected.reflogs) {
319 struct expire_reflog_policy_cb cb = {
320 .cmd = cmd,
321 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
324 set_reflog_expiry_param(&cb.cmd, item->string);
325 status |= reflog_expire(item->string, flags,
326 reflog_expiry_prepare,
327 should_prune_fn,
328 reflog_expiry_cleanup,
329 &cb);
331 string_list_clear(&collected.reflogs, 0);
334 for (i = 0; i < argc; i++) {
335 char *ref;
336 struct expire_reflog_policy_cb cb = { .cmd = cmd };
338 if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
339 status |= error(_("%s points nowhere!"), argv[i]);
340 continue;
342 set_reflog_expiry_param(&cb.cmd, ref);
343 status |= reflog_expire(ref, flags,
344 reflog_expiry_prepare,
345 should_prune_fn,
346 reflog_expiry_cleanup,
347 &cb);
348 free(ref);
350 return status;
353 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
355 int i, status = 0;
356 unsigned int flags = 0;
357 int verbose = 0;
359 const struct option options[] = {
360 OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
361 EXPIRE_REFLOGS_DRY_RUN),
362 OPT_BIT(0, "rewrite", &flags,
363 N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"),
364 EXPIRE_REFLOGS_REWRITE),
365 OPT_BIT(0, "updateref", &flags,
366 N_("update the reference to the value of the top reflog entry"),
367 EXPIRE_REFLOGS_UPDATE_REF),
368 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
369 OPT_END()
372 argc = parse_options(argc, argv, prefix, options, reflog_delete_usage, 0);
374 if (argc < 1)
375 return error(_("no reflog specified to delete"));
377 for (i = 0; i < argc; i++)
378 status |= reflog_delete(argv[i], flags, verbose);
380 return status;
383 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
385 struct option options[] = {
386 OPT_END()
388 const char *refname;
390 argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
392 if (!argc)
393 usage_with_options(reflog_exists_usage, options);
395 refname = argv[0];
396 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
397 die(_("invalid ref format: %s"), refname);
398 return !reflog_exists(refname);
402 * main "reflog"
405 int cmd_reflog(int argc, const char **argv, const char *prefix)
407 struct option options[] = {
408 OPT_END()
411 argc = parse_options(argc, argv, prefix, options, reflog_usage,
412 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
413 PARSE_OPT_KEEP_UNKNOWN |
414 PARSE_OPT_NO_INTERNAL_HELP);
417 * With "git reflog" we default to showing it. !argc is
418 * impossible with PARSE_OPT_KEEP_ARGV0.
420 if (argc == 1)
421 goto log_reflog;
423 if (!strcmp(argv[1], "-h"))
424 usage_with_options(reflog_usage, options);
425 else if (*argv[1] == '-')
426 goto log_reflog;
428 if (!strcmp(argv[1], "show"))
429 return cmd_reflog_show(argc - 1, argv + 1, prefix);
430 else if (!strcmp(argv[1], "expire"))
431 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
432 else if (!strcmp(argv[1], "delete"))
433 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
434 else if (!strcmp(argv[1], "exists"))
435 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
438 * Fall-through for e.g. "git reflog -1", "git reflog master",
439 * as well as the plain "git reflog" above goto above.
441 log_reflog:
442 return cmd_log_reflog(argc, argv, prefix);