Skip to content

Commit

Permalink
Introduce "base_name_compare()" helper function
Browse files Browse the repository at this point in the history
This one compares two pathnames that may be partial basenames, not
full paths. We need to get the path sorting right, since a directory
name will sort as if it had the final '/' at the end.
  • Loading branch information
Linus Torvalds committed May 20, 2005
1 parent e593638 commit 958ba6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ extern void usage(const char *err);
extern void die(const char *err, ...);
extern int error(const char *err, ...);

extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);

extern void *read_object_with_reference(const unsigned char *sha1,
Expand Down
19 changes: 19 additions & 0 deletions read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st)
return changed;
}

int base_name_compare(const char *name1, int len1, int mode1,
const char *name2, int len2, int mode2)
{
unsigned char c1, c2;
int len = len1 < len2 ? len1 : len2;
int cmp;

cmp = memcmp(name1, name2, len);
if (cmp)
return cmp;
c1 = name1[len];
c2 = name2[len];
if (!c1 && S_ISDIR(mode1))
c1 = '/';
if (!c2 && S_ISDIR(mode2))
c2 = '/';
return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
}

int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
{
int len1 = flags1 & CE_NAMEMASK;
Expand Down

0 comments on commit 958ba6c

Please sign in to comment.