]> git.ipfire.org Git - thirdparty/git.git/blob - replace-object.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / replace-object.c
1 #include "cache.h"
2 #include "oidmap.h"
3 #include "object-store.h"
4 #include "replace-object.h"
5 #include "refs.h"
6 #include "repository.h"
7 #include "commit.h"
8
9 static int register_replace_ref(struct repository *r,
10 const char *refname,
11 const struct object_id *oid,
12 int flag UNUSED,
13 void *cb_data UNUSED)
14 {
15 /* Get sha1 from refname */
16 const char *slash = strrchr(refname, '/');
17 const char *hash = slash ? slash + 1 : refname;
18 struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
19
20 if (get_oid_hex(hash, &repl_obj->original.oid)) {
21 free(repl_obj);
22 warning(_("bad replace ref name: %s"), refname);
23 return 0;
24 }
25
26 /* Copy sha1 from the read ref */
27 oidcpy(&repl_obj->replacement, oid);
28
29 /* Register new object */
30 if (oidmap_put(r->objects->replace_map, repl_obj))
31 die(_("duplicate replace ref: %s"), refname);
32
33 return 0;
34 }
35
36 void prepare_replace_object(struct repository *r)
37 {
38 if (r->objects->replace_map_initialized)
39 return;
40
41 pthread_mutex_lock(&r->objects->replace_mutex);
42 if (r->objects->replace_map_initialized) {
43 pthread_mutex_unlock(&r->objects->replace_mutex);
44 return;
45 }
46
47 r->objects->replace_map =
48 xmalloc(sizeof(*r->objects->replace_map));
49 oidmap_init(r->objects->replace_map, 0);
50
51 for_each_replace_ref(r, register_replace_ref, NULL);
52 r->objects->replace_map_initialized = 1;
53
54 pthread_mutex_unlock(&r->objects->replace_mutex);
55 }
56
57 /* We allow "recursive" replacement. Only within reason, though */
58 #define MAXREPLACEDEPTH 5
59
60 /*
61 * If a replacement for object oid has been set up, return the
62 * replacement object's name (replaced recursively, if necessary).
63 * The return value is either oid or a pointer to a
64 * permanently-allocated value. This function always respects replace
65 * references, regardless of the value of read_replace_refs.
66 */
67 const struct object_id *do_lookup_replace_object(struct repository *r,
68 const struct object_id *oid)
69 {
70 int depth = MAXREPLACEDEPTH;
71 const struct object_id *cur = oid;
72
73 prepare_replace_object(r);
74
75 /* Try to recursively replace the object */
76 while (depth-- > 0) {
77 struct replace_object *repl_obj =
78 oidmap_get(r->objects->replace_map, cur);
79 if (!repl_obj)
80 return cur;
81 cur = &repl_obj->replacement;
82 }
83 die(_("replace depth too high for object %s"), oid_to_hex(oid));
84 }