]>
Commit | Line | Data |
---|---|---|
ba84a797 | 1 | #include "cache.h" |
ba84a797 | 2 | #include "commit.h" |
ba84a797 | 3 | #include "diff.h" |
f394e093 | 4 | #include "gettext.h" |
41771fa4 | 5 | #include "hex.h" |
ba84a797 LT |
6 | #include "revision.h" |
7 | #include "builtin.h" | |
94421474 | 8 | #include "reachable.h" |
629de472 | 9 | #include "parse-options.h" |
dc347195 | 10 | #include "progress.h" |
9460fd48 | 11 | #include "prune-packed.h" |
cbeab747 | 12 | #include "replace-object.h" |
cbd53a21 | 13 | #include "object-store.h" |
120ad2b0 | 14 | #include "shallow.h" |
ba84a797 | 15 | |
629de472 | 16 | static const char * const prune_usage[] = { |
1a1fc2d5 | 17 | N_("git prune [-n] [-v] [--progress] [--expire <time>] [--] [<head>...]"), |
629de472 MB |
18 | NULL |
19 | }; | |
96f1e58f | 20 | static int show_only; |
b35ddf41 | 21 | static int verbose; |
dddbad72 | 22 | static timestamp_t expire; |
bf0a59b3 | 23 | static int show_progress = -1; |
ba84a797 | 24 | |
4454e9cb | 25 | static int prune_tmp_file(const char *fullpath) |
0e8316cc | 26 | { |
cbf731ed AS |
27 | struct stat st; |
28 | if (lstat(fullpath, &st)) | |
29 | return error("Could not stat '%s'", fullpath); | |
30 | if (st.st_mtime > expire) | |
31 | return 0; | |
b3cecf49 NS |
32 | if (S_ISDIR(st.st_mode)) { |
33 | if (show_only || verbose) | |
34 | printf("Removing stale temporary directory %s\n", fullpath); | |
35 | if (!show_only) { | |
36 | struct strbuf remove_dir_buf = STRBUF_INIT; | |
37 | ||
38 | strbuf_addstr(&remove_dir_buf, fullpath); | |
39 | remove_dir_recursively(&remove_dir_buf, 0); | |
40 | strbuf_release(&remove_dir_buf); | |
41 | } | |
42 | } else { | |
43 | if (show_only || verbose) | |
44 | printf("Removing stale temporary file %s\n", fullpath); | |
45 | if (!show_only) | |
46 | unlink_or_warn(fullpath); | |
47 | } | |
0e8316cc BC |
48 | return 0; |
49 | } | |
50 | ||
d55a30bb | 51 | static void perform_reachability_traversal(struct rev_info *revs) |
ba84a797 | 52 | { |
d55a30bb JK |
53 | static int initialized; |
54 | struct progress *progress = NULL; | |
55 | ||
56 | if (initialized) | |
57 | return; | |
58 | ||
59 | if (show_progress) | |
60 | progress = start_delayed_progress(_("Checking connectivity"), 0); | |
61 | mark_reachable_objects(revs, 1, expire, progress); | |
62 | stop_progress(&progress); | |
63 | initialized = 1; | |
64 | } | |
65 | ||
66 | static int is_object_reachable(const struct object_id *oid, | |
67 | struct rev_info *revs) | |
68 | { | |
c2bf473d JK |
69 | struct object *obj; |
70 | ||
d55a30bb | 71 | perform_reachability_traversal(revs); |
27e1e22d | 72 | |
d0229abd | 73 | obj = lookup_object(the_repository, oid); |
c2bf473d | 74 | return obj && (obj->flags & SEEN); |
d55a30bb JK |
75 | } |
76 | ||
77 | static int prune_object(const struct object_id *oid, const char *fullpath, | |
78 | void *data) | |
79 | { | |
80 | struct rev_info *revs = data; | |
81 | struct stat st; | |
82 | ||
83 | if (is_object_reachable(oid, revs)) | |
27e1e22d JK |
84 | return 0; |
85 | ||
86 | if (lstat(fullpath, &st)) { | |
87 | /* report errors, but do not stop pruning */ | |
88 | error("Could not stat '%s'", fullpath); | |
89 | return 0; | |
90 | } | |
cbf731ed AS |
91 | if (st.st_mtime > expire) |
92 | return 0; | |
b35ddf41 | 93 | if (show_only || verbose) { |
0df8e965 SB |
94 | enum object_type type = oid_object_info(the_repository, oid, |
95 | NULL); | |
76c1d9a0 | 96 | printf("%s %s\n", oid_to_hex(oid), |
debca9d2 | 97 | (type > 0) ? type_name(type) : "unknown"); |
b35ddf41 MG |
98 | } |
99 | if (!show_only) | |
691f1a28 | 100 | unlink_or_warn(fullpath); |
ba84a797 LT |
101 | return 0; |
102 | } | |
103 | ||
be252d33 JK |
104 | static int prune_cruft(const char *basename, const char *path, |
105 | void *data UNUSED) | |
ba84a797 | 106 | { |
27e1e22d JK |
107 | if (starts_with(basename, "tmp_obj_")) |
108 | prune_tmp_file(path); | |
109 | else | |
110 | fprintf(stderr, "bad sha1 file: %s\n", path); | |
ba84a797 LT |
111 | return 0; |
112 | } | |
113 | ||
be252d33 JK |
114 | static int prune_subdir(unsigned int nr UNUSED, const char *path, |
115 | void *data UNUSED) | |
ba84a797 | 116 | { |
27e1e22d JK |
117 | if (!show_only) |
118 | rmdir(path); | |
119 | return 0; | |
ba84a797 LT |
120 | } |
121 | ||
8464010f DST |
122 | /* |
123 | * Write errors (particularly out of space) can result in | |
124 | * failed temporary packs (and more rarely indexes and other | |
9517e6b8 | 125 | * files beginning with "tmp_") accumulating in the object |
db87e396 | 126 | * and the pack directories. |
8464010f | 127 | */ |
db87e396 | 128 | static void remove_temporary_files(const char *path) |
8464010f DST |
129 | { |
130 | DIR *dir; | |
131 | struct dirent *de; | |
8464010f | 132 | |
db87e396 | 133 | dir = opendir(path); |
8464010f | 134 | if (!dir) { |
69747653 EW |
135 | if (errno != ENOENT) |
136 | fprintf(stderr, "Unable to open directory %s: %s\n", | |
137 | path, strerror(errno)); | |
8464010f DST |
138 | return; |
139 | } | |
0e8316cc | 140 | while ((de = readdir(dir)) != NULL) |
59556548 | 141 | if (starts_with(de->d_name, "tmp_")) |
4454e9cb | 142 | prune_tmp_file(mkpath("%s/%s", path, de->d_name)); |
8464010f DST |
143 | closedir(dir); |
144 | } | |
145 | ||
a633fca0 | 146 | int cmd_prune(int argc, const char **argv, const char *prefix) |
ba84a797 | 147 | { |
24304816 | 148 | struct rev_info revs; |
0c16cd49 | 149 | int exclude_promisor_objects = 0; |
629de472 | 150 | const struct option options[] = { |
8f5b7281 NTND |
151 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
152 | OPT__VERBOSE(&verbose, N_("report pruned objects")), | |
153 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
27ec394a JH |
154 | OPT_EXPIRY_DATE(0, "expire", &expire, |
155 | N_("expire objects older than <time>")), | |
0c16cd49 JT |
156 | OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects, |
157 | N_("limit traversal to objects outside promisor packfiles")), | |
629de472 MB |
158 | OPT_END() |
159 | }; | |
db87e396 | 160 | char *s; |
ba84a797 | 161 | |
dddbad72 | 162 | expire = TIME_MAX; |
16157b80 | 163 | save_commit_buffer = 0; |
6ebd1caf | 164 | read_replace_refs = 0; |
2abf3503 | 165 | repo_init_revisions(the_repository, &revs, prefix); |
ba84a797 | 166 | |
37782920 | 167 | argc = parse_options(argc, argv, prefix, options, prune_usage, 0); |
23af91d1 | 168 | |
067fbd41 JK |
169 | if (repository_format_precious_objects) |
170 | die(_("cannot prune in a precious-objects repo")); | |
171 | ||
fe308f53 | 172 | while (argc--) { |
af6730e7 | 173 | struct object_id oid; |
fe308f53 JH |
174 | const char *name = *argv++; |
175 | ||
af6730e7 | 176 | if (!get_oid(name, &oid)) { |
c251c83d | 177 | struct object *object = parse_object_or_die(&oid, |
178 | name); | |
fe308f53 JH |
179 | add_pending_object(&revs, object, ""); |
180 | } | |
181 | else | |
182 | die("unrecognized argument: %s", name); | |
183 | } | |
bf0a59b3 JK |
184 | |
185 | if (show_progress == -1) | |
186 | show_progress = isatty(2); | |
0c16cd49 JT |
187 | if (exclude_promisor_objects) { |
188 | fetch_if_missing = 0; | |
189 | revs.exclude_promisor_objects = 1; | |
190 | } | |
bf0a59b3 | 191 | |
27e1e22d | 192 | for_each_loose_file_in_objdir(get_object_directory(), prune_object, |
d55a30bb | 193 | prune_cruft, prune_subdir, &revs); |
ba84a797 | 194 | |
af0b4a3b | 195 | prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0); |
db87e396 | 196 | remove_temporary_files(get_object_directory()); |
4e2d094d | 197 | s = mkpathdup("%s/pack", get_object_directory()); |
db87e396 BC |
198 | remove_temporary_files(s); |
199 | free(s); | |
eab3296c | 200 | |
d55a30bb JK |
201 | if (is_repository_shallow(the_repository)) { |
202 | perform_reachability_traversal(&revs); | |
2588f6ed | 203 | prune_shallow(show_only ? PRUNE_SHOW_ONLY : 0); |
d55a30bb | 204 | } |
eab3296c | 205 | |
2108fe4a | 206 | release_revisions(&revs); |
ba84a797 LT |
207 | return 0; |
208 | } |