Imported Upstream version 2.10.3
[platform/upstream/git.git] / graph.c
diff --git a/graph.c b/graph.c
index 6404331..4200f74 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -17,8 +17,8 @@
 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.
@@ -234,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
@@ -267,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);
 }
 
 /*
@@ -677,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)
 {
        /*
@@ -1145,7 +1144,8 @@ 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);
@@ -1161,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
@@ -1211,9 +1208,10 @@ void graph_show_commit(struct git_graph *graph)
 
        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);
        }
 
@@ -1228,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);
 }
 
@@ -1240,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);
 }
 
@@ -1257,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;
        }
@@ -1277,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;
        }
 
@@ -1295,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;
@@ -1315,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;
        }
 
@@ -1336,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);
 
@@ -1344,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);
        }
 }