Commit 98527b5b241ce9d240537f60e87aa9dd084c0f36

Russell Belfer 2013-01-09T16:03:35

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.

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);