git.h (view raw)
1#ifndef GIT_H
2#define GIT_H
3
4
5/*
6 * from git:git-compat-util.h
7 */
8
9
10#ifndef FLEX_ARRAY
11#if defined(__GNUC__) && (__GNUC__ < 3)
12#define FLEX_ARRAY 0
13#else
14#define FLEX_ARRAY /* empty */
15#endif
16#endif
17
18
19#include <unistd.h>
20#include <stdio.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <stddef.h>
24#include <stdlib.h>
25#include <stdarg.h>
26#include <string.h>
27#include <errno.h>
28#include <limits.h>
29#include <sys/param.h>
30#include <netinet/in.h>
31#include <sys/types.h>
32#include <dirent.h>
33#include <time.h>
34
35
36/* On most systems <limits.h> would have given us this, but
37 * not on some systems (e.g. GNU/Hurd).
38 */
39#ifndef PATH_MAX
40#define PATH_MAX 4096
41#endif
42
43#ifdef __GNUC__
44#define NORETURN __attribute__((__noreturn__))
45#else
46#define NORETURN
47#ifndef __attribute__
48#define __attribute__(x)
49#endif
50#endif
51
52
53extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
54
55
56static inline char* xstrdup(const char *str)
57{
58 char *ret = strdup(str);
59 if (!ret)
60 die("Out of memory, strdup failed");
61 return ret;
62}
63
64static inline void *xmalloc(size_t size)
65{
66 void *ret = malloc(size);
67 if (!ret && !size)
68 ret = malloc(1);
69 if (!ret)
70 die("Out of memory, malloc failed");
71#ifdef XMALLOC_POISON
72 memset(ret, 0xA5, size);
73#endif
74 return ret;
75}
76
77static inline void *xrealloc(void *ptr, size_t size)
78{
79 void *ret = realloc(ptr, size);
80 if (!ret && !size)
81 ret = realloc(ptr, 1);
82 if (!ret)
83 die("Out of memory, realloc failed");
84 return ret;
85}
86
87static inline void *xcalloc(size_t nmemb, size_t size)
88{
89 void *ret = calloc(nmemb, size);
90 if (!ret && (!nmemb || !size))
91 ret = calloc(1, 1);
92 if (!ret)
93 die("Out of memory, calloc failed");
94 return ret;
95}
96
97static inline ssize_t xread(int fd, void *buf, size_t len)
98{
99 ssize_t nr;
100 while (1) {
101 nr = read(fd, buf, len);
102 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
103 continue;
104 return nr;
105 }
106}
107
108static inline ssize_t xwrite(int fd, const void *buf, size_t len)
109{
110 ssize_t nr;
111 while (1) {
112 nr = write(fd, buf, len);
113 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
114 continue;
115 return nr;
116 }
117}
118
119
120
121
122/*
123 * from git:cache.h
124 */
125
126
127/* Convert to/from hex/sha1 representation */
128#define MINIMUM_ABBREV 4
129#define DEFAULT_ABBREV 7
130
131extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
132
133extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
134
135extern int get_sha1(const char *str, unsigned char *sha1);
136extern int get_sha1_hex(const char *hex, unsigned char *sha1);
137extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
138
139
140
141/*
142 * from git:object.h
143 */
144
145struct object_list {
146 struct object *item;
147 struct object_list *next;
148};
149
150struct object_refs {
151 unsigned count;
152 struct object *base;
153 struct object *ref[FLEX_ARRAY]; /* more */
154};
155
156struct object_array {
157 unsigned int nr;
158 unsigned int alloc;
159 struct object_array_entry {
160 struct object *item;
161 const char *name;
162 } *objects;
163};
164
165#define TYPE_BITS 3
166#define FLAG_BITS 27
167
168/*
169 * The object type is stored in 3 bits.
170 */
171struct object {
172 unsigned parsed : 1;
173 unsigned used : 1;
174 unsigned type : TYPE_BITS;
175 unsigned flags : FLAG_BITS;
176 unsigned char sha1[20];
177};
178
179
180/*
181 * from git:tree.h
182 */
183
184struct tree {
185 struct object object;
186 void *buffer;
187 unsigned long size;
188};
189
190
191
192
193/* from git:commit.h */
194
195struct commit_list {
196 struct commit *item;
197 struct commit_list *next;
198};
199
200struct commit {
201 struct object object;
202 void *util;
203 unsigned long date;
204 struct commit_list *parents;
205 struct tree *tree;
206 char *buffer;
207};
208
209
210struct commit *lookup_commit(const unsigned char *sha1);
211struct commit *lookup_commit_reference(const unsigned char *sha1);
212struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
213 int quiet);
214
215int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
216int parse_commit(struct commit *item);
217
218struct commit_list * commit_list_insert(struct commit *item, struct commit_list **list_p);
219struct commit_list * insert_by_date(struct commit *item, struct commit_list **list);
220
221void free_commit_list(struct commit_list *list);
222
223void sort_by_date(struct commit_list **list);
224
225/* Commit formats */
226enum cmit_fmt {
227 CMIT_FMT_RAW,
228 CMIT_FMT_MEDIUM,
229 CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM,
230 CMIT_FMT_SHORT,
231 CMIT_FMT_FULL,
232 CMIT_FMT_FULLER,
233 CMIT_FMT_ONELINE,
234 CMIT_FMT_EMAIL,
235
236 CMIT_FMT_UNSPECIFIED,
237};
238
239extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, int relative_date);
240
241
242typedef void (*topo_sort_set_fn_t)(struct commit*, void *data);
243typedef void* (*topo_sort_get_fn_t)(struct commit*);
244
245
246
247
248/*
249 * from git:diff.h
250 */
251
252
253struct rev_info;
254struct diff_options;
255struct diff_queue_struct;
256
257typedef void (*change_fn_t)(struct diff_options *options,
258 unsigned old_mode, unsigned new_mode,
259 const unsigned char *old_sha1,
260 const unsigned char *new_sha1,
261 const char *base, const char *path);
262
263typedef void (*add_remove_fn_t)(struct diff_options *options,
264 int addremove, unsigned mode,
265 const unsigned char *sha1,
266 const char *base, const char *path);
267
268typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
269 struct diff_options *options, void *data);
270
271#define DIFF_FORMAT_RAW 0x0001
272#define DIFF_FORMAT_DIFFSTAT 0x0002
273#define DIFF_FORMAT_NUMSTAT 0x0004
274#define DIFF_FORMAT_SUMMARY 0x0008
275#define DIFF_FORMAT_PATCH 0x0010
276
277/* These override all above */
278#define DIFF_FORMAT_NAME 0x0100
279#define DIFF_FORMAT_NAME_STATUS 0x0200
280#define DIFF_FORMAT_CHECKDIFF 0x0400
281
282/* Same as output_format = 0 but we know that -s flag was given
283 * and we should not give default value to output_format.
284 */
285#define DIFF_FORMAT_NO_OUTPUT 0x0800
286
287#define DIFF_FORMAT_CALLBACK 0x1000
288
289struct diff_options {
290 const char *filter;
291 const char *orderfile;
292 const char *pickaxe;
293 const char *single_follow;
294 unsigned recursive:1,
295 tree_in_recursive:1,
296 binary:1,
297 text:1,
298 full_index:1,
299 silent_on_remove:1,
300 find_copies_harder:1,
301 color_diff:1,
302 color_diff_words:1;
303 int context;
304 int break_opt;
305 int detect_rename;
306 int line_termination;
307 int output_format;
308 int pickaxe_opts;
309 int rename_score;
310 int reverse_diff;
311 int rename_limit;
312 int setup;
313 int abbrev;
314 const char *msg_sep;
315 const char *stat_sep;
316 long xdl_opts;
317
318 int stat_width;
319 int stat_name_width;
320
321 int nr_paths;
322 const char **paths;
323 int *pathlens;
324 change_fn_t change;
325 add_remove_fn_t add_remove;
326 diff_format_fn_t format_callback;
327 void *format_callback_data;
328};
329
330enum color_diff {
331 DIFF_RESET = 0,
332 DIFF_PLAIN = 1,
333 DIFF_METAINFO = 2,
334 DIFF_FRAGINFO = 3,
335 DIFF_FILE_OLD = 4,
336 DIFF_FILE_NEW = 5,
337 DIFF_COMMIT = 6,
338 DIFF_WHITESPACE = 7,
339};
340
341
342
343
344/*
345 * from git:refs.g
346 */
347
348typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
349extern int head_ref(each_ref_fn, void *);
350extern int for_each_ref(each_ref_fn, void *);
351extern int for_each_tag_ref(each_ref_fn, void *);
352extern int for_each_branch_ref(each_ref_fn, void *);
353extern int for_each_remote_ref(each_ref_fn, void *);
354
355
356
357/*
358 * from git:revision.h
359 */
360
361struct rev_info;
362struct log_info;
363
364typedef void (prune_fn_t)(struct rev_info *revs, struct commit *commit);
365
366struct rev_info {
367 /* Starting list */
368 struct commit_list *commits;
369 struct object_array pending;
370
371 /* Basic information */
372 const char *prefix;
373 void *prune_data;
374 prune_fn_t *prune_fn;
375
376 /* Traversal flags */
377 unsigned int dense:1,
378 no_merges:1,
379 no_walk:1,
380 remove_empty_trees:1,
381 simplify_history:1,
382 lifo:1,
383 topo_order:1,
384 tag_objects:1,
385 tree_objects:1,
386 blob_objects:1,
387 edge_hint:1,
388 limited:1,
389 unpacked:1, /* see also ignore_packed below */
390 boundary:1,
391 parents:1;
392
393 /* Diff flags */
394 unsigned int diff:1,
395 full_diff:1,
396 show_root_diff:1,
397 no_commit_id:1,
398 verbose_header:1,
399 ignore_merges:1,
400 combine_merges:1,
401 dense_combined_merges:1,
402 always_show_header:1;
403
404 /* Format info */
405 unsigned int shown_one:1,
406 abbrev_commit:1,
407 relative_date:1;
408
409 const char **ignore_packed; /* pretend objects in these are unpacked */
410 int num_ignore_packed;
411
412 unsigned int abbrev;
413 enum cmit_fmt commit_format;
414 struct log_info *loginfo;
415 int nr, total;
416 const char *mime_boundary;
417 const char *message_id;
418 const char *ref_message_id;
419 const char *add_signoff;
420 const char *extra_headers;
421
422 /* Filter by commit log message */
423 struct grep_opt *grep_filter;
424
425 /* special limits */
426 int max_count;
427 unsigned long max_age;
428 unsigned long min_age;
429
430 /* diff info for patches and for paths limiting */
431 struct diff_options diffopt;
432 struct diff_options pruning;
433
434 topo_sort_set_fn_t topo_setter;
435 topo_sort_get_fn_t topo_getter;
436};
437
438
439extern void init_revisions(struct rev_info *revs, const char *prefix);
440extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def);
441extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename);
442
443extern void prepare_revision_walk(struct rev_info *revs);
444extern struct commit *get_revision(struct rev_info *revs);
445
446
447
448
449#endif /* GIT_H */