Skip to content

Commit 53dca33

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aabc561 commit 53dca33

File tree

4 files changed

+77
-73
lines changed

4 files changed

+77
-73
lines changed

cache.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,6 @@ extern int verify_ce_order;
557557
#define DATA_CHANGED 0x0020
558558
#define TYPE_CHANGED 0x0040
559559

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);
565560
int cmp_cache_name_compare(const void *a_, const void *b_);
566561

567562
/* add */

read-cache.c

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -499,74 +499,6 @@ int ie_modified(struct index_state *istate,
499499
return 0;
500500
}
501501

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-
570502
static int cache_name_stage_compare(const char *name1, int len1, int stage1,
571503
const char *name2, int len2, int stage2)
572504
{

tree.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,74 @@ int read_tree(struct repository *r,
9494
return ret;
9595
}
9696

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+
97165
struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
98166
{
99167
struct object *obj = lookup_object(r, oid);

tree.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ void free_tree_buffer(struct tree *tree);
2929
/* Parses and returns the tree in the given ent, chasing tags and commits. */
3030
struct tree *parse_tree_indirect(const struct object_id *oid);
3131

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

3342
#define READ_TREE_RECURSIVE 1
3443
typedef int (*read_tree_fn_t)(const struct object_id *, struct strbuf *, const char *, unsigned int, void *);

0 commit comments

Comments
 (0)