File: annotated_commit.c

package info (click to toggle)
libgit2 0.25.1%2Breally0.24.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,512 kB
  • ctags: 15,888
  • sloc: ansic: 153,520; sh: 297; python: 175; makefile: 70; php: 65
file content (202 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * 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 "common.h"
#include "annotated_commit.h"
#include "refs.h"
#include "cache.h"

#include "git2/commit.h"
#include "git2/refs.h"
#include "git2/repository.h"
#include "git2/annotated_commit.h"
#include "git2/revparse.h"
#include "git2/tree.h"
#include "git2/index.h"

static int annotated_commit_init(
	git_annotated_commit **out,
	git_repository *repo,
	const git_oid *id,
	const char *ref_name,
	const char *remote_url)
{
	git_annotated_commit *annotated_commit;
	git_commit *commit = NULL;
	int error = 0;

	assert(out && id);

	*out = NULL;

	if ((error = git_commit_lookup(&commit, repo, id)) < 0 ||
		(error = git_annotated_commit_from_commit(&annotated_commit,
			commit)) < 0)
		goto done;

	if (ref_name) {
		annotated_commit->ref_name = git__strdup(ref_name);
		GITERR_CHECK_ALLOC(annotated_commit->ref_name);
	}

	if (remote_url) {
		annotated_commit->remote_url = git__strdup(remote_url);
		GITERR_CHECK_ALLOC(annotated_commit->remote_url);
	}

	*out = annotated_commit;

done:
	git_commit_free(commit);
	return error;
}

int git_annotated_commit_from_ref(
	git_annotated_commit **out,
	git_repository *repo,
	const git_reference *ref)
{
	git_reference *resolved;
	int error = 0;

	assert(out && repo && ref);

	*out = NULL;

	if ((error = git_reference_resolve(&resolved, ref)) < 0)
		return error;
	
	error = annotated_commit_init(out, repo, git_reference_target(resolved),
		git_reference_name(ref), NULL);

	git_reference_free(resolved);
	return error;
}

int git_annotated_commit_from_head(
	git_annotated_commit **out,
	git_repository *repo)
{
	git_reference *head;
	int error;

	assert(out && repo);

	*out = NULL;

    if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
		return -1;

	error = git_annotated_commit_from_ref(out, repo, head);

	git_reference_free(head);
	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(
	git_annotated_commit **out,
	git_repository *repo,
	const char *branch_name,
	const char *remote_url,
	const git_oid *id)
{
	assert(repo && id && branch_name && remote_url);

	return annotated_commit_init(out, repo, id, branch_name, remote_url);
}

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, repo, git_object_id(commit), revspec, NULL);

	git_object_free(obj);
	git_object_free(commit);

	return error;
}


const git_oid *git_annotated_commit_id(
	const git_annotated_commit *annotated_commit)
{
	assert(annotated_commit);
	return git_commit_id(annotated_commit->commit);
}

void git_annotated_commit_free(git_annotated_commit *annotated_commit)
{
	if (annotated_commit == NULL)
		return;

	switch (annotated_commit->type) {
		case GIT_ANNOTATED_COMMIT_REAL:
			git_commit_free(annotated_commit->commit);
			git_tree_free(annotated_commit->tree);
			git__free(annotated_commit->ref_name);
			git__free(annotated_commit->remote_url);
			break;
		case GIT_ANNOTATED_COMMIT_VIRTUAL:
			git_index_free(annotated_commit->index);
			git_array_clear(annotated_commit->parents);
			break;
		default:
			abort();
	}

	git__free(annotated_commit);
}