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