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 (suffixcmp(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"};
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
)
118 const char *exts
[2] = {".pack", ".idx"};
119 struct child_process cmd
;
120 struct string_list_item
*item
;
121 struct argv_array cmd_args
= ARGV_ARRAY_INIT
;
122 struct string_list names
= STRING_LIST_INIT_DUP
;
123 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
124 struct string_list existing_packs
= STRING_LIST_INIT_DUP
;
125 struct strbuf line
= STRBUF_INIT
;
126 int nr_packs
, ext
, ret
, failed
;
129 /* variables to be filled by option parsing */
130 int pack_everything
= 0;
131 int delete_redundant
= 0;
132 char *unpack_unreachable
= NULL
;
133 int window
= 0, window_memory
= 0;
135 int max_pack_size
= 0;
136 int no_reuse_delta
= 0, no_reuse_object
= 0;
137 int no_update_server_info
= 0;
141 struct option builtin_repack_options
[] = {
142 OPT_BIT('a', NULL
, &pack_everything
,
143 N_("pack everything in a single pack"), ALL_INTO_ONE
),
144 OPT_BIT('A', NULL
, &pack_everything
,
145 N_("same as -a, and turn unreachable objects loose"),
146 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
147 OPT_BOOL('d', NULL
, &delete_redundant
,
148 N_("remove redundant packs, and run git-prune-packed")),
149 OPT_BOOL('f', NULL
, &no_reuse_delta
,
150 N_("pass --no-reuse-delta to git-pack-objects")),
151 OPT_BOOL('F', NULL
, &no_reuse_object
,
152 N_("pass --no-reuse-object to git-pack-objects")),
153 OPT_BOOL('n', NULL
, &no_update_server_info
,
154 N_("do not run git-update-server-info")),
155 OPT__QUIET(&quiet
, N_("be quiet")),
156 OPT_BOOL('l', "local", &local
,
157 N_("pass --local to git-pack-objects")),
158 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
159 N_("with -A, do not loosen objects older than this")),
160 OPT_INTEGER(0, "window", &window
,
161 N_("size of the window used for delta compression")),
162 OPT_INTEGER(0, "window-memory", &window_memory
,
163 N_("same as the above, but limit memory size instead of entries count")),
164 OPT_INTEGER(0, "depth", &depth
,
165 N_("limits the maximum delta depth")),
166 OPT_INTEGER(0, "max-pack-size", &max_pack_size
,
167 N_("maximum size of each packfile")),
171 git_config(repack_config
, NULL
);
173 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
174 git_repack_usage
, 0);
176 packdir
= mkpathdup("%s/pack", get_object_directory());
177 packtmp
= mkpathdup("%s/.tmp-%d-pack", packdir
, (int)getpid());
179 sigchain_push_common(remove_pack_on_signal
);
181 argv_array_push(&cmd_args
, "pack-objects");
182 argv_array_push(&cmd_args
, "--keep-true-parents");
183 argv_array_push(&cmd_args
, "--honor-pack-keep");
184 argv_array_push(&cmd_args
, "--non-empty");
185 argv_array_push(&cmd_args
, "--all");
186 argv_array_push(&cmd_args
, "--reflog");
188 argv_array_pushf(&cmd_args
, "--window=%u", window
);
190 argv_array_pushf(&cmd_args
, "--window-memory=%u", window_memory
);
192 argv_array_pushf(&cmd_args
, "--depth=%u", depth
);
194 argv_array_pushf(&cmd_args
, "--max_pack_size=%u", max_pack_size
);
196 argv_array_pushf(&cmd_args
, "--no-reuse-delta");
198 argv_array_pushf(&cmd_args
, "--no-reuse-object");
200 if (pack_everything
& ALL_INTO_ONE
) {
201 get_non_kept_pack_filenames(&existing_packs
);
203 if (existing_packs
.nr
&& delete_redundant
) {
204 if (unpack_unreachable
)
205 argv_array_pushf(&cmd_args
,
206 "--unpack-unreachable=%s",
208 else if (pack_everything
& LOOSEN_UNREACHABLE
)
209 argv_array_push(&cmd_args
,
210 "--unpack-unreachable");
213 argv_array_push(&cmd_args
, "--unpacked");
214 argv_array_push(&cmd_args
, "--incremental");
218 argv_array_push(&cmd_args
, "--local");
220 argv_array_push(&cmd_args
, "--quiet");
221 if (delta_base_offset
)
222 argv_array_push(&cmd_args
, "--delta-base-offset");
224 argv_array_push(&cmd_args
, packtmp
);
226 memset(&cmd
, 0, sizeof(cmd
));
227 cmd
.argv
= cmd_args
.argv
;
232 ret
= start_command(&cmd
);
237 out
= xfdopen(cmd
.out
, "r");
238 while (strbuf_getline(&line
, out
, '\n') != EOF
) {
240 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
241 string_list_append(&names
, line
.buf
);
245 ret
= finish_command(&cmd
);
248 argv_array_clear(&cmd_args
);
250 if (!nr_packs
&& !quiet
)
251 printf("Nothing new to pack.\n");
254 * Ok we have prepared all new packfiles.
255 * First see if there are packs of the same name and if so
256 * if we can move them out of the way (this can happen if we
257 * repacked immediately after packing fully.
260 for_each_string_list_item(item
, &names
) {
261 for (ext
= 0; ext
< 2; ext
++) {
262 char *fname
, *fname_old
;
263 fname
= mkpathdup("%s/%s%s", packdir
,
264 item
->string
, exts
[ext
]);
265 if (!file_exists(fname
)) {
270 fname_old
= mkpath("%s/old-%s%s", packdir
,
271 item
->string
, exts
[ext
]);
272 if (file_exists(fname_old
))
273 if (unlink(fname_old
))
276 if (!failed
&& rename(fname
, fname_old
)) {
281 string_list_append(&rollback
, fname
);
288 struct string_list rollback_failure
= STRING_LIST_INIT_DUP
;
289 for_each_string_list_item(item
, &rollback
) {
290 char *fname
, *fname_old
;
291 fname
= mkpathdup("%s/%s", packdir
, item
->string
);
292 fname_old
= mkpath("%s/old-%s", packdir
, item
->string
);
293 if (rename(fname_old
, fname
))
294 string_list_append(&rollback_failure
, fname
);
298 if (rollback_failure
.nr
) {
301 "WARNING: Some packs in use have been renamed by\n"
302 "WARNING: prefixing old- to their name, in order to\n"
303 "WARNING: replace them with the new version of the\n"
304 "WARNING: file. But the operation failed, and the\n"
305 "WARNING: attempt to rename them back to their\n"
306 "WARNING: original names also failed.\n"
307 "WARNING: Please rename them in %s manually:\n", packdir
);
308 for (i
= 0; i
< rollback_failure
.nr
; i
++)
309 fprintf(stderr
, "WARNING: old-%s -> %s\n",
310 rollback_failure
.items
[i
].string
,
311 rollback_failure
.items
[i
].string
);
316 /* Now the ones with the same name are out of the way... */
317 for_each_string_list_item(item
, &names
) {
318 for (ext
= 0; ext
< 2; ext
++) {
319 char *fname
, *fname_old
;
320 struct stat statbuffer
;
321 fname
= mkpathdup("%s/pack-%s%s",
322 packdir
, item
->string
, exts
[ext
]);
323 fname_old
= mkpathdup("%s-%s%s",
324 packtmp
, item
->string
, exts
[ext
]);
325 if (!stat(fname_old
, &statbuffer
)) {
326 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
327 chmod(fname_old
, statbuffer
.st_mode
);
329 if (rename(fname_old
, fname
))
330 die_errno(_("renaming '%s' failed"), fname_old
);
336 /* Remove the "old-" files */
337 for_each_string_list_item(item
, &names
) {
338 for (ext
= 0; ext
< 2; ext
++) {
340 fname
= mkpath("%s/old-pack-%s%s",
344 if (remove_path(fname
))
345 warning(_("removing '%s' failed"), fname
);
349 /* End of pack replacement. */
351 if (delete_redundant
) {
352 sort_string_list(&names
);
353 for_each_string_list_item(item
, &existing_packs
) {
355 size_t len
= strlen(item
->string
);
358 sha1
= item
->string
+ len
- 40;
359 if (!string_list_has_string(&names
, sha1
))
360 remove_redundant_pack(packdir
, item
->string
);
362 argv_array_push(&cmd_args
, "prune-packed");
364 argv_array_push(&cmd_args
, "--quiet");
366 memset(&cmd
, 0, sizeof(cmd
));
367 cmd
.argv
= cmd_args
.argv
;
370 argv_array_clear(&cmd_args
);
373 if (!no_update_server_info
) {
374 argv_array_push(&cmd_args
, "update-server-info");
375 memset(&cmd
, 0, sizeof(cmd
));
376 cmd
.argv
= cmd_args
.argv
;
379 argv_array_clear(&cmd_args
);
381 remove_temporary_files();
382 string_list_clear(&names
, 0);
383 string_list_clear(&rollback
, 0);
384 string_list_clear(&existing_packs
, 0);
385 strbuf_release(&line
);