summaryrefslogtreecommitdiff
path: root/src/libgit2/grafts.c
blob: 1d9373a56f6fd4e21b3778274aaa3c15a0376892 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * 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 "grafts.h"

#include "futils.h"
#include "oid.h"
#include "oidarray.h"
#include "parse.h"

struct git_grafts {
	/* Map of `git_commit_graft`s */
	git_oidmap *commits;

	/* Type of object IDs */
	git_oid_t oid_type;

	/* File backing the graft. NULL if it's an in-memory graft */
	char *path;
	unsigned char path_checksum[GIT_HASH_SHA256_SIZE];
};

int git_grafts_new(git_grafts **out, git_oid_t oid_type)
{
	git_grafts *grafts;

	GIT_ASSERT_ARG(out && oid_type);

	grafts = git__calloc(1, sizeof(*grafts));
	GIT_ERROR_CHECK_ALLOC(grafts);

	if ((git_oidmap_new(&grafts->commits)) < 0) {
		git__free(grafts);
		return -1;
	}

	grafts->oid_type = oid_type;

	*out = grafts;
	return 0;
}

int git_grafts_open(
	git_grafts **out,
	const char *path,
	git_oid_t oid_type)
{
	git_grafts *grafts = NULL;
	int error;

	GIT_ASSERT_ARG(out && path && oid_type);

	if ((error = git_grafts_new(&grafts, oid_type)) < 0)
		goto error;

	grafts->path = git__strdup(path);
	GIT_ERROR_CHECK_ALLOC(grafts->path);

	if ((error = git_grafts_refresh(grafts)) < 0)
		goto error;

	*out = grafts;

error:
	if (error < 0)
		git_grafts_free(grafts);

	return error;
}

int git_grafts_open_or_refresh(
	git_grafts **out,
	const char *path,
	git_oid_t oid_type)
{
	GIT_ASSERT_ARG(out && path && oid_type);

	return *out ? git_grafts_refresh(*out) : git_grafts_open(out, path, oid_type);
}

void git_grafts_free(git_grafts *grafts)
{
	if (!grafts)
		return;
	git__free(grafts->path);
	git_grafts_clear(grafts);
	git_oidmap_free(grafts->commits);
	git__free(grafts);
}

void git_grafts_clear(git_grafts *grafts)
{
	git_commit_graft *graft;

	if (!grafts)
		return;

	git_oidmap_foreach_value(grafts->commits, graft, {
		git__free(graft->parents.ptr);
		git__free(graft);
	});

	git_oidmap_clear(grafts->commits);
}

int git_grafts_refresh(git_grafts *grafts)
{
	git_str contents = GIT_STR_INIT;
	int error, updated = 0;

	GIT_ASSERT_ARG(grafts);

	if (!grafts->path)
		return 0;

	if ((error = git_futils_readbuffer_updated(&contents, grafts->path,
				grafts->path_checksum, &updated)) < 0) {

		if (error == GIT_ENOTFOUND) {
			git_grafts_clear(grafts);
			error = 0;
		}

		goto cleanup;
	}

	if (!updated) {
		goto cleanup;
	}

	if ((error = git_grafts_parse(grafts, contents.ptr, contents.size)) < 0)
		goto cleanup;

cleanup:
	git_str_dispose(&contents);
	return error;
}

int git_grafts_parse(git_grafts *grafts, const char *buf, size_t len)
{
	git_array_oid_t parents = GIT_ARRAY_INIT;
	git_parse_ctx parser;
	int error;

	git_grafts_clear(grafts);

	if ((error = git_parse_ctx_init(&parser, buf, len)) < 0)
		goto error;

	for (; parser.remain_len; git_parse_advance_line(&parser)) {
		git_oid graft_oid;

		if ((error = git_parse_advance_oid(&graft_oid, &parser, grafts->oid_type)) < 0) {
			git_error_set(GIT_ERROR_GRAFTS, "invalid graft OID at line %" PRIuZ, parser.line_num);
			goto error;
		}

		while (parser.line_len && git_parse_advance_expected(&parser, "\n", 1) != 0) {
			git_oid *id = git_array_alloc(parents);
			GIT_ERROR_CHECK_ALLOC(id);

			if ((error = git_parse_advance_expected(&parser, " ", 1)) < 0 ||
			    (error = git_parse_advance_oid(id, &parser, grafts->oid_type)) < 0) {
				git_error_set(GIT_ERROR_GRAFTS, "invalid parent OID at line %" PRIuZ, parser.line_num);
				goto error;
			}
		}

		if ((error = git_grafts_add(grafts, &graft_oid, parents)) < 0)
			goto error;

		git_array_clear(parents);
	}

error:
	git_array_clear(parents);
	return error;
}

int git_grafts_add(git_grafts *grafts, const git_oid *oid, git_array_oid_t parents)
{
	git_commit_graft *graft;
	git_oid *parent_oid;
	int error;
	size_t i;

	GIT_ASSERT_ARG(grafts && oid);

	graft = git__calloc(1, sizeof(*graft));
	GIT_ERROR_CHECK_ALLOC(graft);

	git_array_init_to_size(graft->parents, git_array_size(parents));
	git_array_foreach(parents, i, parent_oid) {
		git_oid *id = git_array_alloc(graft->parents);
		GIT_ERROR_CHECK_ALLOC(id);

		git_oid_cpy(id, parent_oid);
	}
	git_oid_cpy(&graft->oid, oid);

	if ((error = git_grafts_remove(grafts, &graft->oid)) < 0 && error != GIT_ENOTFOUND)
		goto cleanup;

	if ((error = git_oidmap_set(grafts->commits, &graft->oid, graft)) < 0)
		goto cleanup;

	return 0;

cleanup:
	git_array_clear(graft->parents);
	git__free(graft);
	return error;
}

int git_grafts_remove(git_grafts *grafts, const git_oid *oid)
{
	git_commit_graft *graft;
	int error;

	GIT_ASSERT_ARG(grafts && oid);

	if ((graft = git_oidmap_get(grafts->commits, oid)) == NULL)
		return GIT_ENOTFOUND;

	if ((error = git_oidmap_delete(grafts->commits, oid)) < 0)
		return error;

	git__free(graft->parents.ptr);
	git__free(graft);

	return 0;
}

int git_grafts_get(git_commit_graft **out, git_grafts *grafts, const git_oid *oid)
{
	GIT_ASSERT_ARG(out && grafts && oid);
	if ((*out = git_oidmap_get(grafts->commits, oid)) == NULL)
		return GIT_ENOTFOUND;
	return 0;
}

int git_grafts_oids(git_oid **out, size_t *out_len, git_grafts *grafts)
{
	git_array_oid_t array = GIT_ARRAY_INIT;
	const git_oid *oid;
	size_t existing, i = 0;

	GIT_ASSERT_ARG(out && grafts);

	if ((existing = git_oidmap_size(grafts->commits)) > 0)
		git_array_init_to_size(array, existing);

	while (git_oidmap_iterate(NULL, grafts->commits, &i, &oid) == 0) {
		git_oid *cpy = git_array_alloc(array);
		GIT_ERROR_CHECK_ALLOC(cpy);
		git_oid_cpy(cpy, oid);
	}

	*out = array.ptr;
	*out_len = array.size;

	return 0;
}

size_t git_grafts_size(git_grafts *grafts)
{
	return git_oidmap_size(grafts->commits);
}