Skip to content

Commit c8762c3

Browse files
bk2204gitster
authored andcommitted
object-file-convert: convert tag objects when writing
When writing a tag object in a repository with both SHA-1 and SHA-256, we'll need to convert our commit objects so that we can write the hash values for both into the repository. To do so, let's add a function to convert tag objects. Note that signatures for tag objects in the current algorithm trail the message, and those for the alternate algorithm are in headers. Therefore, we parse the tag object for both a trailing signature and a header and then, when writing the other format, swap the two around. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33a14e8 commit c8762c3

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

object-file-convert.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "hash.h"
88
#include "object.h"
99
#include "loose.h"
10+
#include "commit.h"
11+
#include "gpg-interface.h"
1012
#include "object-file-convert.h"
1113

1214
int repo_oid_to_algop(struct repository *repo, const struct object_id *src,
@@ -83,6 +85,52 @@ static int convert_tree_object(struct strbuf *out,
8385
return 0;
8486
}
8587

88+
static int convert_tag_object(struct strbuf *out,
89+
const struct git_hash_algo *from,
90+
const struct git_hash_algo *to,
91+
const char *buffer, size_t size)
92+
{
93+
struct strbuf payload = STRBUF_INIT, temp = STRBUF_INIT, oursig = STRBUF_INIT, othersig = STRBUF_INIT;
94+
size_t payload_size;
95+
struct object_id oid, mapped_oid;
96+
const char *p;
97+
98+
/* Add some slop for longer signature header in the new algorithm. */
99+
strbuf_grow(out, size + 7);
100+
101+
/* Is there a signature for our algorithm? */
102+
payload_size = parse_signed_buffer(buffer, size);
103+
strbuf_add(&payload, buffer, payload_size);
104+
if (payload_size != size) {
105+
/* Yes, there is. */
106+
strbuf_add(&oursig, buffer + payload_size, size - payload_size);
107+
}
108+
/* Now, is there a signature for the other algorithm? */
109+
if (parse_buffer_signed_by_header(payload.buf, payload.len, &temp, &othersig, to)) {
110+
/* Yes, there is. */
111+
strbuf_swap(&payload, &temp);
112+
strbuf_release(&temp);
113+
}
114+
115+
/*
116+
* Our payload is now in payload and we may have up to two signatrures
117+
* in oursig and othersig.
118+
*/
119+
if (strncmp(payload.buf, "object ", 7) || payload.buf[from->hexsz + 7] != '\n')
120+
return error("bogus tag object");
121+
if (parse_oid_hex_algop(payload.buf + 7, &oid, &p, from) < 0)
122+
return error("bad tag object ID");
123+
if (repo_oid_to_algop(the_repository, &oid, to, &mapped_oid))
124+
return error("unable to map tree %s in tag object",
125+
oid_to_hex(&oid));
126+
strbuf_addf(out, "object %s", oid_to_hex(&mapped_oid));
127+
strbuf_add(out, p, payload.len - (p - payload.buf));
128+
strbuf_addbuf(out, &othersig);
129+
if (oursig.len)
130+
add_header_signature(out, &oursig, from);
131+
return 0;
132+
}
133+
86134
int convert_object_file(struct strbuf *outbuf,
87135
const struct git_hash_algo *from,
88136
const struct git_hash_algo *to,
@@ -100,8 +148,10 @@ int convert_object_file(struct strbuf *outbuf,
100148
case OBJ_TREE:
101149
ret = convert_tree_object(outbuf, from, to, buf, len);
102150
break;
103-
case OBJ_COMMIT:
104151
case OBJ_TAG:
152+
ret = convert_tag_object(outbuf, from, to, buf, len);
153+
break;
154+
case OBJ_COMMIT:
105155
default:
106156
/* Not implemented yet, so fail. */
107157
ret = -1;

0 commit comments

Comments
 (0)