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