Move the path comparison method to fileops.c Signed-off-by: Vicent Marti <tanoku@gmail.com>
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
diff --git a/src/fileops.c b/src/fileops.c
index 602862c..9938011 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -493,3 +493,22 @@ int gitfo_prettify_file_path(char *buffer_out, const char *path)
return GIT_SUCCESS;
}
+
+int gitfo_cmp_path(const char *name1, int len1, int isdir1,
+ const char *name2, int len2, int isdir2)
+{
+ int len = len1 < len2 ? len1 : len2;
+ int cmp;
+
+ cmp = memcmp(name1, name2, len);
+ if (cmp)
+ return cmp;
+ if (len1 < len2)
+ return ((!isdir1 && !isdir2) ? -1 :
+ (isdir1 ? '/' - name2[len1] : name2[len1] - '/'));
+ if (len1 > len2)
+ return ((!isdir1 && !isdir2) ? 1 :
+ (isdir2 ? name1[len2] - '/' : '/' - name1[len2]));
+ return 0;
+}
+
diff --git a/src/fileops.h b/src/fileops.h
index d333805..16e7103 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -131,6 +131,10 @@ extern int gitfo_write_cached(gitfo_cache *ioc, void *buf, size_t len);
extern int gitfo_flush_cached(gitfo_cache *ioc);
extern int gitfo_close_cached(gitfo_cache *ioc);
+
+extern int gitfo_cmp_path(const char *name1, int len1, int isdir1,
+ const char *name2, int len2, int isdir2);
+
/**
* Clean up a provided absolute or relative directory path.
*
diff --git a/src/tree.c b/src/tree.c
index e8cf179..a23be91 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -40,30 +40,12 @@ int entry_search_cmp(const void *key, const void *array_member)
return strcmp(filename, entry->filename);
}
-static int cache_name_compare(const char *name1, int len1, int isdir1,
- const char *name2, int len2, int isdir2)
-{
- int len = len1 < len2 ? len1 : len2;
- int cmp;
-
- cmp = memcmp(name1, name2, len);
- if (cmp)
- return cmp;
- if (len1 < len2)
- return ((!isdir1 && !isdir2) ? -1 :
- (isdir1 ? '/' - name2[len1] : name2[len1] - '/'));
- if (len1 > len2)
- return ((!isdir1 && !isdir2) ? 1 :
- (isdir2 ? name1[len2] - '/' : '/' - name1[len2]));
- return 0;
-}
-
int entry_sort_cmp(const void *a, const void *b)
{
const git_tree_entry *entry_a = *(const git_tree_entry **)(a);
const git_tree_entry *entry_b = *(const git_tree_entry **)(b);
- return cache_name_compare(entry_a->filename, strlen(entry_a->filename),
+ return gitfo_cmp_path(entry_a->filename, strlen(entry_a->filename),
entry_a->attr & 040000,
entry_b->filename, strlen(entry_b->filename),
entry_b->attr & 040000);