]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/reflog.c
revision: rename bit to `do_not_die_on_missing_objects`
[thirdparty/git.git] / builtin / reflog.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "repository.h"
5 #include "revision.h"
6 #include "reachable.h"
7 #include "wildmatch.h"
8 #include "worktree.h"
9 #include "reflog.h"
10 #include "parse-options.h"
11
12 #define BUILTIN_REFLOG_SHOW_USAGE \
13 N_("git reflog [show] [<log-options>] [<ref>]")
14
15 #define BUILTIN_REFLOG_EXPIRE_USAGE \
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>...]")
19
20 #define BUILTIN_REFLOG_DELETE_USAGE \
21 N_("git reflog delete [--rewrite] [--updateref]\n" \
22 " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
23
24 #define BUILTIN_REFLOG_EXISTS_USAGE \
25 N_("git reflog exists <ref>")
26
27 static const char *const reflog_show_usage[] = {
28 BUILTIN_REFLOG_SHOW_USAGE,
29 NULL,
30 };
31
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
42 static const char *const reflog_exists_usage[] = {
43 BUILTIN_REFLOG_EXISTS_USAGE,
44 NULL,
45 };
46
47 static const char *const reflog_usage[] = {
48 BUILTIN_REFLOG_SHOW_USAGE,
49 BUILTIN_REFLOG_EXPIRE_USAGE,
50 BUILTIN_REFLOG_DELETE_USAGE,
51 BUILTIN_REFLOG_EXISTS_USAGE,
52 NULL
53 };
54
55 static timestamp_t default_reflog_expire;
56 static timestamp_t default_reflog_expire_unreachable;
57
58 struct worktree_reflogs {
59 struct worktree *worktree;
60 struct string_list reflogs;
61 };
62
63 static int collect_reflog(const char *ref, const struct object_id *oid UNUSED,
64 int flags UNUSED, void *cb_data)
65 {
66 struct worktree_reflogs *cb = cb_data;
67 struct worktree *worktree = cb->worktree;
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 */
74 if (!worktree->is_current &&
75 parse_worktree_ref(ref, NULL, NULL, NULL) == REF_WORKTREE_SHARED)
76 return 0;
77
78 strbuf_worktree_ref(worktree, &newref, ref);
79 string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
80
81 return 0;
82 }
83
84 static struct reflog_expire_cfg {
85 struct reflog_expire_cfg *next;
86 timestamp_t expire_total;
87 timestamp_t expire_unreachable;
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)
92 {
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)
99 if (!strncmp(ent->pattern, pattern, len) &&
100 ent->pattern[len] == '\0')
101 return ent;
102
103 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
104 *reflog_expire_cfg_tail = ent;
105 reflog_expire_cfg_tail = &(ent->next);
106 return ent;
107 }
108
109 /* expiry timer slot */
110 #define EXPIRE_TOTAL 01
111 #define EXPIRE_UNREACH 02
112
113 static int reflog_expire_config(const char *var, const char *value,
114 const struct config_context *ctx, void *cb)
115 {
116 const char *pattern, *key;
117 size_t pattern_len;
118 timestamp_t expire;
119 int slot;
120 struct reflog_expire_cfg *ent;
121
122 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
123 return git_default_config(var, value, ctx, cb);
124
125 if (!strcmp(key, "reflogexpire")) {
126 slot = EXPIRE_TOTAL;
127 if (git_config_expiry_date(&expire, var, value))
128 return -1;
129 } else if (!strcmp(key, "reflogexpireunreachable")) {
130 slot = EXPIRE_UNREACH;
131 if (git_config_expiry_date(&expire, var, value))
132 return -1;
133 } else
134 return git_default_config(var, value, ctx, cb);
135
136 if (!pattern) {
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 }
145 return 0;
146 }
147
148 ent = find_cfg_ent(pattern, pattern_len);
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
162 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, const char *ref)
163 {
164 struct reflog_expire_cfg *ent;
165
166 if (cb->explicit_expiry == (EXPIRE_TOTAL|EXPIRE_UNREACH))
167 return; /* both given explicitly -- nothing to tweak */
168
169 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
170 if (!wildmatch(ent->pattern, ref, 0)) {
171 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
172 cb->expire_total = ent->expire_total;
173 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
174 cb->expire_unreachable = ent->expire_unreachable;
175 return;
176 }
177 }
178
179 /*
180 * If unconfigured, make stash never expire
181 */
182 if (!strcmp(ref, "refs/stash")) {
183 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
184 cb->expire_total = 0;
185 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
186 cb->expire_unreachable = 0;
187 return;
188 }
189
190 /* Nothing matched -- use the default value */
191 if (!(cb->explicit_expiry & EXPIRE_TOTAL))
192 cb->expire_total = default_reflog_expire;
193 if (!(cb->explicit_expiry & EXPIRE_UNREACH))
194 cb->expire_unreachable = default_reflog_expire_unreachable;
195 }
196
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
203 BUG_ON_OPT_NEG(unset);
204
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
219 BUG_ON_OPT_NEG(unset);
220
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
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 |
237 PARSE_OPT_KEEP_UNKNOWN_OPT);
238
239 return cmd_log_reflog(argc, argv, prefix);
240 }
241
242 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
243 {
244 struct cmd_reflog_expire_cb cmd = { 0 };
245 timestamp_t now = time(NULL);
246 int i, status, do_all, all_worktrees = 1;
247 unsigned int flags = 0;
248 int verbose = 0;
249 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
250 const struct option options[] = {
251 OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
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),
259 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
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")),
271 OPT_BOOL(1, "single-worktree", &all_worktrees,
272 N_("limits processing to reflogs from the current worktree only")),
273 OPT_END()
274 };
275
276 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
277 default_reflog_expire = now - 90 * 24 * 3600;
278 git_config(reflog_expire_config, NULL);
279
280 save_commit_buffer = 0;
281 do_all = status = 0;
282
283 cmd.explicit_expiry = 0;
284 cmd.expire_total = default_reflog_expire;
285 cmd.expire_unreachable = default_reflog_expire_unreachable;
286
287 argc = parse_options(argc, argv, prefix, options, reflog_expire_usage, 0);
288
289 if (verbose)
290 should_prune_fn = should_expire_reflog_ent_verbose;
291
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 */
297 if (cmd.stalefix) {
298 struct rev_info revs;
299
300 repo_init_revisions(the_repository, &revs, prefix);
301 revs.do_not_die_on_missing_objects = 1;
302 revs.ignore_missing = 1;
303 revs.ignore_missing_links = 1;
304 if (verbose)
305 printf(_("Marking reachable objects..."));
306 mark_reachable_objects(&revs, 0, 0, NULL);
307 release_revisions(&revs);
308 if (verbose)
309 putchar('\n');
310 }
311
312 if (do_all) {
313 struct worktree_reflogs collected = {
314 .reflogs = STRING_LIST_INIT_DUP,
315 };
316 struct string_list_item *item;
317 struct worktree **worktrees, **p;
318
319 worktrees = get_worktrees();
320 for (p = worktrees; *p; p++) {
321 if (!all_worktrees && !(*p)->is_current)
322 continue;
323 collected.worktree = *p;
324 refs_for_each_reflog(get_worktree_ref_store(*p),
325 collect_reflog, &collected);
326 }
327 free_worktrees(worktrees);
328
329 for_each_string_list_item(item, &collected.reflogs) {
330 struct expire_reflog_policy_cb cb = {
331 .cmd = cmd,
332 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
333 };
334
335 set_reflog_expiry_param(&cb.cmd, item->string);
336 status |= reflog_expire(item->string, flags,
337 reflog_expiry_prepare,
338 should_prune_fn,
339 reflog_expiry_cleanup,
340 &cb);
341 }
342 string_list_clear(&collected.reflogs, 0);
343 }
344
345 for (i = 0; i < argc; i++) {
346 char *ref;
347 struct expire_reflog_policy_cb cb = { .cmd = cmd };
348
349 if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
350 status |= error(_("%s points nowhere!"), argv[i]);
351 continue;
352 }
353 set_reflog_expiry_param(&cb.cmd, ref);
354 status |= reflog_expire(ref, flags,
355 reflog_expiry_prepare,
356 should_prune_fn,
357 reflog_expiry_cleanup,
358 &cb);
359 free(ref);
360 }
361 return status;
362 }
363
364 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
365 {
366 int i, status = 0;
367 unsigned int flags = 0;
368 int verbose = 0;
369
370 const struct option options[] = {
371 OPT_BIT(0, "dry-run", &flags, N_("do not actually prune any entries"),
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),
379 OPT_BOOL(0, "verbose", &verbose, N_("print extra information on screen")),
380 OPT_END()
381 };
382
383 argc = parse_options(argc, argv, prefix, options, reflog_delete_usage, 0);
384
385 if (argc < 1)
386 return error(_("no reflog specified to delete"));
387
388 for (i = 0; i < argc; i++)
389 status |= reflog_delete(argv[i], flags, verbose);
390
391 return status;
392 }
393
394 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
395 {
396 struct option options[] = {
397 OPT_END()
398 };
399 const char *refname;
400
401 argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
402 0);
403 if (!argc)
404 usage_with_options(reflog_exists_usage, options);
405
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);
410 }
411
412 /*
413 * main "reflog"
414 */
415
416 int cmd_reflog(int argc, const char **argv, const char *prefix)
417 {
418 parse_opt_subcommand_fn *fn = NULL;
419 struct option options[] = {
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),
424 OPT_END()
425 };
426
427 argc = parse_options(argc, argv, prefix, options, reflog_usage,
428 PARSE_OPT_SUBCOMMAND_OPTIONAL |
429 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
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);
435 }