]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/prune.c
Merge branch 'pb/ref-filter-with-crlf'
[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;
90b29cb7
BC
29 if (show_only || verbose)
30 printf("Removing stale temporary file %s\n", fullpath);
0e8316cc 31 if (!show_only)
691f1a28 32 unlink_or_warn(fullpath);
0e8316cc
BC
33 return 0;
34}
35
d55a30bb 36static void perform_reachability_traversal(struct rev_info *revs)
ba84a797 37{
d55a30bb
JK
38 static int initialized;
39 struct progress *progress = NULL;
40
41 if (initialized)
42 return;
43
44 if (show_progress)
45 progress = start_delayed_progress(_("Checking connectivity"), 0);
46 mark_reachable_objects(revs, 1, expire, progress);
47 stop_progress(&progress);
48 initialized = 1;
49}
50
51static int is_object_reachable(const struct object_id *oid,
52 struct rev_info *revs)
53{
c2bf473d
JK
54 struct object *obj;
55
d55a30bb 56 perform_reachability_traversal(revs);
27e1e22d 57
d0229abd 58 obj = lookup_object(the_repository, oid);
c2bf473d 59 return obj && (obj->flags & SEEN);
d55a30bb
JK
60}
61
62static int prune_object(const struct object_id *oid, const char *fullpath,
63 void *data)
64{
65 struct rev_info *revs = data;
66 struct stat st;
67
68 if (is_object_reachable(oid, revs))
27e1e22d
JK
69 return 0;
70
71 if (lstat(fullpath, &st)) {
72 /* report errors, but do not stop pruning */
73 error("Could not stat '%s'", fullpath);
74 return 0;
75 }
cbf731ed
AS
76 if (st.st_mtime > expire)
77 return 0;
b35ddf41 78 if (show_only || verbose) {
0df8e965
SB
79 enum object_type type = oid_object_info(the_repository, oid,
80 NULL);
76c1d9a0 81 printf("%s %s\n", oid_to_hex(oid),
debca9d2 82 (type > 0) ? type_name(type) : "unknown");
b35ddf41
MG
83 }
84 if (!show_only)
691f1a28 85 unlink_or_warn(fullpath);
ba84a797
LT
86 return 0;
87}
88
27e1e22d 89static int prune_cruft(const char *basename, const char *path, void *data)
ba84a797 90{
27e1e22d
JK
91 if (starts_with(basename, "tmp_obj_"))
92 prune_tmp_file(path);
93 else
94 fprintf(stderr, "bad sha1 file: %s\n", path);
ba84a797
LT
95 return 0;
96}
97
70c49050 98static int prune_subdir(unsigned int nr, const char *path, void *data)
ba84a797 99{
27e1e22d
JK
100 if (!show_only)
101 rmdir(path);
102 return 0;
ba84a797
LT
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 121 while ((de = readdir(dir)) != NULL)
59556548 122 if (starts_with(de->d_name, "tmp_"))
4454e9cb 123 prune_tmp_file(mkpath("%s/%s", 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;
0c16cd49 130 int exclude_promisor_objects = 0;
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>")),
0c16cd49
JT
137 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
138 N_("limit traversal to objects outside promisor packfiles")),
629de472
MB
139 OPT_END()
140 };
db87e396 141 char *s;
ba84a797 142
dddbad72 143 expire = TIME_MAX;
16157b80 144 save_commit_buffer = 0;
6ebd1caf 145 read_replace_refs = 0;
ff4056bb 146 ref_paranoia = 1;
2abf3503 147 repo_init_revisions(the_repository, &revs, prefix);
ba84a797 148
37782920 149 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
23af91d1 150
067fbd41
JK
151 if (repository_format_precious_objects)
152 die(_("cannot prune in a precious-objects repo"));
153
fe308f53 154 while (argc--) {
af6730e7 155 struct object_id oid;
fe308f53
JH
156 const char *name = *argv++;
157
af6730e7 158 if (!get_oid(name, &oid)) {
c251c83d 159 struct object *object = parse_object_or_die(&oid,
160 name);
fe308f53
JH
161 add_pending_object(&revs, object, "");
162 }
163 else
164 die("unrecognized argument: %s", name);
165 }
bf0a59b3
JK
166
167 if (show_progress == -1)
168 show_progress = isatty(2);
0c16cd49
JT
169 if (exclude_promisor_objects) {
170 fetch_if_missing = 0;
171 revs.exclude_promisor_objects = 1;
172 }
bf0a59b3 173
27e1e22d 174 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
d55a30bb 175 prune_cruft, prune_subdir, &revs);
ba84a797 176
af0b4a3b 177 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
db87e396 178 remove_temporary_files(get_object_directory());
4e2d094d 179 s = mkpathdup("%s/pack", get_object_directory());
db87e396
BC
180 remove_temporary_files(s);
181 free(s);
eab3296c 182
d55a30bb
JK
183 if (is_repository_shallow(the_repository)) {
184 perform_reachability_traversal(&revs);
2588f6ed 185 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
d55a30bb 186 }
eab3296c 187
ba84a797
LT
188 return 0;
189}