cache.h: add repository argument to oid_object_info_extended
[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[] = {
1a1fc2d5 11 N_("git prune [-n] [-v] [--progress] [--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) {
abef9020 53 enum object_type type = oid_object_info(oid, NULL);
76c1d9a0 54 printf("%s %s\n", oid_to_hex(oid),
debca9d2 55 (type > 0) ? type_name(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;
0c16cd49 104 int exclude_promisor_objects = 0;
629de472 105 const struct option options[] = {
8f5b7281
NTND
106 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
107 OPT__VERBOSE(&verbose, N_("report pruned objects")),
108 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
27ec394a
JH
109 OPT_EXPIRY_DATE(0, "expire", &expire,
110 N_("expire objects older than <time>")),
0c16cd49
JT
111 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
112 N_("limit traversal to objects outside promisor packfiles")),
629de472
MB
113 OPT_END()
114 };
db87e396 115 char *s;
ba84a797 116
dddbad72 117 expire = TIME_MAX;
16157b80 118 save_commit_buffer = 0;
afc711b8 119 check_replace_refs = 0;
ff4056bb 120 ref_paranoia = 1;
a633fca0 121 init_revisions(&revs, prefix);
ba84a797 122
37782920 123 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
23af91d1 124
067fbd41
JK
125 if (repository_format_precious_objects)
126 die(_("cannot prune in a precious-objects repo"));
127
fe308f53 128 while (argc--) {
af6730e7 129 struct object_id oid;
fe308f53
JH
130 const char *name = *argv++;
131
af6730e7 132 if (!get_oid(name, &oid)) {
c251c83d 133 struct object *object = parse_object_or_die(&oid,
134 name);
fe308f53
JH
135 add_pending_object(&revs, object, "");
136 }
137 else
138 die("unrecognized argument: %s", name);
139 }
bf0a59b3
JK
140
141 if (show_progress == -1)
142 show_progress = isatty(2);
143 if (show_progress)
8aade107 144 progress = start_delayed_progress(_("Checking connectivity"), 0);
0c16cd49
JT
145 if (exclude_promisor_objects) {
146 fetch_if_missing = 0;
147 revs.exclude_promisor_objects = 1;
148 }
bf0a59b3 149
d3038d22 150 mark_reachable_objects(&revs, 1, expire, progress);
dc347195 151 stop_progress(&progress);
27e1e22d
JK
152 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
153 prune_cruft, prune_subdir, NULL);
ba84a797 154
af0b4a3b 155 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
db87e396 156 remove_temporary_files(get_object_directory());
4e2d094d 157 s = mkpathdup("%s/pack", get_object_directory());
db87e396
BC
158 remove_temporary_files(s);
159 free(s);
eab3296c
NTND
160
161 if (is_repository_shallow())
162 prune_shallow(show_only);
163
ba84a797
LT
164 return 0;
165}