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