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