]>
Commit | Line | Data |
---|---|---|
68095570 | 1 | #include "cache.h" |
f37b9bc0 | 2 | #include "oidmap.h" |
d88f9fdf SB |
3 | #include "object-store.h" |
4 | #include "replace-object.h" | |
68095570 | 5 | #include "refs.h" |
d88f9fdf | 6 | #include "repository.h" |
c2e86add | 7 | #include "commit.h" |
68095570 | 8 | |
212e0f7e SB |
9 | static int register_replace_ref(struct repository *r, |
10 | const char *refname, | |
00530834 | 11 | const struct object_id *oid, |
5cf88fd8 ÆAB |
12 | int flag UNUSED, |
13 | void *cb_data UNUSED) | |
68095570 CC |
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 | ||
f37b9bc0 | 20 | if (get_oid_hex(hash, &repl_obj->original.oid)) { |
68095570 | 21 | free(repl_obj); |
b73c6e3a | 22 | warning(_("bad replace ref name: %s"), refname); |
68095570 CC |
23 | return 0; |
24 | } | |
25 | ||
26 | /* Copy sha1 from the read ref */ | |
1731a1e2 | 27 | oidcpy(&repl_obj->replacement, oid); |
68095570 CC |
28 | |
29 | /* Register new object */ | |
212e0f7e | 30 | if (oidmap_put(r->objects->replace_map, repl_obj)) |
b73c6e3a | 31 | die(_("duplicate replace ref: %s"), refname); |
68095570 CC |
32 | |
33 | return 0; | |
34 | } | |
35 | ||
d6538246 | 36 | void prepare_replace_object(struct repository *r) |
68095570 | 37 | { |
b1fc9da1 | 38 | if (r->objects->replace_map_initialized) |
68095570 CC |
39 | return; |
40 | ||
b1fc9da1 MT |
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 | ||
5982da9d | 47 | r->objects->replace_map = |
74fd0705 | 48 | xmalloc(sizeof(*r->objects->replace_map)); |
5982da9d | 49 | oidmap_init(r->objects->replace_map, 0); |
c1274495 | 50 | |
5982da9d | 51 | for_each_replace_ref(r, register_replace_ref, NULL); |
b1fc9da1 MT |
52 | r->objects->replace_map_initialized = 1; |
53 | ||
54 | pthread_mutex_unlock(&r->objects->replace_mutex); | |
68095570 CC |
55 | } |
56 | ||
57 | /* We allow "recursive" replacement. Only within reason, though */ | |
58 | #define MAXREPLACEDEPTH 5 | |
59 | ||
1f91e79c | 60 | /* |
b383a13c | 61 | * If a replacement for object oid has been set up, return the |
1f91e79c | 62 | * replacement object's name (replaced recursively, if necessary). |
b383a13c | 63 | * The return value is either oid or a pointer to a |
1f91e79c | 64 | * permanently-allocated value. This function always respects replace |
6ebd1caf | 65 | * references, regardless of the value of read_replace_refs. |
1f91e79c | 66 | */ |
5643557e SB |
67 | const struct object_id *do_lookup_replace_object(struct repository *r, |
68 | const struct object_id *oid) | |
68095570 | 69 | { |
f37b9bc0 | 70 | int depth = MAXREPLACEDEPTH; |
b383a13c | 71 | const struct object_id *cur = oid; |
68095570 | 72 | |
5643557e | 73 | prepare_replace_object(r); |
68095570 CC |
74 | |
75 | /* Try to recursively replace the object */ | |
f37b9bc0 | 76 | while (depth-- > 0) { |
d88f9fdf | 77 | struct replace_object *repl_obj = |
5643557e | 78 | oidmap_get(r->objects->replace_map, cur); |
f37b9bc0 RS |
79 | if (!repl_obj) |
80 | return cur; | |
81 | cur = &repl_obj->replacement; | |
82 | } | |
b73c6e3a | 83 | die(_("replace depth too high for object %s"), oid_to_hex(oid)); |
68095570 | 84 | } |