builtin / prune.con commit revision: Turn off history simplification in --ancestry-path mode (cb7529e)
   1#include "cache.h"
   2#include "commit.h"
   3#include "diff.h"
   4#include "revision.h"
   5#include "builtin.h"
   6#include "reachable.h"
   7#include "parse-options.h"
   8#include "dir.h"
   9
  10static const char * const prune_usage[] = {
  11        "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
  12        NULL
  13};
  14static int show_only;
  15static int verbose;
  16static unsigned long expire;
  17
  18static int prune_tmp_object(const char *path, const char *filename)
  19{
  20        const char *fullpath = mkpath("%s/%s", path, filename);
  21        struct stat st;
  22        if (lstat(fullpath, &st))
  23                return error("Could not stat '%s'", fullpath);
  24        if (st.st_mtime > expire)
  25                return 0;
  26        printf("Removing stale temporary file %s\n", fullpath);
  27        if (!show_only)
  28                unlink_or_warn(fullpath);
  29        return 0;
  30}
  31
  32static int prune_object(char *path, const char *filename, const unsigned char *sha1)
  33{
  34        const char *fullpath = mkpath("%s/%s", path, filename);
  35        struct stat st;
  36        if (lstat(fullpath, &st))
  37                return error("Could not stat '%s'", fullpath);
  38        if (st.st_mtime > expire)
  39                return 0;
  40        if (show_only || verbose) {
  41                enum object_type type = sha1_object_info(sha1, NULL);
  42                printf("%s %s\n", sha1_to_hex(sha1),
  43                       (type > 0) ? typename(type) : "unknown");
  44        }
  45        if (!show_only)
  46                unlink_or_warn(fullpath);
  47        return 0;
  48}
  49
  50static int prune_dir(int i, char *path)
  51{
  52        DIR *dir = opendir(path);
  53        struct dirent *de;
  54
  55        if (!dir)
  56                return 0;
  57
  58        while ((de = readdir(dir)) != NULL) {
  59                char name[100];
  60                unsigned char sha1[20];
  61
  62                if (is_dot_or_dotdot(de->d_name))
  63                        continue;
  64                if (strlen(de->d_name) == 38) {
  65                        sprintf(name, "%02x", i);
  66                        memcpy(name+2, de->d_name, 39);
  67                        if (get_sha1_hex(name, sha1) < 0)
  68                                break;
  69
  70                        /*
  71                         * Do we know about this object?
  72                         * It must have been reachable
  73                         */
  74                        if (lookup_object(sha1))
  75                                continue;
  76
  77                        prune_object(path, de->d_name, sha1);
  78                        continue;
  79                }
  80                if (!prefixcmp(de->d_name, "tmp_obj_")) {
  81                        prune_tmp_object(path, de->d_name);
  82                        continue;
  83                }
  84                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
  85        }
  86        if (!show_only)
  87                rmdir(path);
  88        closedir(dir);
  89        return 0;
  90}
  91
  92static void prune_object_dir(const char *path)
  93{
  94        int i;
  95        for (i = 0; i < 256; i++) {
  96                static char dir[4096];
  97                sprintf(dir, "%s/%02x", path, i);
  98                prune_dir(i, dir);
  99        }
 100}
 101
 102/*
 103 * Write errors (particularly out of space) can result in
 104 * failed temporary packs (and more rarely indexes and other
 105 * files beginning with "tmp_") accumulating in the object
 106 * and the pack directories.
 107 */
 108static void remove_temporary_files(const char *path)
 109{
 110        DIR *dir;
 111        struct dirent *de;
 112
 113        dir = opendir(path);
 114        if (!dir) {
 115                fprintf(stderr, "Unable to open directory %s\n", path);
 116                return;
 117        }
 118        while ((de = readdir(dir)) != NULL)
 119                if (!prefixcmp(de->d_name, "tmp_"))
 120                        prune_tmp_object(path, de->d_name);
 121        closedir(dir);
 122}
 123
 124int cmd_prune(int argc, const char **argv, const char *prefix)
 125{
 126        struct rev_info revs;
 127        const struct option options[] = {
 128                OPT_BOOLEAN('n', NULL, &show_only,
 129                            "do not remove, show only"),
 130                OPT_BOOLEAN('v', NULL, &verbose,
 131                        "report pruned objects"),
 132                OPT_DATE(0, "expire", &expire,
 133                         "expire objects older than <time>"),
 134                OPT_END()
 135        };
 136        char *s;
 137
 138        expire = ULONG_MAX;
 139        save_commit_buffer = 0;
 140        read_replace_refs = 0;
 141        init_revisions(&revs, prefix);
 142
 143        argc = parse_options(argc, argv, prefix, options, prune_usage, 0);
 144        while (argc--) {
 145                unsigned char sha1[20];
 146                const char *name = *argv++;
 147
 148                if (!get_sha1(name, sha1)) {
 149                        struct object *object = parse_object(sha1);
 150                        if (!object)
 151                                die("bad object: %s", name);
 152                        add_pending_object(&revs, object, "");
 153                }
 154                else
 155                        die("unrecognized argument: %s", name);
 156        }
 157        mark_reachable_objects(&revs, 1);
 158        prune_object_dir(get_object_directory());
 159
 160        prune_packed_objects(show_only);
 161        remove_temporary_files(get_object_directory());
 162        s = xstrdup(mkpath("%s/pack", get_object_directory()));
 163        remove_temporary_files(s);
 164        free(s);
 165        return 0;
 166}