]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-prune.c
Merge branch 'jc/reflog-expire' into maint
[thirdparty/git.git] / builtin-prune.c
CommitLineData
ba84a797 1#include "cache.h"
ba84a797 2#include "commit.h"
ba84a797
LT
3#include "diff.h"
4#include "revision.h"
5#include "builtin.h"
94421474 6#include "reachable.h"
629de472 7#include "parse-options.h"
ba84a797 8
629de472
MB
9static const char * const prune_usage[] = {
10 "git-prune [-n] [--expire <time>] [--] [<head>...]",
11 NULL
12};
96f1e58f 13static int show_only;
f01913e4 14static unsigned long expire;
ba84a797
LT
15
16static int prune_object(char *path, const char *filename, const unsigned char *sha1)
17{
f01913e4
JS
18 const char *fullpath = mkpath("%s/%s", path, filename);
19 if (expire) {
20 struct stat st;
21 if (lstat(fullpath, &st))
22 return error("Could not stat '%s'", fullpath);
23 if (st.st_mtime > expire)
24 return 0;
25 }
ba84a797 26 if (show_only) {
21666f1a
NP
27 enum object_type type = sha1_object_info(sha1, NULL);
28 printf("%s %s\n", sha1_to_hex(sha1),
29 (type > 0) ? typename(type) : "unknown");
3254d218 30 } else
f01913e4 31 unlink(fullpath);
ba84a797
LT
32 return 0;
33}
34
35static int prune_dir(int i, char *path)
36{
37 DIR *dir = opendir(path);
38 struct dirent *de;
39
40 if (!dir)
41 return 0;
42
43 while ((de = readdir(dir)) != NULL) {
44 char name[100];
45 unsigned char sha1[20];
46 int len = strlen(de->d_name);
47
48 switch (len) {
49 case 2:
50 if (de->d_name[1] != '.')
51 break;
52 case 1:
53 if (de->d_name[0] != '.')
54 break;
55 continue;
56 case 38:
57 sprintf(name, "%02x", i);
58 memcpy(name+2, de->d_name, len+1);
59 if (get_sha1_hex(name, sha1) < 0)
60 break;
61
62 /*
63 * Do we know about this object?
64 * It must have been reachable
65 */
66 if (lookup_object(sha1))
67 continue;
68
69 prune_object(path, de->d_name, sha1);
70 continue;
71 }
72 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
73 }
3254d218
NP
74 if (!show_only)
75 rmdir(path);
ba84a797
LT
76 closedir(dir);
77 return 0;
78}
79
80static void prune_object_dir(const char *path)
81{
82 int i;
83 for (i = 0; i < 256; i++) {
84 static char dir[4096];
85 sprintf(dir, "%s/%02x", path, i);
86 prune_dir(i, dir);
87 }
88}
89
8464010f
DST
90/*
91 * Write errors (particularly out of space) can result in
92 * failed temporary packs (and more rarely indexes and other
93 * files begining with "tmp_") accumulating in the
94 * object directory.
95 */
96static void remove_temporary_files(void)
97{
98 DIR *dir;
99 struct dirent *de;
100 char* dirname=get_object_directory();
101
102 dir = opendir(dirname);
103 if (!dir) {
104 fprintf(stderr, "Unable to open object directory %s\n",
105 dirname);
106 return;
107 }
108 while ((de = readdir(dir)) != NULL) {
109 if (!prefixcmp(de->d_name, "tmp_")) {
110 char name[PATH_MAX];
111 int c = snprintf(name, PATH_MAX, "%s/%s",
112 dirname, de->d_name);
113 if (c < 0 || c >= PATH_MAX)
114 continue;
115 if (expire) {
116 struct stat st;
117 if (stat(name, &st) != 0 || st.st_mtime >= expire)
118 continue;
119 }
120 printf("Removing stale temporary file %s\n", name);
121 if (!show_only)
122 unlink(name);
123 }
124 }
125 closedir(dir);
126}
127
a633fca0 128int cmd_prune(int argc, const char **argv, const char *prefix)
ba84a797 129{
24304816 130 struct rev_info revs;
629de472
MB
131 const struct option options[] = {
132 OPT_BOOLEAN('n', NULL, &show_only,
133 "do not remove, show only"),
134 OPT_DATE(0, "expire", &expire,
135 "expire objects older than <time>"),
136 OPT_END()
137 };
ba84a797 138
16157b80 139 save_commit_buffer = 0;
a633fca0 140 init_revisions(&revs, prefix);
ba84a797 141
629de472 142 argc = parse_options(argc, argv, options, prune_usage, 0);
fe308f53
JH
143 while (argc--) {
144 unsigned char sha1[20];
145 const char *name = *argv++;
146
147 if (!get_sha1(name, sha1)) {
148 struct object *object = parse_object(sha1);
149 if (!object)
150 die("bad object: %s", name);
151 add_pending_object(&revs, object, "");
152 }
153 else
154 die("unrecognized argument: %s", name);
155 }
629de472 156 mark_reachable_objects(&revs, 1);
ba84a797
LT
157 prune_object_dir(get_object_directory());
158
2eb53e65 159 prune_packed_objects(show_only);
8464010f 160 remove_temporary_files();
ba84a797
LT
161 return 0;
162}