]>
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" |
8ca12c0d | 9 | #include "dir.h" |
ba84a797 | 10 | |
629de472 | 11 | static const char * const prune_usage[] = { |
8f5b7281 | 12 | N_("git prune [-n] [-v] [--expire <time>] [--] [<head>...]"), |
629de472 MB |
13 | NULL |
14 | }; | |
96f1e58f | 15 | static int show_only; |
b35ddf41 | 16 | static int verbose; |
f01913e4 | 17 | static unsigned long 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 | ||
4454e9cb | 34 | static int prune_object(const char *fullpath, const unsigned char *sha1) |
ba84a797 | 35 | { |
cbf731ed AS |
36 | struct stat st; |
37 | if (lstat(fullpath, &st)) | |
38 | return error("Could not stat '%s'", fullpath); | |
39 | if (st.st_mtime > expire) | |
40 | return 0; | |
b35ddf41 | 41 | if (show_only || verbose) { |
21666f1a NP |
42 | enum object_type type = sha1_object_info(sha1, NULL); |
43 | printf("%s %s\n", sha1_to_hex(sha1), | |
44 | (type > 0) ? typename(type) : "unknown"); | |
b35ddf41 MG |
45 | } |
46 | if (!show_only) | |
691f1a28 | 47 | unlink_or_warn(fullpath); |
ba84a797 LT |
48 | return 0; |
49 | } | |
50 | ||
4454e9cb | 51 | static int prune_dir(int i, struct strbuf *path) |
ba84a797 | 52 | { |
4454e9cb JK |
53 | size_t baselen = path->len; |
54 | DIR *dir = opendir(path->buf); | |
ba84a797 LT |
55 | struct dirent *de; |
56 | ||
57 | if (!dir) | |
58 | return 0; | |
59 | ||
60 | while ((de = readdir(dir)) != NULL) { | |
61 | char name[100]; | |
62 | unsigned char sha1[20]; | |
ba84a797 | 63 | |
8ca12c0d | 64 | if (is_dot_or_dotdot(de->d_name)) |
ba84a797 | 65 | continue; |
8ca12c0d | 66 | if (strlen(de->d_name) == 38) { |
ba84a797 | 67 | sprintf(name, "%02x", i); |
8ca12c0d | 68 | memcpy(name+2, de->d_name, 39); |
ba84a797 LT |
69 | if (get_sha1_hex(name, sha1) < 0) |
70 | break; | |
71 | ||
72 | /* | |
73 | * Do we know about this object? | |
74 | * It must have been reachable | |
75 | */ | |
76 | if (lookup_object(sha1)) | |
77 | continue; | |
78 | ||
4454e9cb JK |
79 | strbuf_addf(path, "/%s", de->d_name); |
80 | prune_object(path->buf, sha1); | |
81 | strbuf_setlen(path, baselen); | |
ba84a797 LT |
82 | continue; |
83 | } | |
59556548 | 84 | if (starts_with(de->d_name, "tmp_obj_")) { |
4454e9cb JK |
85 | strbuf_addf(path, "/%s", de->d_name); |
86 | prune_tmp_file(path->buf); | |
87 | strbuf_setlen(path, baselen); | |
3d32a46b BC |
88 | continue; |
89 | } | |
4454e9cb | 90 | fprintf(stderr, "bad sha1 file: %s/%s\n", path->buf, de->d_name); |
ba84a797 | 91 | } |
d34e70d6 | 92 | closedir(dir); |
3254d218 | 93 | if (!show_only) |
4454e9cb | 94 | rmdir(path->buf); |
ba84a797 LT |
95 | return 0; |
96 | } | |
97 | ||
98 | static void prune_object_dir(const char *path) | |
99 | { | |
4454e9cb JK |
100 | struct strbuf buf = STRBUF_INIT; |
101 | size_t baselen; | |
ba84a797 | 102 | int i; |
4454e9cb JK |
103 | |
104 | strbuf_addstr(&buf, path); | |
105 | strbuf_addch(&buf, '/'); | |
106 | baselen = buf.len; | |
107 | ||
ba84a797 | 108 | for (i = 0; i < 256; i++) { |
4454e9cb JK |
109 | strbuf_addf(&buf, "%02x", i); |
110 | prune_dir(i, &buf); | |
111 | strbuf_setlen(&buf, baselen); | |
ba84a797 LT |
112 | } |
113 | } | |
114 | ||
8464010f DST |
115 | /* |
116 | * Write errors (particularly out of space) can result in | |
117 | * failed temporary packs (and more rarely indexes and other | |
9517e6b8 | 118 | * files beginning with "tmp_") accumulating in the object |
db87e396 | 119 | * and the pack directories. |
8464010f | 120 | */ |
db87e396 | 121 | static void remove_temporary_files(const char *path) |
8464010f DST |
122 | { |
123 | DIR *dir; | |
124 | struct dirent *de; | |
8464010f | 125 | |
db87e396 | 126 | dir = opendir(path); |
8464010f | 127 | if (!dir) { |
db87e396 | 128 | fprintf(stderr, "Unable to open directory %s\n", path); |
8464010f DST |
129 | return; |
130 | } | |
0e8316cc | 131 | while ((de = readdir(dir)) != NULL) |
59556548 | 132 | if (starts_with(de->d_name, "tmp_")) |
4454e9cb | 133 | prune_tmp_file(mkpath("%s/%s", path, de->d_name)); |
8464010f DST |
134 | closedir(dir); |
135 | } | |
136 | ||
a633fca0 | 137 | int cmd_prune(int argc, const char **argv, const char *prefix) |
ba84a797 | 138 | { |
24304816 | 139 | struct rev_info revs; |
bf0a59b3 | 140 | struct progress *progress = NULL; |
629de472 | 141 | const struct option options[] = { |
8f5b7281 NTND |
142 | OPT__DRY_RUN(&show_only, N_("do not remove, show only")), |
143 | OPT__VERBOSE(&verbose, N_("report pruned objects")), | |
144 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
27ec394a JH |
145 | OPT_EXPIRY_DATE(0, "expire", &expire, |
146 | N_("expire objects older than <time>")), | |
629de472 MB |
147 | OPT_END() |
148 | }; | |
db87e396 | 149 | char *s; |
ba84a797 | 150 | |
cbf731ed | 151 | expire = ULONG_MAX; |
16157b80 | 152 | save_commit_buffer = 0; |
afc711b8 | 153 | check_replace_refs = 0; |
a633fca0 | 154 | init_revisions(&revs, prefix); |
ba84a797 | 155 | |
37782920 | 156 | argc = parse_options(argc, argv, prefix, options, prune_usage, 0); |
fe308f53 JH |
157 | while (argc--) { |
158 | unsigned char sha1[20]; | |
159 | const char *name = *argv++; | |
160 | ||
161 | if (!get_sha1(name, sha1)) { | |
f7892d18 | 162 | struct object *object = parse_object_or_die(sha1, name); |
fe308f53 JH |
163 | add_pending_object(&revs, object, ""); |
164 | } | |
165 | else | |
166 | die("unrecognized argument: %s", name); | |
167 | } | |
bf0a59b3 JK |
168 | |
169 | if (show_progress == -1) | |
170 | show_progress = isatty(2); | |
171 | if (show_progress) | |
754dbc43 | 172 | progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2); |
bf0a59b3 | 173 | |
dc347195 NTND |
174 | mark_reachable_objects(&revs, 1, progress); |
175 | stop_progress(&progress); | |
ba84a797 LT |
176 | prune_object_dir(get_object_directory()); |
177 | ||
af0b4a3b | 178 | prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0); |
db87e396 | 179 | remove_temporary_files(get_object_directory()); |
4e2d094d | 180 | s = mkpathdup("%s/pack", get_object_directory()); |
db87e396 BC |
181 | remove_temporary_files(s); |
182 | free(s); | |
eab3296c NTND |
183 | |
184 | if (is_repository_shallow()) | |
185 | prune_shallow(show_only); | |
186 | ||
ba84a797 LT |
187 | return 0; |
188 | } |