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
10static const char * const prune_usage[] = {
11 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
12 NULL
13};
14static int show_only;
15static int verbose;
16static timestamp_t expire;
17static int show_progress = -1;
18
19static int prune_tmp_file(const char *fullpath)
20{
21 struct stat st;
22 if (lstat(fullpath, &st))
23 return error("Could not stat '%s'", fullpath);
24 if (st.st_mtime > expire)
25 return 0;
26 if (show_only || verbose)
27 printf("Removing stale temporary file %s\n", fullpath);
28 if (!show_only)
29 unlink_or_warn(fullpath);
30 return 0;
31}
32
33static int prune_object(const struct object_id *oid, const char *fullpath,
34 void *data)
35{
36 struct stat st;
37
38 /*
39 * Do we know about this object?
40 * It must have been reachable
41 */
42 if (lookup_object(oid->hash))
43 return 0;
44
45 if (lstat(fullpath, &st)) {
46 /* report errors, but do not stop pruning */
47 error("Could not stat '%s'", fullpath);
48 return 0;
49 }
50 if (st.st_mtime > expire)
51 return 0;
52 if (show_only || verbose) {
53 enum object_type type = oid_object_info(the_repository, oid,
54 NULL);
55 printf("%s %s\n", oid_to_hex(oid),
56 (type > 0) ? type_name(type) : "unknown");
57 }
58 if (!show_only)
59 unlink_or_warn(fullpath);
60 return 0;
61}
62
63static int prune_cruft(const char *basename, const char *path, void *data)
64{
65 if (starts_with(basename, "tmp_obj_"))
66 prune_tmp_file(path);
67 else
68 fprintf(stderr, "bad sha1 file: %s\n", path);
69 return 0;
70}
71
72static int prune_subdir(unsigned int nr, const char *path, void *data)
73{
74 if (!show_only)
75 rmdir(path);
76 return 0;
77}
78
79/*
80 * Write errors (particularly out of space) can result in
81 * failed temporary packs (and more rarely indexes and other
82 * files beginning with "tmp_") accumulating in the object
83 * and the pack directories.
84 */
85static void remove_temporary_files(const char *path)
86{
87 DIR *dir;
88 struct dirent *de;
89
90 dir = opendir(path);
91 if (!dir) {
92 fprintf(stderr, "Unable to open directory %s\n", path);
93 return;
94 }
95 while ((de = readdir(dir)) != NULL)
96 if (starts_with(de->d_name, "tmp_"))
97 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
98 closedir(dir);
99}
100
101int cmd_prune(int argc, const char **argv, const char *prefix)
102{
103 struct rev_info revs;
104 struct progress *progress = NULL;
105 int exclude_promisor_objects = 0;
106 const struct option options[] = {
107 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
108 OPT__VERBOSE(&verbose, N_("report pruned objects")),
109 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
110 OPT_EXPIRY_DATE(0, "expire", &expire,
111 N_("expire objects older than <time>")),
112 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
113 N_("limit traversal to objects outside promisor packfiles")),
114 OPT_END()
115 };
116 char *s;
117
118 expire = TIME_MAX;
119 save_commit_buffer = 0;
120 check_replace_refs = 0;
121 ref_paranoia = 1;
122 init_revisions(&revs, prefix);
123
124 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
125
126 if (repository_format_precious_objects)
127 die(_("cannot prune in a precious-objects repo"));
128
129 while (argc--) {
130 struct object_id oid;
131 const char *name = *argv++;
132
133 if (!get_oid(name, &oid)) {
134 struct object *object = parse_object_or_die(&oid,
135 name);
136 add_pending_object(&revs, object, "");
137 }
138 else
139 die("unrecognized argument: %s", name);
140 }
141
142 if (show_progress == -1)
143 show_progress = isatty(2);
144 if (show_progress)
145 progress = start_delayed_progress(_("Checking connectivity"), 0);
146 if (exclude_promisor_objects) {
147 fetch_if_missing = 0;
148 revs.exclude_promisor_objects = 1;
149 }
150
151 mark_reachable_objects(&revs, 1, expire, progress);
152 stop_progress(&progress);
153 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
154 prune_cruft, prune_subdir, NULL);
155
156 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
157 remove_temporary_files(get_object_directory());
158 s = mkpathdup("%s/pack", get_object_directory());
159 remove_temporary_files(s);
160 free(s);
161
162 if (is_repository_shallow())
163 prune_shallow(show_only);
164
165 return 0;
166}