Ignore 'gui.encoding' and 'i18n.commitencoding' when set to 'UTF-8'
[tig.git] / test / test-graph.c
blob9a775723b5ad77dc442c26f73f527ff90b136da1
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/tig.h"
15 #include "tig/util.h"
16 #include "tig/io.h"
17 #include "tig/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 struct buffer buf;
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 (; io_get(&io, &buf, '\n', can_read); can_read = FALSE) {
59 char *line = buf.data;
61 if (!prefixcmp(line, "commit ")) {
62 line += STRING_SIZE("commit ");
63 is_boundary = *line == '-';
65 if (is_boundary)
66 line++;
68 if (!realloc_commits(&commits, ncommits, 1))
69 die("Commits");
71 commit = calloc(1, sizeof(*commit));
72 if (!commit)
73 die("Commit");
74 commits[ncommits++] = commit;
75 string_copy_rev(commit->id, line);
76 graph_add_commit(&graph, &commit->canvas, commit->id, line, is_boundary);
77 graph_render_parents(&graph);
79 } else if (!prefixcmp(line, " ")) {
80 int i;
82 if (!commit)
83 continue;
85 for (i = 0; i < commit->canvas.size; i++) {
86 struct graph_symbol *symbol = &commit->canvas.symbols[i];
87 const char *chars = graph_fn(symbol);
89 printf("%s", chars + (i == 0));
91 printf("%s\n", line + 3);
93 commit = NULL;
98 return 0;
101 /* vim: set ts=8 sw=8 noexpandtab: */