]> git.ipfire.org Git - thirdparty/git.git/blob - replace-object.c
Git 2.45
[thirdparty/git.git] / replace-object.c
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"
10
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)
16 {
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));
21
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;
26 }
27
28 /* Copy sha1 from the read ref */
29 oidcpy(&repl_obj->replacement, oid);
30
31 /* Register new object */
32 if (oidmap_put(r->objects->replace_map, repl_obj))
33 die(_("duplicate replace ref: %s"), refname);
34
35 return 0;
36 }
37
38 void prepare_replace_object(struct repository *r)
39 {
40 if (r->objects->replace_map_initialized)
41 return;
42
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;
47 }
48
49 r->objects->replace_map =
50 xmalloc(sizeof(*r->objects->replace_map));
51 oidmap_init(r->objects->replace_map, 0);
52
53 for_each_replace_ref(r, register_replace_ref, NULL);
54 r->objects->replace_map_initialized = 1;
55
56 pthread_mutex_unlock(&r->objects->replace_mutex);
57 }
58
59 /* We allow "recursive" replacement. Only within reason, though */
60 #define MAXREPLACEDEPTH 5
61
62 /*
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.
68 */
69 const struct object_id *do_lookup_replace_object(struct repository *r,
70 const struct object_id *oid)
71 {
72 int depth = MAXREPLACEDEPTH;
73 const struct object_id *cur = oid;
74
75 prepare_replace_object(r);
76
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;
84 }
85 die(_("replace depth too high for object %s"), oid_to_hex(oid));
86 }
87
88 /*
89 * This indicator determines whether replace references should be
90 * respected process-wide, regardless of which repository is being
91 * using at the time.
92 */
93 static int read_replace_refs = 1;
94
95 void disable_replace_refs(void)
96 {
97 read_replace_refs = 0;
98 }
99
100 int replace_refs_enabled(struct repository *r)
101 {
102 if (!read_replace_refs)
103 return 0;
104
105 if (r->gitdir) {
106 prepare_repo_settings(r);
107 return r->settings.read_replace_refs;
108 }
109
110 /* repository has no objects or refs. */
111 return 0;
112 }