ls-refs.con commit Merge branch 'cb/http-push-null-in-message-fix' (8cbad49)
   1#include "cache.h"
   2#include "repository.h"
   3#include "refs.h"
   4#include "remote.h"
   5#include "argv-array.h"
   6#include "ls-refs.h"
   7#include "pkt-line.h"
   8#include "config.h"
   9
  10/*
  11 * Check if one of the prefixes is a prefix of the ref.
  12 * If no prefixes were provided, all refs match.
  13 */
  14static int ref_match(const struct argv_array *prefixes, const char *refname)
  15{
  16        int i;
  17
  18        if (!prefixes->argc)
  19                return 1; /* no restriction */
  20
  21        for (i = 0; i < prefixes->argc; i++) {
  22                const char *prefix = prefixes->argv[i];
  23
  24                if (starts_with(refname, prefix))
  25                        return 1;
  26        }
  27
  28        return 0;
  29}
  30
  31struct ls_refs_data {
  32        unsigned peel;
  33        unsigned symrefs;
  34        struct argv_array prefixes;
  35};
  36
  37static int send_ref(const char *refname, const struct object_id *oid,
  38                    int flag, void *cb_data)
  39{
  40        struct ls_refs_data *data = cb_data;
  41        const char *refname_nons = strip_namespace(refname);
  42        struct strbuf refline = STRBUF_INIT;
  43
  44        if (ref_is_hidden(refname_nons, refname))
  45                return 0;
  46
  47        if (!ref_match(&data->prefixes, refname_nons))
  48                return 0;
  49
  50        strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
  51        if (data->symrefs && flag & REF_ISSYMREF) {
  52                struct object_id unused;
  53                const char *symref_target = resolve_ref_unsafe(refname, 0,
  54                                                               &unused,
  55                                                               &flag);
  56
  57                if (!symref_target)
  58                        die("'%s' is a symref but it is not?", refname);
  59
  60                strbuf_addf(&refline, " symref-target:%s", symref_target);
  61        }
  62
  63        if (data->peel) {
  64                struct object_id peeled;
  65                if (!peel_ref(refname, &peeled))
  66                        strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled));
  67        }
  68
  69        strbuf_addch(&refline, '\n');
  70        packet_write(1, refline.buf, refline.len);
  71
  72        strbuf_release(&refline);
  73        return 0;
  74}
  75
  76static int ls_refs_config(const char *var, const char *value, void *data)
  77{
  78        /*
  79         * We only serve fetches over v2 for now, so respect only "uploadpack"
  80         * config. This may need to eventually be expanded to "receive", but we
  81         * don't yet know how that information will be passed to ls-refs.
  82         */
  83        return parse_hide_refs_config(var, value, "uploadpack");
  84}
  85
  86int ls_refs(struct repository *r, struct argv_array *keys,
  87            struct packet_reader *request)
  88{
  89        struct ls_refs_data data;
  90
  91        memset(&data, 0, sizeof(data));
  92
  93        git_config(ls_refs_config, NULL);
  94
  95        while (packet_reader_read(request) != PACKET_READ_FLUSH) {
  96                const char *arg = request->line;
  97                const char *out;
  98
  99                if (!strcmp("peel", arg))
 100                        data.peel = 1;
 101                else if (!strcmp("symrefs", arg))
 102                        data.symrefs = 1;
 103                else if (skip_prefix(arg, "ref-prefix ", &out))
 104                        argv_array_push(&data.prefixes, out);
 105        }
 106
 107        head_ref_namespaced(send_ref, &data);
 108        for_each_namespaced_ref(send_ref, &data);
 109        packet_flush(1);
 110        argv_array_clear(&data.prefixes);
 111        return 0;
 112}