]>
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[] = { |
b35ddf41 | 12 | "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 | |
db87e396 | 20 | static int prune_tmp_object(const char *path, const char *filename) |
0e8316cc BC |
21 | { |
22 | const char *fullpath = mkpath("%s/%s", path, filename); | |
cbf731ed AS |
23 | struct stat st; |
24 | if (lstat(fullpath, &st)) | |
25 | return error("Could not stat '%s'", fullpath); | |
26 | if (st.st_mtime > expire) | |
27 | return 0; | |
0e8316cc BC |
28 | printf("Removing stale temporary file %s\n", fullpath); |
29 | if (!show_only) | |
691f1a28 | 30 | unlink_or_warn(fullpath); |
0e8316cc BC |
31 | return 0; |
32 | } | |
33 | ||
ba84a797 LT |
34 | static int prune_object(char *path, const char *filename, const unsigned char *sha1) |
35 | { | |
f01913e4 | 36 | const char *fullpath = mkpath("%s/%s", path, filename); |
cbf731ed AS |
37 | struct stat st; |
38 | if (lstat(fullpath, &st)) | |
39 | return error("Could not stat '%s'", fullpath); | |
40 | if (st.st_mtime > expire) | |
41 | return 0; | |
b35ddf41 | 42 | if (show_only || verbose) { |
21666f1a NP |
43 | enum object_type type = sha1_object_info(sha1, NULL); |
44 | printf("%s %s\n", sha1_to_hex(sha1), | |
45 | (type > 0) ? typename(type) : "unknown"); | |
b35ddf41 MG |
46 | } |
47 | if (!show_only) | |
691f1a28 | 48 | unlink_or_warn(fullpath); |
ba84a797 LT |
49 | return 0; |
50 | } | |
51 | ||
52 | static int prune_dir(int i, char *path) | |
53 | { | |
54 | DIR *dir = opendir(path); | |
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 | ||
79 | prune_object(path, de->d_name, sha1); | |
80 | continue; | |
81 | } | |
3d32a46b BC |
82 | if (!prefixcmp(de->d_name, "tmp_obj_")) { |
83 | prune_tmp_object(path, de->d_name); | |
84 | continue; | |
85 | } | |
ba84a797 LT |
86 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); |
87 | } | |
d34e70d6 | 88 | closedir(dir); |
3254d218 NP |
89 | if (!show_only) |
90 | rmdir(path); | |
ba84a797 LT |
91 | return 0; |
92 | } | |
93 | ||
94 | static void prune_object_dir(const char *path) | |
95 | { | |
96 | int i; | |
97 | for (i = 0; i < 256; i++) { | |
98 | static char dir[4096]; | |
99 | sprintf(dir, "%s/%02x", path, i); | |
100 | prune_dir(i, dir); | |
101 | } | |
102 | } | |
103 | ||
8464010f DST |
104 | /* |
105 | * Write errors (particularly out of space) can result in | |
106 | * failed temporary packs (and more rarely indexes and other | |
9517e6b8 | 107 | * files beginning with "tmp_") accumulating in the object |
db87e396 | 108 | * and the pack directories. |
8464010f | 109 | */ |
db87e396 | 110 | static void remove_temporary_files(const char *path) |
8464010f DST |
111 | { |
112 | DIR *dir; | |
113 | struct dirent *de; | |
8464010f | 114 | |
db87e396 | 115 | dir = opendir(path); |
8464010f | 116 | if (!dir) { |
db87e396 | 117 | fprintf(stderr, "Unable to open directory %s\n", path); |
8464010f DST |
118 | return; |
119 | } | |
0e8316cc BC |
120 | while ((de = readdir(dir)) != NULL) |
121 | if (!prefixcmp(de->d_name, "tmp_")) | |
db87e396 | 122 | prune_tmp_object(path, de->d_name); |
8464010f DST |
123 | closedir(dir); |
124 | } | |
125 | ||
a633fca0 | 126 | int cmd_prune(int argc, const char **argv, const char *prefix) |
ba84a797 | 127 | { |
24304816 | 128 | struct rev_info revs; |
bf0a59b3 | 129 | struct progress *progress = NULL; |
629de472 | 130 | const struct option options[] = { |
e21adb8c | 131 | OPT__DRY_RUN(&show_only, "do not remove, show only"), |
fd03881a | 132 | OPT__VERBOSE(&verbose, "report pruned objects"), |
bf0a59b3 | 133 | OPT_BOOL(0, "progress", &show_progress, "show progress"), |
629de472 MB |
134 | OPT_DATE(0, "expire", &expire, |
135 | "expire objects older than <time>"), | |
136 | OPT_END() | |
137 | }; | |
db87e396 | 138 | char *s; |
ba84a797 | 139 | |
cbf731ed | 140 | expire = ULONG_MAX; |
16157b80 | 141 | save_commit_buffer = 0; |
dae556bd | 142 | read_replace_refs = 0; |
a633fca0 | 143 | init_revisions(&revs, prefix); |
ba84a797 | 144 | |
37782920 | 145 | argc = parse_options(argc, argv, prefix, options, prune_usage, 0); |
fe308f53 JH |
146 | while (argc--) { |
147 | unsigned char sha1[20]; | |
148 | const char *name = *argv++; | |
149 | ||
150 | if (!get_sha1(name, sha1)) { | |
151 | struct object *object = parse_object(sha1); | |
152 | if (!object) | |
153 | die("bad object: %s", name); | |
154 | add_pending_object(&revs, object, ""); | |
155 | } | |
156 | else | |
157 | die("unrecognized argument: %s", name); | |
158 | } | |
bf0a59b3 JK |
159 | |
160 | if (show_progress == -1) | |
161 | show_progress = isatty(2); | |
162 | if (show_progress) | |
163 | progress = start_progress_delay("Checking connectivity", 0, 0, 2); | |
164 | ||
dc347195 NTND |
165 | mark_reachable_objects(&revs, 1, progress); |
166 | stop_progress(&progress); | |
ba84a797 LT |
167 | prune_object_dir(get_object_directory()); |
168 | ||
2eb53e65 | 169 | prune_packed_objects(show_only); |
db87e396 BC |
170 | remove_temporary_files(get_object_directory()); |
171 | s = xstrdup(mkpath("%s/pack", get_object_directory())); | |
172 | remove_temporary_files(s); | |
173 | free(s); | |
ba84a797 LT |
174 | return 0; |
175 | } |