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