Commit 63da309a4f2c5978e3fe9ec99266313333116218

Stefan Sperling 2018-11-07T05:22:46

tree entries are sorted, so find_entry_by_name() can exit early

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/lib/object.c b/lib/object.c
index 2100b96..abcc58c 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -707,8 +707,14 @@ find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
 {
 	struct got_tree_entry *te;
 
+	/* Note that tree entries are sorted in strncmp() order. */
 	SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
-		if (strncmp(te->name, name, len) == 0 && te->name[len] == '\0')
+		int cmp = strncmp(te->name, name, len);
+		if (cmp < 0)
+			continue;
+		if (cmp > 0)
+			break;
+		if (te->name[len] == '\0')
 			return te;
 	}
 	return NULL;