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