]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/prune.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / builtin / prune.c
CommitLineData
ba84a797 1#include "cache.h"
ba84a797 2#include "commit.h"
ba84a797 3#include "diff.h"
32a8f510 4#include "environment.h"
f394e093 5#include "gettext.h"
41771fa4 6#include "hex.h"
ba84a797
LT
7#include "revision.h"
8#include "builtin.h"
94421474 9#include "reachable.h"
629de472 10#include "parse-options.h"
dc347195 11#include "progress.h"
9460fd48 12#include "prune-packed.h"
cbeab747 13#include "replace-object.h"
dabab1d6 14#include "object-name.h"
cbd53a21 15#include "object-store.h"
120ad2b0 16#include "shallow.h"
ba84a797 17
629de472 18static const char * const prune_usage[] = {
1a1fc2d5 19 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
629de472
MB
20 NULL
21};
96f1e58f 22static int show_only;
b35ddf41 23static int verbose;
dddbad72 24static timestamp_t expire;
bf0a59b3 25static int show_progress = -1;
ba84a797 26
4454e9cb 27static int prune_tmp_file(const char *fullpath)
0e8316cc 28{
cbf731ed
AS
29 struct stat st;
30 if (lstat(fullpath, &st))
31 return error("Could not stat '%s'", fullpath);
32 if (st.st_mtime > expire)
33 return 0;
b3cecf49
NS
34 if (S_ISDIR(st.st_mode)) {
35 if (show_only || verbose)
36 printf("Removing stale temporary directory %s\n", fullpath);
37 if (!show_only) {
38 struct strbuf remove_dir_buf = STRBUF_INIT;
39
40 strbuf_addstr(&remove_dir_buf, fullpath);
41 remove_dir_recursively(&remove_dir_buf, 0);
42 strbuf_release(&remove_dir_buf);
43 }
44 } else {
45 if (show_only || verbose)
46 printf("Removing stale temporary file %s\n", fullpath);
47 if (!show_only)
48 unlink_or_warn(fullpath);
49 }
0e8316cc
BC
50 return 0;
51}
52
d55a30bb 53static void perform_reachability_traversal(struct rev_info *revs)
ba84a797 54{
d55a30bb
JK
55 static int initialized;
56 struct progress *progress = NULL;
57
58 if (initialized)
59 return;
60
61 if (show_progress)
62 progress = start_delayed_progress(_("Checking connectivity"), 0);
63 mark_reachable_objects(revs, 1, expire, progress);
64 stop_progress(&progress);
65 initialized = 1;
66}
67
68static int is_object_reachable(const struct object_id *oid,
69 struct rev_info *revs)
70{
c2bf473d
JK
71 struct object *obj;
72
d55a30bb 73 perform_reachability_traversal(revs);
27e1e22d 74
d0229abd 75 obj = lookup_object(the_repository, oid);
c2bf473d 76 return obj && (obj->flags & SEEN);
d55a30bb
JK
77}
78
79static int prune_object(const struct object_id *oid, const char *fullpath,
80 void *data)
81{
82 struct rev_info *revs = data;
83 struct stat st;
84
85 if (is_object_reachable(oid, revs))
27e1e22d
JK
86 return 0;
87
88 if (lstat(fullpath, &st)) {
89 /* report errors, but do not stop pruning */
90 error("Could not stat '%s'", fullpath);
91 return 0;
92 }
cbf731ed
AS
93 if (st.st_mtime > expire)
94 return 0;
b35ddf41 95 if (show_only || verbose) {
0df8e965
SB
96 enum object_type type = oid_object_info(the_repository, oid,
97 NULL);
76c1d9a0 98 printf("%s %s\n", oid_to_hex(oid),
debca9d2 99 (type > 0) ? type_name(type) : "unknown");
b35ddf41
MG
100 }
101 if (!show_only)
691f1a28 102 unlink_or_warn(fullpath);
ba84a797
LT
103 return 0;
104}
105
be252d33
JK
106static int prune_cruft(const char *basename, const char *path,
107 void *data UNUSED)
ba84a797 108{
27e1e22d
JK
109 if (starts_with(basename, "tmp_obj_"))
110 prune_tmp_file(path);
111 else
112 fprintf(stderr, "bad sha1 file: %s\n", path);
ba84a797
LT
113 return 0;
114}
115
be252d33
JK
116static int prune_subdir(unsigned int nr UNUSED, const char *path,
117 void *data UNUSED)
ba84a797 118{
27e1e22d
JK
119 if (!show_only)
120 rmdir(path);
121 return 0;
ba84a797
LT
122}
123
8464010f
DST
124/*
125 * Write errors (particularly out of space) can result in
126 * failed temporary packs (and more rarely indexes and other
9517e6b8 127 * files beginning with "tmp_") accumulating in the object
db87e396 128 * and the pack directories.
8464010f 129 */
db87e396 130static void remove_temporary_files(const char *path)
8464010f
DST
131{
132 DIR *dir;
133 struct dirent *de;
8464010f 134
db87e396 135 dir = opendir(path);
8464010f 136 if (!dir) {
69747653
EW
137 if (errno != ENOENT)
138 fprintf(stderr, "Unable to open directory %s: %s\n",
139 path, strerror(errno));
8464010f
DST
140 return;
141 }
0e8316cc 142 while ((de = readdir(dir)) != NULL)
59556548 143 if (starts_with(de->d_name, "tmp_"))
4454e9cb 144 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
8464010f
DST
145 closedir(dir);
146}
147
a633fca0 148int cmd_prune(int argc, const char **argv, const char *prefix)
ba84a797 149{
24304816 150 struct rev_info revs;
0c16cd49 151 int exclude_promisor_objects = 0;
629de472 152 const struct option options[] = {
8f5b7281
NTND
153 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
154 OPT__VERBOSE(&verbose, N_("report pruned objects")),
155 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
27ec394a
JH
156 OPT_EXPIRY_DATE(0, "expire", &expire,
157 N_("expire objects older than <time>")),
0c16cd49
JT
158 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
159 N_("limit traversal to objects outside promisor packfiles")),
629de472
MB
160 OPT_END()
161 };
db87e396 162 char *s;
ba84a797 163
dddbad72 164 expire = TIME_MAX;
16157b80 165 save_commit_buffer = 0;
6ebd1caf 166 read_replace_refs = 0;
2abf3503 167 repo_init_revisions(the_repository, &revs, prefix);
ba84a797 168
37782920 169 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
23af91d1 170
067fbd41
JK
171 if (repository_format_precious_objects)
172 die(_("cannot prune in a precious-objects repo"));
173
fe308f53 174 while (argc--) {
af6730e7 175 struct object_id oid;
fe308f53
JH
176 const char *name = *argv++;
177
d850b7a5 178 if (!repo_get_oid(the_repository, name, &oid)) {
c251c83d 179 struct object *object = parse_object_or_die(&oid,
180 name);
fe308f53
JH
181 add_pending_object(&revs, object, "");
182 }
183 else
184 die("unrecognized argument: %s", name);
185 }
bf0a59b3
JK
186
187 if (show_progress == -1)
188 show_progress = isatty(2);
0c16cd49
JT
189 if (exclude_promisor_objects) {
190 fetch_if_missing = 0;
191 revs.exclude_promisor_objects = 1;
192 }
bf0a59b3 193
27e1e22d 194 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
d55a30bb 195 prune_cruft, prune_subdir, &revs);
ba84a797 196
af0b4a3b 197 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
db87e396 198 remove_temporary_files(get_object_directory());
4e2d094d 199 s = mkpathdup("%s/pack", get_object_directory());
db87e396
BC
200 remove_temporary_files(s);
201 free(s);
eab3296c 202
d55a30bb
JK
203 if (is_repository_shallow(the_repository)) {
204 perform_reachability_traversal(&revs);
2588f6ed 205 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
d55a30bb 206 }
eab3296c 207
2108fe4a 208 release_revisions(&revs);
ba84a797
LT
209 return 0;
210}