Commit b41713206b83bdf88522ae789b56630bb6e0a4bc

Carlos Martín Nieto 2011-07-26T11:34:54

Move the tree cache functions to their own file Rename git_index_tree to git_tree_cache. Signed-off-by: Carlos Martín Nieto <cmn@elego.de>

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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
diff --git a/src/index.c b/src/index.c
index f6f282d..905e9c5 100644
--- a/src/index.c
+++ b/src/index.c
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "repository.h"
 #include "index.h"
+#include "tree-cache.h"
 #include "hash.h"
 #include "git2/odb.h"
 #include "git2/blob.h"
@@ -80,9 +81,6 @@ static size_t read_extension(git_index *index, const char *buffer, size_t buffer
 static size_t read_entry(git_index_entry *dest, const void *buffer, size_t buffer_size);
 static int read_header(struct index_header *dest, const void *buffer);
 
-static int read_tree(git_index *index, const char *buffer, size_t buffer_size);
-static int read_tree_internal(git_index_tree **, const char **, const char *, git_index_tree *);
-
 static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
 static int is_index_extended(git_index *index);
 static int write_index(git_index *index, git_filebuf *file);
@@ -185,21 +183,6 @@ void git_index_free(git_index *index)
 	free(index);
 }
 
-static void free_tree(git_index_tree *tree)
-{
-	unsigned int i;
-
-	if (tree == NULL)
-		return;
-
-	for (i = 0; i < tree->children_count; ++i)
-		free_tree(tree->children[i]);
-
-	free(tree->name);
-	free(tree->children);
-	free(tree);
-}
-
 void git_index_clear(git_index *index)
 {
 	unsigned int i;
@@ -224,7 +207,7 @@ void git_index_clear(git_index *index)
 	git_vector_clear(&index->unmerged);
 	index->last_modified = 0;
 
-	free_tree(index->tree);
+	git_tree_cache_free(index->tree);
 	index->tree = NULL;
 }
 
@@ -537,125 +520,6 @@ const git_index_entry_unmerged *git_index_get_unmerged_byindex(git_index *index,
 	return git_vector_get(&index->unmerged, n);
 }
 
