Simplify subcommand option parsing by moving it out of the loop
[tig] / tig.c
diff --git a/tig.c b/tig.c
index b3a4e8d..f8132fd 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -116,7 +116,7 @@ static size_t utf8_length(const char *string, size_t max_width, int *trimmed, bo
        "git log --no-color --cc --stat -n100 %s 2>/dev/null"
 
 #define TIG_MAIN_CMD \
-       "git log --no-color --topo-order --boundary --pretty=raw %s 2>/dev/null"
+       "git log --no-color --topo-order --parents --boundary --pretty=raw %s 2>/dev/null"
 
 #define TIG_TREE_CMD   \
        "git ls-tree %s %s"
@@ -496,32 +496,33 @@ parse_options(int argc, char *argv[])
 {
        char *altargv[1024];
        int altargc = 0;
-       char *subcommand = NULL;
+       char *subcommand;
        int i;
 
-       for (i = 1; i < argc; i++) {
-               char *opt = argv[i];
+       if (argc <= 1)
+               return TRUE;
 
-               if (!strcmp(opt, "log") ||
-                   !strcmp(opt, "diff")) {
-                       subcommand = opt;
-                       opt_request = opt[0] == 'l'
-                                   ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
-                       warn("`tig %s' has been deprecated", opt);
-                       break;
-               }
+       subcommand = argv[1];
+       if (!strcmp(subcommand, "status")) {
+               opt_request = REQ_VIEW_STATUS;
+               if (argc > 2)
+                       warn("ignoring arguments after `%s'", subcommand);
+               return TRUE;
 
-               if (!strcmp(opt, "show")) {
-                       subcommand = opt;
-                       opt_request = REQ_VIEW_DIFF;
-                       break;
-               }
+       } else if (!strcmp(subcommand, "show")) {
+               opt_request = REQ_VIEW_DIFF;
 
-               if (!strcmp(opt, "status")) {
-                       subcommand = opt;
-                       opt_request = REQ_VIEW_STATUS;
-                       break;
-               }
+       } else if (!strcmp(subcommand, "log") || !strcmp(subcommand, "diff")) {
+               opt_request = subcommand[0] == 'l'
+                           ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
+               warn("`tig %s' has been deprecated", subcommand);
+
+       } else {
+               subcommand = NULL;
+       }
+
+       for (i = 1 + !!subcommand; i < argc; i++) {
+               char *opt = argv[i];
 
                if (opt[0] && opt[0] != '-')
                        break;
@@ -565,18 +566,10 @@ parse_options(int argc, char *argv[])
                warn("`%s' has been deprecated", opt);
        }
 
-       /* Check that no 'alt' arguments occured before a subcommand. */
-       if (subcommand && i < argc && altargc > 0)
-               die("unknown arguments before `%s'", argv[i]);
-
        if (!isatty(STDIN_FILENO)) {
                opt_request = REQ_VIEW_PAGER;
                opt_pipe = stdin;
 
-       } else if (opt_request == REQ_VIEW_STATUS) {
-               if (argc - i > 1)
-                       warn("ignoring arguments after `%s'", argv[i]);
-
        } else if (i < argc || altargc > 0) {
                int alti = 0;
                size_t buf_size;
@@ -584,9 +577,9 @@ parse_options(int argc, char *argv[])
                if (opt_request == REQ_VIEW_MAIN)
                        /* XXX: This is vulnerable to the user overriding
                         * options required for the main view parser. */
-                       string_copy(opt_cmd, "git log --no-color --pretty=raw --boundary");
+                       string_copy(opt_cmd, "git log --no-color --pretty=raw --boundary --parents");
                else
-                       string_copy(opt_cmd, "git");
+                       string_format(opt_cmd, "git %s", subcommand);
                buf_size = strlen(opt_cmd);
 
                while (buf_size < sizeof(opt_cmd) && alti < altargc) {
@@ -605,9 +598,6 @@ parse_options(int argc, char *argv[])
                opt_cmd[buf_size] = 0;
        }
 
-       if (*opt_encoding && strcasecmp(opt_encoding, "UTF-8"))
-               opt_utf8 = FALSE;
-
        return TRUE;
 }
 
@@ -1493,7 +1483,7 @@ static struct view views[] = {
        (view == display[0] || view == display[1])
 
 static int
-draw_text(struct view *view, const char *string, int max_len, int col,
+draw_text(struct view *view, const char *string, int max_len,
          bool use_tilde, int tilde_attr)
 {
        int len = 0;
@@ -2750,7 +2740,7 @@ pager_draw(struct view *view, struct line *line, unsigned int lineno, bool selec
        } else {
                int tilde_attr = get_line_attr(LINE_MAIN_DELIM);
 
-               draw_text(view, text, view->width, 0, TRUE, tilde_attr);
+               draw_text(view, text, view->width, TRUE, tilde_attr);
        }
 
        return TRUE;
@@ -3531,7 +3521,7 @@ status_draw(struct view *view, struct line *line, unsigned int lineno, bool sele
                        return FALSE;
                }
 
-               draw_text(view, text, view->width, 0, TRUE, tilde_attr);
+               draw_text(view, text, view->width, TRUE, tilde_attr);
                return TRUE;
        }
 
@@ -3542,7 +3532,7 @@ status_draw(struct view *view, struct line *line, unsigned int lineno, bool sele
        if (view->width < 5)
                return TRUE;
 
-       draw_text(view, status->new.name, view->width - 5, 5, TRUE, tilde_attr);
+       draw_text(view, status->new.name, view->width - 5, TRUE, tilde_attr);
        return TRUE;
 }
 
@@ -4019,6 +4009,7 @@ struct commit {
        struct ref **refs;              /* Repository references. */
        chtype graph[SIZEOF_REVGRAPH];  /* Ancestry chain graphics. */
        size_t graph_size;              /* The width of the graph array. */
+       bool has_parents;               /* Rewritten --parents seen. */
 };
 
 /* Size of rev graph with no  "padding" columns */
@@ -4248,11 +4239,8 @@ main_draw(struct view *view, struct line *line, unsigned int lineno, bool select
                int n;
 
                timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
-               n = draw_text(
-                       view, buf, view->width - col, col, FALSE, tilde_attr);
-               draw_text(
-                       view, " ", view->width - col - n, col + n, FALSE,
-                       tilde_attr);
+               n = draw_text(view, buf, view->width - col, FALSE, tilde_attr);
+               draw_text(view, " ", view->width - col - n, FALSE, tilde_attr);
 
                col += DATE_COLS;
                wmove(view->win, lineno, col);
@@ -4268,8 +4256,7 @@ main_draw(struct view *view, struct line *line, unsigned int lineno, bool select
                max_len = view->width - col;
                if (max_len > AUTHOR_COLS - 1)
                        max_len = AUTHOR_COLS - 1;
-               draw_text(
-                       view, commit->author, max_len, col, TRUE, tilde_attr);
+               draw_text(view, commit->author, max_len, TRUE, tilde_attr);
                col += AUTHOR_COLS;
                if (col >= view->width)
                        return TRUE;
@@ -4314,20 +4301,13 @@ main_draw(struct view *view, struct line *line, unsigned int lineno, bool select
                        else
                                wattrset(view->win, get_line_attr(LINE_MAIN_REF));
 
-                       col += draw_text(
-                               view, "[", view->width - col, col, TRUE,
-                               tilde_attr);
-                       col += draw_text(
-                               view, commit->refs[i]->name, view->width - col,
-                               col, TRUE, tilde_attr);
-                       col += draw_text(
-                               view, "]", view->width - col, col, TRUE,
-                               tilde_attr);
+                       col += draw_text(view, "[", view->width - col, TRUE, tilde_attr);
+                       col += draw_text(view, commit->refs[i]->name, view->width - col,
+                                        TRUE, tilde_attr);
+                       col += draw_text(view, "]", view->width - col, TRUE, tilde_attr);
                        if (type != LINE_CURSOR)
                                wattrset(view->win, A_NORMAL);
-                       col += draw_text(
-                               view, " ", view->width - col, col, TRUE,
-                               tilde_attr);
+                       col += draw_text(view, " ", view->width - col, TRUE, tilde_attr);
                        if (col >= view->width)
                                return TRUE;
                } while (commit->refs[i++]->next);
@@ -4336,9 +4316,7 @@ main_draw(struct view *view, struct line *line, unsigned int lineno, bool select
        if (type != LINE_CURSOR)
                wattrset(view->win, get_line_attr(type));
 
-       col += draw_text(
-               view, commit->title, view->width - col, col, TRUE, tilde_attr);
-
+       draw_text(view, commit->title, view->width - col, TRUE, tilde_attr);
        return TRUE;
 }
 
@@ -4371,6 +4349,12 @@ main_read(struct view *view, char *line)
                commit->refs = get_refs(commit->id);
                graph->commit = commit;
                add_line_data(view, commit, LINE_MAIN_COMMIT);
+
+               while ((line = strchr(line, ' '))) {
+                       line++;
+                       push_rev_graph(graph->parents, line);
+                       commit->has_parents = TRUE;
+               }
                return TRUE;
        }
 
@@ -4380,6 +4364,8 @@ main_read(struct view *view, char *line)
 
        switch (type) {
        case LINE_PARENT:
+               if (commit->has_parents)
+                       break;
                push_rev_graph(graph->parents, line + STRING_SIZE("parent "));
                break;
 
@@ -5148,6 +5134,9 @@ main(int argc, char *argv[])
        if (!opt_git_dir[0])
                die("Not a git repository");
 
+       if (*opt_encoding && strcasecmp(opt_encoding, "UTF-8"))
+               opt_utf8 = FALSE;
+
        if (*opt_codeset && strcmp(opt_codeset, opt_encoding)) {
                opt_iconv = iconv_open(opt_codeset, opt_encoding);
                if (opt_iconv == ICONV_NONE)