8 #define BUILTIN_REFLOG_SHOW_USAGE \
9 N_("git reflog [show] [<log-options>] [<ref>]")
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>...]")
16 #define BUILTIN_REFLOG_DELETE_USAGE \
17 N_("git reflog delete [--rewrite] [--updateref]\n" \
18 " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
20 #define BUILTIN_REFLOG_EXISTS_USAGE \
21 N_("git reflog exists <ref>")
23 static const char *const reflog_show_usage
[] = {
24 BUILTIN_REFLOG_SHOW_USAGE
,
28 static const char *const reflog_expire_usage
[] = {
29 BUILTIN_REFLOG_EXPIRE_USAGE
,
33 static const char *const reflog_delete_usage
[] = {
34 BUILTIN_REFLOG_DELETE_USAGE
,
38 static const char *const reflog_exists_usage
[] = {
39 BUILTIN_REFLOG_EXISTS_USAGE
,
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
,
51 static timestamp_t default_reflog_expire
;
52 static timestamp_t default_reflog_expire_unreachable
;
54 struct worktree_reflogs
{
55 struct worktree
*worktree
;
56 struct string_list reflogs
;
59 static int collect_reflog(const char *ref
, const struct object_id
*oid UNUSED
,
60 int flags UNUSED
, void *cb_data
)
62 struct worktree_reflogs
*cb
= cb_data
;
63 struct worktree
*worktree
= cb
->worktree
;
64 struct strbuf newref
= STRBUF_INIT
;
67 * Avoid collecting the same shared ref multiple times because
68 * they are available via all worktrees.
70 if (!worktree
->is_current
&&
71 parse_worktree_ref(ref
, NULL
, NULL
, NULL
) == REF_WORKTREE_SHARED
)
74 strbuf_worktree_ref(worktree
, &newref
, ref
);
75 string_list_append_nodup(&cb
->reflogs
, strbuf_detach(&newref
, NULL
));
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
;
87 static struct reflog_expire_cfg
*find_cfg_ent(const char *pattern
, size_t len
)
89 struct reflog_expire_cfg
*ent
;
91 if (!reflog_expire_cfg_tail
)
92 reflog_expire_cfg_tail
= &reflog_expire_cfg
;
94 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
)
95 if (!strncmp(ent
->pattern
, pattern
, len
) &&
96 ent
->pattern
[len
] == '\0')
99 FLEX_ALLOC_MEM(ent
, pattern
, pattern
, len
);
100 *reflog_expire_cfg_tail
= ent
;
101 reflog_expire_cfg_tail
= &(ent
->next
);
105 /* expiry timer slot */
106 #define EXPIRE_TOTAL 01
107 #define EXPIRE_UNREACH 02
109 static int reflog_expire_config(const char *var
, const char *value
, void *cb
)
111 const char *pattern
, *key
;
115 struct reflog_expire_cfg
*ent
;
117 if (parse_config_key(var
, "gc", &pattern
, &pattern_len
, &key
) < 0)
118 return git_default_config(var
, value
, cb
);
120 if (!strcmp(key
, "reflogexpire")) {
122 if (git_config_expiry_date(&expire
, var
, value
))
124 } else if (!strcmp(key
, "reflogexpireunreachable")) {
125 slot
= EXPIRE_UNREACH
;
126 if (git_config_expiry_date(&expire
, var
, value
))
129 return git_default_config(var
, value
, cb
);
134 default_reflog_expire
= expire
;
137 default_reflog_expire_unreachable
= expire
;
143 ent
= find_cfg_ent(pattern
, pattern_len
);
148 ent
->expire_total
= expire
;
151 ent
->expire_unreachable
= expire
;
157 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb
*cb
, const char *ref
)
159 struct reflog_expire_cfg
*ent
;
161 if (cb
->explicit_expiry
== (EXPIRE_TOTAL
|EXPIRE_UNREACH
))
162 return; /* both given explicitly -- nothing to tweak */
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
;
175 * If unconfigured, make stash never expire
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;
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
;
192 static int expire_unreachable_callback(const struct option
*opt
,
196 struct cmd_reflog_expire_cb
*cmd
= opt
->value
;
198 BUG_ON_OPT_NEG(unset
);
200 if (parse_expiry_date(arg
, &cmd
->expire_unreachable
))
201 die(_("invalid timestamp '%s' given to '--%s'"),
202 arg
, opt
->long_name
);
204 cmd
->explicit_expiry
|= EXPIRE_UNREACH
;
208 static int expire_total_callback(const struct option
*opt
,
212 struct cmd_reflog_expire_cb
*cmd
= opt
->value
;
214 BUG_ON_OPT_NEG(unset
);
216 if (parse_expiry_date(arg
, &cmd
->expire_total
))
217 die(_("invalid timestamp '%s' given to '--%s'"),
218 arg
, opt
->long_name
);
220 cmd
->explicit_expiry
|= EXPIRE_TOTAL
;
224 static int cmd_reflog_show(int argc
, const char **argv
, const char *prefix
)
226 struct option options
[] = {
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
);
234 return cmd_log_reflog(argc
, argv
, prefix
);
237 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
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;
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"),
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"),
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")),
271 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
272 default_reflog_expire
= now
- 90 * 24 * 3600;
273 git_config(reflog_expire_config
, NULL
);
275 save_commit_buffer
= 0;
278 cmd
.explicit_expiry
= 0;
279 cmd
.expire_total
= default_reflog_expire
;
280 cmd
.expire_unreachable
= default_reflog_expire_unreachable
;
282 argc
= parse_options(argc
, argv
, prefix
, options
, reflog_expire_usage
, 0);
285 should_prune_fn
= should_expire_reflog_ent_verbose
;
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.
293 struct rev_info revs
;
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;
300 printf(_("Marking reachable objects..."));
301 mark_reachable_objects(&revs
, 0, 0, NULL
);
302 release_revisions(&revs
);
308 struct worktree_reflogs collected
= {
309 .reflogs
= STRING_LIST_INIT_DUP
,
311 struct string_list_item
*item
;
312 struct worktree
**worktrees
, **p
;
314 worktrees
= get_worktrees();
315 for (p
= worktrees
; *p
; p
++) {
316 if (!all_worktrees
&& !(*p
)->is_current
)
318 collected
.worktree
= *p
;
319 refs_for_each_reflog(get_worktree_ref_store(*p
),
320 collect_reflog
, &collected
);
322 free_worktrees(worktrees
);
324 for_each_string_list_item(item
, &collected
.reflogs
) {
325 struct expire_reflog_policy_cb cb
= {
327 .dry_run
= !!(flags
& EXPIRE_REFLOGS_DRY_RUN
),
330 set_reflog_expiry_param(&cb
.cmd
, item
->string
);
331 status
|= reflog_expire(item
->string
, flags
,
332 reflog_expiry_prepare
,
334 reflog_expiry_cleanup
,
337 string_list_clear(&collected
.reflogs
, 0);
340 for (i
= 0; i
< argc
; i
++) {
342 struct expire_reflog_policy_cb cb
= { .cmd
= cmd
};
344 if (!dwim_log(argv
[i
], strlen(argv
[i
]), NULL
, &ref
)) {
345 status
|= error(_("%s points nowhere!"), argv
[i
]);
348 set_reflog_expiry_param(&cb
.cmd
, ref
);
349 status
|= reflog_expire(ref
, flags
,
350 reflog_expiry_prepare
,
352 reflog_expiry_cleanup
,
359 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
362 unsigned int flags
= 0;
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")),
378 argc
= parse_options(argc
, argv
, prefix
, options
, reflog_delete_usage
, 0);
381 return error(_("no reflog specified to delete"));
383 for (i
= 0; i
< argc
; i
++)
384 status
|= reflog_delete(argv
[i
], flags
, verbose
);
389 static int cmd_reflog_exists(int argc
, const char **argv
, const char *prefix
)
391 struct option options
[] = {
396 argc
= parse_options(argc
, argv
, prefix
, options
, reflog_exists_usage
,
399 usage_with_options(reflog_exists_usage
, options
);
402 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
))
403 die(_("invalid ref format: %s"), refname
);
404 return !reflog_exists(refname
);
411 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
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
),
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
);
427 return fn(argc
- 1, argv
+ 1, prefix
);
429 return cmd_log_reflog(argc
, argv
, prefix
);