/src/git/object-file-convert.c
Line | Count | Source (jump to first uncovered line) |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "gettext.h" |
5 | | #include "strbuf.h" |
6 | | #include "hex.h" |
7 | | #include "repository.h" |
8 | | #include "hash.h" |
9 | | #include "hash.h" |
10 | | #include "object.h" |
11 | | #include "loose.h" |
12 | | #include "commit.h" |
13 | | #include "gpg-interface.h" |
14 | | #include "object-file-convert.h" |
15 | | |
16 | | int repo_oid_to_algop(struct repository *repo, const struct object_id *src, |
17 | | const struct git_hash_algo *to, struct object_id *dest) |
18 | 0 | { |
19 | | /* |
20 | | * If the source algorithm is not set, then we're using the |
21 | | * default hash algorithm for that object. |
22 | | */ |
23 | 0 | const struct git_hash_algo *from = |
24 | 0 | src->algo ? &hash_algos[src->algo] : repo->hash_algo; |
25 | |
|
26 | 0 | if (from == to) { |
27 | 0 | if (src != dest) |
28 | 0 | oidcpy(dest, src); |
29 | 0 | return 0; |
30 | 0 | } |
31 | 0 | if (repo_loose_object_map_oid(repo, src, to, dest)) { |
32 | | /* |
33 | | * We may have loaded the object map at repo initialization but |
34 | | * another process (perhaps upstream of a pipe from us) may have |
35 | | * written a new object into the map. If the object is missing, |
36 | | * let's reload the map to see if the object has appeared. |
37 | | */ |
38 | 0 | repo_read_loose_object_map(repo); |
39 | 0 | if (repo_loose_object_map_oid(repo, src, to, dest)) |
40 | 0 | return -1; |
41 | 0 | } |
42 | 0 | return 0; |
43 | 0 | } |
44 | | |
45 | | static int decode_tree_entry_raw(struct object_id *oid, const char **path, |
46 | | size_t *len, const struct git_hash_algo *algo, |
47 | | const char *buf, unsigned long size) |
48 | 0 | { |
49 | 0 | uint16_t mode; |
50 | 0 | const unsigned hashsz = algo->rawsz; |
51 | |
|
52 | 0 | if (size < hashsz + 3 || buf[size - (hashsz + 1)]) { |
53 | 0 | return -1; |
54 | 0 | } |
55 | | |
56 | 0 | *path = parse_mode(buf, &mode); |
57 | 0 | if (!*path || !**path) |
58 | 0 | return -1; |
59 | 0 | *len = strlen(*path) + 1; |
60 | |
|
61 | 0 | oidread(oid, (const unsigned char *)*path + *len, algo); |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | | static int convert_tree_object(struct strbuf *out, |
66 | | const struct git_hash_algo *from, |
67 | | const struct git_hash_algo *to, |
68 | | const char *buffer, size_t size) |
69 | 0 | { |
70 | 0 | const char *p = buffer, *end = buffer + size; |
71 | |
|
72 | 0 | while (p < end) { |
73 | 0 | struct object_id entry_oid, mapped_oid; |
74 | 0 | const char *path = NULL; |
75 | 0 | size_t pathlen; |
76 | |
|
77 | 0 | if (decode_tree_entry_raw(&entry_oid, &path, &pathlen, from, p, |
78 | 0 | end - p)) |
79 | 0 | return error(_("failed to decode tree entry")); |
80 | 0 | if (repo_oid_to_algop(the_repository, &entry_oid, to, &mapped_oid)) |
81 | 0 | return error(_("failed to map tree entry for %s"), oid_to_hex(&entry_oid)); |
82 | 0 | strbuf_add(out, p, path - p); |
83 | 0 | strbuf_add(out, path, pathlen); |
84 | 0 | strbuf_add(out, mapped_oid.hash, to->rawsz); |
85 | 0 | p = path + pathlen + from->rawsz; |
86 | 0 | } |
87 | 0 | return 0; |
88 | 0 | } |
89 | | |
90 | | static int convert_tag_object(struct strbuf *out, |
91 | | const struct git_hash_algo *from, |
92 | | const struct git_hash_algo *to, |
93 | | const char *buffer, size_t size) |
94 | 0 | { |
95 | 0 | struct strbuf payload = STRBUF_INIT, oursig = STRBUF_INIT, othersig = STRBUF_INIT; |
96 | 0 | const int entry_len = from->hexsz + 7; |
97 | 0 | size_t payload_size; |
98 | 0 | struct object_id oid, mapped_oid; |
99 | 0 | const char *p; |
100 | | |
101 | | /* Consume the object line */ |
102 | 0 | if ((entry_len >= size) || |
103 | 0 | memcmp(buffer, "object ", 7) || buffer[entry_len] != '\n') |
104 | 0 | return error("bogus tag object"); |
105 | 0 | if (parse_oid_hex_algop(buffer + 7, &oid, &p, from) < 0) |
106 | 0 | return error("bad tag object ID"); |
107 | 0 | if (repo_oid_to_algop(the_repository, &oid, to, &mapped_oid)) |
108 | 0 | return error("unable to map tree %s in tag object", |
109 | 0 | oid_to_hex(&oid)); |
110 | 0 | size -= ((p + 1) - buffer); |
111 | 0 | buffer = p + 1; |
112 | | |
113 | | /* Is there a signature for our algorithm? */ |
114 | 0 | payload_size = parse_signed_buffer(buffer, size); |
115 | 0 | if (payload_size != size) { |
116 | | /* Yes, there is. */ |
117 | 0 | strbuf_add(&oursig, buffer + payload_size, size - payload_size); |
118 | 0 | } |
119 | | |
120 | | /* Now, is there a signature for the other algorithm? */ |
121 | 0 | parse_buffer_signed_by_header(buffer, payload_size, &payload, &othersig, to); |
122 | | /* |
123 | | * Our payload is now in payload and we may have up to two signatrures |
124 | | * in oursig and othersig. |
125 | | */ |
126 | | |
127 | | /* Add some slop for longer signature header in the new algorithm. */ |
128 | 0 | strbuf_grow(out, (7 + to->hexsz + 1) + size + 7); |
129 | 0 | strbuf_addf(out, "object %s\n", oid_to_hex(&mapped_oid)); |
130 | 0 | strbuf_addbuf(out, &payload); |
131 | 0 | if (oursig.len) |
132 | 0 | add_header_signature(out, &oursig, from); |
133 | 0 | strbuf_addbuf(out, &othersig); |
134 | |
|
135 | 0 | strbuf_release(&payload); |
136 | 0 | strbuf_release(&othersig); |
137 | 0 | strbuf_release(&oursig); |
138 | 0 | return 0; |
139 | 0 | } |
140 | | |
141 | | static int convert_commit_object(struct strbuf *out, |
142 | | const struct git_hash_algo *from, |
143 | | const struct git_hash_algo *to, |
144 | | const char *buffer, size_t size) |
145 | 0 | { |
146 | 0 | const char *tail = buffer; |
147 | 0 | const char *bufptr = buffer; |
148 | 0 | const int tree_entry_len = from->hexsz + 5; |
149 | 0 | const int parent_entry_len = from->hexsz + 7; |
150 | 0 | struct object_id oid, mapped_oid; |
151 | 0 | const char *p, *eol; |
152 | |
|
153 | 0 | tail += size; |
154 | |
|
155 | 0 | while ((bufptr < tail) && (*bufptr != '\n')) { |
156 | 0 | eol = memchr(bufptr, '\n', tail - bufptr); |
157 | 0 | if (!eol) |
158 | 0 | return error(_("bad %s in commit"), "line"); |
159 | | |
160 | 0 | if (((bufptr + 5) < eol) && !memcmp(bufptr, "tree ", 5)) |
161 | 0 | { |
162 | 0 | if (((bufptr + tree_entry_len) != eol) || |
163 | 0 | parse_oid_hex_algop(bufptr + 5, &oid, &p, from) || |
164 | 0 | (p != eol)) |
165 | 0 | return error(_("bad %s in commit"), "tree"); |
166 | | |
167 | 0 | if (repo_oid_to_algop(the_repository, &oid, to, &mapped_oid)) |
168 | 0 | return error(_("unable to map %s %s in commit object"), |
169 | 0 | "tree", oid_to_hex(&oid)); |
170 | 0 | strbuf_addf(out, "tree %s\n", oid_to_hex(&mapped_oid)); |
171 | 0 | } |
172 | 0 | else if (((bufptr + 7) < eol) && !memcmp(bufptr, "parent ", 7)) |
173 | 0 | { |
174 | 0 | if (((bufptr + parent_entry_len) != eol) || |
175 | 0 | parse_oid_hex_algop(bufptr + 7, &oid, &p, from) || |
176 | 0 | (p != eol)) |
177 | 0 | return error(_("bad %s in commit"), "parent"); |
178 | | |
179 | 0 | if (repo_oid_to_algop(the_repository, &oid, to, &mapped_oid)) |
180 | 0 | return error(_("unable to map %s %s in commit object"), |
181 | 0 | "parent", oid_to_hex(&oid)); |
182 | | |
183 | 0 | strbuf_addf(out, "parent %s\n", oid_to_hex(&mapped_oid)); |
184 | 0 | } |
185 | 0 | else if (((bufptr + 9) < eol) && !memcmp(bufptr, "mergetag ", 9)) |
186 | 0 | { |
187 | 0 | struct strbuf tag = STRBUF_INIT, new_tag = STRBUF_INIT; |
188 | | |
189 | | /* Recover the tag object from the mergetag */ |
190 | 0 | strbuf_add(&tag, bufptr + 9, (eol - (bufptr + 9)) + 1); |
191 | |
|
192 | 0 | bufptr = eol + 1; |
193 | 0 | while ((bufptr < tail) && (*bufptr == ' ')) { |
194 | 0 | eol = memchr(bufptr, '\n', tail - bufptr); |
195 | 0 | if (!eol) { |
196 | 0 | strbuf_release(&tag); |
197 | 0 | return error(_("bad %s in commit"), "mergetag continuation"); |
198 | 0 | } |
199 | 0 | strbuf_add(&tag, bufptr + 1, (eol - (bufptr + 1)) + 1); |
200 | 0 | bufptr = eol + 1; |
201 | 0 | } |
202 | | |
203 | | /* Compute the new tag object */ |
204 | 0 | if (convert_tag_object(&new_tag, from, to, tag.buf, tag.len)) { |
205 | 0 | strbuf_release(&tag); |
206 | 0 | strbuf_release(&new_tag); |
207 | 0 | return -1; |
208 | 0 | } |
209 | | |
210 | | /* Write the new mergetag */ |
211 | 0 | strbuf_addstr(out, "mergetag"); |
212 | 0 | strbuf_add_lines(out, " ", new_tag.buf, new_tag.len); |
213 | 0 | strbuf_release(&tag); |
214 | 0 | strbuf_release(&new_tag); |
215 | 0 | } |
216 | 0 | else if (((bufptr + 7) < tail) && !memcmp(bufptr, "author ", 7)) |
217 | 0 | strbuf_add(out, bufptr, (eol - bufptr) + 1); |
218 | 0 | else if (((bufptr + 10) < tail) && !memcmp(bufptr, "committer ", 10)) |
219 | 0 | strbuf_add(out, bufptr, (eol - bufptr) + 1); |
220 | 0 | else if (((bufptr + 9) < tail) && !memcmp(bufptr, "encoding ", 9)) |
221 | 0 | strbuf_add(out, bufptr, (eol - bufptr) + 1); |
222 | 0 | else if (((bufptr + 6) < tail) && !memcmp(bufptr, "gpgsig", 6)) |
223 | 0 | strbuf_add(out, bufptr, (eol - bufptr) + 1); |
224 | 0 | else { |
225 | | /* Unknown line fail it might embed an oid */ |
226 | 0 | return -1; |
227 | 0 | } |
228 | | /* Consume any trailing continuation lines */ |
229 | 0 | bufptr = eol + 1; |
230 | 0 | while ((bufptr < tail) && (*bufptr == ' ')) { |
231 | 0 | eol = memchr(bufptr, '\n', tail - bufptr); |
232 | 0 | if (!eol) |
233 | 0 | return error(_("bad %s in commit"), "continuation"); |
234 | 0 | strbuf_add(out, bufptr, (eol - bufptr) + 1); |
235 | 0 | bufptr = eol + 1; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | if (bufptr < tail) |
239 | 0 | strbuf_add(out, bufptr, tail - bufptr); |
240 | 0 | return 0; |
241 | 0 | } |
242 | | |
243 | | int convert_object_file(struct strbuf *outbuf, |
244 | | const struct git_hash_algo *from, |
245 | | const struct git_hash_algo *to, |
246 | | const void *buf, size_t len, |
247 | | enum object_type type, |
248 | | int gentle) |
249 | 0 | { |
250 | 0 | int ret; |
251 | | |
252 | | /* Don't call this function when no conversion is necessary */ |
253 | 0 | if ((from == to) || (type == OBJ_BLOB)) |
254 | 0 | BUG("Refusing noop object file conversion"); |
255 | | |
256 | 0 | switch (type) { |
257 | 0 | case OBJ_COMMIT: |
258 | 0 | ret = convert_commit_object(outbuf, from, to, buf, len); |
259 | 0 | break; |
260 | 0 | case OBJ_TREE: |
261 | 0 | ret = convert_tree_object(outbuf, from, to, buf, len); |
262 | 0 | break; |
263 | 0 | case OBJ_TAG: |
264 | 0 | ret = convert_tag_object(outbuf, from, to, buf, len); |
265 | 0 | break; |
266 | 0 | default: |
267 | | /* Not implemented yet, so fail. */ |
268 | 0 | ret = -1; |
269 | 0 | break; |
270 | 0 | } |
271 | 0 | if (!ret) |
272 | 0 | return 0; |
273 | 0 | if (gentle) { |
274 | 0 | strbuf_release(outbuf); |
275 | 0 | return ret; |
276 | 0 | } |
277 | 0 | die(_("Failed to convert object from %s to %s"), |
278 | 0 | from->name, to->name); |
279 | 0 | } |