]> git.ipfire.org Git - thirdparty/git.git/blame - replace-object.c
Merge git://ozlabs.org/~paulus/gitk
[thirdparty/git.git] / replace-object.c
CommitLineData
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
9static int register_replace_ref(struct repository *r,
10 const char *refname,
00530834 11 const struct object_id *oid,
68095570
CC
12 int flag, void *cb_data)
13{
14 /* Get sha1 from refname */
15 const char *slash = strrchr(refname, '/');
16 const char *hash = slash ? slash + 1 : refname;
17 struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
18
f37b9bc0 19 if (get_oid_hex(hash, &repl_obj->original.oid)) {
68095570 20 free(repl_obj);
b73c6e3a 21 warning(_("bad replace ref name: %s"), refname);
68095570
CC
22 return 0;
23 }
24
25 /* Copy sha1 from the read ref */
1731a1e2 26 oidcpy(&repl_obj->replacement, oid);
68095570
CC
27
28 /* Register new object */
212e0f7e 29 if (oidmap_put(r->objects->replace_map, repl_obj))
b73c6e3a 30 die(_("duplicate replace ref: %s"), refname);
68095570
CC
31
32 return 0;
33}
34
d6538246 35void prepare_replace_object(struct repository *r)
68095570 36{
5982da9d 37 if (r->objects->replace_map)
68095570
CC
38 return;
39
5982da9d 40 r->objects->replace_map =
74fd0705 41 xmalloc(sizeof(*r->objects->replace_map));
5982da9d 42 oidmap_init(r->objects->replace_map, 0);
c1274495 43
5982da9d 44 for_each_replace_ref(r, register_replace_ref, NULL);
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 54 * permanently-allocated value. This function always respects replace
6ebd1caf 55 * references, regardless of the value of read_replace_refs.
1f91e79c 56 */
5643557e
SB
57const struct object_id *do_lookup_replace_object(struct repository *r,
58 const struct object_id *oid)
68095570 59{
f37b9bc0 60 int depth = MAXREPLACEDEPTH;
b383a13c 61 const struct object_id *cur = oid;
68095570 62
5643557e 63 prepare_replace_object(r);
68095570
CC
64
65 /* Try to recursively replace the object */
f37b9bc0 66 while (depth-- > 0) {
d88f9fdf 67 struct replace_object *repl_obj =
5643557e 68 oidmap_get(r->objects->replace_map, cur);
f37b9bc0
RS
69 if (!repl_obj)
70 return cur;
71 cur = &repl_obj->replacement;
72 }
b73c6e3a 73 die(_("replace depth too high for object %s"), oid_to_hex(oid));
68095570 74}