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