1 #include "git-compat-util.h"
5 #include "object-store-ll.h"
6 #include "replace-object.h"
8 #include "repository.h"
11 static int register_replace_ref(const char *refname
,
12 const char *referent UNUSED
,
13 const struct object_id
*oid
,
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
)) {
26 warning(_("bad replace ref name: %s"), refname
);
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
);
40 void prepare_replace_object(struct repository
*r
)
42 if (r
->objects
->replace_map_initialized
)
45 pthread_mutex_lock(&r
->objects
->replace_mutex
);
46 if (r
->objects
->replace_map_initialized
) {
47 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
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 */
82 struct replace_object
*repl_obj
=
83 oidmap_get(r
->objects
->replace_map
, 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
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
)
109 prepare_repo_settings(r
);
110 return r
->settings
.read_replace_refs
;
113 /* repository has no objects or refs. */