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