t / helper / test-ref-store.con commit Merge branch 'jt/send-email-validate-hook' (07d4c76)
   1#include "cache.h"
   2#include "refs.h"
   3#include "worktree.h"
   4
   5static const char *notnull(const char *arg, const char *name)
   6{
   7        if (!arg)
   8                die("%s required", name);
   9        return arg;
  10}
  11
  12static unsigned int arg_flags(const char *arg, const char *name)
  13{
  14        return atoi(notnull(arg, name));
  15}
  16
  17static const char **get_store(const char **argv, struct ref_store **refs)
  18{
  19        const char *gitdir;
  20
  21        if (!argv[0]) {
  22                die("ref store required");
  23        } else if (!strcmp(argv[0], "main")) {
  24                *refs = get_main_ref_store();
  25        } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
  26                struct strbuf sb = STRBUF_INIT;
  27                int ret;
  28
  29                ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
  30                if (ret)
  31                        die("strbuf_git_path_submodule failed: %d", ret);
  32                add_to_alternates_memory(sb.buf);
  33                strbuf_release(&sb);
  34
  35                *refs = get_submodule_ref_store(gitdir);
  36        } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
  37                struct worktree **p, **worktrees = get_worktrees(0);
  38
  39                for (p = worktrees; *p; p++) {
  40                        struct worktree *wt = *p;
  41
  42                        if (!wt->id) {
  43                                /* special case for main worktree */
  44                                if (!strcmp(gitdir, "main"))
  45                                        break;
  46                        } else if (!strcmp(gitdir, wt->id))
  47                                break;
  48                }
  49                if (!*p)
  50                        die("no such worktree: %s", gitdir);
  51
  52                *refs = get_worktree_ref_store(*p);
  53        } else
  54                die("unknown backend %s", argv[0]);
  55
  56        if (!*refs)
  57                die("no ref store");
  58
  59        /* consume store-specific optional arguments if needed */
  60
  61        return argv + 1;
  62}
  63
  64
  65static int cmd_pack_refs(struct ref_store *refs, const char **argv)
  66{
  67        unsigned int flags = arg_flags(*argv++, "flags");
  68
  69        return refs_pack_refs(refs, flags);
  70}
  71
  72static int cmd_peel_ref(struct ref_store *refs, const char **argv)
  73{
  74        const char *refname = notnull(*argv++, "refname");
  75        unsigned char sha1[20];
  76        int ret;
  77
  78        ret = refs_peel_ref(refs, refname, sha1);
  79        if (!ret)
  80                puts(sha1_to_hex(sha1));
  81        return ret;
  82}
  83
  84static int cmd_create_symref(struct ref_store *refs, const char **argv)
  85{
  86        const char *refname = notnull(*argv++, "refname");
  87        const char *target = notnull(*argv++, "target");
  88        const char *logmsg = *argv++;
  89
  90        return refs_create_symref(refs, refname, target, logmsg);
  91}
  92
  93static int cmd_delete_refs(struct ref_store *refs, const char **argv)
  94{
  95        unsigned int flags = arg_flags(*argv++, "flags");
  96        struct string_list refnames = STRING_LIST_INIT_NODUP;
  97
  98        while (*argv)
  99                string_list_append(&refnames, *argv++);
 100
 101        return refs_delete_refs(refs, &refnames, flags);
 102}
 103
 104static int cmd_rename_ref(struct ref_store *refs, const char **argv)
 105{
 106        const char *oldref = notnull(*argv++, "oldref");
 107        const char *newref = notnull(*argv++, "newref");
 108        const char *logmsg = *argv++;
 109
 110        return refs_rename_ref(refs, oldref, newref, logmsg);
 111}
 112
 113static int each_ref(const char *refname, const struct object_id *oid,
 114                    int flags, void *cb_data)
 115{
 116        printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
 117        return 0;
 118}
 119
 120static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
 121{
 122        const char *prefix = notnull(*argv++, "prefix");
 123
 124        return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
 125}
 126
 127static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
 128{
 129        unsigned char sha1[20];
 130        const char *refname = notnull(*argv++, "refname");
 131        int resolve_flags = arg_flags(*argv++, "resolve-flags");
 132        int flags;
 133        const char *ref;
 134
 135        ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
 136                                      sha1, &flags);
 137        printf("%s %s 0x%x\n", sha1_to_hex(sha1), ref, flags);
 138        return ref ? 0 : 1;
 139}
 140
 141static int cmd_verify_ref(struct ref_store *refs, const char **argv)
 142{
 143        const char *refname = notnull(*argv++, "refname");
 144        struct strbuf err = STRBUF_INIT;
 145        int ret;
 146
 147        ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
 148        if (err.len)
 149                puts(err.buf);
 150        return ret;
 151}
 152
 153static int cmd_for_each_reflog(struct ref_store *refs, const char **argv)
 154{
 155        return refs_for_each_reflog(refs, each_ref, NULL);
 156}
 157
 158static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
 159                       const char *committer, timestamp_t timestamp,
 160                       int tz, const char *msg, void *cb_data)
 161{
 162        printf("%s %s %s %"PRItime" %d %s\n",
 163               oid_to_hex(old_oid), oid_to_hex(new_oid),
 164               committer, timestamp, tz, msg);
 165        return 0;
 166}
 167
 168static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
 169{
 170        const char *refname = notnull(*argv++, "refname");
 171
 172        return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
 173}
 174
 175static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
 176{
 177        const char *refname = notnull(*argv++, "refname");
 178
 179        return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
 180}
 181
 182static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
 183{
 184        const char *refname = notnull(*argv++, "refname");
 185
 186        return !refs_reflog_exists(refs, refname);
 187}
 188
 189static int cmd_create_reflog(struct ref_store *refs, const char **argv)
 190{
 191        const char *refname = notnull(*argv++, "refname");
 192        int force_create = arg_flags(*argv++, "force-create");
 193        struct strbuf err = STRBUF_INIT;
 194        int ret;
 195
 196        ret = refs_create_reflog(refs, refname, force_create, &err);
 197        if (err.len)
 198                puts(err.buf);
 199        return ret;
 200}
 201
 202static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
 203{
 204        const char *refname = notnull(*argv++, "refname");
 205
 206        return refs_delete_reflog(refs, refname);
 207}
 208
 209static int cmd_reflog_expire(struct ref_store *refs, const char **argv)
 210{
 211        die("not supported yet");
 212}
 213
 214static int cmd_delete_ref(struct ref_store *refs, const char **argv)
 215{
 216        const char *msg = notnull(*argv++, "msg");
 217        const char *refname = notnull(*argv++, "refname");
 218        const char *sha1_buf = notnull(*argv++, "old-sha1");
 219        unsigned int flags = arg_flags(*argv++, "flags");
 220        unsigned char old_sha1[20];
 221
 222        if (get_sha1_hex(sha1_buf, old_sha1))
 223                die("not sha-1");
 224
 225        return refs_delete_ref(refs, msg, refname, old_sha1, flags);
 226}
 227
 228static int cmd_update_ref(struct ref_store *refs, const char **argv)
 229{
 230        const char *msg = notnull(*argv++, "msg");
 231        const char *refname = notnull(*argv++, "refname");
 232        const char *new_sha1_buf = notnull(*argv++, "old-sha1");
 233        const char *old_sha1_buf = notnull(*argv++, "old-sha1");
 234        unsigned int flags = arg_flags(*argv++, "flags");
 235        unsigned char old_sha1[20];
 236        unsigned char new_sha1[20];
 237
 238        if (get_sha1_hex(old_sha1_buf, old_sha1) ||
 239            get_sha1_hex(new_sha1_buf, new_sha1))
 240                die("not sha-1");
 241
 242        return refs_update_ref(refs, msg, refname,
 243                               new_sha1, old_sha1,
 244                               flags, UPDATE_REFS_DIE_ON_ERR);
 245}
 246
 247struct command {
 248        const char *name;
 249        int (*func)(struct ref_store *refs, const char **argv);
 250};
 251
 252static struct command commands[] = {
 253        { "pack-refs", cmd_pack_refs },
 254        { "peel-ref", cmd_peel_ref },
 255        { "create-symref", cmd_create_symref },
 256        { "delete-refs", cmd_delete_refs },
 257        { "rename-ref", cmd_rename_ref },
 258        { "for-each-ref", cmd_for_each_ref },
 259        { "resolve-ref", cmd_resolve_ref },
 260        { "verify-ref", cmd_verify_ref },
 261        { "for-each-reflog", cmd_for_each_reflog },
 262        { "for-each-reflog-ent", cmd_for_each_reflog_ent },
 263        { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
 264        { "reflog-exists", cmd_reflog_exists },
 265        { "create-reflog", cmd_create_reflog },
 266        { "delete-reflog", cmd_delete_reflog },
 267        { "reflog-expire", cmd_reflog_expire },
 268        /*
 269         * backend transaction functions can't be tested separately
 270         */
 271        { "delete-ref", cmd_delete_ref },
 272        { "update-ref", cmd_update_ref },
 273        { NULL, NULL }
 274};
 275
 276int cmd_main(int argc, const char **argv)
 277{
 278        struct ref_store *refs;
 279        const char *func;
 280        struct command *cmd;
 281
 282        setup_git_directory();
 283
 284        argv = get_store(argv + 1, &refs);
 285
 286        func = *argv++;
 287        if (!func)
 288                die("ref function required");
 289        for (cmd = commands; cmd->name; cmd++) {
 290                if (!strcmp(func, cmd->name))
 291                        return cmd->func(refs, argv);
 292        }
 293        die("unknown function %s", func);
 294        return 0;
 295}