Imported Upstream version 2.10.3
[platform/upstream/git.git] / graph.c
diff --git a/graph.c b/graph.c
index 391a712..4200f74 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -8,34 +8,6 @@
 /* Internal API */
 
 /*
- * Output the next line for a graph.
- * This formats the next graph line into the specified strbuf.  It is not
- * terminated with a newline.
- *
- * Returns 1 if the line includes the current commit, and 0 otherwise.
- * graph_next_line() will return 1 exactly once for each time
- * graph_update() is called.
- */
-static int graph_next_line(struct git_graph *graph, struct strbuf *sb);
-
-/*
- * Set up a custom scheme for column colors.
- *
- * The default column color scheme inserts ANSI color escapes to colorize
- * the graph. The various color escapes are stored in an array of strings
- * where each entry corresponds to a color, except for the last entry,
- * which denotes the escape for resetting the color back to the default.
- * When generating the graph, strings from this array are inserted before
- * and after the various column characters.
- *
- * This function allows you to enable a custom array of color escapes.
- * The 'colors_max' argument is the index of the last "reset" entry.
- *
- * This functions must be called BEFORE graph_init() is called.
- */
-static void graph_set_column_colors(const char **colors, unsigned short colors_max);
-
-/*
  * Output a padding line in the graph.
  * This is similar to graph_next_line().  However, it is guaranteed to
  * never print the current commit line.  Instead, if the commit line is
@@ -45,8 +17,8 @@ static void graph_set_column_colors(const char **colors, unsigned short colors_m
 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
 
 /*
- * Print a strbuf to stdout.  If the graph is non-NULL, all lines but the
- * first will be prefixed with the graph output.
+ * Print a strbuf.  If the graph is non-NULL, all lines but the first will be
+ * prefixed with the graph output.
  *
  * If the strbuf ends with a newline, the output will end after this
  * newline.  A new graph line will not be printed after the final newline.
@@ -90,7 +62,7 @@ enum graph_state {
 static const char **column_colors;
 static unsigned short column_colors_max;
 
-static void graph_set_column_colors(const char **colors, unsigned short colors_max)
+void graph_set_column_colors(const char **colors, unsigned short colors_max)
 {
        column_colors = colors;
        column_colors_max = colors_max;
@@ -262,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)
         * We'll automatically grow columns later if we need more room.
         */
        graph->column_capacity = 30;
-       graph->columns = xmalloc(sizeof(struct column) *
-                                graph->column_capacity);
-       graph->new_columns = xmalloc(sizeof(struct column) *
-                                    graph->column_capacity);
-       graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
-       graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
+       ALLOC_ARRAY(graph->columns, graph->column_capacity);
+       ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
+       ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
+       ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);
 
        /*
         * The diff output prefix callback, with this we can make
@@ -295,16 +265,10 @@ static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
                graph->column_capacity *= 2;
        } while (graph->column_capacity < num_columns);
 
-       graph->columns = xrealloc(graph->columns,
-                                 sizeof(struct column) *
-                                 graph->column_capacity);
-       graph->new_columns = xrealloc(graph->new_columns,
-                                     sizeof(struct column) *
-                                     graph->column_capacity);
-       graph->mapping = xrealloc(graph->mapping,
-                                 sizeof(int) * 2 * graph->column_capacity);
-       graph->new_mapping = xrealloc(graph->new_mapping,
-                                     sizeof(int) * 2 * graph->column_capacity);
+       REALLOC_ARRAY(graph->columns, graph->column_capacity);
+       REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
+       REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
+       REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
 }
 
 /*
@@ -705,6 +669,13 @@ static void graph_output_padding_line(struct git_graph *graph,
        graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
 }
 
+
+int graph_width(struct git_graph *graph)
+{
+       return graph->width;
+}
+
+
 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
 {
        /*
@@ -829,10 +800,10 @@ static int graph_draw_octopus_merge(struct git_graph *graph,
        int num_dashes =
                ((graph->num_parents - dashless_commits) * 2) - 1;
        for (i = 0; i < num_dashes; i++) {
-               col_num = (i / 2) + dashless_commits;
+               col_num = (i / 2) + dashless_commits + graph->commit_index;
                strbuf_write_column(sb, &graph->new_columns[col_num], '-');
        }
-       col_num = (i / 2) + dashless_commits;
+       col_num = (i / 2) + dashless_commits + graph->commit_index;
        strbuf_write_column(sb, &graph->new_columns[col_num], '.');
        return num_dashes + 1;
 }
@@ -1144,7 +1115,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf
                graph_update_state(graph, GRAPH_PADDING);
 }
 
-static int graph_next_line(struct git_graph *graph, struct strbuf *sb)
+int graph_next_line(struct git_graph *graph, struct strbuf *sb)
 {
        switch (graph->state) {
        case GRAPH_PADDING:
@@ -1173,7 +1144,8 @@ static int graph_next_line(struct git_graph *graph, struct strbuf *sb)
 
 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
 {
-       int i, j;
+       int i;
+       int chars_written = 0;
 
        if (graph->state != GRAPH_COMMIT) {
                graph_next_line(graph, sb);
@@ -1189,24 +1161,21 @@ static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
         */
        for (i = 0; i < graph->num_columns; i++) {
                struct column *col = &graph->columns[i];
-               struct commit *col_commit = col->commit;
-               if (col_commit == graph->commit) {
-                       strbuf_write_column(sb, col, '|');
 
-                       if (graph->num_parents < 3)
-                               strbuf_addch(sb, ' ');
-                       else {
-                               int num_spaces = ((graph->num_parents - 2) * 2);
-                               for (j = 0; j < num_spaces; j++)
-                                       strbuf_addch(sb, ' ');
-                       }
+               strbuf_write_column(sb, col, '|');
+               chars_written++;
+
+               if (col->commit == graph->commit && graph->num_parents > 2) {
+                       int len = (graph->num_parents - 2) * 2;
+                       strbuf_addchars(sb, ' ', len);
+                       chars_written += len;
                } else {
-                       strbuf_write_column(sb, col, '|');
                        strbuf_addch(sb, ' ');
+                       chars_written++;
                }
        }
 
-       graph_pad_horizontally(graph, sb, graph->num_columns);
+       graph_pad_horizontally(graph, sb, chars_written);
 
        /*
         * Update graph->prev_state since we have output a padding line
@@ -1227,11 +1196,22 @@ void graph_show_commit(struct git_graph *graph)
        if (!graph)
                return;
 
+       /*
+        * When showing a diff of a merge against each of its parents, we
+        * are called once for each parent without graph_update having been
+        * called.  In this case, simply output a single padding line.
+        */
+       if (graph_is_commit_finished(graph)) {
+               graph_show_padding(graph);
+               shown_commit_line = 1;
+       }
+
        while (!shown_commit_line && !graph_is_commit_finished(graph)) {
                shown_commit_line = graph_next_line(graph, &msgbuf);
-               fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
+               fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
+                       graph->revs->diffopt.file);
                if (!shown_commit_line)
-                       putchar('\n');
+                       putc('\n', graph->revs->diffopt.file);
                strbuf_setlen(&msgbuf, 0);
        }
 
