1 #include "git-compat-util.h"
5 #include "object-store.h"
6 #include "replace-object.h"
8 #include "repository.h"
11 static int register_replace_ref(struct repository
*r
,
13 const struct object_id
*oid
,
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(r
->objects
->replace_map
, repl_obj
))
33 die(_("duplicate replace ref: %s"), refname
);
38 void prepare_replace_object(struct repository
*r
)
40 if (r
->objects
->replace_map_initialized
)
43 pthread_mutex_lock(&r
->objects
->replace_mutex
);
44 if (r
->objects
->replace_map_initialized
) {
45 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
49 r
->objects
->replace_map
=
50 xmalloc(sizeof(*r
->objects
->replace_map
));
51 oidmap_init(r
->objects
->replace_map
, 0);
53 for_each_replace_ref(r
, register_replace_ref
, NULL
);
54 r
->objects
->replace_map_initialized
= 1;
56 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
59 /* We allow "recursive" replacement. Only within reason, though */
60 #define MAXREPLACEDEPTH 5
63 * If a replacement for object oid has been set up, return the
64 * replacement object's name (replaced recursively, if necessary).
65 * The return value is either oid or a pointer to a
66 * permanently-allocated value. This function always respects replace
67 * references, regardless of the value of read_replace_refs.
69 const struct object_id
*do_lookup_replace_object(struct repository
*r
,
70 const struct object_id
*oid
)
72 int depth
= MAXREPLACEDEPTH
;
73 const struct object_id
*cur
= oid
;
75 prepare_replace_object(r
);
77 /* Try to recursively replace the object */
79 struct replace_object
*repl_obj
=
80 oidmap_get(r
->objects
->replace_map
, cur
);
83 cur
= &repl_obj
->replacement
;
85 die(_("replace depth too high for object %s"), oid_to_hex(oid
));