blob: 3acef5a0abed400f543da673dbcaa2f648bd4bef [file] [log] [blame]
Junio C Hamano4264dc12006-12-19 00:23:12 -08001#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02003#include "lockfile.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Stefan Beller109cd762018-06-28 18:21:51 -07005#include "repository.h"
Junio C Hamano4264dc12006-12-19 00:23:12 -08006#include "commit.h"
7#include "refs.h"
8#include "dir.h"
Junio C Hamano8d8b9f62006-12-22 00:46:33 -08009#include "tree-walk.h"
Junio C Hamano1389d9d2007-01-06 02:16:19 -080010#include "diff.h"
11#include "revision.h"
12#include "reachable.h"
13
Michael Haggertyfe2a1812015-03-03 12:43:15 +010014/* NEEDSWORK: switch to using parse_options */
Junio C Hamano1389d9d2007-01-06 02:16:19 -080015static const char reflog_expire_usage[] =
Michael Haggertyfe2a1812015-03-03 12:43:15 +010016"git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
Brandon Casey3c386aa2008-02-22 15:08:59 -060017static const char reflog_delete_usage[] =
Michael Haggertyfe2a1812015-03-03 12:43:15 +010018"git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
David Turnerafcb2e72015-07-21 17:04:53 -040019static const char reflog_exists_usage[] =
20"git reflog exists <ref>";
Junio C Hamano4264dc12006-12-19 00:23:12 -080021
Johannes Schindelindddbad72017-04-26 21:29:31 +020022static timestamp_t default_reflog_expire;
23static timestamp_t default_reflog_expire_unreachable;
Junio C Hamano4aec56d2006-12-27 01:47:57 -080024
Junio C Hamano1389d9d2007-01-06 02:16:19 -080025struct cmd_reflog_expire_cb {
26 struct rev_info revs;
Junio C Hamano1389d9d2007-01-06 02:16:19 -080027 int stalefix;
Johannes Schindelindddbad72017-04-26 21:29:31 +020028 timestamp_t expire_total;
29 timestamp_t expire_unreachable;
Johannes Schindelin552cecc2007-10-17 02:50:45 +010030 int recno;
Junio C Hamano1389d9d2007-01-06 02:16:19 -080031};
32
Michael Haggertyea7b4f62014-12-12 09:56:52 +010033struct expire_reflog_policy_cb {
Junio C Hamano03cb91b2010-04-09 13:20:02 -070034 enum {
35 UE_NORMAL,
36 UE_ALWAYS,
37 UE_HEAD
38 } unreachable_expire_kind;
Junio C Hamanob4ca1db2010-04-07 11:09:12 -070039 struct commit_list *mark_list;
40 unsigned long mark_limit;
Michael Haggertyb729eff2014-12-12 09:56:58 +010041 struct cmd_reflog_expire_cb cmd;
Michael Haggertyc48a1632014-12-12 09:56:48 +010042 struct commit *tip_commit;
43 struct commit_list *tips;
Junio C Hamano4264dc12006-12-19 00:23:12 -080044};
45
Junio C Hamanobda3a312008-01-25 23:53:05 -080046struct collected_reflog {
brian m. carlsonb8acac52017-10-15 22:06:58 +000047 struct object_id oid;
Junio C Hamanobda3a312008-01-25 23:53:05 -080048 char reflog[FLEX_ARRAY];
49};
Michael Haggertyddd64c52014-12-12 09:56:53 +010050
Junio C Hamanobda3a312008-01-25 23:53:05 -080051struct collect_reflog_cb {
52 struct collected_reflog **e;
53 int alloc;
54 int nr;
55};
56
Nguyễn Thái Ngọc Duy95308d62018-03-06 17:16:14 +070057/* Remember to update object flag allocation in object.h */
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080058#define INCOMPLETE (1u<<10)
59#define STUDYING (1u<<11)
Junio Hamano494fbfe2009-03-30 21:34:14 -070060#define REACHABLE (1u<<12)
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080061
brian m. carlson49a09e72017-05-06 22:10:16 +000062static int tree_is_complete(const struct object_id *oid)
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080063{
64 struct tree_desc desc;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080065 struct name_entry entry;
66 int complete;
67 struct tree *tree;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080068
Stefan Bellerf86bcc72018-06-28 18:21:56 -070069 tree = lookup_tree(the_repository, oid);
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080070 if (!tree)
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080071 return 0;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080072 if (tree->object.flags & SEEN)
73 return 1;
74 if (tree->object.flags & INCOMPLETE)
75 return 0;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080076
Linus Torvalds6fda5e52007-03-21 10:08:25 -070077 if (!tree->buffer) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050078 enum object_type type;
Linus Torvalds6fda5e52007-03-21 10:08:25 -070079 unsigned long size;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000080 void *data = read_object_file(oid, &type, &size);
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080081 if (!data) {
82 tree->object.flags |= INCOMPLETE;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080083 return 0;
84 }
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080085 tree->buffer = data;
Linus Torvalds6fda5e52007-03-21 10:08:25 -070086 tree->size = size;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080087 }
Linus Torvalds6fda5e52007-03-21 10:08:25 -070088 init_tree_desc(&desc, tree->buffer, tree->size);
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080089 complete = 1;
90 while (tree_entry(&desc, &entry)) {
brian m. carlson7d924c92016-04-17 23:10:39 +000091 if (!has_sha1_file(entry.oid->hash) ||
brian m. carlson49a09e72017-05-06 22:10:16 +000092 (S_ISDIR(entry.mode) && !tree_is_complete(entry.oid))) {
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080093 tree->object.flags |= INCOMPLETE;
94 complete = 0;
95 }
96 }
Jeff King6e454b92013-06-05 18:37:39 -040097 free_tree_buffer(tree);
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080098
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080099 if (complete)
100 tree->object.flags |= SEEN;
101 return complete;
102}
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800103
104static int commit_is_complete(struct commit *commit)
105{
106 struct object_array study;
107 struct object_array found;
108 int is_incomplete = 0;
109 int i;
110
111 /* early return */
112 if (commit->object.flags & SEEN)
113 return 1;
114 if (commit->object.flags & INCOMPLETE)
115 return 0;
116 /*
117 * Find all commits that are reachable and are not marked as
118 * SEEN. Then make sure the trees and blobs contained are
119 * complete. After that, mark these commits also as SEEN.
120 * If some of the objects that are needed to complete this
121 * commit are missing, mark this commit as INCOMPLETE.
122 */
123 memset(&study, 0, sizeof(study));
124 memset(&found, 0, sizeof(found));
125 add_object_array(&commit->object, NULL, &study);
126 add_object_array(&commit->object, NULL, &found);
127 commit->object.flags |= STUDYING;
128 while (study.nr) {
129 struct commit *c;
130 struct commit_list *parent;
131
Martin Ågren71992032017-09-23 01:34:53 +0200132 c = (struct commit *)object_array_pop(&study);
Stefan Beller109cd762018-06-28 18:21:51 -0700133 if (!c->object.parsed && !parse_object(the_repository, &c->object.oid))
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800134 c->object.flags |= INCOMPLETE;
135
136 if (c->object.flags & INCOMPLETE) {
137 is_incomplete = 1;
138 break;
139 }
140 else if (c->object.flags & SEEN)
141 continue;
142 for (parent = c->parents; parent; parent = parent->next) {
143 struct commit *p = parent->item;
144 if (p->object.flags & STUDYING)
145 continue;
146 p->object.flags |= STUDYING;
147 add_object_array(&p->object, NULL, &study);
148 add_object_array(&p->object, NULL, &found);
149 }
150 }
151 if (!is_incomplete) {
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800152 /*
153 * make sure all commits in "found" array have all the
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800154 * necessary objects.
155 */
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800156 for (i = 0; i < found.nr; i++) {
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800157 struct commit *c =
158 (struct commit *)found.objects[i].item;
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000159 if (!tree_is_complete(get_commit_tree_oid(c))) {
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800160 is_incomplete = 1;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800161 c->object.flags |= INCOMPLETE;
162 }
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800163 }
164 if (!is_incomplete) {
165 /* mark all found commits as complete, iow SEEN */
166 for (i = 0; i < found.nr; i++)
167 found.objects[i].item->flags |= SEEN;
168 }
169 }
170 /* clear flags from the objects we traversed */
171 for (i = 0; i < found.nr; i++)
172 found.objects[i].item->flags &= ~STUDYING;
173 if (is_incomplete)
174 commit->object.flags |= INCOMPLETE;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800175 else {
176 /*
177 * If we come here, we have (1) traversed the ancestry chain
178 * from the "commit" until we reach SEEN commits (which are
179 * known to be complete), and (2) made sure that the commits
180 * encountered during the above traversal refer to trees that
181 * are complete. Which means that we know *all* the commits
182 * we have seen during this process are complete.
183 */
184 for (i = 0; i < found.nr; i++)
185 found.objects[i].item->flags |= SEEN;
186 }
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800187 /* free object arrays */
Martin Ågrendcb572a2017-09-23 01:34:52 +0200188 object_array_clear(&study);
189 object_array_clear(&found);
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800190 return !is_incomplete;
191}
192
brian m. carlson43224782017-05-06 22:10:00 +0000193static int keep_entry(struct commit **it, struct object_id *oid)
Junio C Hamano4264dc12006-12-19 00:23:12 -0800194{
Junio C Hamano8d8b9f62006-12-22 00:46:33 -0800195 struct commit *commit;
196
brian m. carlson43224782017-05-06 22:10:00 +0000197 if (is_null_oid(oid))
Junio C Hamano4264dc12006-12-19 00:23:12 -0800198 return 1;
Stefan Beller21e1ee82018-06-28 18:21:57 -0700199 commit = lookup_commit_reference_gently(the_repository, oid, 1);
Junio C Hamano8d8b9f62006-12-22 00:46:33 -0800200 if (!commit)
201 return 0;
202
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800203 /*
204 * Make sure everything in this commit exists.
205 *
206 * We have walked all the objects reachable from the refs
207 * and cache earlier. The commits reachable by this commit
208 * must meet SEEN commits -- and then we should mark them as
209 * SEEN as well.
210 */
211 if (!commit_is_complete(commit))
Junio C Hamano8d8b9f62006-12-22 00:46:33 -0800212 return 0;
213 *it = commit;
214 return 1;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800215}
216
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700217/*
218 * Starting from commits in the cb->mark_list, mark commits that are
219 * reachable from them. Stop the traversal at commits older than
220 * the expire_limit and queue them back, so that the caller can call
221 * us again to restart the traversal with longer expire_limit.
222 */
Michael Haggertyea7b4f62014-12-12 09:56:52 +0100223static void mark_reachable(struct expire_reflog_policy_cb *cb)
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700224{
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700225 struct commit_list *pending;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200226 timestamp_t expire_limit = cb->mark_limit;
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700227 struct commit_list *leftover = NULL;
228
229 for (pending = cb->mark_list; pending; pending = pending->next)
230 pending->item->object.flags &= ~REACHABLE;
231
232 pending = cb->mark_list;
233 while (pending) {
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700234 struct commit_list *parent;
René Scharfee510ab82015-10-24 18:21:31 +0200235 struct commit *commit = pop_commit(&pending);
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700236 if (commit->object.flags & REACHABLE)
237 continue;
238 if (parse_commit(commit))
239 continue;
240 commit->object.flags |= REACHABLE;
241 if (commit->date < expire_limit) {
242 commit_list_insert(commit, &leftover);
243 continue;
244 }
245 commit->object.flags |= REACHABLE;
246 parent = commit->parents;
247 while (parent) {
248 commit = parent->item;
249 parent = parent->next;
250 if (commit->object.flags & REACHABLE)
251 continue;
252 commit_list_insert(commit, &pending);
253 }
254 }
255 cb->mark_list = leftover;
256}
257
brian m. carlson43224782017-05-06 22:10:00 +0000258static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, struct object_id *oid)
Linus Torvalds666e07e2009-03-31 09:45:22 -0700259{
260 /*
261 * We may or may not have the commit yet - if not, look it
262 * up using the supplied sha1.
263 */
264 if (!commit) {
brian m. carlson43224782017-05-06 22:10:00 +0000265 if (is_null_oid(oid))
Linus Torvalds666e07e2009-03-31 09:45:22 -0700266 return 0;
267
Stefan Beller21e1ee82018-06-28 18:21:57 -0700268 commit = lookup_commit_reference_gently(the_repository, oid,
269 1);
Linus Torvalds666e07e2009-03-31 09:45:22 -0700270
271 /* Not a commit -- keep it */
272 if (!commit)
273 return 0;
274 }
275
276 /* Reachable from the current ref? Don't prune. */
Junio Hamano494fbfe2009-03-30 21:34:14 -0700277 if (commit->object.flags & REACHABLE)
278 return 0;
Linus Torvalds666e07e2009-03-31 09:45:22 -0700279
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700280 if (cb->mark_list && cb->mark_limit) {
281 cb->mark_limit = 0; /* dig down to the root */
282 mark_reachable(cb);
Junio Hamano494fbfe2009-03-30 21:34:14 -0700283 }
Junio C Hamanob4ca1db2010-04-07 11:09:12 -0700284
285 return !(commit->object.flags & REACHABLE);
Junio Hamano494fbfe2009-03-30 21:34:14 -0700286}
287
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100288/*
289 * Return true iff the specified reflog entry should be expired.
290 */
brian m. carlson43224782017-05-06 22:10:00 +0000291static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200292 const char *email, timestamp_t timestamp, int tz,
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100293 const char *message, void *cb_data)
Junio C Hamano4264dc12006-12-19 00:23:12 -0800294{
Michael Haggertyea7b4f62014-12-12 09:56:52 +0100295 struct expire_reflog_policy_cb *cb = cb_data;
Brandon Williamsdfa59902018-02-14 10:59:34 -0800296 struct commit *old_commit, *new_commit;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800297
Michael Haggertyb729eff2014-12-12 09:56:58 +0100298 if (timestamp < cb->cmd.expire_total)
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100299 return 1;
Brandon Casey2b81fab2008-02-22 12:56:50 -0600300
Brandon Williamsdfa59902018-02-14 10:59:34 -0800301 old_commit = new_commit = NULL;
Michael Haggertyb729eff2014-12-12 09:56:58 +0100302 if (cb->cmd.stalefix &&
Brandon Williamsdfa59902018-02-14 10:59:34 -0800303 (!keep_entry(&old_commit, ooid) || !keep_entry(&new_commit, noid)))
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100304 return 1;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800305
Michael Haggertyb729eff2014-12-12 09:56:58 +0100306 if (timestamp < cb->cmd.expire_unreachable) {
Junio C Hamano03cb91b2010-04-09 13:20:02 -0700307 if (cb->unreachable_expire_kind == UE_ALWAYS)
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100308 return 1;
Brandon Williamsdfa59902018-02-14 10:59:34 -0800309 if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100310 return 1;
Junio C Hamano9bbaa6c2007-01-11 19:56:43 -0800311 }
Junio C Hamano4264dc12006-12-19 00:23:12 -0800312
Michael Haggertyb729eff2014-12-12 09:56:58 +0100313 if (cb->cmd.recno && --(cb->cmd.recno) == 0)
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100314 return 1;
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100315
Junio C Hamano4264dc12006-12-19 00:23:12 -0800316 return 0;
Michael Haggerty60cc3c42014-12-12 09:56:47 +0100317}
318
Michael Haggerty5bcad1b2015-05-25 18:38:40 +0000319static int push_tip_to_list(const char *refname, const struct object_id *oid,
Michael Haggertyea7b4f62014-12-12 09:56:52 +0100320 int flags, void *cb_data)
Junio C Hamano03cb91b2010-04-09 13:20:02 -0700321{
322 struct commit_list **list = cb_data;
323 struct commit *tip_commit;
324 if (flags & REF_ISSYMREF)
325 return 0;
Stefan Beller21e1ee82018-06-28 18:21:57 -0700326 tip_commit = lookup_commit_reference_gently(the_repository, oid, 1);
Junio C Hamano03cb91b2010-04-09 13:20:02 -0700327 if (!tip_commit)
328 return 0;
329 commit_list_insert(tip_commit, list);
330 return 0;
331}
332
Michael Haggertyc48a1632014-12-12 09:56:48 +0100333static void reflog_expiry_prepare(const char *refname,
brian m. carlson43224782017-05-06 22:10:00 +0000334 const struct object_id *oid,
Michael Haggertyb729eff2014-12-12 09:56:58 +0100335 void *cb_data)
Michael Haggertyc48a1632014-12-12 09:56:48 +0100336{
Michael Haggertyb729eff2014-12-12 09:56:58 +0100337 struct expire_reflog_policy_cb *cb = cb_data;
338
339 if (!cb->cmd.expire_unreachable || !strcmp(refname, "HEAD")) {
Michael Haggertyc48a1632014-12-12 09:56:48 +0100340 cb->tip_commit = NULL;
341 cb->unreachable_expire_kind = UE_HEAD;
342 } else {
Stefan Beller21e1ee82018-06-28 18:21:57 -0700343 cb->tip_commit = lookup_commit_reference_gently(the_repository,
344 oid, 1);
Michael Haggertyc48a1632014-12-12 09:56:48 +0100345 if (!cb->tip_commit)
346 cb->unreachable_expire_kind = UE_ALWAYS;
347 else
348 cb->unreachable_expire_kind = UE_NORMAL;
349 }
350
Michael Haggertyb729eff2014-12-12 09:56:58 +0100351 if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
Michael Haggertyc48a1632014-12-12 09:56:48 +0100352 cb->unreachable_expire_kind = UE_ALWAYS;
353
354 cb->mark_list = NULL;
355 cb->tips = NULL;
356 if (cb->unreachable_expire_kind != UE_ALWAYS) {
357 if (cb->unreachable_expire_kind == UE_HEAD) {
358 struct commit_list *elem;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +0000359
Michael Haggerty5bcad1b2015-05-25 18:38:40 +0000360 for_each_ref(push_tip_to_list, &cb->tips);
Michael Haggertyc48a1632014-12-12 09:56:48 +0100361 for (elem = cb->tips; elem; elem = elem->next)
362 commit_list_insert(elem->item, &cb->mark_list);
363 } else {
364 commit_list_insert(cb->tip_commit, &cb->mark_list);
365 }
Michael Haggertyb729eff2014-12-12 09:56:58 +0100366 cb->mark_limit = cb->cmd.expire_total;
Michael Haggertyc48a1632014-12-12 09:56:48 +0100367 mark_reachable(cb);
368 }
369}
370
Michael Haggertyb729eff2014-12-12 09:56:58 +0100371static void reflog_expiry_cleanup(void *cb_data)
Michael Haggertyc48a1632014-12-12 09:56:48 +0100372{
Michael Haggertyb729eff2014-12-12 09:56:58 +0100373 struct expire_reflog_policy_cb *cb = cb_data;
374
Michael Haggertyc48a1632014-12-12 09:56:48 +0100375 if (cb->unreachable_expire_kind != UE_ALWAYS) {
376 if (cb->unreachable_expire_kind == UE_HEAD) {
377 struct commit_list *elem;
378 for (elem = cb->tips; elem; elem = elem->next)
379 clear_commit_marks(elem->item, REACHABLE);
380 free_commit_list(cb->tips);
381 } else {
382 clear_commit_marks(cb->tip_commit, REACHABLE);
383 }
384 }
385}
386
Michael Haggerty5bcad1b2015-05-25 18:38:40 +0000387static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
Junio C Hamanobda3a312008-01-25 23:53:05 -0800388{
389 struct collected_reflog *e;
390 struct collect_reflog_cb *cb = cb_data;
Junio C Hamanobda3a312008-01-25 23:53:05 -0800391
Jeff King96ffc062016-02-22 17:44:32 -0500392 FLEX_ALLOC_STR(e, reflog, ref);
brian m. carlsonb8acac52017-10-15 22:06:58 +0000393 oidcpy(&e->oid, oid);
Junio C Hamanobda3a312008-01-25 23:53:05 -0800394 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
395 cb->e[cb->nr++] = e;
396 return 0;
397}
398
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700399static struct reflog_expire_cfg {
400 struct reflog_expire_cfg *next;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200401 timestamp_t expire_total;
402 timestamp_t expire_unreachable;
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700403 char pattern[FLEX_ARRAY];
404} *reflog_expire_cfg, **reflog_expire_cfg_tail;
405
406static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
407{
408 struct reflog_expire_cfg *ent;
409
410 if (!reflog_expire_cfg_tail)
411 reflog_expire_cfg_tail = &reflog_expire_cfg;
412
413 for (ent = reflog_expire_cfg; ent; ent = ent->next)
Jeff Kingc3a700f2016-02-19 06:21:08 -0500414 if (!strncmp(ent->pattern, pattern, len) &&
415 ent->pattern[len] == '\0')
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700416 return ent;
417
Jeff King96ffc062016-02-22 17:44:32 -0500418 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700419 *reflog_expire_cfg_tail = ent;
420 reflog_expire_cfg_tail = &(ent->next);
421 return ent;
422}
423
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700424/* expiry timer slot */
425#define EXPIRE_TOTAL 01
426#define EXPIRE_UNREACH 02
427
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100428static int reflog_expire_config(const char *var, const char *value, void *cb)
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800429{
Jeff Kingb3873c32013-01-23 01:27:37 -0500430 const char *pattern, *key;
431 int pattern_len;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200432 timestamp_t expire;
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700433 int slot;
434 struct reflog_expire_cfg *ent;
435
Jeff Kingb3873c32013-01-23 01:27:37 -0500436 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700437 return git_default_config(var, value, cb);
438
Jeff Kingb3873c32013-01-23 01:27:37 -0500439 if (!strcmp(key, "reflogexpire")) {
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700440 slot = EXPIRE_TOTAL;
Haaris Mehmood5f967422017-11-18 02:27:27 +0000441 if (git_config_expiry_date(&expire, var, value))
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700442 return -1;
Jeff Kingb3873c32013-01-23 01:27:37 -0500443 } else if (!strcmp(key, "reflogexpireunreachable")) {
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700444 slot = EXPIRE_UNREACH;
Haaris Mehmood5f967422017-11-18 02:27:27 +0000445 if (git_config_expiry_date(&expire, var, value))
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700446 return -1;
447 } else
448 return git_default_config(var, value, cb);
449
Jeff Kingb3873c32013-01-23 01:27:37 -0500450 if (!pattern) {
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700451 switch (slot) {
452 case EXPIRE_TOTAL:
453 default_reflog_expire = expire;
454 break;
455 case EXPIRE_UNREACH:
456 default_reflog_expire_unreachable = expire;
457 break;
458 }
Junio C Hamano4f342b92008-02-11 10:50:06 -0800459 return 0;
460 }
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700461
Jeff Kingb3873c32013-01-23 01:27:37 -0500462 ent = find_cfg_ent(pattern, pattern_len);
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700463 if (!ent)
464 return -1;
465 switch (slot) {
466 case EXPIRE_TOTAL:
467 ent->expire_total = expire;
468 break;
469 case EXPIRE_UNREACH:
470 ent->expire_unreachable = expire;
471 break;
Junio C Hamano4f342b92008-02-11 10:50:06 -0800472 }
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700473 return 0;
474}
475
476static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
477{
478 struct reflog_expire_cfg *ent;
479
480 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
481 return; /* both given explicitly -- nothing to tweak */
482
483 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +0000484 if (!wildmatch(ent->pattern, ref, 0)) {
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700485 if (!(slot & EXPIRE_TOTAL))
486 cb->expire_total = ent->expire_total;
487 if (!(slot & EXPIRE_UNREACH))
488 cb->expire_unreachable = ent->expire_unreachable;
489 return;
490 }
491 }
492
Junio C Hamano60bce2b2008-06-28 22:24:49 -0700493 /*
494 * If unconfigured, make stash never expire
495 */
496 if (!strcmp(ref, "refs/stash")) {
497 if (!(slot & EXPIRE_TOTAL))
498 cb->expire_total = 0;
499 if (!(slot & EXPIRE_UNREACH))
500 cb->expire_unreachable = 0;
501 return;
502 }
503
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700504 /* Nothing matched -- use the default value */
505 if (!(slot & EXPIRE_TOTAL))
506 cb->expire_total = default_reflog_expire;
507 if (!(slot & EXPIRE_UNREACH))
508 cb->expire_unreachable = default_reflog_expire_unreachable;
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800509}
510
Junio C Hamano4264dc12006-12-19 00:23:12 -0800511static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
512{
Michael Haggertyb729eff2014-12-12 09:56:58 +0100513 struct expire_reflog_policy_cb cb;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200514 timestamp_t now = time(NULL);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800515 int i, status, do_all;
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700516 int explicit_expiry = 0;
Michael Haggertyaba56c82014-12-12 09:56:49 +0100517 unsigned int flags = 0;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800518
Adam Simpkins4a9f4392010-02-26 19:50:03 -0800519 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
520 default_reflog_expire = now - 90 * 24 * 3600;
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100521 git_config(reflog_expire_config, NULL);
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800522
Junio C Hamano4264dc12006-12-19 00:23:12 -0800523 save_commit_buffer = 0;
524 do_all = status = 0;
525 memset(&cb, 0, sizeof(cb));
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800526
Michael Haggertyb729eff2014-12-12 09:56:58 +0100527 cb.cmd.expire_total = default_reflog_expire;
528 cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800529
530 for (i = 1; i < argc; i++) {
531 const char *arg = argv[i];
532 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
Michael Haggerty98f31d82014-12-12 09:56:50 +0100533 flags |= EXPIRE_REFLOGS_DRY_RUN;
Christian Couder59556542013-11-30 21:55:40 +0100534 else if (starts_with(arg, "--expire=")) {
Michael Haggertyb729eff2014-12-12 09:56:58 +0100535 if (parse_expiry_date(arg + 9, &cb.cmd.expire_total))
Junio C Hamano3d27b9b2013-04-17 15:38:08 -0700536 die(_("'%s' is not a valid timestamp"), arg);
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700537 explicit_expiry |= EXPIRE_TOTAL;
538 }
Christian Couder59556542013-11-30 21:55:40 +0100539 else if (starts_with(arg, "--expire-unreachable=")) {
Michael Haggertyb729eff2014-12-12 09:56:58 +0100540 if (parse_expiry_date(arg + 21, &cb.cmd.expire_unreachable))
Junio C Hamano3d27b9b2013-04-17 15:38:08 -0700541 die(_("'%s' is not a valid timestamp"), arg);
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700542 explicit_expiry |= EXPIRE_UNREACH;
543 }
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800544 else if (!strcmp(arg, "--stale-fix"))
Michael Haggertyb729eff2014-12-12 09:56:58 +0100545 cb.cmd.stalefix = 1;
Brandon Casey2b81fab2008-02-22 12:56:50 -0600546 else if (!strcmp(arg, "--rewrite"))
Michael Haggerty553daf12014-12-12 09:56:56 +0100547 flags |= EXPIRE_REFLOGS_REWRITE;
Brandon Casey55f10562008-02-22 13:04:12 -0600548 else if (!strcmp(arg, "--updateref"))
Michael Haggertyc4c4fbf2014-12-12 09:56:51 +0100549 flags |= EXPIRE_REFLOGS_UPDATE_REF;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800550 else if (!strcmp(arg, "--all"))
551 do_all = 1;
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800552 else if (!strcmp(arg, "--verbose"))
Michael Haggertybc111552014-12-12 09:56:55 +0100553 flags |= EXPIRE_REFLOGS_VERBOSE;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800554 else if (!strcmp(arg, "--")) {
555 i++;
556 break;
557 }
558 else if (arg[0] == '-')
559 usage(reflog_expire_usage);
560 else
561 break;
562 }
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700563
564 /*
565 * We can trust the commits and objects reachable from refs
566 * even in older repository. We cannot trust what's reachable
567 * from reflog if the repository was pruned with older git.
568 */
Michael Haggertyb729eff2014-12-12 09:56:58 +0100569 if (cb.cmd.stalefix) {
570 init_revisions(&cb.cmd.revs, prefix);
Michael Haggertybc111552014-12-12 09:56:55 +0100571 if (flags & EXPIRE_REFLOGS_VERBOSE)
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800572 printf("Marking reachable objects...");
Michael Haggertyb729eff2014-12-12 09:56:58 +0100573 mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
Michael Haggertybc111552014-12-12 09:56:55 +0100574 if (flags & EXPIRE_REFLOGS_VERBOSE)
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800575 putchar('\n');
576 }
577
Junio C Hamanobda3a312008-01-25 23:53:05 -0800578 if (do_all) {
579 struct collect_reflog_cb collected;
580 int i;
581
582 memset(&collected, 0, sizeof(collected));
Michael Haggerty5bcad1b2015-05-25 18:38:40 +0000583 for_each_reflog(collect_reflog, &collected);
Junio C Hamanobda3a312008-01-25 23:53:05 -0800584 for (i = 0; i < collected.nr; i++) {
585 struct collected_reflog *e = collected.e[i];
Michael Haggertyb729eff2014-12-12 09:56:58 +0100586 set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
brian m. carlson0155f712017-10-15 22:07:04 +0000587 status |= reflog_expire(e->reflog, &e->oid, flags,
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100588 reflog_expiry_prepare,
589 should_expire_reflog_ent,
590 reflog_expiry_cleanup,
591 &cb);
Junio C Hamanobda3a312008-01-25 23:53:05 -0800592 free(e);
593 }
594 free(collected.e);
595 }
596
Pieter de Bie90fb46e2008-08-10 22:22:21 +0200597 for (; i < argc; i++) {
598 char *ref;
brian m. carlsonb8acac52017-10-15 22:06:58 +0000599 struct object_id oid;
brian m. carlson334dc522017-10-15 22:06:59 +0000600 if (!dwim_log(argv[i], strlen(argv[i]), &oid, &ref)) {
Pieter de Bie90fb46e2008-08-10 22:22:21 +0200601 status |= error("%s points nowhere!", argv[i]);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800602 continue;
603 }
Michael Haggertyb729eff2014-12-12 09:56:58 +0100604 set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
brian m. carlson0155f712017-10-15 22:07:04 +0000605 status |= reflog_expire(ref, &oid, flags,
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100606 reflog_expiry_prepare,
607 should_expire_reflog_ent,
608 reflog_expiry_cleanup,
609 &cb);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800610 }
611 return status;
612}
613
brian m. carlson9461d272017-02-21 23:47:32 +0000614static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200615 const char *email, timestamp_t timestamp, int tz,
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100616 const char *message, void *cb_data)
617{
Michael Haggertyb729eff2014-12-12 09:56:58 +0100618 struct expire_reflog_policy_cb *cb = cb_data;
619 if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
620 cb->cmd.recno++;
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100621 return 0;
622}
623
624static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
625{
Michael Haggertyb729eff2014-12-12 09:56:58 +0100626 struct expire_reflog_policy_cb cb;
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100627 int i, status = 0;
Michael Haggertyaba56c82014-12-12 09:56:49 +0100628 unsigned int flags = 0;
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100629
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100630 memset(&cb, 0, sizeof(cb));
631
632 for (i = 1; i < argc; i++) {
Brandon Casey3c386aa2008-02-22 15:08:59 -0600633 const char *arg = argv[i];
634 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
Michael Haggerty98f31d82014-12-12 09:56:50 +0100635 flags |= EXPIRE_REFLOGS_DRY_RUN;
Brandon Casey2b81fab2008-02-22 12:56:50 -0600636 else if (!strcmp(arg, "--rewrite"))
Michael Haggerty553daf12014-12-12 09:56:56 +0100637 flags |= EXPIRE_REFLOGS_REWRITE;
Brandon Casey55f10562008-02-22 13:04:12 -0600638 else if (!strcmp(arg, "--updateref"))
Michael Haggertyc4c4fbf2014-12-12 09:56:51 +0100639 flags |= EXPIRE_REFLOGS_UPDATE_REF;
Brandon Casey3c386aa2008-02-22 15:08:59 -0600640 else if (!strcmp(arg, "--verbose"))
Michael Haggertybc111552014-12-12 09:56:55 +0100641 flags |= EXPIRE_REFLOGS_VERBOSE;
Brandon Casey3c386aa2008-02-22 15:08:59 -0600642 else if (!strcmp(arg, "--")) {
643 i++;
644 break;
645 }
646 else if (arg[0] == '-')
647 usage(reflog_delete_usage);
648 else
649 break;
650 }
651
652 if (argc - i < 1)
653 return error("Nothing to delete?");
654
655 for ( ; i < argc; i++) {
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100656 const char *spec = strstr(argv[i], "@{");
brian m. carlsonb8acac52017-10-15 22:06:58 +0000657 struct object_id oid;
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100658 char *ep, *ref;
659 int recno;
660
661 if (!spec) {
Brandon Caseycb97cc92008-01-04 19:11:37 -0600662 status |= error("Not a reflog: %s", argv[i]);
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100663 continue;
664 }
665
brian m. carlson334dc522017-10-15 22:06:59 +0000666 if (!dwim_log(argv[i], spec - argv[i], &oid, &ref)) {
Junio C Hamano55beff42008-08-10 23:21:25 -0700667 status |= error("no reflog for '%s'", argv[i]);
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100668 continue;
669 }
670
671 recno = strtoul(spec + 2, &ep, 10);
672 if (*ep == '}') {
Michael Haggertyb729eff2014-12-12 09:56:58 +0100673 cb.cmd.recno = -recno;
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100674 for_each_reflog_ent(ref, count_reflog_ent, &cb);
675 } else {
Michael Haggertyb729eff2014-12-12 09:56:58 +0100676 cb.cmd.expire_total = approxidate(spec + 2);
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100677 for_each_reflog_ent(ref, count_reflog_ent, &cb);
Michael Haggertyb729eff2014-12-12 09:56:58 +0100678 cb.cmd.expire_total = 0;
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100679 }
680
brian m. carlson0155f712017-10-15 22:07:04 +0000681 status |= reflog_expire(ref, &oid, flags,
Michael Haggertyfa5b1832014-12-12 09:56:59 +0100682 reflog_expiry_prepare,
683 should_expire_reflog_ent,
684 reflog_expiry_cleanup,
685 &cb);
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100686 free(ref);
687 }
688 return status;
689}
690
David Turnerafcb2e72015-07-21 17:04:53 -0400691static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
692{
693 int i, start = 0;
694
695 for (i = 1; i < argc; i++) {
696 const char *arg = argv[i];
697 if (!strcmp(arg, "--")) {
698 i++;
699 break;
700 }
701 else if (arg[0] == '-')
702 usage(reflog_exists_usage);
703 else
704 break;
705 }
706
707 start = i;
708
709 if (argc - start != 1)
710 usage(reflog_exists_usage);
711
712 if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
713 die("invalid ref format: %s", argv[start]);
714 return !reflog_exists(argv[start]);
715}
716
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800717/*
718 * main "reflog"
719 */
720
Junio C Hamano4264dc12006-12-19 00:23:12 -0800721static const char reflog_usage[] =
David Turnerafcb2e72015-07-21 17:04:53 -0400722"git reflog [ show | expire | delete | exists ]";
Junio C Hamano4264dc12006-12-19 00:23:12 -0800723
724int cmd_reflog(int argc, const char **argv, const char *prefix)
725{
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600726 if (argc > 1 && !strcmp(argv[1], "-h"))
727 usage(reflog_usage);
728
Linus Torvaldscf39f542007-02-08 09:51:56 -0800729 /* With no command, we default to showing it. */
730 if (argc < 2 || *argv[1] == '-')
731 return cmd_log_reflog(argc, argv, prefix);
732
733 if (!strcmp(argv[1], "show"))
734 return cmd_log_reflog(argc - 1, argv + 1, prefix);
735
736 if (!strcmp(argv[1], "expire"))
Junio C Hamano4264dc12006-12-19 00:23:12 -0800737 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
Linus Torvaldscf39f542007-02-08 09:51:56 -0800738
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100739 if (!strcmp(argv[1], "delete"))
740 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
741
David Turnerafcb2e72015-07-21 17:04:53 -0400742 if (!strcmp(argv[1], "exists"))
743 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
744
Michael Schubertbf01d4a2011-08-01 13:20:42 +0200745 return cmd_log_reflog(argc, argv, prefix);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800746}