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