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 struct object_id
*oid
,
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_algop(hash
, &repl_obj
->original
.oid
, r
->hash_algo
)) {
25 warning(_("bad replace ref name: %s"), refname
);
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
);
39 void prepare_replace_object(struct repository
*r
)
41 if (r
->objects
->replace_map_initialized
)
44 pthread_mutex_lock(&r
->objects
->replace_mutex
);
45 if (r
->objects
->replace_map_initialized
) {
46 pthread_mutex_unlock(&r
->objects
->replace_mutex
);
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 */
81 struct replace_object
*repl_obj
=
82 oidmap_get(r
->objects
->replace_map
, 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
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
)
108 prepare_repo_settings(r
);
109 return r
->settings
.read_replace_refs
;
112 /* repository has no objects or refs. */