-
-static int read_tree_internal(git_index_tree **out,
-		const char **buffer_in, const char *buffer_end, git_index_tree *parent)
-{
-	git_index_tree *tree;
-	const char *name_start, *buffer;
-	int count;
-	int error = GIT_SUCCESS;
-
-	if ((tree = git__malloc(sizeof(git_index_tree))) == NULL)
-		return GIT_ENOMEM;
-
-	memset(tree, 0x0, sizeof(git_index_tree));
-	tree->parent = parent;
-
-	buffer = name_start = *buffer_in;
-
-	if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) {
-		error = GIT_EOBJCORRUPTED;
-		goto cleanup;
-	}
-
-	/* NUL-terminated tree name */
-	tree->name = git__strdup(name_start);
-	if (tree->name == NULL) {
-		error = GIT_ENOMEM;
-		goto cleanup;
-	}
-
-	if (++buffer >= buffer_end) {
-		error = GIT_EOBJCORRUPTED;
-		goto cleanup;
-	}
-
-	/* Blank-terminated ASCII decimal number of entries in this tree */
-	if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) {
-		error = GIT_EOBJCORRUPTED;
-		goto cleanup;
-	}
-
-	/* Invalidated TREE. Free the tree but report success */
-	if (count == -1) {
-		/* FIXME: return buffer_end or the end position for
-		 * this single tree entry */
-		*buffer_in = buffer_end;
-		*out = NULL;
-		free_tree(tree); /* Needs to be done manually */
-		return GIT_SUCCESS;
-	}
-
-	tree->entries = count;
-
-	if (*buffer != ' ' || ++buffer >= buffer_end) {
-		error = GIT_EOBJCORRUPTED;
-		goto cleanup;
-	}
-
-	 /* Number of children of the tree, newline-terminated */
-	if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS ||
-		count < 0) {
-		error = GIT_EOBJCORRUPTED;
-		goto cleanup;
-	}
-
-	tree->children_count = count;
-
-	if (*buffer != '\n' || ++buffer >= buffer_end) {
-		error = GIT_EOBJCORRUPTED;
-		goto cleanup;
-	}
-
-	/* 160-bit SHA-1 for this tree and it's children */
-	if (buffer + GIT_OID_RAWSZ > buffer_end) {
-		error = GIT_EOBJCORRUPTED;
-		goto cleanup;
-	}
-
-	git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
-	buffer += GIT_OID_RAWSZ;
-
-	/* Parse children: */
-	if (tree->children_count > 0) {
-		unsigned int i;
-		int err;
-
-		tree->children = git__malloc(tree->children_count * sizeof(git_index_tree *));
-		if (tree->children == NULL)
-			goto cleanup;
-
-		for (i = 0; i < tree->children_count; ++i) {
-			err = read_tree_internal(&tree->children[i], &buffer, buffer_end, tree);
-
-			if (err < GIT_SUCCESS)
-				goto cleanup;
-		}
-	}
-
-	*buffer_in = buffer;
-	*out = tree;
-	return GIT_SUCCESS;
-
- cleanup:
-	free_tree(tree);
-	return error;
-}
-
-static int read_tree(git_index *index, const char *buffer, size_t buffer_size)
-{
-	const char *buffer_end = buffer + buffer_size;
-	int error;
-
-	error = read_tree_internal(&index->tree, &buffer, buffer_end, NULL);
-
-	if (buffer < buffer_end)
-		return GIT_EOBJCORRUPTED;
-
-	return error;
-}
-
 static int read_unmerged(git_index *index, const char *buffer, size_t size)
 {
 	const char *endptr;
@@ -814,7 +678,7 @@ static size_t read_extension(git_index *index, const char *buffer, size_t buffer
 	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
 		/* tree cache */
 		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
-			if (read_tree(index, buffer + 8, dest.extension_size) < GIT_SUCCESS)
+			if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size) < GIT_SUCCESS)
 				return 0;
 		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
 			if (read_unmerged(index, buffer + 8, dest.extension_size) < GIT_SUCCESS)
diff --git a/src/index.h b/src/index.h
index 76633a9..e912770 100644
--- a/src/index.h
+++ b/src/index.h
@@ -10,22 +10,10 @@
 #include "fileops.h"
 #include "filebuf.h"
 #include "vector.h"
+#include "tree-cache.h"
 #include "git2/odb.h"
 #include "git2/index.h"
 
