Merge pull request #36 from tsibley/reload-diff-view-on-toggle
[tig.git] / test-graph.c
blob48650e65ff725160e8188b18a9de53ac5392bd1e
1 /* Copyright (c) 2006-2010 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.
13 * Example usage:
15 * # git log --pretty=raw --parents | ./test-graph
16 * # git log --pretty=raw --parents | ./test-graph --ascii
19 #include "tig.h"
20 #include "io.h"
21 #include "graph.h"
23 static void __NORETURN
24 die(const char *err, ...)
26 va_list args;
28 endwin();
30 va_start(args, err);
31 fputs("tig: ", stderr);
32 vfprintf(stderr, err, args);
33 fputs("\n", stderr);
34 va_end(args);
36 exit(1);
39 struct commit {
40 char id[SIZEOF_REV];
41 struct graph_canvas canvas;
44 DEFINE_ALLOCATOR(realloc_commits, struct commit *, 8)
46 int
47 main(int argc, const char *argv[])
49 struct graph graph = { };
50 struct io io = { };
51 char *line;
52 struct commit **commits = NULL;
53 size_t ncommits = 0;
54 struct commit *commit = NULL;
55 bool is_boundary;
56 const char *(*graph_fn)(struct graph_symbol *) = graph_symbol_to_utf8;
58 if (argc > 1 && !strcmp(argv[1], "--ascii"))
59 graph_fn = graph_symbol_to_ascii;
61 if (!io_open(&io, ""))
62 die("IO");
64 while (!io_eof(&io)) {
65 bool can_read = io_can_read(&io, TRUE);
67 for (; (line = io_get(&io, '\n', can_read)); can_read = FALSE) {
68 if (!prefixcmp(line, "commit ")) {
69 line += STRING_SIZE("commit ");
70 is_boundary = *line == '-';
72 if (is_boundary)
73 line++;
75 if (!realloc_commits(&commits, ncommits, 1))
76 die("Commits");
78 commit = calloc(1, sizeof(*commit));
79 if (!commit)
80 die("Commit");
81 commits[ncommits++] = commit;
82 string_copy_rev(commit->id, line);
83 graph_add_commit(&graph, &commit->canvas, commit->id, line, is_boundary);
84 graph_render_parents(&graph);
86 } else if (!prefixcmp(line, " ")) {
87 int i;
89 if (!commit)
90 continue;
92 for (i = 0; i < commit->canvas.size; i++) {
93 struct graph_symbol *symbol = &commit->canvas.symbols[i];
94 const char *chars = graph_fn(symbol);
96 printf("%s", chars + (i == 0));
98 printf("%s\n", line + 3);
100 commit = NULL;
105 return 0;