-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-compat.h
More file actions
326 lines (271 loc) · 6.93 KB
/
git-compat.h
File metadata and controls
326 lines (271 loc) · 6.93 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
#ifndef GIT_COMPAT_H
#define GIT_COMPAT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
/*
* Minimal compatibility header to compile git's graph.c standalone.
* This stubs out the Git internals with simplified versions.
*/
/* ============== Memory allocation macros ============== */
#define ALLOC_ARRAY(x, n) ((x) = malloc(sizeof(*(x)) * (n)))
#define REALLOC_ARRAY(x, n) ((x) = realloc((x), sizeof(*(x)) * (n)))
#define COPY_ARRAY(dst, src, n) memcpy(dst, src, sizeof(*(dst)) * (n))
#define MOVE_ARRAY(dst, src, n) memmove(dst, src, sizeof(*(dst)) * (n))
#define CALLOC_ARRAY(x, n) ((x) = calloc((n), sizeof(*(x))))
#define SWAP(a, b) do { \
void *_swap_tmp = malloc(sizeof(a)); \
memcpy(_swap_tmp, &(a), sizeof(a)); \
memcpy(&(a), &(b), sizeof(a)); \
memcpy(&(b), _swap_tmp, sizeof(a)); \
free(_swap_tmp); \
} while(0)
#define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while(0)
/* ============== Object identity ============== */
#define GIT_MAX_RAWSZ 32
#define GIT_SHA1_RAWSZ 20
struct object_id {
unsigned char hash[GIT_MAX_RAWSZ];
};
static inline int oideq(const struct object_id *a, const struct object_id *b)
{
return memcmp(a->hash, b->hash, GIT_SHA1_RAWSZ) == 0;
}
/* ============== Object flags ============== */
#define FLAG_BITS 28
/* Flag bits used by graph.c */
#define BOUNDARY (1u << 1)
#define CHILD_SHOWN (1u << 0)
#define UNINTERESTING (1u << 2)
#define TREESAME (1u << 3)
struct object {
unsigned parsed : 1;
unsigned type : 3;
unsigned flags : FLAG_BITS;
struct object_id oid;
};
/* ============== Commit structures ============== */
struct commit_list {
struct commit *item;
struct commit_list *next;
};
struct commit {
struct object object;
unsigned long date;
struct commit_list *parents;
void *maybe_tree; /* unused, but keeps struct similar */
unsigned int index;
};
/* Helper to build parent list */
static inline struct commit_list *commit_list_append(struct commit *commit,
struct commit_list **tail)
{
struct commit_list *node = malloc(sizeof(struct commit_list));
node->item = commit;
node->next = NULL;
*tail = node;
return node;
}
static inline void free_commit_list(struct commit_list *list)
{
while (list) {
struct commit_list *next = list->next;
free(list);
list = next;
}
}
/* ============== Diff options (minimal) ============== */
struct diff_options {
FILE *file;
const char *line_prefix;
int use_color;
int line_termination;
const char *(*output_prefix)(struct diff_options *opt, void *data);
void *output_prefix_data;
};
/* ============== Rev info (minimal) ============== */
struct rev_info {
struct diff_options diffopt;
void *repo;
int boundary;
int first_parent_only;
/* Add other fields as needed */
};
/* ============== Commit action (filtering) ============== */
enum commit_action {
commit_ignore = 0,
commit_show = 1,
commit_error = 2
};
/*
* Stub: always show all commits.
* In real Git this filters based on revision options.
*/
static inline enum commit_action get_commit_action(struct rev_info *revs,
struct commit *commit)
{
(void)revs;
(void)commit;
return commit_show;
}
/* ============== Revision mark ============== */
/*
* Returns the character to display for this commit.
* In real Git this varies based on boundary/cherry-pick status.
*/
static inline const char *get_revision_mark(struct rev_info *revs,
struct commit *commit)
{
(void)revs;
if (commit->object.flags & BOUNDARY)
return "-";
return "*";
}
/* ============== Color support ============== */
#define COLOR_MAXLEN 75
#define GIT_COLOR_NORMAL ""
#define GIT_COLOR_RESET "\033[0m"
#define GIT_COLOR_RED "\033[31m"
#define GIT_COLOR_GREEN "\033[32m"
#define GIT_COLOR_YELLOW "\033[33m"
#define GIT_COLOR_BLUE "\033[34m"
#define GIT_COLOR_MAGENTA "\033[35m"
#define GIT_COLOR_CYAN "\033[36m"
#define GIT_COLOR_BOLD_RED "\033[1;31m"
/* Default column colors for graph branches */
static const char *column_colors_ansi[] = {
GIT_COLOR_RED,
GIT_COLOR_GREEN,
GIT_COLOR_YELLOW,
GIT_COLOR_BLUE,
GIT_COLOR_MAGENTA,
GIT_COLOR_CYAN,
GIT_COLOR_RESET,
};
#define column_colors_ansi_max (sizeof(column_colors_ansi) / sizeof(column_colors_ansi[0]) - 1)
/* Color preference: -1 = auto, 0 = never, 1 = always */
static inline int want_color(int var)
{
/* Return 0 to disable colors, 1 to enable */
return var > 0;
}
static inline int color_parse_mem(const char *value, int len, char *dst)
{
(void)value;
(void)len;
(void)dst;
return -1; /* Fail: don't support custom color parsing */
}
/* ============== String utilities ============== */
static inline char *xstrdup(const char *s)
{
char *ret = strdup(s);
if (!ret) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
return ret;
}
static inline void *xmalloc(size_t size)
{
void *ret = malloc(size);
if (!ret && size) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
return ret;
}
static inline void *xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);
if (!ret && size) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
return ret;
}
static inline char *xstrndup(const char *s, size_t n)
{
char *ret = strndup(s, n);
if (!ret) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
return ret;
}
/* strchrnul - find char or end of string */
static inline char *git_strchrnul(const char *s, int c)
{
while (*s && *s != c)
s++;
return (char *)s;
}
#define strchrnul git_strchrnul
/* ============== Gettext stub ============== */
#define _(s) (s)
#define N_(s) (s)
/* ============== Warning/error output ============== */
static inline void warning(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "warning: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
static inline void die(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "fatal: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
/* ============== strvec (string vector) ============== */
struct strvec {
const char **v;
size_t nr;
size_t alloc;
};
#define STRVEC_INIT { NULL, 0, 0 }
static inline void strvec_init(struct strvec *sv)
{
sv->v = NULL;
sv->nr = 0;
sv->alloc = 0;
}
static inline void strvec_push(struct strvec *sv, const char *s)
{
if (sv->nr + 1 >= sv->alloc) {
sv->alloc = sv->alloc ? sv->alloc * 2 : 8;
sv->v = xrealloc(sv->v, sv->alloc * sizeof(char *));
}
sv->v[sv->nr++] = xstrdup(s);
sv->v[sv->nr] = NULL;
}
static inline void strvec_clear(struct strvec *sv)
{
for (size_t i = 0; i < sv->nr; i++)
free((char *)sv->v[i]);
free(sv->v);
strvec_init(sv);
}
/* ============== Config stubs ============== */
/* Stub out git config reading */
static inline int git_config_get_string(const char *key, char **value)
{
(void)key;
(void)value;
return 1; /* Not found */
}
static inline int repo_config_get_string(const void *repo, const char *key, char **value)
{
(void)repo;
return git_config_get_string(key, value);
}
#endif /* GIT_COMPAT_H */