annotated_commit.c 5.05 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "annotated_commit.h"
9

10
#include "refs.h"
11
#include "cache.h"
12 13 14 15 16

#include "git2/commit.h"
#include "git2/refs.h"
#include "git2/repository.h"
#include "git2/annotated_commit.h"
17
#include "git2/revparse.h"
18 19
#include "git2/tree.h"
#include "git2/index.h"
20 21 22

static int annotated_commit_init(
	git_annotated_commit **out,
23 24 25 26 27 28
	git_commit *commit,
	const char *description)
{
	git_annotated_commit *annotated_commit;
	int error = 0;

29 30
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(commit);
31 32 33 34

	*out = NULL;

	annotated_commit = git__calloc(1, sizeof(git_annotated_commit));
35
	GIT_ERROR_CHECK_ALLOC(annotated_commit);
36 37 38 39 40 41 42

	annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL;

	if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0)
		goto done;

	git_oid_fmt(annotated_commit->id_str, git_commit_id(commit));
43
	annotated_commit->id_str[GIT_OID_SHA1_HEXSIZE] = '\0';
44 45 46 47 48

	if (!description)
		description = annotated_commit->id_str;

	annotated_commit->description = git__strdup(description);
49
	GIT_ERROR_CHECK_ALLOC(annotated_commit->description);
50 51 52 53 54 55 56 57 58 59

done:
	if (!error)
		*out = annotated_commit;

	return error;
}

static int annotated_commit_init_from_id(
	git_annotated_commit **out,
60 61
	git_repository *repo,
	const git_oid *id,
62
	const char *description)
63
{
64
	git_commit *commit = NULL;
65 66
	int error = 0;

67 68 69
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(id);
70 71 72

	*out = NULL;

73
	if ((error = git_commit_lookup(&commit, repo, id)) < 0)
74
		goto done;
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	error = annotated_commit_init(out, commit, description);

done:
	git_commit_free(commit);
	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);
}
97

98 99 100 101 102 103 104 105
int git_annotated_commit_from_revspec(
	git_annotated_commit **out,
	git_repository *repo,
	const char *revspec)
{
	git_object *obj, *commit;
	int error;

106 107 108
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(revspec);
109 110 111 112

	if ((error = git_revparse_single(&obj, repo, revspec)) < 0)
		return error;

113
	if ((error = git_object_peel(&commit, obj, GIT_OBJECT_COMMIT))) {
114 115
		git_object_free(obj);
		return error;
116 117
	}

118 119 120 121
	error = annotated_commit_init(out, (git_commit *)commit, revspec);

	git_object_free(obj);
	git_object_free(commit);
122

123 124 125 126 127 128 129 130
	return error;
}

int git_annotated_commit_from_ref(
	git_annotated_commit **out,
	git_repository *repo,
	const git_reference *ref)
{
131
	git_object *peeled;
132 133
	int error = 0;

134 135 136
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(ref);
137 138 139

	*out = NULL;

140
	if ((error = git_reference_peel(&peeled, ref, GIT_OBJECT_COMMIT)) < 0)
141
		return error;
142

143 144
	error = annotated_commit_init_from_id(out,
		repo,
145
		git_object_id(peeled),
146 147 148 149
		git_reference_name(ref));

	if (!error) {
		(*out)->ref_name = git__strdup(git_reference_name(ref));
150
		GIT_ERROR_CHECK_ALLOC((*out)->ref_name);
151
	}
152

153
	git_object_free(peeled);
154 155 156
	return error;
}

157 158 159 160 161 162 163
int git_annotated_commit_from_head(
	git_annotated_commit **out,
	git_repository *repo)
{
	git_reference *head;
	int error;

164 165
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(repo);
166 167 168

	*out = NULL;

169
	if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
170 171 172 173 174 175 176 177
		return -1;

	error = git_annotated_commit_from_ref(out, repo, head);

	git_reference_free(head);
	return error;
}

178 179 180 181 182 183 184
int git_annotated_commit_from_fetchhead(
	git_annotated_commit **out,
	git_repository *repo,
	const char *branch_name,
	const char *remote_url,
	const git_oid *id)
{
185 186 187 188 189
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(branch_name);
	GIT_ASSERT_ARG(remote_url);
	GIT_ASSERT_ARG(id);
190

191 192
	if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0)
		return -1;
193

194
	(*out)->ref_name = git__strdup(branch_name);
195
	GIT_ERROR_CHECK_ALLOC((*out)->ref_name);
196

197
	(*out)->remote_url = git__strdup(remote_url);
198
	GIT_ERROR_CHECK_ALLOC((*out)->remote_url);
199

200
	return 0;
201 202 203
}


204 205 206
const git_oid *git_annotated_commit_id(
	const git_annotated_commit *annotated_commit)
{
207
	GIT_ASSERT_ARG_WITH_RETVAL(annotated_commit, NULL);
208 209 210
	return git_commit_id(annotated_commit->commit);
}

211 212 213
const char *git_annotated_commit_ref(
	const git_annotated_commit *annotated_commit)
{
214
	GIT_ASSERT_ARG_WITH_RETVAL(annotated_commit, NULL);
215 216 217
	return annotated_commit->ref_name;
}

218 219 220 221 222
void git_annotated_commit_free(git_annotated_commit *annotated_commit)
{
	if (annotated_commit == NULL)
		return;

223 224 225 226
	switch (annotated_commit->type) {
		case GIT_ANNOTATED_COMMIT_REAL:
			git_commit_free(annotated_commit->commit);
			git_tree_free(annotated_commit->tree);
227 228 229
			git__free((char *)annotated_commit->description);
			git__free((char *)annotated_commit->ref_name);
			git__free((char *)annotated_commit->remote_url);
230 231 232 233 234 235 236 237
			break;
		case GIT_ANNOTATED_COMMIT_VIRTUAL:
			git_index_free(annotated_commit->index);
			git_array_clear(annotated_commit->parents);
			break;
		default:
			abort();
	}
238 239 240

	git__free(annotated_commit);
}