Blame src/annotated_commit.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "annotated_commit.h"
Packit Service 20376f
#include "refs.h"
Packit Service 20376f
#include "cache.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/commit.h"
Packit Service 20376f
#include "git2/refs.h"
Packit Service 20376f
#include "git2/repository.h"
Packit Service 20376f
#include "git2/annotated_commit.h"
Packit Service 20376f
#include "git2/revparse.h"
Packit Service 20376f
#include "git2/tree.h"
Packit Service 20376f
#include "git2/index.h"
Packit Service 20376f
Packit Service 20376f
static int annotated_commit_init(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_commit *commit,
Packit Service 20376f
	const char *description)
Packit Service 20376f
{
Packit Service 20376f
	git_annotated_commit *annotated_commit;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && commit);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
Packit Service 20376f
	GITERR_CHECK_ALLOC(annotated_commit);
Packit Service 20376f
Packit Service 20376f
	annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	git_oid_fmt(annotated_commit->id_str, git_commit_id(commit));
Packit Service 20376f
	annotated_commit->id_str[GIT_OID_HEXSZ] = '\0';
Packit Service 20376f
Packit Service 20376f
	if (!description)
Packit Service 20376f
		description = annotated_commit->id_str;
Packit Service 20376f
Packit Service 20376f
	annotated_commit->description = git__strdup(description);
Packit Service 20376f
	GITERR_CHECK_ALLOC(annotated_commit->description);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (!error)
Packit Service 20376f
		*out = annotated_commit;
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int annotated_commit_init_from_id(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_oid *id,
Packit Service 20376f
	const char *description)
Packit Service 20376f
{
Packit Service 20376f
	git_commit *commit = NULL;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo && id);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_commit_lookup(&commit, repo, id)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	error = annotated_commit_init(out, commit, description);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_commit_free(commit);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_annotated_commit_lookup(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_oid *id)
Packit Service 20376f
{
Packit Service 20376f
	return annotated_commit_init_from_id(out, repo, id, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_annotated_commit_from_commit(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_commit *commit)
Packit Service 20376f
{
Packit Service 20376f
	return annotated_commit_init(out, commit, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_annotated_commit_from_revspec(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *revspec)
Packit Service 20376f
{
Packit Service 20376f
	git_object *obj, *commit;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo && revspec);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) {
Packit Service 20376f
		git_object_free(obj);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = annotated_commit_init(out, (git_commit *)commit, revspec);
Packit Service 20376f
Packit Service 20376f
	git_object_free(obj);
Packit Service 20376f
	git_object_free(commit);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_annotated_commit_from_ref(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const git_reference *ref)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *resolved;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo && ref);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_reference_resolve(&resolved, ref)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
	
Packit Service 20376f
	error = annotated_commit_init_from_id(out,
Packit Service 20376f
		repo,
Packit Service 20376f
		git_reference_target(resolved),
Packit Service 20376f
		git_reference_name(ref));
Packit Service 20376f
Packit Service 20376f
	if (!error) {
Packit Service 20376f
		(*out)->ref_name = git__strdup(git_reference_name(ref));
Packit Service 20376f
		GITERR_CHECK_ALLOC((*out)->ref_name);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_reference_free(resolved);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_annotated_commit_from_head(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_reference *head;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	assert(out && repo);
Packit Service 20376f
Packit Service 20376f
	*out = NULL;
Packit Service 20376f
Packit Service 20376f
    if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	error = git_annotated_commit_from_ref(out, repo, head);
Packit Service 20376f
Packit Service 20376f
	git_reference_free(head);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_annotated_commit_from_fetchhead(
Packit Service 20376f
	git_annotated_commit **out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	const char *branch_name,
Packit Service 20376f
	const char *remote_url,
Packit Service 20376f
	const git_oid *id)
Packit Service 20376f
{
Packit Service 20376f
	assert(repo && id && branch_name && remote_url);
Packit Service 20376f
Packit Service 20376f
	if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	(*out)->ref_name = git__strdup(branch_name);
Packit Service 20376f
	GITERR_CHECK_ALLOC((*out)->ref_name);
Packit Service 20376f
Packit Service 20376f
	(*out)->remote_url = git__strdup(remote_url);
Packit Service 20376f
	GITERR_CHECK_ALLOC((*out)->remote_url);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
const git_oid *git_annotated_commit_id(
Packit Service 20376f
	const git_annotated_commit *annotated_commit)
Packit Service 20376f
{
Packit Service 20376f
	assert(annotated_commit);
Packit Service 20376f
	return git_commit_id(annotated_commit->commit);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_annotated_commit_free(git_annotated_commit *annotated_commit)
Packit Service 20376f
{
Packit Service 20376f
	if (annotated_commit == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	switch (annotated_commit->type) {
Packit Service 20376f
		case GIT_ANNOTATED_COMMIT_REAL:
Packit Service 20376f
			git_commit_free(annotated_commit->commit);
Packit Service 20376f
			git_tree_free(annotated_commit->tree);
Packit Service 20376f
			git__free((char *)annotated_commit->description);
Packit Service 20376f
			git__free((char *)annotated_commit->ref_name);
Packit Service 20376f
			git__free((char *)annotated_commit->remote_url);
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_ANNOTATED_COMMIT_VIRTUAL:
Packit Service 20376f
			git_index_free(annotated_commit->index);
Packit Service 20376f
			git_array_clear(annotated_commit->parents);
Packit Service 20376f
			break;
Packit Service 20376f
		default:
Packit Service 20376f
			abort();
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(annotated_commit);
Packit Service 20376f
}