summaryrefslogtreecommitdiffabout
path: root/shared.c
Unidiff
Diffstat (limited to 'shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--shared.c2
1 files changed, 0 insertions, 2 deletions
diff --git a/shared.c b/shared.c
index cd60da5..48002ac 100644
--- a/shared.c
+++ b/shared.c
@@ -1,208 +1,206 @@
1/* shared.c: global vars + some callback functions 1/* shared.c: global vars + some callback functions
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11struct cgit_repolist cgit_repolist; 11struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 12struct cgit_context ctx;
13int cgit_cmd; 13int cgit_cmd;
14 14
15const char *cgit_version = CGIT_VERSION;
16
17int chk_zero(int result, char *msg) 15int chk_zero(int result, char *msg)
18{ 16{
19 if (result != 0) 17 if (result != 0)
20 die("%s: %s", msg, strerror(errno)); 18 die("%s: %s", msg, strerror(errno));
21 return result; 19 return result;
22} 20}
23 21
24int chk_positive(int result, char *msg) 22int chk_positive(int result, char *msg)
25{ 23{
26 if (result <= 0) 24 if (result <= 0)
27 die("%s: %s", msg, strerror(errno)); 25 die("%s: %s", msg, strerror(errno));
28 return result; 26 return result;
29} 27}
30 28
31int chk_non_negative(int result, char *msg) 29int chk_non_negative(int result, char *msg)
32{ 30{
33 if (result < 0) 31 if (result < 0)
34 die("%s: %s",msg, strerror(errno)); 32 die("%s: %s",msg, strerror(errno));
35 return result; 33 return result;
36} 34}
37 35
38struct cgit_repo *cgit_add_repo(const char *url) 36struct cgit_repo *cgit_add_repo(const char *url)
39{ 37{
40 struct cgit_repo *ret; 38 struct cgit_repo *ret;
41 39
42 if (++cgit_repolist.count > cgit_repolist.length) { 40 if (++cgit_repolist.count > cgit_repolist.length) {
43 if (cgit_repolist.length == 0) 41 if (cgit_repolist.length == 0)
44 cgit_repolist.length = 8; 42 cgit_repolist.length = 8;
45 else 43 else
46 cgit_repolist.length *= 2; 44 cgit_repolist.length *= 2;
47 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 45 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
48 cgit_repolist.length * 46 cgit_repolist.length *
49 sizeof(struct cgit_repo)); 47 sizeof(struct cgit_repo));
50 } 48 }
51 49
52 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
53 ret->url = trim_end(url, '/'); 51 ret->url = trim_end(url, '/');
54 ret->name = ret->url; 52 ret->name = ret->url;
55 ret->path = NULL; 53 ret->path = NULL;
56 ret->desc = "[no description]"; 54 ret->desc = "[no description]";
57 ret->owner = NULL; 55 ret->owner = NULL;
58 ret->group = ctx.cfg.repo_group; 56 ret->group = ctx.cfg.repo_group;
59 ret->defbranch = "master"; 57 ret->defbranch = "master";
60 ret->snapshots = ctx.cfg.snapshots; 58 ret->snapshots = ctx.cfg.snapshots;
61 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 59 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
62 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
63 ret->module_link = ctx.cfg.module_link; 61 ret->module_link = ctx.cfg.module_link;
64 ret->readme = NULL; 62 ret->readme = NULL;
65 return ret; 63 return ret;
66} 64}
67 65
68struct cgit_repo *cgit_get_repoinfo(const char *url) 66struct cgit_repo *cgit_get_repoinfo(const char *url)
69{ 67{
70 int i; 68 int i;
71 struct cgit_repo *repo; 69 struct cgit_repo *repo;
72 70
73 for (i=0; i<cgit_repolist.count; i++) { 71 for (i=0; i<cgit_repolist.count; i++) {
74 repo = &cgit_repolist.repos[i]; 72 repo = &cgit_repolist.repos[i];
75 if (!strcmp(repo->url, url)) 73 if (!strcmp(repo->url, url))
76 return repo; 74 return repo;
77 } 75 }
78 return NULL; 76 return NULL;
79} 77}
80 78
81void *cgit_free_commitinfo(struct commitinfo *info) 79void *cgit_free_commitinfo(struct commitinfo *info)
82{ 80{
83 free(info->author); 81 free(info->author);
84 free(info->author_email); 82 free(info->author_email);
85 free(info->committer); 83 free(info->committer);
86 free(info->committer_email); 84 free(info->committer_email);
87 free(info->subject); 85 free(info->subject);
88 free(info->msg); 86 free(info->msg);
89 free(info->msg_encoding); 87 free(info->msg_encoding);
90 free(info); 88 free(info);
91 return NULL; 89 return NULL;
92} 90}
93 91
94int hextoint(char c) 92int hextoint(char c)
95{ 93{
96 if (c >= 'a' && c <= 'f') 94 if (c >= 'a' && c <= 'f')
97 return 10 + c - 'a'; 95 return 10 + c - 'a';
98 else if (c >= 'A' && c <= 'F') 96 else if (c >= 'A' && c <= 'F')
99 return 10 + c - 'A'; 97 return 10 + c - 'A';
100 else if (c >= '0' && c <= '9') 98 else if (c >= '0' && c <= '9')
101 return c - '0'; 99 return c - '0';
102 else 100 else
103 return -1; 101 return -1;
104} 102}
105 103
106char *trim_end(const char *str, char c) 104char *trim_end(const char *str, char c)
107{ 105{
108 int len; 106 int len;
109 char *s, *t; 107 char *s, *t;
110 108
111 if (str == NULL) 109 if (str == NULL)
112 return NULL; 110 return NULL;
113 t = (char *)str; 111 t = (char *)str;
114 len = strlen(t); 112 len = strlen(t);
115 while(len > 0 && t[len - 1] == c) 113 while(len > 0 && t[len - 1] == c)
116 len--; 114 len--;
117 115
118 if (len == 0) 116 if (len == 0)
119 return NULL; 117 return NULL;
120 118
121 c = t[len]; 119 c = t[len];
122 t[len] = '\0'; 120 t[len] = '\0';
123 s = xstrdup(t); 121 s = xstrdup(t);
124 t[len] = c; 122 t[len] = c;
125 return s; 123 return s;
126} 124}
127 125
128char *strlpart(char *txt, int maxlen) 126char *strlpart(char *txt, int maxlen)
129{ 127{
130 char *result; 128 char *result;
131 129
132 if (!txt) 130 if (!txt)
133 return txt; 131 return txt;
134 132
135 if (strlen(txt) <= maxlen) 133 if (strlen(txt) <= maxlen)
136 return txt; 134 return txt;
137 result = xmalloc(maxlen + 1); 135 result = xmalloc(maxlen + 1);
138 memcpy(result, txt, maxlen - 3); 136 memcpy(result, txt, maxlen - 3);
139 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; 137 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
140 result[maxlen] = '\0'; 138 result[maxlen] = '\0';
141 return result; 139 return result;
142} 140}
143 141
144char *strrpart(char *txt, int maxlen) 142char *strrpart(char *txt, int maxlen)
145{ 143{
146 char *result; 144 char *result;
147 145
148 if (!txt) 146 if (!txt)
149 return txt; 147 return txt;
150 148
151 if (strlen(txt) <= maxlen) 149 if (strlen(txt) <= maxlen)
152 return txt; 150 return txt;
153 result = xmalloc(maxlen + 1); 151 result = xmalloc(maxlen + 1);
154 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); 152 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
155 result[0] = result[1] = result[2] = '.'; 153 result[0] = result[1] = result[2] = '.';
156 return result; 154 return result;
157} 155}
158 156
159void cgit_add_ref(struct reflist *list, struct refinfo *ref) 157void cgit_add_ref(struct reflist *list, struct refinfo *ref)
160{ 158{
161 size_t size; 159 size_t size;
162 160
163 if (list->count >= list->alloc) { 161 if (list->count >= list->alloc) {
164 list->alloc += (list->alloc ? list->alloc : 4); 162 list->alloc += (list->alloc ? list->alloc : 4);
165 size = list->alloc * sizeof(struct refinfo *); 163 size = list->alloc * sizeof(struct refinfo *);
166 list->refs = xrealloc(list->refs, size); 164 list->refs = xrealloc(list->refs, size);
167 } 165 }
168 list->refs[list->count++] = ref; 166 list->refs[list->count++] = ref;
169} 167}
170 168
171struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) 169struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
172{ 170{
173 struct refinfo *ref; 171 struct refinfo *ref;
174 172
175 ref = xmalloc(sizeof (struct refinfo)); 173 ref = xmalloc(sizeof (struct refinfo));
176 ref->refname = xstrdup(refname); 174 ref->refname = xstrdup(refname);
177 ref->object = parse_object(sha1); 175 ref->object = parse_object(sha1);
178 switch (ref->object->type) { 176 switch (ref->object->type) {
179 case OBJ_TAG: 177 case OBJ_TAG:
180 ref->tag = cgit_parse_tag((struct tag *)ref->object); 178 ref->tag = cgit_parse_tag((struct tag *)ref->object);
181 break; 179 break;
182 case OBJ_COMMIT: 180 case OBJ_COMMIT:
183 ref->commit = cgit_parse_commit((struct commit *)ref->object); 181 ref->commit = cgit_parse_commit((struct commit *)ref->object);
184 break; 182 break;
185 } 183 }
186 return ref; 184 return ref;
187} 185}
188 186
189int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, 187int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
190 void *cb_data) 188 void *cb_data)
191{ 189{
192 struct reflist *list = (struct reflist *)cb_data; 190 struct reflist *list = (struct reflist *)cb_data;
193 struct refinfo *info = cgit_mk_refinfo(refname, sha1); 191 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
194 192
195 if (info) 193 if (info)
196 cgit_add_ref(list, info); 194 cgit_add_ref(list, info);
197 return 0; 195 return 0;
198} 196}
199 197
200void cgit_diff_tree_cb(struct diff_queue_struct *q, 198void cgit_diff_tree_cb(struct diff_queue_struct *q,
201 struct diff_options *options, void *data) 199 struct diff_options *options, void *data)
202{ 200{
203 int i; 201 int i;
204 202
205 for (i = 0; i < q->nr; i++) { 203 for (i = 0; i < q->nr; i++) {
206 if (q->queue[i]->status == 'U') 204 if (q->queue[i]->status == 'U')
207 continue; 205 continue;
208 ((filepair_fn)data)(q->queue[i]); 206 ((filepair_fn)data)(q->queue[i]);