Merge branch 'jt/use-trailer-api-in-commands'
authorJunio C Hamano <gitster@pobox.com>
Mon, 19 Dec 2016 22:45:29 +0000 (14:45 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 19 Dec 2016 22:45:29 +0000 (14:45 -0800)
Commands that operate on a log message and add lines to the trailer
blocks, such as "format-patch -s", "cherry-pick (-x|-s)", and
"commit -s", have been taught to use the logic of and share the
code with "git interpret-trailer".

* jt/use-trailer-api-in-commands:
sequencer: use trailer's trailer layout
trailer: have function to describe trailer layout
trailer: avoid unnecessary splitting on lines
commit: make ignore_non_trailer take buf/len
trailer: be stricter in parsing separators

1  2 
sequencer.c
diff --combined sequencer.c
index 30b10ba143959bf2618872427f730a9f0e30d369,d64c97397b5387eb707f84e44f32b1dcb4e3f7c3..050e28bbc3d136509e8b7d909b27e9d954fa7754
@@@ -16,6 -16,7 +16,7 @@@
  #include "refs.h"
  #include "argv-array.h"
  #include "quote.h"
+ #include "trailer.h"
  
  #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
  
@@@ -56,30 -57,6 +57,6 @@@ static const char *get_todo_path(const 
        return git_path_todo_file();
  }
  
- static int is_rfc2822_line(const char *buf, int len)
- {
-       int i;
-       for (i = 0; i < len; i++) {
-               int ch = buf[i];
-               if (ch == ':')
-                       return 1;
-               if (!isalnum(ch) && ch != '-')
-                       break;
-       }
-       return 0;
- }
- static int is_cherry_picked_from_line(const char *buf, int len)
- {
-       /*
-        * We only care that it looks roughly like (cherry picked from ...)
-        */
-       return len > strlen(cherry_picked_prefix) + 1 &&
-               starts_with(buf, cherry_picked_prefix) && buf[len - 1] == ')';
- }
  /*
   * Returns 0 for non-conforming footer
   * Returns 1 for conforming footer
  static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
        int ignore_footer)
  {
-       char prev;
-       int i, k;
-       int len = sb->len - ignore_footer;
-       const char *buf = sb->buf;
-       int found_sob = 0;
-       /* footer must end with newline */
-       if (!len || buf[len - 1] != '\n')
-               return 0;
+       struct trailer_info info;
+       int i;
+       int found_sob = 0, found_sob_last = 0;
  
-       prev = '\0';
-       for (i = len - 1; i > 0; i--) {
-               char ch = buf[i];
-               if (prev == '\n' && ch == '\n') /* paragraph break */
-                       break;
-               prev = ch;
-       }
+       trailer_info_get(&info, sb->buf);
  
-       /* require at least one blank line */
-       if (prev != '\n' || buf[i] != '\n')
+       if (info.trailer_start == info.trailer_end)
                return 0;
  
-       /* advance to start of last paragraph */
-       while (i < len - 1 && buf[i] == '\n')
-               i++;
-       for (; i < len; i = k) {
-               int found_rfc2822;
-               for (k = i; k < len && buf[k] != '\n'; k++)
-                       ; /* do nothing */
-               k++;
+       for (i = 0; i < info.trailer_nr; i++)
+               if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) {
+                       found_sob = 1;
+                       if (i == info.trailer_nr - 1)
+                               found_sob_last = 1;
+               }
  
-               found_rfc2822 = is_rfc2822_line(buf + i, k - i - 1);
-               if (found_rfc2822 && sob &&
-                   !strncmp(buf + i, sob->buf, sob->len))
-                       found_sob = k;
+       trailer_info_release(&info);
  
-               if (!(found_rfc2822 ||
-                     is_cherry_picked_from_line(buf + i, k - i - 1)))
-                       return 0;
-       }
-       if (found_sob == i)
+       if (found_sob_last)
                return 3;
        if (found_sob)
                return 2;
@@@ -248,7 -201,7 +201,7 @@@ static int write_message(const void *bu
        }
        if (append_eol && write(msg_fd, "\n", 1) < 0) {
                rollback_lock_file(&msg_file);
 -              return error_errno(_("could not write eol to '%s"), filename);
 +              return error_errno(_("could not write eol to '%s'"), filename);
        }
        if (commit_lock_file(&msg_file) < 0) {
                rollback_lock_file(&msg_file);
@@@ -629,7 -582,7 +582,7 @@@ static const char *todo_command_strings
  
  static const char *command_to_string(const enum todo_command command)
  {
 -      if (command < ARRAY_SIZE(todo_command_strings))
 +      if ((size_t)command < ARRAY_SIZE(todo_command_strings))
                return todo_command_strings[command];
        die("Unknown command: %d", command);
  }