repack: remove 'generated_pack' API from the builtin

Now that we have factored the "generated_pack" API, we can move it to repack.ch, further slimming down builtin/repack.c. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Oct 15, 2025 at 18:28 UTC f053ab6c2be6a9869cbdfaabe5bd844a2471f8b7
3 files changed +91 -83
builtin/repack.c
-83
@@ -134,89 +134,6 @@ static int write_oid(const struct object_id *oid,
134 return 0;
135 }
136
137 -static struct {
138 - const char *name;
139 - unsigned optional:1;
140 -} exts[] = {
141 - {".pack"},
142 - {".rev", 1},
143 - {".mtimes", 1},
144 - {".bitmap", 1},
145 - {".promisor", 1},
146 - {".idx"},
147 -};
148 -
149 -struct generated_pack {
150 - struct tempfile *tempfiles[ARRAY_SIZE(exts)];
151 -};
152 -
153 -static struct generated_pack *generated_pack_populate(const char *name,
154 - const char *packtmp)
155 -{
156 - struct stat statbuf;
157 - struct strbuf path = STRBUF_INIT;
158 - struct generated_pack *pack = xcalloc(1, sizeof(*pack));
159 - int i;
160 -
161 - for (i = 0; i < ARRAY_SIZE(exts); i++) {
162 - strbuf_reset(&path);
163 - strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
164 -
165 - if (stat(path.buf, &statbuf))
166 - continue;
167 -
168 - pack->tempfiles[i] = register_tempfile(path.buf);
169 - }
170 -
171 - strbuf_release(&path);
172 - return pack;
173 -}
174 -
175 -static int generated_pack_has_ext(const struct generated_pack *pack,
176 - const char *ext)
177 -{
178 - int i;
179 - for (i = 0; i < ARRAY_SIZE(exts); i++) {
180 - if (strcmp(exts[i].name, ext))
181 - continue;
182 - return !!pack->tempfiles[i];
183 - }
184 - BUG("unknown pack extension: '%s'", ext);
185 -}
186 -
187 -static void generated_pack_install(struct generated_pack *pack,
188 - const char *name,
189 - const char *packdir, const char *packtmp)
190 -{
191 - int ext;
192 - for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
193 - char *fname;
194 -
195 - fname = mkpathdup("%s/pack-%s%s", packdir, name,
196 - exts[ext].name);
197 -
198 - if (pack->tempfiles[ext]) {
199 - const char *fname_old = get_tempfile_path(pack->tempfiles[ext]);
200 - struct stat statbuffer;
201 -
202 - if (!stat(fname_old, &statbuffer)) {
203 - statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
204 - chmod(fname_old, statbuffer.st_mode);
205 - }
206 -
207 - if (rename_tempfile(&pack->tempfiles[ext], fname))
208 - die_errno(_("renaming pack to '%s' failed"),
209 - fname);
210 - } else if (!exts[ext].optional)
211 - die(_("pack-objects did not write a '%s' file for pack %s-%s"),
212 - exts[ext].name, packtmp, name);
213 - else if (unlink(fname) < 0 && errno != ENOENT)
214 - die_errno(_("could not unlink: %s"), fname);
215 -
216 - free(fname);
217 - }
218 -}
219 -
137 static void repack_promisor_objects(struct repository *repo,
138 const struct pack_objects_args *args,
139 struct string_list *names)
repack.c
+83
@@ -3,9 +3,11 @@
3 #include "midx.h"
4 #include "odb.h"
5 #include "packfile.h"
6 +#include "path.h"
7 #include "repack.h"
8 #include "repository.h"
9 #include "run-command.h"
10 +#include "tempfile.h"
11
12 void prepare_pack_objects(struct child_process *cmd,
13 const struct pack_objects_args *args,
@@ -219,3 +221,84 @@ void existing_packs_release(struct existing_packs *existing)
221 string_list_clear(&existing->non_kept_packs, 0);
222 string_list_clear(&existing->cruft_packs, 0);
223 }
224 +
225 +static struct {
226 + const char *name;
227 + unsigned optional:1;
228 +} exts[] = {
229 + {".pack"},
230 + {".rev", 1},
231 + {".mtimes", 1},
232 + {".bitmap", 1},
233 + {".promisor", 1},
234 + {".idx"},
235 +};
236 +
237 +struct generated_pack {
238 + struct tempfile *tempfiles[ARRAY_SIZE(exts)];
239 +};
240 +
241 +struct generated_pack *generated_pack_populate(const char *name,
242 + const char *packtmp)
243 +{
244 + struct stat statbuf;
245 + struct strbuf path = STRBUF_INIT;
246 + struct generated_pack *pack = xcalloc(1, sizeof(*pack));
247 + size_t i;
248 +
249 + for (i = 0; i < ARRAY_SIZE(exts); i++) {
250 + strbuf_reset(&path);
251 + strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
252 +
253 + if (stat(path.buf, &statbuf))
254 + continue;
255 +
256 + pack->tempfiles[i] = register_tempfile(path.buf);
257 + }
258 +
259 + strbuf_release(&path);
260 + return pack;
261 +}
262 +
263 +int generated_pack_has_ext(const struct generated_pack *pack, const char *ext)
264 +{
265 + size_t i;
266 + for (i = 0; i < ARRAY_SIZE(exts); i++) {
267 + if (strcmp(exts[i].name, ext))
268 + continue;
269 + return !!pack->tempfiles[i];
270 + }
271 + BUG("unknown pack extension: '%s'", ext);
272 +}
273 +
274 +void generated_pack_install(struct generated_pack *pack, const char *name,
275 + const char *packdir, const char *packtmp)
276 +{
277 + size_t ext;
278 + for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
279 + char *fname;
280 +
281 + fname = mkpathdup("%s/pack-%s%s", packdir, name,
282 + exts[ext].name);
283 +
284 + if (pack->tempfiles[ext]) {
285 + const char *fname_old = get_tempfile_path(pack->tempfiles[ext]);
286 + struct stat statbuffer;
287 +
288 + if (!stat(fname_old, &statbuffer)) {
289 + statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
290 + chmod(fname_old, statbuffer.st_mode);
291 + }
292 +
293 + if (rename_tempfile(&pack->tempfiles[ext], fname))
294 + die_errno(_("renaming pack to '%s' failed"),
295 + fname);
296 + } else if (!exts[ext].optional)
297 + die(_("pack-objects did not write a '%s' file for pack %s-%s"),
298 + exts[ext].name, packtmp, name);
299 + else if (unlink(fname) < 0 && errno != ENOENT)
300 + die_errno(_("could not unlink: %s"), fname);
301 +
302 + free(fname);
303 + }
304 +}
repack.h
+8
@@ -66,4 +66,12 @@ void existing_packs_remove_redundant(struct existing_packs *existing,
66 const char *packdir);
67 void existing_packs_release(struct existing_packs *existing);
68
69 +struct generated_pack;
70 +
71 +struct generated_pack *generated_pack_populate(const char *name,
72 + const char *packtmp);
73 +int generated_pack_has_ext(const struct generated_pack *pack, const char *ext);
74 +void generated_pack_install(struct generated_pack *pack, const char *name,
75 + const char *packdir, const char *packtmp);
76 +
77 #endif /* REPACK_H */