-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_logjoin.c
More file actions
354 lines (313 loc) · 8.62 KB
/
git_logjoin.c
File metadata and controls
354 lines (313 loc) · 8.62 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*-
* Copyright (c) 2013-2019 Devin Teske <dteske@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $DruidBSD: /cvsroot/druidbsd/git_logjoin/git_logjoin.c,v 1.2 2017/06/21 02:39:56 devinteske Exp $
*/
#include <sys/cdefs.h>
#ifdef __FBSDID
__FBSDID("$FreeBSD$");
__FBSDID("$FrauBSD: //github.com/FrauBSD/git_logjoin/git_logjoin.c 2019-05-15 21:16:24 -0700 freebsdfrau $");
#endif
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#ifdef __APPLE__
#undef ARG_MAX
#endif
#ifndef ARG_MAX
#define ARG_MAX 2048
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __linux
#define __USE_XOPEN
#endif
#include <time.h>
#include "string_m.h"
#define FILE_ENTRY_FOOTER ""
#define FILE_ENTRY_HEADER "user:"
#define FILE_ENTRY_HEADER_LEN 5
#define FILE_DATE_FORMAT_ERROR "%s: Invalid epoch"
struct log_entry
{
uint8_t hold;
struct tm date;
char *paths;
char *user;
size_t paths_len;
size_t user_len;
};
struct log_file
{
char *repos, *path;
FILE *file;
struct log_entry entry;
};
static char *gbuf = NULL;
static size_t gbuf_len = 0;
/* Function prototypes for private functions (see style(9)) */
void gbuf_xrealloc(size_t len);
void paths_xrealloc(struct log_entry *entry, size_t len);
char * read_one(struct log_file *log, size_t *llen);
void xrealloc(void **ptr, size_t newlen, size_t *curlen);
int
main(int argc, char *argv[])
{
uint8_t failed = 0;
uint8_t print_sep = 0;
int n, completed;
size_t len;
struct log_file logs[ARG_MAX], *log;
struct log_entry *entry;
/* We need at least 1 positional argument */
if (argc < 2)
errx(EXIT_FAILURE, "too few arguments");
/* Attempt to open each/every log file */
for (n = 0; n < argc - 1; n++)
{
log = &logs[n];
/* Parse the repos name and logfile path from argument */
log->repos = argv[n+1];
log->path = strchr(argv[n+1], ':');
if (log->path == NULL)
errx(EXIT_FAILURE, "%s: Need `repos:logfile'",
argv[n+1]);
else
*(log->path++) = '\0';
/* open the logfile */
if ((log->file = fopen(log->path, "r")) == NULL) {
failed = 1;
break;
}
}
if (failed) {
warn("%s", log->path);
/* Close previously opened log files */
for (; --n >= 0;) fclose(logs[n].file);
return EXIT_FAILURE;
}
fprintf(stderr, "All logs opened successfully!\n");
/*
* Initialize the output
*/
for (n = 0; n < argc - 1; n++) {
log = &logs[n];
log->entry.paths = NULL;
log->entry.paths_len = 0;
log->entry.hold = 0;
}
/*
* Loop until we've read all logentries from all logfiles
*/
for (;;)
{
time_t time0, time1;
struct log_file *oldest;
char seconds[21];
completed = 0;
for (n = 0; n < argc - 1; n++)
if (logs[n].file == NULL) completed++;
if (completed == argc - 1) break;
/* Fill the ranks */
for (n = 0; n < argc - 1; n++)
{
log = &logs[n];
/* Skip logs that are closed and done */
if (log->file == NULL) continue;
entry = &(log->entry);
/* Skip logs that have a held entry already */
if (entry->hold) continue;
if (read_one(log, &len) == NULL) {
fclose(log->file);
log->file = NULL;
continue;
}
}
/* Find the log with the [current] oldest entry */
oldest = NULL;
for (n = 0; n < argc - 1; n++)
{
log = &logs[n];
/* Skip logs that are closed and done */
if (log->file == NULL) continue;
entry = &(log->entry);
entry->hold = 1;
if (oldest == NULL) {
oldest = log;
continue;
}
time0 = mktime(&(oldest->entry.date));
time1 = mktime(&(entry->date));
if (difftime(time1, time0) < 0) {
/* new oldest entry found */
oldest = log;
}
}
/* No logfile entries left */
if (oldest == NULL) break;
log = oldest;
entry = &(log->entry);
/* Add the repository name */
gbuf_xrealloc(strlen(log->repos) + 3);
sprintf(gbuf, "\t%s/", log->repos);
len = strlen(entry->paths);
len += strcount(entry->paths, "\t") * (strlen(gbuf) - 1);
paths_xrealloc(entry, len);
if (replaceall(entry->paths, "\t", gbuf) < 0)
err(EXIT_FAILURE, "%s: replaceall()",
__func__);
if (print_sep) printf("\n");
printf("%s%s\n", FILE_ENTRY_HEADER, entry->user);
strftime(seconds, 20, "%s", &(entry->date));
printf("%s\n", seconds);
printf("%s\n", entry->paths);
entry->hold = 0;
print_sep = 1;
}
/* Close all the log files */
for (n = 0; n < argc - 1; n++) {
log = &logs[n];
if (log->file == NULL) continue;
fclose(log->file);
}
return EXIT_SUCCESS;
}
char *
read_one(struct log_file *log, size_t *llen)
{
uint8_t entry_start = 0, entry_end = 0;
size_t len;
#ifdef __linux
size_t size = 0;
#endif
char *line;
char *retval = NULL;
struct log_entry *entry = &(log->entry);
/* Find and stash the user */
while (!entry_start)
{
#ifdef __linux
len = (size_t)getline(&line, &size, log->file);
#else
line = fgetln(log->file, &len);
#endif
if (len <= 0 || line[len-1] != '\n') break;
gbuf_xrealloc(len);
snprintf(gbuf, len, "%s", line);
if (strncmp(gbuf, FILE_ENTRY_HEADER, FILE_ENTRY_HEADER_LEN)
!= 0) continue;
entry_start = 1;
}
if (!entry_start) goto read_one_fail;
xrealloc((void **)&(entry->user),
len - FILE_ENTRY_HEADER_LEN + 1, &(entry->user_len));
snprintf(entry->user, entry->user_len,
"%s", (gbuf + FILE_ENTRY_HEADER_LEN));
/* Get the epoch */
#ifdef __linux
len = (size_t)getline(&line, &size, log->file);
#else
line = fgetln(log->file, &len);
#endif
if (len <= 0 || line[len-1] != '\n') goto read_one_fail;
retval = line;
if (strptime(line, "%s", &(entry->date)) == NULL)
errx(EXIT_FAILURE, FILE_DATE_FORMAT_ERROR, line);
/* Initialize paths */
paths_xrealloc(entry, 1);
entry->paths[0] = '\0';
/* Determine how long the entry is by reading up to the footer */
while (!entry_end)
{
#ifdef __linux
len = (size_t)getline(&line, &size, log->file);
#else
line = fgetln(log->file, &len);
#endif
if (len <= 0 || line[len-1] != '\n') break;
retval = line;
gbuf_xrealloc(len);
snprintf(gbuf, len, "%s", line);
if (strncmp(gbuf, FILE_ENTRY_HEADER, FILE_ENTRY_HEADER_LEN)
== 0)
{
paths_xrealloc(entry, 1);
entry->paths[0] = '\0';
xrealloc((void **)&(entry->user),
len - FILE_ENTRY_HEADER_LEN + 1,
&(entry->user_len));
snprintf(entry->user, entry->user_len,
"%s", (gbuf + FILE_ENTRY_HEADER_LEN));
#ifdef __linux
len = (size_t)getline(&line, &size, log->file);
#else
line = fgetln(log->file, &len);
#endif
if (len <= 0 || line[len-1] != '\n')
goto read_one_fail;
retval = line;
gbuf_xrealloc(len);
snprintf(gbuf, len, "%s", line);
if (strptime(gbuf, "%s", &(entry->date)) == NULL)
errx(EXIT_FAILURE,
FILE_DATE_FORMAT_ERROR, gbuf);
continue;
}
else if (strcmp(gbuf, FILE_ENTRY_FOOTER) == 0) {
entry_end = 1;
break;
}
paths_xrealloc(entry, strlen(entry->paths) + len + 1);
if (entry->paths[0] != '\0') strcat(entry->paths, "\n");
strcat(entry->paths, gbuf);
}
*llen = len; /* pass back length of last line */
return retval; /* return pointer to last line */
read_one_fail:
if (entry->paths != NULL) entry->paths[0] = '\0';
*llen = 0;
return retval;
}
void
xrealloc(void **ptr, size_t newlen, size_t *curlen)
{
if (newlen <= *curlen) return;
if ((*ptr = realloc(*ptr, newlen)) == NULL)
errx(EXIT_FAILURE, "realloc: out of memory?!");
*curlen = newlen;
}
void
gbuf_xrealloc(size_t len)
{
xrealloc((void **)&gbuf, len, &gbuf_len);
}
void
paths_xrealloc(struct log_entry *entry, size_t len)
{
xrealloc((void **)&(entry->paths), len, &(entry->paths_len));
}