Merge branch 'md/exclude-promisor-objects-fix-cleanup'
[git/raj.git] / builtin / prune.c
blob1ec9ddd751df6644d2c39ace41a1494800082638
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 int prune_object(const struct object_id *oid, const char *fullpath,
35 void *data)
37 struct stat st;
40 * Do we know about this object?
41 * It must have been reachable
43 if (lookup_object(the_repository, oid->hash))
44 return 0;
46 if (lstat(fullpath, &st)) {
47 /* report errors, but do not stop pruning */
48 error("Could not stat '%s'", fullpath);
49 return 0;
51 if (st.st_mtime > expire)
52 return 0;
53 if (show_only || verbose) {
54 enum object_type type = oid_object_info(the_repository, oid,
55 NULL);
56 printf("%s %s\n", oid_to_hex(oid),
57 (type > 0) ? type_name(type) : "unknown");
59 if (!show_only)
60 unlink_or_warn(fullpath);
61 return 0;
64 static int prune_cruft(const char *basename, const char *path, void *data)
66 if (starts_with(basename, "tmp_obj_"))
67 prune_tmp_file(path);
68 else
69 fprintf(stderr, "bad sha1 file: %s\n", path);
70 return 0;
73 static int prune_subdir(unsigned int nr, const char *path, void *data)
75 if (!show_only)
76 rmdir(path);
77 return 0;
81 * Write errors (particularly out of space) can result in
82 * failed temporary packs (and more rarely indexes and other
83 * files beginning with "tmp_") accumulating in the object
84 * and the pack directories.
86 static void remove_temporary_files(const char *path)
88 DIR *dir;
89 struct dirent *de;
91 dir = opendir(path);
92 if (!dir) {
93 fprintf(stderr, "Unable to open directory %s\n", path);
94 return;
96 while ((de = readdir(dir)) != NULL)
97 if (starts_with(de->d_name, "tmp_"))
98 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
99 closedir(dir);
102 int cmd_prune(int argc, const char **argv, const char *prefix)
104 struct rev_info revs;
105 struct progress *progress = NULL;
106 int exclude_promisor_objects = 0;
107 const struct option options[] = {
108 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
109 OPT__VERBOSE(&verbose, N_("report pruned objects")),
110 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
111 OPT_EXPIRY_DATE(0, "expire", &expire,
112 N_("expire objects older than <time>")),
113 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
114 N_("limit traversal to objects outside promisor packfiles")),
115 OPT_END()
117 char *s;
119 expire = TIME_MAX;
120 save_commit_buffer = 0;
121 read_replace_refs = 0;
122 ref_paranoia = 1;
123 repo_init_revisions(the_repository, &revs, prefix);
125 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
127 if (repository_format_precious_objects)
128 die(_("cannot prune in a precious-objects repo"));
130 while (argc--) {
131 struct object_id oid;
132 const char *name = *argv++;
134 if (!get_oid(name, &oid)) {
135 struct object *object = parse_object_or_die(&oid,
136 name);
137 add_pending_object(&revs, object, "");
139 else
140 die("unrecognized argument: %s", name);
143 if (show_progress == -1)
144 show_progress = isatty(2);
145 if (show_progress)
146 progress = start_delayed_progress(_("Checking connectivity"), 0);
147 if (exclude_promisor_objects) {
148 fetch_if_missing = 0;
149 revs.exclude_promisor_objects = 1;
152 mark_reachable_objects(&revs, 1, expire, progress);
153 stop_progress(&progress);
154 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
155 prune_cruft, prune_subdir, NULL);
157 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
158 remove_temporary_files(get_object_directory());
159 s = mkpathdup("%s/pack", get_object_directory());
160 remove_temporary_files(s);
161 free(s);
163 if (is_repository_shallow(the_repository))
164 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
166 return 0;