merge-recursive: clean up get_renamed_dir_portion()
[git.git] / builtin / prune.c
blob2b76872ad2207857077f4ecf285780e94388d00d
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "builtin.h"
6 #include "reachable.h"
7 #include "parse-options.h"
8 #include "progress.h"
9 #include "object-store.h"
11 static const char * const prune_usage[] = {
12 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
13 NULL
15 static int show_only;
16 static int verbose;
17 static timestamp_t expire;
18 static int show_progress = -1;
20 static int prune_tmp_file(const char *fullpath)
22 struct stat st;
23 if (lstat(fullpath, &st))
24 return error("Could not stat '%s'", fullpath);
25 if (st.st_mtime > expire)
26 return 0;
27 if (show_only || verbose)
28 printf("Removing stale temporary file %s\n", fullpath);
29 if (!show_only)
30 unlink_or_warn(fullpath);
31 return 0;
34 static void perform_reachability_traversal(struct rev_info *revs)
36 static int initialized;
37 struct progress *progress = NULL;
39 if (initialized)
40 return;
42 if (show_progress)
43 progress = start_delayed_progress(_("Checking connectivity"), 0);
44 mark_reachable_objects(revs, 1, expire, progress);
45 stop_progress(&progress);
46 initialized = 1;
49 static int is_object_reachable(const struct object_id *oid,
50 struct rev_info *revs)
52 struct object *obj;
54 perform_reachability_traversal(revs);
56 obj = lookup_object(the_repository, oid);
57 return obj && (obj->flags & SEEN);
60 static int prune_object(const struct object_id *oid, const char *fullpath,
61 void *data)
63 struct rev_info *revs = data;
64 struct stat st;
66 if (is_object_reachable(oid, revs))
67 return 0;
69 if (lstat(fullpath, &st)) {
70 /* report errors, but do not stop pruning */
71 error("Could not stat '%s'", fullpath);
72 return 0;
74 if (st.st_mtime > expire)
75 return 0;
76 if (show_only || verbose) {
77 enum object_type type = oid_object_info(the_repository, oid,
78 NULL);
79 printf("%s %s\n", oid_to_hex(oid),
80 (type > 0) ? type_name(type) : "unknown");
82 if (!show_only)
83 unlink_or_warn(fullpath);
84 return 0;
87 static int prune_cruft(const char *basename, const char *path, void *data)
89 if (starts_with(basename, "tmp_obj_"))
90 prune_tmp_file(path);
91 else
92 fprintf(stderr, "bad sha1 file: %s\n", path);
93 return 0;
96 static int prune_subdir(unsigned int nr, const char *path, void *data)
98 if (!show_only)
99 rmdir(path);
100 return 0;
104 * Write errors (particularly out of space) can result in
105 * failed temporary packs (and more rarely indexes and other
106 * files beginning with "tmp_") accumulating in the object
107 * and the pack directories.
109 static void remove_temporary_files(const char *path)
111 DIR *dir;
112 struct dirent *de;
114 dir = opendir(path);
115 if (!dir) {
116 fprintf(stderr, "Unable to open directory %s\n", path);
117 return;
119 while ((de = readdir(dir)) != NULL)
120 if (starts_with(de->d_name, "tmp_"))
121 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
122 closedir(dir);
125 int cmd_prune(int argc, const char **argv, const char *prefix)
127 struct rev_info revs;
128 int exclude_promisor_objects = 0;
129 const struct option options[] = {
130 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
131 OPT__VERBOSE(&verbose, N_("report pruned objects")),
132 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
133 OPT_EXPIRY_DATE(0, "expire", &expire,
134 N_("expire objects older than <time>")),
135 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
136 N_("limit traversal to objects outside promisor packfiles")),
137 OPT_END()
139 char *s;
141 expire = TIME_MAX;
142 save_commit_buffer = 0;
143 read_replace_refs = 0;
144 ref_paranoia = 1;
145 repo_init_revisions(the_repository, &revs, prefix);
147 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
149 if (repository_format_precious_objects)
150 die(_("cannot prune in a precious-objects repo"));
152 while (argc--) {
153 struct object_id oid;
154 const char *name = *argv++;
156 if (!get_oid(name, &oid)) {
157 struct object *object = parse_object_or_die(&oid,
158 name);
159 add_pending_object(&revs, object, "");
161 else
162 die("unrecognized argument: %s", name);
165 if (show_progress == -1)
166 show_progress = isatty(2);
167 if (exclude_promisor_objects) {
168 fetch_if_missing = 0;
169 revs.exclude_promisor_objects = 1;
172 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
173 prune_cruft, prune_subdir, &revs);
175 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
176 remove_temporary_files(get_object_directory());
177 s = mkpathdup("%s/pack", get_object_directory());
178 remove_temporary_files(s);
179 free(s);
181 if (is_repository_shallow(the_repository)) {
182 perform_reachability_traversal(&revs);
183 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
186 return 0;