diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2014-12-21 15:31:03 +0000 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2015-03-03 18:35:12 +0100 |
commit | 9a97f49e3aa15edc479fc590f4b28fc44c155c40 (patch) | |
tree | b85de2d396b9d0d408f688212c0cdc74347ea7b3 /include | |
parent | 76f034180aee96fcc1fffd5267ccbc6ada68482a (diff) | |
download | libgit2-9a97f49e3aa15edc479fc590f4b28fc44c155c40.tar.gz |
config: borrow refcounted referencescmn/config-borrow-entry
This changes the get_entry() method to return a refcounted version of
the config entry, which you have to free when you're done.
This allows us to avoid freeing the memory in which the entry is stored
on a refresh, which may happen at any time for a live config.
For this reason, get_string() has been forbidden on live configs and a
new function get_string_buf() has been added, which stores the string in
a git_buf which the user then owns.
The functions which parse the string value takea advantage of the
borrowing to parse safely and then release the entry.
Diffstat (limited to 'include')
-rw-r--r-- | include/git2/config.h | 40 | ||||
-rw-r--r-- | include/git2/sys/config.h | 4 |
2 files changed, 34 insertions, 10 deletions
diff --git a/include/git2/config.h b/include/git2/config.h index 70c8beade..6d3fdb0c2 100644 --- a/include/git2/config.h +++ b/include/git2/config.h @@ -58,12 +58,19 @@ typedef enum { /** * An entry in a configuration file */ -typedef struct { +typedef struct git_config_entry { const char *name; /**< Name of the entry (normalised) */ const char *value; /**< String value of the entry */ git_config_level_t level; /**< Which config file this was found in */ + void (*free)(struct git_config_entry *entry); /**< Free function for this entry */ + void *payload; /**< Opaque value for the free function. Do not read or write */ } git_config_entry; +/** + * Free a config entry + */ +GIT_EXTERN(void) git_config_entry_free(git_config_entry *); + typedef int (*git_config_foreach_cb)(const git_config_entry *, void *); typedef struct git_config_iterator git_config_iterator; @@ -261,16 +268,15 @@ GIT_EXTERN(void) git_config_free(git_config *cfg); /** * Get the git_config_entry of a config variable. * - * The git_config_entry is owned by the config and should not be freed by the - * user. - + * Free the git_config_entry after use with `git_config_entry_free()`. + * * @param out pointer to the variable git_config_entry * @param cfg where to look for the variable * @param name the variable's name * @return 0 or an error code */ GIT_EXTERN(int) git_config_get_entry( - const git_config_entry **out, + git_config_entry **out, const git_config *cfg, const char *name); @@ -340,15 +346,15 @@ GIT_EXTERN(int) git_config_get_path(git_buf *out, const git_config *cfg, const c /** * Get the value of a string config variable. * - * The string is owned by the variable and should not be freed by the - * user. The pointer will be valid until the next operation on this - * config object. + * This function can only be used on snapshot config objects. The + * string is owned by the config and should not be freed by the + * user. The pointer will be valid until the config is freed. * * All config files will be looked into, in the order of their * defined level. A higher level means a higher priority. The * first occurrence of the variable will be returned here. * - * @param out pointer to the variable's value + * @param out pointer to the string * @param cfg where to look for the variable * @param name the variable's name * @return 0 or an error code @@ -356,6 +362,22 @@ GIT_EXTERN(int) git_config_get_path(git_buf *out, const git_config *cfg, const c GIT_EXTERN(int) git_config_get_string(const char **out, const git_config *cfg, const char *name); /** + * Get the value of a string config variable. + * + * The value of the config will be copied into the buffer. + * + * All config files will be looked into, in the order of their + * defined level. A higher level means a higher priority. The + * first occurrence of the variable will be returned here. + * + * @param out buffer in which to store the string + * @param cfg where to look for the variable + * @param name the variable's name + * @return 0 or an error code + */ +GIT_EXTERN(int) git_config_get_string_buf(git_buf *out, const git_config *cfg, const char *name); + +/** * Get each value of a multivar in a foreach callback * * The callback will be called on each variable found diff --git a/include/git2/sys/config.h b/include/git2/sys/config.h index 9136635ef..b5b7df15f 100644 --- a/include/git2/sys/config.h +++ b/include/git2/sys/config.h @@ -53,11 +53,13 @@ struct git_config_iterator { */ struct git_config_backend { unsigned int version; + /** True if this backend is for a snapshot */ + int readonly; struct git_config *cfg; /* Open means open the file/database and parse if necessary */ int (*open)(struct git_config_backend *, git_config_level_t level); - int (*get)(struct git_config_backend *, const char *key, const git_config_entry **entry); + int (*get)(struct git_config_backend *, const char *key, git_config_entry **entry); int (*set)(struct git_config_backend *, const char *key, const char *value); int (*set_multivar)(git_config_backend *cfg, const char *name, const char *regexp, const char *value); int (*del)(struct git_config_backend *, const char *key); |