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