-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
122 lines (101 loc) · 3.2 KB
/
main.c
File metadata and controls
122 lines (101 loc) · 3.2 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "graph.h"
// git-compat.h is included by graph.h
#define MAX_COMMITS 5000
static struct commit *commits[MAX_COMMITS];
static int commits_nr = 0;
static int hexval(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0;
}
static void parse_oid(const char *hex, struct object_id *oid) {
for (int i = 0; i < 20; i++) {
unsigned int v = (hexval(hex[2*i]) << 4) | hexval(hex[2*i+1]);
oid->hash[i] = v;
}
}
static char *oid_to_hex_str(const struct object_id *oid) {
static char buf[41];
for (int i = 0; i < 20; i++) {
sprintf(buf + 2*i, "%02x", oid->hash[i]);
}
return buf;
}
static struct commit *get_commit(const struct object_id *oid) {
for (int i = 0; i < commits_nr; i++) {
if (oideq(&commits[i]->object.oid, oid))
return commits[i];
}
if (commits_nr >= MAX_COMMITS) {
fprintf(stderr, "Too many commits\n");
exit(1);
}
struct commit *c = calloc(1, sizeof(struct commit));
c->object.oid = *oid;
c->index = commits_nr;
commits[commits_nr++] = c;
return c;
}
static void add_parent(struct commit *child, struct commit *parent) {
struct commit_list **pp = &child->parents;
while (*pp) {
if ((*pp)->item == parent) return;
pp = &(*pp)->next;
}
commit_list_append(parent, pp);
}
// List to store input order
struct commit_list *input_commits = NULL;
struct commit_list **input_tail = &input_commits;
int main(void) {
char buf[4096];
struct rev_info revs;
struct git_graph *graph;
// Setup revs
memset(&revs, 0, sizeof(revs));
revs.diffopt.file = stdout;
// Read stdin
while (fgets(buf, sizeof(buf), stdin)) {
// Strip newline and trailing spaces
size_t len = strlen(buf);
while (len > 0 && isspace(buf[len-1])) buf[--len] = '\0';
if (len == 0) continue;
char *ptr = buf;
char *token = strtok(ptr, " ");
if (!token) continue;
struct object_id oid;
parse_oid(token, &oid);
struct commit *c = get_commit(&oid);
// Keep track of input order
struct commit_list *node = commit_list_append(c, input_tail);
input_tail = &node->next;
while ((token = strtok(NULL, " ")) != NULL) {
struct object_id p_oid;
parse_oid(token, &p_oid);
struct commit *p = get_commit(&p_oid);
add_parent(c, p);
}
}
graph = graph_init(&revs);
for (struct commit_list *l = input_commits; l; l = l->next) {
struct commit *c = l->item;
graph_update(graph, c);
while (!graph_is_commit_finished(graph)) {
struct strbuf sb = STRBUF_INIT;
int is_commit = graph_next_line(graph, &sb);
fwrite(sb.buf, 1, sb.len, stdout);
if (is_commit) {
printf(" %s", oid_to_hex_str(&c->object.oid));
}
printf("\n");
strbuf_release(&sb);
}
}
graph_clear(graph);
return 0;
}