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#include <regex.h>
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 const unsigned char null_sha1[20];
132
133extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
134
135extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
136
137extern int get_sha1(const char *str, unsigned char *sha1);
138extern int get_sha1_hex(const char *hex, unsigned char *sha1);
139extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
140
141static inline int is_null_sha1(const unsigned char *sha1)
142{
143 return !memcmp(sha1, null_sha1, 20);
144}
145static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
146{
147 return memcmp(sha1, sha2, 20);
148}
149static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
150{
151 memcpy(sha_dst, sha_src, 20);
152}
153static inline void hashclr(unsigned char *hash)
154{
155 memset(hash, 0, 20);
156}
157
158
159/*
160 * from git:grep.h
161 */
162
163enum grep_pat_token {
164 GREP_PATTERN,
165 GREP_PATTERN_HEAD,
166 GREP_PATTERN_BODY,
167 GREP_AND,
168 GREP_OPEN_PAREN,
169 GREP_CLOSE_PAREN,
170 GREP_NOT,
171 GREP_OR,
172};
173
174enum grep_context {
175 GREP_CONTEXT_HEAD,
176 GREP_CONTEXT_BODY,
177};
178
179struct grep_pat {
180 struct grep_pat *next;
181 const char *origin;
182 int no;
183 enum grep_pat_token token;
184 const char *pattern;
185 regex_t regexp;
186};
187
188enum grep_expr_node {
189 GREP_NODE_ATOM,
190 GREP_NODE_NOT,
191 GREP_NODE_AND,
192 GREP_NODE_OR,
193};
194
195struct grep_opt {
196 struct grep_pat *pattern_list;
197 struct grep_pat **pattern_tail;
198 struct grep_expr *pattern_expression;
199 int prefix_length;
200 regex_t regexp;
201 unsigned linenum:1;
202 unsigned invert:1;
203 unsigned status_only:1;
204 unsigned name_only:1;
205 unsigned unmatch_name_only:1;
206 unsigned count:1;
207 unsigned word_regexp:1;
208 unsigned fixed:1;
209 unsigned all_match:1;
210#define GREP_BINARY_DEFAULT 0
211#define GREP_BINARY_NOMATCH 1
212#define GREP_BINARY_TEXT 2
213 unsigned binary:2;
214 unsigned extended:1;
215 unsigned relative:1;
216 unsigned pathname:1;
217 int regflags;
218 unsigned pre_context;
219 unsigned post_context;
220};
221
222
223extern void compile_grep_patterns(struct grep_opt *opt);
224extern void free_grep_patterns(struct grep_opt *opt);
225
226
227/*
228 * from git:object.h
229 */
230
231struct object_list {
232 struct object *item;
233 struct object_list *next;
234};
235
236struct object_refs {
237 unsigned count;
238 struct object *base;
239 struct object *ref[FLEX_ARRAY]; /* more */
240};
241
242struct object_array {
243 unsigned int nr;
244 unsigned int alloc;
245 struct object_array_entry {
246 struct object *item;
247 const char *name;
248 } *objects;
249};
250
251#define TYPE_BITS 3
252#define FLAG_BITS 27
253
254/*
255 * The object type is stored in 3 bits.
256 */
257struct object {
258 unsigned parsed : 1;
259 unsigned used : 1;
260 unsigned type : TYPE_BITS;
261 unsigned flags : FLAG_BITS;
262 unsigned char sha1[20];
263};
264
265
266/*
267 * from git:tree.h
268 */
269
270struct tree {
271 struct object object;
272 void *buffer;
273 unsigned long size;
274};
275
276
277struct tree *lookup_tree(const unsigned char *sha1);
278int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size);
279int parse_tree(struct tree *tree);
280struct tree *parse_tree_indirect(const unsigned char *sha1);
281
282typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const char *, unsigned int, int);
283
284extern int read_tree_recursive(struct tree *tree,
285 const char *base, int baselen,
286 int stage, const char **match,
287 read_tree_fn_t fn);
288
289extern int read_tree(struct tree *tree, int stage, const char **paths);
290
291
292/* from git:commit.h */
293
294struct commit_list {
295 struct commit *item;
296 struct commit_list *next;
297};
298
299struct commit {
300 struct object object;
301 void *util;
302 unsigned long date;
303 struct commit_list *parents;
304 struct tree *tree;
305 char *buffer;
306};
307
308
309struct commit *lookup_commit(const unsigned char *sha1);
310struct commit *lookup_commit_reference(const unsigned char *sha1);
311struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
312 int quiet);
313
314int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
315int parse_commit(struct commit *item);
316
317struct commit_list * commit_list_insert(struct commit *item, struct commit_list **list_p);
318struct commit_list * insert_by_date(struct commit *item, struct commit_list **list);
319
320void free_commit_list(struct commit_list *list);
321
322void sort_by_date(struct commit_list **list);
323
324/* Commit formats */
325enum cmit_fmt {
326 CMIT_FMT_RAW,
327 CMIT_FMT_MEDIUM,
328 CMIT_FMT_DEFAULT = CMIT_FMT_MEDIUM,
329 CMIT_FMT_SHORT,
330 CMIT_FMT_FULL,
331 CMIT_FMT_FULLER,
332 CMIT_FMT_ONELINE,
333 CMIT_FMT_EMAIL,
334
335 CMIT_FMT_UNSPECIFIED,
336};
337
338extern 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);
339
340
341typedef void (*topo_sort_set_fn_t)(struct commit*, void *data);
342typedef void* (*topo_sort_get_fn_t)(struct commit*);
343
344
345
346/*
347 * from git:diffcore.h
348 */
349
350struct diff_filespec {
351 unsigned char sha1[20];
352 char *path;
353 void *data;
354 void *cnt_data;
355 unsigned long size;
356 int xfrm_flags; /* for use by the xfrm */
357 unsigned short mode; /* file mode */
358 unsigned sha1_valid : 1; /* if true, use sha1 and trust mode;
359 * if false, use the name and read from
360 * the filesystem.
361 */
362#define DIFF_FILE_VALID(spec) (((spec)->mode) != 0)
363 unsigned should_free : 1; /* data should be free()'ed */
364 unsigned should_munmap : 1; /* data should be munmap()'ed */
365};
366
367struct diff_filepair {
368 struct diff_filespec *one;
369 struct diff_filespec *two;
370 unsigned short int score;
371 char status; /* M C R N D U (see Documentation/diff-format.txt) */
372 unsigned source_stays : 1; /* all of R/C are copies */
373 unsigned broken_pair : 1;
374 unsigned renamed_pair : 1;
375};
376
377#define DIFF_PAIR_UNMERGED(p) \
378 (!DIFF_FILE_VALID((p)->one) && !DIFF_FILE_VALID((p)->two))
379
380#define DIFF_PAIR_RENAME(p) ((p)->renamed_pair)
381
382#define DIFF_PAIR_BROKEN(p) \
383 ( (!DIFF_FILE_VALID((p)->one) != !DIFF_FILE_VALID((p)->two)) && \
384 ((p)->broken_pair != 0) )
385
386#define DIFF_PAIR_TYPE_CHANGED(p) \
387 ((S_IFMT & (p)->one->mode) != (S_IFMT & (p)->two->mode))
388
389#define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
390
391extern void diff_free_filepair(struct diff_filepair *);
392
393extern int diff_unmodified_pair(struct diff_filepair *);
394
395struct diff_queue_struct {
396 struct diff_filepair **queue;
397 int alloc;
398 int nr;
399};
400
401
402/*
403 * from git:diff.h
404 */
405
406
407struct rev_info;
408struct diff_options;
409struct diff_queue_struct;
410
411typedef void (*change_fn_t)(struct diff_options *options,
412 unsigned old_mode, unsigned new_mode,
413 const unsigned char *old_sha1,
414 const unsigned char *new_sha1,
415 const char *base, const char *path);
416
417typedef void (*add_remove_fn_t)(struct diff_options *options,
418 int addremove, unsigned mode,
419 const unsigned char *sha1,
420 const char *base, const char *path);
421
422typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
423 struct diff_options *options, void *data);
424
425#define DIFF_FORMAT_RAW 0x0001
426#define DIFF_FORMAT_DIFFSTAT 0x0002
427#define DIFF_FORMAT_NUMSTAT 0x0004
428#define DIFF_FORMAT_SUMMARY 0x0008
429#define DIFF_FORMAT_PATCH 0x0010
430
431/* These override all above */
432#define DIFF_FORMAT_NAME 0x0100
433#define DIFF_FORMAT_NAME_STATUS 0x0200
434#define DIFF_FORMAT_CHECKDIFF 0x0400
435
436/* Same as output_format = 0 but we know that -s flag was given
437 * and we should not give default value to output_format.
438 */
439#define DIFF_FORMAT_NO_OUTPUT 0x0800
440
441#define DIFF_FORMAT_CALLBACK 0x1000
442
443struct diff_options {
444 const char *filter;
445 const char *orderfile;
446 const char *pickaxe;
447 const char *single_follow;
448 unsigned recursive:1,
449 tree_in_recursive:1,
450 binary:1,
451 text:1,
452 full_index:1,
453 silent_on_remove:1,
454 find_copies_harder:1,
455 color_diff:1,
456 color_diff_words:1;
457 int context;
458 int break_opt;
459 int detect_rename;
460 int line_termination;
461 int output_format;
462 int pickaxe_opts;
463 int rename_score;
464 int reverse_diff;
465 int rename_limit;
466 int setup;
467 int abbrev;
468 const char *msg_sep;
469 const char *stat_sep;
470 long xdl_opts;
471
472 int stat_width;
473 int stat_name_width;
474
475 int nr_paths;
476 const char **paths;
477 int *pathlens;
478 change_fn_t change;
479 add_remove_fn_t add_remove;
480 diff_format_fn_t format_callback;
481 void *format_callback_data;
482};
483
484enum color_diff {
485 DIFF_RESET = 0,
486 DIFF_PLAIN = 1,
487 DIFF_METAINFO = 2,
488 DIFF_FRAGINFO = 3,
489 DIFF_FILE_OLD = 4,
490 DIFF_FILE_NEW = 5,
491 DIFF_COMMIT = 6,
492 DIFF_WHITESPACE = 7,
493};
494
495
496extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
497 const char *base, struct diff_options *opt);
498
499extern int diff_root_tree_sha1(const unsigned char *new, const char *base,
500 struct diff_options *opt);
501
502extern int git_diff_ui_config(const char *var, const char *value);
503extern void diff_setup(struct diff_options *);
504extern int diff_opt_parse(struct diff_options *, const char **, int);
505extern int diff_setup_done(struct diff_options *);
506
507
508extern void diffcore_std(struct diff_options *);
509extern void diff_flush(struct diff_options*);
510
511
512/* diff-raw status letters */
513#define DIFF_STATUS_ADDED 'A'
514#define DIFF_STATUS_COPIED 'C'
515#define DIFF_STATUS_DELETED 'D'
516#define DIFF_STATUS_MODIFIED 'M'
517#define DIFF_STATUS_RENAMED 'R'
518#define DIFF_STATUS_TYPE_CHANGED 'T'
519#define DIFF_STATUS_UNKNOWN 'X'
520#define DIFF_STATUS_UNMERGED 'U'
521
522
523
524/*
525 * from git:refs.g
526 */
527
528typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
529extern int head_ref(each_ref_fn, void *);
530extern int for_each_ref(each_ref_fn, void *);
531extern int for_each_tag_ref(each_ref_fn, void *);
532extern int for_each_branch_ref(each_ref_fn, void *);
533extern int for_each_remote_ref(each_ref_fn, void *);
534
535
536
537/*
538 * from git:revision.h
539 */
540
541struct rev_info;
542struct log_info;
543
544typedef void (prune_fn_t)(struct rev_info *revs, struct commit *commit);
545
546struct rev_info {
547 /* Starting list */
548 struct commit_list *commits;
549 struct object_array pending;
550
551 /* Basic information */
552 const char *prefix;
553 void *prune_data;
554 prune_fn_t *prune_fn;
555
556 /* Traversal flags */
557 unsigned int dense:1,
558 no_merges:1,
559 no_walk:1,
560 remove_empty_trees:1,
561 simplify_history:1,
562 lifo:1,
563 topo_order:1,
564 tag_objects:1,
565 tree_objects:1,
566 blob_objects:1,
567 edge_hint:1,
568 limited:1,
569 unpacked:1, /* see also ignore_packed below */
570 boundary:1,
571 parents:1;
572
573 /* Diff flags */
574 unsigned int diff:1,
575 full_diff:1,
576 show_root_diff:1,
577 no_commit_id:1,
578 verbose_header:1,
579 ignore_merges:1,
580 combine_merges:1,
581 dense_combined_merges:1,
582 always_show_header:1;
583
584 /* Format info */
585 unsigned int shown_one:1,
586 abbrev_commit:1,
587 relative_date:1;
588
589 const char **ignore_packed; /* pretend objects in these are unpacked */
590 int num_ignore_packed;
591
592 unsigned int abbrev;
593 enum cmit_fmt commit_format;
594 struct log_info *loginfo;
595 int nr, total;
596 const char *mime_boundary;
597 const char *message_id;
598 const char *ref_message_id;
599 const char *add_signoff;
600 const char *extra_headers;
601
602 /* Filter by commit log message */
603 struct grep_opt *grep_filter;
604
605 /* special limits */
606 int max_count;
607 unsigned long max_age;
608 unsigned long min_age;
609
610 /* diff info for patches and for paths limiting */
611 struct diff_options diffopt;
612 struct diff_options pruning;
613
614 topo_sort_set_fn_t topo_setter;
615 topo_sort_get_fn_t topo_getter;
616};
617
618
619extern void init_revisions(struct rev_info *revs, const char *prefix);
620extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def);
621extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename);
622
623extern void prepare_revision_walk(struct rev_info *revs);
624extern struct commit *get_revision(struct rev_info *revs);
625
626
627
628/* from git:log-tree.h */
629
630int log_tree_commit(struct rev_info *, struct commit *);
631
632
633
634#endif /* GIT_H */