Merge branch 'jc/strbuf-branchname-fix'
[gitweb.git] / builtin / mailsplit.c
index 207e358ed19cecb8cf7b57d59a9149619909459d..06296d4bdf607431bfcb38f0bc6720f555d6917a 100644 (file)
@@ -10,7 +10,7 @@
 #include "strbuf.h"
 
 static const char git_mailsplit_usage[] =
-"git mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> [<mbox>|<Maildir>...]";
+"git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
 
 static int is_from_line(const char *line, int len)
 {
@@ -121,7 +121,7 @@ static int populate_maildir_list(struct string_list *list, const char *path)
                        if (dent->d_name[0] == '.')
                                continue;
                        snprintf(name, sizeof(name), "%s/%s", *sub, dent->d_name);
-                       string_list_insert(name, list);
+                       string_list_insert(list, name);
                }
 
                closedir(dir);
@@ -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)
 {
@@ -137,7 +158,9 @@ static int split_maildir(const char *maildir, const char *dir,
        char name[PATH_MAX];
        int ret = -1;
        int i;
-       struct string_list list = {NULL, 0, 0, 1};
+       struct string_list list = STRING_LIST_INIT_DUP;
+
+       list.cmp = maildir_filename_cmp;
 
        if (populate_maildir_list(&list, maildir) < 0)
                goto out;