4 #include "object-store.h"
5 #include "replace-object.h"
7 #include "repository.h"
10 static int register_replace_ref(struct repository
*r
,
12 const struct object_id
*oid
,
16 /* Get sha1 from refname */
17 const char *slash
= strrchr(refname
, '/');
18 const char *hash
= slash
? slash
+ 1 : refname
;
19 struct replace_object
*repl_obj
= xmalloc(sizeof(*repl_obj
));
21 if (get_oid_hex(hash
, &repl_obj
->original
.oid
)) {
23 warning(_("bad replace ref name: %s"), refname
);
27 /* Copy sha1 from the read ref */
28 oidcpy(&repl_obj
->replacement
, oid
);
30 /* Register new object */
31 if (oidmap_put(r
->objects
->replace_map
, repl_obj
))
32 die(_("duplicate replace ref: %s"), refname
);
37 void prepare_replace_object(struct repository
*r
)
39 if (r
->objects
->replace_map_initialized
)
42 pthread_mutex_lock(&r
->objects
->replace_mutex
);
43 if (r
->objects
->replace_map_initialized
) {
44 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
48 r
->objects
->replace_map
=
49 xmalloc(sizeof(*r
->objects
->replace_map
));
50 oidmap_init(r
->objects
->replace_map
, 0);
52 for_each_replace_ref(r
, register_replace_ref
, NULL
);
53 r
->objects
->replace_map_initialized
= 1;
55 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
58 /* We allow "recursive" replacement. Only within reason, though */
59 #define MAXREPLACEDEPTH 5
62 * If a replacement for object oid has been set up, return the
63 * replacement object's name (replaced recursively, if necessary).
64 * The return value is either oid or a pointer to a
65 * permanently-allocated value. This function always respects replace
66 * references, regardless of the value of read_replace_refs.
68 const struct object_id
*do_lookup_replace_object(struct repository
*r
,
69 const struct object_id
*oid
)
71 int depth
= MAXREPLACEDEPTH
;
72 const struct object_id
*cur
= oid
;
74 prepare_replace_object(r
);
76 /* Try to recursively replace the object */
78 struct replace_object
*repl_obj
=
79 oidmap_get(r
->objects
->replace_map
, cur
);
82 cur
= &repl_obj
->replacement
;
84 die(_("replace depth too high for object %s"), oid_to_hex(oid
));