git-merge-tree.txt: replace spurious HTML entity
[git/debian.git] / replace-object.c
blob320be2522d80a87fdb1569a97c5e24b77fd4a525
1 #include "cache.h"
2 #include "oidmap.h"
3 #include "object-store.h"
4 #include "replace-object.h"
5 #include "refs.h"
6 #include "repository.h"
7 #include "commit.h"
9 static int register_replace_ref(struct repository *r,
10 const char *refname,
11 const struct object_id *oid,
12 int flag UNUSED,
13 void *cb_data UNUSED)
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)) {
21 free(repl_obj);
22 warning(_("bad replace ref name: %s"), refname);
23 return 0;
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);
33 return 0;
36 void prepare_replace_object(struct repository *r)
38 if (r->objects->replace_map_initialized)
39 return;
41 pthread_mutex_lock(&r->objects->replace_mutex);
42 if (r->objects->replace_map_initialized) {
43 pthread_mutex_unlock(&r->objects->replace_mutex);
44 return;
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 */
76 while (depth-- > 0) {
77 struct replace_object *repl_obj =
78 oidmap_get(r->objects->replace_map, cur);
79 if (!repl_obj)
80 return cur;
81 cur = &repl_obj->replacement;
83 die(_("replace depth too high for object %s"), oid_to_hex(oid));