blob: 02c6ab7cbaafbac6492fd458bedc0f162e81d683 [file] [log] [blame]
Linus Torvaldsba84a792006-07-06 10:16:22 -07001#include "cache.h"
Linus Torvaldsba84a792006-07-06 10:16:22 -07002#include "commit.h"
Linus Torvaldsba84a792006-07-06 10:16:22 -07003#include "diff.h"
4#include "revision.h"
5#include "builtin.h"
Junio C Hamano94421472007-01-06 02:16:17 -08006#include "reachable.h"
Michele Ballabio629de472008-03-23 21:50:29 +01007#include "parse-options.h"
Nguyễn Thái Ngọc Duydc347192011-11-05 19:00:08 +07008#include "progress.h"
Denton Liu9460fd42020-03-23 21:07:52 -04009#include "prune-packed.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070010#include "object-store.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060011#include "shallow.h"
Linus Torvaldsba84a792006-07-06 10:16:22 -070012
Michele Ballabio629de472008-03-23 21:50:29 +010013static const char * const prune_usage[] = {
Robert P. J. Day1a1fc2d2017-11-21 10:51:52 -050014 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
Michele Ballabio629de472008-03-23 21:50:29 +010015 NULL
16};
David Rientjes96f1e582006-08-15 10:23:48 -070017static int show_only;
Michael J Gruberb35ddf42008-09-29 18:49:52 +020018static int verbose;
Johannes Schindelindddbad72017-04-26 21:29:31 +020019static timestamp_t expire;
Jeff Kingbf0a59b2011-11-08 00:34:08 -050020static int show_progress = -1;
Linus Torvaldsba84a792006-07-06 10:16:22 -070021
Jeff King4454e9c2013-12-17 18:22:31 -050022static int prune_tmp_file(const char *fullpath)
Brandon Casey0e8316c2008-07-24 17:41:12 -050023{
Adam Simpkinscbf731e2010-02-26 19:50:02 -080024 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;
Brandon Casey90b29cb2012-08-06 22:01:49 -070029 if (show_only || verbose)
30 printf("Removing stale temporary file %s\n", fullpath);
Brandon Casey0e8316c2008-07-24 17:41:12 -050031 if (!show_only)
Alex Riesen691f1a22009-04-29 23:22:56 +020032 unlink_or_warn(fullpath);
Brandon Casey0e8316c2008-07-24 17:41:12 -050033 return 0;
34}
35
Jeff Kingd55a30b2019-02-13 23:35:22 -050036static void perform_reachability_traversal(struct rev_info *revs)
Linus Torvaldsba84a792006-07-06 10:16:22 -070037{
Jeff Kingd55a30b2019-02-13 23:35:22 -050038 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
51static int is_object_reachable(const struct object_id *oid,
52 struct rev_info *revs)
53{
Jeff Kingc2bf4732019-02-13 23:38:21 -050054 struct object *obj;
55
Jeff Kingd55a30b2019-02-13 23:35:22 -050056 perform_reachability_traversal(revs);
Jeff King27e1e222014-10-15 18:38:55 -040057
Jeff Kingd0229ab2019-06-20 03:41:14 -040058 obj = lookup_object(the_repository, oid);
Jeff Kingc2bf4732019-02-13 23:38:21 -050059 return obj && (obj->flags & SEEN);
Jeff Kingd55a30b2019-02-13 23:35:22 -050060}
61
62static 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))
Jeff King27e1e222014-10-15 18:38:55 -040069 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 }
Adam Simpkinscbf731e2010-02-26 19:50:02 -080076 if (st.st_mtime > expire)
77 return 0;
Michael J Gruberb35ddf42008-09-29 18:49:52 +020078 if (show_only || verbose) {
Stefan Beller0df8e962018-04-25 11:20:59 -070079 enum object_type type = oid_object_info(the_repository, oid,
80 NULL);
brian m. carlson76c1d9a2017-02-21 23:47:35 +000081 printf("%s %s\n", oid_to_hex(oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -080082 (type > 0) ? type_name(type) : "unknown");
Michael J Gruberb35ddf42008-09-29 18:49:52 +020083 }
84 if (!show_only)
Alex Riesen691f1a22009-04-29 23:22:56 +020085 unlink_or_warn(fullpath);
Linus Torvaldsba84a792006-07-06 10:16:22 -070086 return 0;
87}
88
Jeff King27e1e222014-10-15 18:38:55 -040089static int prune_cruft(const char *basename, const char *path, void *data)
Linus Torvaldsba84a792006-07-06 10:16:22 -070090{
Jeff King27e1e222014-10-15 18:38:55 -040091 if (starts_with(basename, "tmp_obj_"))
92 prune_tmp_file(path);
93 else
94 fprintf(stderr, "bad sha1 file: %s\n", path);
Linus Torvaldsba84a792006-07-06 10:16:22 -070095 return 0;
96}
97
René Scharfe70c49052017-06-24 16:09:39 +020098static int prune_subdir(unsigned int nr, const char *path, void *data)
Linus Torvaldsba84a792006-07-06 10:16:22 -070099{
Jeff King27e1e222014-10-15 18:38:55 -0400100 if (!show_only)
101 rmdir(path);
102 return 0;
Linus Torvaldsba84a792006-07-06 10:16:22 -0700103}
104
David Steven Tweed84640102008-02-07 02:55:14 +0000105/*
106 * Write errors (particularly out of space) can result in
107 * failed temporary packs (and more rarely indexes and other
Junio C Hamano9517e6b2010-02-03 21:23:18 -0800108 * files beginning with "tmp_") accumulating in the object
Brandon Caseydb87e392008-09-22 18:34:26 -0500109 * and the pack directories.
David Steven Tweed84640102008-02-07 02:55:14 +0000110 */
Brandon Caseydb87e392008-09-22 18:34:26 -0500111static void remove_temporary_files(const char *path)
David Steven Tweed84640102008-02-07 02:55:14 +0000112{
113 DIR *dir;
114 struct dirent *de;
David Steven Tweed84640102008-02-07 02:55:14 +0000115
Brandon Caseydb87e392008-09-22 18:34:26 -0500116 dir = opendir(path);
David Steven Tweed84640102008-02-07 02:55:14 +0000117 if (!dir) {
Brandon Caseydb87e392008-09-22 18:34:26 -0500118 fprintf(stderr, "Unable to open directory %s\n", path);
David Steven Tweed84640102008-02-07 02:55:14 +0000119 return;
120 }
Brandon Casey0e8316c2008-07-24 17:41:12 -0500121 while ((de = readdir(dir)) != NULL)
Christian Couder59556542013-11-30 21:55:40 +0100122 if (starts_with(de->d_name, "tmp_"))
Jeff King4454e9c2013-12-17 18:22:31 -0500123 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
David Steven Tweed84640102008-02-07 02:55:14 +0000124 closedir(dir);
125}
126
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700127int cmd_prune(int argc, const char **argv, const char *prefix)
Linus Torvaldsba84a792006-07-06 10:16:22 -0700128{
Junio C Hamano24304812007-01-06 02:16:10 -0800129 struct rev_info revs;
Jonathan Tan0c16cd42017-12-08 15:27:16 +0000130 int exclude_promisor_objects = 0;
Michele Ballabio629de472008-03-23 21:50:29 +0100131 const struct option options[] = {
Nguyễn Thái Ngọc Duy8f5b7282012-08-20 19:32:32 +0700132 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")),
Junio C Hamano27ec3942013-04-25 11:13:49 -0700135 OPT_EXPIRY_DATE(0, "expire", &expire,
136 N_("expire objects older than <time>")),
Jonathan Tan0c16cd42017-12-08 15:27:16 +0000137 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
138 N_("limit traversal to objects outside promisor packfiles")),
Michele Ballabio629de472008-03-23 21:50:29 +0100139 OPT_END()
140 };
Brandon Caseydb87e392008-09-22 18:34:26 -0500141 char *s;
Linus Torvaldsba84a792006-07-06 10:16:22 -0700142
Johannes Schindelindddbad72017-04-26 21:29:31 +0200143 expire = TIME_MAX;
Junio C Hamano16157b82007-01-05 13:31:43 -0800144 save_commit_buffer = 0;
Jeff King6ebd1ca2018-07-18 16:45:20 -0400145 read_replace_refs = 0;
Jeff Kingff4056b2015-03-20 14:43:09 -0400146 ref_paranoia = 1;
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200147 repo_init_revisions(the_repository, &revs, prefix);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700148
Stephen Boyd37782922009-05-23 11:53:12 -0700149 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
Nguyễn Thái Ngọc Duy23af91d2014-11-30 15:24:48 +0700150
Jeff King067fbd42015-06-23 06:54:11 -0400151 if (repository_format_precious_objects)
152 die(_("cannot prune in a precious-objects repo"));
153
Junio C Hamanofe308f52008-03-24 23:20:51 -0700154 while (argc--) {
brian m. carlsonaf6730e2017-05-01 02:28:58 +0000155 struct object_id oid;
Junio C Hamanofe308f52008-03-24 23:20:51 -0700156 const char *name = *argv++;
157
brian m. carlsonaf6730e2017-05-01 02:28:58 +0000158 if (!get_oid(name, &oid)) {
brian m. carlsonc251c832017-05-06 22:10:38 +0000159 struct object *object = parse_object_or_die(&oid,
160 name);
Junio C Hamanofe308f52008-03-24 23:20:51 -0700161 add_pending_object(&revs, object, "");
162 }
163 else
164 die("unrecognized argument: %s", name);
165 }
Jeff Kingbf0a59b2011-11-08 00:34:08 -0500166
167 if (show_progress == -1)
168 show_progress = isatty(2);
Jonathan Tan0c16cd42017-12-08 15:27:16 +0000169 if (exclude_promisor_objects) {
170 fetch_if_missing = 0;
171 revs.exclude_promisor_objects = 1;
172 }
Jeff Kingbf0a59b2011-11-08 00:34:08 -0500173
Jeff King27e1e222014-10-15 18:38:55 -0400174 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
Jeff Kingd55a30b2019-02-13 23:35:22 -0500175 prune_cruft, prune_subdir, &revs);
Linus Torvaldsba84a792006-07-06 10:16:22 -0700176
Nguyễn Thái Ngọc Duyaf0b4a32013-05-27 18:18:47 +0700177 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
Brandon Caseydb87e392008-09-22 18:34:26 -0500178 remove_temporary_files(get_object_directory());
Ramsay Jones4e2d0942012-09-04 18:31:14 +0100179 s = mkpathdup("%s/pack", get_object_directory());
Brandon Caseydb87e392008-09-22 18:34:26 -0500180 remove_temporary_files(s);
181 free(s);
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700182
Jeff Kingd55a30b2019-02-13 23:35:22 -0500183 if (is_repository_shallow(the_repository)) {
184 perform_reachability_traversal(&revs);
Johannes Schindelin2588f6e2018-10-24 08:56:12 -0700185 prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0);
Jeff Kingd55a30b2019-02-13 23:35:22 -0500186 }
Nguyễn Thái Ngọc Duyeab32962013-12-05 20:02:54 +0700187
Linus Torvaldsba84a792006-07-06 10:16:22 -0700188 return 0;
189}