rename read_replace_refs to check_replace_refs
[git/git.git] / replace_object.c
CommitLineData
68095570
CC
1#include "cache.h"
2#include "sha1-lookup.h"
3#include "refs.h"
c2e86add 4#include "commit.h"
68095570
CC
5
6static struct replace_object {
7 unsigned char sha1[2][20];
8} **replace_object;
9
10static int replace_object_alloc, replace_object_nr;
11
12static const unsigned char *replace_sha1_access(size_t index, void *table)
13{
14 struct replace_object **replace = table;
15 return replace[index]->sha1[0];
16}
17
18static int replace_object_pos(const unsigned char *sha1)
19{
20 return sha1_pos(sha1, replace_object, replace_object_nr,
21 replace_sha1_access);
22}
23
24static int register_replace_object(struct replace_object *replace,
25 int ignore_dups)
26{
27 int pos = replace_object_pos(replace->sha1[0]);
28
29 if (0 <= pos) {
30 if (ignore_dups)
31 free(replace);
32 else {
33 free(replace_object[pos]);
34 replace_object[pos] = replace;
35 }
36 return 1;
37 }
38 pos = -pos - 1;
39 if (replace_object_alloc <= ++replace_object_nr) {
40 replace_object_alloc = alloc_nr(replace_object_alloc);
41 replace_object = xrealloc(replace_object,
42 sizeof(*replace_object) *
43 replace_object_alloc);
44 }
45 if (pos < replace_object_nr)
46 memmove(replace_object + pos + 1,
47 replace_object + pos,
48 (replace_object_nr - pos - 1) *
49 sizeof(*replace_object));
50 replace_object[pos] = replace;
51 return 0;
52}
53
54static int register_replace_ref(const char *refname,
55 const unsigned char *sha1,
56 int flag, void *cb_data)
57{
58 /* Get sha1 from refname */
59 const char *slash = strrchr(refname, '/');
60 const char *hash = slash ? slash + 1 : refname;
61 struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj));
62
63 if (strlen(hash) != 40 || get_sha1_hex(hash, repl_obj->sha1[0])) {
64 free(repl_obj);
65 warning("bad replace ref name: %s", refname);
66 return 0;
67 }
68
69 /* Copy sha1 from the read ref */
70 hashcpy(repl_obj->sha1[1], sha1);
71
72 /* Register new object */
73 if (register_replace_object(repl_obj, 1))
74 die("duplicate replace ref: %s", refname);
75
76 return 0;
77}
78
79static void prepare_replace_object(void)
80{
81 static int replace_object_prepared;
82
83 if (replace_object_prepared)
84 return;
85
86 for_each_replace_ref(register_replace_ref, NULL);
87 replace_object_prepared = 1;
e1111cef 88 if (!replace_object_nr)
afc711b8 89 check_replace_refs = 0;
68095570
CC
90}
91
92/* We allow "recursive" replacement. Only within reason, though */
93#define MAXREPLACEDEPTH 5
94
e1111cef 95const unsigned char *do_lookup_replace_object(const unsigned char *sha1)
68095570
CC
96{
97 int pos, depth = MAXREPLACEDEPTH;
98 const unsigned char *cur = sha1;
99
100 prepare_replace_object();
101
102 /* Try to recursively replace the object */
103 do {
104 if (--depth < 0)
105 die("replace depth too high for object %s",
106 sha1_to_hex(sha1));
107
108 pos = replace_object_pos(cur);
109 if (0 <= pos)
110 cur = replace_object[pos]->sha1[1];
111 } while (0 <= pos);
112
113 return cur;
114}