Merge branch 'pv/doc-submodule-update-settings'
[git/debian.git] / replace-object.c
blob523215589de9221b0823aab9edbb06389ca52825
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(struct repository *r,
12 const char *refname,
13 const struct object_id *oid,
14 int flag UNUSED,
15 void *cb_data UNUSED)
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)) {
23 free(repl_obj);
24 warning(_("bad replace ref name: %s"), refname);
25 return 0;
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);
35 return 0;
38 void prepare_replace_object(struct repository *r)
40 if (r->objects->replace_map_initialized)
41 return;
43 pthread_mutex_lock(&r->objects->replace_mutex);
44 if (r->objects->replace_map_initialized) {
45 pthread_mutex_unlock(&r->objects->replace_mutex);
46 return;
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 r->settings.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 */
78 while (depth-- > 0) {
79 struct replace_object *repl_obj =
80 oidmap_get(r->objects->replace_map, cur);
81 if (!repl_obj)
82 return cur;
83 cur = &repl_obj->replacement;
85 die(_("replace depth too high for object %s"), oid_to_hex(oid));
89 * This indicator determines whether replace references should be
90 * respected process-wide, regardless of which repository is being
91 * using at the time.
93 static int read_replace_refs = 1;
95 void disable_replace_refs(void)
97 read_replace_refs = 0;
100 int replace_refs_enabled(struct repository *r)
102 if (!read_replace_refs)
103 return 0;
105 if (r->gitdir) {
106 prepare_repo_settings(r);
107 return r->settings.read_replace_refs;
110 /* repository has no objects or refs. */
111 return 0;