alloc: allow arbitrary repositories for alloc functions
[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) {
0df8e965
SB
53 enum object_type type = oid_object_info(the_repository, oid,
54 NULL);
76c1d9a0 55 printf("%s %s\n", oid_to_hex(oid),
debca9d2 56 (type > 0) ? type_name(type) : "unknown");
b35ddf41
MG
57 }
58 if (!show_only)
691f1a28 59 unlink_or_warn(fullpath);
ba84a797
LT
60 return 0;
61}
62
27e1e22d 63static int prune_cruft(const char *basename, const char *path, void *data)
ba84a797 64{
27e1e22d
JK
65 if (starts_with(basename, "tmp_obj_"))
66 prune_tmp_file(path);
67 else
68 fprintf(stderr, "bad sha1 file: %s\n", path);
ba84a797
LT
69 return 0;
70}
71
70c49050 72static int prune_subdir(unsigned int nr, const char *path, void *data)
ba84a797 73{
27e1e22d
JK
74 if (!show_only)
75 rmdir(path);
76 return 0;
ba84a797
LT
77}
78
8464010f
DST
79/*
80 * Write errors (particularly out of space) can result in
81 * failed temporary packs (and more rarely indexes and other
9517e6b8 82 * files beginning with "tmp_") accumulating in the object
db87e396 83 * and the pack directories.
8464010f 84 */
db87e396 85static void remove_temporary_files(const char *path)
8464010f
DST
86{
87 DIR *dir;
88 struct dirent *de;
8464010f 89
db87e396 90 dir = opendir(path);
8464010f 91 if (!dir) {
db87e396 92 fprintf(stderr, "Unable to open directory %s\n", path);
8464010f
DST
93 return;
94 }
0e8316cc 95 while ((de = readdir(dir)) != NULL)
59556548 96 if (starts_with(de->d_name, "tmp_"))
4454e9cb 97 prune_tmp_file(mkpath("%s/%s", path, de->d_name));
8464010f
DST
98 closedir(dir);
99}
100
a633fca0 101int cmd_prune(int argc, const char **argv, const char *prefix)
ba84a797 102{
24304816 103 struct rev_info revs;
bf0a59b3 104 struct progress *progress = NULL;
0c16cd49 105 int exclude_promisor_objects = 0;
629de472 106 const struct option options[] = {
8f5b7281
NTND
107 OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
108 OPT__VERBOSE(&verbose, N_("report pruned objects")),
109 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
27ec394a
JH
110 OPT_EXPIRY_DATE(0, "expire", &expire,
111 N_("expire objects older than <time>")),
0c16cd49
JT
112 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
113 N_("limit traversal to objects outside promisor packfiles")),
629de472
MB
114 OPT_END()
115 };
db87e396 116 char *s;
ba84a797 117
dddbad72 118 expire = TIME_MAX;
16157b80 119 save_commit_buffer = 0;
afc711b8 120 check_replace_refs = 0;
ff4056bb 121 ref_paranoia = 1;
a633fca0 122 init_revisions(&revs, prefix);
ba84a797 123
37782920 124 argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
23af91d1 125
067fbd41
JK
126 if (repository_format_precious_objects)
127 die(_("cannot prune in a precious-objects repo"));
128
fe308f53 129 while (argc--) {
af6730e7 130 struct object_id oid;
fe308f53
JH
131 const char *name = *argv++;
132
af6730e7 133 if (!get_oid(name, &oid)) {
c251c83d 134 struct object *object = parse_object_or_die(&oid,
135 name);
fe308f53
JH
136 add_pending_object(&revs, object, "");
137 }
138 else
139 die("unrecognized argument: %s", name);
140 }
bf0a59b3
JK
141
142 if (show_progress == -1)
143 show_progress = isatty(2);
144 if (show_progress)
8aade107 145 progress = start_delayed_progress(_("Checking connectivity"), 0);
0c16cd49
JT
146 if (exclude_promisor_objects) {
147 fetch_if_missing = 0;
148 revs.exclude_promisor_objects = 1;
149 }
bf0a59b3 150
d3038d22 151 mark_reachable_objects(&revs, 1, expire, progress);
dc347195 152 stop_progress(&progress);
27e1e22d
JK
153 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
154 prune_cruft, prune_subdir, NULL);
ba84a797 155
af0b4a3b 156 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
db87e396 157 remove_temporary_files(get_object_directory());
4e2d094d 158 s = mkpathdup("%s/pack", get_object_directory());
db87e396
BC
159 remove_temporary_files(s);
160 free(s);
eab3296c
NTND
161
162 if (is_repository_shallow())
163 prune_shallow(show_only);
164
ba84a797
LT
165 return 0;
166}