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 | |
68095570 | 9 | static int register_replace_ref(const char *refname, |
00530834 | 10 | const struct object_id *oid, |
68095570 CC |
11 | int flag, void *cb_data) |
12 | { | |
13 | /* Get sha1 from refname */ | |
14 | const char *slash = strrchr(refname, '/'); | |
15 | const char *hash = slash ? slash + 1 : refname; | |
16 | struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj)); | |
17 | ||
f37b9bc0 | 18 | if (get_oid_hex(hash, &repl_obj->original.oid)) { |
68095570 CC |
19 | free(repl_obj); |
20 | warning("bad replace ref name: %s", refname); | |
21 | return 0; | |
22 | } | |
23 | ||
24 | /* Copy sha1 from the read ref */ | |
1731a1e2 | 25 | oidcpy(&repl_obj->replacement, oid); |
68095570 CC |
26 | |
27 | /* Register new object */ | |
d88f9fdf | 28 | if (oidmap_put(&the_repository->objects->replace_map, repl_obj)) |
68095570 CC |
29 | die("duplicate replace ref: %s", refname); |
30 | ||
31 | return 0; | |
32 | } | |
33 | ||
34 | static void prepare_replace_object(void) | |
35 | { | |
36 | static int replace_object_prepared; | |
37 | ||
38 | if (replace_object_prepared) | |
39 | return; | |
40 | ||
00530834 | 41 | for_each_replace_ref(register_replace_ref, NULL); |
68095570 | 42 | replace_object_prepared = 1; |
d88f9fdf | 43 | if (!the_repository->objects->replace_map.map.tablesize) |
afc711b8 | 44 | check_replace_refs = 0; |
68095570 CC |
45 | } |
46 | ||
47 | /* We allow "recursive" replacement. Only within reason, though */ | |
48 | #define MAXREPLACEDEPTH 5 | |
49 | ||
1f91e79c | 50 | /* |
b383a13c | 51 | * If a replacement for object oid has been set up, return the |
1f91e79c | 52 | * replacement object's name (replaced recursively, if necessary). |
b383a13c | 53 | * The return value is either oid or a pointer to a |
1f91e79c MH |
54 | * permanently-allocated value. This function always respects replace |
55 | * references, regardless of the value of check_replace_refs. | |
56 | */ | |
b383a13c | 57 | const struct object_id *do_lookup_replace_object(const struct object_id *oid) |
68095570 | 58 | { |
f37b9bc0 | 59 | int depth = MAXREPLACEDEPTH; |
b383a13c | 60 | const struct object_id *cur = oid; |
68095570 CC |
61 | |
62 | prepare_replace_object(); | |
63 | ||
64 | /* Try to recursively replace the object */ | |
f37b9bc0 | 65 | while (depth-- > 0) { |
d88f9fdf SB |
66 | struct replace_object *repl_obj = |
67 | oidmap_get(&the_repository->objects->replace_map, cur); | |
f37b9bc0 RS |
68 | if (!repl_obj) |
69 | return cur; | |
70 | cur = &repl_obj->replacement; | |
71 | } | |
72 | die("replace depth too high for object %s", oid_to_hex(oid)); | |
68095570 | 73 | } |