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