builtin-gc.con commit Merge branch 'master' of git://git.kernel.org/pub/scm/gitk/gitk (f8db788)
   1/*
   2 * git gc builtin command
   3 *
   4 * Cleanup unreachable files and optimize the repository.
   5 *
   6 * Copyright (c) 2007 James Bowes
   7 *
   8 * Based on git-gc.sh, which is
   9 *
  10 * Copyright (c) 2006 Shawn O. Pearce
  11 */
  12
  13#include "cache.h"
  14#include "run-command.h"
  15
  16#define FAILED_RUN "failed to run %s"
  17
  18static const char builtin_gc_usage[] = "git-gc [--prune] [--aggressive]";
  19
  20static int pack_refs = 1;
  21static int aggressive_window = -1;
  22
  23#define MAX_ADD 10
  24static const char *argv_pack_refs[] = {"pack-refs", "--all", "--prune", NULL};
  25static const char *argv_reflog[] = {"reflog", "expire", "--all", NULL};
  26static const char *argv_repack[MAX_ADD] = {"repack", "-a", "-d", "-l", NULL};
  27static const char *argv_prune[] = {"prune", NULL};
  28static const char *argv_rerere[] = {"rerere", "gc", NULL};
  29
  30static int gc_config(const char *var, const char *value)
  31{
  32        if (!strcmp(var, "gc.packrefs")) {
  33                if (!strcmp(value, "notbare"))
  34                        pack_refs = -1;
  35                else
  36                        pack_refs = git_config_bool(var, value);
  37                return 0;
  38        }
  39        if (!strcmp(var, "gc.aggressivewindow")) {
  40                aggressive_window = git_config_int(var, value);
  41                return 0;
  42        }
  43        return git_default_config(var, value);
  44}
  45
  46static void append_option(const char **cmd, const char *opt, int max_length)
  47{
  48        int i;
  49
  50        for (i = 0; cmd[i]; i++)
  51                ;
  52
  53        if (i + 2 >= max_length)
  54                die("Too many options specified");
  55        cmd[i++] = opt;
  56        cmd[i] = NULL;
  57}
  58
  59int cmd_gc(int argc, const char **argv, const char *prefix)
  60{
  61        int i;
  62        int prune = 0;
  63        char buf[80];
  64
  65        git_config(gc_config);
  66
  67        if (pack_refs < 0)
  68                pack_refs = !is_bare_repository();
  69
  70        for (i = 1; i < argc; i++) {
  71                const char *arg = argv[i];
  72                if (!strcmp(arg, "--prune")) {
  73                        prune = 1;
  74                        continue;
  75                }
  76                if (!strcmp(arg, "--aggressive")) {
  77                        append_option(argv_repack, "-f", MAX_ADD);
  78                        if (aggressive_window > 0) {
  79                                sprintf(buf, "--window=%d", aggressive_window);
  80                                append_option(argv_repack, buf, MAX_ADD);
  81                        }
  82                        continue;
  83                }
  84                /* perhaps other parameters later... */
  85                break;
  86        }
  87        if (i != argc)
  88                usage(builtin_gc_usage);
  89
  90        if (pack_refs && run_command_v_opt(argv_pack_refs, RUN_GIT_CMD))
  91                return error(FAILED_RUN, argv_pack_refs[0]);
  92
  93        if (run_command_v_opt(argv_reflog, RUN_GIT_CMD))
  94                return error(FAILED_RUN, argv_reflog[0]);
  95
  96        if (run_command_v_opt(argv_repack, RUN_GIT_CMD))
  97                return error(FAILED_RUN, argv_repack[0]);
  98
  99        if (prune && run_command_v_opt(argv_prune, RUN_GIT_CMD))
 100                return error(FAILED_RUN, argv_prune[0]);
 101
 102        if (run_command_v_opt(argv_rerere, RUN_GIT_CMD))
 103                return error(FAILED_RUN, argv_rerere[0]);
 104
 105        return 0;
 106}