3 #include "object-store.h"
4 #include "replace-object.h"
6 #include "repository.h"
9 static int register_replace_ref(struct repository
*r
,
11 const struct object_id
*oid
,
15 /* Get sha1 from refname */
16 const char *slash
= strrchr(refname
, '/');
17 const char *hash
= slash
? slash
+ 1 : refname
;
18 struct replace_object
*repl_obj
= xmalloc(sizeof(*repl_obj
));
20 if (get_oid_hex(hash
, &repl_obj
->original
.oid
)) {
22 warning(_("bad replace ref name: %s"), refname
);
26 /* Copy sha1 from the read ref */
27 oidcpy(&repl_obj
->replacement
, oid
);
29 /* Register new object */
30 if (oidmap_put(r
->objects
->replace_map
, repl_obj
))
31 die(_("duplicate replace ref: %s"), refname
);
36 void prepare_replace_object(struct repository
*r
)
38 if (r
->objects
->replace_map_initialized
)
41 pthread_mutex_lock(&r
->objects
->replace_mutex
);
42 if (r
->objects
->replace_map_initialized
) {
43 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
47 r
->objects
->replace_map
=
48 xmalloc(sizeof(*r
->objects
->replace_map
));
49 oidmap_init(r
->objects
->replace_map
, 0);
51 for_each_replace_ref(r
, register_replace_ref
, NULL
);
52 r
->objects
->replace_map_initialized
= 1;
54 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
57 /* We allow "recursive" replacement. Only within reason, though */
58 #define MAXREPLACEDEPTH 5
61 * If a replacement for object oid has been set up, return the
62 * replacement object's name (replaced recursively, if necessary).
63 * The return value is either oid or a pointer to a
64 * permanently-allocated value. This function always respects replace
65 * references, regardless of the value of read_replace_refs.
67 const struct object_id
*do_lookup_replace_object(struct repository
*r
,
68 const struct object_id
*oid
)
70 int depth
= MAXREPLACEDEPTH
;
71 const struct object_id
*cur
= oid
;
73 prepare_replace_object(r
);
75 /* Try to recursively replace the object */
77 struct replace_object
*repl_obj
=
78 oidmap_get(r
->objects
->replace_map
, cur
);
81 cur
= &repl_obj
->replacement
;
83 die(_("replace depth too high for object %s"), oid_to_hex(oid
));