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 char *packdir
, *packtmp
;
14 static const char *const git_repack_usage
[] = {
15 N_("git repack [options]"),
19 static int repack_config(const char *var
, const char *value
, void *cb
)
21 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
22 delta_base_offset
= git_config_bool(var
, value
);
25 return git_default_config(var
, value
, cb
);
29 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
31 static void remove_temporary_files(void)
33 struct strbuf buf
= STRBUF_INIT
;
34 size_t dirlen
, prefixlen
;
38 dir
= opendir(packdir
);
42 /* Point at the slash at the end of ".../objects/pack/" */
43 dirlen
= strlen(packdir
) + 1;
44 strbuf_addstr(&buf
, packtmp
);
45 /* Hold the length of ".tmp-%d-pack-" */
46 prefixlen
= buf
.len
- dirlen
;
48 while ((e
= readdir(dir
))) {
49 if (strncmp(e
->d_name
, buf
.buf
+ dirlen
, prefixlen
))
51 strbuf_setlen(&buf
, dirlen
);
52 strbuf_addstr(&buf
, e
->d_name
);
59 static void remove_pack_on_signal(int signo
)
61 remove_temporary_files();
67 * Adds all packs hex strings to the fname list, which do not
68 * have a corresponding .keep file.
70 static void get_non_kept_pack_filenames(struct string_list
*fname_list
)
77 if (!(dir
= opendir(packdir
)))
80 while ((e
= readdir(dir
)) != NULL
) {
81 if (!ends_with(e
->d_name
, ".pack"))
84 len
= strlen(e
->d_name
) - strlen(".pack");
85 fname
= xmemdupz(e
->d_name
, len
);
87 if (!file_exists(mkpath("%s/%s.keep", packdir
, fname
)))
88 string_list_append_nodup(fname_list
, fname
);
95 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
97 const char *exts
[] = {".pack", ".idx", ".keep", ".bitmap"};
99 struct strbuf buf
= STRBUF_INIT
;
102 strbuf_addf(&buf
, "%s/%s", dir_name
, base_name
);
105 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
106 strbuf_setlen(&buf
, plen
);
107 strbuf_addstr(&buf
, exts
[i
]);
110 strbuf_release(&buf
);
113 #define ALL_INTO_ONE 1
114 #define LOOSEN_UNREACHABLE 2
116 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
126 struct child_process cmd
;
127 struct string_list_item
*item
;
128 struct argv_array cmd_args
= ARGV_ARRAY_INIT
;
129 struct string_list names
= STRING_LIST_INIT_DUP
;
130 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
131 struct string_list existing_packs
= STRING_LIST_INIT_DUP
;
132 struct strbuf line
= STRBUF_INIT
;
133 int ext
, ret
, failed
;
136 /* variables to be filled by option parsing */
137 int pack_everything
= 0;
138 int delete_redundant
= 0;
139 const char *unpack_unreachable
= NULL
;
140 const char *window
= NULL
, *window_memory
= NULL
;
141 const char *depth
= NULL
;
142 const char *max_pack_size
= NULL
;
143 int no_reuse_delta
= 0, no_reuse_object
= 0;
144 int no_update_server_info
= 0;
147 int write_bitmap
= -1;
149 struct option builtin_repack_options
[] = {
150 OPT_BIT('a', NULL
, &pack_everything
,
151 N_("pack everything in a single pack"), ALL_INTO_ONE
),
152 OPT_BIT('A', NULL
, &pack_everything
,
153 N_("same as -a, and turn unreachable objects loose"),
154 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
155 OPT_BOOL('d', NULL
, &delete_redundant
,
156 N_("remove redundant packs, and run git-prune-packed")),
157 OPT_BOOL('f', NULL
, &no_reuse_delta
,
158 N_("pass --no-reuse-delta to git-pack-objects")),
159 OPT_BOOL('F', NULL
, &no_reuse_object
,
160 N_("pass --no-reuse-object to git-pack-objects")),
161 OPT_BOOL('n', NULL
, &no_update_server_info
,
162 N_("do not run git-update-server-info")),
163 OPT__QUIET(&quiet
, N_("be quiet")),
164 OPT_BOOL('l', "local", &local
,
165 N_("pass --local to git-pack-objects")),
166 OPT_BOOL('b', "write-bitmap-index", &write_bitmap
,
167 N_("write bitmap index")),
168 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
169 N_("with -A, do not loosen objects older than this")),
170 OPT_STRING(0, "window", &window
, N_("n"),
171 N_("size of the window used for delta compression")),
172 OPT_STRING(0, "window-memory", &window_memory
, N_("bytes"),
173 N_("same as the above, but limit memory size instead of entries count")),
174 OPT_STRING(0, "depth", &depth
, N_("n"),
175 N_("limits the maximum delta depth")),
176 OPT_STRING(0, "max-pack-size", &max_pack_size
, N_("bytes"),
177 N_("maximum size of each packfile")),
181 git_config(repack_config
, NULL
);
183 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
184 git_repack_usage
, 0);
186 packdir
= mkpathdup("%s/pack", get_object_directory());
187 packtmp
= mkpathdup("%s/.tmp-%d-pack", packdir
, (int)getpid());
189 sigchain_push_common(remove_pack_on_signal
);
191 argv_array_push(&cmd_args
, "pack-objects");
192 argv_array_push(&cmd_args
, "--keep-true-parents");
193 argv_array_push(&cmd_args
, "--honor-pack-keep");
194 argv_array_push(&cmd_args
, "--non-empty");
195 argv_array_push(&cmd_args
, "--all");
196 argv_array_push(&cmd_args
, "--reflog");
198 argv_array_pushf(&cmd_args
, "--window=%s", window
);
200 argv_array_pushf(&cmd_args
, "--window-memory=%s", window_memory
);
202 argv_array_pushf(&cmd_args
, "--depth=%s", depth
);
204 argv_array_pushf(&cmd_args
, "--max-pack-size=%s", max_pack_size
);
206 argv_array_pushf(&cmd_args
, "--no-reuse-delta");
208 argv_array_pushf(&cmd_args
, "--no-reuse-object");
209 if (write_bitmap
>= 0)
210 argv_array_pushf(&cmd_args
, "--%swrite-bitmap-index",
211 write_bitmap
? "" : "no-");
213 if (pack_everything
& ALL_INTO_ONE
) {
214 get_non_kept_pack_filenames(&existing_packs
);
216 if (existing_packs
.nr
&& delete_redundant
) {
217 if (unpack_unreachable
)
218 argv_array_pushf(&cmd_args
,
219 "--unpack-unreachable=%s",
221 else if (pack_everything
& LOOSEN_UNREACHABLE
)
222 argv_array_push(&cmd_args
,
223 "--unpack-unreachable");
226 argv_array_push(&cmd_args
, "--unpacked");
227 argv_array_push(&cmd_args
, "--incremental");
231 argv_array_push(&cmd_args
, "--local");
233 argv_array_push(&cmd_args
, "--quiet");
234 if (delta_base_offset
)
235 argv_array_push(&cmd_args
, "--delta-base-offset");
237 argv_array_push(&cmd_args
, packtmp
);
239 memset(&cmd
, 0, sizeof(cmd
));
240 cmd
.argv
= cmd_args
.argv
;
245 ret
= start_command(&cmd
);
249 out
= xfdopen(cmd
.out
, "r");
250 while (strbuf_getline(&line
, out
, '\n') != EOF
) {
252 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
253 string_list_append(&names
, line
.buf
);
256 ret
= finish_command(&cmd
);
259 argv_array_clear(&cmd_args
);
261 if (!names
.nr
&& !quiet
)
262 printf("Nothing new to pack.\n");
265 * Ok we have prepared all new packfiles.
266 * First see if there are packs of the same name and if so
267 * if we can move them out of the way (this can happen if we
268 * repacked immediately after packing fully.
271 for_each_string_list_item(item
, &names
) {
272 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
273 char *fname
, *fname_old
;
274 fname
= mkpathdup("%s/pack-%s%s", packdir
,
275 item
->string
, exts
[ext
].name
);
276 if (!file_exists(fname
)) {
281 fname_old
= mkpath("%s/old-%s%s", packdir
,
282 item
->string
, exts
[ext
].name
);
283 if (file_exists(fname_old
))
284 if (unlink(fname_old
))
287 if (!failed
&& rename(fname
, fname_old
)) {
292 string_list_append(&rollback
, fname
);
299 struct string_list rollback_failure
= STRING_LIST_INIT_DUP
;
300 for_each_string_list_item(item
, &rollback
) {
301 char *fname
, *fname_old
;
302 fname
= mkpathdup("%s/%s", packdir
, item
->string
);
303 fname_old
= mkpath("%s/old-%s", packdir
, item
->string
);
304 if (rename(fname_old
, fname
))
305 string_list_append(&rollback_failure
, fname
);
309 if (rollback_failure
.nr
) {
312 "WARNING: Some packs in use have been renamed by\n"
313 "WARNING: prefixing old- to their name, in order to\n"
314 "WARNING: replace them with the new version of the\n"
315 "WARNING: file. But the operation failed, and the\n"
316 "WARNING: attempt to rename them back to their\n"
317 "WARNING: original names also failed.\n"
318 "WARNING: Please rename them in %s manually:\n", packdir
);
319 for (i
= 0; i
< rollback_failure
.nr
; i
++)
320 fprintf(stderr
, "WARNING: old-%s -> %s\n",
321 rollback_failure
.items
[i
].string
,
322 rollback_failure
.items
[i
].string
);
327 /* Now the ones with the same name are out of the way... */
328 for_each_string_list_item(item
, &names
) {
329 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
330 char *fname
, *fname_old
;
331 struct stat statbuffer
;
333 fname
= mkpathdup("%s/pack-%s%s",
334 packdir
, item
->string
, exts
[ext
].name
);
335 fname_old
= mkpathdup("%s-%s%s",
336 packtmp
, item
->string
, exts
[ext
].name
);
337 if (!stat(fname_old
, &statbuffer
)) {
338 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
339 chmod(fname_old
, statbuffer
.st_mode
);
342 if (exists
|| !exts
[ext
].optional
) {
343 if (rename(fname_old
, fname
))
344 die_errno(_("renaming '%s' failed"), fname_old
);
351 /* Remove the "old-" files */
352 for_each_string_list_item(item
, &names
) {
353 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
355 fname
= mkpath("%s/old-%s%s",
359 if (remove_path(fname
))
360 warning(_("removing '%s' failed"), fname
);
364 /* End of pack replacement. */
366 if (delete_redundant
) {
367 sort_string_list(&names
);
368 for_each_string_list_item(item
, &existing_packs
) {
370 size_t len
= strlen(item
->string
);
373 sha1
= item
->string
+ len
- 40;
374 if (!string_list_has_string(&names
, sha1
))
375 remove_redundant_pack(packdir
, item
->string
);
377 argv_array_push(&cmd_args
, "prune-packed");
379 argv_array_push(&cmd_args
, "--quiet");
381 memset(&cmd
, 0, sizeof(cmd
));
382 cmd
.argv
= cmd_args
.argv
;
385 argv_array_clear(&cmd_args
);
388 if (!no_update_server_info
) {
389 argv_array_push(&cmd_args
, "update-server-info");
390 memset(&cmd
, 0, sizeof(cmd
));
391 cmd
.argv
= cmd_args
.argv
;
394 argv_array_clear(&cmd_args
);
396 remove_temporary_files();
397 string_list_clear(&names
, 0);
398 string_list_clear(&rollback
, 0);
399 string_list_clear(&existing_packs
, 0);
400 strbuf_release(&line
);