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