2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #include "config_backend.h"
11 #include "config_parse.h"
12 #include "config_entries.h"
15 git_config_backend parent
;
16 git_config_entries
*entries
;
18 } config_memory_backend
;
21 git_config_entries
*entries
;
22 git_config_level_t level
;
23 } config_memory_parse_data
;
25 static int config_error_readonly(void)
27 git_error_set(GIT_ERROR_CONFIG
, "this backend is read-only");
31 static int read_variable_cb(
32 git_config_parser
*reader
,
33 const char *current_section
,
35 const char *var_value
,
40 config_memory_parse_data
*parse_data
= (config_memory_parse_data
*) payload
;
41 git_buf buf
= GIT_BUF_INIT
;
42 git_config_entry
*entry
;
50 if (current_section
) {
51 /* TODO: Once warnings land, we should likely warn
52 * here. Git appears to warn in most cases if it sees
53 * un-namespaced config options.
55 git_buf_puts(&buf
, current_section
);
56 git_buf_putc(&buf
, '.');
59 for (c
= var_name
; *c
; c
++)
60 git_buf_putc(&buf
, git__tolower(*c
));
62 if (git_buf_oom(&buf
))
65 entry
= git__calloc(1, sizeof(git_config_entry
));
66 GIT_ERROR_CHECK_ALLOC(entry
);
67 entry
->name
= git_buf_detach(&buf
);
68 entry
->value
= var_value
? git__strdup(var_value
) : NULL
;
69 entry
->level
= parse_data
->level
;
70 entry
->include_depth
= 0;
72 if ((result
= git_config_entries_append(parse_data
->entries
, entry
)) < 0)
78 static int config_memory_open(git_config_backend
*backend
, git_config_level_t level
, const git_repository
*repo
)
80 config_memory_backend
*memory_backend
= (config_memory_backend
*) backend
;
81 git_config_parser parser
= GIT_PARSE_CTX_INIT
;
82 config_memory_parse_data parse_data
;
87 if ((error
= git_config_parser_init(&parser
, "in-memory", memory_backend
->cfg
.ptr
,
88 memory_backend
->cfg
.size
)) < 0)
90 parse_data
.entries
= memory_backend
->entries
;
91 parse_data
.level
= level
;
93 if ((error
= git_config_parse(&parser
, NULL
, read_variable_cb
, NULL
, NULL
, &parse_data
)) < 0)
97 git_config_parser_dispose(&parser
);
101 static int config_memory_get(git_config_backend
*backend
, const char *key
, git_config_entry
**out
)
103 config_memory_backend
*memory_backend
= (config_memory_backend
*) backend
;
104 return git_config_entries_get(out
, memory_backend
->entries
, key
);
107 static int config_memory_iterator(
108 git_config_iterator
**iter
,
109 git_config_backend
*backend
)
111 config_memory_backend
*memory_backend
= (config_memory_backend
*) backend
;
112 git_config_entries
*entries
;
115 if ((error
= git_config_entries_dup(&entries
, memory_backend
->entries
)) < 0)
118 if ((error
= git_config_entries_iterator_new(iter
, entries
)) < 0)
122 /* Let iterator delete duplicated entries when it's done */
123 git_config_entries_free(entries
);
127 static int config_memory_set(git_config_backend
*backend
, const char *name
, const char *value
)
132 return config_error_readonly();
135 static int config_memory_set_multivar(
136 git_config_backend
*backend
, const char *name
, const char *regexp
, const char *value
)
142 return config_error_readonly();
145 static int config_memory_delete(git_config_backend
*backend
, const char *name
)
149 return config_error_readonly();
152 static int config_memory_delete_multivar(git_config_backend
*backend
, const char *name
, const char *regexp
)
157 return config_error_readonly();
160 static int config_memory_lock(git_config_backend
*backend
)
163 return config_error_readonly();
166 static int config_memory_unlock(git_config_backend
*backend
, int success
)
170 return config_error_readonly();
173 static void config_memory_free(git_config_backend
*_backend
)
175 config_memory_backend
*backend
= (config_memory_backend
*)_backend
;
180 git_config_entries_free(backend
->entries
);
181 git_buf_dispose(&backend
->cfg
);
185 int git_config_backend_from_string(git_config_backend
**out
, const char *cfg
, size_t len
)
187 config_memory_backend
*backend
;
189 backend
= git__calloc(1, sizeof(config_memory_backend
));
190 GIT_ERROR_CHECK_ALLOC(backend
);
192 if (git_config_entries_new(&backend
->entries
) < 0) {
197 if (git_buf_set(&backend
->cfg
, cfg
, len
) < 0) {
198 git_config_entries_free(backend
->entries
);
203 backend
->parent
.version
= GIT_CONFIG_BACKEND_VERSION
;
204 backend
->parent
.readonly
= 1;
205 backend
->parent
.open
= config_memory_open
;
206 backend
->parent
.get
= config_memory_get
;
207 backend
->parent
.set
= config_memory_set
;
208 backend
->parent
.set_multivar
= config_memory_set_multivar
;
209 backend
->parent
.del
= config_memory_delete
;
210 backend
->parent
.del_multivar
= config_memory_delete_multivar
;
211 backend
->parent
.iterator
= config_memory_iterator
;
212 backend
->parent
.lock
= config_memory_lock
;
213 backend
->parent
.unlock
= config_memory_unlock
;
214 backend
->parent
.snapshot
= git_config_backend_snapshot
;
215 backend
->parent
.free
= config_memory_free
;
217 *out
= (git_config_backend
*)backend
;