]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/prune.c
sha1_file: introduce an nth_packed_object_oid function
[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"
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;
f01913e4 16static unsigned long 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
27e1e22d
JK
33static int prune_object(const unsigned char *sha1, const char *fullpath,
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 */
42 if (lookup_object(sha1))
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) {
21666f1a
NP
53 enum object_type type = sha1_object_info(sha1, NULL);
54 printf("%s %s\n", sha1_to_hex(sha1),
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
27e1e22d 71static int prune_subdir(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
cbf731ed 114 expire = ULONG_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
JH
125 while (argc--) {
126 unsigned char sha1[20];
127 const char *name = *argv++;
128
129 if (!get_sha1(name, sha1)) {
f7892d18 130 struct object *object = parse_object_or_die(sha1, name);
fe308f53
JH
131 add_pending_object(&revs, object, "");
132 }
133 else
134 die("unrecognized argument: %s", name);
135 }
bf0a59b3
JK
136
137 if (show_progress == -1)
138 show_progress = isatty(2);
139 if (show_progress)
754dbc43 140 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
bf0a59b3 141
d3038d22 142 mark_reachable_objects(&revs, 1, expire, progress);
dc347195 143 stop_progress(&progress);
27e1e22d
JK
144 for_each_loose_file_in_objdir(get_object_directory(), prune_object,
145 prune_cruft, prune_subdir, NULL);
ba84a797 146
af0b4a3b 147 prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
db87e396 148 remove_temporary_files(get_object_directory());
4e2d094d 149 s = mkpathdup("%s/pack", get_object_directory());
db87e396
BC
150 remove_temporary_files(s);
151 free(s);
eab3296c
NTND
152
153 if (is_repository_shallow())
154 prune_shallow(show_only);
155
ba84a797
LT
156 return 0;
157}