4 #include "repository.h"
10 #include "parse-options.h"
12 #define BUILTIN_REFLOG_SHOW_USAGE \
13 N_("git reflog [show] [<log-options>] [<ref>]")
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>...]")
20 #define BUILTIN_REFLOG_DELETE_USAGE \
21 N_("git reflog delete [--rewrite] [--updateref]\n" \
22 " [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
24 #define BUILTIN_REFLOG_EXISTS_USAGE \
25 N_("git reflog exists <ref>")
27 static const char *const reflog_show_usage
[] = {
28 BUILTIN_REFLOG_SHOW_USAGE
,
32 static const char *const reflog_expire_usage
[] = {
33 BUILTIN_REFLOG_EXPIRE_USAGE
,
37 static const char *const reflog_delete_usage
[] = {
38 BUILTIN_REFLOG_DELETE_USAGE
,
42 static const char *const reflog_exists_usage
[] = {
43 BUILTIN_REFLOG_EXISTS_USAGE
,
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
,
55 static timestamp_t default_reflog_expire
;
56 static timestamp_t default_reflog_expire_unreachable
;
58 struct worktree_reflogs
{
59 struct worktree
*worktree
;
60 struct string_list reflogs
;
63 static int collect_reflog(const char *ref
, const struct object_id
*oid UNUSED
,
64 int flags UNUSED
, void *cb_data
)
66 struct worktree_reflogs
*cb
= cb_data
;
67 struct worktree
*worktree
= cb
->worktree
;
68 struct strbuf newref
= STRBUF_INIT
;
71 * Avoid collecting the same shared ref multiple times because
72 * they are available via all worktrees.
74 if (!worktree
->is_current
&&
75 parse_worktree_ref(ref
, NULL
, NULL
, NULL
) == REF_WORKTREE_SHARED
)
78 strbuf_worktree_ref(worktree
, &newref
, ref
);
79 string_list_append_nodup(&cb
->reflogs
, strbuf_detach(&newref
, NULL
));
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
;
91 static struct reflog_expire_cfg
*find_cfg_ent(const char *pattern
, size_t len
)
93 struct reflog_expire_cfg
*ent
;
95 if (!reflog_expire_cfg_tail
)
96 reflog_expire_cfg_tail
= &reflog_expire_cfg
;
98 for (ent
= reflog_expire_cfg
; ent
; ent
= ent
->next
)
99 if (!strncmp(ent
->pattern
, pattern
, len
) &&
100 ent
->pattern
[len
] == '\0')
103 FLEX_ALLOC_MEM(ent
, pattern
, pattern
, len
);
104 *reflog_expire_cfg_tail
= ent
;
105 reflog_expire_cfg_tail
= &(ent
->next
);
109 /* expiry timer slot */
110 #define EXPIRE_TOTAL 01
111 #define EXPIRE_UNREACH 02
113 static int reflog_expire_config(const char *var
, const char *value
,
114 const struct config_context
*ctx
, void *cb
)
116 const char *pattern
, *key
;
120 struct reflog_expire_cfg
*ent
;
122 if (parse_config_key(var
, "gc", &pattern
, &pattern_len
, &key
) < 0)
123 return git_default_config(var
, value
, ctx
, cb
);
125 if (!strcmp(key
, "reflogexpire")) {
127 if (git_config_expiry_date(&expire
, var
, value
))
129 } else if (!strcmp(key
, "reflogexpireunreachable")) {
130 slot
= EXPIRE_UNREACH
;
131 if (git_config_expiry_date(&expire
, var
, value
))
134 return git_default_config(var
, value
, ctx
, cb
);
139 default_reflog_expire
= expire
;
142 default_reflog_expire_unreachable
= expire
;
148 ent
= find_cfg_ent(pattern
, pattern_len
);
153 ent
->expire_total
= expire
;
156 ent
->expire_unreachable
= expire
;
162 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb
*cb
, const char *ref
)
164 struct reflog_expire_cfg
*ent
;
166 if (cb
->explicit_expiry
== (EXPIRE_TOTAL
|EXPIRE_UNREACH
))
167 return; /* both given explicitly -- nothing to tweak */
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
;
180 * If unconfigured, make stash never expire
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;
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
;
197 static int expire_unreachable_callback(const struct option
*opt
,
201 struct cmd_reflog_expire_cb
*cmd
= opt
->value
;
203 BUG_ON_OPT_NEG(unset
);
205 if (parse_expiry_date(arg
, &cmd
->expire_unreachable
))
206 die(_("invalid timestamp '%s' given to '--%s'"),
207 arg
, opt
->long_name
);
209 cmd
->explicit_expiry
|= EXPIRE_UNREACH
;
213 static int expire_total_callback(const struct option
*opt
,
217 struct cmd_reflog_expire_cb
*cmd
= opt
->value
;
219 BUG_ON_OPT_NEG(unset
);
221 if (parse_expiry_date(arg
, &cmd
->expire_total
))
222 die(_("invalid timestamp '%s' given to '--%s'"),
223 arg
, opt
->long_name
);
225 cmd
->explicit_expiry
|= EXPIRE_TOTAL
;
229 static int cmd_reflog_show(int argc
, const char **argv
, const char *prefix
)
231 struct option options
[] = {
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
);
239 return cmd_log_reflog(argc
, argv
, prefix
);
242 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
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;
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"),
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"),
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")),
276 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
277 default_reflog_expire
= now
- 90 * 24 * 3600;
278 git_config(reflog_expire_config
, NULL
);
280 save_commit_buffer
= 0;
283 cmd
.explicit_expiry
= 0;
284 cmd
.expire_total
= default_reflog_expire
;
285 cmd
.expire_unreachable
= default_reflog_expire_unreachable
;
287 argc
= parse_options(argc
, argv
, prefix
, options
, reflog_expire_usage
, 0);
290 should_prune_fn
= should_expire_reflog_ent_verbose
;
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.
298 struct rev_info revs
;
300 repo_init_revisions(the_repository
, &revs
, prefix
);
301 revs
.do_not_die_on_missing_tree
= 1;
302 revs
.ignore_missing
= 1;
303 revs
.ignore_missing_links
= 1;
305 printf(_("Marking reachable objects..."));
306 mark_reachable_objects(&revs
, 0, 0, NULL
);
307 release_revisions(&revs
);
313 struct worktree_reflogs collected
= {
314 .reflogs
= STRING_LIST_INIT_DUP
,
316 struct string_list_item
*item
;
317 struct worktree
**worktrees
, **p
;
319 worktrees
= get_worktrees();
320 for (p
= worktrees
; *p
; p
++) {
321 if (!all_worktrees
&& !(*p
)->is_current
)
323 collected
.worktree
= *p
;
324 refs_for_each_reflog(get_worktree_ref_store(*p
),
325 collect_reflog
, &collected
);
327 free_worktrees(worktrees
);
329 for_each_string_list_item(item
, &collected
.reflogs
) {
330 struct expire_reflog_policy_cb cb
= {
332 .dry_run
= !!(flags
& EXPIRE_REFLOGS_DRY_RUN
),
335 set_reflog_expiry_param(&cb
.cmd
, item
->string
);
336 status
|= reflog_expire(item
->string
, flags
,
337 reflog_expiry_prepare
,
339 reflog_expiry_cleanup
,
342 string_list_clear(&collected
.reflogs
, 0);
345 for (i
= 0; i
< argc
; i
++) {
347 struct expire_reflog_policy_cb cb
= { .cmd
= cmd
};
349 if (!dwim_log(argv
[i
], strlen(argv
[i
]), NULL
, &ref
)) {
350 status
|= error(_("%s points nowhere!"), argv
[i
]);
353 set_reflog_expiry_param(&cb
.cmd
, ref
);
354 status
|= reflog_expire(ref
, flags
,
355 reflog_expiry_prepare
,
357 reflog_expiry_cleanup
,
364 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
367 unsigned int flags
= 0;
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")),
383 argc
= parse_options(argc
, argv
, prefix
, options
, reflog_delete_usage
, 0);
386 return error(_("no reflog specified to delete"));
388 for (i
= 0; i
< argc
; i
++)
389 status
|= reflog_delete(argv
[i
], flags
, verbose
);
394 static int cmd_reflog_exists(int argc
, const char **argv
, const char *prefix
)
396 struct option options
[] = {
401 argc
= parse_options(argc
, argv
, prefix
, options
, reflog_exists_usage
,
404 usage_with_options(reflog_exists_usage
, options
);
407 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
))
408 die(_("invalid ref format: %s"), refname
);
409 return !reflog_exists(refname
);
416 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
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
),
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
);
432 return fn(argc
- 1, argv
+ 1, prefix
);
434 return cmd_log_reflog(argc
, argv
, prefix
);