]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/reflog.c
Merge branch 'gc/branch-recurse-submodules-fix'
[thirdparty/git.git] / builtin / reflog.c
CommitLineData
4264dc15 1#include "builtin.h"
b2141fc1 2#include "config.h"
1389d9dd
JH
3#include "revision.h"
4#include "reachable.h"
c9ef0d95 5#include "worktree.h"
7d3d226e 6#include "reflog.h"
1389d9dd 7
fbc15b13
ÆAB
8#define BUILTIN_REFLOG_SHOW_USAGE \
9 N_("git reflog [show] [<log-options>] [<ref>]")
10
1e91d3fa 11#define BUILTIN_REFLOG_EXPIRE_USAGE \
cbe48529
ÆAB
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>...]")
1e91d3fa
ÆAB
15
16#define BUILTIN_REFLOG_DELETE_USAGE \
cbe48529
ÆAB
17 N_("git reflog delete [--rewrite] [--updateref]\n" \
18 " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
1e91d3fa
ÆAB
19
20#define BUILTIN_REFLOG_EXISTS_USAGE \
21 N_("git reflog exists <ref>")
22
fbc15b13
ÆAB
23static const char *const reflog_show_usage[] = {
24 BUILTIN_REFLOG_SHOW_USAGE,
25 NULL,
26};
27
1e91d3fa
ÆAB
28static const char *const reflog_expire_usage[] = {
29 BUILTIN_REFLOG_EXPIRE_USAGE,
30 NULL
31};
32
33static const char *const reflog_delete_usage[] = {
34 BUILTIN_REFLOG_DELETE_USAGE,
35 NULL
36};
37
a34393f5
ÆAB
38static const char *const reflog_exists_usage[] = {
39 BUILTIN_REFLOG_EXISTS_USAGE,
40 NULL,
41};
4264dc15 42
e3c36758 43static const char *const reflog_usage[] = {
fbc15b13 44 BUILTIN_REFLOG_SHOW_USAGE,
e3c36758
ÆAB
45 BUILTIN_REFLOG_EXPIRE_USAGE,
46 BUILTIN_REFLOG_DELETE_USAGE,
47 BUILTIN_REFLOG_EXISTS_USAGE,
48 NULL
49};
50
dddbad72
JS
51static timestamp_t default_reflog_expire;
52static timestamp_t default_reflog_expire_unreachable;
4aec56d1 53
f2919bae
ÆAB
54struct worktree_reflogs {
55 struct worktree *worktree;
56 struct string_list reflogs;
bda3a31c
JH
57};
58
5bcad1bc 59static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
bda3a31c 60{
f2919bae
ÆAB
61 struct worktree_reflogs *cb = cb_data;
62 struct worktree *worktree = cb->worktree;
c9ef0d95
NTND
63 struct strbuf newref = STRBUF_INIT;
64
65 /*
66 * Avoid collecting the same shared ref multiple times because
67 * they are available via all worktrees.
68 */
f2919bae 69 if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
c9ef0d95
NTND
70 return 0;
71
f2919bae
ÆAB
72 strbuf_worktree_ref(worktree, &newref, ref);
73 string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
bda3a31c 74
bda3a31c
JH
75 return 0;
76}
77
3cb22b8e
JH
78static struct reflog_expire_cfg {
79 struct reflog_expire_cfg *next;
dddbad72
JS
80 timestamp_t expire_total;
81 timestamp_t expire_unreachable;
3cb22b8e
JH
82 char pattern[FLEX_ARRAY];
83} *reflog_expire_cfg, **reflog_expire_cfg_tail;
84
85static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
4aec56d1 86{
3cb22b8e
JH
87 struct reflog_expire_cfg *ent;
88
89 if (!reflog_expire_cfg_tail)
90 reflog_expire_cfg_tail = &reflog_expire_cfg;
91
92 for (ent = reflog_expire_cfg; ent; ent = ent->next)
c3a700fb
JK
93 if (!strncmp(ent->pattern, pattern, len) &&
94 ent->pattern[len] == '\0')
3cb22b8e
JH
95 return ent;
96
96ffc06f 97 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
3cb22b8e
JH
98 *reflog_expire_cfg_tail = ent;
99 reflog_expire_cfg_tail = &(ent->next);
100 return ent;
101}
102
3cb22b8e
JH
103/* expiry timer slot */
104#define EXPIRE_TOTAL 01
105#define EXPIRE_UNREACH 02
106
107static int reflog_expire_config(const char *var, const char *value, void *cb)
108{
b3873c33 109 const char *pattern, *key;
f5914f4b 110 size_t pattern_len;
dddbad72 111 timestamp_t expire;
3cb22b8e
JH
112 int slot;
113 struct reflog_expire_cfg *ent;
114
b3873c33 115 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
3cb22b8e
JH
116 return git_default_config(var, value, cb);
117
b3873c33 118 if (!strcmp(key, "reflogexpire")) {
3cb22b8e 119 slot = EXPIRE_TOTAL;
5f967424 120 if (git_config_expiry_date(&expire, var, value))
3cb22b8e 121 return -1;
b3873c33 122 } else if (!strcmp(key, "reflogexpireunreachable")) {
3cb22b8e 123 slot = EXPIRE_UNREACH;
5f967424 124 if (git_config_expiry_date(&expire, var, value))
3cb22b8e
JH
125 return -1;
126 } else
127 return git_default_config(var, value, cb);
128
b3873c33 129 if (!pattern) {
3cb22b8e
JH
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;
137 }
4f342b96
JH
138 return 0;
139 }
3cb22b8e 140
b3873c33 141 ent = find_cfg_ent(pattern, pattern_len);
3cb22b8e
JH
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;
151 }
152 return 0;
153}
154
33d7bdd6 155static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, const char *ref)
3cb22b8e
JH
156{
157 struct reflog_expire_cfg *ent;
158
33d7bdd6 159 if (cb->explicit_expiry == (EXPIRE_TOTAL|EXPIRE_UNREACH))
3cb22b8e
JH
160 return; /* both given explicitly -- nothing to tweak */
161
162 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
55d34269 163 if (!wildmatch(ent->pattern, ref, 0)) {
33d7bdd6 164 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
3cb22b8e 165 cb->expire_total = ent->expire_total;
33d7bdd6 166 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
3cb22b8e
JH
167 cb->expire_unreachable = ent->expire_unreachable;
168 return;
169 }
170 }
171
60bce2bb
JH
172 /*
173 * If unconfigured, make stash never expire
174 */
175 if (!strcmp(ref, "refs/stash")) {
33d7bdd6 176 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
60bce2bb 177 cb->expire_total = 0;
33d7bdd6 178 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
60bce2bb
JH
179 cb->expire_unreachable = 0;
180 return;
181 }
182
3cb22b8e 183 /* Nothing matched -- use the default value */
33d7bdd6 184 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
3cb22b8e 185 cb->expire_total = default_reflog_expire;
33d7bdd6 186 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
3cb22b8e 187 cb->expire_unreachable = default_reflog_expire_unreachable;
4aec56d1
JH
188}
189
33d7bdd6
JC
190static int expire_unreachable_callback(const struct option *opt,
191 const char *arg,
192 int unset)
193{
194 struct cmd_reflog_expire_cb *cmd = opt->value;
195
196 if (parse_expiry_date(arg, &cmd->expire_unreachable))
197 die(_("invalid timestamp '%s' given to '--%s'"),
198 arg, opt->long_name);
199
200 cmd->explicit_expiry |= EXPIRE_UNREACH;
201 return 0;
202}
203
204static int expire_total_callback(const struct option *opt,
205 const char *arg,
206 int unset)
207{
208 struct cmd_reflog_expire_cb *cmd = opt->value;
209
210 if (parse_expiry_date(arg, &cmd->expire_total))
211 die(_("invalid timestamp '%s' given to '--%s'"),
212 arg, opt->long_name);
213
214 cmd->explicit_expiry |= EXPIRE_TOTAL;
215 return 0;
216}
217
fbc15b13
ÆAB
218static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
219{
220 struct option options[] = {
221 OPT_END()
222 };
223
224 parse_options(argc, argv, prefix, options, reflog_show_usage,
225 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
226 PARSE_OPT_KEEP_UNKNOWN);
227
840344db 228 return cmd_log_reflog(argc, argv, prefix);
fbc15b13
ÆAB
229}
230
4264dc15
JH
231static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
232{
46fbe418 233 struct cmd_reflog_expire_cb cmd = { 0 };
dddbad72 234 timestamp_t now = time(NULL);
c9ef0d95 235 int i, status, do_all, all_worktrees = 1;
aba56c89 236 unsigned int flags = 0;
fcd2c3d9
ÆAB
237 int verbose = 0;
238 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
33d7bdd6
JC
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),
9e1f22c8 248 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
33d7bdd6
JC
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,
9e1f22c8 261 N_("limits processing to reflogs from the current worktree only")),
33d7bdd6
JC
262 OPT_END()
263 };
4264dc15 264
4a9f4394
AS
265 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
266 default_reflog_expire = now - 90 * 24 * 3600;
ef90d6d4 267 git_config(reflog_expire_config, NULL);
4aec56d1 268
4264dc15
JH
269 save_commit_buffer = 0;
270 do_all = status = 0;
4aec56d1 271
33d7bdd6 272 cmd.explicit_expiry = 0;
46fbe418
ÆAB
273 cmd.expire_total = default_reflog_expire;
274 cmd.expire_unreachable = default_reflog_expire_unreachable;
4264dc15 275
33d7bdd6 276 argc = parse_options(argc, argv, prefix, options, reflog_expire_usage, 0);
3cb22b8e 277
fcd2c3d9
ÆAB
278 if (verbose)
279 should_prune_fn = should_expire_reflog_ent_verbose;
280
3cb22b8e
JH
281 /*
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.
285 */
46fbe418 286 if (cmd.stalefix) {
994b328f
ÆAB
287 struct rev_info revs;
288
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;
fcd2c3d9 293 if (verbose)
dd509db3 294 printf(_("Marking reachable objects..."));
994b328f 295 mark_reachable_objects(&revs, 0, 0, NULL);
fcd2c3d9 296 if (verbose)
1389d9dd
JH
297 putchar('\n');
298 }
299
bda3a31c 300 if (do_all) {
f2919bae
ÆAB
301 struct worktree_reflogs collected = {
302 .reflogs = STRING_LIST_INIT_DUP,
303 };
304 struct string_list_item *item;
c9ef0d95 305 struct worktree **worktrees, **p;
bda3a31c 306
03f2465b 307 worktrees = get_worktrees();
c9ef0d95
NTND
308 for (p = worktrees; *p; p++) {
309 if (!all_worktrees && !(*p)->is_current)
310 continue;
f2919bae 311 collected.worktree = *p;
c9ef0d95
NTND
312 refs_for_each_reflog(get_worktree_ref_store(*p),
313 collect_reflog, &collected);
314 }
315 free_worktrees(worktrees);
f2919bae
ÆAB
316
317 for_each_string_list_item(item, &collected.reflogs) {
fcd2c3d9
ÆAB
318 struct expire_reflog_policy_cb cb = {
319 .cmd = cmd,
320 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
321 };
ae35e16c 322
33d7bdd6 323 set_reflog_expiry_param(&cb.cmd, item->string);
f2919bae 324 status |= reflog_expire(item->string, flags,
fa5b1830 325 reflog_expiry_prepare,
fcd2c3d9 326 should_prune_fn,
fa5b1830
MH
327 reflog_expiry_cleanup,
328 &cb);
bda3a31c 329 }
f2919bae 330 string_list_clear(&collected.reflogs, 0);
bda3a31c
JH
331 }
332
33d7bdd6 333 for (i = 0; i < argc; i++) {
90fb46ec 334 char *ref;
46fbe418
ÆAB
335 struct expire_reflog_policy_cb cb = { .cmd = cmd };
336
ae35e16c 337 if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
dd509db3 338 status |= error(_("%s points nowhere!"), argv[i]);
4264dc15
JH
339 continue;
340 }
33d7bdd6 341 set_reflog_expiry_param(&cb.cmd, ref);
cc40b5ce 342 status |= reflog_expire(ref, flags,
fa5b1830 343 reflog_expiry_prepare,
fcd2c3d9 344 should_prune_fn,
fa5b1830
MH
345 reflog_expiry_cleanup,
346 &cb);
3c815049 347 free(ref);
4264dc15
JH
348 }
349 return status;
350}
351
552cecc2
JS
352static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
353{
552cecc2 354 int i, status = 0;
aba56c89 355 unsigned int flags = 0;
fcd2c3d9 356 int verbose = 0;
7d3d226e 357
33d7bdd6
JC
358 const struct option options[] = {
359 OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
360 EXPIRE_REFLOGS_DRY_RUN),
361 OPT_BIT(0, "rewrite", &flags,
362 N_("rewrite the old SHA1 with the new SHA1 of the entry that now precedes it"),
363 EXPIRE_REFLOGS_REWRITE),
364 OPT_BIT(0, "updateref", &flags,
365 N_("update the reference to the value of the top reflog entry"),
366 EXPIRE_REFLOGS_UPDATE_REF),
9e1f22c8 367 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
33d7bdd6
JC
368 OPT_END()
369 };
370
371 argc = parse_options(argc, argv, prefix, options, reflog_delete_usage, 0);
3c386aa3 372
33d7bdd6 373 if (argc < 1)
dd509db3 374 return error(_("no reflog specified to delete"));
3c386aa3 375
7d3d226e
JC
376 for (i = 0; i < argc; i++)
377 status |= reflog_delete(argv[i], flags, verbose);
552cecc2 378
552cecc2
JS
379 return status;
380}
381
afcb2e7a
DT
382static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
383{
a34393f5
ÆAB
384 struct option options[] = {
385 OPT_END()
386 };
387 const char *refname;
afcb2e7a 388
a34393f5
ÆAB
389 argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
390 0);
391 if (!argc)
392 usage_with_options(reflog_exists_usage, options);
afcb2e7a 393
a34393f5
ÆAB
394 refname = argv[0];
395 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
396 die(_("invalid ref format: %s"), refname);
397 return !reflog_exists(refname);
afcb2e7a
DT
398}
399
1389d9dd
JH
400/*
401 * main "reflog"
402 */
403
4264dc15
JH
404int cmd_reflog(int argc, const char **argv, const char *prefix)
405{
e3c36758
ÆAB
406 struct option options[] = {
407 OPT_END()
408 };
409
410 argc = parse_options(argc, argv, prefix, options, reflog_usage,
411 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
412 PARSE_OPT_KEEP_UNKNOWN |
413 PARSE_OPT_NO_INTERNAL_HELP);
414
415 /*
416 * With "git reflog" we default to showing it. !argc is
417 * impossible with PARSE_OPT_KEEP_ARGV0.
418 */
419 if (argc == 1)
420 goto log_reflog;
99caeed0 421
e3c36758
ÆAB
422 if (!strcmp(argv[1], "-h"))
423 usage_with_options(reflog_usage, options);
424 else if (*argv[1] == '-')
425 goto log_reflog;
cf39f54e
LT
426
427 if (!strcmp(argv[1], "show"))
840344db 428 return cmd_reflog_show(argc - 1, argv + 1, prefix);
5f9b64a6 429 else if (!strcmp(argv[1], "expire"))
4264dc15 430 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
5f9b64a6 431 else if (!strcmp(argv[1], "delete"))
552cecc2 432 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
5f9b64a6 433 else if (!strcmp(argv[1], "exists"))
afcb2e7a
DT
434 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
435
e3c36758
ÆAB
436 /*
437 * Fall-through for e.g. "git reflog -1", "git reflog master",
438 * as well as the plain "git reflog" above goto above.
439 */
440log_reflog:
bf01d4a3 441 return cmd_log_reflog(argc, argv, prefix);
4264dc15 442}