Merge branch 'ds/line-log-asan-fix'
[git/gitster.git] / replace-object.c
blob9a3cdd809a9b484312940f16c9178a90b5e3b94a
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "oidmap.h"
5 #include "object-store-ll.h"
6 #include "replace-object.h"
7 #include "refs.h"
8 #include "repository.h"
9 #include "commit.h"
11 static int register_replace_ref(const char *refname,
12 const char *referent UNUSED,
13 const struct object_id *oid,
14 int flag UNUSED,
15 void *cb_data)
17 struct repository *r = cb_data;
19 /* Get sha1 from refname */
20 const char *slash = strrchr(refname, '/');
21 const char *hash = slash ? slash + 1 : refname;
22 struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
24 if (get_oid_hex_algop(hash, &repl_obj->original.oid, r->hash_algo)) {
25 free(repl_obj);
26 warning(_("bad replace ref name: %s"), refname);
27 return 0;
30 /* Copy sha1 from the read ref */
31 oidcpy(&repl_obj->replacement, oid);
33 /* Register new object */
34 if (oidmap_put(r->objects->replace_map, repl_obj))
35 die(_("duplicate replace ref: %s"), refname);
37 return 0;
40 void prepare_replace_object(struct repository *r)
42 if (r->objects->replace_map_initialized)
43 return;
45 pthread_mutex_lock(&r->objects->replace_mutex);
46 if (r->objects->replace_map_initialized) {
47 pthread_mutex_unlock(&r->objects->replace_mutex);
48 return;
51 r->objects->replace_map =
52 xmalloc(sizeof(*r->objects->replace_map));
53 oidmap_init(r->objects->replace_map, 0);
55 refs_for_each_replace_ref(get_main_ref_store(r),
56 register_replace_ref, r);
57 r->objects->replace_map_initialized = 1;
59 pthread_mutex_unlock(&r->objects->replace_mutex);
62 /* We allow "recursive" replacement. Only within reason, though */
63 #define MAXREPLACEDEPTH 5
66 * If a replacement for object oid has been set up, return the
67 * replacement object's name (replaced recursively, if necessary).
68 * The return value is either oid or a pointer to a
69 * permanently-allocated value. This function always respects replace
70 * references, regardless of the value of r->settings.read_replace_refs.
72 const struct object_id *do_lookup_replace_object(struct repository *r,
73 const struct object_id *oid)
75 int depth = MAXREPLACEDEPTH;
76 const struct object_id *cur = oid;
78 prepare_replace_object(r);
80 /* Try to recursively replace the object */
81 while (depth-- > 0) {
82 struct replace_object *repl_obj =
83 oidmap_get(r->objects->replace_map, cur);
84 if (!repl_obj)
85 return cur;
86 cur = &repl_obj->replacement;
88 die(_("replace depth too high for object %s"), oid_to_hex(oid));
92 * This indicator determines whether replace references should be
93 * respected process-wide, regardless of which repository is being
94 * using at the time.
96 static int read_replace_refs = 1;
98 void disable_replace_refs(void)
100 read_replace_refs = 0;
103 int replace_refs_enabled(struct repository *r)
105 if (!read_replace_refs)
106 return 0;
108 if (r->gitdir) {
109 prepare_repo_settings(r);
110 return r->settings.read_replace_refs;
113 /* repository has no objects or refs. */
114 return 0;