]>
Commit | Line | Data |
---|---|---|
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 |
23 | static const char *const reflog_show_usage[] = { |
24 | BUILTIN_REFLOG_SHOW_USAGE, | |
25 | NULL, | |
26 | }; | |
27 | ||
1e91d3fa ÆAB |
28 | static const char *const reflog_expire_usage[] = { |
29 | BUILTIN_REFLOG_EXPIRE_USAGE, | |
30 | NULL | |
31 | }; | |
32 | ||
33 | static const char *const reflog_delete_usage[] = { | |
34 | BUILTIN_REFLOG_DELETE_USAGE, | |
35 | NULL | |
36 | }; | |
37 | ||
a34393f5 ÆAB |
38 | static const char *const reflog_exists_usage[] = { |
39 | BUILTIN_REFLOG_EXISTS_USAGE, | |
40 | NULL, | |
41 | }; | |
4264dc15 | 42 | |
e3c36758 | 43 | static 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 |
51 | static timestamp_t default_reflog_expire; |
52 | static timestamp_t default_reflog_expire_unreachable; | |
4aec56d1 | 53 | |
f2919bae ÆAB |
54 | struct worktree_reflogs { |
55 | struct worktree *worktree; | |
56 | struct string_list reflogs; | |
bda3a31c JH |
57 | }; |
58 | ||
5bcad1bc | 59 | static 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 |
78 | static 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 | ||
85 | static 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 | ||
107 | static 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 | 155 | static 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 |
190 | static 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 | ||
204 | static 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 |
218 | static 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 | | |
99d86d60 | 226 | PARSE_OPT_KEEP_UNKNOWN_OPT); |
fbc15b13 | 227 | |
840344db | 228 | return cmd_log_reflog(argc, argv, prefix); |
fbc15b13 ÆAB |
229 | } |
230 | ||
4264dc15 JH |
231 | static 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); |
2108fe4a | 296 | release_revisions(&revs); |
fcd2c3d9 | 297 | if (verbose) |
1389d9dd JH |
298 | putchar('\n'); |
299 | } | |
300 | ||
bda3a31c | 301 | if (do_all) { |
f2919bae ÆAB |
302 | struct worktree_reflogs collected = { |
303 | .reflogs = STRING_LIST_INIT_DUP, | |
304 | }; | |
305 | struct string_list_item *item; | |
c9ef0d95 | 306 | struct worktree **worktrees, **p; |
bda3a31c | 307 | |
03f2465b | 308 | worktrees = get_worktrees(); |
c9ef0d95 NTND |
309 | for (p = worktrees; *p; p++) { |
310 | if (!all_worktrees && !(*p)->is_current) | |
311 | continue; | |
f2919bae | 312 | collected.worktree = *p; |
c9ef0d95 NTND |
313 | refs_for_each_reflog(get_worktree_ref_store(*p), |
314 | collect_reflog, &collected); | |
315 | } | |
316 | free_worktrees(worktrees); | |
f2919bae ÆAB |
317 | |
318 | for_each_string_list_item(item, &collected.reflogs) { | |
fcd2c3d9 ÆAB |
319 | struct expire_reflog_policy_cb cb = { |
320 | .cmd = cmd, | |
321 | .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN), | |
322 | }; | |
ae35e16c | 323 | |
33d7bdd6 | 324 | set_reflog_expiry_param(&cb.cmd, item->string); |
f2919bae | 325 | status |= reflog_expire(item->string, flags, |
fa5b1830 | 326 | reflog_expiry_prepare, |
fcd2c3d9 | 327 | should_prune_fn, |
fa5b1830 MH |
328 | reflog_expiry_cleanup, |
329 | &cb); | |
bda3a31c | 330 | } |
f2919bae | 331 | string_list_clear(&collected.reflogs, 0); |
bda3a31c JH |
332 | } |
333 | ||
33d7bdd6 | 334 | for (i = 0; i < argc; i++) { |
90fb46ec | 335 | char *ref; |
46fbe418 ÆAB |
336 | struct expire_reflog_policy_cb cb = { .cmd = cmd }; |
337 | ||
ae35e16c | 338 | if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) { |
dd509db3 | 339 | status |= error(_("%s points nowhere!"), argv[i]); |
4264dc15 JH |
340 | continue; |
341 | } | |
33d7bdd6 | 342 | set_reflog_expiry_param(&cb.cmd, ref); |
cc40b5ce | 343 | status |= reflog_expire(ref, flags, |
fa5b1830 | 344 | reflog_expiry_prepare, |
fcd2c3d9 | 345 | should_prune_fn, |
fa5b1830 MH |
346 | reflog_expiry_cleanup, |
347 | &cb); | |
3c815049 | 348 | free(ref); |
4264dc15 JH |
349 | } |
350 | return status; | |
351 | } | |
352 | ||
552cecc2 JS |
353 | static int cmd_reflog_delete(int argc, const char **argv, const char *prefix) |
354 | { | |
552cecc2 | 355 | int i, status = 0; |
aba56c89 | 356 | unsigned int flags = 0; |
fcd2c3d9 | 357 | int verbose = 0; |
7d3d226e | 358 | |
33d7bdd6 JC |
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), | |
9e1f22c8 | 368 | OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")), |
33d7bdd6 JC |
369 | OPT_END() |
370 | }; | |
371 | ||
372 | argc = parse_options(argc, argv, prefix, options, reflog_delete_usage, 0); | |
3c386aa3 | 373 | |
33d7bdd6 | 374 | if (argc < 1) |
dd509db3 | 375 | return error(_("no reflog specified to delete")); |
3c386aa3 | 376 | |
7d3d226e JC |
377 | for (i = 0; i < argc; i++) |
378 | status |= reflog_delete(argv[i], flags, verbose); | |
552cecc2 | 379 | |
552cecc2 JS |
380 | return status; |
381 | } | |
382 | ||
afcb2e7a DT |
383 | static int cmd_reflog_exists(int argc, const char **argv, const char *prefix) |
384 | { | |
a34393f5 ÆAB |
385 | struct option options[] = { |
386 | OPT_END() | |
387 | }; | |
388 | const char *refname; | |
afcb2e7a | 389 | |
a34393f5 ÆAB |
390 | argc = parse_options(argc, argv, prefix, options, reflog_exists_usage, |
391 | 0); | |
392 | if (!argc) | |
393 | usage_with_options(reflog_exists_usage, options); | |
afcb2e7a | 394 | |
a34393f5 ÆAB |
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); | |
afcb2e7a DT |
399 | } |
400 | ||
1389d9dd JH |
401 | /* |
402 | * main "reflog" | |
403 | */ | |
404 | ||
4264dc15 JH |
405 | int cmd_reflog(int argc, const char **argv, const char *prefix) |
406 | { | |
e3c36758 ÆAB |
407 | struct option options[] = { |
408 | OPT_END() | |
409 | }; | |
410 | ||
411 | argc = parse_options(argc, argv, prefix, options, reflog_usage, | |
412 | PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 | | |
99d86d60 | 413 | PARSE_OPT_KEEP_UNKNOWN_OPT | |
e3c36758 ÆAB |
414 | PARSE_OPT_NO_INTERNAL_HELP); |
415 | ||
416 | /* | |
417 | * With "git reflog" we default to showing it. !argc is | |
418 | * impossible with PARSE_OPT_KEEP_ARGV0. | |
419 | */ | |
420 | if (argc == 1) | |
421 | goto log_reflog; | |
99caeed0 | 422 | |
e3c36758 ÆAB |
423 | if (!strcmp(argv[1], "-h")) |
424 | usage_with_options(reflog_usage, options); | |
425 | else if (*argv[1] == '-') | |
426 | goto log_reflog; | |
cf39f54e LT |
427 | |
428 | if (!strcmp(argv[1], "show")) | |
840344db | 429 | return cmd_reflog_show(argc - 1, argv + 1, prefix); |
5f9b64a6 | 430 | else if (!strcmp(argv[1], "expire")) |
4264dc15 | 431 | return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
5f9b64a6 | 432 | else if (!strcmp(argv[1], "delete")) |
552cecc2 | 433 | return cmd_reflog_delete(argc - 1, argv + 1, prefix); |
5f9b64a6 | 434 | else if (!strcmp(argv[1], "exists")) |
afcb2e7a DT |
435 | return cmd_reflog_exists(argc - 1, argv + 1, prefix); |
436 | ||
e3c36758 ÆAB |
437 | /* |
438 | * Fall-through for e.g. "git reflog -1", "git reflog master", | |
439 | * as well as the plain "git reflog" above goto above. | |
440 | */ | |
441 | log_reflog: | |
bf01d4a3 | 442 | return cmd_log_reflog(argc, argv, prefix); |
4264dc15 | 443 | } |