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