File: test-graph.c

package info (click to toggle)
tig 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,104 kB
  • sloc: ansic: 7,783; sh: 280; makefile: 223
file content (106 lines) | stat: -rw-r--r-- 2,461 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Copyright (c) 2006-2010 Jonas Fonseca <fonseca@diku.dk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Example usage:
 *
 *	# git log --pretty=raw --parents | ./test-graph
 *	# git log --pretty=raw --parents | ./test-graph --ascii
 */

#include "tig.h"
#include "io.h"
#include "graph.h"

static void __NORETURN
die(const char *err, ...)
{
	va_list args;

	endwin();

	va_start(args, err);
	fputs("tig: ", stderr);
	vfprintf(stderr, err, args);
	fputs("\n", stderr);
	va_end(args);

	exit(1);
}

struct commit {
	char id[SIZEOF_REV];
	struct graph_canvas canvas;
};

DEFINE_ALLOCATOR(realloc_commits, struct commit *, 8)

int
main(int argc, const char *argv[])
{
	struct graph graph = { };
	struct io io = { };
	char *line;
	struct commit **commits = NULL;
	size_t ncommits = 0;
	struct commit *commit = NULL;
	bool is_boundary;
	const char *(*graph_fn)(struct graph_symbol *) = graph_symbol_to_utf8;

	if (argc > 1 && !strcmp(argv[1], "--ascii"))
		graph_fn = graph_symbol_to_ascii;

	if (!io_open(&io, ""))
		die("IO");

	while (!io_eof(&io)) {
		bool can_read = io_can_read(&io, TRUE);

		for (; (line = io_get(&io, '\n', can_read)); can_read = FALSE) {
			if (!prefixcmp(line, "commit ")) {
				line += STRING_SIZE("commit ");
				is_boundary = *line == '-';

				if (is_boundary)
					line++;

				if (!realloc_commits(&commits, ncommits, 1))
					die("Commits");

				commit = calloc(1, sizeof(*commit));
				if (!commit)
					die("Commit");
				commits[ncommits++] = commit;
				string_copy_rev(commit->id, line);
				graph_add_commit(&graph, &commit->canvas, commit->id, line, is_boundary);
				graph_render_parents(&graph);

			} else if (!prefixcmp(line, "    ")) {
				int i;

				if (!commit)
					continue;

				for (i = 0; i < commit->canvas.size; i++) {
					struct graph_symbol *symbol = &commit->canvas.symbols[i];
					const char *chars = graph_fn(symbol);

					printf("%s", chars + (i == 0));
				}
				printf("%s\n", line + 3);

				commit = NULL;
			}
		}
	}

	return 0;
}