]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/reflog.c
Start the 2.46 cycle
[thirdparty/git.git] / builtin / reflog.c
CommitLineData
4264dc15 1#include "builtin.h"
b2141fc1 2#include "config.h"
f394e093 3#include "gettext.h"
df6e8744 4#include "repository.h"
1389d9dd
JH
5#include "revision.h"
6#include "reachable.h"
dd77d587 7#include "wildmatch.h"
c9ef0d95 8#include "worktree.h"
7d3d226e 9#include "reflog.h"
d699d15c 10#include "refs.h"
49fd5511 11#include "parse-options.h"
1389d9dd 12
fbc15b13
ÆAB
13#define BUILTIN_REFLOG_SHOW_USAGE \
14 N_("git reflog [show] [<log-options>] [<ref>]")
15
d699d15c
PS
16#define BUILTIN_REFLOG_LIST_USAGE \
17 N_("git reflog list")
18
1e91d3fa 19#define BUILTIN_REFLOG_EXPIRE_USAGE \
cbe48529
ÆAB
20 N_("git reflog expire [--expire=<time>] [--expire-unreachable=<time>]\n" \
21 " [--rewrite] [--updateref] [--stale-fix]\n" \
22 " [--dry-run | -n] [--verbose] [--all [--single-worktree] | <refs>...]")
1e91d3fa
ÆAB
23
24#define BUILTIN_REFLOG_DELETE_USAGE \
cbe48529
ÆAB
25 N_("git reflog delete [--rewrite] [--updateref]\n" \
26 " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
1e91d3fa
ÆAB
27
28#define BUILTIN_REFLOG_EXISTS_USAGE \
29 N_("git reflog exists <ref>")
30
fbc15b13
ÆAB
31static const char *const reflog_show_usage[] = {
32 BUILTIN_REFLOG_SHOW_USAGE,
33 NULL,
34};
35
d699d15c
PS
36static const char *const reflog_list_usage[] = {
37 BUILTIN_REFLOG_LIST_USAGE,
38 NULL,
39};
40
1e91d3fa
ÆAB
41static const char *const reflog_expire_usage[] = {
42 BUILTIN_REFLOG_EXPIRE_USAGE,
43 NULL
44};
45
46static const char *const reflog_delete_usage[] = {
47 BUILTIN_REFLOG_DELETE_USAGE,
48 NULL
49};
50
a34393f5
ÆAB
51static const char *const reflog_exists_usage[] = {
52 BUILTIN_REFLOG_EXISTS_USAGE,
53 NULL,
54};
4264dc15 55
e3c36758 56static const char *const reflog_usage[] = {
fbc15b13 57 BUILTIN_REFLOG_SHOW_USAGE,
d699d15c 58 BUILTIN_REFLOG_LIST_USAGE,
e3c36758
ÆAB
59 BUILTIN_REFLOG_EXPIRE_USAGE,
60 BUILTIN_REFLOG_DELETE_USAGE,
61 BUILTIN_REFLOG_EXISTS_USAGE,
62 NULL
63};
64
dddbad72
JS
65static timestamp_t default_reflog_expire;
66static timestamp_t default_reflog_expire_unreachable;
4aec56d1 67
f2919bae
ÆAB
68struct worktree_reflogs {
69 struct worktree *worktree;
70 struct string_list reflogs;
bda3a31c
JH
71};
72
31f89839 73static int collect_reflog(const char *ref, void *cb_data)
bda3a31c 74{
f2919bae
ÆAB
75 struct worktree_reflogs *cb = cb_data;
76 struct worktree *worktree = cb->worktree;
c9ef0d95
NTND
77 struct strbuf newref = STRBUF_INIT;
78
79 /*
80 * Avoid collecting the same shared ref multiple times because
81 * they are available via all worktrees.
82 */
71e54734
HWN
83 if (!worktree->is_current &&
84 parse_worktree_ref(ref, NULL, NULL, NULL) == REF_WORKTREE_SHARED)
c9ef0d95
NTND
85 return 0;
86
f2919bae
ÆAB
87 strbuf_worktree_ref(worktree, &newref, ref);
88 string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
bda3a31c 89
bda3a31c
JH
90 return 0;
91}
92
3cb22b8e
JH
93static struct reflog_expire_cfg {
94 struct reflog_expire_cfg *next;
dddbad72
JS
95 timestamp_t expire_total;
96 timestamp_t expire_unreachable;
3cb22b8e
JH
97 char pattern[FLEX_ARRAY];
98} *reflog_expire_cfg, **reflog_expire_cfg_tail;
99
100static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
4aec56d1 101{
3cb22b8e
JH
102 struct reflog_expire_cfg *ent;
103
104 if (!reflog_expire_cfg_tail)
105 reflog_expire_cfg_tail = &reflog_expire_cfg;
106
107 for (ent = reflog_expire_cfg; ent; ent = ent->next)
f0e578c6 108 if (!xstrncmpz(ent->pattern, pattern, len))
3cb22b8e
JH
109 return ent;
110
96ffc06f 111 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
3cb22b8e
JH
112 *reflog_expire_cfg_tail = ent;
113 reflog_expire_cfg_tail = &(ent->next);
114 return ent;
115}
116
3cb22b8e
JH
117/* expiry timer slot */
118#define EXPIRE_TOTAL 01
119#define EXPIRE_UNREACH 02
120
a4e7e317
GC
121static int reflog_expire_config(const char *var, const char *value,
122 const struct config_context *ctx, void *cb)
3cb22b8e 123{
b3873c33 124 const char *pattern, *key;
f5914f4b 125 size_t pattern_len;
dddbad72 126 timestamp_t expire;
3cb22b8e
JH
127 int slot;
128 struct reflog_expire_cfg *ent;
129
b3873c33 130 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
a4e7e317 131 return git_default_config(var, value, ctx, cb);
3cb22b8e 132
b3873c33 133 if (!strcmp(key, "reflogexpire")) {
3cb22b8e 134 slot = EXPIRE_TOTAL;
5f967424 135 if (git_config_expiry_date(&expire, var, value))
3cb22b8e 136 return -1;
b3873c33 137 } else if (!strcmp(key, "reflogexpireunreachable")) {
3cb22b8e 138 slot = EXPIRE_UNREACH;
5f967424 139 if (git_config_expiry_date(&expire, var, value))
3cb22b8e
JH
140 return -1;
141 } else
a4e7e317 142 return git_default_config(var, value, ctx, cb);
3cb22b8e 143
b3873c33 144 if (!pattern) {
3cb22b8e
JH
145 switch (slot) {
146 case EXPIRE_TOTAL:
147 default_reflog_expire = expire;
148 break;
149 case EXPIRE_UNREACH:
150 default_reflog_expire_unreachable = expire;
151 break;
152 }
4f342b96
JH
153 return 0;
154 }
3cb22b8e 155
b3873c33 156 ent = find_cfg_ent(pattern, pattern_len);
3cb22b8e
JH
157 if (!ent)
158 return -1;
159 switch (slot) {
160 case EXPIRE_TOTAL:
161 ent->expire_total = expire;
162 break;
163 case EXPIRE_UNREACH:
164 ent->expire_unreachable = expire;
165 break;
166 }
167 return 0;
168}
169
33d7bdd6 170static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, const char *ref)
3cb22b8e
JH
171{
172 struct reflog_expire_cfg *ent;
173
33d7bdd6 174 if (cb->explicit_expiry == (EXPIRE_TOTAL|EXPIRE_UNREACH))
3cb22b8e
JH
175 return; /* both given explicitly -- nothing to tweak */
176
177 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
55d34269 178 if (!wildmatch(ent->pattern, ref, 0)) {
33d7bdd6 179 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
3cb22b8e 180 cb->expire_total = ent->expire_total;
33d7bdd6 181 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
3cb22b8e
JH
182 cb->expire_unreachable = ent->expire_unreachable;
183 return;
184 }
185 }
186
60bce2bb
JH
187 /*
188 * If unconfigured, make stash never expire
189 */
190 if (!strcmp(ref, "refs/stash")) {
33d7bdd6 191 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
60bce2bb 192 cb->expire_total = 0;
33d7bdd6 193 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
60bce2bb
JH
194 cb->expire_unreachable = 0;
195 return;
196 }
197
3cb22b8e 198 /* Nothing matched -- use the default value */
33d7bdd6 199 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
3cb22b8e 200 cb->expire_total = default_reflog_expire;
33d7bdd6 201 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
3cb22b8e 202 cb->expire_unreachable = default_reflog_expire_unreachable;
4aec56d1
JH
203}
204
33d7bdd6
JC
205static int expire_unreachable_callback(const struct option *opt,
206 const char *arg,
207 int unset)
208{
209 struct cmd_reflog_expire_cb *cmd = opt->value;
210
ee610f00
JK
211 BUG_ON_OPT_NEG(unset);
212
33d7bdd6
JC
213 if (parse_expiry_date(arg, &cmd->expire_unreachable))
214 die(_("invalid timestamp '%s' given to '--%s'"),
215 arg, opt->long_name);
216
217 cmd->explicit_expiry |= EXPIRE_UNREACH;
218 return 0;
219}
220
221static int expire_total_callback(const struct option *opt,
222 const char *arg,
223 int unset)
224{
225 struct cmd_reflog_expire_cb *cmd = opt->value;
226
ee610f00
JK
227 BUG_ON_OPT_NEG(unset);
228
33d7bdd6
JC
229 if (parse_expiry_date(arg, &cmd->expire_total))
230 die(_("invalid timestamp '%s' given to '--%s'"),
231 arg, opt->long_name);
232
233 cmd->explicit_expiry |= EXPIRE_TOTAL;
234 return 0;
235}
236
fbc15b13
ÆAB
237static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
238{
239 struct option options[] = {
240 OPT_END()
241 };
242
243 parse_options(argc, argv, prefix, options, reflog_show_usage,
244 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
99d86d60 245 PARSE_OPT_KEEP_UNKNOWN_OPT);
fbc15b13 246
840344db 247 return cmd_log_reflog(argc, argv, prefix);
fbc15b13
ÆAB
248}
249
d699d15c
PS
250static int show_reflog(const char *refname, void *cb_data UNUSED)
251{
252 printf("%s\n", refname);
253 return 0;
254}
255
256static int cmd_reflog_list(int argc, const char **argv, const char *prefix)
257{
258 struct option options[] = {
259 OPT_END()
260 };
261 struct ref_store *ref_store;
262
263 argc = parse_options(argc, argv, prefix, options, reflog_list_usage, 0);
264 if (argc)
265 return error(_("%s does not accept arguments: '%s'"),
266 "list", argv[0]);
267
268 ref_store = get_main_ref_store(the_repository);
269
270 return refs_for_each_reflog(ref_store, show_reflog, NULL);
271}
272
4264dc15
JH
273static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
274{
46fbe418 275 struct cmd_reflog_expire_cb cmd = { 0 };
dddbad72 276 timestamp_t now = time(NULL);
26d4c51d 277 int i, status, do_all, single_worktree = 0;
aba56c89 278 unsigned int flags = 0;
fcd2c3d9
ÆAB
279 int verbose = 0;
280 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
33d7bdd6 281 const struct option options[] = {
cbf498eb 282 OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
33d7bdd6
JC
283 EXPIRE_REFLOGS_DRY_RUN),
284 OPT_BIT(0, "rewrite", &flags,
285 N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"),
286 EXPIRE_REFLOGS_REWRITE),
287 OPT_BIT(0, "updateref", &flags,
288 N_("update the reference to the value of the top reflog entry"),
289 EXPIRE_REFLOGS_UPDATE_REF),
9e1f22c8 290 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
33d7bdd6
JC
291 OPT_CALLBACK_F(0, "expire", &cmd, N_("timestamp"),
292 N_("prune entries older than the specified time"),
293 PARSE_OPT_NONEG,
294 expire_total_callback),
295 OPT_CALLBACK_F(0, "expire-unreachable", &cmd, N_("timestamp"),
296 N_("prune entries older than <time> that are not reachable from the current tip of the branch"),
297 PARSE_OPT_NONEG,
298 expire_unreachable_callback),
299 OPT_BOOL(0, "stale-fix", &cmd.stalefix,
300 N_("prune any reflog entries that point to broken commits")),
301 OPT_BOOL(0, "all", &do_all, N_("process the reflogs of all references")),
26d4c51d 302 OPT_BOOL(0, "single-worktree", &single_worktree,
9e1f22c8 303 N_("limits processing to reflogs from the current worktree only")),
33d7bdd6
JC
304 OPT_END()
305 };
4264dc15 306
4a9f4394
AS
307 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
308 default_reflog_expire = now - 90 * 24 * 3600;
ef90d6d4 309 git_config(reflog_expire_config, NULL);
4aec56d1 310
4264dc15
JH
311 save_commit_buffer = 0;
312 do_all = status = 0;
4aec56d1 313
33d7bdd6 314 cmd.explicit_expiry = 0;
46fbe418
ÆAB
315 cmd.expire_total = default_reflog_expire;
316 cmd.expire_unreachable = default_reflog_expire_unreachable;
4264dc15 317
33d7bdd6 318 argc = parse_options(argc, argv, prefix, options, reflog_expire_usage, 0);
3cb22b8e 319
fcd2c3d9
ÆAB
320 if (verbose)
321 should_prune_fn = should_expire_reflog_ent_verbose;
322
3cb22b8e
JH
323 /*
324 * We can trust the commits and objects reachable from refs
325 * even in older repository. We cannot trust what's reachable
326 * from reflog if the repository was pruned with older git.
327 */
46fbe418 328 if (cmd.stalefix) {
994b328f
ÆAB
329 struct rev_info revs;
330
331 repo_init_revisions(the_repository, &revs, prefix);
ca556f47 332 revs.do_not_die_on_missing_objects = 1;
994b328f
ÆAB
333 revs.ignore_missing = 1;
334 revs.ignore_missing_links = 1;
fcd2c3d9 335 if (verbose)
dd509db3 336 printf(_("Marking reachable objects..."));
994b328f 337 mark_reachable_objects(&revs, 0, 0, NULL);
2108fe4a 338 release_revisions(&revs);
fcd2c3d9 339 if (verbose)
1389d9dd
JH
340 putchar('\n');
341 }
342
bda3a31c 343 if (do_all) {
f2919bae
ÆAB
344 struct worktree_reflogs collected = {
345 .reflogs = STRING_LIST_INIT_DUP,
346 };
347 struct string_list_item *item;
c9ef0d95 348 struct worktree **worktrees, **p;
bda3a31c 349
03f2465b 350 worktrees = get_worktrees();
c9ef0d95 351 for (p = worktrees; *p; p++) {
26d4c51d 352 if (single_worktree && !(*p)->is_current)
c9ef0d95 353 continue;
f2919bae 354 collected.worktree = *p;
c9ef0d95
NTND
355 refs_for_each_reflog(get_worktree_ref_store(*p),
356 collect_reflog, &collected);
357 }
358 free_worktrees(worktrees);
f2919bae
ÆAB
359
360 for_each_string_list_item(item, &collected.reflogs) {
fcd2c3d9
ÆAB
361 struct expire_reflog_policy_cb cb = {
362 .cmd = cmd,
363 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
364 };
ae35e16c 365
33d7bdd6 366 set_reflog_expiry_param(&cb.cmd, item->string);
f2919bae 367 status |= reflog_expire(item->string, flags,
fa5b1830 368 reflog_expiry_prepare,
fcd2c3d9 369 should_prune_fn,
fa5b1830
MH
370 reflog_expiry_cleanup,
371 &cb);
bda3a31c 372 }
f2919bae 373 string_list_clear(&collected.reflogs, 0);
bda3a31c
JH
374 }
375
33d7bdd6 376 for (i = 0; i < argc; i++) {
90fb46ec 377 char *ref;
46fbe418
ÆAB
378 struct expire_reflog_policy_cb cb = { .cmd = cmd };
379
ae35e16c 380 if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
dd509db3 381 status |= error(_("%s points nowhere!"), argv[i]);
4264dc15
JH
382 continue;
383 }
33d7bdd6 384 set_reflog_expiry_param(&cb.cmd, ref);
cc40b5ce 385 status |= reflog_expire(ref, flags,
fa5b1830 386 reflog_expiry_prepare,
fcd2c3d9 387 should_prune_fn,
fa5b1830
MH
388 reflog_expiry_cleanup,
389 &cb);
3c815049 390 free(ref);
4264dc15
JH
391 }
392 return status;
393}
394
552cecc2
JS
395static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
396{
552cecc2 397 int i, status = 0;
aba56c89 398 unsigned int flags = 0;
fcd2c3d9 399 int verbose = 0;
7d3d226e 400
33d7bdd6 401 const struct option options[] = {
cbf498eb 402 OPT_BIT('n', "dry-run", &flags, N_("do not actually prune any entries"),
33d7bdd6
JC
403 EXPIRE_REFLOGS_DRY_RUN),
404 OPT_BIT(0, "rewrite", &flags,
405 N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"),
406 EXPIRE_REFLOGS_REWRITE),
407 OPT_BIT(0, "updateref", &flags,
408 N_("update the reference to the value of the top reflog entry"),
409 EXPIRE_REFLOGS_UPDATE_REF),
9e1f22c8 410 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
33d7bdd6
JC
411 OPT_END()
412 };
413
414 argc = parse_options(argc, argv, prefix, options, reflog_delete_usage, 0);
3c386aa3 415
33d7bdd6 416 if (argc < 1)
dd509db3 417 return error(_("no reflog specified to delete"));
3c386aa3 418
7d3d226e
JC
419 for (i = 0; i < argc; i++)
420 status |= reflog_delete(argv[i], flags, verbose);
552cecc2 421
552cecc2
JS
422 return status;
423}
424
afcb2e7a
DT
425static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
426{
a34393f5
ÆAB
427 struct option options[] = {
428 OPT_END()
429 };
430 const char *refname;
afcb2e7a 431
a34393f5
ÆAB
432 argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
433 0);
434 if (!argc)
435 usage_with_options(reflog_exists_usage, options);
afcb2e7a 436
a34393f5
ÆAB
437 refname = argv[0];
438 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
439 die(_("invalid ref format: %s"), refname);
440 return !reflog_exists(refname);
afcb2e7a
DT
441}
442
1389d9dd
JH
443/*
444 * main "reflog"
445 */
446
4264dc15
JH
447int cmd_reflog(int argc, const char **argv, const char *prefix)
448{
729b9733 449 parse_opt_subcommand_fn *fn = NULL;
e3c36758 450 struct option options[] = {
729b9733 451 OPT_SUBCOMMAND("show", &fn, cmd_reflog_show),
d699d15c 452 OPT_SUBCOMMAND("list", &fn, cmd_reflog_list),
729b9733
SG
453 OPT_SUBCOMMAND("expire", &fn, cmd_reflog_expire),
454 OPT_SUBCOMMAND("delete", &fn, cmd_reflog_delete),
455 OPT_SUBCOMMAND("exists", &fn, cmd_reflog_exists),
e3c36758
ÆAB
456 OPT_END()
457 };
458
459 argc = parse_options(argc, argv, prefix, options, reflog_usage,
729b9733 460 PARSE_OPT_SUBCOMMAND_OPTIONAL |
e3c36758 461 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
729b9733
SG
462 PARSE_OPT_KEEP_UNKNOWN_OPT);
463 if (fn)
464 return fn(argc - 1, argv + 1, prefix);
465 else
466 return cmd_log_reflog(argc, argv, prefix);
4264dc15 467}