This repository has been archived by the owner on Jul 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
branch.c
259 lines (213 loc) · 7.04 KB
/
branch.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* Copyright (C) 2015 by Maxim Bublis <b@codemonkey.ru>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "branch.h"
#include "utils.h"
#include <apr_strings.h>
#include <svn_dirent_uri.h>
#include <svn_hash.h>
#include <svn_string.h>
svn_boolean_t
branch_path_is_root(const branch_t *b, const char *path)
{
return strcmp(b->path, path) == 0;
}
const char *
branch_refname_from_path(const char *path, apr_pool_t *pool)
{
apr_array_header_t *strings = svn_cstring_split(path, "/ ", FALSE, pool);
svn_stringbuf_t *buf = svn_stringbuf_create_empty(pool);
for (int i = 0; i < strings->nelts; i++) {
const char *str = APR_ARRAY_IDX(strings, i, const char *);
if (buf->len > 0) {
svn_stringbuf_appendcstr(buf, "--");
}
svn_stringbuf_appendcstr(buf, str);
}
return apr_psprintf(pool, "refs/heads/%s", buf->data);
}
branch_storage_t *
branch_storage_create(apr_pool_t *pool)
{
branch_storage_t *bs = apr_pcalloc(pool, sizeof(branch_storage_t));
bs->pool = pool;
bs->tree = tree_create(pool);
bs->pfx = tree_create(pool);
bs->refnames = apr_hash_make(pool);
return bs;
}
void
branch_storage_add_prefix(branch_storage_t *bs,
const char *pfx,
svn_boolean_t is_tag,
apr_pool_t *pool)
{
tree_insert(bs->pfx, pfx, pfx, pool);
}
branch_t *
branch_storage_add_branch(branch_storage_t *bs,
const char *refname,
const char *path,
apr_pool_t *pool)
{
branch_t *b = apr_pcalloc(bs->pool, sizeof(branch_t));
b->refname = apr_pstrdup(bs->pool, refname);
b->path = apr_pstrdup(bs->pool, path);
b->dirty = TRUE;
tree_insert(bs->tree, b->path, b, pool);
svn_hash_sets(bs->refnames, b->refname, b);
return b;
}
branch_t *
branch_storage_lookup_refname(branch_storage_t *bs, const char *refname)
{
return svn_hash_gets(bs->refnames, refname);
}
branch_t *
branch_storage_lookup_path(branch_storage_t *bs, const char *path, apr_pool_t *pool)
{
branch_t *branch;
const char *branch_path, *prefix, *refname, *root, *subpath;
branch = (branch_t *) tree_match(bs->tree, path, pool);
if (branch != NULL) {
return branch;
}
prefix = tree_match(bs->pfx, path, pool);
if (prefix == NULL) {
return NULL;
}
subpath = svn_dirent_skip_ancestor(prefix, path);
if (subpath == NULL) {
return NULL;
}
if (*subpath == '\0') {
return NULL;
}
root = strchr(subpath, '/');
if (root == NULL) {
branch_path = path;
} else {
branch_path = apr_pstrndup(pool, path, root - path);
}
refname = branch_refname_from_path(branch_path, pool);
return branch_storage_add_branch(bs, refname, branch_path, pool);
}
svn_error_t *
branch_storage_dump(branch_storage_t *bs, svn_stream_t *dst, apr_pool_t *pool)
{
apr_array_header_t *values;
values = tree_values(bs->tree, "", pool, pool);
for (int i = 0; i < values->nelts; i++) {
const branch_t *branch = APR_ARRAY_IDX(values, i, branch_t *);
SVN_ERR(svn_stream_printf(dst, pool, "%s \"%s\" %d\n",
branch->refname, branch->path, branch->dirty));
}
return SVN_NO_ERROR;
}
svn_error_t *
branch_storage_dump_path(branch_storage_t *bs, const char *path, apr_pool_t *pool)
{
apr_file_t *fd;
svn_stream_t *dst;
SVN_ERR(svn_io_file_open(&fd, path,
APR_CREATE | APR_TRUNCATE | APR_BUFFERED | APR_WRITE,
APR_OS_DEFAULT, pool));
dst = svn_stream_from_aprfile2(fd, FALSE, pool);
SVN_ERR(branch_storage_dump(bs, dst, pool));
SVN_ERR(svn_stream_close(dst));
return SVN_NO_ERROR;
}
static svn_error_t *
svn_malformed_file_error(int lineno, const char *line)
{
return svn_error_createf(SVN_ERR_MALFORMED_FILE, NULL,
"line %d: %s", lineno, line);
}
svn_error_t *
branch_storage_load(branch_storage_t *bs, svn_stream_t *src, apr_pool_t *pool)
{
svn_boolean_t eof;
int lineno = 0;
while (TRUE) {
const char *prev, *next;
const char *refname, *path;
branch_t *branch;
svn_boolean_t dirty = TRUE;
svn_stringbuf_t *buf;
SVN_ERR(svn_stream_readline(src, &buf, "\n", &eof, pool));
if (eof) {
break;
}
lineno++;
prev = buf->data;
next = strchr(buf->data, ' ');
if (next == NULL) {
return svn_malformed_file_error(lineno, buf->data);
}
refname = apr_pstrndup(pool, prev, next - prev);
// Skip whitespace.
next++;
next = strchr(next, '"');
if (next == NULL) {
return svn_malformed_file_error(lineno, buf->data);
}
next++;
prev = next;
next = strchr(next, '"');
if (next == NULL) {
return svn_malformed_file_error(lineno, buf->data);
}
path = apr_pstrndup(pool, prev, next - prev);
next++;
next = strchr(next, ' ');
if (next == NULL) {
return svn_malformed_file_error(lineno, buf->data);
}
next++;
if (*next == '0') {
dirty = FALSE;
}
branch = branch_storage_lookup_refname(bs, refname);
if (branch == NULL) {
branch = branch_storage_add_branch(bs, refname, path, pool);
}
branch->dirty = dirty;
}
return SVN_NO_ERROR;
}
svn_error_t *
branch_storage_load_path(branch_storage_t *bs, const char *path, apr_pool_t *pool)
{
svn_error_t *err;
svn_stream_t *src;
svn_node_kind_t kind;
SVN_ERR(svn_io_check_path(path, &kind, pool));
if (kind == svn_node_none) {
return SVN_NO_ERROR;
}
SVN_ERR(svn_stream_open_readonly(&src, path, pool, pool));
err = branch_storage_load(bs, src, pool);
if (err) {
return svn_error_quick_wrap(err, "Malformed branches file");
}
SVN_ERR(svn_stream_close(src));
return SVN_NO_ERROR;
}