builtin-prune.con commit Documentation: undocument gc'd function graph_release() (aa8dc96)
   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        if (expire) {
  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        }
  28        printf("Removing stale temporary file %s\n", fullpath);
  29        if (!show_only)
  30                unlink_or_warn(fullpath);
  31        return 0;
  32}
  33
  34static int prune_object(char *path, const char *filename, const unsigned char *sha1)
  35{
  36        const char *fullpath = mkpath("%s/%s", path, filename);
  37        if (expire) {
  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        }
  44        if (show_only || verbose) {
  45                enum object_type type = sha1_object_info(sha1, NULL);
  46                printf("%s %s\n", sha1_to_hex(sha1),
  47                       (type > 0) ? typename(type) : "unknown");
  48        }
  49        if (!show_only)
  50                unlink_or_warn(fullpath);
  51        return 0;
  52}
  53
  54static int prune_dir(int i, char *path)
  55{
  56        DIR *dir = opendir(path);
  57        struct dirent *de;
  58
  59        if (!dir)
  60                return 0;
  61
  62        while ((de = readdir(dir)) != NULL) {
  63                char name[100];
  64                unsigned char sha1[20];
  65
  66                if (is_dot_or_dotdot(de->d_name))
  67                        continue;
  68                if (strlen(de->d_name) == 38) {
  69                        sprintf(name, "%02x", i);
  70                        memcpy(name+2, de->d_name, 39);
  71                        if (get_sha1_hex(name, sha1) < 0)
  72                                break;
  73
  74                        /*
  75                         * Do we know about this object?
  76                         * It must have been reachable
  77                         */
  78                        if (lookup_object(sha1))
  79                                continue;
  80
  81                        prune_object(path, de->d_name, sha1);
  82                        continue;
  83                }
  84                if (!prefixcmp(de->d_name, "tmp_obj_")) {
  85                        prune_tmp_object(path, de->d_name);
  86                        continue;
  87                }
  88                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
  89        }
  90        if (!show_only)
  91                rmdir(path);
  92        closedir(dir);
  93        return 0;
  94}
  95
  96static void prune_object_dir(const char *path)
  97{
  98        int i;
  99        for (i = 0; i < 256; i++) {
 100                static char dir[4096];
 101                sprintf(dir, "%s/%02x", path, i);
 102                prune_dir(i, dir);
 103        }
 104}
 105
 106/*
 107 * Write errors (particularly out of space) can result in
 108 * failed temporary packs (and more rarely indexes and other
 109 * files begining with "tmp_") accumulating in the object
 110 * and the pack directories.
 111 */
 112static void remove_temporary_files(const char *path)
 113{
 114        DIR *dir;
 115        struct dirent *de;
 116
 117        dir = opendir(path);
 118        if (!dir) {
 119                fprintf(stderr, "Unable to open directory %s\n", path);
 120                return;
 121        }
 122        while ((de = readdir(dir)) != NULL)
 123                if (!prefixcmp(de->d_name, "tmp_"))
 124                        prune_tmp_object(path, de->d_name);
 125        closedir(dir);
 126}
 127
 128int cmd_prune(int argc, const char **argv, const char *prefix)
 129{
 130        struct rev_info revs;
 131        const struct option options[] = {
 132                OPT_BOOLEAN('n', NULL, &show_only,
 133                            "do not remove, show only"),
 134                OPT_BOOLEAN('v', NULL, &verbose,
 135                        "report pruned objects"),
 136                OPT_DATE(0, "expire", &expire,
 137                         "expire objects older than <time>"),
 138                OPT_END()
 139        };
 140        char *s;
 141
 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(sha1);
 153                        if (!object)
 154                                die("bad object: %s", name);
 155                        add_pending_object(&revs, object, "");
 156                }
 157                else
 158                        die("unrecognized argument: %s", name);
 159        }
 160        mark_reachable_objects(&revs, 1);
 161        prune_object_dir(get_object_directory());
 162
 163        prune_packed_objects(show_only);
 164        remove_temporary_files(get_object_directory());
 165        s = xstrdup(mkpath("%s/pack", get_object_directory()));
 166        remove_temporary_files(s);
 167        free(s);
 168        return 0;
 169}