mailsplit.con commit sample update-hook: sanely handle a new branch head. (41e79c9)
   1/*
   2 * Totally braindamaged mbox splitter program.
   3 *
   4 * It just splits a mbox into a list of files: "0001" "0002" ..
   5 * so you can process them further from there.
   6 */
   7#include <unistd.h>
   8#include <stdlib.h>
   9#include <fcntl.h>
  10#include <sys/types.h>
  11#include <sys/stat.h>
  12#include <string.h>
  13#include <stdio.h>
  14#include <assert.h>
  15#include "cache.h"
  16
  17static const char git_mailsplit_usage[] =
  18"git-mailsplit [-d<prec>] [-f<n>] [-b] -o<directory> <mbox>...";
  19
  20static int is_from_line(const char *line, int len)
  21{
  22        const char *colon;
  23
  24        if (len < 20 || memcmp("From ", line, 5))
  25                return 0;
  26
  27        colon = line + len - 2;
  28        line += 5;
  29        for (;;) {
  30                if (colon < line)
  31                        return 0;
  32                if (*--colon == ':')
  33                        break;
  34        }
  35
  36        if (!isdigit(colon[-4]) ||
  37            !isdigit(colon[-2]) ||
  38            !isdigit(colon[-1]) ||
  39            !isdigit(colon[ 1]) ||
  40            !isdigit(colon[ 2]))
  41                return 0;
  42
  43        /* year */
  44        if (strtol(colon+3, NULL, 10) <= 90)
  45                return 0;
  46
  47        /* Ok, close enough */
  48        return 1;
  49}
  50
  51/* Could be as small as 64, enough to hold a Unix "From " line. */
  52static char buf[4096];
  53
  54/* Called with the first line (potentially partial)
  55 * already in buf[] -- normally that should begin with
  56 * the Unix "From " line.  Write it into the specified
  57 * file.
  58 */
  59static int split_one(FILE *mbox, const char *name, int allow_bare)
  60{
  61        FILE *output = NULL;
  62        int len = strlen(buf);
  63        int fd;
  64        int status = 0;
  65        int is_bare = !is_from_line(buf, len);
  66
  67        if (is_bare && !allow_bare)
  68                goto corrupt;
  69
  70        fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
  71        if (fd < 0)
  72                die("cannot open output file %s", name);
  73        output = fdopen(fd, "w");
  74
  75        /* Copy it out, while searching for a line that begins with
  76         * "From " and having something that looks like a date format.
  77         */
  78        for (;;) {
  79                int is_partial = (buf[len-1] != '\n');
  80
  81                if (fputs(buf, output) == EOF)
  82                        die("cannot write output");
  83
  84                if (fgets(buf, sizeof(buf), mbox) == NULL) {
  85                        if (feof(mbox)) {
  86                                status = 1;
  87                                break;
  88                        }
  89                        die("cannot read mbox");
  90                }
  91                len = strlen(buf);
  92                if (!is_partial && !is_bare && is_from_line(buf, len))
  93                        break; /* done with one message */
  94        }
  95        fclose(output);
  96        return status;
  97
  98 corrupt:
  99        if (output)
 100                fclose(output);
 101        unlink(name);
 102        fprintf(stderr, "corrupt mailbox\n");
 103        exit(1);
 104}
 105
 106int main(int argc, const char **argv)
 107{
 108        int nr = 0, nr_prec = 4;
 109        int allow_bare = 0;
 110        const char *dir = NULL;
 111        const char **argp;
 112        static const char *stdin_only[] = { "-", NULL };
 113        char *name;
 114
 115        for (argp = argv+1; *argp; argp++) {
 116                const char *arg = *argp;
 117
 118                if (arg[0] != '-')
 119                        break;
 120                /* do flags here */
 121                if ( arg[1] == 'd' ) {
 122                        nr_prec = strtol(arg+2, NULL, 10);
 123                        if (nr_prec < 3 || 10 <= nr_prec)
 124                                usage(git_mailsplit_usage);
 125                        continue;
 126                } else if ( arg[1] == 'f' ) {
 127                        nr = strtol(arg+2, NULL, 10);
 128                } else if ( arg[1] == 'b' && !arg[2] ) {
 129                        allow_bare = 1;
 130                } else if ( arg[1] == 'o' && arg[2] ) {
 131                        dir = arg+2;
 132                } else if ( arg[1] == '-' && !arg[2] ) {
 133                        argp++; /* -- marks end of options */
 134                        break;
 135                } else {
 136                        die("unknown option: %s", arg);
 137                }
 138        }
 139
 140        if ( !dir ) {
 141                /* Backwards compatibility: if no -o specified, accept
 142                   <mbox> <dir> or just <dir> */
 143                switch (argc - (argp-argv)) {
 144                case 1:
 145                        dir = argp[0];
 146                        argp = stdin_only;
 147                        break;
 148                case 2:
 149                        stdin_only[0] = argp[0];
 150                        dir = argp[1];
 151                        argp = stdin_only;
 152                        break;
 153                default:
 154                        usage(git_mailsplit_usage);
 155                }
 156        } else {
 157                /* New usage: if no more argument, parse stdin */
 158                if ( !*argp )
 159                        argp = stdin_only;
 160        }
 161
 162        name = xmalloc(strlen(dir) + 2 + 3 * sizeof(nr));
 163
 164        while (*argp) {
 165                const char *file = *argp++;
 166                FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "rt");
 167                int file_done = 0;
 168
 169                if ( !f )
 170                        die ("cannot open mbox %s", file);
 171
 172                if (fgets(buf, sizeof(buf), f) == NULL) {
 173                        if (f == stdin)
 174                                break; /* empty stdin is OK */
 175                        die("cannot read mbox %s", file);
 176                }
 177
 178                while (!file_done) {
 179                        sprintf(name, "%s/%0*d", dir, nr_prec, ++nr);
 180                        file_done = split_one(f, name, allow_bare);
 181                }
 182
 183                if (f != stdin)
 184                        fclose(f);
 185        }
 186
 187        printf("%d\n", nr);
 188        return 0;
 189}