Commit 4d3a7b7846bd2091c37f82ed78ccafb749ee8492

Russell Belfer 2012-07-10T21:45:16

Merge pull request #793 from libgit2/tree-entry-by-path Bring back `entry_bypath`

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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 8f62e75..f12b15e 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -46,7 +46,11 @@ GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git
  * @param len the length of the short identifier
  * @return 0 or an error code
  */
-GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, const git_oid *id, unsigned int len)
+GIT_INLINE(int) git_tree_lookup_prefix(
+	git_tree **tree,
+	git_repository *repo,
+	const git_oid *id,
+	unsigned int len)
 {
 	return git_object_lookup_prefix((git_object **)tree, repo, id, len, GIT_OBJ_TREE);
 }
@@ -62,12 +66,33 @@ GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, co
  *
  * @param tree the tree to close
  */
-
 GIT_INLINE(void) git_tree_free(git_tree *tree)
 {
 	git_object_free((git_object *) tree);
 }
 
+/**
+ * Free a tree entry
+ *
+ * IMPORTANT: This function is only needed for tree
+ * entries owned by the user, such as the ones returned
+ * by `git_tree_entry_dup`.
+ *
+ * @param entry The entry to free
+ */
+GIT_EXTERN(void) git_tree_entry_free(git_tree_entry *entry);
+
+/**
+ * Duplicate a tree entry
+ *
+ * Create a copy of a tree entry. The returned copy is owned
+ * by the user, and must be freed manually with
+ * `git_tree_entry_free`.
+ *
+ * @param entry A tree entry to duplicate
+ * @return a copy of the original entry
+ */
+GIT_EXTERN(git_tree_entry *) git_tree_entry_dup(const git_tree_entry *entry);
 
 /**
  * Get the id of a tree.
@@ -143,7 +168,10 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry);
  * @param entry a tree entry
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_tree_entry_to_object(git_object **object_out, git_repository *repo, const git_tree_entry *entry);
+GIT_EXTERN(int) git_tree_entry_to_object(
+	git_object **object_out,
+	git_repository *repo,
+	const git_tree_entry *entry);
 
 /**
  * Write a tree to the ODB from the index file
@@ -231,7 +259,12 @@ GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, con
  * @param attributes Folder attributes of the entry
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes);
+GIT_EXTERN(int) git_treebuilder_insert(
+	const git_tree_entry **entry_out,
+	git_treebuilder *bld,
+	const char *filename,
+	const git_oid *id,
+	unsigned int attributes);
 
 /**
  * Remove an entry from the builder by its filename
@@ -252,7 +285,10 @@ GIT_EXTERN(int) git_treebuilder_remove(git_treebuilder *bld, const char *filenam
  * @param bld Tree builder
  * @param filter Callback to filter entries
  */
-GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(const git_tree_entry *, void *), void *payload);
+GIT_EXTERN(void) git_treebuilder_filter(
+	git_treebuilder *bld,
+	int (*filter)(const git_tree_entry *, void *),
+	void *payload);
 
 /**
  * Write the contents of the tree builder as a tree object
@@ -269,21 +305,24 @@ GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(cons
 GIT_EXTERN(int) git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld);
 
 /**
- * Retrieve a subtree contained in a tree, given its
- * relative path.
+ * Retrieve a tree entry contained in a tree or in any
+ * of its subtrees, given its relative path.
  *
- * The returned tree is owned by the repository and
- * should be closed with the `git_object_free` method.
+ * The returned tree entry is owned by the user and must
+ * be freed manually with `git_tree_entry_free`.
  *
- * @param subtree Pointer where to store the subtree
+ * @param entry Pointer where to store the tree entry
  * @param root A previously loaded tree which will be the root of the relative path
- * @param subtree_path Path to the contained subtree
- * @return 0 on success; GIT_ENOTFOUND if the path does not lead to a subtree
+ * @param subtree_path Path to the contained entry
+ * @return 0 on success; GIT_ENOTFOUND if the path does not exist
  */
