Commit 98527b5b by Russell Belfer

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.
parent 23594c1d
......@@ -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.
......
......@@ -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)
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment