4 #include "parse-options.h"
5 #include "run-command.h"
8 #include "string-list.h"
9 #include "argv-array.h"
11 static int delta_base_offset
= 1;
12 static int pack_kept_objects
= -1;
13 static int write_bitmaps
;
14 static char *packdir
, *packtmp
;
16 static const char *const git_repack_usage
[] = {
17 N_("git repack [<options>]"),
21 static int repack_config(const char *var
, const char *value
, void *cb
)
23 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
24 delta_base_offset
= git_config_bool(var
, value
);
27 if (!strcmp(var
, "repack.packkeptobjects")) {
28 pack_kept_objects
= git_config_bool(var
, value
);
31 if (!strcmp(var
, "repack.writebitmaps") ||
32 !strcmp(var
, "pack.writebitmaps")) {
33 write_bitmaps
= git_config_bool(var
, value
);
36 return git_default_config(var
, value
, cb
);
40 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
42 static void remove_temporary_files(void)
44 struct strbuf buf
= STRBUF_INIT
;
45 size_t dirlen
, prefixlen
;
49 dir
= opendir(packdir
);
53 /* Point at the slash at the end of ".../objects/pack/" */
54 dirlen
= strlen(packdir
) + 1;
55 strbuf_addstr(&buf
, packtmp
);
56 /* Hold the length of ".tmp-%d-pack-" */
57 prefixlen
= buf
.len
- dirlen
;
59 while ((e
= readdir(dir
))) {
60 if (strncmp(e
->d_name
, buf
.buf
+ dirlen
, prefixlen
))
62 strbuf_setlen(&buf
, dirlen
);
63 strbuf_addstr(&buf
, e
->d_name
);
70 static void remove_pack_on_signal(int signo
)
72 remove_temporary_files();
78 * Adds all packs hex strings to the fname list, which do not
79 * have a corresponding .keep file.
81 static void get_non_kept_pack_filenames(struct string_list
*fname_list
)
87 if (!(dir
= opendir(packdir
)))
90 while ((e
= readdir(dir
)) != NULL
) {
92 if (!strip_suffix(e
->d_name
, ".pack", &len
))
95 fname
= xmemdupz(e
->d_name
, len
);
97 if (!file_exists(mkpath("%s/%s.keep", packdir
, fname
)))
98 string_list_append_nodup(fname_list
, fname
);
105 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
107 const char *exts
[] = {".pack", ".idx", ".keep", ".bitmap"};
109 struct strbuf buf
= STRBUF_INIT
;
112 strbuf_addf(&buf
, "%s/%s", dir_name
, base_name
);
115 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
116 strbuf_setlen(&buf
, plen
);
117 strbuf_addstr(&buf
, exts
[i
]);
120 strbuf_release(&buf
);
123 #define ALL_INTO_ONE 1
124 #define LOOSEN_UNREACHABLE 2
126 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
136 struct child_process cmd
= CHILD_PROCESS_INIT
;
137 struct string_list_item
*item
;
138 struct string_list names
= STRING_LIST_INIT_DUP
;
139 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
140 struct string_list existing_packs
= STRING_LIST_INIT_DUP
;
141 struct strbuf line
= STRBUF_INIT
;
142 int ext
, ret
, failed
;
145 /* variables to be filled by option parsing */
146 int pack_everything
= 0;
147 int delete_redundant
= 0;
148 const char *unpack_unreachable
= NULL
;
149 int keep_unreachable
= 0;
150 const char *window
= NULL
, *window_memory
= NULL
;
151 const char *depth
= NULL
;
152 const char *max_pack_size
= NULL
;
153 int no_reuse_delta
= 0, no_reuse_object
= 0;
154 int no_update_server_info
= 0;
158 struct option builtin_repack_options
[] = {
159 OPT_BIT('a', NULL
, &pack_everything
,
160 N_("pack everything in a single pack"), ALL_INTO_ONE
),
161 OPT_BIT('A', NULL
, &pack_everything
,
162 N_("same as -a, and turn unreachable objects loose"),
163 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
164 OPT_BOOL('d', NULL
, &delete_redundant
,
165 N_("remove redundant packs, and run git-prune-packed")),
166 OPT_BOOL('f', NULL
, &no_reuse_delta
,
167 N_("pass --no-reuse-delta to git-pack-objects")),
168 OPT_BOOL('F', NULL
, &no_reuse_object
,
169 N_("pass --no-reuse-object to git-pack-objects")),
170 OPT_BOOL('n', NULL
, &no_update_server_info
,
171 N_("do not run git-update-server-info")),
172 OPT__QUIET(&quiet
, N_("be quiet")),
173 OPT_BOOL('l', "local", &local
,
174 N_("pass --local to git-pack-objects")),
175 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
176 N_("write bitmap index")),
177 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
178 N_("with -A, do not loosen objects older than this")),
179 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
180 N_("with -a, repack unreachable objects")),
181 OPT_STRING(0, "window", &window
, N_("n"),
182 N_("size of the window used for delta compression")),
183 OPT_STRING(0, "window-memory", &window_memory
, N_("bytes"),
184 N_("same as the above, but limit memory size instead of entries count")),
185 OPT_STRING(0, "depth", &depth
, N_("n"),
186 N_("limits the maximum delta depth")),
187 OPT_STRING(0, "max-pack-size", &max_pack_size
, N_("bytes"),
188 N_("maximum size of each packfile")),
189 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
190 N_("repack objects in packs marked with .keep")),
194 git_config(repack_config
, NULL
);
196 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
197 git_repack_usage
, 0);
199 if (delete_redundant
&& repository_format_precious_objects
)
200 die(_("cannot delete packs in a precious-objects repo"));
202 if (keep_unreachable
&&
203 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
204 die(_("--keep-unreachable and -A are incompatible"));
206 if (pack_kept_objects
< 0)
207 pack_kept_objects
= write_bitmaps
;
209 packdir
= mkpathdup("%s/pack", get_object_directory());
210 packtmp
= mkpathdup("%s/.tmp-%d-pack", packdir
, (int)getpid());
212 sigchain_push_common(remove_pack_on_signal
);
214 argv_array_push(&cmd
.args
, "pack-objects");
215 argv_array_push(&cmd
.args
, "--keep-true-parents");
216 if (!pack_kept_objects
)
217 argv_array_push(&cmd
.args
, "--honor-pack-keep");
218 argv_array_push(&cmd
.args
, "--non-empty");
219 argv_array_push(&cmd
.args
, "--all");
220 argv_array_push(&cmd
.args
, "--reflog");
221 argv_array_push(&cmd
.args
, "--indexed-objects");
223 argv_array_pushf(&cmd
.args
, "--window=%s", window
);
225 argv_array_pushf(&cmd
.args
, "--window-memory=%s", window_memory
);
227 argv_array_pushf(&cmd
.args
, "--depth=%s", depth
);
229 argv_array_pushf(&cmd
.args
, "--max-pack-size=%s", max_pack_size
);
231 argv_array_pushf(&cmd
.args
, "--no-reuse-delta");
233 argv_array_pushf(&cmd
.args
, "--no-reuse-object");
235 argv_array_push(&cmd
.args
, "--write-bitmap-index");
237 if (pack_everything
& ALL_INTO_ONE
) {
238 get_non_kept_pack_filenames(&existing_packs
);
240 if (existing_packs
.nr
&& delete_redundant
) {
241 if (unpack_unreachable
) {
242 argv_array_pushf(&cmd
.args
,
243 "--unpack-unreachable=%s",
245 argv_array_push(&cmd
.env_array
, "GIT_REF_PARANOIA=1");
246 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
247 argv_array_push(&cmd
.args
,
248 "--unpack-unreachable");
249 } else if (keep_unreachable
) {
250 argv_array_push(&cmd
.args
, "--keep-unreachable");
251 argv_array_push(&cmd
.args
, "--pack-loose-unreachable");
253 argv_array_push(&cmd
.env_array
, "GIT_REF_PARANOIA=1");
257 argv_array_push(&cmd
.args
, "--unpacked");
258 argv_array_push(&cmd
.args
, "--incremental");
262 argv_array_push(&cmd
.args
, "--local");
264 argv_array_push(&cmd
.args
, "--quiet");
265 if (delta_base_offset
)
266 argv_array_push(&cmd
.args
, "--delta-base-offset");
268 argv_array_push(&cmd
.args
, packtmp
);
274 ret
= start_command(&cmd
);
278 out
= xfdopen(cmd
.out
, "r");
279 while (strbuf_getline_lf(&line
, out
) != EOF
) {
281 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
282 string_list_append(&names
, line
.buf
);
285 ret
= finish_command(&cmd
);
289 if (!names
.nr
&& !quiet
)
290 printf("Nothing new to pack.\n");
293 * Ok we have prepared all new packfiles.
294 * First see if there are packs of the same name and if so
295 * if we can move them out of the way (this can happen if we
296 * repacked immediately after packing fully.
299 for_each_string_list_item(item
, &names
) {
300 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
301 char *fname
, *fname_old
;
302 fname
= mkpathdup("%s/pack-%s%s", packdir
,
303 item
->string
, exts
[ext
].name
);
304 if (!file_exists(fname
)) {
309 fname_old
= mkpathdup("%s/old-%s%s", packdir
,
310 item
->string
, exts
[ext
].name
);
311 if (file_exists(fname_old
))
312 if (unlink(fname_old
))
315 if (!failed
&& rename(fname
, fname_old
)) {
321 string_list_append(&rollback
, fname
);
329 struct string_list rollback_failure
= STRING_LIST_INIT_DUP
;
330 for_each_string_list_item(item
, &rollback
) {
331 char *fname
, *fname_old
;
332 fname
= mkpathdup("%s/%s", packdir
, item
->string
);
333 fname_old
= mkpathdup("%s/old-%s", packdir
, item
->string
);
334 if (rename(fname_old
, fname
))
335 string_list_append(&rollback_failure
, fname
);
340 if (rollback_failure
.nr
) {
343 "WARNING: Some packs in use have been renamed by\n"
344 "WARNING: prefixing old- to their name, in order to\n"
345 "WARNING: replace them with the new version of the\n"
346 "WARNING: file. But the operation failed, and the\n"
347 "WARNING: attempt to rename them back to their\n"
348 "WARNING: original names also failed.\n"
349 "WARNING: Please rename them in %s manually:\n", packdir
);
350 for (i
= 0; i
< rollback_failure
.nr
; i
++)
351 fprintf(stderr
, "WARNING: old-%s -> %s\n",
352 rollback_failure
.items
[i
].string
,
353 rollback_failure
.items
[i
].string
);
358 /* Now the ones with the same name are out of the way... */
359 for_each_string_list_item(item
, &names
) {
360 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
361 char *fname
, *fname_old
;
362 struct stat statbuffer
;
364 fname
= mkpathdup("%s/pack-%s%s",
365 packdir
, item
->string
, exts
[ext
].name
);
366 fname_old
= mkpathdup("%s-%s%s",
367 packtmp
, item
->string
, exts
[ext
].name
);
368 if (!stat(fname_old
, &statbuffer
)) {
369 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
370 chmod(fname_old
, statbuffer
.st_mode
);
373 if (exists
|| !exts
[ext
].optional
) {
374 if (rename(fname_old
, fname
))
375 die_errno(_("renaming '%s' failed"), fname_old
);
382 /* Remove the "old-" files */
383 for_each_string_list_item(item
, &names
) {
384 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
386 fname
= mkpathdup("%s/old-%s%s",
390 if (remove_path(fname
))
391 warning(_("failed to remove '%s'"), fname
);
396 /* End of pack replacement. */
398 if (delete_redundant
) {
400 string_list_sort(&names
);
401 for_each_string_list_item(item
, &existing_packs
) {
403 size_t len
= strlen(item
->string
);
406 sha1
= item
->string
+ len
- 40;
407 if (!string_list_has_string(&names
, sha1
))
408 remove_redundant_pack(packdir
, item
->string
);
410 if (!quiet
&& isatty(2))
411 opts
|= PRUNE_PACKED_VERBOSE
;
412 prune_packed_objects(opts
);
415 if (!no_update_server_info
)
416 update_server_info(0);
417 remove_temporary_files();
418 string_list_clear(&names
, 0);
419 string_list_clear(&rollback
, 0);
420 string_list_clear(&existing_packs
, 0);
421 strbuf_release(&line
);