@@ -1246,7 +1226,7 @@ void graph_show_oneline(struct git_graph *graph)
                return;
 
        graph_next_line(graph, &msgbuf);
-       fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
+       fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
        strbuf_release(&msgbuf);
 }
 
@@ -1258,7 +1238,7 @@ void graph_show_padding(struct git_graph *graph)
                return;
 
        graph_padding_line(graph, &msgbuf);
-       fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
+       fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
        strbuf_release(&msgbuf);
 }
 
@@ -1275,12 +1255,13 @@ int graph_show_remainder(struct git_graph *graph)
 
        for (;;) {
                graph_next_line(graph, &msgbuf);
-               fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
+               fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
+                       graph->revs->diffopt.file);
                strbuf_setlen(&msgbuf, 0);
                shown = 1;
 
                if (!graph_is_commit_finished(graph))
-                       putchar('\n');
+                       putc('\n', graph->revs->diffopt.file);
                else
                        break;
        }
@@ -1295,7 +1276,8 @@ static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
        char *p;
 
        if (!graph) {
-               fwrite(sb->buf, sizeof(char), sb->len, stdout);
+               fwrite(sb->buf, sizeof(char), sb->len,
+                       graph->revs->diffopt.file);
                return;
        }
 
@@ -1313,7 +1295,7 @@ static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
                } else {
                        len = (sb->buf + sb->len) - p;
                }
-               fwrite(p, sizeof(char), len, stdout);
+               fwrite(p, sizeof(char), len, graph->revs->diffopt.file);
                if (next_p && *next_p != '\0')
                        graph_show_oneline(graph);
                p = next_p;
@@ -1333,7 +1315,8 @@ void graph_show_commit_msg(struct git_graph *graph,
                 * CMIT_FMT_USERFORMAT are already missing a terminating
                 * newline.  All of the other formats should have it.
                 */
-               fwrite(sb->buf, sizeof(char), sb->len, stdout);
+               fwrite(sb->buf, sizeof(char), sb->len,
+                       graph->revs->diffopt.file);
                return;
        }
 
@@ -1354,7 +1337,7 @@ void graph_show_commit_msg(struct git_graph *graph,
                 * new line.
                 */
                if (!newline_terminated)
-                       putchar('\n');
+                       putc('\n', graph->revs->diffopt.file);
 
                graph_show_remainder(graph);
 
@@ -1362,6 +1345,6 @@ void graph_show_commit_msg(struct git_graph *graph,
                 * If sb ends with a newline, our output should too.
                 */
                if (newline_terminated)
-                       putchar('\n');
+                       putc('\n', graph->revs->diffopt.file);
        }
 }