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