Merge branch 'mk/diff'
[gitweb.git] / builtin-shortlog.c
index 2f71a2a6e2ecf556956a0271d8af9ad852d5a93c..3f93498bb711ccaf1f8a1ad5ed9ac8e02958d357 100644 (file)
@@ -4,6 +4,7 @@
 #include "diff.h"
 #include "path-list.h"
 #include "revision.h"
+#include "utf8.h"
 
 static const char shortlog_usage[] =
 "git-shortlog [-n] [-s] [<commit-id>... ]";
@@ -217,13 +218,13 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)
 
        prepare_revision_walk(rev);
        while ((commit = get_revision(rev)) != NULL) {
-               char *author = NULL, *oneline, *buffer;
+               const char *author = NULL, *oneline, *buffer;
                int authorlen = authorlen, onelinelen;
 
                /* get author and oneline */
                for (buffer = commit->buffer; buffer && *buffer != '\0' &&
                                *buffer != '\n'; ) {
-                       char *eol = strchr(buffer, '\n');
+                       const char *eol = strchr(buffer, '\n');
 
                        if (eol == NULL)
                                eol = buffer + strlen(buffer);
@@ -276,11 +277,64 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)
 
 }
 
+static int parse_uint(char const **arg, int comma)
+{
+       unsigned long ul;
+       int ret;
+       char *endp;
+
+       ul = strtoul(*arg, &endp, 10);
+       if (endp != *arg && *endp && *endp != comma)
+               return -1;
+       ret = (int) ul;
+       if (ret != ul)
+               return -1;
+       *arg = endp;
+       if (**arg)
+               (*arg)++;
+       return ret;
+}
+
+static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
+#define DEFAULT_WRAPLEN 76
+#define DEFAULT_INDENT1 6
+#define DEFAULT_INDENT2 9
+
+static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
+{
+       arg += 2; /* skip -w */
+
+       *wrap = parse_uint(&arg, ',');
+       if (*wrap < 0)
+               die(wrap_arg_usage);
+       *in1 = parse_uint(&arg, ',');
+       if (*in1 < 0)
+               die(wrap_arg_usage);
+       *in2 = parse_uint(&arg, '\0');
+       if (*in2 < 0)
+               die(wrap_arg_usage);
+
+       if (!*wrap)
+               *wrap = DEFAULT_WRAPLEN;
+       if (!*in1)
+               *in1 = DEFAULT_INDENT1;
+       if (!*in2)
+               *in2 = DEFAULT_INDENT2;
+       if (*wrap &&
+           ((*in1 && *wrap <= *in1) ||
+            (*in2 && *wrap <= *in2)))
+               die(wrap_arg_usage);
+}
+
 int cmd_shortlog(int argc, const char **argv, const char *prefix)
 {
        struct rev_info rev;
        struct path_list list = { NULL, 0, 0, 1 };
        int i, j, sort_by_number = 0, summary = 0;
+       int wrap_lines = 0;
+       int wrap = DEFAULT_WRAPLEN;
+       int in1 = DEFAULT_INDENT1;
+       int in2 = DEFAULT_INDENT2;
 
        /* since -n is a shadowed rev argument, parse our args first */
        while (argc > 1) {
@@ -289,6 +343,10 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
                else if (!strcmp(argv[1], "-s") ||
                                !strcmp(argv[1], "--summary"))
                        summary = 1;
+               else if (!prefixcmp(argv[1], "-w")) {
+                       wrap_lines = 1;
+                       parse_wrap_args(argv[1], &in1, &in2, &wrap);
+               }
                else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
                        usage(shortlog_usage);
                else
@@ -304,8 +362,11 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
        if (!access(".mailmap", R_OK))
                read_mailmap(".mailmap");
 
-       if (rev.pending.nr == 0)
+       if (rev.pending.nr == 0) {
+               if (isatty(0))
+                       fprintf(stderr, "(reading log to summarize from standard input)\n");
                read_from_stdin(&list);
+       }
        else
                get_from_rev(&rev, &list);
 
@@ -320,9 +381,18 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
                        printf("%s: %d\n", list.items[i].path, onelines->nr);
                } else {
                        printf("%s (%d):\n", list.items[i].path, onelines->nr);
-                       for (j = onelines->nr - 1; j >= 0; j--)
-                               printf("      %s\n", onelines->items[j].path);
-                       printf("\n");
+                       for (j = onelines->nr - 1; j >= 0; j--) {
+                               const char *msg = onelines->items[j].path;
+
+                               if (wrap_lines) {
+                                       int col = print_wrapped_text(msg, in1, in2, wrap);
+                                       if (col != wrap)
+                                               putchar('\n');
+                               }
+                               else
+                                       printf("      %s\n", msg);
+                       }
+                       putchar('\n');
                }
 
                onelines->strdup_paths = 1;