-GIT_EXTERN(int) git_tree_get_subtree(git_tree **subtree, git_tree *root, const char *subtree_path);
+GIT_EXTERN(int) git_tree_entry_bypath(
+	git_tree_entry **entry,
+	git_tree *root,
+	const char *path);
 
 /** Callback for the tree traversal method */
-typedef int (*git_treewalk_cb)(const char *root, git_tree_entry *entry, void *payload);
+typedef int (*git_treewalk_cb)(const char *root, const git_tree_entry *entry, void *payload);
 
 /** Tree traversal modes */
 enum git_treewalk_mode {
diff --git a/src/index.c b/src/index.c
index 3fedcd2..89d4798 100644
--- a/src/index.c
+++ b/src/index.c
@@ -985,7 +985,7 @@ int git_index_entry_stage(const git_index_entry *entry)
 	return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT;
 }
 
-static int read_tree_cb(const char *root, git_tree_entry *tentry, void *data)
+static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *data)
 {
 	git_index *index = data;
 	git_index_entry *entry = NULL;
diff --git a/src/notes.c b/src/notes.c
index 0dfd3f8..7813e99 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -97,7 +97,7 @@ static int tree_write(
 {
 	int error;
 	git_treebuilder *tb = NULL;
-	git_tree_entry *entry;
+	const git_tree_entry *entry;
 	git_oid tree_oid;
 
 	if ((error = git_treebuilder_create(&tb, source_tree)) < 0)
diff --git a/src/tree.c b/src/tree.c
index 9bdc218..b609eea 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -35,6 +35,22 @@ static int entry_sort_cmp(const void *a, const void *b)
 		entry_b->filename, entry_b->filename_len, git_tree_entry__is_tree(entry_b));
 }
 
+static git_tree_entry *alloc_entry(const char *filename)
+{
+	git_tree_entry *entry = NULL;
+	size_t filename_len = strlen(filename);
+
+	entry = git__malloc(sizeof(git_tree_entry) + filename_len + 1);
+	if (!entry)
+		return NULL;
+
+	memset(entry, 0x0, sizeof(git_tree_entry));
+	memcpy(entry->filename, filename, filename_len);
+	entry->filename[filename_len] = 0;
+	entry->filename_len = filename_len;
+
+	return entry;
+}
 
 struct tree_key_search {
 	const char *filename;
@@ -76,7 +92,7 @@ static int homing_search_cmp(const void *key, const void *array_member)
  * ambiguous because of folder vs file sorting, we look linearly
  * around the area for our target file.
  */
-static int tree_key_search(git_vector *entries, const char *filename)
+static int tree_key_search(git_vector *entries, const char *filename, size_t filename_len)
 {
 	struct tree_key_search ksearch;
 	const git_tree_entry *entry;
@@ -84,7 +100,7 @@ static int tree_key_search(git_vector *entries, const char *filename)
 	int homing, i;
 
 	ksearch.filename = filename;
-	ksearch.filename_len = strlen(filename);
+	ksearch.filename_len = filename_len;
 
 	/* Initial homing search; find an entry on the tree with
 	 * the same prefix as the filename we're looking for */
@@ -100,7 +116,8 @@ static int tree_key_search(git_vector *entries, const char *filename)
 		if (homing_search_cmp(&ksearch, entry) < 0)
 			break;
 
-		if (strcmp(filename, entry->filename) == 0)
+		if (entry->filename_len == filename_len &&
+			memcmp(filename, entry->filename, filename_len) == 0)
 			return i;
 	}
 
@@ -112,7 +129,8 @@ static int tree_key_search(git_vector *entries, const char *filename)
 		if (homing_search_cmp(&ksearch, entry) > 0)
 			break;
 
-		if (strcmp(filename, entry->filename) == 0)
+		if (entry->filename_len == filename_len &&
+			memcmp(filename, entry->filename, filename_len) == 0)
 			return i;
 	}
 
@@ -120,16 +138,35 @@ static int tree_key_search(git_vector *entries, const char *filename)
 	return GIT_ENOTFOUND;
 }
 
