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 char *packdir
, *packtmp
;
15 static const char *const git_repack_usage
[] = {
16 N_("git repack [options]"),
20 static int repack_config(const char *var
, const char *value
, void *cb
)
22 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
23 delta_base_offset
= git_config_bool(var
, value
);
26 if (!strcmp(var
, "repack.packkeptobjects")) {
27 pack_kept_objects
= git_config_bool(var
, value
);
30 return git_default_config(var
, value
, cb
);
34 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
36 static void remove_temporary_files(void)
38 struct strbuf buf
= STRBUF_INIT
;
39 size_t dirlen
, prefixlen
;
43 dir
= opendir(packdir
);
47 /* Point at the slash at the end of ".../objects/pack/" */
48 dirlen
= strlen(packdir
) + 1;
49 strbuf_addstr(&buf
, packtmp
);
50 /* Hold the length of ".tmp-%d-pack-" */
51 prefixlen
= buf
.len
- dirlen
;
53 while ((e
= readdir(dir
))) {
54 if (strncmp(e
->d_name
, buf
.buf
+ dirlen
, prefixlen
))
56 strbuf_setlen(&buf
, dirlen
);
57 strbuf_addstr(&buf
, e
->d_name
);
64 static void remove_pack_on_signal(int signo
)
66 remove_temporary_files();
72 * Adds all packs hex strings to the fname list, which do not
73 * have a corresponding .keep file.
75 static void get_non_kept_pack_filenames(struct string_list
*fname_list
)
82 if (!(dir
= opendir(packdir
)))
85 while ((e
= readdir(dir
)) != NULL
) {
86 if (!ends_with(e
->d_name
, ".pack"))
89 len
= strlen(e
->d_name
) - strlen(".pack");
90 fname
= xmemdupz(e
->d_name
, len
);
92 if (!file_exists(mkpath("%s/%s.keep", packdir
, fname
)))
93 string_list_append_nodup(fname_list
, fname
);
100 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
102 const char *exts
[] = {".pack", ".idx", ".keep", ".bitmap"};
104 struct strbuf buf
= STRBUF_INIT
;
107 strbuf_addf(&buf
, "%s/%s", dir_name
, base_name
);
110 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
111 strbuf_setlen(&buf
, plen
);
112 strbuf_addstr(&buf
, exts
[i
]);
115 strbuf_release(&buf
);
118 #define ALL_INTO_ONE 1
119 #define LOOSEN_UNREACHABLE 2
121 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
131 struct child_process cmd
;
132 struct string_list_item
*item
;
133 struct argv_array cmd_args
= ARGV_ARRAY_INIT
;
134 struct string_list names
= STRING_LIST_INIT_DUP
;
135 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
136 struct string_list existing_packs
= STRING_LIST_INIT_DUP
;
137 struct strbuf line
= STRBUF_INIT
;
138 int ext
, ret
, failed
;
141 /* variables to be filled by option parsing */
142 int pack_everything
= 0;
143 int delete_redundant
= 0;
144 const char *unpack_unreachable
= NULL
;
145 const char *window
= NULL
, *window_memory
= NULL
;
146 const char *depth
= NULL
;
147 const char *max_pack_size
= NULL
;
148 int no_reuse_delta
= 0, no_reuse_object
= 0;
149 int no_update_server_info
= 0;
152 int write_bitmap
= -1;
154 struct option builtin_repack_options
[] = {
155 OPT_BIT('a', NULL
, &pack_everything
,
156 N_("pack everything in a single pack"), ALL_INTO_ONE
),
157 OPT_BIT('A', NULL
, &pack_everything
,
158 N_("same as -a, and turn unreachable objects loose"),
159 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
160 OPT_BOOL('d', NULL
, &delete_redundant
,
161 N_("remove redundant packs, and run git-prune-packed")),
162 OPT_BOOL('f', NULL
, &no_reuse_delta
,
163 N_("pass --no-reuse-delta to git-pack-objects")),
164 OPT_BOOL('F', NULL
, &no_reuse_object
,
165 N_("pass --no-reuse-object to git-pack-objects")),
166 OPT_BOOL('n', NULL
, &no_update_server_info
,
167 N_("do not run git-update-server-info")),
168 OPT__QUIET(&quiet
, N_("be quiet")),
169 OPT_BOOL('l', "local", &local
,
170 N_("pass --local to git-pack-objects")),
171 OPT_BOOL('b', "write-bitmap-index", &write_bitmap
,
172 N_("write bitmap index")),
173 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
174 N_("with -A, do not loosen objects older than this")),
175 OPT_STRING(0, "window", &window
, N_("n"),
176 N_("size of the window used for delta compression")),
177 OPT_STRING(0, "window-memory", &window_memory
, N_("bytes"),
178 N_("same as the above, but limit memory size instead of entries count")),
179 OPT_STRING(0, "depth", &depth
, N_("n"),
180 N_("limits the maximum delta depth")),
181 OPT_STRING(0, "max-pack-size", &max_pack_size
, N_("bytes"),
182 N_("maximum size of each packfile")),
183 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
184 N_("repack objects in packs marked with .keep")),
188 git_config(repack_config
, NULL
);
190 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
191 git_repack_usage
, 0);
193 if (pack_kept_objects
< 0)
194 pack_kept_objects
= write_bitmap
;
196 packdir
= mkpathdup("%s/pack", get_object_directory());
197 packtmp
= mkpathdup("%s/.tmp-%d-pack", packdir
, (int)getpid());
199 sigchain_push_common(remove_pack_on_signal
);
201 argv_array_push(&cmd_args
, "pack-objects");
202 argv_array_push(&cmd_args
, "--keep-true-parents");
203 if (!pack_kept_objects
)
204 argv_array_push(&cmd_args
, "--honor-pack-keep");
205 argv_array_push(&cmd_args
, "--non-empty");
206 argv_array_push(&cmd_args
, "--all");
207 argv_array_push(&cmd_args
, "--reflog");
209 argv_array_pushf(&cmd_args
, "--window=%s", window
);
211 argv_array_pushf(&cmd_args
, "--window-memory=%s", window_memory
);
213 argv_array_pushf(&cmd_args
, "--depth=%s", depth
);
215 argv_array_pushf(&cmd_args
, "--max-pack-size=%s", max_pack_size
);
217 argv_array_pushf(&cmd_args
, "--no-reuse-delta");
219 argv_array_pushf(&cmd_args
, "--no-reuse-object");
220 if (write_bitmap
>= 0)
221 argv_array_pushf(&cmd_args
, "--%swrite-bitmap-index",
222 write_bitmap
? "" : "no-");
224 if (pack_everything
& ALL_INTO_ONE
) {
225 get_non_kept_pack_filenames(&existing_packs
);
227 if (existing_packs
.nr
&& delete_redundant
) {
228 if (unpack_unreachable
)
229 argv_array_pushf(&cmd_args
,
230 "--unpack-unreachable=%s",
232 else if (pack_everything
& LOOSEN_UNREACHABLE
)
233 argv_array_push(&cmd_args
,
234 "--unpack-unreachable");
237 argv_array_push(&cmd_args
, "--unpacked");
238 argv_array_push(&cmd_args
, "--incremental");
242 argv_array_push(&cmd_args
, "--local");
244 argv_array_push(&cmd_args
, "--quiet");
245 if (delta_base_offset
)
246 argv_array_push(&cmd_args
, "--delta-base-offset");
248 argv_array_push(&cmd_args
, packtmp
);
250 memset(&cmd
, 0, sizeof(cmd
));
251 cmd
.argv
= cmd_args
.argv
;
256 ret
= start_command(&cmd
);
260 out
= xfdopen(cmd
.out
, "r");
261 while (strbuf_getline(&line
, out
, '\n') != EOF
) {
263 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
264 string_list_append(&names
, line
.buf
);
267 ret
= finish_command(&cmd
);
270 argv_array_clear(&cmd_args
);
272 if (!names
.nr
&& !quiet
)
273 printf("Nothing new to pack.\n");
276 * Ok we have prepared all new packfiles.
277 * First see if there are packs of the same name and if so
278 * if we can move them out of the way (this can happen if we
279 * repacked immediately after packing fully.
282 for_each_string_list_item(item
, &names
) {
283 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
284 char *fname
, *fname_old
;
285 fname
= mkpathdup("%s/pack-%s%s", packdir
,
286 item
->string
, exts
[ext
].name
);
287 if (!file_exists(fname
)) {
292 fname_old
= mkpath("%s/old-%s%s", packdir
,
293 item
->string
, exts
[ext
].name
);
294 if (file_exists(fname_old
))
295 if (unlink(fname_old
))
298 if (!failed
&& rename(fname
, fname_old
)) {
303 string_list_append(&rollback
, fname
);
310 struct string_list rollback_failure
= STRING_LIST_INIT_DUP
;
311 for_each_string_list_item(item
, &rollback
) {
312 char *fname
, *fname_old
;
313 fname
= mkpathdup("%s/%s", packdir
, item
->string
);
314 fname_old
= mkpath("%s/old-%s", packdir
, item
->string
);
315 if (rename(fname_old
, fname
))
316 string_list_append(&rollback_failure
, fname
);
320 if (rollback_failure
.nr
) {
323 "WARNING: Some packs in use have been renamed by\n"
324 "WARNING: prefixing old- to their name, in order to\n"
325 "WARNING: replace them with the new version of the\n"
326 "WARNING: file. But the operation failed, and the\n"
327 "WARNING: attempt to rename them back to their\n"
328 "WARNING: original names also failed.\n"
329 "WARNING: Please rename them in %s manually:\n", packdir
);
330 for (i
= 0; i
< rollback_failure
.nr
; i
++)
331 fprintf(stderr
, "WARNING: old-%s -> %s\n",
332 rollback_failure
.items
[i
].string
,
333 rollback_failure
.items
[i
].string
);
338 /* Now the ones with the same name are out of the way... */
339 for_each_string_list_item(item
, &names
) {
340 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
341 char *fname
, *fname_old
;
342 struct stat statbuffer
;
344 fname
= mkpathdup("%s/pack-%s%s",
345 packdir
, item
->string
, exts
[ext
].name
);
346 fname_old
= mkpathdup("%s-%s%s",
347 packtmp
, item
->string
, exts
[ext
].name
);
348 if (!stat(fname_old
, &statbuffer
)) {
349 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
350 chmod(fname_old
, statbuffer
.st_mode
);
353 if (exists
|| !exts
[ext
].optional
) {
354 if (rename(fname_old
, fname
))
355 die_errno(_("renaming '%s' failed"), fname_old
);
362 /* Remove the "old-" files */
363 for_each_string_list_item(item
, &names
) {
364 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
366 fname
= mkpath("%s/old-%s%s",
370 if (remove_path(fname
))
371 warning(_("removing '%s' failed"), fname
);
375 /* End of pack replacement. */
377 if (delete_redundant
) {
378 sort_string_list(&names
);
379 for_each_string_list_item(item
, &existing_packs
) {
381 size_t len
= strlen(item
->string
);
384 sha1
= item
->string
+ len
- 40;
385 if (!string_list_has_string(&names
, sha1
))
386 remove_redundant_pack(packdir
, item
->string
);
388 argv_array_push(&cmd_args
, "prune-packed");
390 argv_array_push(&cmd_args
, "--quiet");
392 memset(&cmd
, 0, sizeof(cmd
));
393 cmd
.argv
= cmd_args
.argv
;
396 argv_array_clear(&cmd_args
);
399 if (!no_update_server_info
) {
400 argv_array_push(&cmd_args
, "update-server-info");
401 memset(&cmd
, 0, sizeof(cmd
));
402 cmd
.argv
= cmd_args
.argv
;
405 argv_array_clear(&cmd_args
);
407 remove_temporary_files();
408 string_list_clear(&names
, 0);
409 string_list_clear(&rollback
, 0);
410 string_list_clear(&existing_packs
, 0);
411 strbuf_release(&line
);