builtin-pack-refs.con commit Allow the user to control the verbosity of merge-recursive. (8c3275a)
   1#include "cache.h"
   2#include "refs.h"
   3#include "object.h"
   4#include "tag.h"
   5
   6static const char builtin_pack_refs_usage[] =
   7"git-pack-refs [--all] [--prune | --no-prune]";
   8
   9struct ref_to_prune {
  10        struct ref_to_prune *next;
  11        unsigned char sha1[20];
  12        char name[FLEX_ARRAY];
  13};
  14
  15struct pack_refs_cb_data {
  16        int prune;
  17        int all;
  18        struct ref_to_prune *ref_to_prune;
  19        FILE *refs_file;
  20};
  21
  22static int do_not_prune(int flags)
  23{
  24        /* If it is already packed or if it is a symref,
  25         * do not prune it.
  26         */
  27        return (flags & (REF_ISSYMREF|REF_ISPACKED));
  28}
  29
  30static int handle_one_ref(const char *path, const unsigned char *sha1,
  31                          int flags, void *cb_data)
  32{
  33        struct pack_refs_cb_data *cb = cb_data;
  34        int is_tag_ref;
  35
  36        /* Do not pack the symbolic refs */
  37        if ((flags & REF_ISSYMREF))
  38                return 0;
  39        is_tag_ref = !strncmp(path, "refs/tags/", 10);
  40        if (!cb->all && !is_tag_ref)
  41                return 0;
  42
  43        fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
  44        if (is_tag_ref) {
  45                struct object *o = parse_object(sha1);
  46                if (o->type == OBJ_TAG) {
  47                        o = deref_tag(o, path, 0);
  48                        if (o)
  49                                fprintf(cb->refs_file, "^%s\n",
  50                                        sha1_to_hex(o->sha1));
  51                }
  52        }
  53
  54        if (cb->prune && !do_not_prune(flags)) {
  55                int namelen = strlen(path) + 1;
  56                struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
  57                hashcpy(n->sha1, sha1);
  58                strcpy(n->name, path);
  59                n->next = cb->ref_to_prune;
  60                cb->ref_to_prune = n;
  61        }
  62        return 0;
  63}
  64
  65/* make sure nobody touched the ref, and unlink */
  66static void prune_ref(struct ref_to_prune *r)
  67{
  68        struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
  69
  70        if (lock) {
  71                unlink(git_path("%s", r->name));
  72                unlock_ref(lock);
  73        }
  74}
  75
  76static void prune_refs(struct ref_to_prune *r)
  77{
  78        while (r) {
  79                prune_ref(r);
  80                r = r->next;
  81        }
  82}
  83
  84static struct lock_file packed;
  85
  86int cmd_pack_refs(int argc, const char **argv, const char *prefix)
  87{
  88        int fd, i;
  89        struct pack_refs_cb_data cbdata;
  90
  91        memset(&cbdata, 0, sizeof(cbdata));
  92
  93        cbdata.prune = 1;
  94        for (i = 1; i < argc; i++) {
  95                const char *arg = argv[i];
  96                if (!strcmp(arg, "--prune")) {
  97                        cbdata.prune = 1; /* now the default */
  98                        continue;
  99                }
 100                if (!strcmp(arg, "--no-prune")) {
 101                        cbdata.prune = 0;
 102                        continue;
 103                }
 104                if (!strcmp(arg, "--all")) {
 105                        cbdata.all = 1;
 106                        continue;
 107                }
 108                /* perhaps other parameters later... */
 109                break;
 110        }
 111        if (i != argc)
 112                usage(builtin_pack_refs_usage);
 113
 114        fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
 115        cbdata.refs_file = fdopen(fd, "w");
 116        if (!cbdata.refs_file)
 117                die("unable to create ref-pack file structure (%s)",
 118                    strerror(errno));
 119
 120        /* perhaps other traits later as well */
 121        fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
 122
 123        for_each_ref(handle_one_ref, &cbdata);
 124        fflush(cbdata.refs_file);
 125        fsync(fd);
 126        fclose(cbdata.refs_file);
 127        if (commit_lock_file(&packed) < 0)
 128                die("unable to overwrite old ref-pack file (%s)", strerror(errno));
 129        if (cbdata.prune)
 130                prune_refs(cbdata.ref_to_prune);
 131        return 0;
 132}