-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
325 lines (271 loc) · 6.9 KB
/
util.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
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
/* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
*
* 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.
*/
#include "tig/tig.h"
#include "tig/util.h"
/*
* Error handling.
*/
static const char *status_messages[] = {
"Success",
#define STATUS_CODE_MESSAGE(name, msg) msg
STATUS_CODE_INFO(STATUS_CODE_MESSAGE)
};
static char status_custom_message[SIZEOF_STR];
static bool status_success_message = false;
const char *
get_status_message(enum status_code code)
{
if (code == SUCCESS) {
const char *message = status_success_message ? status_custom_message : "";
status_success_message = false;
return message;
}
if (code == ERROR_CUSTOM_MESSAGE)
return status_custom_message;
return status_messages[code];
}
enum status_code
error(const char *msg, ...)
{
int retval;
FORMAT_BUFFER(status_custom_message, sizeof(status_custom_message), msg, retval, true);
status_success_message = false;
return ERROR_CUSTOM_MESSAGE;
}
enum status_code
success(const char *msg, ...)
{
int retval;
FORMAT_BUFFER(status_custom_message, sizeof(status_custom_message), msg, retval, true);
status_success_message = true;
return SUCCESS;
}
void
warn(const char *msg, ...)
{
va_list args;
va_start(args, msg);
fputs("tig warning: ", stderr);
vfprintf(stderr, msg, args);
fputs("\n", stderr);
va_end(args);
}
die_fn die_callback = NULL;
void TIG_NORETURN
die(const char *err, ...)
{
va_list args;
if (die_callback)
die_callback();
va_start(args, err);
fputs("tig: ", stderr);
vfprintf(stderr, err, args);
fputs("\n", stderr);
va_end(args);
exit(1);
}
/*
* Git data formatters and parsers.
*/
int
timecmp(const struct time *t1, const struct time *t2)
{
return t1->sec - t2->sec;
}
const char *
mkdate(const struct time *time, enum date date)
{
static char buf[STRING_SIZE("2006-04-29 14:21") + 1];
static const struct enum_map_entry reldate[] = {
{ "second", 1, 60 * 2 },
{ "minute", 60, 60 * 60 * 2 },
{ "hour", 60 * 60, 60 * 60 * 24 * 2 },
{ "day", 60 * 60 * 24, 60 * 60 * 24 * 7 * 2 },
{ "week", 60 * 60 * 24 * 7, 60 * 60 * 24 * 7 * 5 },
{ "month", 60 * 60 * 24 * 30, 60 * 60 * 24 * 365 },
{ "year", 60 * 60 * 24 * 365, 0 },
};
struct tm tm;
const char *format;
if (!date || !time || !time->sec)
return "";
if (date == DATE_RELATIVE) {
struct timeval now;
time_t date = time->sec + time->tz;
time_t seconds;
int i;
gettimeofday(&now, NULL);
seconds = now.tv_sec < date ? date - now.tv_sec : now.tv_sec - date;
for (i = 0; i < ARRAY_SIZE(reldate); i++) {
if (seconds >= reldate[i].value && reldate[i].value)
continue;
seconds /= reldate[i].namelen;
if (!string_format(buf, "%ld %s%s %s",
seconds, reldate[i].name,
seconds > 1 ? "s" : "",
now.tv_sec >= date ? "ago" : "ahead"))
break;
return buf;
}
}
if (date == DATE_LOCAL) {
time_t date = time->sec + time->tz;
localtime_r(&date, &tm);
}
else {
gmtime_r(&time->sec, &tm);
}
format = date == DATE_SHORT ? "%Y-%m-%d" : "%Y-%m-%d %H:%M";
return strftime(buf, sizeof(buf), format, &tm) ? buf : NULL;
}
const char *
mkfilesize(unsigned long size, enum file_size format)
{
static char buf[64 + 1];
static const char relsize[] = {
'B', 'K', 'M', 'G', 'T', 'P'
};
if (!format)
return "";
if (format == FILE_SIZE_UNITS) {
const char *fmt = "%.0f%c";
double rsize = size;
int i;
for (i = 0; i < ARRAY_SIZE(relsize); i++) {
if (rsize > 1024.0 && i + 1 < ARRAY_SIZE(relsize)) {
rsize /= 1024;
continue;
}
size = rsize * 10;
if (size % 10 > 0)
fmt = "%.1f%c";
return string_format(buf, fmt, rsize, relsize[i])
? buf : NULL;
}
}
return string_format(buf, "%ld", size) ? buf : NULL;
}
const struct ident unknown_ident = { "Unknown", "unknown@localhost" };
int
ident_compare(const struct ident *i1, const struct ident *i2)
{
if (!i1 || !i2)
return (!!i1) - (!!i2);
if (!i1->name || !i2->name)
return (!!i1->name) - (!!i2->name);
return strcmp(i1->name, i2->name);
}
static const char *
get_author_initials(const char *author)
{
static char initials[256];
size_t pos = 0;
const char *end = strchr(author, '\0');
#define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@' || (c) == '-')
memset(initials, 0, sizeof(initials));
while (author < end) {
unsigned char bytes;
size_t i;
while (author < end && is_initial_sep(*author))
author++;
bytes = utf8_char_length(author);
if (bytes >= sizeof(initials) - 1 - pos)
break;
while (bytes--) {
initials[pos++] = *author++;
}
i = pos;
while (author < end && !is_initial_sep(*author)) {
bytes = utf8_char_length(author);
if (bytes >= sizeof(initials) - 1 - i) {
while (author < end && !is_initial_sep(*author))
author++;
break;
}
while (bytes--) {
initials[i++] = *author++;
}
}
initials[i++] = 0;
}
return initials;
}
static const char *
get_email_user(const char *email)
{
static char user[SIZEOF_STR + 1];
const char *end = strchr(email, '@');
int length = end ? end - email : strlen(email);
string_format(user, "%.*s%c", length, email, 0);
return user;
}
const char *
mkauthor(const struct ident *ident, int cols, enum author author)
{
bool trim = author_trim(cols);
bool abbreviate = author == AUTHOR_ABBREVIATED || !trim;
if (author == AUTHOR_NO || !ident)
return "";
if (author == AUTHOR_EMAIL && ident->email)
return ident->email;
if (author == AUTHOR_EMAIL_USER && ident->email)
return get_email_user(ident->email);
if (abbreviate && ident->name)
return get_author_initials(ident->name);
return ident->name;
}
const char *
mkmode(mode_t mode)
{
if (S_ISDIR(mode))
return "drwxr-xr-x";
else if (S_ISLNK(mode))
return "lrwxrwxrwx";
else if (S_ISGITLINK(mode))
return "m---------";
else if (S_ISREG(mode) && mode & S_IXUSR)
return "-rwxr-xr-x";
else if (S_ISREG(mode))
return "-rw-r--r--";
else
return "----------";
}
const char *
mkstatus(const char status, enum status_label label)
{
static char default_label[] = { '?', 0 };
static const char *labels[][2] = {
{ "!", "ignored" },
{ "?", "untracked" },
{ "A", "added" },
{ "C", "copied" },
{ "D", "deleted" },
{ "M", "modified" },
{ "R", "renamed" },
{ "U", "unmerged" },
};
int i;
if (label == STATUS_LABEL_NO)
return "";
for (i = 0; i < ARRAY_SIZE(labels); i++) {
if (status == *labels[i][0]) {
if (label == STATUS_LABEL_LONG)
return labels[i][1];
else
return labels[i][0];
}
}
default_label[0] = status;
return default_label;
}
/* vim: set ts=8 sw=8 noexpandtab: */