main: add toggle for commit title overflow
[tig.git] / test-graph.c
blob28cbcb6960c3c002c25607c33961d397185043b7
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.
14 #include "tig.h"
15 #include "io.h"
16 #include "graph.h"
18 #define USAGE \
19 "test-graph [--ascii]\n" \
20 "\n" \
21 "Example usage:\n" \
22 " # git log --pretty=raw --parents | ./test-graph\n" \
23 " # git log --pretty=raw --parents | ./test-graph --ascii"
25 static void TIG_NORETURN
26 die(const char *err, ...)
28 va_list args;
30 endwin();
32 va_start(args, err);
33 fputs("test-graph: ", stderr);
34 vfprintf(stderr, err, args);
35 fputs("\n", stderr);
36 va_end(args);
38 exit(1);
41 struct commit {
42 char id[SIZEOF_REV];
43 struct graph_canvas canvas;
46 DEFINE_ALLOCATOR(realloc_commits, struct commit *, 8)
48 int
49 main(int argc, const char *argv[])
51 struct graph graph = { };
52 struct io io = { };
53 char *line;
54 struct commit **commits = NULL;
55 size_t ncommits = 0;
56 struct commit *commit = NULL;
57 bool is_boundary;
58 const char *(*graph_fn)(struct graph_symbol *) = graph_symbol_to_utf8;
60 if (argc > 1 && !strcmp(argv[1], "--ascii"))
61 graph_fn = graph_symbol_to_ascii;
63 if (isatty(STDIN_FILENO)) {
64 die(USAGE);
67 if (!io_open(&io, "%s", ""))
68 die("IO");
70 while (!io_eof(&io)) {
71 bool can_read = io_can_read(&io, TRUE);
73 for (; (line = io_get(&io, '\n', can_read)); can_read = FALSE) {
74 if (!prefixcmp(line, "commit ")) {
75 line += STRING_SIZE("commit ");
76 is_boundary = *line == '-';
78 if (is_boundary)
79 line++;
81 if (!realloc_commits(&commits, ncommits, 1))
82 die("Commits");
84 commit = calloc(1, sizeof(*commit));
85 if (!commit)
86 die("Commit");
87 commits[ncommits++] = commit;
88 string_copy_rev(commit->id, line);
89 graph_add_commit(&graph, &commit->canvas, commit->id, line, is_boundary);
90 graph_render_parents(&graph);
92 } else if (!prefixcmp(line, " ")) {
93 int i;
95 if (!commit)
96 continue;
98 for (i = 0; i < commit->canvas.size; i++) {
99 struct graph_symbol *symbol = &commit->canvas.symbols[i];
100 const char *chars = graph_fn(symbol);
102 printf("%s", chars + (i == 0));
104 printf("%s\n", line + 3);
106 commit = NULL;
111 return 0;
114 /* vim: set ts=8 sw=8 noexpandtab: */