hash: require hash algorithm in `empty_tree_oid_hex()`
[git/gitster.git] / replace-object.c
blob73f5acbcd9f8527a5659490f546d580ef50bd3fb
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 struct object_id *oid,
13 int flag UNUSED,
14 void *cb_data)
16 struct repository *r = cb_data;
18 /* Get sha1 from refname */
19 const char *slash = strrchr(refname, '/');
20 const char *hash = slash ? slash + 1 : refname;
21 struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
23 if (get_oid_hex(hash, &repl_obj->original.oid)) {
24 free(repl_obj);
25 warning(_("bad replace ref name: %s"), refname);
26 return 0;
29 /* Copy sha1 from the read ref */
30 oidcpy(&repl_obj->replacement, oid);
32 /* Register new object */
33 if (oidmap_put(r->objects->replace_map, repl_obj))
34 die(_("duplicate replace ref: %s"), refname);
36 return 0;
39 void prepare_replace_object(struct repository *r)
41 if (r->objects->replace_map_initialized)
42 return;
44 pthread_mutex_lock(&r->objects->replace_mutex);
45 if (r->objects->replace_map_initialized) {
46 pthread_mutex_unlock(&r->objects->replace_mutex);
47 return;
50 r->objects->replace_map =
51 xmalloc(sizeof(*r->objects->replace_map));
52 oidmap_init(r->objects->replace_map, 0);
54 refs_for_each_replace_ref(get_main_ref_store(r),
55 register_replace_ref, r);
56 r->objects->replace_map_initialized = 1;
58 pthread_mutex_unlock(&r->objects->replace_mutex);
61 /* We allow "recursive" replacement. Only within reason, though */
62 #define MAXREPLACEDEPTH 5
65 * If a replacement for object oid has been set up, return the
66 * replacement object's name (replaced recursively, if necessary).
67 * The return value is either oid or a pointer to a
68 * permanently-allocated value. This function always respects replace
69 * references, regardless of the value of r->settings.read_replace_refs.
71 const struct object_id *do_lookup_replace_object(struct repository *r,
72 const struct object_id *oid)
74 int depth = MAXREPLACEDEPTH;
75 const struct object_id *cur = oid;
77 prepare_replace_object(r);
79 /* Try to recursively replace the object */
80 while (depth-- > 0) {
81 struct replace_object *repl_obj =
82 oidmap_get(r->objects->replace_map, cur);
83 if (!repl_obj)
84 return cur;
85 cur = &repl_obj->replacement;
87 die(_("replace depth too high for object %s"), oid_to_hex(oid));
91 * This indicator determines whether replace references should be
92 * respected process-wide, regardless of which repository is being
93 * using at the time.
95 static int read_replace_refs = 1;
97 void disable_replace_refs(void)
99 read_replace_refs = 0;
102 int replace_refs_enabled(struct repository *r)
104 if (!read_replace_refs)
105 return 0;
107 if (r->gitdir) {
108 prepare_repo_settings(r);
109 return r->settings.read_replace_refs;
112 /* repository has no objects or refs. */
113 return 0;