Move refs helpers to refs module
[tig.git] / test / test-graph.c
blobbd59a3de9bb1cf94855a373d53531c0bf9c73d0b
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 "util.h"
16 #include "io.h"
17 #include "graph.h"
19 #define USAGE \
20 "test-graph [--ascii]\n" \
21 "\n" \
22 "Example usage:\n" \
23 " # git log --pretty=raw --parents | ./test-graph\n" \
24 " # git log --pretty=raw --parents | ./test-graph --ascii"
26 struct commit {
27 char id[SIZEOF_REV];
28 struct graph_canvas canvas;
31 DEFINE_ALLOCATOR(realloc_commits, struct commit *, 8)
33 int
34 main(int argc, const char *argv[])
36 struct graph graph = { };
37 struct io io = { };
38 char *line;
39 struct commit **commits = NULL;
40 size_t ncommits = 0;
41 struct commit *commit = NULL;
42 bool is_boundary;
43 const char *(*graph_fn)(struct graph_symbol *) = graph_symbol_to_utf8;
45 if (argc > 1 && !strcmp(argv[1], "--ascii"))
46 graph_fn = graph_symbol_to_ascii;
48 if (isatty(STDIN_FILENO)) {
49 die(USAGE);
52 if (!io_open(&io, "%s", ""))
53 die("IO");
55 while (!io_eof(&io)) {
56 bool can_read = io_can_read(&io, TRUE);
58 for (; (line = io_get(&io, '\n', can_read)); can_read = FALSE) {
59 if (!prefixcmp(line, "commit ")) {
60 line += STRING_SIZE("commit ");
61 is_boundary = *line == '-';
63 if (is_boundary)
64 line++;
66 if (!realloc_commits(&commits, ncommits, 1))
67 die("Commits");
69 commit = calloc(1, sizeof(*commit));
70 if (!commit)
71 die("Commit");
72 commits[ncommits++] = commit;
73 string_copy_rev(commit->id, line);
74 graph_add_commit(&graph, &commit->canvas, commit->id, line, is_boundary);
75 graph_render_parents(&graph);
77 } else if (!prefixcmp(line, " ")) {
78 int i;
80 if (!commit)
81 continue;
83 for (i = 0; i < commit->canvas.size; i++) {
84 struct graph_symbol *symbol = &commit->canvas.symbols[i];
85 const char *chars = graph_fn(symbol);
87 printf("%s", chars + (i == 0));
89 printf("%s\n", line + 3);
91 commit = NULL;
96 return 0;
99 /* vim: set ts=8 sw=8 noexpandtab: */