line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright (C) the libgit2 contributors. All rights reserved. |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* This file is part of libgit2, distributed under the GNU GPL v2 with |
5
|
|
|
|
|
|
|
* a Linking Exception. For full terms see the included COPYING file. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "config.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "git2/config.h" |
11
|
|
|
|
|
|
|
#include "git2/sys/config.h" |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#include "array.h" |
14
|
|
|
|
|
|
|
#include "buffer.h" |
15
|
|
|
|
|
|
|
#include "config_backend.h" |
16
|
|
|
|
|
|
|
#include "config_entries.h" |
17
|
|
|
|
|
|
|
#include "config_parse.h" |
18
|
|
|
|
|
|
|
#include "filebuf.h" |
19
|
|
|
|
|
|
|
#include "regexp.h" |
20
|
|
|
|
|
|
|
#include "sysdir.h" |
21
|
|
|
|
|
|
|
#include "wildmatch.h" |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
/* Max depth for [include] directives */ |
24
|
|
|
|
|
|
|
#define MAX_INCLUDE_DEPTH 10 |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
typedef struct config_file { |
27
|
|
|
|
|
|
|
git_futils_filestamp stamp; |
28
|
|
|
|
|
|
|
git_oid checksum; |
29
|
|
|
|
|
|
|
char *path; |
30
|
|
|
|
|
|
|
git_array_t(struct config_file) includes; |
31
|
|
|
|
|
|
|
} config_file; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
typedef struct { |
34
|
|
|
|
|
|
|
git_config_backend parent; |
35
|
|
|
|
|
|
|
git_mutex values_mutex; |
36
|
|
|
|
|
|
|
git_config_entries *entries; |
37
|
|
|
|
|
|
|
const git_repository *repo; |
38
|
|
|
|
|
|
|
git_config_level_t level; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
git_array_t(git_config_parser) readers; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
bool locked; |
43
|
|
|
|
|
|
|
git_filebuf locked_buf; |
44
|
|
|
|
|
|
|
git_buf locked_content; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
config_file file; |
47
|
|
|
|
|
|
|
} config_file_backend; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
typedef struct { |
50
|
|
|
|
|
|
|
const git_repository *repo; |
51
|
|
|
|
|
|
|
config_file *file; |
52
|
|
|
|
|
|
|
git_config_entries *entries; |
53
|
|
|
|
|
|
|
git_config_level_t level; |
54
|
|
|
|
|
|
|
unsigned int depth; |
55
|
|
|
|
|
|
|
} config_file_parse_data; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
static int config_file_read(git_config_entries *entries, const git_repository *repo, config_file *file, git_config_level_t level, int depth); |
58
|
|
|
|
|
|
|
static int config_file_read_buffer(git_config_entries *entries, const git_repository *repo, config_file *file, git_config_level_t level, int depth, const char *buf, size_t buflen); |
59
|
|
|
|
|
|
|
static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char *value); |
60
|
|
|
|
|
|
|
static char *escape_value(const char *ptr); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
/** |
63
|
|
|
|
|
|
|
* Take the current values map from the backend and increase its |
64
|
|
|
|
|
|
|
* refcount. This is its own function to make sure we use the mutex to |
65
|
|
|
|
|
|
|
* avoid the map pointer from changing under us. |
66
|
|
|
|
|
|
|
*/ |
67
|
1818
|
|
|
|
|
|
static int config_file_entries_take(git_config_entries **out, config_file_backend *b) |
68
|
|
|
|
|
|
|
{ |
69
|
|
|
|
|
|
|
int error; |
70
|
|
|
|
|
|
|
|
71
|
1818
|
50
|
|
|
|
|
if ((error = git_mutex_lock(&b->values_mutex)) < 0) { |
72
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock config backend"); |
73
|
0
|
|
|
|
|
|
return error; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
1818
|
|
|
|
|
|
git_config_entries_incref(b->entries); |
77
|
1818
|
|
|
|
|
|
*out = b->entries; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
git_mutex_unlock(&b->values_mutex); |
80
|
|
|
|
|
|
|
|
81
|
1818
|
|
|
|
|
|
return 0; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
96
|
|
|
|
|
|
static void config_file_clear(config_file *file) |
85
|
|
|
|
|
|
|
{ |
86
|
|
|
|
|
|
|
config_file *include; |
87
|
|
|
|
|
|
|
uint32_t i; |
88
|
|
|
|
|
|
|
|
89
|
96
|
50
|
|
|
|
|
if (file == NULL) |
90
|
0
|
|
|
|
|
|
return; |
91
|
|
|
|
|
|
|
|
92
|
96
|
50
|
|
|
|
|
git_array_foreach(file->includes, i, include) { |
|
|
0
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
config_file_clear(include); |
94
|
|
|
|
|
|
|
} |
95
|
96
|
|
|
|
|
|
git_array_clear(file->includes); |
96
|
|
|
|
|
|
|
|
97
|
96
|
|
|
|
|
|
git__free(file->path); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
96
|
|
|
|
|
|
static int config_file_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo) |
101
|
|
|
|
|
|
|
{ |
102
|
96
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
103
|
|
|
|
|
|
|
int res; |
104
|
|
|
|
|
|
|
|
105
|
96
|
|
|
|
|
|
b->level = level; |
106
|
96
|
|
|
|
|
|
b->repo = repo; |
107
|
|
|
|
|
|
|
|
108
|
96
|
50
|
|
|
|
|
if ((res = git_config_entries_new(&b->entries)) < 0) |
109
|
0
|
|
|
|
|
|
return res; |
110
|
|
|
|
|
|
|
|
111
|
96
|
100
|
|
|
|
|
if (!git_path_exists(b->file.path)) |
112
|
47
|
|
|
|
|
|
return 0; |
113
|
|
|
|
|
|
|
|
114
|
49
|
50
|
|
|
|
|
if (res < 0 || (res = config_file_read(b->entries, repo, &b->file, level, 0)) < 0) { |
|
|
50
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
git_config_entries_free(b->entries); |
116
|
0
|
|
|
|
|
|
b->entries = NULL; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
49
|
|
|
|
|
|
return res; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
1763
|
|
|
|
|
|
static int config_file_is_modified(int *modified, config_file *file) |
123
|
|
|
|
|
|
|
{ |
124
|
|
|
|
|
|
|
config_file *include; |
125
|
1763
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
126
|
|
|
|
|
|
|
git_oid hash; |
127
|
|
|
|
|
|
|
uint32_t i; |
128
|
1763
|
|
|
|
|
|
int error = 0; |
129
|
|
|
|
|
|
|
|
130
|
1763
|
|
|
|
|
|
*modified = 0; |
131
|
|
|
|
|
|
|
|
132
|
1763
|
100
|
|
|
|
|
if (!git_futils_filestamp_check(&file->stamp, file->path)) |
133
|
897
|
|
|
|
|
|
goto check_includes; |
134
|
|
|
|
|
|
|
|
135
|
866
|
100
|
|
|
|
|
if ((error = git_futils_readbuffer(&buf, file->path)) < 0) |
136
|
847
|
|
|
|
|
|
goto out; |
137
|
|
|
|
|
|
|
|
138
|
19
|
50
|
|
|
|
|
if ((error = git_hash_buf(&hash, buf.ptr, buf.size)) < 0) |
139
|
0
|
|
|
|
|
|
goto out; |
140
|
|
|
|
|
|
|
|
141
|
19
|
50
|
|
|
|
|
if (!git_oid_equal(&hash, &file->checksum)) { |
142
|
19
|
|
|
|
|
|
*modified = 1; |
143
|
19
|
|
|
|
|
|
goto out; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
check_includes: |
147
|
897
|
50
|
|
|
|
|
git_array_foreach(file->includes, i, include) { |
|
|
0
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if ((error = config_file_is_modified(modified, include)) < 0 || *modified) |
|
|
0
|
|
|
|
|
|
149
|
|
|
|
|
|
|
goto out; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
out: |
153
|
1763
|
|
|
|
|
|
git_buf_dispose(&buf); |
154
|
|
|
|
|
|
|
|
155
|
1763
|
|
|
|
|
|
return error; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
68
|
|
|
|
|
|
static int config_file_set_entries(git_config_backend *cfg, git_config_entries *entries) |
159
|
|
|
|
|
|
|
{ |
160
|
68
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
161
|
68
|
|
|
|
|
|
git_config_entries *old = NULL; |
162
|
|
|
|
|
|
|
config_file *include; |
163
|
|
|
|
|
|
|
int error; |
164
|
|
|
|
|
|
|
uint32_t i; |
165
|
|
|
|
|
|
|
|
166
|
68
|
50
|
|
|
|
|
if (b->parent.readonly) { |
167
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_CONFIG, "this backend is read-only"); |
168
|
0
|
|
|
|
|
|
return -1; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
68
|
50
|
|
|
|
|
git_array_foreach(b->file.includes, i, include) |
|
|
0
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
config_file_clear(include); |
173
|
68
|
|
|
|
|
|
git_array_clear(b->file.includes); |
174
|
|
|
|
|
|
|
|
175
|
68
|
50
|
|
|
|
|
if ((error = git_mutex_lock(&b->values_mutex)) < 0) { |
176
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock config backend"); |
177
|
0
|
|
|
|
|
|
goto out; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
68
|
|
|
|
|
|
old = b->entries; |
181
|
68
|
|
|
|
|
|
b->entries = entries; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
git_mutex_unlock(&b->values_mutex); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
out: |
186
|
68
|
|
|
|
|
|
git_config_entries_free(old); |
187
|
68
|
|
|
|
|
|
return error; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
49
|
|
|
|
|
|
static int config_file_refresh_from_buffer(git_config_backend *cfg, const char *buf, size_t buflen) |
191
|
|
|
|
|
|
|
{ |
192
|
49
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
193
|
49
|
|
|
|
|
|
git_config_entries *entries = NULL; |
194
|
|
|
|
|
|
|
int error; |
195
|
|
|
|
|
|
|
|
196
|
49
|
50
|
|
|
|
|
if ((error = git_config_entries_new(&entries)) < 0 || |
|
|
50
|
|
|
|
|
|
197
|
49
|
|
|
|
|
|
(error = config_file_read_buffer(entries, b->repo, &b->file, |
198
|
49
|
50
|
|
|
|
|
b->level, 0, buf, buflen)) < 0 || |
199
|
49
|
|
|
|
|
|
(error = config_file_set_entries(cfg, entries)) < 0) |
200
|
|
|
|
|
|
|
goto out; |
201
|
|
|
|
|
|
|
|
202
|
49
|
|
|
|
|
|
entries = NULL; |
203
|
|
|
|
|
|
|
out: |
204
|
49
|
|
|
|
|
|
git_config_entries_free(entries); |
205
|
49
|
|
|
|
|
|
return error; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
1763
|
|
|
|
|
|
static int config_file_refresh(git_config_backend *cfg) |
209
|
|
|
|
|
|
|
{ |
210
|
1763
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
211
|
1763
|
|
|
|
|
|
git_config_entries *entries = NULL; |
212
|
|
|
|
|
|
|
int error, modified; |
213
|
|
|
|
|
|
|
|
214
|
1763
|
50
|
|
|
|
|
if (cfg->readonly) |
215
|
0
|
|
|
|
|
|
return 0; |
216
|
|
|
|
|
|
|
|
217
|
1763
|
100
|
|
|
|
|
if ((error = config_file_is_modified(&modified, &b->file)) < 0 && error != GIT_ENOTFOUND) |
|
|
50
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
goto out; |
219
|
|
|
|
|
|
|
|
220
|
1763
|
100
|
|
|
|
|
if (!modified) |
221
|
1744
|
|
|
|
|
|
return 0; |
222
|
|
|
|
|
|
|
|
223
|
19
|
50
|
|
|
|
|
if ((error = git_config_entries_new(&entries)) < 0 || |
|
|
50
|
|
|
|
|
|
224
|
19
|
50
|
|
|
|
|
(error = config_file_read(entries, b->repo, &b->file, b->level, 0)) < 0 || |
225
|
19
|
|
|
|
|
|
(error = config_file_set_entries(cfg, entries)) < 0) |
226
|
|
|
|
|
|
|
goto out; |
227
|
|
|
|
|
|
|
|
228
|
19
|
|
|
|
|
|
entries = NULL; |
229
|
|
|
|
|
|
|
out: |
230
|
19
|
|
|
|
|
|
git_config_entries_free(entries); |
231
|
|
|
|
|
|
|
|
232
|
1763
|
50
|
|
|
|
|
return (error == GIT_ENOTFOUND) ? 0 : error; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
96
|
|
|
|
|
|
static void config_file_free(git_config_backend *_backend) |
236
|
|
|
|
|
|
|
{ |
237
|
96
|
|
|
|
|
|
config_file_backend *backend = GIT_CONTAINER_OF(_backend, config_file_backend, parent); |
238
|
|
|
|
|
|
|
|
239
|
96
|
50
|
|
|
|
|
if (backend == NULL) |
240
|
0
|
|
|
|
|
|
return; |
241
|
|
|
|
|
|
|
|
242
|
96
|
|
|
|
|
|
config_file_clear(&backend->file); |
243
|
96
|
|
|
|
|
|
git_config_entries_free(backend->entries); |
244
|
|
|
|
|
|
|
git_mutex_free(&backend->values_mutex); |
245
|
96
|
|
|
|
|
|
git__free(backend); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
994
|
|
|
|
|
|
static int config_file_iterator( |
249
|
|
|
|
|
|
|
git_config_iterator **iter, |
250
|
|
|
|
|
|
|
struct git_config_backend *backend) |
251
|
|
|
|
|
|
|
{ |
252
|
994
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(backend, config_file_backend, parent); |
253
|
994
|
|
|
|
|
|
git_config_entries *dupped = NULL, *entries = NULL; |
254
|
|
|
|
|
|
|
int error; |
255
|
|
|
|
|
|
|
|
256
|
994
|
50
|
|
|
|
|
if ((error = config_file_refresh(backend)) < 0 || |
|
|
50
|
|
|
|
|
|
257
|
994
|
50
|
|
|
|
|
(error = config_file_entries_take(&entries, b)) < 0 || |
258
|
994
|
|
|
|
|
|
(error = git_config_entries_dup(&dupped, entries)) < 0 || |
259
|
994
|
|
|
|
|
|
(error = git_config_entries_iterator_new(iter, dupped)) < 0) |
260
|
|
|
|
|
|
|
goto out; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
out: |
263
|
|
|
|
|
|
|
/* Let iterator delete duplicated entries when it's done */ |
264
|
994
|
|
|
|
|
|
git_config_entries_free(entries); |
265
|
994
|
|
|
|
|
|
git_config_entries_free(dupped); |
266
|
994
|
|
|
|
|
|
return error; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
966
|
|
|
|
|
|
static int config_file_snapshot(git_config_backend **out, git_config_backend *backend) |
270
|
|
|
|
|
|
|
{ |
271
|
966
|
|
|
|
|
|
return git_config_backend_snapshot(out, backend); |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
40
|
|
|
|
|
|
static int config_file_set(git_config_backend *cfg, const char *name, const char *value) |
275
|
|
|
|
|
|
|
{ |
276
|
40
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
277
|
|
|
|
|
|
|
git_config_entries *entries; |
278
|
|
|
|
|
|
|
git_config_entry *existing; |
279
|
40
|
|
|
|
|
|
char *key, *esc_value = NULL; |
280
|
|
|
|
|
|
|
int error; |
281
|
|
|
|
|
|
|
|
282
|
40
|
50
|
|
|
|
|
if ((error = git_config__normalize_name(name, &key)) < 0) |
283
|
0
|
|
|
|
|
|
return error; |
284
|
|
|
|
|
|
|
|
285
|
40
|
50
|
|
|
|
|
if ((error = config_file_entries_take(&entries, b)) < 0) |
286
|
0
|
|
|
|
|
|
return error; |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
/* Check whether we'd be modifying an included or multivar key */ |
289
|
40
|
100
|
|
|
|
|
if ((error = git_config_entries_get_unique(&existing, entries, key)) < 0) { |
290
|
36
|
50
|
|
|
|
|
if (error != GIT_ENOTFOUND) |
291
|
0
|
|
|
|
|
|
goto out; |
292
|
36
|
|
|
|
|
|
error = 0; |
293
|
4
|
50
|
|
|
|
|
} else if ((!existing->value && !value) || |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
294
|
4
|
50
|
|
|
|
|
(existing->value && value && !strcmp(existing->value, value))) { |
|
|
100
|
|
|
|
|
|
295
|
|
|
|
|
|
|
/* don't update if old and new values already match */ |
296
|
1
|
|
|
|
|
|
error = 0; |
297
|
1
|
|
|
|
|
|
goto out; |
298
|
|
|
|
|
|
|
} |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
/* No early returns due to sanity checks, let's write it out and refresh */ |
301
|
39
|
50
|
|
|
|
|
if (value) { |
302
|
39
|
|
|
|
|
|
esc_value = escape_value(value); |
303
|
39
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(esc_value); |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
39
|
50
|
|
|
|
|
if ((error = config_file_write(b, name, key, NULL, esc_value)) < 0) |
307
|
0
|
|
|
|
|
|
goto out; |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
out: |
310
|
40
|
|
|
|
|
|
git_config_entries_free(entries); |
311
|
40
|
|
|
|
|
|
git__free(esc_value); |
312
|
40
|
|
|
|
|
|
git__free(key); |
313
|
40
|
|
|
|
|
|
return error; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
/* release the map containing the entry as an equivalent to freeing it */ |
317
|
69
|
|
|
|
|
|
static void config_file_entry_free(git_config_entry *entry) |
318
|
|
|
|
|
|
|
{ |
319
|
69
|
|
|
|
|
|
git_config_entries *entries = (git_config_entries *) entry->payload; |
320
|
69
|
|
|
|
|
|
git_config_entries_free(entries); |
321
|
69
|
|
|
|
|
|
} |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
/* |
324
|
|
|
|
|
|
|
* Internal function that actually gets the value in string form |
325
|
|
|
|
|
|
|
*/ |
326
|
769
|
|
|
|
|
|
static int config_file_get(git_config_backend *cfg, const char *key, git_config_entry **out) |
327
|
|
|
|
|
|
|
{ |
328
|
769
|
|
|
|
|
|
config_file_backend *h = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
329
|
769
|
|
|
|
|
|
git_config_entries *entries = NULL; |
330
|
|
|
|
|
|
|
git_config_entry *entry; |
331
|
769
|
|
|
|
|
|
int error = 0; |
332
|
|
|
|
|
|
|
|
333
|
769
|
50
|
|
|
|
|
if (!h->parent.readonly && ((error = config_file_refresh(cfg)) < 0)) |
|
|
50
|
|
|
|
|
|
334
|
0
|
|
|
|
|
|
return error; |
335
|
|
|
|
|
|
|
|
336
|
769
|
50
|
|
|
|
|
if ((error = config_file_entries_take(&entries, h)) < 0) |
337
|
0
|
|
|
|
|
|
return error; |
338
|
|
|
|
|
|
|
|
339
|
769
|
100
|
|
|
|
|
if ((error = (git_config_entries_get(&entry, entries, key))) < 0) { |
340
|
700
|
|
|
|
|
|
git_config_entries_free(entries); |
341
|
700
|
|
|
|
|
|
return error; |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
69
|
|
|
|
|
|
entry->free = config_file_entry_free; |
345
|
69
|
|
|
|
|
|
entry->payload = entries; |
346
|
69
|
|
|
|
|
|
*out = entry; |
347
|
|
|
|
|
|
|
|
348
|
769
|
|
|
|
|
|
return 0; |
349
|
|
|
|
|
|
|
} |
350
|
|
|
|
|
|
|
|
351
|
5
|
|
|
|
|
|
static int config_file_set_multivar( |
352
|
|
|
|
|
|
|
git_config_backend *cfg, const char *name, const char *regexp, const char *value) |
353
|
|
|
|
|
|
|
{ |
354
|
5
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
355
|
|
|
|
|
|
|
git_regexp preg; |
356
|
|
|
|
|
|
|
int result; |
357
|
|
|
|
|
|
|
char *key; |
358
|
|
|
|
|
|
|
|
359
|
5
|
50
|
|
|
|
|
assert(regexp); |
360
|
|
|
|
|
|
|
|
361
|
5
|
50
|
|
|
|
|
if ((result = git_config__normalize_name(name, &key)) < 0) |
362
|
0
|
|
|
|
|
|
return result; |
363
|
|
|
|
|
|
|
|
364
|
5
|
50
|
|
|
|
|
if ((result = git_regexp_compile(&preg, regexp, 0)) < 0) |
365
|
0
|
|
|
|
|
|
goto out; |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
/* If we do have it, set call config_file_write() and reload */ |
368
|
5
|
50
|
|
|
|
|
if ((result = config_file_write(b, name, key, &preg, value)) < 0) |
369
|
0
|
|
|
|
|
|
goto out; |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
out: |
372
|
5
|
|
|
|
|
|
git__free(key); |
373
|
5
|
|
|
|
|
|
git_regexp_dispose(&preg); |
374
|
|
|
|
|
|
|
|
375
|
5
|
|
|
|
|
|
return result; |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
|
378
|
15
|
|
|
|
|
|
static int config_file_delete(git_config_backend *cfg, const char *name) |
379
|
|
|
|
|
|
|
{ |
380
|
15
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
381
|
15
|
|
|
|
|
|
git_config_entries *entries = NULL; |
382
|
|
|
|
|
|
|
git_config_entry *entry; |
383
|
15
|
|
|
|
|
|
char *key = NULL; |
384
|
|
|
|
|
|
|
int error; |
385
|
|
|
|
|
|
|
|
386
|
15
|
50
|
|
|
|
|
if ((error = git_config__normalize_name(name, &key)) < 0) |
387
|
0
|
|
|
|
|
|
goto out; |
388
|
|
|
|
|
|
|
|
389
|
15
|
50
|
|
|
|
|
if ((error = config_file_entries_take(&entries, b)) < 0) |
390
|
0
|
|
|
|
|
|
goto out; |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
/* Check whether we'd be modifying an included or multivar key */ |
393
|
15
|
100
|
|
|
|
|
if ((error = git_config_entries_get_unique(&entry, entries, key)) < 0) { |
394
|
10
|
50
|
|
|
|
|
if (error == GIT_ENOTFOUND) |
395
|
10
|
|
|
|
|
|
git_error_set(GIT_ERROR_CONFIG, "could not find key '%s' to delete", name); |
396
|
10
|
|
|
|
|
|
goto out; |
397
|
|
|
|
|
|
|
} |
398
|
|
|
|
|
|
|
|
399
|
5
|
50
|
|
|
|
|
if ((error = config_file_write(b, name, entry->name, NULL, NULL)) < 0) |
400
|
0
|
|
|
|
|
|
goto out; |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
out: |
403
|
15
|
|
|
|
|
|
git_config_entries_free(entries); |
404
|
15
|
|
|
|
|
|
git__free(key); |
405
|
15
|
|
|
|
|
|
return error; |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
|
408
|
0
|
|
|
|
|
|
static int config_file_delete_multivar(git_config_backend *cfg, const char *name, const char *regexp) |
409
|
|
|
|
|
|
|
{ |
410
|
0
|
|
|
|
|
|
config_file_backend *b = GIT_CONTAINER_OF(cfg, config_file_backend, parent); |
411
|
0
|
|
|
|
|
|
git_config_entries *entries = NULL; |
412
|
0
|
|
|
|
|
|
git_config_entry *entry = NULL; |
413
|
0
|
|
|
|
|
|
git_regexp preg = GIT_REGEX_INIT; |
414
|
0
|
|
|
|
|
|
char *key = NULL; |
415
|
|
|
|
|
|
|
int result; |
416
|
|
|
|
|
|
|
|
417
|
0
|
0
|
|
|
|
|
if ((result = git_config__normalize_name(name, &key)) < 0) |
418
|
0
|
|
|
|
|
|
goto out; |
419
|
|
|
|
|
|
|
|
420
|
0
|
0
|
|
|
|
|
if ((result = config_file_entries_take(&entries, b)) < 0) |
421
|
0
|
|
|
|
|
|
goto out; |
422
|
|
|
|
|
|
|
|
423
|
0
|
0
|
|
|
|
|
if ((result = git_config_entries_get(&entry, entries, key)) < 0) { |
424
|
0
|
0
|
|
|
|
|
if (result == GIT_ENOTFOUND) |
425
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_CONFIG, "could not find key '%s' to delete", name); |
426
|
0
|
|
|
|
|
|
goto out; |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
|
429
|
0
|
0
|
|
|
|
|
if ((result = git_regexp_compile(&preg, regexp, 0)) < 0) |
430
|
0
|
|
|
|
|
|
goto out; |
431
|
|
|
|
|
|
|
|
432
|
0
|
0
|
|
|
|
|
if ((result = config_file_write(b, name, key, &preg, NULL)) < 0) |
433
|
0
|
|
|
|
|
|
goto out; |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
out: |
436
|
0
|
|
|
|
|
|
git_config_entries_free(entries); |
437
|
0
|
|
|
|
|
|
git__free(key); |
438
|
0
|
|
|
|
|
|
git_regexp_dispose(&preg); |
439
|
0
|
|
|
|
|
|
return result; |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
|
442
|
0
|
|
|
|
|
|
static int config_file_lock(git_config_backend *_cfg) |
443
|
|
|
|
|
|
|
{ |
444
|
0
|
|
|
|
|
|
config_file_backend *cfg = GIT_CONTAINER_OF(_cfg, config_file_backend, parent); |
445
|
|
|
|
|
|
|
int error; |
446
|
|
|
|
|
|
|
|
447
|
0
|
0
|
|
|
|
|
if ((error = git_filebuf_open(&cfg->locked_buf, cfg->file.path, 0, GIT_CONFIG_FILE_MODE)) < 0) |
448
|
0
|
|
|
|
|
|
return error; |
449
|
|
|
|
|
|
|
|
450
|
0
|
|
|
|
|
|
error = git_futils_readbuffer(&cfg->locked_content, cfg->file.path); |
451
|
0
|
0
|
|
|
|
|
if (error < 0 && error != GIT_ENOTFOUND) { |
|
|
0
|
|
|
|
|
|
452
|
0
|
|
|
|
|
|
git_filebuf_cleanup(&cfg->locked_buf); |
453
|
0
|
|
|
|
|
|
return error; |
454
|
|
|
|
|
|
|
} |
455
|
|
|
|
|
|
|
|
456
|
0
|
|
|
|
|
|
cfg->locked = true; |
457
|
0
|
|
|
|
|
|
return 0; |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
} |
460
|
|
|
|
|
|
|
|
461
|
0
|
|
|
|
|
|
static int config_file_unlock(git_config_backend *_cfg, int success) |
462
|
|
|
|
|
|
|
{ |
463
|
0
|
|
|
|
|
|
config_file_backend *cfg = GIT_CONTAINER_OF(_cfg, config_file_backend, parent); |
464
|
0
|
|
|
|
|
|
int error = 0; |
465
|
|
|
|
|
|
|
|
466
|
0
|
0
|
|
|
|
|
if (success) { |
467
|
0
|
|
|
|
|
|
git_filebuf_write(&cfg->locked_buf, cfg->locked_content.ptr, cfg->locked_content.size); |
468
|
0
|
|
|
|
|
|
error = git_filebuf_commit(&cfg->locked_buf); |
469
|
|
|
|
|
|
|
} |
470
|
|
|
|
|
|
|
|
471
|
0
|
|
|
|
|
|
git_filebuf_cleanup(&cfg->locked_buf); |
472
|
0
|
|
|
|
|
|
git_buf_dispose(&cfg->locked_content); |
473
|
0
|
|
|
|
|
|
cfg->locked = false; |
474
|
|
|
|
|
|
|
|
475
|
0
|
|
|
|
|
|
return error; |
476
|
|
|
|
|
|
|
} |
477
|
|
|
|
|
|
|
|
478
|
96
|
|
|
|
|
|
int git_config_backend_from_file(git_config_backend **out, const char *path) |
479
|
|
|
|
|
|
|
{ |
480
|
|
|
|
|
|
|
config_file_backend *backend; |
481
|
|
|
|
|
|
|
|
482
|
96
|
|
|
|
|
|
backend = git__calloc(1, sizeof(config_file_backend)); |
483
|
96
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(backend); |
484
|
|
|
|
|
|
|
|
485
|
96
|
|
|
|
|
|
backend->parent.version = GIT_CONFIG_BACKEND_VERSION; |
486
|
96
|
|
|
|
|
|
git_mutex_init(&backend->values_mutex); |
487
|
|
|
|
|
|
|
|
488
|
96
|
|
|
|
|
|
backend->file.path = git__strdup(path); |
489
|
96
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(backend->file.path); |
490
|
96
|
|
|
|
|
|
git_array_init(backend->file.includes); |
491
|
|
|
|
|
|
|
|
492
|
96
|
|
|
|
|
|
backend->parent.open = config_file_open; |
493
|
96
|
|
|
|
|
|
backend->parent.get = config_file_get; |
494
|
96
|
|
|
|
|
|
backend->parent.set = config_file_set; |
495
|
96
|
|
|
|
|
|
backend->parent.set_multivar = config_file_set_multivar; |
496
|
96
|
|
|
|
|
|
backend->parent.del = config_file_delete; |
497
|
96
|
|
|
|
|
|
backend->parent.del_multivar = config_file_delete_multivar; |
498
|
96
|
|
|
|
|
|
backend->parent.iterator = config_file_iterator; |
499
|
96
|
|
|
|
|
|
backend->parent.snapshot = config_file_snapshot; |
500
|
96
|
|
|
|
|
|
backend->parent.lock = config_file_lock; |
501
|
96
|
|
|
|
|
|
backend->parent.unlock = config_file_unlock; |
502
|
96
|
|
|
|
|
|
backend->parent.free = config_file_free; |
503
|
|
|
|
|
|
|
|
504
|
96
|
|
|
|
|
|
*out = (git_config_backend *)backend; |
505
|
|
|
|
|
|
|
|
506
|
96
|
|
|
|
|
|
return 0; |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
|
509
|
0
|
|
|
|
|
|
static int included_path(git_buf *out, const char *dir, const char *path) |
510
|
|
|
|
|
|
|
{ |
511
|
|
|
|
|
|
|
/* From the user's home */ |
512
|
0
|
0
|
|
|
|
|
if (path[0] == '~' && path[1] == '/') |
|
|
0
|
|
|
|
|
|
513
|
0
|
|
|
|
|
|
return git_sysdir_expand_global_file(out, &path[1]); |
514
|
|
|
|
|
|
|
|
515
|
0
|
|
|
|
|
|
return git_path_join_unrooted(out, path, dir, NULL); |
516
|
|
|
|
|
|
|
} |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
/* Escape the values to write them to the file */ |
519
|
43
|
|
|
|
|
|
static char *escape_value(const char *ptr) |
520
|
|
|
|
|
|
|
{ |
521
|
|
|
|
|
|
|
git_buf buf; |
522
|
|
|
|
|
|
|
size_t len; |
523
|
|
|
|
|
|
|
const char *esc; |
524
|
|
|
|
|
|
|
|
525
|
43
|
50
|
|
|
|
|
assert(ptr); |
526
|
|
|
|
|
|
|
|
527
|
43
|
|
|
|
|
|
len = strlen(ptr); |
528
|
43
|
50
|
|
|
|
|
if (!len) |
529
|
0
|
|
|
|
|
|
return git__calloc(1, sizeof(char)); |
530
|
|
|
|
|
|
|
|
531
|
43
|
50
|
|
|
|
|
if (git_buf_init(&buf, len) < 0) |
532
|
0
|
|
|
|
|
|
return NULL; |
533
|
|
|
|
|
|
|
|
534
|
593
|
100
|
|
|
|
|
while (*ptr != '\0') { |
535
|
550
|
50
|
|
|
|
|
if ((esc = strchr(git_config_escaped, *ptr)) != NULL) { |
536
|
0
|
|
|
|
|
|
git_buf_putc(&buf, '\\'); |
537
|
0
|
|
|
|
|
|
git_buf_putc(&buf, git_config_escapes[esc - git_config_escaped]); |
538
|
|
|
|
|
|
|
} else { |
539
|
550
|
|
|
|
|
|
git_buf_putc(&buf, *ptr); |
540
|
|
|
|
|
|
|
} |
541
|
550
|
|
|
|
|
|
ptr++; |
542
|
|
|
|
|
|
|
} |
543
|
|
|
|
|
|
|
|
544
|
43
|
50
|
|
|
|
|
if (git_buf_oom(&buf)) |
545
|
0
|
|
|
|
|
|
return NULL; |
546
|
|
|
|
|
|
|
|
547
|
43
|
|
|
|
|
|
return git_buf_detach(&buf); |
548
|
|
|
|
|
|
|
} |
549
|
|
|
|
|
|
|
|
550
|
0
|
|
|
|
|
|
static int parse_include(config_file_parse_data *parse_data, const char *file) |
551
|
|
|
|
|
|
|
{ |
552
|
|
|
|
|
|
|
config_file *include; |
553
|
0
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT; |
554
|
|
|
|
|
|
|
char *dir; |
555
|
|
|
|
|
|
|
int result; |
556
|
|
|
|
|
|
|
|
557
|
0
|
0
|
|
|
|
|
if (!file) |
558
|
0
|
|
|
|
|
|
return 0; |
559
|
|
|
|
|
|
|
|
560
|
0
|
0
|
|
|
|
|
if ((result = git_path_dirname_r(&path, parse_data->file->path)) < 0) |
561
|
0
|
|
|
|
|
|
return result; |
562
|
|
|
|
|
|
|
|
563
|
0
|
|
|
|
|
|
dir = git_buf_detach(&path); |
564
|
0
|
|
|
|
|
|
result = included_path(&path, dir, file); |
565
|
0
|
|
|
|
|
|
git__free(dir); |
566
|
|
|
|
|
|
|
|
567
|
0
|
0
|
|
|
|
|
if (result < 0) |
568
|
0
|
|
|
|
|
|
return result; |
569
|
|
|
|
|
|
|
|
570
|
0
|
0
|
|
|
|
|
include = git_array_alloc(parse_data->file->includes); |
|
|
0
|
|
|
|
|
|
571
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(include); |
572
|
0
|
|
|
|
|
|
memset(include, 0, sizeof(*include)); |
573
|
0
|
|
|
|
|
|
git_array_init(include->includes); |
574
|
0
|
|
|
|
|
|
include->path = git_buf_detach(&path); |
575
|
|
|
|
|
|
|
|
576
|
0
|
|
|
|
|
|
result = config_file_read(parse_data->entries, parse_data->repo, include, |
577
|
0
|
|
|
|
|
|
parse_data->level, parse_data->depth+1); |
578
|
|
|
|
|
|
|
|
579
|
0
|
0
|
|
|
|
|
if (result == GIT_ENOTFOUND) { |
580
|
0
|
|
|
|
|
|
git_error_clear(); |
581
|
0
|
|
|
|
|
|
result = 0; |
582
|
|
|
|
|
|
|
} |
583
|
|
|
|
|
|
|
|
584
|
0
|
|
|
|
|
|
return result; |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
|
587
|
0
|
|
|
|
|
|
static int do_match_gitdir( |
588
|
|
|
|
|
|
|
int *matches, |
589
|
|
|
|
|
|
|
const git_repository *repo, |
590
|
|
|
|
|
|
|
const char *cfg_file, |
591
|
|
|
|
|
|
|
const char *condition, |
592
|
|
|
|
|
|
|
bool case_insensitive) |
593
|
|
|
|
|
|
|
{ |
594
|
0
|
|
|
|
|
|
git_buf pattern = GIT_BUF_INIT, gitdir = GIT_BUF_INIT; |
595
|
|
|
|
|
|
|
int error; |
596
|
|
|
|
|
|
|
|
597
|
0
|
0
|
|
|
|
|
if (condition[0] == '.' && git_path_is_dirsep(condition[1])) { |
|
|
0
|
|
|
|
|
|
598
|
0
|
|
|
|
|
|
git_path_dirname_r(&pattern, cfg_file); |
599
|
0
|
|
|
|
|
|
git_buf_joinpath(&pattern, pattern.ptr, condition + 2); |
600
|
0
|
0
|
|
|
|
|
} else if (condition[0] == '~' && git_path_is_dirsep(condition[1])) |
|
|
0
|
|
|
|
|
|
601
|
0
|
|
|
|
|
|
git_sysdir_expand_global_file(&pattern, condition + 1); |
602
|
0
|
0
|
|
|
|
|
else if (!git_path_is_absolute(condition)) |
603
|
0
|
|
|
|
|
|
git_buf_joinpath(&pattern, "**", condition); |
604
|
|
|
|
|
|
|
else |
605
|
0
|
|
|
|
|
|
git_buf_sets(&pattern, condition); |
606
|
|
|
|
|
|
|
|
607
|
0
|
0
|
|
|
|
|
if (git_path_is_dirsep(condition[strlen(condition) - 1])) |
608
|
0
|
|
|
|
|
|
git_buf_puts(&pattern, "**"); |
609
|
|
|
|
|
|
|
|
610
|
0
|
0
|
|
|
|
|
if (git_buf_oom(&pattern)) { |
611
|
0
|
|
|
|
|
|
error = -1; |
612
|
0
|
|
|
|
|
|
goto out; |
613
|
|
|
|
|
|
|
} |
614
|
|
|
|
|
|
|
|
615
|
0
|
0
|
|
|
|
|
if ((error = git_repository_item_path(&gitdir, repo, GIT_REPOSITORY_ITEM_GITDIR)) < 0) |
616
|
0
|
|
|
|
|
|
goto out; |
617
|
|
|
|
|
|
|
|
618
|
0
|
0
|
|
|
|
|
if (git_path_is_dirsep(gitdir.ptr[gitdir.size - 1])) |
619
|
0
|
|
|
|
|
|
git_buf_truncate(&gitdir, gitdir.size - 1); |
620
|
|
|
|
|
|
|
|
621
|
0
|
|
|
|
|
|
*matches = wildmatch(pattern.ptr, gitdir.ptr, |
622
|
0
|
|
|
|
|
|
WM_PATHNAME | (case_insensitive ? WM_CASEFOLD : 0)) == WM_MATCH; |
623
|
|
|
|
|
|
|
out: |
624
|
0
|
|
|
|
|
|
git_buf_dispose(&pattern); |
625
|
0
|
|
|
|
|
|
git_buf_dispose(&gitdir); |
626
|
0
|
|
|
|
|
|
return error; |
627
|
|
|
|
|
|
|
} |
628
|
|
|
|
|
|
|
|
629
|
0
|
|
|
|
|
|
static int conditional_match_gitdir( |
630
|
|
|
|
|
|
|
int *matches, |
631
|
|
|
|
|
|
|
const git_repository *repo, |
632
|
|
|
|
|
|
|
const char *cfg_file, |
633
|
|
|
|
|
|
|
const char *value) |
634
|
|
|
|
|
|
|
{ |
635
|
0
|
|
|
|
|
|
return do_match_gitdir(matches, repo, cfg_file, value, false); |
636
|
|
|
|
|
|
|
} |
637
|
|
|
|
|
|
|
|
638
|
0
|
|
|
|
|
|
static int conditional_match_gitdir_i( |
639
|
|
|
|
|
|
|
int *matches, |
640
|
|
|
|
|
|
|
const git_repository *repo, |
641
|
|
|
|
|
|
|
const char *cfg_file, |
642
|
|
|
|
|
|
|
const char *value) |
643
|
|
|
|
|
|
|
{ |
644
|
0
|
|
|
|
|
|
return do_match_gitdir(matches, repo, cfg_file, value, true); |
645
|
|
|
|
|
|
|
} |
646
|
|
|
|
|
|
|
|
647
|
0
|
|
|
|
|
|
static int conditional_match_onbranch( |
648
|
|
|
|
|
|
|
int *matches, |
649
|
|
|
|
|
|
|
const git_repository *repo, |
650
|
|
|
|
|
|
|
const char *cfg_file, |
651
|
|
|
|
|
|
|
const char *condition) |
652
|
|
|
|
|
|
|
{ |
653
|
0
|
|
|
|
|
|
git_buf reference = GIT_BUF_INIT, buf = GIT_BUF_INIT; |
654
|
|
|
|
|
|
|
int error; |
655
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
GIT_UNUSED(cfg_file); |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
/* |
659
|
|
|
|
|
|
|
* NOTE: you cannot use `git_repository_head` here. Looking up the |
660
|
|
|
|
|
|
|
* HEAD reference will create the ODB, which causes us to read the |
661
|
|
|
|
|
|
|
* repo's config for keys like core.precomposeUnicode. As we're |
662
|
|
|
|
|
|
|
* just parsing the config right now, though, this would result in |
663
|
|
|
|
|
|
|
* an endless recursion. |
664
|
|
|
|
|
|
|
*/ |
665
|
|
|
|
|
|
|
|
666
|
0
|
0
|
|
|
|
|
if ((error = git_buf_joinpath(&buf, git_repository_path(repo), GIT_HEAD_FILE)) < 0 || |
|
|
0
|
|
|
|
|
|
667
|
0
|
|
|
|
|
|
(error = git_futils_readbuffer(&reference, buf.ptr)) < 0) |
668
|
|
|
|
|
|
|
goto out; |
669
|
0
|
|
|
|
|
|
git_buf_rtrim(&reference); |
670
|
|
|
|
|
|
|
|
671
|
0
|
0
|
|
|
|
|
if (git__strncmp(reference.ptr, GIT_SYMREF, strlen(GIT_SYMREF))) |
672
|
0
|
|
|
|
|
|
goto out; |
673
|
0
|
|
|
|
|
|
git_buf_consume(&reference, reference.ptr + strlen(GIT_SYMREF)); |
674
|
|
|
|
|
|
|
|
675
|
0
|
0
|
|
|
|
|
if (git__strncmp(reference.ptr, GIT_REFS_HEADS_DIR, strlen(GIT_REFS_HEADS_DIR))) |
676
|
0
|
|
|
|
|
|
goto out; |
677
|
0
|
|
|
|
|
|
git_buf_consume(&reference, reference.ptr + strlen(GIT_REFS_HEADS_DIR)); |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
/* |
680
|
|
|
|
|
|
|
* If the condition ends with a '/', then we should treat it as if |
681
|
|
|
|
|
|
|
* it had '**' appended. |
682
|
|
|
|
|
|
|
*/ |
683
|
0
|
0
|
|
|
|
|
if ((error = git_buf_sets(&buf, condition)) < 0) |
684
|
0
|
|
|
|
|
|
goto out; |
685
|
0
|
0
|
|
|
|
|
if (git_path_is_dirsep(condition[strlen(condition) - 1]) && |
|
|
0
|
|
|
|
|
|
686
|
|
|
|
|
|
|
(error = git_buf_puts(&buf, "**")) < 0) |
687
|
0
|
|
|
|
|
|
goto out; |
688
|
|
|
|
|
|
|
|
689
|
0
|
|
|
|
|
|
*matches = wildmatch(buf.ptr, reference.ptr, WM_PATHNAME) == WM_MATCH; |
690
|
|
|
|
|
|
|
out: |
691
|
0
|
|
|
|
|
|
git_buf_dispose(&reference); |
692
|
0
|
|
|
|
|
|
git_buf_dispose(&buf); |
693
|
|
|
|
|
|
|
|
694
|
0
|
|
|
|
|
|
return error; |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
} |
697
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
static const struct { |
699
|
|
|
|
|
|
|
const char *prefix; |
700
|
|
|
|
|
|
|
int (*matches)(int *matches, const git_repository *repo, const char *cfg, const char *value); |
701
|
|
|
|
|
|
|
} conditions[] = { |
702
|
|
|
|
|
|
|
{ "gitdir:", conditional_match_gitdir }, |
703
|
|
|
|
|
|
|
{ "gitdir/i:", conditional_match_gitdir_i }, |
704
|
|
|
|
|
|
|
{ "onbranch:", conditional_match_onbranch } |
705
|
|
|
|
|
|
|
}; |
706
|
|
|
|
|
|
|
|
707
|
0
|
|
|
|
|
|
static int parse_conditional_include(config_file_parse_data *parse_data, const char *section, const char *file) |
708
|
|
|
|
|
|
|
{ |
709
|
|
|
|
|
|
|
char *condition; |
710
|
|
|
|
|
|
|
size_t i; |
711
|
0
|
|
|
|
|
|
int error = 0, matches; |
712
|
|
|
|
|
|
|
|
713
|
0
|
0
|
|
|
|
|
if (!parse_data->repo || !file) |
|
|
0
|
|
|
|
|
|
714
|
0
|
|
|
|
|
|
return 0; |
715
|
|
|
|
|
|
|
|
716
|
0
|
|
|
|
|
|
condition = git__substrdup(section + strlen("includeIf."), |
717
|
|
|
|
|
|
|
strlen(section) - strlen("includeIf.") - strlen(".path")); |
718
|
|
|
|
|
|
|
|
719
|
0
|
0
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(conditions); i++) { |
720
|
0
|
0
|
|
|
|
|
if (git__prefixcmp(condition, conditions[i].prefix)) |
721
|
0
|
|
|
|
|
|
continue; |
722
|
|
|
|
|
|
|
|
723
|
0
|
0
|
|
|
|
|
if ((error = conditions[i].matches(&matches, |
724
|
|
|
|
|
|
|
parse_data->repo, |
725
|
0
|
|
|
|
|
|
parse_data->file->path, |
726
|
0
|
|
|
|
|
|
condition + strlen(conditions[i].prefix))) < 0) |
727
|
0
|
|
|
|
|
|
break; |
728
|
|
|
|
|
|
|
|
729
|
0
|
0
|
|
|
|
|
if (matches) |
730
|
0
|
|
|
|
|
|
error = parse_include(parse_data, file); |
731
|
|
|
|
|
|
|
|
732
|
0
|
|
|
|
|
|
break; |
733
|
|
|
|
|
|
|
} |
734
|
|
|
|
|
|
|
|
735
|
0
|
|
|
|
|
|
git__free(condition); |
736
|
0
|
|
|
|
|
|
return error; |
737
|
|
|
|
|
|
|
} |
738
|
|
|
|
|
|
|
|
739
|
1216
|
|
|
|
|
|
static int read_on_variable( |
740
|
|
|
|
|
|
|
git_config_parser *reader, |
741
|
|
|
|
|
|
|
const char *current_section, |
742
|
|
|
|
|
|
|
const char *var_name, |
743
|
|
|
|
|
|
|
const char *var_value, |
744
|
|
|
|
|
|
|
const char *line, |
745
|
|
|
|
|
|
|
size_t line_len, |
746
|
|
|
|
|
|
|
void *data) |
747
|
|
|
|
|
|
|
{ |
748
|
1216
|
|
|
|
|
|
config_file_parse_data *parse_data = (config_file_parse_data *)data; |
749
|
1216
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
750
|
|
|
|
|
|
|
git_config_entry *entry; |
751
|
|
|
|
|
|
|
const char *c; |
752
|
1216
|
|
|
|
|
|
int result = 0; |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
GIT_UNUSED(reader); |
755
|
|
|
|
|
|
|
GIT_UNUSED(line); |
756
|
|
|
|
|
|
|
GIT_UNUSED(line_len); |
757
|
|
|
|
|
|
|
|
758
|
1216
|
50
|
|
|
|
|
if (current_section) { |
759
|
|
|
|
|
|
|
/* TODO: Once warnings lang, we should likely warn |
760
|
|
|
|
|
|
|
* here. Git appears to warn in most cases if it sees |
761
|
|
|
|
|
|
|
* un-namespaced config options. |
762
|
|
|
|
|
|
|
*/ |
763
|
1216
|
|
|
|
|
|
git_buf_puts(&buf, current_section); |
764
|
1216
|
|
|
|
|
|
git_buf_putc(&buf, '.'); |
765
|
|
|
|
|
|
|
} |
766
|
|
|
|
|
|
|
|
767
|
10082
|
100
|
|
|
|
|
for (c = var_name; *c; c++) |
768
|
8866
|
|
|
|
|
|
git_buf_putc(&buf, git__tolower(*c)); |
769
|
|
|
|
|
|
|
|
770
|
1216
|
50
|
|
|
|
|
if (git_buf_oom(&buf)) |
771
|
0
|
|
|
|
|
|
return -1; |
772
|
|
|
|
|
|
|
|
773
|
1216
|
|
|
|
|
|
entry = git__calloc(1, sizeof(git_config_entry)); |
774
|
1216
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(entry); |
775
|
1216
|
|
|
|
|
|
entry->name = git_buf_detach(&buf); |
776
|
1216
|
50
|
|
|
|
|
entry->value = var_value ? git__strdup(var_value) : NULL; |
777
|
1216
|
|
|
|
|
|
entry->level = parse_data->level; |
778
|
1216
|
|
|
|
|
|
entry->include_depth = parse_data->depth; |
779
|
|
|
|
|
|
|
|
780
|
1216
|
50
|
|
|
|
|
if ((result = git_config_entries_append(parse_data->entries, entry)) < 0) |
781
|
0
|
|
|
|
|
|
return result; |
782
|
|
|
|
|
|
|
|
783
|
1216
|
|
|
|
|
|
result = 0; |
784
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
/* Add or append the new config option */ |
786
|
1216
|
50
|
|
|
|
|
if (!git__strcmp(entry->name, "include.path")) |
787
|
0
|
|
|
|
|
|
result = parse_include(parse_data, entry->value); |
788
|
1216
|
|
|
|
|
|
else if (!git__prefixcmp(entry->name, "includeif.") && |
789
|
0
|
|
|
|
|
|
!git__suffixcmp(entry->name, ".path")) |
790
|
0
|
|
|
|
|
|
result = parse_conditional_include(parse_data, entry->name, entry->value); |
791
|
|
|
|
|
|
|
|
792
|
1216
|
|
|
|
|
|
return result; |
793
|
|
|
|
|
|
|
} |
794
|
|
|
|
|
|
|
|
795
|
117
|
|
|
|
|
|
static int config_file_read_buffer( |
796
|
|
|
|
|
|
|
git_config_entries *entries, |
797
|
|
|
|
|
|
|
const git_repository *repo, |
798
|
|
|
|
|
|
|
config_file *file, |
799
|
|
|
|
|
|
|
git_config_level_t level, |
800
|
|
|
|
|
|
|
int depth, |
801
|
|
|
|
|
|
|
const char *buf, |
802
|
|
|
|
|
|
|
size_t buflen) |
803
|
|
|
|
|
|
|
{ |
804
|
|
|
|
|
|
|
config_file_parse_data parse_data; |
805
|
|
|
|
|
|
|
git_config_parser reader; |
806
|
|
|
|
|
|
|
int error; |
807
|
|
|
|
|
|
|
|
808
|
117
|
50
|
|
|
|
|
if (depth >= MAX_INCLUDE_DEPTH) { |
809
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_CONFIG, "maximum config include depth reached"); |
810
|
0
|
|
|
|
|
|
return -1; |
811
|
|
|
|
|
|
|
} |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
/* Initialize the reading position */ |
814
|
117
|
|
|
|
|
|
reader.path = file->path; |
815
|
117
|
|
|
|
|
|
git_parse_ctx_init(&reader.ctx, buf, buflen); |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
/* If the file is empty, there's nothing for us to do */ |
818
|
117
|
50
|
|
|
|
|
if (!reader.ctx.content || *reader.ctx.content == '\0') { |
|
|
100
|
|
|
|
|
|
819
|
5
|
|
|
|
|
|
error = 0; |
820
|
5
|
|
|
|
|
|
goto out; |
821
|
|
|
|
|
|
|
} |
822
|
|
|
|
|
|
|
|
823
|
112
|
|
|
|
|
|
parse_data.repo = repo; |
824
|
112
|
|
|
|
|
|
parse_data.file = file; |
825
|
112
|
|
|
|
|
|
parse_data.entries = entries; |
826
|
112
|
|
|
|
|
|
parse_data.level = level; |
827
|
112
|
|
|
|
|
|
parse_data.depth = depth; |
828
|
|
|
|
|
|
|
|
829
|
112
|
|
|
|
|
|
error = git_config_parse(&reader, NULL, read_on_variable, NULL, NULL, &parse_data); |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
out: |
832
|
117
|
|
|
|
|
|
return error; |
833
|
|
|
|
|
|
|
} |
834
|
|
|
|
|
|
|
|
835
|
68
|
|
|
|
|
|
static int config_file_read( |
836
|
|
|
|
|
|
|
git_config_entries *entries, |
837
|
|
|
|
|
|
|
const git_repository *repo, |
838
|
|
|
|
|
|
|
config_file *file, |
839
|
|
|
|
|
|
|
git_config_level_t level, |
840
|
|
|
|
|
|
|
int depth) |
841
|
|
|
|
|
|
|
{ |
842
|
68
|
|
|
|
|
|
git_buf contents = GIT_BUF_INIT; |
843
|
|
|
|
|
|
|
struct stat st; |
844
|
|
|
|
|
|
|
int error; |
845
|
|
|
|
|
|
|
|
846
|
68
|
50
|
|
|
|
|
if (p_stat(file->path, &st) < 0) { |
847
|
0
|
|
|
|
|
|
error = git_path_set_error(errno, file->path, "stat"); |
848
|
0
|
|
|
|
|
|
goto out; |
849
|
|
|
|
|
|
|
} |
850
|
|
|
|
|
|
|
|
851
|
68
|
50
|
|
|
|
|
if ((error = git_futils_readbuffer(&contents, file->path)) < 0) |
852
|
0
|
|
|
|
|
|
goto out; |
853
|
|
|
|
|
|
|
|
854
|
68
|
|
|
|
|
|
git_futils_filestamp_set_from_stat(&file->stamp, &st); |
855
|
68
|
50
|
|
|
|
|
if ((error = git_hash_buf(&file->checksum, contents.ptr, contents.size)) < 0) |
856
|
0
|
|
|
|
|
|
goto out; |
857
|
|
|
|
|
|
|
|
858
|
68
|
50
|
|
|
|
|
if ((error = config_file_read_buffer(entries, repo, file, level, depth, |
859
|
68
|
|
|
|
|
|
contents.ptr, contents.size)) < 0) |
860
|
0
|
|
|
|
|
|
goto out; |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
out: |
863
|
68
|
|
|
|
|
|
git_buf_dispose(&contents); |
864
|
68
|
|
|
|
|
|
return error; |
865
|
|
|
|
|
|
|
} |
866
|
|
|
|
|
|
|
|
867
|
16
|
|
|
|
|
|
static int write_section(git_buf *fbuf, const char *key) |
868
|
|
|
|
|
|
|
{ |
869
|
|
|
|
|
|
|
int result; |
870
|
|
|
|
|
|
|
const char *dot; |
871
|
16
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
/* All of this just for [section "subsection"] */ |
874
|
16
|
|
|
|
|
|
dot = strchr(key, '.'); |
875
|
16
|
|
|
|
|
|
git_buf_putc(&buf, '['); |
876
|
16
|
100
|
|
|
|
|
if (dot == NULL) { |
877
|
12
|
|
|
|
|
|
git_buf_puts(&buf, key); |
878
|
|
|
|
|
|
|
} else { |
879
|
|
|
|
|
|
|
char *escaped; |
880
|
4
|
|
|
|
|
|
git_buf_put(&buf, key, dot - key); |
881
|
4
|
|
|
|
|
|
escaped = escape_value(dot + 1); |
882
|
4
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(escaped); |
883
|
4
|
|
|
|
|
|
git_buf_printf(&buf, " \"%s\"", escaped); |
884
|
4
|
|
|
|
|
|
git__free(escaped); |
885
|
|
|
|
|
|
|
} |
886
|
16
|
|
|
|
|
|
git_buf_puts(&buf, "]\n"); |
887
|
|
|
|
|
|
|
|
888
|
16
|
50
|
|
|
|
|
if (git_buf_oom(&buf)) |
889
|
0
|
|
|
|
|
|
return -1; |
890
|
|
|
|
|
|
|
|
891
|
16
|
|
|
|
|
|
result = git_buf_put(fbuf, git_buf_cstr(&buf), buf.size); |
892
|
16
|
|
|
|
|
|
git_buf_dispose(&buf); |
893
|
|
|
|
|
|
|
|
894
|
16
|
|
|
|
|
|
return result; |
895
|
|
|
|
|
|
|
} |
896
|
|
|
|
|
|
|
|
897
|
44
|
|
|
|
|
|
static const char *quotes_for_value(const char *value) |
898
|
|
|
|
|
|
|
{ |
899
|
|
|
|
|
|
|
const char *ptr; |
900
|
|
|
|
|
|
|
|
901
|
44
|
50
|
|
|
|
|
if (value[0] == ' ' || value[0] == '\0') |
|
|
50
|
|
|
|
|
|
902
|
0
|
|
|
|
|
|
return "\""; |
903
|
|
|
|
|
|
|
|
904
|
676
|
100
|
|
|
|
|
for (ptr = value; *ptr; ++ptr) { |
905
|
632
|
50
|
|
|
|
|
if (*ptr == ';' || *ptr == '#') |
|
|
50
|
|
|
|
|
|
906
|
0
|
|
|
|
|
|
return "\""; |
907
|
|
|
|
|
|
|
} |
908
|
|
|
|
|
|
|
|
909
|
44
|
50
|
|
|
|
|
if (ptr[-1] == ' ') |
910
|
0
|
|
|
|
|
|
return "\""; |
911
|
|
|
|
|
|
|
|
912
|
44
|
|
|
|
|
|
return ""; |
913
|
|
|
|
|
|
|
} |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
struct write_data { |
916
|
|
|
|
|
|
|
git_buf *buf; |
917
|
|
|
|
|
|
|
git_buf buffered_comment; |
918
|
|
|
|
|
|
|
unsigned int in_section : 1, |
919
|
|
|
|
|
|
|
preg_replaced : 1; |
920
|
|
|
|
|
|
|
const char *orig_section; |
921
|
|
|
|
|
|
|
const char *section; |
922
|
|
|
|
|
|
|
const char *orig_name; |
923
|
|
|
|
|
|
|
const char *name; |
924
|
|
|
|
|
|
|
const git_regexp *preg; |
925
|
|
|
|
|
|
|
const char *value; |
926
|
|
|
|
|
|
|
}; |
927
|
|
|
|
|
|
|
|
928
|
571
|
|
|
|
|
|
static int write_line_to(git_buf *buf, const char *line, size_t line_len) |
929
|
|
|
|
|
|
|
{ |
930
|
571
|
|
|
|
|
|
int result = git_buf_put(buf, line, line_len); |
931
|
|
|
|
|
|
|
|
932
|
571
|
50
|
|
|
|
|
if (!result && line_len && line[line_len-1] != '\n') |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
933
|
0
|
|
|
|
|
|
result = git_buf_printf(buf, "\n"); |
934
|
|
|
|
|
|
|
|
935
|
571
|
|
|
|
|
|
return result; |
936
|
|
|
|
|
|
|
} |
937
|
|
|
|
|
|
|
|
938
|
571
|
|
|
|
|
|
static int write_line(struct write_data *write_data, const char *line, size_t line_len) |
939
|
|
|
|
|
|
|
{ |
940
|
571
|
|
|
|
|
|
return write_line_to(write_data->buf, line, line_len); |
941
|
|
|
|
|
|
|
} |
942
|
|
|
|
|
|
|
|
943
|
44
|
|
|
|
|
|
static int write_value(struct write_data *write_data) |
944
|
|
|
|
|
|
|
{ |
945
|
|
|
|
|
|
|
const char *q; |
946
|
|
|
|
|
|
|
int result; |
947
|
|
|
|
|
|
|
|
948
|
44
|
|
|
|
|
|
q = quotes_for_value(write_data->value); |
949
|
44
|
|
|
|
|
|
result = git_buf_printf(write_data->buf, |
950
|
|
|
|
|
|
|
"\t%s = %s%s%s\n", write_data->orig_name, q, write_data->value, q); |
951
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
/* If we are updating a single name/value, we're done. Setting `value` |
953
|
|
|
|
|
|
|
* to `NULL` will prevent us from trying to write it again later (in |
954
|
|
|
|
|
|
|
* `write_on_section`) if we see the same section repeated. |
955
|
|
|
|
|
|
|
*/ |
956
|
44
|
100
|
|
|
|
|
if (!write_data->preg) |
957
|
39
|
|
|
|
|
|
write_data->value = NULL; |
958
|
|
|
|
|
|
|
|
959
|
44
|
|
|
|
|
|
return result; |
960
|
|
|
|
|
|
|
} |
961
|
|
|
|
|
|
|
|
962
|
178
|
|
|
|
|
|
static int write_on_section( |
963
|
|
|
|
|
|
|
git_config_parser *reader, |
964
|
|
|
|
|
|
|
const char *current_section, |
965
|
|
|
|
|
|
|
const char *line, |
966
|
|
|
|
|
|
|
size_t line_len, |
967
|
|
|
|
|
|
|
void *data) |
968
|
|
|
|
|
|
|
{ |
969
|
178
|
|
|
|
|
|
struct write_data *write_data = (struct write_data *)data; |
970
|
178
|
|
|
|
|
|
int result = 0; |
971
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
GIT_UNUSED(reader); |
973
|
|
|
|
|
|
|
|
974
|
|
|
|
|
|
|
/* If we were previously in the correct section (but aren't anymore) |
975
|
|
|
|
|
|
|
* and haven't written our value (for a simple name/value set, not |
976
|
|
|
|
|
|
|
* a multivar), then append it to the end of the section before writing |
977
|
|
|
|
|
|
|
* the new one. |
978
|
|
|
|
|
|
|
*/ |
979
|
178
|
100
|
|
|
|
|
if (write_data->in_section && !write_data->preg && write_data->value) |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
980
|
2
|
|
|
|
|
|
result = write_value(write_data); |
981
|
|
|
|
|
|
|
|
982
|
178
|
|
|
|
|
|
write_data->in_section = strcmp(current_section, write_data->section) == 0; |
983
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
/* |
985
|
|
|
|
|
|
|
* If there were comments just before this section, dump them as well. |
986
|
|
|
|
|
|
|
*/ |
987
|
178
|
50
|
|
|
|
|
if (!result) { |
988
|
178
|
|
|
|
|
|
result = git_buf_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size); |
989
|
178
|
|
|
|
|
|
git_buf_clear(&write_data->buffered_comment); |
990
|
|
|
|
|
|
|
} |
991
|
|
|
|
|
|
|
|
992
|
178
|
50
|
|
|
|
|
if (!result) |
993
|
178
|
|
|
|
|
|
result = write_line(write_data, line, line_len); |
994
|
|
|
|
|
|
|
|
995
|
178
|
|
|
|
|
|
return result; |
996
|
|
|
|
|
|
|
} |
997
|
|
|
|
|
|
|
|
998
|
401
|
|
|
|
|
|
static int write_on_variable( |
999
|
|
|
|
|
|
|
git_config_parser *reader, |
1000
|
|
|
|
|
|
|
const char *current_section, |
1001
|
|
|
|
|
|
|
const char *var_name, |
1002
|
|
|
|
|
|
|
const char *var_value, |
1003
|
|
|
|
|
|
|
const char *line, |
1004
|
|
|
|
|
|
|
size_t line_len, |
1005
|
|
|
|
|
|
|
void *data) |
1006
|
|
|
|
|
|
|
{ |
1007
|
401
|
|
|
|
|
|
struct write_data *write_data = (struct write_data *)data; |
1008
|
401
|
|
|
|
|
|
bool has_matched = false; |
1009
|
|
|
|
|
|
|
int error; |
1010
|
|
|
|
|
|
|
|
1011
|
|
|
|
|
|
|
GIT_UNUSED(reader); |
1012
|
|
|
|
|
|
|
GIT_UNUSED(current_section); |
1013
|
|
|
|
|
|
|
|
1014
|
|
|
|
|
|
|
/* |
1015
|
|
|
|
|
|
|
* If there were comments just before this variable, let's dump them as well. |
1016
|
|
|
|
|
|
|
*/ |
1017
|
401
|
50
|
|
|
|
|
if ((error = git_buf_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0) |
1018
|
0
|
|
|
|
|
|
return error; |
1019
|
|
|
|
|
|
|
|
1020
|
401
|
|
|
|
|
|
git_buf_clear(&write_data->buffered_comment); |
1021
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
/* See if we are to update this name/value pair; first examine name */ |
1023
|
401
|
100
|
|
|
|
|
if (write_data->in_section && |
|
|
100
|
|
|
|
|
|
1024
|
61
|
|
|
|
|
|
strcasecmp(write_data->name, var_name) == 0) |
1025
|
9
|
|
|
|
|
|
has_matched = true; |
1026
|
|
|
|
|
|
|
|
1027
|
|
|
|
|
|
|
/* If we have a regex to match the value, see if it matches */ |
1028
|
401
|
100
|
|
|
|
|
if (has_matched && write_data->preg != NULL) |
|
|
100
|
|
|
|
|
|
1029
|
1
|
|
|
|
|
|
has_matched = (git_regexp_match(write_data->preg, var_value) == 0); |
1030
|
|
|
|
|
|
|
|
1031
|
|
|
|
|
|
|
/* If this isn't the name/value we're looking for, simply dump the |
1032
|
|
|
|
|
|
|
* existing data back out and continue on. |
1033
|
|
|
|
|
|
|
*/ |
1034
|
401
|
100
|
|
|
|
|
if (!has_matched) |
1035
|
393
|
|
|
|
|
|
return write_line(write_data, line, line_len); |
1036
|
|
|
|
|
|
|
|
1037
|
8
|
|
|
|
|
|
write_data->preg_replaced = 1; |
1038
|
|
|
|
|
|
|
|
1039
|
|
|
|
|
|
|
/* If value is NULL, we are deleting this value; write nothing. */ |
1040
|
8
|
100
|
|
|
|
|
if (!write_data->value) |
1041
|
5
|
|
|
|
|
|
return 0; |
1042
|
|
|
|
|
|
|
|
1043
|
3
|
|
|
|
|
|
return write_value(write_data); |
1044
|
|
|
|
|
|
|
} |
1045
|
|
|
|
|
|
|
|
1046
|
0
|
|
|
|
|
|
static int write_on_comment(git_config_parser *reader, const char *line, size_t line_len, void *data) |
1047
|
|
|
|
|
|
|
{ |
1048
|
|
|
|
|
|
|
struct write_data *write_data; |
1049
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
GIT_UNUSED(reader); |
1051
|
|
|
|
|
|
|
|
1052
|
0
|
|
|
|
|
|
write_data = (struct write_data *)data; |
1053
|
0
|
|
|
|
|
|
return write_line_to(&write_data->buffered_comment, line, line_len); |
1054
|
|
|
|
|
|
|
} |
1055
|
|
|
|
|
|
|
|
1056
|
49
|
|
|
|
|
|
static int write_on_eof( |
1057
|
|
|
|
|
|
|
git_config_parser *reader, const char *current_section, void *data) |
1058
|
|
|
|
|
|
|
{ |
1059
|
49
|
|
|
|
|
|
struct write_data *write_data = (struct write_data *)data; |
1060
|
49
|
|
|
|
|
|
int result = 0; |
1061
|
|
|
|
|
|
|
|
1062
|
|
|
|
|
|
|
GIT_UNUSED(reader); |
1063
|
|
|
|
|
|
|
|
1064
|
|
|
|
|
|
|
/* |
1065
|
|
|
|
|
|
|
* If we've buffered comments when reaching EOF, make sure to dump them. |
1066
|
|
|
|
|
|
|
*/ |
1067
|
49
|
50
|
|
|
|
|
if ((result = git_buf_put(write_data->buf, write_data->buffered_comment.ptr, write_data->buffered_comment.size)) < 0) |
1068
|
0
|
|
|
|
|
|
return result; |
1069
|
|
|
|
|
|
|
|
1070
|
|
|
|
|
|
|
/* If we are at the EOF and have not written our value (again, for a |
1071
|
|
|
|
|
|
|
* simple name/value set, not a multivar) then we have never seen the |
1072
|
|
|
|
|
|
|
* section in question and should create a new section and write the |
1073
|
|
|
|
|
|
|
* value. |
1074
|
|
|
|
|
|
|
*/ |
1075
|
49
|
100
|
|
|
|
|
if ((!write_data->preg || !write_data->preg_replaced) && write_data->value) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
/* write the section header unless we're already in it */ |
1077
|
39
|
100
|
|
|
|
|
if (!current_section || strcmp(current_section, write_data->section)) |
|
|
100
|
|
|
|
|
|
1078
|
16
|
|
|
|
|
|
result = write_section(write_data->buf, write_data->orig_section); |
1079
|
|
|
|
|
|
|
|
1080
|
39
|
50
|
|
|
|
|
if (!result) |
1081
|
39
|
|
|
|
|
|
result = write_value(write_data); |
1082
|
|
|
|
|
|
|
} |
1083
|
|
|
|
|
|
|
|
1084
|
49
|
|
|
|
|
|
return result; |
1085
|
|
|
|
|
|
|
} |
1086
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
/* |
1088
|
|
|
|
|
|
|
* This is pretty much the parsing, except we write out anything we don't have |
1089
|
|
|
|
|
|
|
*/ |
1090
|
49
|
|
|
|
|
|
static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char* value) |
1091
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
{ |
1093
|
49
|
|
|
|
|
|
char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot; |
1094
|
49
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT, contents = GIT_BUF_INIT; |
1095
|
49
|
|
|
|
|
|
git_config_parser parser = GIT_CONFIG_PARSER_INIT; |
1096
|
49
|
|
|
|
|
|
git_filebuf file = GIT_FILEBUF_INIT; |
1097
|
|
|
|
|
|
|
struct write_data write_data; |
1098
|
|
|
|
|
|
|
int error; |
1099
|
|
|
|
|
|
|
|
1100
|
49
|
|
|
|
|
|
memset(&write_data, 0, sizeof(write_data)); |
1101
|
|
|
|
|
|
|
|
1102
|
49
|
50
|
|
|
|
|
if (cfg->locked) { |
1103
|
0
|
0
|
|
|
|
|
error = git_buf_puts(&contents, git_buf_cstr(&cfg->locked_content) == NULL ? "" : git_buf_cstr(&cfg->locked_content)); |
1104
|
|
|
|
|
|
|
} else { |
1105
|
49
|
50
|
|
|
|
|
if ((error = git_filebuf_open(&file, cfg->file.path, GIT_FILEBUF_HASH_CONTENTS, |
1106
|
|
|
|
|
|
|
GIT_CONFIG_FILE_MODE)) < 0) |
1107
|
0
|
|
|
|
|
|
goto done; |
1108
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
/* We need to read in our own config file */ |
1110
|
49
|
|
|
|
|
|
error = git_futils_readbuffer(&contents, cfg->file.path); |
1111
|
|
|
|
|
|
|
} |
1112
|
49
|
100
|
|
|
|
|
if (error < 0 && error != GIT_ENOTFOUND) |
|
|
50
|
|
|
|
|
|
1113
|
0
|
|
|
|
|
|
goto done; |
1114
|
|
|
|
|
|
|
|
1115
|
49
|
50
|
|
|
|
|
if ((git_config_parser_init(&parser, cfg->file.path, contents.ptr, contents.size)) < 0) |
1116
|
0
|
|
|
|
|
|
goto done; |
1117
|
|
|
|
|
|
|
|
1118
|
49
|
|
|
|
|
|
ldot = strrchr(key, '.'); |
1119
|
49
|
|
|
|
|
|
name = ldot + 1; |
1120
|
49
|
|
|
|
|
|
section = git__strndup(key, ldot - key); |
1121
|
49
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(section); |
1122
|
|
|
|
|
|
|
|
1123
|
49
|
|
|
|
|
|
ldot = strrchr(orig_key, '.'); |
1124
|
49
|
|
|
|
|
|
orig_name = ldot + 1; |
1125
|
49
|
|
|
|
|
|
orig_section = git__strndup(orig_key, ldot - orig_key); |
1126
|
49
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(orig_section); |
1127
|
|
|
|
|
|
|
|
1128
|
49
|
|
|
|
|
|
write_data.buf = &buf; |
1129
|
49
|
|
|
|
|
|
write_data.orig_section = orig_section; |
1130
|
49
|
|
|
|
|
|
write_data.section = section; |
1131
|
49
|
|
|
|
|
|
write_data.orig_name = orig_name; |
1132
|
49
|
|
|
|
|
|
write_data.name = name; |
1133
|
49
|
|
|
|
|
|
write_data.preg = preg; |
1134
|
49
|
|
|
|
|
|
write_data.value = value; |
1135
|
|
|
|
|
|
|
|
1136
|
49
|
50
|
|
|
|
|
if ((error = git_config_parse(&parser, write_on_section, write_on_variable, |
1137
|
|
|
|
|
|
|
write_on_comment, write_on_eof, &write_data)) < 0) |
1138
|
0
|
|
|
|
|
|
goto done; |
1139
|
|
|
|
|
|
|
|
1140
|
49
|
50
|
|
|
|
|
if (cfg->locked) { |
1141
|
0
|
|
|
|
|
|
size_t len = buf.asize; |
1142
|
|
|
|
|
|
|
/* Update our copy with the modified contents */ |
1143
|
0
|
|
|
|
|
|
git_buf_dispose(&cfg->locked_content); |
1144
|
0
|
|
|
|
|
|
git_buf_attach(&cfg->locked_content, git_buf_detach(&buf), len); |
1145
|
|
|
|
|
|
|
} else { |
1146
|
49
|
|
|
|
|
|
git_filebuf_write(&file, git_buf_cstr(&buf), git_buf_len(&buf)); |
1147
|
|
|
|
|
|
|
|
1148
|
49
|
50
|
|
|
|
|
if ((error = git_filebuf_commit(&file)) < 0) |
1149
|
0
|
|
|
|
|
|
goto done; |
1150
|
|
|
|
|
|
|
|
1151
|
49
|
50
|
|
|
|
|
if ((error = config_file_refresh_from_buffer(&cfg->parent, buf.ptr, buf.size)) < 0) |
1152
|
0
|
|
|
|
|
|
goto done; |
1153
|
|
|
|
|
|
|
} |
1154
|
|
|
|
|
|
|
|
1155
|
|
|
|
|
|
|
done: |
1156
|
49
|
|
|
|
|
|
git__free(section); |
1157
|
49
|
|
|
|
|
|
git__free(orig_section); |
1158
|
49
|
|
|
|
|
|
git_buf_dispose(&write_data.buffered_comment); |
1159
|
49
|
|
|
|
|
|
git_buf_dispose(&buf); |
1160
|
49
|
|
|
|
|
|
git_buf_dispose(&contents); |
1161
|
49
|
|
|
|
|
|
git_filebuf_cleanup(&file); |
1162
|
49
|
|
|
|
|
|
git_config_parser_dispose(&parser); |
1163
|
|
|
|
|
|
|
|
1164
|
49
|
|
|
|
|
|
return error; |
1165
|
|
|
|
|
|
|
} |