286a94c3fc46e2af1f0a31fbaa4aaacf7b876783
   1#include "cache.h"
   2#include "refs.h"
   3#include "tag.h"
   4#include "commit.h"
   5#include "tree.h"
   6#include "blob.h"
   7#include "tree-walk.h"
   8#include "diff.h"
   9#include "revision.h"
  10#include "builtin.h"
  11#include "cache-tree.h"
  12
  13static const char prune_usage[] = "git-prune [-n]";
  14static int show_only;
  15static struct rev_info revs;
  16
  17static int prune_object(char *path, const char *filename, const unsigned char *sha1)
  18{
  19        char buf[20];
  20        const char *type;
  21
  22        if (show_only) {
  23                type = buf;
  24                if (sha1_object_info(sha1, type, NULL))
  25                        type = "unknown";
  26                printf("%s %s\n", sha1_to_hex(sha1), type );
  27                return 0;
  28        }
  29        unlink(mkpath("%s/%s", path, filename));
  30        rmdir(path);
  31        return 0;
  32}
  33
  34static int prune_dir(int i, char *path)
  35{
  36        DIR *dir = opendir(path);
  37        struct dirent *de;
  38
  39        if (!dir)
  40                return 0;
  41
  42        while ((de = readdir(dir)) != NULL) {
  43                char name[100];
  44                unsigned char sha1[20];
  45                int len = strlen(de->d_name);
  46
  47                switch (len) {
  48                case 2:
  49                        if (de->d_name[1] != '.')
  50                                break;
  51                case 1:
  52                        if (de->d_name[0] != '.')
  53                                break;
  54                        continue;
  55                case 38:
  56                        sprintf(name, "%02x", i);
  57                        memcpy(name+2, de->d_name, len+1);
  58                        if (get_sha1_hex(name, sha1) < 0)
  59                                break;
  60
  61                        /*
  62                         * Do we know about this object?
  63                         * It must have been reachable
  64                         */
  65                        if (lookup_object(sha1))
  66                                continue;
  67
  68                        prune_object(path, de->d_name, sha1);
  69                        continue;
  70                }
  71                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
  72        }
  73        closedir(dir);
  74        return 0;
  75}
  76
  77static void prune_object_dir(const char *path)
  78{
  79        int i;
  80        for (i = 0; i < 256; i++) {
  81                static char dir[4096];
  82                sprintf(dir, "%s/%02x", path, i);
  83                prune_dir(i, dir);
  84        }
  85}
  86
  87static void process_blob(struct blob *blob,
  88                         struct object_array *p,
  89                         struct name_path *path,
  90                         const char *name)
  91{
  92        struct object *obj = &blob->object;
  93
  94        if (obj->flags & SEEN)
  95                return;
  96        obj->flags |= SEEN;
  97        /* Nothing to do, really .. The blob lookup was the important part */
  98}
  99
 100static void process_tree(struct tree *tree,
 101                         struct object_array *p,
 102                         struct name_path *path,
 103                         const char *name)
 104{
 105        struct object *obj = &tree->object;
 106        struct tree_desc desc;
 107        struct name_entry entry;
 108        struct name_path me;
 109
 110        if (obj->flags & SEEN)
 111                return;
 112        obj->flags |= SEEN;
 113        if (parse_tree(tree) < 0)
 114                die("bad tree object %s", sha1_to_hex(obj->sha1));
 115        name = xstrdup(name);
 116        add_object(obj, p, path, name);
 117        me.up = path;
 118        me.elem = name;
 119        me.elem_len = strlen(name);
 120
 121        desc.buf = tree->buffer;
 122        desc.size = tree->size;
 123
 124        while (tree_entry(&desc, &entry)) {
 125                if (S_ISDIR(entry.mode))
 126                        process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
 127                else
 128                        process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
 129        }
 130        free(tree->buffer);
 131        tree->buffer = NULL;
 132}
 133
 134static void process_tag(struct tag *tag, struct object_array *p, const char *name)
 135{
 136        struct object *obj = &tag->object;
 137        struct name_path me;
 138
 139        if (obj->flags & SEEN)
 140                return;
 141        obj->flags |= SEEN;
 142
 143        me.up = NULL;
 144        me.elem = "tag:/";
 145        me.elem_len = 5;
 146
 147        if (parse_tag(tag) < 0)
 148                die("bad tag object %s", sha1_to_hex(obj->sha1));
 149        add_object(tag->tagged, p, NULL, name);
 150}
 151
 152static void walk_commit_list(struct rev_info *revs)
 153{
 154        int i;
 155        struct commit *commit;
 156        struct object_array objects = { 0, 0, NULL };
 157
 158        /* Walk all commits, process their trees */
 159        while ((commit = get_revision(revs)) != NULL)
 160                process_tree(commit->tree, &objects, NULL, "");
 161
 162        /* Then walk all the pending objects, recursively processing them too */
 163        for (i = 0; i < revs->pending.nr; i++) {
 164                struct object_array_entry *pending = revs->pending.objects + i;
 165                struct object *obj = pending->item;
 166                const char *name = pending->name;
 167                if (obj->type == OBJ_TAG) {
 168                        process_tag((struct tag *) obj, &objects, name);
 169                        continue;
 170                }
 171                if (obj->type == OBJ_TREE) {
 172                        process_tree((struct tree *)obj, &objects, NULL, name);
 173                        continue;
 174                }
 175                if (obj->type == OBJ_BLOB) {
 176                        process_blob((struct blob *)obj, &objects, NULL, name);
 177                        continue;
 178                }
 179                die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
 180        }
 181}
 182
 183static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 184{
 185        struct object *object = parse_object(sha1);
 186        if (!object)
 187                die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
 188        add_pending_object(&revs, object, "");
 189        return 0;
 190}
 191
 192static void add_one_tree(const unsigned char *sha1)
 193{
 194        struct tree *tree = lookup_tree(sha1);
 195        add_pending_object(&revs, &tree->object, "");
 196}
 197
 198static void add_cache_tree(struct cache_tree *it)
 199{
 200        int i;
 201
 202        if (it->entry_count >= 0)
 203                add_one_tree(it->sha1);
 204        for (i = 0; i < it->subtree_nr; i++)
 205                add_cache_tree(it->down[i]->cache_tree);
 206}
 207
 208static void add_cache_refs(void)
 209{
 210        int i;
 211
 212        read_cache();
 213        for (i = 0; i < active_nr; i++) {
 214                lookup_blob(active_cache[i]->sha1);
 215                /*
 216                 * We could add the blobs to the pending list, but quite
 217                 * frankly, we don't care. Once we've looked them up, and
 218                 * added them as objects, we've really done everything
 219                 * there is to do for a blob
 220                 */
 221        }
 222        if (active_cache_tree)
 223                add_cache_tree(active_cache_tree);
 224}
 225
 226int cmd_prune(int argc, const char **argv, const char *prefix)
 227{
 228        int i;
 229
 230        for (i = 1; i < argc; i++) {
 231                const char *arg = argv[i];
 232                if (!strcmp(arg, "-n")) {
 233                        show_only = 1;
 234                        continue;
 235                }
 236                usage(prune_usage);
 237        }
 238
 239        /*
 240         * Set up revision parsing, and mark us as being interested
 241         * in all object types, not just commits.
 242         */
 243        init_revisions(&revs, prefix);
 244        revs.tag_objects = 1;
 245        revs.blob_objects = 1;
 246        revs.tree_objects = 1;
 247
 248        /* Add all external refs */
 249        for_each_ref(add_one_ref, NULL);
 250
 251        /* Add all refs from the index file */
 252        add_cache_refs();
 253
 254        /*
 255         * Set up the revision walk - this will move all commits
 256         * from the pending list to the commit walking list.
 257         */
 258        prepare_revision_walk(&revs);
 259
 260        walk_commit_list(&revs);
 261
 262        prune_object_dir(get_object_directory());
 263
 264        sync();
 265        prune_packed_objects(show_only);
 266        return 0;
 267}