]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/prune.c
ws.c: remove implicit dependency on the_index
[thirdparty/git.git] / builtin / prune.c
CommitLineData
ba84a797 1#include "cache.h"
ba84a797 2#include "commit.h"
ba84a797
LT
3#include "diff.h"
4#include "revision.h"
5#include "builtin.h"
94421474 6#include "reachable.h"
629de472 7#include "parse-options.h"
dc347195 8#include "progress.h"
cbd53a21 9#include "object-store.h"
ba84a797 10
629de472 11static const char * const prune_usage[] = {
1a1fc2d5 12 N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"),
629de472
MB
13 NULL
14};
96f1e58f 15static int show_only;
b35ddf41 16static int verbose;
dddbad72 17static timestamp_t expire;
bf0a59b3 18static int show_progress = -1;
ba84a797 19
4454e9cb 20static int prune_tmp_file(const char *fullpath)
0e8316cc 21{
cbf731ed
AS
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;
90b29cb7
BC
27 if (show_only || verbose)
28 printf("Removing stale temporary file %s\n", fullpath);
0e8316cc 29 if (!show_only)
691f1a28 30 unlink_or_warn(fullpath);
0e8316cc
BC
31 return 0;
32}
33
76c1d9a0 34static int prune_object(const struct object_id *oid, const char *fullpath,
27e1e22d 35 void *data)
ba84a797 36{
cbf731ed 37 struct stat st;
27e1e22d
JK
38
39 /*
40 * Do we know about this object?
41 * It must have been reachable
42 */
5abddd1e 43 if (lookup_object(the_repository, oid->hash))
27e1e22d
JK
44 return 0;
45
46 if (lstat(fullpath, &st)) {
47 /* report errors, but do not stop pruning */
48 error("Could not stat '%s'", fullpath);
49 return 0;
50 }
cbf731ed
AS
51 if (st.st_mtime > expire)
52 return 0;
b35ddf41 53 if (show_only || verbose) {
0df8e965
SB
54 enum object_type type = oid_object_info(the_repository, oid,
55 NULL);
76c1d9a0 56 printf("%s %s\n", oid_to_hex(oid),
debca9d2 57 (type > 0) ? type_name(type) : "unknown");
b35ddf41
MG
58 }
59 if (!show_only)
691f1a28 60 unlink_or_warn(fullpath);
ba84a797
LT
61 return 0;
62}
63
27e1e22d 64static int prune_cruft(const char *basename, const char *path, void *data)
ba84a797 65{
27e1e22d
JK
66 if (starts_with(basename, "tmp_obj_"))
67 prune_tmp_file(path);
68 else
69 fprintf(stderr, "bad sha1 file: %s\n", path);
ba84a797
LT
70 return 0;
71}
72
70c49050 73static int prune_subdir(unsigned int nr, const char *path, void *data)
ba84a797 74{
27e1e22d
JK
75 if (!show_only)
76 rmdir(path);
77 return 0;
ba84a797
LT
78}
79
8464010f
DST
80/*
81 * Write errors (particularly out of space) can result in
82 * failed temporary packs (and more rarely indexes and other
9517e6b8 83 * files beginning with "tmp_") accumulating in the object
db87e396 84 * and the pack directories.
8464010f 85 */
db87e396 86static void remove_temporary_files(const char *path)
8464010f
DST
87{
88 DIR *dir;
89 struct dirent *de;
8464010f 90
db87e396 91 dir = opendir(path);
8464010f 92 if (!dir) {
db87e396 93 fprintf(stderr, "Unable to open directory %s\n", path);
8464010f
DST
94 return;
95 }
0e8316cc 96 while ((de = readdir(dir)) != NULL)
59556548 97 if (starts_with(de->d_name, "tmp_"))
4454e9cb 98 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
8464010f
DST
99 closedir(dir);
100}
101
a633fca0 102int cmd_prune(int argc, const char **argv, const char *prefix)
ba84a797 103{
24304816 104 struct rev_info revs;
bf0a59b3 105 struct progress *progress = NULL;
0c16cd49 106 int exclude_promisor_objects = 0;
629de472 107 const struct option options[] = {
8f5b7281
NTND
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")),
27ec394a
JH
111 OPT_EXPIRY_DATE(0, "expire", &expire,
112 N_("expire objects older than <time>")),
0c16cd49
JT
113 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
114 N_("limit traversal to objects outside promisor packfiles")),
629de472
MB
115 OPT_END()
116 };
db87e396 117 char *s;
ba84a797 118
dddbad72 119 expire = TIME_MAX;
16157b80 120 save_commit_buffer = 0;
6ebd1caf 121 read_replace_refs = 0;
ff4056bb 122 ref_paranoia = 1;
a633fca0 123 init_revisions(&revs, prefix);
ba84a797 124
37782920 125 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
23af91d1 126
067fbd41
JK
127 if (repository_format_precious_objects)
128 die(_("cannot prune in a precious-objects repo"));
129
fe308f53 130 while (argc--) {
af6730e7 131 struct object_id oid;
fe308f53
JH
132 const char *name = *argv++;
133
af6730e7 134 if (!get_oid(name, &oid)) {
c251c83d 135 struct object *object = parse_object_or_die(&oid,
136 name);
fe308f53
JH
137 add_pending_object(&revs, object, "");
138 }
139 else
140 die("unrecognized argument: %s", name);
141 }
bf0a59b3
JK
142
143 if (show_progress == -1)
144 show_progress = isatty(2);
145 if (show_progress)
8aade107 146 progress = start_delayed_progress(_("Checking connectivity"), 0);
0c16cd49
JT
147 if (exclude_promisor_objects) {
148 fetch_if_missing = 0;
149 revs.exclude_promisor_objects = 1;
150 }
bf0a59b3 151
d3038d22 152 mark_reachable_objects(&revs, 1, expire, progress);
dc347195 153 stop_progress(&progress);
27e1e22d
JK
154 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
155 prune_cruft, prune_subdir, NULL);
ba84a797 156
af0b4a3b 157 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
db87e396 158 remove_temporary_files(get_object_directory());
4e2d094d 159 s = mkpathdup("%s/pack", get_object_directory());
db87e396
BC
160 remove_temporary_files(s);
161 free(s);
eab3296c 162
c8813487 163 if (is_repository_shallow(the_repository))
eab3296c
NTND
164 prune_shallow(show_only);
165
ba84a797
LT
166 return 0;
167}