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