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