blob: 10aa659825d42a428583f69fae78dce91696b622 (plain)
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
/*
 * The helpers that belong to no single page. Repository registration, the
 * lifetimes of the reference and commit records the renderers build, thin
 * wrappers over git's diff machinery, and a handful of string, date and
 * mimetype utilities all live here because several renderers need them. The
 * two process-wide globals, ctx and cgit_repolist, are defined here as well.
 */

#ifndef CGIT_SHARED_H
#define CGIT_SHARED_H

#include "cgit.h"

extern char cgit_default_repo_desc[];

typedef void (*filepair_fn)(struct diff_filepair *pair);
typedef void (*linediff_fn)(char *line, int len);

/*
 * Each of these returns its argument untouched when the syscall it wraps
 * succeeded, and dies with the message and errno otherwise.
 */
extern int cgit_die_unless_zero(int result, const char *msg);
extern int cgit_die_unless_positive(int result, const char *msg);
extern int cgit_die_unless_non_negative(int result, const char *msg);

/*
 * Append a repository to cgit_repolist and return it, seeded from the current
 * global configuration. The returned pointer is invalidated by the next call,
 * because the list grows by reallocation.
 */
extern struct cgit_repo *cgit_add_repo(const char *url);
extern struct cgit_repo *cgit_get_repoinfo(const char *url);

/*
 * Export the CGIT_REPO_* variables that filters and hooks read.
 */
extern void cgit_prepare_repo_env(struct cgit_repo *repo);

extern void cgit_free_reflist_inner(struct reflist *list);

/*
 * Matches git's reference iteration callback, and appends each ref it is given
 * to the struct reflist passed as cb_data.
 */
extern int cgit_refs_cb(const struct reference *ref, void *cb_data);

extern void cgit_free_commitinfo(struct commitinfo *info);
extern void cgit_free_taginfo(struct taginfo *info);

/*
 * A diff format callback that hands each filepair to the filepair_fn data
 * points at. ISO C has no conversion between a function pointer and void *,
 * so the function travels by address.
 */
extern void cgit_diff_tree_cb(struct diff_queue_struct *q, struct diff_options *options, void *data);

/*
 * Diff two blobs and hand fn one line at a time. Returns non-zero when either
 * blob could not be read. A blob that is binary or larger than max-blob-size
 * sets binary instead, and produces no lines, so the caller can offer a link
 * in place of the content.
 */
extern int cgit_diff_files(const struct object_id *old_oid, const struct object_id *new_oid,
	size_t *old_size, size_t *new_size, int *binary, int context, int ignorews,
	linediff_fn fn);

/*
 * Whether a blob of size bytes is past max-blob-size. Callers ask before
 * reading the object, so a huge blob stays out of memory rather than being
 * noticed once it is already there.
 */
extern int cgit_blob_too_large(size_t size);

/*
 * Limit a diff to the paths below prefix, which is taken literally.
 */
extern void cgit_diff_limit_path(struct diff_options *opt, const char *prefix);

extern void cgit_diff_tree(const struct object_id *old_oid, const struct object_id *new_oid,
	filepair_fn fn, const char *prefix, int ignorews);
extern void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix);

/*
 * Accept only the date formats cgit documents, leaving mode unchanged for
 * anything else.
 */
extern void cgit_parse_date_format(const char *format, struct date_mode *mode);

/*
 * Copy str without its trailing runs of c, returning NULL if nothing is left.
 */
extern char *cgit_trim_end(const char *str, char c);
extern char *cgit_ensure_end(const char *str, char c);
extern void strbuf_ensure_end(struct strbuf *sb, char c);

/*
 * Read the first line of a file into a newly allocated, zero terminated
 * buffer. Returns 0 on success and an errno value otherwise.
 */
extern int cgit_read_first_line(const char *path, char **buf, size_t *size);
extern char *cgit_strdup_first_line(const char *text);

/*
 * How many bytes the UTF-8 sequence at p holds, or 0 when it is invalid,
 * looking at most left bytes ahead.
 */
extern size_t cgit_utf8_seq_len(const unsigned char *p, size_t left);

/*
 * Replace every byte of text that is not part of a valid UTF-8 sequence with
 * a question mark, in place.
 */
extern void cgit_utf8_sanitize(char *text);

/*
 * Replace every $token in text with the matching environment variable. The
 * result is a static buffer, so a caller that needs to keep it must copy it.
 */
extern char *cgit_expand_macros(const char *text);

extern char *cgit_get_mimetype_for_filename(const char *filename);

/*
 * Whether a readme setting names a file tracked in the repository, written
 * as ref:path or :path, rather than a file on the server's disk.
 */
extern int cgit_readme_is_tracked(const char *readme);

/*
 * Whether a revision from the request is a hex object id or a possible ref
 * name, the only forms cgit hands to git.
 */
extern int cgit_valid_rev(const char *rev);

#endif // CGIT_SHARED_H