1#include "builtin.h"2#include "cache.h"3#include "dir.h"4#include "parse-options.h"5#include "run-command.h"6#include "sigchain.h"7#include "strbuf.h"8#include "string-list.h"9#include "argv-array.h"1011static int delta_base_offset = 1;12static int pack_kept_objects = -1;13static int write_bitmaps;14static char *packdir, *packtmp;1516static const char *const git_repack_usage[] = {17N_("git repack [<options>]"),18NULL19};2021static const char incremental_bitmap_conflict_error[] = N_(22"Incremental repacks are incompatible with bitmap indexes. Use\n"23"--no-write-bitmap-index or disable the pack.writebitmaps configuration."24);252627static int repack_config(const char *var, const char *value, void *cb)28{29if (!strcmp(var, "repack.usedeltabaseoffset")) {30delta_base_offset = git_config_bool(var, value);31return 0;32}33if (!strcmp(var, "repack.packkeptobjects")) {34pack_kept_objects = git_config_bool(var, value);35return 0;36}37if (!strcmp(var, "repack.writebitmaps") ||38!strcmp(var, "pack.writebitmaps")) {39write_bitmaps = git_config_bool(var, value);40return 0;41}42return git_default_config(var, value, cb);43}4445/*46* Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.47*/48static void remove_temporary_files(void)49{50struct strbuf buf = STRBUF_INIT;51size_t dirlen, prefixlen;52DIR *dir;53struct dirent *e;5455dir = opendir(packdir);56if (!dir)57return;5859/* Point at the slash at the end of ".../objects/pack/" */60dirlen = strlen(packdir) + 1;61strbuf_addstr(&buf, packtmp);62/* Hold the length of ".tmp-%d-pack-" */63prefixlen = buf.len - dirlen;6465while ((e = readdir(dir))) {66if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))67continue;68strbuf_setlen(&buf, dirlen);69strbuf_addstr(&buf, e->d_name);70unlink(buf.buf);71}72closedir(dir);73strbuf_release(&buf);74}7576static void remove_pack_on_signal(int signo)77{78remove_temporary_files();79sigchain_pop(signo);80raise(signo);81}8283/*84* Adds all packs hex strings to the fname list, which do not85* have a corresponding .keep file.86*/87static void get_non_kept_pack_filenames(struct string_list *fname_list)88{89DIR *dir;90struct dirent *e;91char *fname;9293if (!(dir = opendir(packdir)))94return;9596while ((e = readdir(dir)) != NULL) {97size_t len;98if (!strip_suffix(e->d_name, ".pack", &len))99continue;100101fname = xmemdupz(e->d_name, len);102103if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))104string_list_append_nodup(fname_list, fname);105else106free(fname);107}108closedir(dir);109}110111static void remove_redundant_pack(const char *dir_name, const char *base_name)112{113const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};114int i;115struct strbuf buf = STRBUF_INIT;116size_t plen;117118strbuf_addf(&buf, "%s/%s", dir_name, base_name);119plen = buf.len;120121for (i = 0; i < ARRAY_SIZE(exts); i++) {122strbuf_setlen(&buf, plen);123strbuf_addstr(&buf, exts[i]);124unlink(buf.buf);125}126strbuf_release(&buf);127}128129#define ALL_INTO_ONE 1130#define LOOSEN_UNREACHABLE 2131132int cmd_repack(int argc, const char **argv, const char *prefix)133{134struct {135const char *name;136unsigned optional:1;137} exts[] = {138{".pack"},139{".idx"},140{".bitmap", 1},141};142struct child_process cmd = CHILD_PROCESS_INIT;143struct string_list_item *item;144struct string_list names = STRING_LIST_INIT_DUP;145struct string_list rollback = STRING_LIST_INIT_NODUP;146struct string_list existing_packs = STRING_LIST_INIT_DUP;147struct strbuf line = STRBUF_INIT;148int ext, ret, failed;149FILE *out;150151/* variables to be filled by option parsing */152int pack_everything = 0;153int delete_redundant = 0;154const char *unpack_unreachable = NULL;155int keep_unreachable = 0;156const char *window = NULL, *window_memory = NULL;157const char *depth = NULL;158const char *max_pack_size = NULL;159int no_reuse_delta = 0, no_reuse_object = 0;160int no_update_server_info = 0;161int quiet = 0;162int local = 0;163164struct option builtin_repack_options[] = {165OPT_BIT('a', NULL, &pack_everything,166N_("pack everything in a single pack"), ALL_INTO_ONE),167OPT_BIT('A', NULL, &pack_everything,168N_("same as -a, and turn unreachable objects loose"),169LOOSEN_UNREACHABLE | ALL_INTO_ONE),170OPT_BOOL('d', NULL, &delete_redundant,171N_("remove redundant packs, and run git-prune-packed")),172OPT_BOOL('f', NULL, &no_reuse_delta,173N_("pass --no-reuse-delta to git-pack-objects")),174OPT_BOOL('F', NULL, &no_reuse_object,175N_("pass --no-reuse-object to git-pack-objects")),176OPT_BOOL('n', NULL, &no_update_server_info,177N_("do not run git-update-server-info")),178OPT__QUIET(&quiet, N_("be quiet")),179OPT_BOOL('l', "local", &local,180N_("pass --local to git-pack-objects")),181OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,182N_("write bitmap index")),183OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),184N_("with -A, do not loosen objects older than this")),185OPT_BOOL('k', "keep-unreachable", &keep_unreachable,186N_("with -a, repack unreachable objects")),187OPT_STRING(0, "window", &window, N_("n"),188N_("size of the window used for delta compression")),189OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),190N_("same as the above, but limit memory size instead of entries count")),191OPT_STRING(0, "depth", &depth, N_("n"),192N_("limits the maximum delta depth")),193OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),194N_("maximum size of each packfile")),195OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,196N_("repack objects in packs marked with .keep")),197OPT_END()198};199200git_config(repack_config, NULL);201202argc = parse_options(argc, argv, prefix, builtin_repack_options,203git_repack_usage, 0);204205if (delete_redundant && repository_format_precious_objects)206die(_("cannot delete packs in a precious-objects repo"));207208if (keep_unreachable &&209(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))210die(_("--keep-unreachable and -A are incompatible"));211212if (pack_kept_objects < 0)213pack_kept_objects = write_bitmaps;214215if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))216die(_(incremental_bitmap_conflict_error));217218packdir = mkpathdup("%s/pack", get_object_directory());219packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());220221sigchain_push_common(remove_pack_on_signal);222223argv_array_push(&cmd.args, "pack-objects");224argv_array_push(&cmd.args, "--keep-true-parents");225if (!pack_kept_objects)226argv_array_push(&cmd.args, "--honor-pack-keep");227argv_array_push(&cmd.args, "--non-empty");228argv_array_push(&cmd.args, "--all");229argv_array_push(&cmd.args, "--reflog");230argv_array_push(&cmd.args, "--indexed-objects");231if (window)232argv_array_pushf(&cmd.args, "--window=%s", window);233if (window_memory)234argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory);235if (depth)236argv_array_pushf(&cmd.args, "--depth=%s", depth);237if (max_pack_size)238argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size);239if (no_reuse_delta)240argv_array_pushf(&cmd.args, "--no-reuse-delta");241if (no_reuse_object)242argv_array_pushf(&cmd.args, "--no-reuse-object");243if (write_bitmaps)244argv_array_push(&cmd.args, "--write-bitmap-index");245246if (pack_everything & ALL_INTO_ONE) {247get_non_kept_pack_filenames(&existing_packs);248249if (existing_packs.nr && delete_redundant) {250if (unpack_unreachable) {251argv_array_pushf(&cmd.args,252"--unpack-unreachable=%s",253unpack_unreachable);254argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");255} else if (pack_everything & LOOSEN_UNREACHABLE) {256argv_array_push(&cmd.args,257"--unpack-unreachable");258} else if (keep_unreachable) {259argv_array_push(&cmd.args, "--keep-unreachable");260argv_array_push(&cmd.args, "--pack-loose-unreachable");261} else {262argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");263}264}265} else {266argv_array_push(&cmd.args, "--unpacked");267argv_array_push(&cmd.args, "--incremental");268}269270if (local)271argv_array_push(&cmd.args, "--local");272if (quiet)273argv_array_push(&cmd.args, "--quiet");274if (delta_base_offset)275argv_array_push(&cmd.args, "--delta-base-offset");276277argv_array_push(&cmd.args, packtmp);278279cmd.git_cmd = 1;280cmd.out = -1;281cmd.no_stdin = 1;282283ret = start_command(&cmd);284if (ret)285return ret;286287out = xfdopen(cmd.out, "r");288while (strbuf_getline_lf(&line, out) != EOF) {289if (line.len != 40)290die("repack: Expecting 40 character sha1 lines only from pack-objects.");291string_list_append(&names, line.buf);292}293fclose(out);294ret = finish_command(&cmd);295if (ret)296return ret;297298if (!names.nr && !quiet)299printf("Nothing new to pack.\n");300301/*302* Ok we have prepared all new packfiles.303* First see if there are packs of the same name and if so304* if we can move them out of the way (this can happen if we305* repacked immediately after packing fully.306*/307failed = 0;308for_each_string_list_item(item, &names) {309for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {310char *fname, *fname_old;311fname = mkpathdup("%s/pack-%s%s", packdir,312item->string, exts[ext].name);313if (!file_exists(fname)) {314free(fname);315continue;316}317318fname_old = mkpathdup("%s/old-%s%s", packdir,319item->string, exts[ext].name);320if (file_exists(fname_old))321if (unlink(fname_old))322failed = 1;323324if (!failed && rename(fname, fname_old)) {325free(fname);326free(fname_old);327failed = 1;328break;329} else {330string_list_append(&rollback, fname);331free(fname_old);332}333}334if (failed)335break;336}337if (failed) {338struct string_list rollback_failure = STRING_LIST_INIT_DUP;339for_each_string_list_item(item, &rollback) {340char *fname, *fname_old;341fname = mkpathdup("%s/%s", packdir, item->string);342fname_old = mkpathdup("%s/old-%s", packdir, item->string);343if (rename(fname_old, fname))344string_list_append(&rollback_failure, fname);345free(fname);346free(fname_old);347}348349if (rollback_failure.nr) {350int i;351fprintf(stderr,352"WARNING: Some packs in use have been renamed by\n"353"WARNING: prefixing old- to their name, in order to\n"354"WARNING: replace them with the new version of the\n"355"WARNING: file. But the operation failed, and the\n"356"WARNING: attempt to rename them back to their\n"357"WARNING: original names also failed.\n"358"WARNING: Please rename them in %s manually:\n", packdir);359for (i = 0; i < rollback_failure.nr; i++)360fprintf(stderr, "WARNING: old-%s -> %s\n",361rollback_failure.items[i].string,362rollback_failure.items[i].string);363}364exit(1);365}366367/* Now the ones with the same name are out of the way... */368for_each_string_list_item(item, &names) {369for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {370char *fname, *fname_old;371struct stat statbuffer;372int exists = 0;373fname = mkpathdup("%s/pack-%s%s",374packdir, item->string, exts[ext].name);375fname_old = mkpathdup("%s-%s%s",376packtmp, item->string, exts[ext].name);377if (!stat(fname_old, &statbuffer)) {378statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);379chmod(fname_old, statbuffer.st_mode);380exists = 1;381}382if (exists || !exts[ext].optional) {383if (rename(fname_old, fname))384die_errno(_("renaming '%s' failed"), fname_old);385}386free(fname);387free(fname_old);388}389}390391/* Remove the "old-" files */392for_each_string_list_item(item, &names) {393for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {394char *fname;395fname = mkpathdup("%s/old-%s%s",396packdir,397item->string,398exts[ext].name);399if (remove_path(fname))400warning(_("failed to remove '%s'"), fname);401free(fname);402}403}404405/* End of pack replacement. */406407if (delete_redundant) {408int opts = 0;409string_list_sort(&names);410for_each_string_list_item(item, &existing_packs) {411char *sha1;412size_t len = strlen(item->string);413if (len < 40)414continue;415sha1 = item->string + len - 40;416if (!string_list_has_string(&names, sha1))417remove_redundant_pack(packdir, item->string);418}419if (!quiet && isatty(2))420opts |= PRUNE_PACKED_VERBOSE;421prune_packed_objects(opts);422}423424if (!no_update_server_info)425update_server_info(0);426remove_temporary_files();427string_list_clear(&names, 0);428string_list_clear(&rollback, 0);429string_list_clear(&existing_packs, 0);430strbuf_release(&line);431432return 0;433}