]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/prune.c
Merge branch 'tz/lib-gpg-prereq-fix'
[thirdparty/git.git] / builtin / prune.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "revision.h"
8 #include "builtin.h"
9 #include "reachable.h"
10 #include "parse-options.h"
11 #include "progress.h"
12 #include "prune-packed.h"
13 #include "replace-object.h"
14 #include "object-file.h"
15 #include "object-name.h"
16 #include "object-store.h"
17 #include "shallow.h"
18
19 static const char * const prune_usage[] = {
20 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
21 NULL
22 };
23 static int show_only;
24 static int verbose;
25 static timestamp_t expire;
26 static int show_progress = -1;
27
28 static int prune_tmp_file(const char *fullpath)
29 {
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;
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 }
51 return 0;
52 }
53
54 static void perform_reachability_traversal(struct rev_info *revs)
55 {
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
69 static int is_object_reachable(const struct object_id *oid,
70 struct rev_info *revs)
71 {
72 struct object *obj;
73
74 perform_reachability_traversal(revs);
75
76 obj = lookup_object(the_repository, oid);
77 return obj && (obj->flags & SEEN);
78 }
79
80 static 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))
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 }
94 if (st.st_mtime > expire)
95 return 0;
96 if (show_only || verbose) {
97 enum object_type type = oid_object_info(the_repository, oid,
98 NULL);
99 printf("%s %s\n", oid_to_hex(oid),
100 (type > 0) ? type_name(type) : "unknown");
101 }
102 if (!show_only)
103 unlink_or_warn(fullpath);
104 return 0;
105 }
106
107 static int prune_cruft(const char *basename, const char *path,
108 void *data UNUSED)
109 {
110 if (starts_with(basename, "tmp_obj_"))
111 prune_tmp_file(path);
112 else
113 fprintf(stderr, "bad sha1 file: %s\n", path);
114 return 0;
115 }
116
117 static int prune_subdir(unsigned int nr UNUSED, const char *path,
118 void *data UNUSED)
119 {
120 if (!show_only)
121 rmdir(path);
122 return 0;
123 }
124
125 /*
126 * Write errors (particularly out of space) can result in
127 * failed temporary packs (and more rarely indexes and other
128 * files beginning with "tmp_") accumulating in the object
129 * and the pack directories.
130 */
131 static void remove_temporary_files(const char *path)
132 {
133 DIR *dir;
134 struct dirent *de;
135
136 dir = opendir(path);
137 if (!dir) {
138 if (errno != ENOENT)
139 fprintf(stderr, "Unable to open directory %s: %s\n",
140 path, strerror(errno));
141 return;
142 }
143 while ((de = readdir(dir)) != NULL)
144 if (starts_with(de->d_name, "tmp_"))
145 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
146 closedir(dir);
147 }
148
149 int cmd_prune(int argc, const char **argv, const char *prefix)
150 {
151 struct rev_info revs;
152 int exclude_promisor_objects = 0;
153 const struct option options[] = {
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")),
157 OPT_EXPIRY_DATE(0, "expire", &expire,
158 N_("expire objects older than <time>")),
159 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
160 N_("limit traversal to objects outside promisor packfiles")),
161 OPT_END()
162 };
163 char *s;
164
165 expire = TIME_MAX;
166 save_commit_buffer = 0;
167 disable_replace_refs();
168 repo_init_revisions(the_repository, &revs, prefix);
169
170 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
171
172 if (repository_format_precious_objects)
173 die(_("cannot prune in a precious-objects repo"));
174
175 while (argc--) {
176 struct object_id oid;
177 const char *name = *argv++;
178
179 if (!repo_get_oid(the_repository, name, &oid)) {
180 struct object *object = parse_object_or_die(&oid,
181 name);
182 add_pending_object(&revs, object, "");
183 }
184 else
185 die("unrecognized argument: %s", name);
186 }
187
188 if (show_progress == -1)
189 show_progress = isatty(2);
190 if (exclude_promisor_objects) {
191 fetch_if_missing = 0;
192 revs.exclude_promisor_objects = 1;
193 }
194
195 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
196 prune_cruft, prune_subdir, &revs);
197
198 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
199 remove_temporary_files(get_object_directory());
200 s = mkpathdup("%s/pack", get_object_directory());
201 remove_temporary_files(s);
202 free(s);
203
204 if (is_repository_shallow(the_repository)) {
205 perform_reachability_traversal(&revs);
206 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
207 }
208
209 release_revisions(&revs);
210 return 0;
211 }