Skip to content

Commit

Permalink
Merge branch 'jk/mailsplit-maildir-muttsort' into maint
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Junio C Hamano committed Mar 26, 2013
2 parents 7d2726c + 18505c3 commit ece12fd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions builtin/mailsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ static int populate_maildir_list(struct string_list *list, const char *path)
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,
int nr_prec, int skip)
{
Expand All @@ -139,6 +160,8 @@ static int split_maildir(const char *maildir, const char *dir,
int i;
struct string_list list = STRING_LIST_INIT_DUP;

list.cmp = maildir_filename_cmp;

if (populate_maildir_list(&list, maildir) < 0)
goto out;

Expand Down

0 comments on commit ece12fd

Please sign in to comment.