Code

1da7da0916af92eed0a3168036175376f0ac0c0a
[git.git] / builtin-reflog.c
1 #include "cache.h"
2 #include "builtin.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "dir.h"
6 #include "tree-walk.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "reachable.h"
11 /*
12  * reflog expire
13  */
15 static const char reflog_expire_usage[] =
16 "git-reflog expire [--verbose] [--dry-run] [--fix-stale] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
18 static unsigned long default_reflog_expire;
19 static unsigned long default_reflog_expire_unreachable;
21 struct 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 };
30 struct expire_reflog_cb {
31         FILE *newlog;
32         const char *ref;
33         struct commit *ref_commit;
34         struct cmd_reflog_expire_cb *cmd;
35 };
37 static int tree_is_complete(const unsigned char *sha1)
38 {
39         struct tree_desc desc;
40         void *buf;
41         char type[20];
43         buf = read_sha1_file(sha1, type, &desc.size);
44         if (!buf)
45                 return 0;
46         desc.buf = buf;
47         while (desc.size) {
48                 const unsigned char *elem;
49                 const char *name;
50                 unsigned mode;
52                 elem = tree_entry_extract(&desc, &name, &mode);
53                 if (!has_sha1_file(elem) ||
54                     (S_ISDIR(mode) && !tree_is_complete(elem))) {
55                         free(buf);
56                         return 0;
57                 }
58                 update_tree_entry(&desc);
59         }
60         free(buf);
61         return 1;
62 }
64 #define INCOMPLETE      (1u<<10)
65 #define STUDYING        (1u<<11)
67 static int commit_is_complete(struct commit *commit)
68 {
69         struct object_array study;
70         struct object_array found;
71         int is_incomplete = 0;
72         int i;
74         /* early return */
75         if (commit->object.flags & SEEN)
76                 return 1;
77         if (commit->object.flags & INCOMPLETE)
78                 return 0;
79         /*
80          * Find all commits that are reachable and are not marked as
81          * SEEN.  Then make sure the trees and blobs contained are
82          * complete.  After that, mark these commits also as SEEN.
83          * If some of the objects that are needed to complete this
84          * commit are missing, mark this commit as INCOMPLETE.
85          */
86         memset(&study, 0, sizeof(study));
87         memset(&found, 0, sizeof(found));
88         add_object_array(&commit->object, NULL, &study);
89         add_object_array(&commit->object, NULL, &found);
90         commit->object.flags |= STUDYING;
91         while (study.nr) {
92                 struct commit *c;
93                 struct commit_list *parent;
95                 c = (struct commit *)study.objects[--study.nr].item;
96                 if (!c->object.parsed && !parse_object(c->object.sha1))
97                         c->object.flags |= INCOMPLETE;
99                 if (c->object.flags & INCOMPLETE) {
100                         is_incomplete = 1;
101                         break;
102                 }
103                 else if (c->object.flags & SEEN)
104                         continue;
105                 for (parent = c->parents; parent; parent = parent->next) {
106                         struct commit *p = parent->item;
107                         if (p->object.flags & STUDYING)
108                                 continue;
109                         p->object.flags |= STUDYING;
110                         add_object_array(&p->object, NULL, &study);
111                         add_object_array(&p->object, NULL, &found);
112                 }
113         }
114         if (!is_incomplete) {
115                 /* make sure all commits in found have all the
116                  * necessary objects.
117                  */
118                 for (i = 0; !is_incomplete && i < found.nr; i++) {
119                         struct commit *c =
120                                 (struct commit *)found.objects[i].item;
121                         if (!tree_is_complete(c->tree->object.sha1))
122                                 is_incomplete = 1;
123                 }
124                 if (!is_incomplete) {
125                         /* mark all found commits as complete, iow SEEN */
126                         for (i = 0; i < found.nr; i++)
127                                 found.objects[i].item->flags |= SEEN;
128                 }
129         }
130         /* clear flags from the objects we traversed */
131         for (i = 0; i < found.nr; i++)
132                 found.objects[i].item->flags &= ~STUDYING;
133         if (is_incomplete)
134                 commit->object.flags |= INCOMPLETE;
135         /* free object arrays */
136         free(study.objects);
137         free(found.objects);
138         return !is_incomplete;
141 static int keep_entry(struct commit **it, unsigned char *sha1)
143         struct commit *commit;
145         *it = NULL;
146         if (is_null_sha1(sha1))
147                 return 1;
148         commit = lookup_commit_reference_gently(sha1, 1);
149         if (!commit)
150                 return 0;
152         /*
153          * Make sure everything in this commit exists.
154          *
155          * We have walked all the objects reachable from the refs
156          * and cache earlier.  The commits reachable by this commit
157          * must meet SEEN commits -- and then we should mark them as
158          * SEEN as well.
159          */
160         if (!commit_is_complete(commit))
161                 return 0;
162         *it = commit;
163         return 1;
166 static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
167                              char *data, void *cb_data)
169         struct expire_reflog_cb *cb = cb_data;
170         unsigned long timestamp;
171         char *cp, *ep;
172         struct commit *old, *new;
174         cp = strchr(data, '>');
175         if (!cp || *++cp != ' ')
176                 goto prune;
177         timestamp = strtoul(cp, &ep, 10);
178         if (*ep != ' ')
179                 goto prune;
180         if (timestamp < cb->cmd->expire_total)
181                 goto prune;
183         if (cb->cmd->stalefix &&
184             (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1)))
185                 goto prune;
187         if ((timestamp < cb->cmd->expire_unreachable) &&
188             (!cb->ref_commit ||
189              (old && !in_merge_bases(old, cb->ref_commit)) ||
190              (new && !in_merge_bases(new, cb->ref_commit))))
191                 goto prune;
193         if (cb->newlog)
194                 fprintf(cb->newlog, "%s %s %s",
195                         sha1_to_hex(osha1), sha1_to_hex(nsha1), data);
196         if (cb->cmd->verbose)
197                 printf("keep %s", data);
198         return 0;
199  prune:
200         if (!cb->newlog || cb->cmd->verbose)
201                 printf("%sprune %s", cb->newlog ? "" : "would ", data);
202         return 0;
205 static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data)
207         struct cmd_reflog_expire_cb *cmd = cb_data;
208         struct expire_reflog_cb cb;
209         struct ref_lock *lock;
210         char *newlog_path = NULL;
211         int status = 0;
213         if (strncmp(ref, "refs/", 5))
214                 return error("not a ref '%s'", ref);
216         memset(&cb, 0, sizeof(cb));
217         /* we take the lock for the ref itself to prevent it from
218          * getting updated.
219          */
220         lock = lock_ref_sha1(ref + 5, sha1);
221         if (!lock)
222                 return error("cannot lock ref '%s'", ref);
223         if (!file_exists(lock->log_file))
224                 goto finish;
225         if (!cmd->dry_run) {
226                 newlog_path = xstrdup(git_path("logs/%s.lock", ref));
227                 cb.newlog = fopen(newlog_path, "w");
228         }
230         cb.ref_commit = lookup_commit_reference_gently(sha1, 1);
231         if (!cb.ref_commit)
232                 fprintf(stderr,
233                         "warning: ref '%s' does not point at a commit\n", ref);
234         cb.ref = ref;
235         cb.cmd = cmd;
236         for_each_reflog_ent(ref, expire_reflog_ent, &cb);
237  finish:
238         if (cb.newlog) {
239                 if (fclose(cb.newlog))
240                         status |= error("%s: %s", strerror(errno),
241                                         newlog_path);
242                 if (rename(newlog_path, lock->log_file)) {
243                         status |= error("cannot rename %s to %s",
244                                         newlog_path, lock->log_file);
245                         unlink(newlog_path);
246                 }
247         }
248         free(newlog_path);
249         unlock_ref(lock);
250         return status;
253 static int reflog_expire_config(const char *var, const char *value)
255         if (!strcmp(var, "gc.reflogexpire"))
256                 default_reflog_expire = approxidate(value);
257         else if (!strcmp(var, "gc.reflogexpireunreachable"))
258                 default_reflog_expire_unreachable = approxidate(value);
259         else
260                 return git_default_config(var, value);
261         return 0;
264 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
266         struct cmd_reflog_expire_cb cb;
267         unsigned long now = time(NULL);
268         int i, status, do_all;
270         git_config(reflog_expire_config);
272         save_commit_buffer = 0;
273         do_all = status = 0;
274         memset(&cb, 0, sizeof(cb));
276         if (!default_reflog_expire_unreachable)
277                 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
278         if (!default_reflog_expire)
279                 default_reflog_expire = now - 90 * 24 * 3600;
280         cb.expire_total = default_reflog_expire;
281         cb.expire_unreachable = default_reflog_expire_unreachable;
283         /*
284          * We can trust the commits and objects reachable from refs
285          * even in older repository.  We cannot trust what's reachable
286          * from reflog if the repository was pruned with older git.
287          */
289         for (i = 1; i < argc; i++) {
290                 const char *arg = argv[i];
291                 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
292                         cb.dry_run = 1;
293                 else if (!strncmp(arg, "--expire=", 9))
294                         cb.expire_total = approxidate(arg + 9);
295                 else if (!strncmp(arg, "--expire-unreachable=", 21))
296                         cb.expire_unreachable = approxidate(arg + 21);
297                 else if (!strcmp(arg, "--stale-fix"))
298                         cb.stalefix = 1;
299                 else if (!strcmp(arg, "--all"))
300                         do_all = 1;
301                 else if (!strcmp(arg, "--verbose"))
302                         cb.verbose = 1;
303                 else if (!strcmp(arg, "--")) {
304                         i++;
305                         break;
306                 }
307                 else if (arg[0] == '-')
308                         usage(reflog_expire_usage);
309                 else
310                         break;
311         }
312         if (cb.stalefix) {
313                 init_revisions(&cb.revs, prefix);
314                 if (cb.verbose)
315                         printf("Marking reachable objects...");
316                 mark_reachable_objects(&cb.revs, 0);
317                 if (cb.verbose)
318                         putchar('\n');
319         }
321         if (do_all)
322                 status |= for_each_ref(expire_reflog, &cb);
323         while (i < argc) {
324                 const char *ref = argv[i++];
325                 unsigned char sha1[20];
326                 if (!resolve_ref(ref, sha1, 1, NULL)) {
327                         status |= error("%s points nowhere!", ref);
328                         continue;
329                 }
330                 status |= expire_reflog(ref, sha1, 0, &cb);
331         }
332         return status;
335 /*
336  * main "reflog"
337  */
339 static const char reflog_usage[] =
340 "git-reflog (expire | ...)";
342 int cmd_reflog(int argc, const char **argv, const char *prefix)
344         if (argc < 2)
345                 usage(reflog_usage);
346         else if (!strcmp(argv[1], "expire"))
347                 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
348         else
349                 usage(reflog_usage);