1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
17 #include "tig/graph.h"
20 "test-graph [--ascii]\n" \
23 " # git log --pretty=raw --parents | ./test-graph\n" \
24 " # git log --pretty=raw --parents | ./test-graph --ascii"
28 struct graph_canvas canvas
;
31 DEFINE_ALLOCATOR(realloc_commits
, struct commit
*, 8)
33 static const char *(*graph_fn
)(const struct graph_symbol
*);
36 print_symbol(void *__
, const struct graph
*graph
, const struct graph_symbol
*symbol
, int color_id
, bool first
)
38 const char *chars
= graph_fn(symbol
);
40 printf("%s", chars
+ !!first
);
45 print_commit(struct graph
*graph
, struct commit
*commit
, const char *title
)
47 graph
->foreach_symbol(graph
, &commit
->canvas
, print_symbol
, NULL
);
48 printf(" %s\n", title
);
52 main(int argc
, const char *argv
[])
57 struct commit
**commits
= NULL
;
59 struct commit
*commit
= NULL
;
62 if (isatty(STDIN_FILENO
)) {
66 if (!(graph
= init_graph(GRAPH_DISPLAY_V2
)))
67 die("Failed to allocated graph");
69 if (argc
> 1 && !strcmp(argv
[1], "--ascii"))
70 graph_fn
= graph
->symbol_to_ascii
;
72 graph_fn
= graph
->symbol_to_utf8
;
74 if (!io_open(&io
, "%s", ""))
77 while (!io_eof(&io
)) {
78 for (; io_get(&io
, &buf
, '\n', TRUE
); ) {
79 char *line
= buf
.data
;
81 if (!prefixcmp(line
, "commit ")) {
82 line
+= STRING_SIZE("commit ");
83 is_boundary
= *line
== '-';
88 if (!realloc_commits(&commits
, ncommits
, 1))
91 commit
= calloc(1, sizeof(*commit
));
94 commits
[ncommits
++] = commit
;
95 string_copy_rev(commit
->id
, line
);
96 graph
->add_commit(graph
, &commit
->canvas
, commit
->id
, line
, is_boundary
);
97 graph
->render_parents(graph
, &commit
->canvas
);
99 if ((line
= io_memchr(&buf
, line
, 0))) {
100 print_commit(graph
, commit
, line
);
104 } else if (!prefixcmp(line
, " ")) {
109 print_commit(graph
, commit
, line
+ 4);
119 /* vim: set ts=8 sw=8 noexpandtab: */