Add git_tree_entry_cmp and git_tree_entry_icmp This adds a new external API git_tree_entry_cmp and a new internal API git_tree_entry_icmp for sorting tree entries. The case insensitive one is internal only because general users should never be seeing case-insensitively sorted trees.
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
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 7726a65..3861102 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -209,6 +209,15 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry);
GIT_EXTERN(git_filemode_t) git_tree_entry_filemode(const git_tree_entry *entry);
/**
+ * Compare two tree entries
+ *
+ * @param e1 first tree entry
+ * @param e2 second tree entry
+ * @return <0 if e1 is before e2, 0 if e1 == e2, >0 if e1 is after e2
+ */
+GIT_EXTERN(int) git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2);
+
+/**
* Convert a tree entry to the git_object it points too.
*
* You must call `git_object_free()` on the object when you are done with it.
diff --git a/src/tree.c b/src/tree.c
index cd1cd60..c34e9b9 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -55,14 +55,23 @@ static int valid_entry_name(const char *filename)
strcmp(filename, DOT_GIT) != 0));
}
-static int entry_sort_cmp(const void *a, const void *b)
+int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2)
{
- const git_tree_entry *entry_a = (const git_tree_entry *)(a);
- const git_tree_entry *entry_b = (const git_tree_entry *)(b);
-
return git_path_cmp(
- entry_a->filename, entry_a->filename_len, git_tree_entry__is_tree(entry_a),
- entry_b->filename, entry_b->filename_len, git_tree_entry__is_tree(entry_b));
+ e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
+ e2->filename, e2->filename_len, git_tree_entry__is_tree(e2));
+}
+
+int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
+{
+ return git_path_icmp(
+ e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
+ e2->filename, e2->filename_len, git_tree_entry__is_tree(e2));
+}
+
+static int entry_sort_cmp(const void *a, const void *b)
+{
+ return git_tree_entry_cmp((const git_tree_entry *)a, (const git_tree_entry *)b);
}
static git_tree_entry *alloc_entry(const char *filename)
diff --git a/src/tree.h b/src/tree.h
index 6f05f5a..27afd4f 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -39,6 +39,8 @@ GIT_INLINE(bool) git_tree_entry__is_tree(const struct git_tree_entry *e)
return (S_ISDIR(e->attr) && !S_ISGITLINK(e->attr));
}
+extern int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2);
+
void git_tree__free(git_tree *tree);
int git_tree__parse(git_tree *tree, git_odb_object *obj);