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