+void git_tree_entry_free(git_tree_entry *entry)
+{
+	git__free(entry);
+}
+
+git_tree_entry *git_tree_entry_dup(const git_tree_entry *entry)
+{
+	size_t total_size;
+	git_tree_entry *copy;
+
+	assert(entry);
+
+	total_size = sizeof(git_tree_entry) + entry->filename_len + 1;
+
+	copy = git__malloc(total_size);
+	if (!copy)
+		return NULL;
+
+	memcpy(copy, entry, total_size);
+	return copy;
+}
+
 void git_tree__free(git_tree *tree)
 {
 	unsigned int i;
 
 	for (i = 0; i < tree->entries.length; ++i) {
-		git_tree_entry *e;
-		e = git_vector_get(&tree->entries, i);
-
-		git__free(e->filename);
-		git__free(e);
+		git_tree_entry *e = git_vector_get(&tree->entries, i);
+		git_tree_entry_free(e);
 	}
 
 	git_vector_free(&tree->entries);
@@ -179,19 +216,21 @@ int git_tree_entry_to_object(
 	return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY);
 }
 
-const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
+static git_tree_entry *entry_fromname(git_tree *tree, const char *name, size_t name_len)
 {
-	int idx;
-
-	assert(tree && filename);
-
-	idx = tree_key_search(&tree->entries, filename);
-	if (idx == GIT_ENOTFOUND)
+	int idx = tree_key_search(&tree->entries, name, name_len);
+	if (idx < 0)
 		return NULL;
 
 	return git_vector_get(&tree->entries, idx);
 }
 
