-
Notifications
You must be signed in to change notification settings - Fork 0
/
commitlist.c
228 lines (181 loc) · 5.14 KB
/
commitlist.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* commitlist.c - Parsing git log's output into a data structure
* Blake Mitchell, 2012
*/
#include <string.h>
#include <stdlib.h>
#include "commitlist.h"
#define MAX_LBUF_SIZE 512
#define MAX_COMMENT_SIZE 2046
static char *COMMIT_TOKEN = "commit ";
static char *AUTHOR_TOKEN = "Author: ";
static char *DATE_TOKEN = "Date: ";
static char *COMMENT_TOKEN = " ";
struct commit_node* new_commit_node(int ind, struct commit_node *prev,
struct commit_node *next)
{
struct commit_node *n;
n = (struct commit_node*)malloc(sizeof(struct commit_node));
memset(n->hash, '\0', COMMIT_HASH_SIZE);
n->ind = ind;
n->author = NULL;
n->date = NULL;
n->comment = NULL;
n->prev = prev;
n->next = next;
return n;
}
int begins_with(char *s, char *tkn)
{
if (!(s && tkn))
return 0;
return (!strncmp(s, tkn, strlen(tkn)));
}
char *new_str_after_token(char *s, char *tkn)
{
char *ret, *p;
ret = NULL;
if (begins_with(s, tkn)) {
/* Don't want spaces at the beginning */
for (p = (s + strlen(tkn)); *p == ' '; p++);
ret = (char*)malloc(strlen(p) + 1);
memset(ret, '\0', strlen(p) + 1);
strcpy(ret, p);
/* Don't want newlines at the end */
for (p = (ret + strlen(ret) - 1); *p == '\n' && p > ret; p--)
*p = '\0';
}
return ret;
}
void remove_newlines_and_tabs(char *s)
{
int i;
if (!s)
return;
for (i = 0; i < strlen(s); i++)
if (s[i] == '\n' || s[i] == '\t')
s[i] = ' ';
}
int parse_comment(struct commit_node *cn, char *lbuf, FILE *f)
{
char cbuf[MAX_COMMENT_SIZE];
char *cp, *lp;
memset(cbuf, '\0', sizeof(cbuf));
cp = cbuf;
while (begins_with(fgets(lbuf, MAX_LBUF_SIZE, f), COMMENT_TOKEN))
{
lp = lbuf + strlen(COMMENT_TOKEN);
if (cp - cbuf + strlen(lp) < MAX_COMMENT_SIZE) {
memcpy(cp, lp, strlen(lp));
cp += strlen(lp);
}
}
cn->comment = new_str_after_token(cbuf, "");
remove_newlines_and_tabs(cn->comment);
return 0;
}
int parse_commit(struct commit_node* n, FILE *f)
{
char lbuf[MAX_LBUF_SIZE];
fgets(lbuf, MAX_LBUF_SIZE, f);
if (begins_with(lbuf, COMMIT_TOKEN))
memcpy(n->hash, lbuf + strlen(COMMIT_TOKEN), COMMIT_HASH_SIZE);
else
return 0;
while (strcmp(fgets(lbuf, MAX_LBUF_SIZE, f), "")
&& strcmp(lbuf, "\n"))
{
if (begins_with(lbuf, AUTHOR_TOKEN))
n->author = new_str_after_token(lbuf, AUTHOR_TOKEN);
if (begins_with(lbuf, DATE_TOKEN))
n->date = new_str_after_token(lbuf, DATE_TOKEN);
}
parse_comment(n, lbuf, f);
return 1;
}
commit_list parse_commit_list(FILE *f)
{
struct commit_node *root, *n;
int ind = 0;
root = NULL;
n = new_commit_node(ind++, NULL, NULL);
while (parse_commit(n, f) == 1) {
if (!root)
root = n;
if (n->prev)
n->prev->next = n;
n = new_commit_node(ind++, n, NULL);
}
if (n->prev)
n->prev->next = NULL;
free_commit_list(&n);
return root;
}
void free_commit_list(commit_list* cl)
{
struct commit_node *n, *tmp;
n = *cl;
while (n) {
if (n->author)
free(n->author);
if (n->date)
free(n->date);
if (n->comment)
free(n->comment);
tmp = n->next;
free(n);
n = tmp;
}
*cl = NULL;
}
int commit_list_count(commit_list cl)
{
struct commit_node *n;
int c;
for (n = cl, c = 0; n; n = n->next, c++)
;
return c;
}
int traverse_back(commit_list *cl, int max)
{
struct commit_node*ln;
int i, inc;
ln = *cl;
if (max < 0) {
i = max - 1;
inc = 0;
} else {
i = 0;
inc = 1;
}
for (; *cl && i < max; i += inc) {
ln = *cl;
*cl = (*cl)->prev;
}
if (!*cl) {
*cl = ln;
i--;
}
return i;
}
int traverse_forward(commit_list *cl, int max)
{
struct commit_node*ln;
int i, inc;
if (max < 0) {
i = max - 1;
inc = 0;
} else {
i = 0;
inc = 1;
}
ln = *cl;
for (; *cl && i < max; i += inc) {
ln = *cl;
*cl = (*cl)->next;
}
if (!*cl) {
*cl = ln;
i--;
}
return i;
}