mirror of
https://git.proxmox.com/git/libgit2
synced 2025-06-21 16:56:53 +00:00
annotated_commit: provide refs and description
Differentiate between the ref_name used to create an annotated_commit (that can subsequently be used to look up the reference) and the description that we resolved this with (which _cannot_ be looked up). The description is used for things like reflogs (and may be a ref name, and ID something that we revparsed to get here), while the ref name must actually be a reference name, and is used for things like rebase to return to the initial branch.
This commit is contained in:
parent
b3ffd8f638
commit
d55923788c
@ -20,41 +20,104 @@
|
|||||||
|
|
||||||
static int annotated_commit_init(
|
static int annotated_commit_init(
|
||||||
git_annotated_commit **out,
|
git_annotated_commit **out,
|
||||||
git_repository *repo,
|
git_commit *commit,
|
||||||
const git_oid *id,
|
const char *description)
|
||||||
const char *ref_name,
|
|
||||||
const char *remote_url)
|
|
||||||
{
|
{
|
||||||
git_annotated_commit *annotated_commit;
|
git_annotated_commit *annotated_commit;
|
||||||
git_commit *commit = NULL;
|
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
|
||||||
assert(out && id);
|
assert(out && commit);
|
||||||
|
|
||||||
*out = NULL;
|
*out = NULL;
|
||||||
|
|
||||||
if ((error = git_commit_lookup(&commit, repo, id)) < 0 ||
|
annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
|
||||||
(error = git_annotated_commit_from_commit(&annotated_commit,
|
GITERR_CHECK_ALLOC(annotated_commit);
|
||||||
commit)) < 0)
|
|
||||||
|
annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL;
|
||||||
|
|
||||||
|
if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
if (ref_name) {
|
git_oid_fmt(annotated_commit->id_str, git_commit_id(commit));
|
||||||
annotated_commit->ref_name = git__strdup(ref_name);
|
annotated_commit->id_str[GIT_OID_HEXSZ] = '\0';
|
||||||
GITERR_CHECK_ALLOC(annotated_commit->ref_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (remote_url) {
|
if (!description)
|
||||||
annotated_commit->remote_url = git__strdup(remote_url);
|
description = annotated_commit->id_str;
|
||||||
GITERR_CHECK_ALLOC(annotated_commit->remote_url);
|
|
||||||
}
|
|
||||||
|
|
||||||
*out = annotated_commit;
|
annotated_commit->description = git__strdup(description);
|
||||||
|
GITERR_CHECK_ALLOC(annotated_commit->description);
|
||||||
|
|
||||||
|
done:
|
||||||
|
if (!error)
|
||||||
|
*out = annotated_commit;
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int annotated_commit_init_from_id(
|
||||||
|
git_annotated_commit **out,
|
||||||
|
git_repository *repo,
|
||||||
|
const git_oid *id,
|
||||||
|
const char *description)
|
||||||
|
{
|
||||||
|
git_commit *commit = NULL;
|
||||||
|
int error = 0;
|
||||||
|
|
||||||
|
assert(out && repo && id);
|
||||||
|
|
||||||
|
*out = NULL;
|
||||||
|
|
||||||
|
if ((error = git_commit_lookup(&commit, repo, id)) < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
error = annotated_commit_init(out, commit, description);
|
||||||
|
|
||||||
done:
|
done:
|
||||||
git_commit_free(commit);
|
git_commit_free(commit);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int git_annotated_commit_lookup(
|
||||||
|
git_annotated_commit **out,
|
||||||
|
git_repository *repo,
|
||||||
|
const git_oid *id)
|
||||||
|
{
|
||||||
|
return annotated_commit_init_from_id(out, repo, id, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_annotated_commit_from_commit(
|
||||||
|
git_annotated_commit **out,
|
||||||
|
git_commit *commit)
|
||||||
|
{
|
||||||
|
return annotated_commit_init(out, commit, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_annotated_commit_from_revspec(
|
||||||
|
git_annotated_commit **out,
|
||||||
|
git_repository *repo,
|
||||||
|
const char *revspec)
|
||||||
|
{
|
||||||
|
git_object *obj, *commit;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
assert(out && repo && revspec);
|
||||||
|
|
||||||
|
if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) {
|
||||||
|
git_object_free(obj);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
error = annotated_commit_init(out, (git_commit *)commit, revspec);
|
||||||
|
|
||||||
|
git_object_free(obj);
|
||||||
|
git_object_free(commit);
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
int git_annotated_commit_from_ref(
|
int git_annotated_commit_from_ref(
|
||||||
git_annotated_commit **out,
|
git_annotated_commit **out,
|
||||||
git_repository *repo,
|
git_repository *repo,
|
||||||
@ -70,8 +133,15 @@ int git_annotated_commit_from_ref(
|
|||||||
if ((error = git_reference_resolve(&resolved, ref)) < 0)
|
if ((error = git_reference_resolve(&resolved, ref)) < 0)
|
||||||
return error;
|
return error;
|
||||||
|
|
||||||
error = annotated_commit_init(out, repo, git_reference_target(resolved),
|
error = annotated_commit_init_from_id(out,
|
||||||
git_reference_name(ref), NULL);
|
repo,
|
||||||
|
git_reference_target(resolved),
|
||||||
|
git_reference_name(ref));
|
||||||
|
|
||||||
|
if (!error) {
|
||||||
|
(*out)->ref_name = git__strdup(git_reference_name(ref));
|
||||||
|
GITERR_CHECK_ALLOC((*out)->ref_name);
|
||||||
|
}
|
||||||
|
|
||||||
git_reference_free(resolved);
|
git_reference_free(resolved);
|
||||||
return error;
|
return error;
|
||||||
@ -97,41 +167,6 @@ int git_annotated_commit_from_head(
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_annotated_commit_from_commit(
|
|
||||||
git_annotated_commit **out,
|
|
||||||
git_commit *commit)
|
|
||||||
{
|
|
||||||
git_annotated_commit *annotated_commit;
|
|
||||||
|
|
||||||
assert(out && commit);
|
|
||||||
|
|
||||||
*out = NULL;
|
|
||||||
|
|
||||||
annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
|
|
||||||
GITERR_CHECK_ALLOC(annotated_commit);
|
|
||||||
|
|
||||||
annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL;
|
|
||||||
|
|
||||||
git_cached_obj_incref(commit);
|
|
||||||
annotated_commit->commit = commit;
|
|
||||||
|
|
||||||
git_oid_fmt(annotated_commit->id_str, git_commit_id(commit));
|
|
||||||
annotated_commit->id_str[GIT_OID_HEXSZ] = '\0';
|
|
||||||
|
|
||||||
*out = annotated_commit;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int git_annotated_commit_lookup(
|
|
||||||
git_annotated_commit **out,
|
|
||||||
git_repository *repo,
|
|
||||||
const git_oid *id)
|
|
||||||
{
|
|
||||||
assert(out && repo && id);
|
|
||||||
|
|
||||||
return annotated_commit_init(out, repo, id, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int git_annotated_commit_from_fetchhead(
|
int git_annotated_commit_from_fetchhead(
|
||||||
git_annotated_commit **out,
|
git_annotated_commit **out,
|
||||||
git_repository *repo,
|
git_repository *repo,
|
||||||
@ -141,33 +176,16 @@ int git_annotated_commit_from_fetchhead(
|
|||||||
{
|
{
|
||||||
assert(repo && id && branch_name && remote_url);
|
assert(repo && id && branch_name && remote_url);
|
||||||
|
|
||||||
return annotated_commit_init(out, repo, id, branch_name, remote_url);
|
if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0)
|
||||||
}
|
return -1;
|
||||||
|
|
||||||
int git_annotated_commit_from_revspec(
|
(*out)->ref_name = git__strdup(branch_name);
|
||||||
git_annotated_commit **out,
|
GITERR_CHECK_ALLOC((*out)->ref_name);
|
||||||
git_repository *repo,
|
|
||||||
const char *revspec)
|
|
||||||
{
|
|
||||||
git_object *obj, *commit;
|
|
||||||
int error;
|
|
||||||
|
|
||||||
assert(out && repo && revspec);
|
(*out)->remote_url = git__strdup(remote_url);
|
||||||
|
GITERR_CHECK_ALLOC((*out)->remote_url);
|
||||||
|
|
||||||
if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
|
return 0;
|
||||||
return error;
|
|
||||||
|
|
||||||
if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) {
|
|
||||||
git_object_free(obj);
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
error = annotated_commit_init(out, repo, git_object_id(commit), revspec, NULL);
|
|
||||||
|
|
||||||
git_object_free(obj);
|
|
||||||
git_object_free(commit);
|
|
||||||
|
|
||||||
return error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -187,8 +205,9 @@ void git_annotated_commit_free(git_annotated_commit *annotated_commit)
|
|||||||
case GIT_ANNOTATED_COMMIT_REAL:
|
case GIT_ANNOTATED_COMMIT_REAL:
|
||||||
git_commit_free(annotated_commit->commit);
|
git_commit_free(annotated_commit->commit);
|
||||||
git_tree_free(annotated_commit->tree);
|
git_tree_free(annotated_commit->tree);
|
||||||
git__free(annotated_commit->ref_name);
|
git__free((char *)annotated_commit->description);
|
||||||
git__free(annotated_commit->remote_url);
|
git__free((char *)annotated_commit->ref_name);
|
||||||
|
git__free((char *)annotated_commit->remote_url);
|
||||||
break;
|
break;
|
||||||
case GIT_ANNOTATED_COMMIT_VIRTUAL:
|
case GIT_ANNOTATED_COMMIT_VIRTUAL:
|
||||||
git_index_free(annotated_commit->index);
|
git_index_free(annotated_commit->index);
|
||||||
|
@ -33,8 +33,11 @@ struct git_annotated_commit {
|
|||||||
git_index *index;
|
git_index *index;
|
||||||
git_array_oid_t parents;
|
git_array_oid_t parents;
|
||||||
|
|
||||||
char *ref_name;
|
/* how this commit was looked up */
|
||||||
char *remote_url;
|
const char *description;
|
||||||
|
|
||||||
|
const char *ref_name;
|
||||||
|
const char *remote_url;
|
||||||
|
|
||||||
char id_str[GIT_OID_HEXSZ+1];
|
char id_str[GIT_OID_HEXSZ+1];
|
||||||
};
|
};
|
||||||
|
@ -121,7 +121,8 @@ int git_branch_create_from_annotated(
|
|||||||
const git_annotated_commit *commit,
|
const git_annotated_commit *commit,
|
||||||
int force)
|
int force)
|
||||||
{
|
{
|
||||||
return create_branch(ref_out, repository, branch_name, commit->commit, commit->ref_name, force);
|
return create_branch(ref_out,
|
||||||
|
repository, branch_name, commit->commit, commit->description, force);
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_branch_delete(git_reference *branch)
|
int git_branch_delete(git_reference *branch)
|
||||||
|
@ -2162,7 +2162,7 @@ int git_repository_set_head_detached_from_annotated(
|
|||||||
{
|
{
|
||||||
assert(repo && commitish);
|
assert(repo && commitish);
|
||||||
|
|
||||||
return detach(repo, git_annotated_commit_id(commitish), commitish->ref_name);
|
return detach(repo, git_annotated_commit_id(commitish), commitish->description);
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_repository_detach_head(git_repository* repo)
|
int git_repository_detach_head(git_repository* repo)
|
||||||
|
@ -195,5 +195,5 @@ int git_reset_from_annotated(
|
|||||||
git_reset_t reset_type,
|
git_reset_t reset_type,
|
||||||
const git_checkout_options *checkout_opts)
|
const git_checkout_options *checkout_opts)
|
||||||
{
|
{
|
||||||
return reset(repo, (git_object *) commit->commit, commit->ref_name, reset_type, checkout_opts);
|
return reset(repo, (git_object *) commit->commit, commit->description, reset_type, checkout_opts);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user