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