blob: e23b5ef979d98e5e44693345856a13485695246f [file] [log] [blame]
Junio C Hamano4264dc12006-12-19 00:23:12 -08001#include "cache.h"
2#include "builtin.h"
3#include "commit.h"
4#include "refs.h"
5#include "dir.h"
Junio C Hamano8d8b9f62006-12-22 00:46:33 -08006#include "tree-walk.h"
Junio C Hamano1389d9d2007-01-06 02:16:19 -08007#include "diff.h"
8#include "revision.h"
9#include "reachable.h"
10
11/*
12 * reflog expire
13 */
14
15static const char reflog_expire_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020016"git reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
Brandon Casey3c386aa2008-02-22 15:08:59 -060017static const char reflog_delete_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020018"git reflog delete [--verbose] [--dry-run] [--rewrite] [--updateref] <refs>...";
Junio C Hamano4264dc12006-12-19 00:23:12 -080019
Junio C Hamano4aec56d2006-12-27 01:47:57 -080020static unsigned long default_reflog_expire;
21static unsigned long default_reflog_expire_unreachable;
22
Junio C Hamano1389d9d2007-01-06 02:16:19 -080023struct cmd_reflog_expire_cb {
24 struct rev_info revs;
25 int dry_run;
26 int stalefix;
Brandon Casey2b81fab2008-02-22 12:56:50 -060027 int rewrite;
Brandon Casey55f10562008-02-22 13:04:12 -060028 int updateref;
Junio C Hamano1389d9d2007-01-06 02:16:19 -080029 int verbose;
30 unsigned long expire_total;
31 unsigned long expire_unreachable;
Johannes Schindelin552cecc2007-10-17 02:50:45 +010032 int recno;
Junio C Hamano1389d9d2007-01-06 02:16:19 -080033};
34
Junio C Hamano4264dc12006-12-19 00:23:12 -080035struct expire_reflog_cb {
36 FILE *newlog;
37 const char *ref;
38 struct commit *ref_commit;
Junio C Hamano1389d9d2007-01-06 02:16:19 -080039 struct cmd_reflog_expire_cb *cmd;
Brandon Casey2b81fab2008-02-22 12:56:50 -060040 unsigned char last_kept_sha1[20];
Junio C Hamano4264dc12006-12-19 00:23:12 -080041};
42
Junio C Hamanobda3a312008-01-25 23:53:05 -080043struct collected_reflog {
44 unsigned char sha1[20];
45 char reflog[FLEX_ARRAY];
46};
47struct collect_reflog_cb {
48 struct collected_reflog **e;
49 int alloc;
50 int nr;
51};
52
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080053#define INCOMPLETE (1u<<10)
54#define STUDYING (1u<<11)
Junio Hamano494fbfe2009-03-30 21:34:14 -070055#define REACHABLE (1u<<12)
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080056
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080057static int tree_is_complete(const unsigned char *sha1)
58{
59 struct tree_desc desc;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080060 struct name_entry entry;
61 int complete;
62 struct tree *tree;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080063
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080064 tree = lookup_tree(sha1);
65 if (!tree)
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080066 return 0;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080067 if (tree->object.flags & SEEN)
68 return 1;
69 if (tree->object.flags & INCOMPLETE)
70 return 0;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080071
Linus Torvalds6fda5e52007-03-21 10:08:25 -070072 if (!tree->buffer) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050073 enum object_type type;
Linus Torvalds6fda5e52007-03-21 10:08:25 -070074 unsigned long size;
75 void *data = read_sha1_file(sha1, &type, &size);
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080076 if (!data) {
77 tree->object.flags |= INCOMPLETE;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080078 return 0;
79 }
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080080 tree->buffer = data;
Linus Torvalds6fda5e52007-03-21 10:08:25 -070081 tree->size = size;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080082 }
Linus Torvalds6fda5e52007-03-21 10:08:25 -070083 init_tree_desc(&desc, tree->buffer, tree->size);
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080084 complete = 1;
85 while (tree_entry(&desc, &entry)) {
86 if (!has_sha1_file(entry.sha1) ||
87 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
88 tree->object.flags |= INCOMPLETE;
89 complete = 0;
90 }
91 }
92 free(tree->buffer);
93 tree->buffer = NULL;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -080094
Junio C Hamanocd1f9c32007-01-06 22:32:41 -080095 if (complete)
96 tree->object.flags |= SEEN;
97 return complete;
98}
Junio C Hamano1389d9d2007-01-06 02:16:19 -080099
100static int commit_is_complete(struct commit *commit)
101{
102 struct object_array study;
103 struct object_array found;
104 int is_incomplete = 0;
105 int i;
106
107 /* early return */
108 if (commit->object.flags & SEEN)
109 return 1;
110 if (commit->object.flags & INCOMPLETE)
111 return 0;
112 /*
113 * Find all commits that are reachable and are not marked as
114 * SEEN. Then make sure the trees and blobs contained are
115 * complete. After that, mark these commits also as SEEN.
116 * If some of the objects that are needed to complete this
117 * commit are missing, mark this commit as INCOMPLETE.
118 */
119 memset(&study, 0, sizeof(study));
120 memset(&found, 0, sizeof(found));
121 add_object_array(&commit->object, NULL, &study);
122 add_object_array(&commit->object, NULL, &found);
123 commit->object.flags |= STUDYING;
124 while (study.nr) {
125 struct commit *c;
126 struct commit_list *parent;
127
128 c = (struct commit *)study.objects[--study.nr].item;
129 if (!c->object.parsed && !parse_object(c->object.sha1))
130 c->object.flags |= INCOMPLETE;
131
132 if (c->object.flags & INCOMPLETE) {
133 is_incomplete = 1;
134 break;
135 }
136 else if (c->object.flags & SEEN)
137 continue;
138 for (parent = c->parents; parent; parent = parent->next) {
139 struct commit *p = parent->item;
140 if (p->object.flags & STUDYING)
141 continue;
142 p->object.flags |= STUDYING;
143 add_object_array(&p->object, NULL, &study);
144 add_object_array(&p->object, NULL, &found);
145 }
146 }
147 if (!is_incomplete) {
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800148 /*
149 * make sure all commits in "found" array have all the
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800150 * necessary objects.
151 */
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800152 for (i = 0; i < found.nr; i++) {
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800153 struct commit *c =
154 (struct commit *)found.objects[i].item;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800155 if (!tree_is_complete(c->tree->object.sha1)) {
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800156 is_incomplete = 1;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800157 c->object.flags |= INCOMPLETE;
158 }
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800159 }
160 if (!is_incomplete) {
161 /* mark all found commits as complete, iow SEEN */
162 for (i = 0; i < found.nr; i++)
163 found.objects[i].item->flags |= SEEN;
164 }
165 }
166 /* clear flags from the objects we traversed */
167 for (i = 0; i < found.nr; i++)
168 found.objects[i].item->flags &= ~STUDYING;
169 if (is_incomplete)
170 commit->object.flags |= INCOMPLETE;
Junio C Hamanocd1f9c32007-01-06 22:32:41 -0800171 else {
172 /*
173 * If we come here, we have (1) traversed the ancestry chain
174 * from the "commit" until we reach SEEN commits (which are
175 * known to be complete), and (2) made sure that the commits
176 * encountered during the above traversal refer to trees that
177 * are complete. Which means that we know *all* the commits
178 * we have seen during this process are complete.
179 */
180 for (i = 0; i < found.nr; i++)
181 found.objects[i].item->flags |= SEEN;
182 }
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800183 /* free object arrays */
184 free(study.objects);
185 free(found.objects);
186 return !is_incomplete;
187}
188
Junio C Hamano4264dc12006-12-19 00:23:12 -0800189static int keep_entry(struct commit **it, unsigned char *sha1)
190{
Junio C Hamano8d8b9f62006-12-22 00:46:33 -0800191 struct commit *commit;
192
Junio C Hamano4264dc12006-12-19 00:23:12 -0800193 if (is_null_sha1(sha1))
194 return 1;
Junio C Hamano8d8b9f62006-12-22 00:46:33 -0800195 commit = lookup_commit_reference_gently(sha1, 1);
196 if (!commit)
197 return 0;
198
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800199 /*
200 * Make sure everything in this commit exists.
201 *
202 * We have walked all the objects reachable from the refs
203 * and cache earlier. The commits reachable by this commit
204 * must meet SEEN commits -- and then we should mark them as
205 * SEEN as well.
206 */
207 if (!commit_is_complete(commit))
Junio C Hamano8d8b9f62006-12-22 00:46:33 -0800208 return 0;
209 *it = commit;
210 return 1;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800211}
212
Linus Torvalds666e07e2009-03-31 09:45:22 -0700213static int unreachable(struct expire_reflog_cb *cb, struct commit *commit, unsigned char *sha1)
214{
215 /*
216 * We may or may not have the commit yet - if not, look it
217 * up using the supplied sha1.
218 */
219 if (!commit) {
220 if (is_null_sha1(sha1))
221 return 0;
222
223 commit = lookup_commit_reference_gently(sha1, 1);
224
225 /* Not a commit -- keep it */
226 if (!commit)
227 return 0;
228 }
229
230 /* Reachable from the current ref? Don't prune. */
Junio Hamano494fbfe2009-03-30 21:34:14 -0700231 if (commit->object.flags & REACHABLE)
232 return 0;
Linus Torvalds666e07e2009-03-31 09:45:22 -0700233 if (in_merge_bases(commit, &cb->ref_commit, 1))
234 return 0;
235
236 /* We can't reach it - prune it. */
237 return 1;
238}
239
Junio Hamano494fbfe2009-03-30 21:34:14 -0700240static void mark_reachable(struct commit *commit, unsigned long expire_limit)
241{
242 /*
Michael J Gruberb18cc5a2009-04-22 23:15:56 +0200243 * We need to compute whether the commit on either side of a reflog
Junio Hamano494fbfe2009-03-30 21:34:14 -0700244 * entry is reachable from the tip of the ref for all entries.
245 * Mark commits that are reachable from the tip down to the
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100246 * time threshold first; we know a commit marked thusly is
Junio Hamano494fbfe2009-03-30 21:34:14 -0700247 * reachable from the tip without running in_merge_bases()
248 * at all.
249 */
250 struct commit_list *pending = NULL;
251
252 commit_list_insert(commit, &pending);
253 while (pending) {
254 struct commit_list *entry = pending;
255 struct commit_list *parent;
256 pending = entry->next;
257 commit = entry->item;
258 free(entry);
259 if (commit->object.flags & REACHABLE)
260 continue;
261 if (parse_commit(commit))
262 continue;
263 commit->object.flags |= REACHABLE;
264 if (commit->date < expire_limit)
265 continue;
266 parent = commit->parents;
267 while (parent) {
268 commit = parent->item;
269 parent = parent->next;
270 if (commit->object.flags & REACHABLE)
271 continue;
272 commit_list_insert(commit, &pending);
273 }
274 }
275}
276
Junio C Hamano4264dc12006-12-19 00:23:12 -0800277static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100278 const char *email, unsigned long timestamp, int tz,
279 const char *message, void *cb_data)
Junio C Hamano4264dc12006-12-19 00:23:12 -0800280{
281 struct expire_reflog_cb *cb = cb_data;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800282 struct commit *old, *new;
283
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800284 if (timestamp < cb->cmd->expire_total)
Junio C Hamano4264dc12006-12-19 00:23:12 -0800285 goto prune;
286
Brandon Casey2b81fab2008-02-22 12:56:50 -0600287 if (cb->cmd->rewrite)
288 osha1 = cb->last_kept_sha1;
289
Junio C Hamano9bbaa6c2007-01-11 19:56:43 -0800290 old = new = NULL;
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800291 if (cb->cmd->stalefix &&
292 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
Junio C Hamano4264dc12006-12-19 00:23:12 -0800293 goto prune;
294
Junio C Hamano9bbaa6c2007-01-11 19:56:43 -0800295 if (timestamp < cb->cmd->expire_unreachable) {
296 if (!cb->ref_commit)
297 goto prune;
Linus Torvalds666e07e2009-03-31 09:45:22 -0700298 if (unreachable(cb, old, osha1) || unreachable(cb, new, nsha1))
Junio C Hamano9bbaa6c2007-01-11 19:56:43 -0800299 goto prune;
300 }
Junio C Hamano4264dc12006-12-19 00:23:12 -0800301
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100302 if (cb->cmd->recno && --(cb->cmd->recno) == 0)
303 goto prune;
304
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100305 if (cb->newlog) {
306 char sign = (tz < 0) ? '-' : '+';
307 int zone = (tz < 0) ? (-tz) : tz;
308 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
309 sha1_to_hex(osha1), sha1_to_hex(nsha1),
310 email, timestamp, sign, zone,
311 message);
Brandon Casey2b81fab2008-02-22 12:56:50 -0600312 hashcpy(cb->last_kept_sha1, nsha1);
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100313 }
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800314 if (cb->cmd->verbose)
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100315 printf("keep %s", message);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800316 return 0;
317 prune:
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800318 if (!cb->newlog || cb->cmd->verbose)
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100319 printf("%sprune %s", cb->newlog ? "" : "would ", message);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800320 return 0;
321}
322
Junio C Hamano4264dc12006-12-19 00:23:12 -0800323static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
324{
325 struct cmd_reflog_expire_cb *cmd = cb_data;
326 struct expire_reflog_cb cb;
327 struct ref_lock *lock;
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -0500328 char *log_file, *newlog_path = NULL;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800329 int status = 0;
330
Junio C Hamano4264dc12006-12-19 00:23:12 -0800331 memset(&cb, 0, sizeof(cb));
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700332
333 /*
334 * we take the lock for the ref itself to prevent it from
Junio C Hamano4264dc12006-12-19 00:23:12 -0800335 * getting updated.
336 */
Sven Verdoolaege68db31c2007-05-09 12:33:20 +0200337 lock = lock_any_ref_for_update(ref, sha1, 0);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800338 if (!lock)
339 return error("cannot lock ref '%s'", ref);
Alex Riesena4f34cb2008-10-27 11:22:09 +0100340 log_file = git_pathdup("logs/%s", ref);
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -0500341 if (!file_exists(log_file))
Junio C Hamano4264dc12006-12-19 00:23:12 -0800342 goto finish;
343 if (!cmd->dry_run) {
Alex Riesena4f34cb2008-10-27 11:22:09 +0100344 newlog_path = git_pathdup("logs/%s.lock", ref);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800345 cb.newlog = fopen(newlog_path, "w");
346 }
347
348 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800349 cb.ref = ref;
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800350 cb.cmd = cmd;
Junio Hamano494fbfe2009-03-30 21:34:14 -0700351 if (cb.ref_commit)
352 mark_reachable(cb.ref_commit, cmd->expire_total);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800353 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
Junio Hamano494fbfe2009-03-30 21:34:14 -0700354 if (cb.ref_commit)
355 clear_commit_marks(cb.ref_commit, REACHABLE);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800356 finish:
357 if (cb.newlog) {
Brandon Casey4cd883d2008-02-22 12:47:08 -0600358 if (fclose(cb.newlog)) {
Junio C Hamano4264dc12006-12-19 00:23:12 -0800359 status |= error("%s: %s", strerror(errno),
360 newlog_path);
Brandon Casey4cd883d2008-02-22 12:47:08 -0600361 unlink(newlog_path);
Brandon Casey55f10562008-02-22 13:04:12 -0600362 } else if (cmd->updateref &&
363 (write_in_full(lock->lock_fd,
364 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
Jim Meyering2b7ca832009-09-12 10:54:32 +0200365 write_str_in_full(lock->lock_fd, "\n") != 1 ||
Brandon Casey55f10562008-02-22 13:04:12 -0600366 close_ref(lock) < 0)) {
367 status |= error("Couldn't write %s",
368 lock->lk->filename);
369 unlink(newlog_path);
Brandon Casey4cd883d2008-02-22 12:47:08 -0600370 } else if (rename(newlog_path, log_file)) {
Junio C Hamano4264dc12006-12-19 00:23:12 -0800371 status |= error("cannot rename %s to %s",
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -0500372 newlog_path, log_file);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800373 unlink(newlog_path);
Brandon Casey55f10562008-02-22 13:04:12 -0600374 } else if (cmd->updateref && commit_ref(lock)) {
375 status |= error("Couldn't set %s", lock->ref_name);
Pierre Habouzit336d09d2008-06-15 23:37:42 +0200376 } else {
377 adjust_shared_perm(log_file);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800378 }
379 }
380 free(newlog_path);
Nicolas Pitre9a13f0b2007-01-26 17:26:05 -0500381 free(log_file);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800382 unlock_ref(lock);
383 return status;
384}
385
Junio C Hamanobda3a312008-01-25 23:53:05 -0800386static int collect_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
387{
388 struct collected_reflog *e;
389 struct collect_reflog_cb *cb = cb_data;
390 size_t namelen = strlen(ref);
391
392 e = xmalloc(sizeof(*e) + namelen + 1);
393 hashcpy(e->sha1, sha1);
394 memcpy(e->reflog, ref, namelen + 1);
395 ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
396 cb->e[cb->nr++] = e;
397 return 0;
398}
399
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700400static struct reflog_expire_cfg {
401 struct reflog_expire_cfg *next;
402 unsigned long expire_total;
403 unsigned long expire_unreachable;
404 size_t len;
405 char pattern[FLEX_ARRAY];
406} *reflog_expire_cfg, **reflog_expire_cfg_tail;
407
408static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
409{
410 struct reflog_expire_cfg *ent;
411
412 if (!reflog_expire_cfg_tail)
413 reflog_expire_cfg_tail = &reflog_expire_cfg;
414
415 for (ent = reflog_expire_cfg; ent; ent = ent->next)
416 if (ent->len == len &&
417 !memcmp(ent->pattern, pattern, len))
418 return ent;
419
420 ent = xcalloc(1, (sizeof(*ent) + len));
421 memcpy(ent->pattern, pattern, len);
422 ent->len = len;
423 *reflog_expire_cfg_tail = ent;
424 reflog_expire_cfg_tail = &(ent->next);
425 return ent;
426}
427
428static int parse_expire_cfg_value(const char *var, const char *value, unsigned long *expire)
429{
430 if (!value)
431 return config_error_nonbool(var);
432 if (!strcmp(value, "never") || !strcmp(value, "false")) {
433 *expire = 0;
434 return 0;
435 }
436 *expire = approxidate(value);
437 return 0;
438}
439
440/* expiry timer slot */
441#define EXPIRE_TOTAL 01
442#define EXPIRE_UNREACH 02
443
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100444static int reflog_expire_config(const char *var, const char *value, void *cb)
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800445{
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700446 const char *lastdot = strrchr(var, '.');
447 unsigned long expire;
448 int slot;
449 struct reflog_expire_cfg *ent;
450
451 if (!lastdot || prefixcmp(var, "gc."))
452 return git_default_config(var, value, cb);
453
454 if (!strcmp(lastdot, ".reflogexpire")) {
455 slot = EXPIRE_TOTAL;
456 if (parse_expire_cfg_value(var, value, &expire))
457 return -1;
458 } else if (!strcmp(lastdot, ".reflogexpireunreachable")) {
459 slot = EXPIRE_UNREACH;
460 if (parse_expire_cfg_value(var, value, &expire))
461 return -1;
462 } else
463 return git_default_config(var, value, cb);
464
465 if (lastdot == var + 2) {
466 switch (slot) {
467 case EXPIRE_TOTAL:
468 default_reflog_expire = expire;
469 break;
470 case EXPIRE_UNREACH:
471 default_reflog_expire_unreachable = expire;
472 break;
473 }
Junio C Hamano4f342b92008-02-11 10:50:06 -0800474 return 0;
475 }
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700476
477 ent = find_cfg_ent(var + 3, lastdot - (var+3));
478 if (!ent)
479 return -1;
480 switch (slot) {
481 case EXPIRE_TOTAL:
482 ent->expire_total = expire;
483 break;
484 case EXPIRE_UNREACH:
485 ent->expire_unreachable = expire;
486 break;
Junio C Hamano4f342b92008-02-11 10:50:06 -0800487 }
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700488 return 0;
489}
490
491static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
492{
493 struct reflog_expire_cfg *ent;
494
495 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
496 return; /* both given explicitly -- nothing to tweak */
497
498 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
499 if (!fnmatch(ent->pattern, ref, 0)) {
500 if (!(slot & EXPIRE_TOTAL))
501 cb->expire_total = ent->expire_total;
502 if (!(slot & EXPIRE_UNREACH))
503 cb->expire_unreachable = ent->expire_unreachable;
504 return;
505 }
506 }
507
Junio C Hamano60bce2b2008-06-28 22:24:49 -0700508 /*
509 * If unconfigured, make stash never expire
510 */
511 if (!strcmp(ref, "refs/stash")) {
512 if (!(slot & EXPIRE_TOTAL))
513 cb->expire_total = 0;
514 if (!(slot & EXPIRE_UNREACH))
515 cb->expire_unreachable = 0;
516 return;
517 }
518
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700519 /* Nothing matched -- use the default value */
520 if (!(slot & EXPIRE_TOTAL))
521 cb->expire_total = default_reflog_expire;
522 if (!(slot & EXPIRE_UNREACH))
523 cb->expire_unreachable = default_reflog_expire_unreachable;
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800524}
525
Junio C Hamano4264dc12006-12-19 00:23:12 -0800526static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
527{
528 struct cmd_reflog_expire_cb cb;
529 unsigned long now = time(NULL);
530 int i, status, do_all;
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700531 int explicit_expiry = 0;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800532
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100533 git_config(reflog_expire_config, NULL);
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800534
Junio C Hamano4264dc12006-12-19 00:23:12 -0800535 save_commit_buffer = 0;
536 do_all = status = 0;
537 memset(&cb, 0, sizeof(cb));
Junio C Hamano4aec56d2006-12-27 01:47:57 -0800538
539 if (!default_reflog_expire_unreachable)
540 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
541 if (!default_reflog_expire)
542 default_reflog_expire = now - 90 * 24 * 3600;
543 cb.expire_total = default_reflog_expire;
544 cb.expire_unreachable = default_reflog_expire_unreachable;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800545
546 for (i = 1; i < argc; i++) {
547 const char *arg = argv[i];
548 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
549 cb.dry_run = 1;
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700550 else if (!prefixcmp(arg, "--expire=")) {
Junio C Hamano4264dc12006-12-19 00:23:12 -0800551 cb.expire_total = approxidate(arg + 9);
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700552 explicit_expiry |= EXPIRE_TOTAL;
553 }
554 else if (!prefixcmp(arg, "--expire-unreachable=")) {
Junio C Hamano4264dc12006-12-19 00:23:12 -0800555 cb.expire_unreachable = approxidate(arg + 21);
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700556 explicit_expiry |= EXPIRE_UNREACH;
557 }
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800558 else if (!strcmp(arg, "--stale-fix"))
559 cb.stalefix = 1;
Brandon Casey2b81fab2008-02-22 12:56:50 -0600560 else if (!strcmp(arg, "--rewrite"))
561 cb.rewrite = 1;
Brandon Casey55f10562008-02-22 13:04:12 -0600562 else if (!strcmp(arg, "--updateref"))
563 cb.updateref = 1;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800564 else if (!strcmp(arg, "--all"))
565 do_all = 1;
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800566 else if (!strcmp(arg, "--verbose"))
567 cb.verbose = 1;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800568 else if (!strcmp(arg, "--")) {
569 i++;
570 break;
571 }
572 else if (arg[0] == '-')
573 usage(reflog_expire_usage);
574 else
575 break;
576 }
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700577
578 /*
579 * We can trust the commits and objects reachable from refs
580 * even in older repository. We cannot trust what's reachable
581 * from reflog if the repository was pruned with older git.
582 */
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800583 if (cb.stalefix) {
584 init_revisions(&cb.revs, prefix);
585 if (cb.verbose)
586 printf("Marking reachable objects...");
587 mark_reachable_objects(&cb.revs, 0);
588 if (cb.verbose)
589 putchar('\n');
590 }
591
Junio C Hamanobda3a312008-01-25 23:53:05 -0800592 if (do_all) {
593 struct collect_reflog_cb collected;
594 int i;
595
596 memset(&collected, 0, sizeof(collected));
597 for_each_reflog(collect_reflog, &collected);
598 for (i = 0; i < collected.nr; i++) {
599 struct collected_reflog *e = collected.e[i];
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700600 set_reflog_expiry_param(&cb, explicit_expiry, e->reflog);
Junio C Hamanobda3a312008-01-25 23:53:05 -0800601 status |= expire_reflog(e->reflog, e->sha1, 0, &cb);
602 free(e);
603 }
604 free(collected.e);
605 }
606
Pieter de Bie90fb46e2008-08-10 22:22:21 +0200607 for (; i < argc; i++) {
608 char *ref;
Junio C Hamano4264dc12006-12-19 00:23:12 -0800609 unsigned char sha1[20];
Pieter de Bie90fb46e2008-08-10 22:22:21 +0200610 if (!dwim_log(argv[i], strlen(argv[i]), sha1, &ref)) {
611 status |= error("%s points nowhere!", argv[i]);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800612 continue;
613 }
Junio C Hamano3cb22b82008-06-15 23:48:46 -0700614 set_reflog_expiry_param(&cb, explicit_expiry, ref);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800615 status |= expire_reflog(ref, sha1, 0, &cb);
616 }
617 return status;
618}
619
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100620static int count_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
621 const char *email, unsigned long timestamp, int tz,
622 const char *message, void *cb_data)
623{
624 struct cmd_reflog_expire_cb *cb = cb_data;
625 if (!cb->expire_total || timestamp < cb->expire_total)
626 cb->recno++;
627 return 0;
628}
629
630static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
631{
632 struct cmd_reflog_expire_cb cb;
633 int i, status = 0;
634
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100635 memset(&cb, 0, sizeof(cb));
636
637 for (i = 1; i < argc; i++) {
Brandon Casey3c386aa2008-02-22 15:08:59 -0600638 const char *arg = argv[i];
639 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
640 cb.dry_run = 1;
Brandon Casey2b81fab2008-02-22 12:56:50 -0600641 else if (!strcmp(arg, "--rewrite"))
642 cb.rewrite = 1;
Brandon Casey55f10562008-02-22 13:04:12 -0600643 else if (!strcmp(arg, "--updateref"))
644 cb.updateref = 1;
Brandon Casey3c386aa2008-02-22 15:08:59 -0600645 else if (!strcmp(arg, "--verbose"))
646 cb.verbose = 1;
647 else if (!strcmp(arg, "--")) {
648 i++;
649 break;
650 }
651 else if (arg[0] == '-')
652 usage(reflog_delete_usage);
653 else
654 break;
655 }
656
657 if (argc - i < 1)
658 return error("Nothing to delete?");
659
660 for ( ; i < argc; i++) {
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100661 const char *spec = strstr(argv[i], "@{");
662 unsigned char sha1[20];
663 char *ep, *ref;
664 int recno;
665
666 if (!spec) {
Brandon Caseycb97cc92008-01-04 19:11:37 -0600667 status |= error("Not a reflog: %s", argv[i]);
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100668 continue;
669 }
670
Junio C Hamano55beff42008-08-10 23:21:25 -0700671 if (!dwim_log(argv[i], spec - argv[i], sha1, &ref)) {
672 status |= error("no reflog for '%s'", argv[i]);
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100673 continue;
674 }
675
676 recno = strtoul(spec + 2, &ep, 10);
677 if (*ep == '}') {
678 cb.recno = -recno;
679 for_each_reflog_ent(ref, count_reflog_ent, &cb);
680 } else {
681 cb.expire_total = approxidate(spec + 2);
682 for_each_reflog_ent(ref, count_reflog_ent, &cb);
683 cb.expire_total = 0;
684 }
685
686 status |= expire_reflog(ref, sha1, 0, &cb);
687 free(ref);
688 }
689 return status;
690}
691
Junio C Hamano1389d9d2007-01-06 02:16:19 -0800692/*
693 * main "reflog"
694 */
695
Junio C Hamano4264dc12006-12-19 00:23:12 -0800696static const char reflog_usage[] =
Matthieu Moye77095e2009-08-05 17:36:28 +0200697"git reflog [ show | expire | delete ]";
Junio C Hamano4264dc12006-12-19 00:23:12 -0800698
699int cmd_reflog(int argc, const char **argv, const char *prefix)
700{
Linus Torvaldscf39f542007-02-08 09:51:56 -0800701 /* With no command, we default to showing it. */
702 if (argc < 2 || *argv[1] == '-')
703 return cmd_log_reflog(argc, argv, prefix);
704
705 if (!strcmp(argv[1], "show"))
706 return cmd_log_reflog(argc - 1, argv + 1, prefix);
707
708 if (!strcmp(argv[1], "expire"))
Junio C Hamano4264dc12006-12-19 00:23:12 -0800709 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
Linus Torvaldscf39f542007-02-08 09:51:56 -0800710
Johannes Schindelin552cecc2007-10-17 02:50:45 +0100711 if (!strcmp(argv[1], "delete"))
712 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
713
Linus Torvaldscf39f542007-02-08 09:51:56 -0800714 /* Not a recognized reflog command..*/
715 usage(reflog_usage);
Junio C Hamano4264dc12006-12-19 00:23:12 -0800716}