git/object-file-convert.c
brian m. carlson 23b2c7e95b loose: add a mapping between SHA-1 and SHA-256 for loose objects
As part of the transition plan, we'd like to add a file in the .git
directory that maps loose objects between SHA-1 and SHA-256.  Let's
implement the specification in the transition plan and store this data
on a per-repository basis in struct repository.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2023-10-02 14:57:38 -07:00

70 lines
1.7 KiB
C

#include "git-compat-util.h"
#include "gettext.h"
#include "strbuf.h"
#include "repository.h"
#include "hash-ll.h"
#include "object.h"
#include "loose.h"
#include "object-file-convert.h"
int repo_oid_to_algop(struct repository *repo, const struct object_id *src,
const struct git_hash_algo *to, struct object_id *dest)
{
/*
* If the source algorithm is not set, then we're using the
* default hash algorithm for that object.
*/
const struct git_hash_algo *from =
src->algo ? &hash_algos[src->algo] : repo->hash_algo;
if (from == to) {
if (src != dest)
oidcpy(dest, src);
return 0;
}
if (repo_loose_object_map_oid(repo, src, to, dest)) {
/*
* We may have loaded the object map at repo initialization but
* another process (perhaps upstream of a pipe from us) may have
* written a new object into the map. If the object is missing,
* let's reload the map to see if the object has appeared.
*/
repo_read_loose_object_map(repo);
if (repo_loose_object_map_oid(repo, src, to, dest))
return -1;
}
return 0;
}
int convert_object_file(struct strbuf *outbuf,
const struct git_hash_algo *from,
const struct git_hash_algo *to,
const void *buf, size_t len,
enum object_type type,
int gentle)
{
int ret;
/* Don't call this function when no conversion is necessary */
if ((from == to) || (type == OBJ_BLOB))
BUG("Refusing noop object file conversion");
switch (type) {
case OBJ_COMMIT:
case OBJ_TREE:
case OBJ_TAG:
default:
/* Not implemented yet, so fail. */
ret = -1;
break;
}
if (!ret)
return 0;
if (gentle) {
strbuf_release(outbuf);
return ret;
}
die(_("Failed to convert object from %s to %s"),
from->name, to->name);
}