]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-reflog.c
Reduce memory usage of fast-import.
[thirdparty/git.git] / builtin-reflog.c
CommitLineData
4264dc15
JH
1#include "cache.h"
2#include "builtin.h"
3#include "commit.h"
4#include "refs.h"
5#include "dir.h"
8d8b9f62 6#include "tree-walk.h"
1389d9dd
JH
7#include "diff.h"
8#include "revision.h"
9#include "reachable.h"
10
11/*
12 * reflog expire
13 */
14
15static const char reflog_expire_usage[] =
15261e3b 16"git-reflog expire [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
4264dc15 17
4aec56d1
JH
18static unsigned long default_reflog_expire;
19static unsigned long default_reflog_expire_unreachable;
20
1389d9dd
JH
21struct cmd_reflog_expire_cb {
22 struct rev_info revs;
23 int dry_run;
24 int stalefix;
25 int verbose;
26 unsigned long expire_total;
27 unsigned long expire_unreachable;
28};
29
4264dc15
JH
30struct expire_reflog_cb {
31 FILE *newlog;
32 const char *ref;
33 struct commit *ref_commit;
1389d9dd 34 struct cmd_reflog_expire_cb *cmd;
4264dc15
JH
35};
36
cd1f9c36
JH
37#define INCOMPLETE (1u<<10)
38#define STUDYING (1u<<11)
39
8d8b9f62
JH
40static int tree_is_complete(const unsigned char *sha1)
41{
42 struct tree_desc desc;
cd1f9c36
JH
43 struct name_entry entry;
44 int complete;
45 struct tree *tree;
8d8b9f62 46
cd1f9c36
JH
47 tree = lookup_tree(sha1);
48 if (!tree)
8d8b9f62 49 return 0;
cd1f9c36
JH
50 if (tree->object.flags & SEEN)
51 return 1;
52 if (tree->object.flags & INCOMPLETE)
53 return 0;
54
55 desc.buf = tree->buffer;
56 desc.size = tree->size;
57 if (!desc.buf) {
58 char type[20];
59 void *data = read_sha1_file(sha1, type, &desc.size);
60 if (!data) {
61 tree->object.flags |= INCOMPLETE;
8d8b9f62
JH
62 return 0;
63 }
cd1f9c36
JH
64 desc.buf = data;
65 tree->buffer = data;
8d8b9f62 66 }
cd1f9c36
JH
67 complete = 1;
68 while (tree_entry(&desc, &entry)) {
69 if (!has_sha1_file(entry.sha1) ||
70 (S_ISDIR(entry.mode) && !tree_is_complete(entry.sha1))) {
71 tree->object.flags |= INCOMPLETE;
72 complete = 0;
73 }
74 }
75 free(tree->buffer);
76 tree->buffer = NULL;
8d8b9f62 77
cd1f9c36
JH
78 if (complete)
79 tree->object.flags |= SEEN;
80 return complete;
81}
1389d9dd
JH
82
83static int commit_is_complete(struct commit *commit)
84{
85 struct object_array study;
86 struct object_array found;
87 int is_incomplete = 0;
88 int i;
89
90 /* early return */
91 if (commit->object.flags & SEEN)
92 return 1;
93 if (commit->object.flags & INCOMPLETE)
94 return 0;
95 /*
96 * Find all commits that are reachable and are not marked as
97 * SEEN. Then make sure the trees and blobs contained are
98 * complete. After that, mark these commits also as SEEN.
99 * If some of the objects that are needed to complete this
100 * commit are missing, mark this commit as INCOMPLETE.
101 */
102 memset(&study, 0, sizeof(study));
103 memset(&found, 0, sizeof(found));
104 add_object_array(&commit->object, NULL, &study);
105 add_object_array(&commit->object, NULL, &found);
106 commit->object.flags |= STUDYING;
107 while (study.nr) {
108 struct commit *c;
109 struct commit_list *parent;
110
111 c = (struct commit *)study.objects[--study.nr].item;
112 if (!c->object.parsed && !parse_object(c->object.sha1))
113 c->object.flags |= INCOMPLETE;
114
115 if (c->object.flags & INCOMPLETE) {
116 is_incomplete = 1;
117 break;
118 }
119 else if (c->object.flags & SEEN)
120 continue;
121 for (parent = c->parents; parent; parent = parent->next) {
122 struct commit *p = parent->item;
123 if (p->object.flags & STUDYING)
124 continue;
125 p->object.flags |= STUDYING;
126 add_object_array(&p->object, NULL, &study);
127 add_object_array(&p->object, NULL, &found);
128 }
129 }
130 if (!is_incomplete) {
cd1f9c36
JH
131 /*
132 * make sure all commits in "found" array have all the
1389d9dd
JH
133 * necessary objects.
134 */
cd1f9c36 135 for (i = 0; i < found.nr; i++) {
1389d9dd
JH
136 struct commit *c =
137 (struct commit *)found.objects[i].item;
cd1f9c36 138 if (!tree_is_complete(c->tree->object.sha1)) {
1389d9dd 139 is_incomplete = 1;
cd1f9c36
JH
140 c->object.flags |= INCOMPLETE;
141 }
1389d9dd
JH
142 }
143 if (!is_incomplete) {
144 /* mark all found commits as complete, iow SEEN */
145 for (i = 0; i < found.nr; i++)
146 found.objects[i].item->flags |= SEEN;
147 }
148 }
149 /* clear flags from the objects we traversed */
150 for (i = 0; i < found.nr; i++)
151 found.objects[i].item->flags &= ~STUDYING;
152 if (is_incomplete)
153 commit->object.flags |= INCOMPLETE;
cd1f9c36
JH
154 else {
155 /*
156 * If we come here, we have (1) traversed the ancestry chain
157 * from the "commit" until we reach SEEN commits (which are
158 * known to be complete), and (2) made sure that the commits
159 * encountered during the above traversal refer to trees that
160 * are complete. Which means that we know *all* the commits
161 * we have seen during this process are complete.
162 */
163 for (i = 0; i < found.nr; i++)
164 found.objects[i].item->flags |= SEEN;
165 }
1389d9dd
JH
166 /* free object arrays */
167 free(study.objects);
168 free(found.objects);
169 return !is_incomplete;
170}
171
4264dc15
JH
172static int keep_entry(struct commit **it, unsigned char *sha1)
173{
8d8b9f62
JH
174 struct commit *commit;
175
4264dc15
JH
176 if (is_null_sha1(sha1))
177 return 1;
8d8b9f62
JH
178 commit = lookup_commit_reference_gently(sha1, 1);
179 if (!commit)
180 return 0;
181
1389d9dd
JH
182 /*
183 * Make sure everything in this commit exists.
184 *
185 * We have walked all the objects reachable from the refs
186 * and cache earlier. The commits reachable by this commit
187 * must meet SEEN commits -- and then we should mark them as
188 * SEEN as well.
189 */
190 if (!commit_is_complete(commit))
8d8b9f62
JH
191 return 0;
192 *it = commit;
193 return 1;
4264dc15
JH
194}
195
196static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
883d60fa
JS
197 const char *email, unsigned long timestamp, int tz,
198 const char *message, void *cb_data)
4264dc15
JH
199{
200 struct expire_reflog_cb *cb = cb_data;
4264dc15
JH
201 struct commit *old, *new;
202
1389d9dd 203 if (timestamp < cb->cmd->expire_total)
4264dc15
JH
204 goto prune;
205
9bbaa6cc 206 old = new = NULL;
1389d9dd
JH
207 if (cb->cmd->stalefix &&
208 (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
4264dc15
JH
209 goto prune;
210
9bbaa6cc
JH
211 if (timestamp < cb->cmd->expire_unreachable) {
212 if (!cb->ref_commit)
213 goto prune;
214 if (!old && !is_null_sha1(osha1))
215 old = lookup_commit_reference_gently(osha1, 1);
216 if (!new && !is_null_sha1(nsha1))
217 new = lookup_commit_reference_gently(nsha1, 1);
218 if ((old && !in_merge_bases(old, cb->ref_commit)) ||
219 (new && !in_merge_bases(new, cb->ref_commit)))
220 goto prune;
221 }
4264dc15 222
883d60fa
JS
223 if (cb->newlog) {
224 char sign = (tz < 0) ? '-' : '+';
225 int zone = (tz < 0) ? (-tz) : tz;
226 fprintf(cb->newlog, "%s %s %s %lu %c%04d\t%s",
227 sha1_to_hex(osha1), sha1_to_hex(nsha1),
228 email, timestamp, sign, zone,
229 message);
230 }
1389d9dd 231 if (cb->cmd->verbose)
883d60fa 232 printf("keep %s", message);
4264dc15
JH
233 return 0;
234 prune:
1389d9dd 235 if (!cb->newlog || cb->cmd->verbose)
883d60fa 236 printf("%sprune %s", cb->newlog ? "" : "would ", message);
4264dc15
JH
237 return 0;
238}
239
4264dc15
JH
240static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
241{
242 struct cmd_reflog_expire_cb *cmd = cb_data;
243 struct expire_reflog_cb cb;
244 struct ref_lock *lock;
245 char *newlog_path = NULL;
246 int status = 0;
247
248 if (strncmp(ref, "refs/", 5))
249 return error("not a ref '%s'", ref);
250
251 memset(&cb, 0, sizeof(cb));
252 /* we take the lock for the ref itself to prevent it from
253 * getting updated.
254 */
255 lock = lock_ref_sha1(ref + 5, sha1);
256 if (!lock)
257 return error("cannot lock ref '%s'", ref);
258 if (!file_exists(lock->log_file))
259 goto finish;
260 if (!cmd->dry_run) {
261 newlog_path = xstrdup(git_path("logs/%s.lock", ref));
262 cb.newlog = fopen(newlog_path, "w");
263 }
264
265 cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
4264dc15 266 cb.ref = ref;
1389d9dd 267 cb.cmd = cmd;
4264dc15
JH
268 for_each_reflog_ent(ref, expire_reflog_ent, &cb);
269 finish:
270 if (cb.newlog) {
271 if (fclose(cb.newlog))
272 status |= error("%s: %s", strerror(errno),
273 newlog_path);
274 if (rename(newlog_path, lock->log_file)) {
275 status |= error("cannot rename %s to %s",
276 newlog_path, lock->log_file);
277 unlink(newlog_path);
278 }
279 }
280 free(newlog_path);
281 unlock_ref(lock);
282 return status;
283}
284
4aec56d1
JH
285static int reflog_expire_config(const char *var, const char *value)
286{
287 if (!strcmp(var, "gc.reflogexpire"))
288 default_reflog_expire = approxidate(value);
289 else if (!strcmp(var, "gc.reflogexpireunreachable"))
290 default_reflog_expire_unreachable = approxidate(value);
291 else
292 return git_default_config(var, value);
293 return 0;
294}
295
4264dc15
JH
296static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
297{
298 struct cmd_reflog_expire_cb cb;
299 unsigned long now = time(NULL);
300 int i, status, do_all;
301
4aec56d1
JH
302 git_config(reflog_expire_config);
303
4264dc15
JH
304 save_commit_buffer = 0;
305 do_all = status = 0;
306 memset(&cb, 0, sizeof(cb));
4aec56d1
JH
307
308 if (!default_reflog_expire_unreachable)
309 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
310 if (!default_reflog_expire)
311 default_reflog_expire = now - 90 * 24 * 3600;
312 cb.expire_total = default_reflog_expire;
313 cb.expire_unreachable = default_reflog_expire_unreachable;
4264dc15 314
1389d9dd
JH
315 /*
316 * We can trust the commits and objects reachable from refs
317 * even in older repository. We cannot trust what's reachable
318 * from reflog if the repository was pruned with older git.
319 */
320
4264dc15
JH
321 for (i = 1; i < argc; i++) {
322 const char *arg = argv[i];
323 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
324 cb.dry_run = 1;
325 else if (!strncmp(arg, "--expire=", 9))
326 cb.expire_total = approxidate(arg + 9);
327 else if (!strncmp(arg, "--expire-unreachable=", 21))
328 cb.expire_unreachable = approxidate(arg + 21);
1389d9dd
JH
329 else if (!strcmp(arg, "--stale-fix"))
330 cb.stalefix = 1;
4264dc15
JH
331 else if (!strcmp(arg, "--all"))
332 do_all = 1;
1389d9dd
JH
333 else if (!strcmp(arg, "--verbose"))
334 cb.verbose = 1;
4264dc15
JH
335 else if (!strcmp(arg, "--")) {
336 i++;
337 break;
338 }
339 else if (arg[0] == '-')
340 usage(reflog_expire_usage);
341 else
342 break;
343 }
1389d9dd
JH
344 if (cb.stalefix) {
345 init_revisions(&cb.revs, prefix);
346 if (cb.verbose)
347 printf("Marking reachable objects...");
348 mark_reachable_objects(&cb.revs, 0);
349 if (cb.verbose)
350 putchar('\n');
351 }
352
4264dc15
JH
353 if (do_all)
354 status |= for_each_ref(expire_reflog, &cb);
355 while (i < argc) {
356 const char *ref = argv[i++];
357 unsigned char sha1[20];
358 if (!resolve_ref(ref, sha1, 1, NULL)) {
359 status |= error("%s points nowhere!", ref);
360 continue;
361 }
362 status |= expire_reflog(ref, sha1, 0, &cb);
363 }
364 return status;
365}
366
1389d9dd
JH
367/*
368 * main "reflog"
369 */
370
4264dc15
JH
371static const char reflog_usage[] =
372"git-reflog (expire | ...)";
373
374int cmd_reflog(int argc, const char **argv, const char *prefix)
375{
376 if (argc < 2)
377 usage(reflog_usage);
378 else if (!strcmp(argv[1], "expire"))
379 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
380 else
381 usage(reflog_usage);
382}