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