+const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
+{
+	assert(tree && filename);
+	return entry_fromname(tree, filename, strlen(filename));
+}
+
 const git_tree_entry *git_tree_entry_byindex(git_tree *tree, unsigned int idx)
 {
 	assert(tree);
@@ -244,28 +283,28 @@ static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buf
 
 	while (buffer < buffer_end) {
 		git_tree_entry *entry;
-		int tmp;
+		int attr;
 
-		entry = git__calloc(1, sizeof(git_tree_entry));
-		GITERR_CHECK_ALLOC(entry);
-
-		if (git_vector_insert(&tree->entries, entry) < 0)
-			return -1;
-
-		if (git__strtol32(&tmp, buffer, &buffer, 8) < 0 ||
-			!buffer || !valid_attributes(tmp))
+		if (git__strtol32(&attr, buffer, &buffer, 8) < 0 ||
+			!buffer || !valid_attributes(attr))
 			return tree_error("Failed to parse tree. Can't parse attributes");
 
-		entry->attr = tmp;
-
 		if (*buffer++ != ' ')
 			return tree_error("Failed to parse tree. Object is corrupted");
 
 		if (memchr(buffer, 0, buffer_end - buffer) == NULL)
 			return tree_error("Failed to parse tree. Object is corrupted");
 
-		entry->filename = git__strdup(buffer);
-		entry->filename_len = strlen(buffer);
+		/** Allocate the entry and store it in the entries vector */
+		{
+			entry = alloc_entry(buffer);
+			GITERR_CHECK_ALLOC(entry);
+
+			if (git_vector_insert(&tree->entries, entry) < 0)
+				return -1;
+
+			entry->attr = attr;
+		}
 
 		while (buffer < buffer_end && *buffer != 0)
 			buffer++;
@@ -303,16 +342,17 @@ static unsigned int find_next_dir(const char *dirname, git_index *index, unsigne
 	return i;
 }
 
-static int append_entry(git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
+static int append_entry(
+	git_treebuilder *bld,
+	const char *filename,
+	const git_oid *id,
+	unsigned int attributes)
 {
 	git_tree_entry *entry;
 
-	entry = git__calloc(1, sizeof(git_tree_entry));
+	entry = alloc_entry(filename);
 	GITERR_CHECK_ALLOC(entry);
 
-	entry->filename = git__strdup(filename);
-	entry->filename_len = strlen(entry->filename);
-
 	git_oid_cpy(&entry->oid, id);
 	entry->attr = attributes;
 
@@ -488,7 +528,12 @@ on_error:
 	return -1;
 }
 
-int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
+int git_treebuilder_insert(
+	const git_tree_entry **entry_out,
+	git_treebuilder *bld,
+	const char *filename,
+	const git_oid *id,
+	unsigned int attributes)
 {
 	git_tree_entry *entry;
 	int pos;
@@ -501,30 +546,28 @@ int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, con
 	if (!valid_entry_name(filename))
 		return tree_error("Failed to insert entry. Invalid name for a tree entry");
 
-	pos = tree_key_search(&bld->entries, filename);
+	pos = tree_key_search(&bld->entries, filename, strlen(filename));
 
 	if (pos >= 0) {
 		entry = git_vector_get(&bld->entries, pos);
 		if (entry->removed)
 			entry->removed = 0;
 	} else {
-		entry = git__calloc(1, sizeof(git_tree_entry));
+		entry = alloc_entry(filename);
 		GITERR_CHECK_ALLOC(entry);
-
-		entry->filename = git__strdup(filename);
-		entry->filename_len = strlen(entry->filename);
 	}
 
 	git_oid_cpy(&entry->oid, id);
 	entry->attr = attributes;
 
-	if (pos == GIT_ENOTFOUND) {
+	if (pos < 0) {
 		if (git_vector_insert(&bld->entries, entry) < 0)
 			return -1;
 	}
 
-	if (entry_out != NULL)
+	if (entry_out != NULL) {
 		*entry_out = entry;
+	}
 
 	return 0;
 }
@@ -536,7 +579,7 @@ static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filenam
 
 	assert(bld && filename);
 
-	idx = tree_key_search(&bld->entries, filename);
+	idx = tree_key_search(&bld->entries, filename, strlen(filename));
 	if (idx < 0)
 		return NULL;
 
@@ -625,8 +668,7 @@ void git_treebuilder_clear(git_treebuilder *bld)
 
 	for (i = 0; i < bld->entries.length; ++i) {
 		git_tree_entry *e = bld->entries.contents[i];
-		git__free(e->filename);
-		git__free(e);
+		git_tree_entry_free(e);
 	}
 
 	git_vector_clear(&bld->entries);
@@ -642,85 +684,78 @@ void git_treebuilder_free(git_treebuilder *bld)
 	git__free(bld);
 }
 
-static int tree_frompath(
-	git_tree **parent_out,
+static size_t subpath_len(const char *path)
+{
+	const char *slash_pos = strchr(path, '/');
+	if (slash_pos == NULL)
+		return strlen(path);
+
+	return slash_pos - path;
+}
+
+int git_tree_entry_bypath(
+	git_tree_entry **entry_out,
 	git_tree *root,
-	git_buf *treeentry_path,
-	size_t offset)
+	const char *path)
 {
-	char *slash_pos = NULL;
-	const git_tree_entry* entry;
 	int error = 0;
 	git_tree *subtree;
+	const git_tree_entry *entry;
+	size_t filename_len;
 
-	if (!*(treeentry_path->ptr + offset)) {
-		giterr_set(GITERR_INVALID,
-			"Invalid relative path to a tree entry '%s'.", treeentry_path->ptr);
-		return -1;
-	}
-
-	slash_pos = (char *)strchr(treeentry_path->ptr + offset, '/');
+	/* Find how long is the current path component (i.e.
+	 * the filename between two slashes */
+	filename_len = subpath_len(path);
 
-	if (slash_pos == NULL)
-		return git_tree_lookup(
-			parent_out,
-			root->object.repo,
-			git_object_id((const git_object *)root)
-		);
-
-	if (slash_pos == treeentry_path->ptr + offset) {
-		giterr_set(GITERR_INVALID,
-			"Invalid relative path to a tree entry '%s'.", treeentry_path->ptr);
-		return -1;
+	if (filename_len == 0) {
+		giterr_set(GITERR_TREE, "Invalid tree path given");
+		return GIT_ENOTFOUND;
 	}
 
-	*slash_pos = '\0';
-
-	entry = git_tree_entry_byname(root, treeentry_path->ptr + offset);
-
-	if (slash_pos != NULL)
-		*slash_pos = '/';
+	entry = entry_fromname(root, path, filename_len);
 
 	if (entry == NULL) {
 		giterr_set(GITERR_TREE,
-			"No tree entry can be found from "
-			"the given tree and relative path '%s'.", treeentry_path->ptr);
+			"The path '%s' does not exist in the given tree", path);
 		return GIT_ENOTFOUND;
 	}
 
+	switch (path[filename_len]) {
+	case '/': 
+		/* If there are more components in the path...
+		 * then this entry *must* be a tree */
+		if (!git_tree_entry__is_tree(entry)) {
+			giterr_set(GITERR_TREE,
+				"The path '%s' does not exist in the given tree", path);
+			return -1;
+		}
+
+		/* If there's only a slash left in the path, we 
+		 * return the current entry; otherwise, we keep
+		 * walking down the path */
+		if (path[filename_len + 1] != '\0')
+			break;
+
+	case '\0':
+		/* If there are no more components in the path, return
+		 * this entry */
+		*entry_out = git_tree_entry_dup(entry);
+		return 0;
+	}
 
 	if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0)
-		return error;
+		return -1;
 
-	error = tree_frompath(
-		parent_out,
+	error = git_tree_entry_bypath(
+		entry_out,
 		subtree,
-		treeentry_path,
-		(slash_pos - treeentry_path->ptr) + 1
+		path + filename_len + 1
 	);
 
 	git_tree_free(subtree);
 	return error;
 }
 
-int git_tree_get_subtree(
-	git_tree **subtree,
-	git_tree *root,
-	const char *subtree_path)
-{
-	int error;
-	git_buf buffer = GIT_BUF_INIT;
-
-	assert(subtree && root && subtree_path);
-
-	if ((error = git_buf_sets(&buffer, subtree_path)) == 0)
-		error = tree_frompath(subtree, root, &buffer, 0);
-
-	git_buf_free(&buffer);
-
-	return error;
-}
-
 static int tree_walk_post(
 	git_tree *tree,
 	git_treewalk_cb callback,
diff --git a/src/tree.h b/src/tree.h
index 498a90d..c49309c 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -13,11 +13,11 @@
 #include "vector.h"
 
 struct git_tree_entry {
-	unsigned int attr;
-	char *filename;
+	uint16_t removed;
+	uint16_t attr;
 	git_oid oid;
 	size_t filename_len;
-	int removed;
+	char filename[1];
 };
 
 struct git_tree {
diff --git a/tests-clar/object/tree/frompath.c b/tests-clar/object/tree/frompath.c
index 06c69ac..ef092d1 100644
--- a/tests-clar/object/tree/frompath.c
+++ b/tests-clar/object/tree/frompath.c
@@ -1,15 +1,14 @@
 #include "clar_libgit2.h"
 
 static git_repository *repo;
-const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
 static	git_tree *tree;
 
 void test_object_tree_frompath__initialize(void)
 {
 	git_oid id;
+	const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
 
-	cl_fixture_sandbox("testrepo.git");
-	cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
 	cl_assert(repo != NULL);
 
 	cl_git_pass(git_oid_fromstr(&id, tree_with_subtrees_oid));
@@ -21,61 +20,46 @@ void test_object_tree_frompath__cleanup(void)
 {
 	git_tree_free(tree);
 	git_repository_free(repo);
-	cl_fixture_cleanup("testrepo.git");
 }
 
-static void assert_tree_from_path(git_tree *root, const char *path, int expected_result, const char *expected_raw_oid)
+static void assert_tree_from_path(
+	git_tree *root,
+	const char *path,
+	const char *expected_entry_name)
 {
-	git_tree *containing_tree = NULL;
+	git_tree_entry *entry;
 
-	cl_assert(git_tree_get_subtree(&containing_tree, root, path) == expected_result);
-	
-	if (containing_tree == NULL && expected_result != 0)
-		return;
-	
-	cl_assert(containing_tree != NULL && expected_result == 0);
-
-	cl_git_pass(git_oid_streq(git_object_id((const git_object *)containing_tree), expected_raw_oid));
-
-	git_tree_free(containing_tree);
-}
-
-static void assert_tree_from_path_klass(git_tree *root, const char *path, int expected_result, const char *expected_raw_oid)
-{
-	assert_tree_from_path(root, path, GIT_ERROR, expected_raw_oid);
-	cl_assert(giterr_last()->klass == expected_result);
+	cl_git_pass(git_tree_entry_bypath(&entry, root, path));
+	cl_assert_equal_s(git_tree_entry_name(entry), expected_entry_name);
+	git_tree_entry_free(entry);
 }
 
 void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void)
 {
-	/* Will return self if given a one path segment... */
-	assert_tree_from_path(tree, "README", 0, tree_with_subtrees_oid);
-	
-	/* ...even one that lead to a non existent tree entry. */
-	assert_tree_from_path(tree, "i-do-not-exist.txt", 0, tree_with_subtrees_oid);
-	
-	/* Will return fgh tree oid given this following path... */
-	assert_tree_from_path(tree, "ab/de/fgh/1.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54");
-	
-	/* ... and ab tree oid given this one. */
-	assert_tree_from_path(tree, "ab/de", 0, "f1425cef211cc08caa31e7b545ffb232acb098c3");
+	git_tree_entry *e;
 
-	/* Will succeed if given a valid path which leads to a tree entry which doesn't exist */
-	assert_tree_from_path(tree, "ab/de/fgh/i-do-not-exist.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54");
-}
+	assert_tree_from_path(tree, "README", "README");
+	assert_tree_from_path(tree, "ab/de/fgh/1.txt", "1.txt");
+	assert_tree_from_path(tree, "ab/de/fgh", "fgh");
+	assert_tree_from_path(tree, "ab/de/fgh/", "fgh");
+	assert_tree_from_path(tree, "ab/de", "de");
+	assert_tree_from_path(tree, "ab/", "ab");
+	assert_tree_from_path(tree, "ab/de/", "de");
 
-void test_object_tree_frompath__fail_when_processing_an_unknown_tree_segment(void)
-{
-	assert_tree_from_path(tree, "nope/de/fgh/1.txt", GIT_ENOTFOUND, NULL);
-	assert_tree_from_path(tree, "ab/me-neither/fgh/2.txt", GIT_ENOTFOUND, NULL);
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "i-do-not-exist.txt"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "README/"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/de/fgh/i-do-not-exist.txt"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "nope/de/fgh/1.txt"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt/"));
 }
 
 void test_object_tree_frompath__fail_when_processing_an_invalid_path(void)
 {
-	assert_tree_from_path_klass(tree, "/", GITERR_INVALID, NULL);
-	assert_tree_from_path_klass(tree, "/ab", GITERR_INVALID, NULL);
-	assert_tree_from_path_klass(tree, "/ab/de", GITERR_INVALID, NULL);
-	assert_tree_from_path_klass(tree, "ab/", GITERR_INVALID, NULL);
-	assert_tree_from_path_klass(tree, "ab//de", GITERR_INVALID, NULL);
-	assert_tree_from_path_klass(tree, "ab/de/", GITERR_INVALID, NULL);
+	git_tree_entry *e;
+
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "/"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab/de"));
+	cl_must_fail(git_tree_entry_bypath(&e, tree, "ab//de"));
 }