1 /* Copyright (c) 2006-2013 Jonas Fonseca <fonseca@diku.dk>
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
*) = graph_symbol_to_utf8
;
36 print_symbol(void *__
, 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 commit
*commit
, const char *title
)
47 graph_foreach_symbol(&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 (argc
> 1 && !strcmp(argv
[1], "--ascii"))
63 graph_fn
= graph_symbol_to_ascii
;
65 if (isatty(STDIN_FILENO
)) {
69 if (!(graph
= init_graph()))
70 die("Failed to allocated graph");
71 if (!io_open(&io
, "%s", ""))
74 while (!io_eof(&io
)) {
75 for (; io_get(&io
, &buf
, '\n', TRUE
); ) {
76 char *line
= buf
.data
;
78 if (!prefixcmp(line
, "commit ")) {
79 line
+= STRING_SIZE("commit ");
80 is_boundary
= *line
== '-';
85 if (!realloc_commits(&commits
, ncommits
, 1))
88 commit
= calloc(1, sizeof(*commit
));
91 commits
[ncommits
++] = commit
;
92 string_copy_rev(commit
->id
, line
);
93 graph_add_commit(graph
, &commit
->canvas
, commit
->id
, line
, is_boundary
);
94 graph_render_parents(graph
, &commit
->canvas
);
96 if ((line
= io_memchr(&buf
, line
, 0))) {
97 print_commit(commit
, line
);
101 } else if (!prefixcmp(line
, " ")) {
106 print_commit(commit
, line
+ 4);
116 /* vim: set ts=8 sw=8 noexpandtab: */