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