Merge branch 'ak/restore-both-incompatible-with-conflicts'
[git/debian.git] / builtin / prune.c
blob119a253a2adc4b3e867b95d61c97c40f21625614
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "hex.h"
5 #include "revision.h"
6 #include "builtin.h"
7 #include "reachable.h"
8 #include "parse-options.h"
9 #include "progress.h"
10 #include "prune-packed.h"
11 #include "replace-object.h"
12 #include "object-store.h"
13 #include "shallow.h"
15 static const char * const prune_usage[] = {
16 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
17 NULL
19 static int show_only;
20 static int verbose;
21 static timestamp_t expire;
22 static int show_progress = -1;
24 static int prune_tmp_file(const char *fullpath)
26 struct stat st;
27 if (lstat(fullpath, &st))
28 return error("Could not stat '%s'", fullpath);
29 if (st.st_mtime > expire)
30 return 0;
31 if (S_ISDIR(st.st_mode)) {
32 if (show_only || verbose)
33 printf("Removing stale temporary directory %s\n", fullpath);
34 if (!show_only) {
35 struct strbuf remove_dir_buf = STRBUF_INIT;
37 strbuf_addstr(&remove_dir_buf, fullpath);
38 remove_dir_recursively(&remove_dir_buf, 0);
39 strbuf_release(&remove_dir_buf);
41 } else {
42 if (show_only || verbose)
43 printf("Removing stale temporary file %s\n", fullpath);
44 if (!show_only)
45 unlink_or_warn(fullpath);
47 return 0;
50 static void perform_reachability_traversal(struct rev_info *revs)
52 static int initialized;
53 struct progress *progress = NULL;
55 if (initialized)
56 return;
58 if (show_progress)
59 progress = start_delayed_progress(_("Checking connectivity"), 0);
60 mark_reachable_objects(revs, 1, expire, progress);
61 stop_progress(&progress);
62 initialized = 1;
65 static int is_object_reachable(const struct object_id *oid,
66 struct rev_info *revs)
68 struct object *obj;
70 perform_reachability_traversal(revs);
72 obj = lookup_object(the_repository, oid);
73 return obj && (obj->flags & SEEN);
76 static int prune_object(const struct object_id *oid, const char *fullpath,
77 void *data)
79 struct rev_info *revs = data;
80 struct stat st;
82 if (is_object_reachable(oid, revs))
83 return 0;
85 if (lstat(fullpath, &st)) {
86 /* report errors, but do not stop pruning */
87 error("Could not stat '%s'", fullpath);
88 return 0;
90 if (st.st_mtime > expire)
91 return 0;
92 if (show_only || verbose) {
93 enum object_type type = oid_object_info(the_repository, oid,
94 NULL);
95 printf("%s %s\n", oid_to_hex(oid),
96 (type > 0) ? type_name(type) : "unknown");
98 if (!show_only)
99 unlink_or_warn(fullpath);
100 return 0;
103 static int prune_cruft(const char *basename, const char *path,
104 void *data UNUSED)
106 if (starts_with(basename, "tmp_obj_"))
107 prune_tmp_file(path);
108 else
109 fprintf(stderr, "bad sha1 file: %s\n", path);
110 return 0;
113 static int prune_subdir(unsigned int nr UNUSED, const char *path,
114 void *data UNUSED)
116 if (!show_only)
117 rmdir(path);
118 return 0;
122 * Write errors (particularly out of space) can result in
123 * failed temporary packs (and more rarely indexes and other
124 * files beginning with "tmp_") accumulating in the object
125 * and the pack directories.
127 static void remove_temporary_files(const char *path)
129 DIR *dir;
130 struct dirent *de;
132 dir = opendir(path);
133 if (!dir) {
134 if (errno != ENOENT)
135 fprintf(stderr, "Unable to open directory %s: %s\n",
136 path, strerror(errno));
137 return;
139 while ((de = readdir(dir)) != NULL)
140 if (starts_with(de->d_name, "tmp_"))
141 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
142 closedir(dir);
145 int cmd_prune(int argc, const char **argv, const char *prefix)
147 struct rev_info revs;
148 int exclude_promisor_objects = 0;
149 const struct option options[] = {
150 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
151 OPT__VERBOSE(&verbose, N_("report pruned objects")),
152 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
153 OPT_EXPIRY_DATE(0, "expire", &expire,
154 N_("expire objects older than <time>")),
155 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
156 N_("limit traversal to objects outside promisor packfiles")),
157 OPT_END()
159 char *s;
161 expire = TIME_MAX;
162 save_commit_buffer = 0;
163 read_replace_refs = 0;
164 repo_init_revisions(the_repository, &revs, prefix);
166 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
168 if (repository_format_precious_objects)
169 die(_("cannot prune in a precious-objects repo"));
171 while (argc--) {
172 struct object_id oid;
173 const char *name = *argv++;
175 if (!get_oid(name, &oid)) {
176 struct object *object = parse_object_or_die(&oid,
177 name);
178 add_pending_object(&revs, object, "");
180 else
181 die("unrecognized argument: %s", name);
184 if (show_progress == -1)
185 show_progress = isatty(2);
186 if (exclude_promisor_objects) {
187 fetch_if_missing = 0;
188 revs.exclude_promisor_objects = 1;
191 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
192 prune_cruft, prune_subdir, &revs);
194 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
195 remove_temporary_files(get_object_directory());
196 s = mkpathdup("%s/pack", get_object_directory());
197 remove_temporary_files(s);
198 free(s);
200 if (is_repository_shallow(the_repository)) {
201 perform_reachability_traversal(&revs);
202 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
205 release_revisions(&revs);
206 return 0;