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