cache,tree: move basic name compare functions from read-cache to tree
None of base_name_compare(), df_name_compare(), or name_compare() depended upon a cache_entry or index_state in any way. By moving these functions to tree.h, half a dozen other files can stop depending upon cache.h (though that change will be made in a later commit). Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren committed
Apr 22, 2023 at 20:17 UTC
53dca334d6c56488994c0c80a247707419cddbfc
4 files changed
+77
-73
cache.h
-5
@@ -557,11 +557,6 @@ extern int verify_ce_order;
557
#define DATA_CHANGED 0x0020
558
#define TYPE_CHANGED 0x0040
559
560
-int base_name_compare(const char *name1, size_t len1, int mode1,
561
- const char *name2, size_t len2, int mode2);
562
-int df_name_compare(const char *name1, size_t len1, int mode1,
563
- const char *name2, size_t len2, int mode2);
564
-int name_compare(const char *name1, size_t len1, const char *name2, size_t len2);
560
int cmp_cache_name_compare(const void *a_, const void *b_);
561
562
/* add */
read-cache.c
-68
@@ -499,74 +499,6 @@ int ie_modified(struct index_state *istate,
499
return 0;
500
}
501
502
-int base_name_compare(const char *name1, size_t len1, int mode1,
503
- const char *name2, size_t len2, int mode2)
504
-{
505
- unsigned char c1, c2;
506
- size_t len = len1 < len2 ? len1 : len2;
507
- int cmp;
508
-
509
- cmp = memcmp(name1, name2, len);
510
- if (cmp)
511
- return cmp;
512
- c1 = name1[len];
513
- c2 = name2[len];
514
- if (!c1 && S_ISDIR(mode1))
515
- c1 = '/';
516
- if (!c2 && S_ISDIR(mode2))
517
- c2 = '/';
518
- return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
519
-}
520
-
521
-/*
522
- * df_name_compare() is identical to base_name_compare(), except it
523
- * compares conflicting directory/file entries as equal. Note that
524
- * while a directory name compares as equal to a regular file, they
525
- * then individually compare _differently_ to a filename that has
526
- * a dot after the basename (because '\0' < '.' < '/').
527
- *
528
- * This is used by routines that want to traverse the git namespace
529
- * but then handle conflicting entries together when possible.
530
- */
531
-int df_name_compare(const char *name1, size_t len1, int mode1,
532
- const char *name2, size_t len2, int mode2)
533
-{
534
- unsigned char c1, c2;
535
- size_t len = len1 < len2 ? len1 : len2;
536
- int cmp;
537
-
538
- cmp = memcmp(name1, name2, len);
539
- if (cmp)
540
- return cmp;
541
- /* Directories and files compare equal (same length, same name) */
542
- if (len1 == len2)
543
- return 0;
544
- c1 = name1[len];
545
- if (!c1 && S_ISDIR(mode1))
546
- c1 = '/';
547
- c2 = name2[len];
548
- if (!c2 && S_ISDIR(mode2))
549
- c2 = '/';
550
- if (c1 == '/' && !c2)
551
- return 0;
552
- if (c2 == '/' && !c1)
553
- return 0;
554
- return c1 - c2;
555
-}
556
-
557
-int name_compare(const char *name1, size_t len1, const char *name2, size_t len2)
558
-{
559
- size_t min_len = (len1 < len2) ? len1 : len2;
560
- int cmp = memcmp(name1, name2, min_len);
561
- if (cmp)
562
- return cmp;
563
- if (len1 < len2)
564
- return -1;
565
- if (len1 > len2)
566
- return 1;
567
- return 0;
568
-}
569
-
502
static int cache_name_stage_compare(const char *name1, int len1, int stage1,
503
const char *name2, int len2, int stage2)
504
{
tree.c
+68
@@ -94,6 +94,74 @@ int read_tree(struct repository *r,
94
return ret;
95
}
96
97
+int base_name_compare(const char *name1, size_t len1, int mode1,
98
+ const char *name2, size_t len2, int mode2)
99
+{
100
+ unsigned char c1, c2;
101
+ size_t len = len1 < len2 ? len1 : len2;
102
+ int cmp;
103
+
104
+ cmp = memcmp(name1, name2, len);
105
+ if (cmp)
106
+ return cmp;
107
+ c1 = name1[len];
108
+ c2 = name2[len];
109
+ if (!c1 && S_ISDIR(mode1))
110
+ c1 = '/';
111
+ if (!c2 && S_ISDIR(mode2))
112
+ c2 = '/';
113
+ return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
114
+}
115
+
116
+/*
117
+ * df_name_compare() is identical to base_name_compare(), except it
118
+ * compares conflicting directory/file entries as equal. Note that
119
+ * while a directory name compares as equal to a regular file, they
120
+ * then individually compare _differently_ to a filename that has
121
+ * a dot after the basename (because '\0' < '.' < '/').
122
+ *
123
+ * This is used by routines that want to traverse the git namespace
124
+ * but then handle conflicting entries together when possible.
125
+ */
126
+int df_name_compare(const char *name1, size_t len1, int mode1,
127
+ const char *name2, size_t len2, int mode2)
128
+{
129
+ unsigned char c1, c2;
130
+ size_t len = len1 < len2 ? len1 : len2;
131
+ int cmp;
132
+
133
+ cmp = memcmp(name1, name2, len);
134
+ if (cmp)
135
+ return cmp;
136
+ /* Directories and files compare equal (same length, same name) */
137
+ if (len1 == len2)
138
+ return 0;
139
+ c1 = name1[len];
140
+ if (!c1 && S_ISDIR(mode1))
141
+ c1 = '/';
142
+ c2 = name2[len];
143
+ if (!c2 && S_ISDIR(mode2))
144
+ c2 = '/';
145
+ if (c1 == '/' && !c2)
146
+ return 0;
147
+ if (c2 == '/' && !c1)
148
+ return 0;
149
+ return c1 - c2;
150
+}
151
+
152
+int name_compare(const char *name1, size_t len1, const char *name2, size_t len2)
153
+{
154
+ size_t min_len = (len1 < len2) ? len1 : len2;
155
+ int cmp = memcmp(name1, name2, min_len);
156
+ if (cmp)
157
+ return cmp;
158
+ if (len1 < len2)
159
+ return -1;
160
+ if (len1 > len2)
161
+ return 1;
162
+ return 0;
163
+}
164
+
165
struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
166
{
167
struct object *obj = lookup_object(r, oid);
tree.h
+9
@@ -29,6 +29,15 @@ void free_tree_buffer(struct tree *tree);
29
/* Parses and returns the tree in the given ent, chasing tags and commits. */
30
struct tree *parse_tree_indirect(const struct object_id *oid);
31
32
+/*
33
+ * Functions for comparing pathnames
34
+ */
35
+int base_name_compare(const char *name1, size_t len1, int mode1,
36
+ const char *name2, size_t len2, int mode2);
37
+int df_name_compare(const char *name1, size_t len1, int mode1,
38
+ const char *name2, size_t len2, int mode2);
39
+int name_compare(const char *name1, size_t len1,
40
+ const char *name2, size_t len2);
41
42
#define READ_TREE_RECURSIVE 1
43
typedef int (*read_tree_fn_t)(const struct object_id *, struct strbuf *, const char *, unsigned int, void *);