Git fork
1#include "git-compat-util.h"
2#include "gettext.h"
3#include "hex.h"
4#include "oidmap.h"
5#include "odb.h"
6#include "replace-object.h"
7#include "refs.h"
8#include "repository.h"
9#include "commit.h"
10
11static 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)
16{
17 struct repository *r = cb_data;
18
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));
23
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;
28 }
29
30 /* Copy sha1 from the read ref */
31 oidcpy(&repl_obj->replacement, oid);
32
33 /* Register new object */
34 if (oidmap_put(&r->objects->replace_map, repl_obj))
35 die(_("duplicate replace ref: %s"), refname);
36
37 return 0;
38}
39
40void prepare_replace_object(struct repository *r)
41{
42 if (r->objects->replace_map_initialized)
43 return;
44
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;
49 }
50
51 oidmap_init(&r->objects->replace_map, 0);
52
53 refs_for_each_replace_ref(get_main_ref_store(r),
54 register_replace_ref, r);
55 r->objects->replace_map_initialized = 1;
56
57 pthread_mutex_unlock(&r->objects->replace_mutex);
58}
59
60/* We allow "recursive" replacement. Only within reason, though */
61#define MAXREPLACEDEPTH 5
62
63/*
64 * If a replacement for object oid has been set up, return the
65 * replacement object's name (replaced recursively, if necessary).
66 * The return value is either oid or a pointer to a
67 * permanently-allocated value. This function always respects replace
68 * references, regardless of the value of r->settings.read_replace_refs.
69 */
70const struct object_id *do_lookup_replace_object(struct repository *r,
71 const struct object_id *oid)
72{
73 int depth = MAXREPLACEDEPTH;
74 const struct object_id *cur = oid;
75
76 prepare_replace_object(r);
77
78 /* Try to recursively replace the object */
79 while (depth-- > 0) {
80 struct replace_object *repl_obj =
81 oidmap_get(&r->objects->replace_map, cur);
82 if (!repl_obj)
83 return cur;
84 cur = &repl_obj->replacement;
85 }
86 die(_("replace depth too high for object %s"), oid_to_hex(oid));
87}
88
89/*
90 * This indicator determines whether replace references should be
91 * respected process-wide, regardless of which repository is being
92 * using at the time.
93 */
94static int read_replace_refs = 1;
95
96void disable_replace_refs(void)
97{
98 read_replace_refs = 0;
99}
100
101int replace_refs_enabled(struct repository *r)
102{
103 if (!read_replace_refs)
104 return 0;
105
106 if (r->gitdir) {
107 prepare_repo_settings(r);
108 return r->settings.read_replace_refs;
109 }
110
111 /* repository has no objects or refs. */
112 return 0;
113}