git/object-file-convert.c
Eric W. Biederman 5e9d802a33 object-file-convert: stubs for converting from one object format to another
Two basic functions are provided:
- convert_object_file Takes an object file it's type and hash algorithm
  and converts it into the equivalent object file that would
  have been generated with hash algorithm "to".

  For blob objects there is no conversation to be done and it is an
  error to use this function on them.

  For commit, tree, and tag objects embedded oids are replaced by the
  oids of the objects they refer to with those objects and their
  object ids reencoded in with the hash algorithm "to".  Signatures
  are rearranged so that they remain valid after the object has
  been reencoded.

- repo_oid_to_algop which takes an oid that refers to an object file
  and returns the oid of the equivalent object file generated
  with the target hash algorithm.

The pair of files object-file-convert.c and object-file-convert.h are
introduced to hold as much of this logic as possible to keep this
conversion logic cleanly separated from everything else and in the
hopes that someday the code will be clean enough git can support
compiling out support for sha1 and the various conversion functions.

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

57 lines
1.2 KiB
C

#include "git-compat-util.h"
#include "gettext.h"
#include "strbuf.h"
#include "repository.h"
#include "hash-ll.h"
#include "object.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;
}
return -1;
}
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);
}