3 #include "object-store.h"
4 #include "replace-object.h"
6 #include "repository.h"
9 static int register_replace_ref(const char *refname,
10 const struct object_id *oid,
11 int flag, void *cb_data)
13 /* Get sha1 from refname */
14 const char *slash = strrchr(refname, '/');
15 const char *hash = slash ? slash + 1 : refname;
16 struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
18 if (get_oid_hex(hash, &repl_obj->original.oid)) {
20 warning(_("bad replace ref name: %s"), refname);
24 /* Copy sha1 from the read ref */
25 oidcpy(&repl_obj->replacement, oid);
27 /* Register new object */
28 if (oidmap_put(the_repository->objects->replace_map, repl_obj))
29 die(_("duplicate replace ref: %s"), refname);
34 static void prepare_replace_object(struct repository *r)
36 if (r->objects->replace_map)
39 r->objects->replace_map =
40 xmalloc(sizeof(*r->objects->replace_map));
41 oidmap_init(r->objects->replace_map, 0);
43 for_each_replace_ref(r, register_replace_ref, NULL);
46 /* We allow "recursive" replacement. Only within reason, though */
47 #define MAXREPLACEDEPTH 5
50 * If a replacement for object oid has been set up, return the
51 * replacement object's name (replaced recursively, if necessary).
52 * The return value is either oid or a pointer to a
53 * permanently-allocated value. This function always respects replace
54 * references, regardless of the value of read_replace_refs.
56 const struct object_id *do_lookup_replace_object(struct repository *r,
57 const struct object_id *oid)
59 int depth = MAXREPLACEDEPTH;
60 const struct object_id *cur = oid;
62 prepare_replace_object(r);
64 /* Try to recursively replace the object */
66 struct replace_object *repl_obj =
67 oidmap_get(r->objects->replace_map, cur);
70 cur = &repl_obj->replacement;
72 die(_("replace depth too high for object %s"), oid_to_hex(oid));