[PATCH] Fix git-rev-parse --default and --flags handling
[gitweb.git] / tools / mailinfo.c
index ae279bffa5743aa0d58701f98eff17e25325fe0b..a36123a1f5bc27e90debcf19d5348937c738d2f2 100644 (file)
@@ -9,6 +9,7 @@
 
 static FILE *cmitmsg, *patchfile;
 
+static int keep_subject = 0;
 static char line[1000];
 static char date[1000];
 static char name[1000];
@@ -89,49 +90,20 @@ static void handle_subject(char *line)
        strcpy(subject, line);
 }
 
-static void add_subject_line(char *line)
-{
-       while (isspace(*line))
-               line++;
-       *--line = ' ';
-       strcat(subject, line);
-}
-
 static void check_line(char *line, int len)
 {
-       static int cont = -1;
-       if (!memcmp(line, "From:", 5) && isspace(line[5])) {
+       if (!memcmp(line, "From:", 5) && isspace(line[5]))
                handle_from(line+6);
-               cont = 0;
-               return;
-       }
-       if (!memcmp(line, "Date:", 5) && isspace(line[5])) {
+       else if (!memcmp(line, "Date:", 5) && isspace(line[5]))
                handle_date(line+6);
-               cont = 0;
-               return;
-       }
-       if (!memcmp(line, "Subject:", 8) && isspace(line[8])) {
+       else if (!memcmp(line, "Subject:", 8) && isspace(line[8]))
                handle_subject(line+9);
-               cont = 1;
-               return;
-       }
-       if (isspace(*line)) {
-               switch (cont) {
-               case 0:
-                       fprintf(stderr, "I don't do 'Date:' or 'From:' line continuations\n");
-                       break;
-               case 1:
-                       add_subject_line(line);
-                       return;
-               default:
-                       break;
-               }
-       }
-       cont = -1;
 }
 
 static char * cleanup_subject(char *subject)
 {
+       if (keep_subject)
+               return subject;
        for (;;) {
                char *p;
                int len, remove;
@@ -183,13 +155,13 @@ static void cleanup_space(char *buf)
 
 static void handle_rest(void)
 {
+       FILE *out = cmitmsg;
        char *sub = cleanup_subject(subject);
        cleanup_space(name);
        cleanup_space(date);
        cleanup_space(email);
        cleanup_space(sub);
        printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", name, email, sub, date);
-       FILE *out = cmitmsg;
 
        do {
                if (!memcmp("diff -", line, 6) ||
@@ -220,8 +192,9 @@ static int eatspace(char *line)
 static void handle_body(void)
 {
        int has_from = 0;
+       int has_date = 0;
 
-       /* First line of body can be a From: */
+       /* First lines of body can have From: and Date: */
        while (fgets(line, sizeof(line), stdin) != NULL) {
                int len = eatspace(line);
                if (!len)
@@ -232,20 +205,60 @@ static void handle_body(void)
                                continue;
                        }
                }
+               if (!memcmp("Date:", line, 5) && isspace(line[5])) {
+                       if (!has_date) {
+                               handle_date(line+6);
+                               has_date = 1;
+                               continue;
+                       }
+               }
                line[len] = '\n';
                handle_rest();
                break;
        }
 }
 
+static int read_one_header_line(char *line, int sz, FILE *in)
+{
+       int ofs = 0;
+       while (ofs < sz) {
+               int peek, len;
+               if (fgets(line + ofs, sz - ofs, in) == NULL)
+                       return ofs;
+               len = eatspace(line + ofs);
+               if (len == 0)
+                       return ofs;
+               peek = fgetc(in); ungetc(peek, in);
+               if (peek == ' ' || peek == '\t') {
+                       /* Yuck, 2822 header "folding" */
+                       ofs += len;
+                       continue;
+               }
+               return ofs + len;
+       }
+       return ofs;
+}
+
 static void usage(void)
 {
-       fprintf(stderr, "mailinfo msg-file path-file < email\n");
+       fprintf(stderr, "mailinfo msg-file patch-file < email\n");
        exit(1);
 }
 
+static const char mailinfo_usage[] =
+"git-mailinfo [-k] msg patch <mail >info";
 int main(int argc, char ** argv)
 {
+       while (1 < argc && argv[1][0] == '-') {
+               if (!strcmp(argv[1], "-k"))
+                       keep_subject = 1;
+               else {
+                       fprintf(stderr, "usage: %s\n", mailinfo_usage);
+                       exit(1);
+               }
+               argc--; argv++;
+       }
+
        if (argc != 3)
                usage();
        cmitmsg = fopen(argv[1], "w");
@@ -258,8 +271,8 @@ int main(int argc, char ** argv)
                perror(argv[2]);
                exit(1);
        }
-       while (fgets(line, sizeof(line), stdin) != NULL) {
-               int len = eatspace(line);
+       while (1) {
+               int len = read_one_header_line(line, sizeof(line), stdin);
                if (!len) {
                        handle_body();
                        break;