mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-08 19:51:31 +00:00
refs: Centralize reference creation logic
This commit is contained in:
parent
a2e873d1a1
commit
92f95a170c
51
src/refs.c
51
src/refs.c
@ -335,6 +335,8 @@ static int reference__create(
|
|||||||
git_reference *ref = NULL;
|
git_reference *ref = NULL;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
|
||||||
|
assert(repo && name);
|
||||||
|
|
||||||
if (ref_out)
|
if (ref_out)
|
||||||
*ref_out = NULL;
|
*ref_out = NULL;
|
||||||
|
|
||||||
@ -347,10 +349,29 @@ static int reference__create(
|
|||||||
return error;
|
return error;
|
||||||
|
|
||||||
if (oid != NULL) {
|
if (oid != NULL) {
|
||||||
|
git_odb *odb;
|
||||||
|
|
||||||
assert(symbolic == NULL);
|
assert(symbolic == NULL);
|
||||||
ref = git_reference__alloc(normalized, oid, NULL);
|
|
||||||
|
/* Sanity check the reference being created - target must exist. */
|
||||||
|
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
if (!git_odb_exists(odb, oid)) {
|
||||||
|
giterr_set(GITERR_REFERENCE,
|
||||||
|
"Target OID for the reference doesn't exist on the repository");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref = git_reference__alloc(name, oid, NULL);
|
||||||
} else {
|
} else {
|
||||||
ref = git_reference__alloc_symbolic(normalized, symbolic);
|
char normalized_target[GIT_REFNAME_MAX];
|
||||||
|
|
||||||
|
if ((error = git_reference__normalize_name_lax(
|
||||||
|
normalized_target, sizeof(normalized_target), symbolic)) < 0)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
ref = git_reference__alloc_symbolic(name, normalized_target);
|
||||||
}
|
}
|
||||||
|
|
||||||
GITERR_CHECK_ALLOC(ref);
|
GITERR_CHECK_ALLOC(ref);
|
||||||
@ -375,20 +396,7 @@ int git_reference_create(
|
|||||||
const git_oid *oid,
|
const git_oid *oid,
|
||||||
int force)
|
int force)
|
||||||
{
|
{
|
||||||
git_odb *odb;
|
assert(oid);
|
||||||
int error = 0;
|
|
||||||
|
|
||||||
assert(repo && name && oid);
|
|
||||||
|
|
||||||
/* Sanity check the reference being created - target must exist. */
|
|
||||||
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
|
|
||||||
return error;
|
|
||||||
|
|
||||||
if (!git_odb_exists(odb, oid)) {
|
|
||||||
giterr_set(GITERR_REFERENCE,
|
|
||||||
"Target OID for the reference doesn't exist on the repository");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return reference__create(ref_out, repo, name, oid, NULL, force);
|
return reference__create(ref_out, repo, name, oid, NULL, force);
|
||||||
}
|
}
|
||||||
@ -400,16 +408,9 @@ int git_reference_symbolic_create(
|
|||||||
const char *target,
|
const char *target,
|
||||||
int force)
|
int force)
|
||||||
{
|
{
|
||||||
char normalized[GIT_REFNAME_MAX];
|
assert(target);
|
||||||
int error = 0;
|
|
||||||
|
|
||||||
assert(repo && name && target);
|
return reference__create(ref_out, repo, name, NULL, target, force);
|
||||||
|
|
||||||
if ((error = git_reference__normalize_name_lax(
|
|
||||||
normalized, sizeof(normalized), target)) < 0)
|
|
||||||
return error;
|
|
||||||
|
|
||||||
return reference__create(ref_out, repo, name, NULL, normalized, force);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_reference_set_target(
|
int git_reference_set_target(
|
||||||
|
Loading…
Reference in New Issue
Block a user