6 struct replace_object
{
7 struct oidmap_entry original
;
8 struct object_id replacement
;
11 static struct oidmap replace_map
= OIDMAP_INIT
;
13 static int register_replace_ref(const char *refname
,
14 const struct object_id
*oid
,
15 int flag
, void *cb_data
)
17 /* Get sha1 from refname */
18 const char *slash
= strrchr(refname
, '/');
19 const char *hash
= slash
? slash
+ 1 : refname
;
20 struct replace_object
*repl_obj
= xmalloc(sizeof(*repl_obj
));
22 if (get_oid_hex(hash
, &repl_obj
->original
.oid
)) {
24 warning("bad replace ref name: %s", refname
);
28 /* Copy sha1 from the read ref */
29 oidcpy(&repl_obj
->replacement
, oid
);
31 /* Register new object */
32 if (oidmap_put(&replace_map
, repl_obj
))
33 die("duplicate replace ref: %s", refname
);
38 static void prepare_replace_object(void)
40 static int replace_object_prepared
;
42 if (replace_object_prepared
)
45 for_each_replace_ref(register_replace_ref
, NULL
);
46 replace_object_prepared
= 1;
47 if (!replace_map
.map
.tablesize
)
48 check_replace_refs
= 0;
51 /* We allow "recursive" replacement. Only within reason, though */
52 #define MAXREPLACEDEPTH 5
55 * If a replacement for object oid has been set up, return the
56 * replacement object's name (replaced recursively, if necessary).
57 * The return value is either oid or a pointer to a
58 * permanently-allocated value. This function always respects replace
59 * references, regardless of the value of check_replace_refs.
61 const struct object_id
*do_lookup_replace_object(const struct object_id
*oid
)
63 int depth
= MAXREPLACEDEPTH
;
64 const struct object_id
*cur
= oid
;
66 prepare_replace_object();
68 /* Try to recursively replace the object */
70 struct replace_object
*repl_obj
= oidmap_get(&replace_map
, cur
);
73 cur
= &repl_obj
->replacement
;
75 die("replace depth too high for object %s", oid_to_hex(oid
));