Merge branch 'jk/mailsplit-maildir-muttsort' into maint

Sort filenames read from the maildir/ in a way that is more likely
to sort messages in the order the writing MUA meant to, by sorting
numeric segment in numeric order and non-numeric segment in
alphabetical order.

* jk/mailsplit-maildir-muttsort:
  mailsplit: sort maildir filenames more cleverly
This commit is contained in:
Junio C Hamano 2013-03-26 12:44:11 -07:00
commit ece12fd844

View File

@ -130,6 +130,27 @@ static int populate_maildir_list(struct string_list *list, const char *path)
return 0; return 0;
} }
static int maildir_filename_cmp(const char *a, const char *b)
{
while (*a && *b) {
if (isdigit(*a) && isdigit(*b)) {
long int na, nb;
na = strtol(a, (char **)&a, 10);
nb = strtol(b, (char **)&b, 10);
if (na != nb)
return na - nb;
/* strtol advanced our pointers */
}
else {
if (*a != *b)
return (unsigned char)*a - (unsigned char)*b;
a++;
b++;
}
}
return (unsigned char)*a - (unsigned char)*b;
}
static int split_maildir(const char *maildir, const char *dir, static int split_maildir(const char *maildir, const char *dir,
int nr_prec, int skip) int nr_prec, int skip)
{ {
@ -139,6 +160,8 @@ static int split_maildir(const char *maildir, const char *dir,
int i; int i;
struct string_list list = STRING_LIST_INIT_DUP; struct string_list list = STRING_LIST_INIT_DUP;
list.cmp = maildir_filename_cmp;
if (populate_maildir_list(&list, maildir) < 0) if (populate_maildir_list(&list, maildir) < 0)
goto out; goto out;