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