replace_object.con commit push: test to verify that push errors are colored (8301266)
   1#include "cache.h"
   2#include "sha1-lookup.h"
   3#include "refs.h"
   4#include "commit.h"
   5
   6/*
   7 * An array of replacements.  The array is kept sorted by the original
   8 * sha1.
   9 */
  10static struct replace_object {
  11        struct object_id original;
  12        struct object_id replacement;
  13} **replace_object;
  14
  15static int replace_object_alloc, replace_object_nr;
  16
  17static const unsigned char *replace_sha1_access(size_t index, void *table)
  18{
  19        struct replace_object **replace = table;
  20        return replace[index]->original.hash;
  21}
  22
  23static int replace_object_pos(const unsigned char *sha1)
  24{
  25        return sha1_pos(sha1, replace_object, replace_object_nr,
  26                        replace_sha1_access);
  27}
  28
  29static int register_replace_object(struct replace_object *replace,
  30                                   int ignore_dups)
  31{
  32        int pos = replace_object_pos(replace->original.hash);
  33
  34        if (0 <= pos) {
  35                if (ignore_dups)
  36                        free(replace);
  37                else {
  38                        free(replace_object[pos]);
  39                        replace_object[pos] = replace;
  40                }
  41                return 1;
  42        }
  43        pos = -pos - 1;
  44        ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc);
  45        replace_object_nr++;
  46        if (pos < replace_object_nr)
  47                MOVE_ARRAY(replace_object + pos + 1, replace_object + pos,
  48                           replace_object_nr - pos - 1);
  49        replace_object[pos] = replace;
  50        return 0;
  51}
  52
  53static int register_replace_ref(const char *refname,
  54                                const struct object_id *oid,
  55                                int flag, void *cb_data)
  56{
  57        /* Get sha1 from refname */
  58        const char *slash = strrchr(refname, '/');
  59        const char *hash = slash ? slash + 1 : refname;
  60        struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
  61
  62        if (get_oid_hex(hash, &repl_obj->original)) {
  63                free(repl_obj);
  64                warning("bad replace ref name: %s", refname);
  65                return 0;
  66        }
  67
  68        /* Copy sha1 from the read ref */
  69        oidcpy(&repl_obj->replacement, oid);
  70
  71        /* Register new object */
  72        if (register_replace_object(repl_obj, 1))
  73                die("duplicate replace ref: %s", refname);
  74
  75        return 0;
  76}
  77
  78static void prepare_replace_object(void)
  79{
  80        static int replace_object_prepared;
  81
  82        if (replace_object_prepared)
  83                return;
  84
  85        for_each_replace_ref(register_replace_ref, NULL);
  86        replace_object_prepared = 1;
  87        if (!replace_object_nr)
  88                check_replace_refs = 0;
  89}
  90
  91/* We allow "recursive" replacement. Only within reason, though */
  92#define MAXREPLACEDEPTH 5
  93
  94/*
  95 * If a replacement for object oid has been set up, return the
  96 * replacement object's name (replaced recursively, if necessary).
  97 * The return value is either oid or a pointer to a
  98 * permanently-allocated value.  This function always respects replace
  99 * references, regardless of the value of check_replace_refs.
 100 */
 101const struct object_id *do_lookup_replace_object(const struct object_id *oid)
 102{
 103        int pos, depth = MAXREPLACEDEPTH;
 104        const struct object_id *cur = oid;
 105
 106        prepare_replace_object();
 107
 108        /* Try to recursively replace the object */
 109        do {
 110                if (--depth < 0)
 111                        die("replace depth too high for object %s",
 112                            oid_to_hex(oid));
 113
 114                pos = replace_object_pos(cur->hash);
 115                if (0 <= pos)
 116                        cur = &replace_object[pos]->replacement;
 117        } while (0 <= pos);
 118
 119        return cur;
 120}