-struct git_index_tree {
-	char *name;
-
-	struct git_index_tree *parent;
-	struct git_index_tree **children;
-	size_t children_count;
-
-	size_t entries;
-	git_oid oid;
-};
-
-typedef struct git_index_tree git_index_tree;
-
 struct git_index {
 	git_repository *repository;
 	char *index_file_path;
@@ -34,7 +22,7 @@ struct git_index {
 	git_vector entries;
 
 	unsigned int on_disk:1;
-	git_index_tree *tree;
+	git_tree_cache *tree;
 
 	git_vector unmerged;
 };
diff --git a/src/tree-cache.c b/src/tree-cache.c
new file mode 100644
index 0000000..b3e8a58
--- /dev/null
+++ b/src/tree-cache.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2009-2011 the libgit2 contributors
+ *
+ * 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 "tree-cache.h"
+
+static int read_tree_internal(git_tree_cache **out,
+		const char **buffer_in, const char *buffer_end, git_tree_cache *parent)
+{
+	git_tree_cache *tree;
+	const char *name_start, *buffer;
+	int count;
+	int error = GIT_SUCCESS;
+
+	if ((tree = git__malloc(sizeof(git_tree_cache))) == NULL)
+		return GIT_ENOMEM;
+
+	memset(tree, 0x0, sizeof(git_tree_cache));
+	tree->parent = parent;
+
+	buffer = name_start = *buffer_in;
+
+	if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) {
+		error = GIT_EOBJCORRUPTED;
+		goto cleanup;
+	}
+
+	/* NUL-terminated tree name */
+	tree->name = git__strdup(name_start);
+	if (tree->name == NULL) {
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+
+	if (++buffer >= buffer_end) {
+		error = GIT_EOBJCORRUPTED;
+		goto cleanup;
+	}
+
+	/* Blank-terminated ASCII decimal number of entries in this tree */
+	if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) {
+		error = GIT_EOBJCORRUPTED;
+		goto cleanup;
+	}
+
+	/* Invalidated TREE. Free the tree but report success */
+	if (count == -1) {
+		/* FIXME: return buffer_end or the end position for
+		 * this single tree entry */
+		*buffer_in = buffer_end;
+		*out = NULL;
+		git_tree_cache_free(tree); /* Needs to be done manually */
+		return GIT_SUCCESS;
+	}
+
+	tree->entries = count;
+
+	if (*buffer != ' ' || ++buffer >= buffer_end) {
+		error = GIT_EOBJCORRUPTED;
+		goto cleanup;
+	}
+
+	 /* Number of children of the tree, newline-terminated */
+	if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS ||
+		count < 0) {
+		error = GIT_EOBJCORRUPTED;
+		goto cleanup;
+	}
+
+	tree->children_count = count;
+
+	if (*buffer != '\n' || ++buffer >= buffer_end) {
+		error = GIT_EOBJCORRUPTED;
+		goto cleanup;
+	}
+
+	/* 160-bit SHA-1 for this tree and it's children */
+	if (buffer + GIT_OID_RAWSZ > buffer_end) {
+		error = GIT_EOBJCORRUPTED;
+		goto cleanup;
+	}
+
+	git_oid_fromraw(&tree->oid, (const unsigned char *)buffer);
+	buffer += GIT_OID_RAWSZ;
+
+	/* Parse children: */
+	if (tree->children_count > 0) {
+		unsigned int i;
+		int err;
+
+		tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *));
+		if (tree->children == NULL)
+			goto cleanup;
+
+		for (i = 0; i < tree->children_count; ++i) {
+			err = read_tree_internal(&tree->children[i], &buffer, buffer_end, tree);
+
+			if (err < GIT_SUCCESS)
+				goto cleanup;
+		}
+	}
+
+	*buffer_in = buffer;
+	*out = tree;
+	return GIT_SUCCESS;
+
+ cleanup:
+	git_tree_cache_free(tree);
+	return error;
+}
+
+int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size)
+{
+	const char *buffer_end = buffer + buffer_size;
+	int error;
+
+	error = read_tree_internal(tree, &buffer, buffer_end, NULL);
+
+	if (buffer < buffer_end)
+		return GIT_EOBJCORRUPTED;
+
+	return error;
+}
+
+void git_tree_cache_free(git_tree_cache *tree)
+{
+	unsigned int i;
+
+	if (tree == NULL)
+		return;
+
+	for (i = 0; i < tree->children_count; ++i)
+		git_tree_cache_free(tree->children[i]);
+
+	free(tree->name);
+	free(tree->children);
+	free(tree);
+}
diff --git a/src/tree-cache.h b/src/tree-cache.h
new file mode 100644
index 0000000..a9e6d2d
--- /dev/null
+++ b/src/tree-cache.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2009-2011 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_tree_cache_h__
+#define INCLUDE_tree_cache_h__
+
+#include "common.h"
+#include "git2/oid.h"
+
+struct git_tree_cache {
+	char *name;
+
+	struct git_tree_cache *parent;
+	struct git_tree_cache **children;
+	size_t children_count;
+
+	ssize_t entries;
+	git_oid oid;
+};
+
+typedef struct git_tree_cache git_tree_cache;
+
+int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size);
+void git_tree_cache_free(git_tree_cache *tree);
+
+#endif