Commit 9a97f49e by Carlos Martín Nieto

config: borrow refcounted references

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.
parent 76f03418
...@@ -25,6 +25,11 @@ v0.22 + 1 ...@@ -25,6 +25,11 @@ v0.22 + 1
set and query which identity will be used when writing to the set and query which identity will be used when writing to the
reflog. reflog.
* `git_config_entry_free()` frees a config entry.
* `git_config_get_string_buf()` provides a way to safely retrieve a
string from a non-snapshot configuration.
### API removals ### API removals
### Breaking API changes ### Breaking API changes
...@@ -53,6 +58,13 @@ v0.22 + 1 ...@@ -53,6 +58,13 @@ v0.22 + 1
`git_repository_detach_head()` `git_repository_detach_head()`
* `git_reset()` * `git_reset()`
* `git_config_get_entry()` now gives back a ref-counted
`git_config_entry`. You must free it when you no longer need it.
* `git_config_get_string()` will return an error if used on a
non-snapshot configuration, as there can be no guarantee that the
returned pointer is valid.
v0.22 v0.22
------ ------
......
...@@ -58,12 +58,19 @@ typedef enum { ...@@ -58,12 +58,19 @@ typedef enum {
/** /**
* An entry in a configuration file * An entry in a configuration file
*/ */
typedef struct { typedef struct git_config_entry {
const char *name; /**< Name of the entry (normalised) */ const char *name; /**< Name of the entry (normalised) */
const char *value; /**< String value of the entry */ const char *value; /**< String value of the entry */
git_config_level_t level; /**< Which config file this was found in */ 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; } 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 int (*git_config_foreach_cb)(const git_config_entry *, void *);
typedef struct git_config_iterator git_config_iterator; typedef struct git_config_iterator git_config_iterator;
...@@ -261,16 +268,15 @@ GIT_EXTERN(void) git_config_free(git_config *cfg); ...@@ -261,16 +268,15 @@ GIT_EXTERN(void) git_config_free(git_config *cfg);
/** /**
* Get the git_config_entry of a config variable. * 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 * Free the git_config_entry after use with `git_config_entry_free()`.
* user. *
* @param out pointer to the variable git_config_entry * @param out pointer to the variable git_config_entry
* @param cfg where to look for the variable * @param cfg where to look for the variable
* @param name the variable's name * @param name the variable's name
* @return 0 or an error code * @return 0 or an error code
*/ */
GIT_EXTERN(int) git_config_get_entry( GIT_EXTERN(int) git_config_get_entry(
const git_config_entry **out, git_config_entry **out,
const git_config *cfg, const git_config *cfg,
const char *name); const char *name);
...@@ -340,15 +346,15 @@ GIT_EXTERN(int) git_config_get_path(git_buf *out, const git_config *cfg, const c ...@@ -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. * Get the value of a string config variable.
* *
* The string is owned by the variable and should not be freed by the * This function can only be used on snapshot config objects. The
* user. The pointer will be valid until the next operation on this * string is owned by the config and should not be freed by the
* config object. * user. The pointer will be valid until the config is freed.
* *
* All config files will be looked into, in the order of their * All config files will be looked into, in the order of their
* defined level. A higher level means a higher priority. The * defined level. A higher level means a higher priority. The
* first occurrence of the variable will be returned here. * 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 cfg where to look for the variable
* @param name the variable's name * @param name the variable's name
* @return 0 or an error code * @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 ...@@ -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); 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 * Get each value of a multivar in a foreach callback
* *
* The callback will be called on each variable found * The callback will be called on each variable found
......
...@@ -53,11 +53,13 @@ struct git_config_iterator { ...@@ -53,11 +53,13 @@ struct git_config_iterator {
*/ */
struct git_config_backend { struct git_config_backend {
unsigned int version; unsigned int version;
/** True if this backend is for a snapshot */
int readonly;
struct git_config *cfg; struct git_config *cfg;
/* Open means open the file/database and parse if necessary */ /* Open means open the file/database and parse if necessary */
int (*open)(struct git_config_backend *, git_config_level_t level); 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)(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 (*set_multivar)(git_config_backend *cfg, const char *name, const char *regexp, const char *value);
int (*del)(struct git_config_backend *, const char *key); int (*del)(struct git_config_backend *, const char *key);
......
...@@ -276,7 +276,7 @@ static int attr_cache__lookup_path( ...@@ -276,7 +276,7 @@ static int attr_cache__lookup_path(
{ {
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
int error; int error;
const git_config_entry *entry = NULL; git_config_entry *entry = NULL;
*out = NULL; *out = NULL;
...@@ -296,6 +296,7 @@ static int attr_cache__lookup_path( ...@@ -296,6 +296,7 @@ static int attr_cache__lookup_path(
else if (!git_sysdir_find_xdg_file(&buf, fallback)) else if (!git_sysdir_find_xdg_file(&buf, fallback))
*out = git_buf_detach(&buf); *out = git_buf_detach(&buf);
git_config_entry_free(entry);
git_buf_free(&buf); git_buf_free(&buf);
return error; return error;
......
...@@ -301,7 +301,7 @@ int git_branch_name( ...@@ -301,7 +301,7 @@ int git_branch_name(
} }
static int retrieve_upstream_configuration( static int retrieve_upstream_configuration(
const char **out, git_buf *out,
const git_config *config, const git_config *config,
const char *canonical_branch_name, const char *canonical_branch_name,
const char *format) const char *format)
...@@ -313,7 +313,7 @@ static int retrieve_upstream_configuration( ...@@ -313,7 +313,7 @@ static int retrieve_upstream_configuration(
canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0) canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0)
return -1; return -1;
error = git_config_get_string(out, config, git_buf_cstr(&buf)); error = git_config_get_string_buf(out, config, git_buf_cstr(&buf));
git_buf_free(&buf); git_buf_free(&buf);
return error; return error;
} }
...@@ -323,7 +323,8 @@ int git_branch_upstream_name( ...@@ -323,7 +323,8 @@ int git_branch_upstream_name(
git_repository *repo, git_repository *repo,
const char *refname) const char *refname)
{ {
const char *remote_name, *merge_name; git_buf remote_name = GIT_BUF_INIT;
git_buf merge_name = GIT_BUF_INIT;
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
int error = -1; int error = -1;
git_remote *remote = NULL; git_remote *remote = NULL;
...@@ -348,27 +349,27 @@ int git_branch_upstream_name( ...@@ -348,27 +349,27 @@ int git_branch_upstream_name(
&merge_name, config, refname, "branch.%s.merge")) < 0) &merge_name, config, refname, "branch.%s.merge")) < 0)
goto cleanup; goto cleanup;
if (!*remote_name || !*merge_name) { if (git_buf_len(&remote_name) == 0 || git_buf_len(&merge_name) == 0) {
giterr_set(GITERR_REFERENCE, giterr_set(GITERR_REFERENCE,
"branch '%s' does not have an upstream", refname); "branch '%s' does not have an upstream", refname);
error = GIT_ENOTFOUND; error = GIT_ENOTFOUND;
goto cleanup; goto cleanup;
} }
if (strcmp(".", remote_name) != 0) { if (strcmp(".", git_buf_cstr(&remote_name)) != 0) {
if ((error = git_remote_lookup(&remote, repo, remote_name)) < 0) if ((error = git_remote_lookup(&remote, repo, git_buf_cstr(&remote_name))) < 0)
goto cleanup; goto cleanup;
refspec = git_remote__matching_refspec(remote, merge_name); refspec = git_remote__matching_refspec(remote, git_buf_cstr(&merge_name));
if (!refspec) { if (!refspec) {
error = GIT_ENOTFOUND; error = GIT_ENOTFOUND;
goto cleanup; goto cleanup;
} }
if (git_refspec_transform(&buf, refspec, merge_name) < 0) if (git_refspec_transform(&buf, refspec, git_buf_cstr(&merge_name)) < 0)
goto cleanup; goto cleanup;
} else } else
if (git_buf_sets(&buf, merge_name) < 0) if (git_buf_set(&buf, git_buf_cstr(&merge_name), git_buf_len(&merge_name)) < 0)
goto cleanup; goto cleanup;
error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf)); error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf));
...@@ -376,6 +377,8 @@ int git_branch_upstream_name( ...@@ -376,6 +377,8 @@ int git_branch_upstream_name(
cleanup: cleanup:
git_config_free(config); git_config_free(config);
git_remote_free(remote); git_remote_free(remote);
git_buf_free(&remote_name);
git_buf_free(&merge_name);
git_buf_free(&buf); git_buf_free(&buf);
return error; return error;
} }
...@@ -383,29 +386,25 @@ cleanup: ...@@ -383,29 +386,25 @@ cleanup:
int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname) int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname)
{ {
int error; int error;
const char *str;
git_config *cfg; git_config *cfg;
if (!git_reference__is_branch(refname)) if (!git_reference__is_branch(refname))
return not_a_local_branch(refname); return not_a_local_branch(refname);
git_buf_sanitize(buf); if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
if ((error = git_repository_config_snapshot(&cfg, repo)) < 0)
return error; return error;
if ((error = retrieve_upstream_configuration(&str, cfg, refname, "branch.%s.remote")) < 0) git_buf_sanitize(buf);
goto cleanup;
if (!*str) { if ((error = retrieve_upstream_configuration(buf, cfg, refname, "branch.%s.remote")) < 0)
return error;
if (git_buf_len(buf) == 0) {
giterr_set(GITERR_REFERENCE, "branch '%s' does not have an upstream remote", refname); giterr_set(GITERR_REFERENCE, "branch '%s' does not have an upstream remote", refname);
error = GIT_ENOTFOUND; error = GIT_ENOTFOUND;
goto cleanup; git_buf_clear(buf);
} }
error = git_buf_puts(buf, str);
cleanup:
git_config_free(cfg);
return error; return error;
} }
......
...@@ -2390,25 +2390,27 @@ static int checkout_data_init( ...@@ -2390,25 +2390,27 @@ static int checkout_data_init(
if ((data->opts.checkout_strategy & if ((data->opts.checkout_strategy &
(GIT_CHECKOUT_CONFLICT_STYLE_MERGE | GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)) == 0) { (GIT_CHECKOUT_CONFLICT_STYLE_MERGE | GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)) == 0) {
const char *conflict_style; git_config_entry *conflict_style = NULL;
git_config *cfg = NULL; git_config *cfg = NULL;
if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 || if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 ||
(error = git_config_get_string(&conflict_style, cfg, "merge.conflictstyle")) < 0 || (error = git_config_get_entry(&conflict_style, cfg, "merge.conflictstyle")) < 0 ||
error == GIT_ENOTFOUND) error == GIT_ENOTFOUND)
; ;
else if (error) else if (error)
goto cleanup; goto cleanup;
else if (strcmp(conflict_style, "merge") == 0) else if (strcmp(conflict_style->value, "merge") == 0)
data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_MERGE; data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_MERGE;
else if (strcmp(conflict_style, "diff3") == 0) else if (strcmp(conflict_style->value, "diff3") == 0)
data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3; data->opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3;
else { else {
giterr_set(GITERR_CHECKOUT, "unknown style '%s' given for 'merge.conflictstyle'", giterr_set(GITERR_CHECKOUT, "unknown style '%s' given for 'merge.conflictstyle'",
conflict_style); conflict_style);
error = -1; error = -1;
git_config_entry_free(conflict_style);
goto cleanup; goto cleanup;
} }
git_config_entry_free(conflict_style);
} }
if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
......
...@@ -19,6 +19,14 @@ ...@@ -19,6 +19,14 @@
#include <ctype.h> #include <ctype.h>
void git_config_entry_free(git_config_entry *entry)
{
if (!entry)
return;
entry->free(entry);
}
typedef struct { typedef struct {
git_refcount rc; git_refcount rc;
...@@ -638,7 +646,7 @@ int git_config__update_entry( ...@@ -638,7 +646,7 @@ int git_config__update_entry(
bool only_if_existing) bool only_if_existing)
{ {
int error = 0; int error = 0;
const git_config_entry *ce = NULL; git_config_entry *ce = NULL;
if ((error = git_config__lookup_entry(&ce, config, key, false)) < 0) if ((error = git_config__lookup_entry(&ce, config, key, false)) < 0)
return error; return error;
...@@ -657,6 +665,7 @@ int git_config__update_entry( ...@@ -657,6 +665,7 @@ int git_config__update_entry(
else else
error = git_config_set_string(config, key, value); error = git_config_set_string(config, key, value);
git_config_entry_free(ce);
return error; return error;
} }
...@@ -677,7 +686,7 @@ enum { ...@@ -677,7 +686,7 @@ enum {
}; };
static int get_entry( static int get_entry(
const git_config_entry **out, git_config_entry **out,
const git_config *cfg, const git_config *cfg,
const char *name, const char *name,
bool normalize_name, bool normalize_name,
...@@ -721,13 +730,13 @@ cleanup: ...@@ -721,13 +730,13 @@ cleanup:
} }
int git_config_get_entry( int git_config_get_entry(
const git_config_entry **out, const git_config *cfg, const char *name) git_config_entry **out, const git_config *cfg, const char *name)
{ {
return get_entry(out, cfg, name, true, GET_ALL_ERRORS); return get_entry(out, cfg, name, true, GET_ALL_ERRORS);
} }
int git_config__lookup_entry( int git_config__lookup_entry(
const git_config_entry **out, git_config_entry **out,
const git_config *cfg, const git_config *cfg,
const char *key, const char *key,
bool no_errors) bool no_errors)
...@@ -743,87 +752,154 @@ int git_config_get_mapped( ...@@ -743,87 +752,154 @@ int git_config_get_mapped(
const git_cvar_map *maps, const git_cvar_map *maps,
size_t map_n) size_t map_n)
{ {
const git_config_entry *entry; git_config_entry *entry;
int ret; int ret;
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
return ret; return ret;
return git_config_lookup_map_value(out, maps, map_n, entry->value); ret = git_config_lookup_map_value(out, maps, map_n, entry->value);
git_config_entry_free(entry);
return ret;
} }
int git_config_get_int64(int64_t *out, const git_config *cfg, const char *name) int git_config_get_int64(int64_t *out, const git_config *cfg, const char *name)
{ {
const git_config_entry *entry; git_config_entry *entry;
int ret; int ret;
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
return ret; return ret;
return git_config_parse_int64(out, entry->value); ret = git_config_parse_int64(out, entry->value);
git_config_entry_free(entry);
return ret;
} }
int git_config_get_int32(int32_t *out, const git_config *cfg, const char *name) int git_config_get_int32(int32_t *out, const git_config *cfg, const char *name)
{ {
const git_config_entry *entry; git_config_entry *entry;
int ret; int ret;
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
return ret; return ret;
return git_config_parse_int32(out, entry->value); ret = git_config_parse_int32(out, entry->value);
git_config_entry_free(entry);
return ret;
} }
int git_config_get_bool(int *out, const git_config *cfg, const char *name) int git_config_get_bool(int *out, const git_config *cfg, const char *name)
{ {
const git_config_entry *entry; git_config_entry *entry;
int ret; int ret;
if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) if ((ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
return ret; return ret;
return git_config_parse_bool(out, entry->value); ret = git_config_parse_bool(out, entry->value);
git_config_entry_free(entry);
return ret;
}
static int is_readonly(const git_config *cfg)
{
size_t i;
file_internal *internal;
git_vector_foreach(&cfg->files, i, internal) {
if (!internal || !internal->file)
continue;
if (!internal->file->readonly)
return 0;
}
return 1;
} }
int git_config_get_path(git_buf *out, const git_config *cfg, const char *name) int git_config_get_path(git_buf *out, const git_config *cfg, const char *name)
{ {
const git_config_entry *entry; git_config_entry *entry;
int error; int error;
if ((error = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0) if ((error = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS)) < 0)
return error; return error;
return git_config_parse_path(out, entry->value); error = git_config_parse_path(out, entry->value);
git_config_entry_free(entry);
return error;
} }
int git_config_get_string( int git_config_get_string(
const char **out, const git_config *cfg, const char *name) const char **out, const git_config *cfg, const char *name)
{ {
const git_config_entry *entry; git_config_entry *entry;
int ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS); int ret;
if (!is_readonly(cfg)) {
giterr_set(GITERR_CONFIG, "get_string called on a live config object");
return -1;
}
ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS);
*out = !ret ? (entry->value ? entry->value : "") : NULL; *out = !ret ? (entry->value ? entry->value : "") : NULL;
git_config_entry_free(entry);
return ret; return ret;
} }
const char *git_config__get_string_force( int git_config_get_string_buf(
git_buf *out, const git_config *cfg, const char *name)
{
git_config_entry *entry;
int ret;
const char *str;
git_buf_sanitize(out);
ret = get_entry(&entry, cfg, name, true, GET_ALL_ERRORS);
str = !ret ? (entry->value ? entry->value : "") : NULL;
if (str)
ret = git_buf_puts(out, str);
git_config_entry_free(entry);
return ret;
}
char *git_config__get_string_force(
const git_config *cfg, const char *key, const char *fallback_value) const git_config *cfg, const char *key, const char *fallback_value)
{ {
const git_config_entry *entry; git_config_entry *entry;
char *ret;
get_entry(&entry, cfg, key, false, GET_NO_ERRORS); get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
return (entry && entry->value) ? entry->value : fallback_value; ret = (entry && entry->value) ? git__strdup(entry->value) : fallback_value ? git__strdup(fallback_value) : NULL;
git_config_entry_free(entry);
return ret;
} }
int git_config__get_bool_force( int git_config__get_bool_force(
const git_config *cfg, const char *key, int fallback_value) const git_config *cfg, const char *key, int fallback_value)
{ {
int val = fallback_value; int val = fallback_value;
const git_config_entry *entry; git_config_entry *entry;
get_entry(&entry, cfg, key, false, GET_NO_ERRORS); get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
if (entry && git_config_parse_bool(&val, entry->value) < 0) if (entry && git_config_parse_bool(&val, entry->value) < 0)
giterr_clear(); giterr_clear();
git_config_entry_free(entry);
return val; return val;
} }
...@@ -831,13 +907,14 @@ int git_config__get_int_force( ...@@ -831,13 +907,14 @@ int git_config__get_int_force(
const git_config *cfg, const char *key, int fallback_value) const git_config *cfg, const char *key, int fallback_value)
{ {
int32_t val = (int32_t)fallback_value; int32_t val = (int32_t)fallback_value;
const git_config_entry *entry; git_config_entry *entry;
get_entry(&entry, cfg, key, false, GET_NO_ERRORS); get_entry(&entry, cfg, key, false, GET_NO_ERRORS);
if (entry && git_config_parse_int32(&val, entry->value) < 0) if (entry && git_config_parse_int32(&val, entry->value) < 0)
giterr_clear(); giterr_clear();
git_config_entry_free(entry);
return (int)val; return (int)val;
} }
......
...@@ -48,7 +48,7 @@ extern int git_config__normalize_name(const char *in, char **out); ...@@ -48,7 +48,7 @@ extern int git_config__normalize_name(const char *in, char **out);
/* internal only: does not normalize key and sets out to NULL if not found */ /* internal only: does not normalize key and sets out to NULL if not found */
extern int git_config__lookup_entry( extern int git_config__lookup_entry(
const git_config_entry **out, git_config_entry **out,
const git_config *cfg, const git_config *cfg,
const char *key, const char *key,
bool no_errors); bool no_errors);
...@@ -67,7 +67,7 @@ extern int git_config__update_entry( ...@@ -67,7 +67,7 @@ extern int git_config__update_entry(
* failures occur while trying to access the value. * failures occur while trying to access the value.
*/ */
extern const char *git_config__get_string_force( extern char *git_config__get_string_force(
const git_config *cfg, const char *key, const char *fallback_value); const git_config *cfg, const char *key, const char *fallback_value);
extern int git_config__get_bool_force( extern int git_config__get_bool_force(
......
...@@ -84,7 +84,7 @@ int git_config__cvar(int *out, git_config *config, git_cvar_cached cvar) ...@@ -84,7 +84,7 @@ int git_config__cvar(int *out, git_config *config, git_cvar_cached cvar)
{ {
int error = 0; int error = 0;
struct map_data *data = &_cvar_maps[(int)cvar]; struct map_data *data = &_cvar_maps[(int)cvar];
const git_config_entry *entry; git_config_entry *entry;
git_config__lookup_entry(&entry, config, data->cvar_name, false); git_config__lookup_entry(&entry, config, data->cvar_name, false);
...@@ -96,6 +96,7 @@ int git_config__cvar(int *out, git_config *config, git_cvar_cached cvar) ...@@ -96,6 +96,7 @@ int git_config__cvar(int *out, git_config *config, git_cvar_cached cvar)
else else
error = git_config_parse_bool(out, entry->value); error = git_config_parse_bool(out, entry->value);
git_config_entry_free(entry);
return error; return error;
} }
......
...@@ -96,7 +96,6 @@ typedef struct { ...@@ -96,7 +96,6 @@ typedef struct {
/* mutex to coordinate accessing the values */ /* mutex to coordinate accessing the values */
git_mutex values_mutex; git_mutex values_mutex;
refcounted_strmap *values; refcounted_strmap *values;
int readonly;
} diskfile_header; } diskfile_header;
typedef struct { typedef struct {
...@@ -504,19 +503,26 @@ out: ...@@ -504,19 +503,26 @@ out:
return ret; return ret;
} }
/* release the map containing the entry as an equivalent to freeing it */
static void release_map(git_config_entry *entry)
{
refcounted_strmap *map = (refcounted_strmap *) entry->payload;
refcounted_strmap_free(map);
}
/* /*
* Internal function that actually gets the value in string form * Internal function that actually gets the value in string form
*/ */
static int config_get(git_config_backend *cfg, const char *key, const git_config_entry **out) static int config_get(git_config_backend *cfg, const char *key, git_config_entry **out)
{ {
diskfile_header *h = (diskfile_header *)cfg; diskfile_header *h = (diskfile_header *)cfg;
refcounted_strmap *map; refcounted_strmap *map;
git_strmap *values; git_strmap *values;
khiter_t pos; khiter_t pos;
cvar_t *var; cvar_t *var;
int error; int error = 0;
if (!h->readonly && ((error = config_refresh(cfg)) < 0)) if (!h->parent.readonly && ((error = config_refresh(cfg)) < 0))
return error; return error;
map = refcounted_strmap_take(h); map = refcounted_strmap_take(h);
...@@ -534,9 +540,11 @@ static int config_get(git_config_backend *cfg, const char *key, const git_config ...@@ -534,9 +540,11 @@ static int config_get(git_config_backend *cfg, const char *key, const git_config
while (var->next) while (var->next)
var = var->next; var = var->next;
refcounted_strmap_free(map);
*out = var->entry; *out = var->entry;
return 0; (*out)->free = release_map;
(*out)->payload = map;
return error;
} }
static int config_set_multivar( static int config_set_multivar(
...@@ -763,7 +771,7 @@ static int config_readonly_open(git_config_backend *cfg, git_config_level_t leve ...@@ -763,7 +771,7 @@ static int config_readonly_open(git_config_backend *cfg, git_config_level_t leve
refcounted_strmap *src_map; refcounted_strmap *src_map;
int error; int error;
if (!src_header->readonly && (error = config_refresh(&src_header->parent)) < 0) if (!src_header->parent.readonly && (error = config_refresh(&src_header->parent)) < 0)
return error; return error;
/* We're just copying data, don't care about the level */ /* We're just copying data, don't care about the level */
...@@ -787,7 +795,7 @@ int git_config_file__snapshot(git_config_backend **out, diskfile_backend *in) ...@@ -787,7 +795,7 @@ int git_config_file__snapshot(git_config_backend **out, diskfile_backend *in)
backend->snapshot_from = in; backend->snapshot_from = in;
backend->header.readonly = 1; backend->header.parent.readonly = 1;
backend->header.parent.version = GIT_CONFIG_BACKEND_VERSION; backend->header.parent.version = GIT_CONFIG_BACKEND_VERSION;
backend->header.parent.open = config_readonly_open; backend->header.parent.open = config_readonly_open;
backend->header.parent.get = config_get; backend->header.parent.get = config_get;
......
...@@ -21,7 +21,7 @@ GIT_INLINE(void) git_config_file_free(git_config_backend *cfg) ...@@ -21,7 +21,7 @@ GIT_INLINE(void) git_config_file_free(git_config_backend *cfg)
} }
GIT_INLINE(int) git_config_file_get_string( GIT_INLINE(int) git_config_file_get_string(
const git_config_entry **out, git_config_backend *cfg, const char *name) git_config_entry **out, git_config_backend *cfg, const char *name)
{ {
return cfg->get(cfg, name, out); return cfg->get(cfg, name, out);
} }
......
...@@ -461,12 +461,13 @@ static int diff_list_apply_options( ...@@ -461,12 +461,13 @@ static int diff_list_apply_options(
/* if ignore_submodules not explicitly set, check diff config */ /* if ignore_submodules not explicitly set, check diff config */
if (diff->opts.ignore_submodules <= 0) { if (diff->opts.ignore_submodules <= 0) {
const git_config_entry *entry; git_config_entry *entry;
git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true); git_config__lookup_entry(&entry, cfg, "diff.ignoresubmodules", true);
if (entry && git_submodule_parse_ignore( if (entry && git_submodule_parse_ignore(
&diff->opts.ignore_submodules, entry->value) < 0) &diff->opts.ignore_submodules, entry->value) < 0)
giterr_clear(); giterr_clear();
git_config_entry_free(entry);
} }
/* if either prefix is not set, figure out appropriate value */ /* if either prefix is not set, figure out appropriate value */
......
...@@ -242,7 +242,7 @@ static int git_diff_driver_load( ...@@ -242,7 +242,7 @@ static int git_diff_driver_load(
khiter_t pos; khiter_t pos;
git_config *cfg = NULL; git_config *cfg = NULL;
git_buf name = GIT_BUF_INIT; git_buf name = GIT_BUF_INIT;
const git_config_entry *ce; git_config_entry *ce = NULL;
bool found_driver = false; bool found_driver = false;
if ((reg = git_repository_driver_registry(repo)) == NULL) if ((reg = git_repository_driver_registry(repo)) == NULL)
...@@ -341,6 +341,7 @@ static int git_diff_driver_load( ...@@ -341,6 +341,7 @@ static int git_diff_driver_load(
*out = drv; *out = drv;
done: done:
git_config_entry_free(ce);
git_buf_free(&name); git_buf_free(&name);
git_config_free(cfg); git_config_free(cfg);
......
...@@ -52,7 +52,7 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch) ...@@ -52,7 +52,7 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
static int download_tags_value(git_remote *remote, git_config *cfg) static int download_tags_value(git_remote *remote, git_config *cfg)
{ {
const git_config_entry *ce; git_config_entry *ce;
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
int error; int error;
...@@ -70,6 +70,7 @@ static int download_tags_value(git_remote *remote, git_config *cfg) ...@@ -70,6 +70,7 @@ static int download_tags_value(git_remote *remote, git_config *cfg)
remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; remote->download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL;
} }
git_config_entry_free(ce);
return error; return error;
} }
...@@ -548,7 +549,7 @@ int git_remote_save(const git_remote *remote) ...@@ -548,7 +549,7 @@ int git_remote_save(const git_remote *remote)
git_config *cfg; git_config *cfg;
const char *tagopt = NULL; const char *tagopt = NULL;
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
const git_config_entry *existing; git_config_entry *existing = NULL;
assert(remote); assert(remote);
...@@ -618,6 +619,7 @@ int git_remote_save(const git_remote *remote) ...@@ -618,6 +619,7 @@ int git_remote_save(const git_remote *remote)
cfg, git_buf_cstr(&buf), tagopt, true, false); cfg, git_buf_cstr(&buf), tagopt, true, false);
cleanup: cleanup:
git_config_entry_free(existing);
git_buf_free(&buf); git_buf_free(&buf);
return error; return error;
} }
...@@ -753,7 +755,7 @@ int git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote ...@@ -753,7 +755,7 @@ int git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote
int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url) int git_remote__get_http_proxy(git_remote *remote, bool use_ssl, char **proxy_url)
{ {
git_config *cfg; git_config *cfg;
const git_config_entry *ce; git_config_entry *ce = NULL;
const char *val = NULL; const char *val = NULL;
int error; int error;
...@@ -805,6 +807,7 @@ found: ...@@ -805,6 +807,7 @@ found:
*proxy_url = git__strdup(val); *proxy_url = git__strdup(val);
GITERR_CHECK_ALLOC(*proxy_url); GITERR_CHECK_ALLOC(*proxy_url);
} }
git_config_entry_free(ce);
return 0; return 0;
} }
......
...@@ -191,7 +191,7 @@ static int load_config_data(git_repository *repo, const git_config *config) ...@@ -191,7 +191,7 @@ static int load_config_data(git_repository *repo, const git_config *config)
static int load_workdir(git_repository *repo, git_config *config, git_buf *parent_path) static int load_workdir(git_repository *repo, git_config *config, git_buf *parent_path)
{ {
int error; int error;
const git_config_entry *ce; git_config_entry *ce;
git_buf worktree = GIT_BUF_INIT; git_buf worktree = GIT_BUF_INIT;
if (repo->is_bare) if (repo->is_bare)
...@@ -204,7 +204,7 @@ static int load_workdir(git_repository *repo, git_config *config, git_buf *paren ...@@ -204,7 +204,7 @@ static int load_workdir(git_repository *repo, git_config *config, git_buf *paren
if (ce && ce->value) { if (ce && ce->value) {
if ((error = git_path_prettify_dir( if ((error = git_path_prettify_dir(
&worktree, ce->value, repo->path_repository)) < 0) &worktree, ce->value, repo->path_repository)) < 0)
return error; goto cleanup;
repo->workdir = git_buf_detach(&worktree); repo->workdir = git_buf_detach(&worktree);
} }
...@@ -212,14 +212,18 @@ static int load_workdir(git_repository *repo, git_config *config, git_buf *paren ...@@ -212,14 +212,18 @@ static int load_workdir(git_repository *repo, git_config *config, git_buf *paren
repo->workdir = git_buf_detach(parent_path); repo->workdir = git_buf_detach(parent_path);
else { else {
if (git_path_dirname_r(&worktree, repo->path_repository) < 0 || if (git_path_dirname_r(&worktree, repo->path_repository) < 0 ||
git_path_to_dir(&worktree) < 0) git_path_to_dir(&worktree) < 0) {
return -1; error = -1;
goto cleanup;
}
repo->workdir = git_buf_detach(&worktree); repo->workdir = git_buf_detach(&worktree);
} }
GITERR_CHECK_ALLOC(repo->workdir); GITERR_CHECK_ALLOC(repo->workdir);
return 0; cleanup:
git_config_entry_free(ce);
return error;
} }
/* /*
......
...@@ -291,7 +291,7 @@ void test_clone_nonetwork__clone_from_empty_sets_upstream(void) ...@@ -291,7 +291,7 @@ void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
cl_set_cleanup(&cleanup_repository, "./repowithunborn"); cl_set_cleanup(&cleanup_repository, "./repowithunborn");
cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL)); cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string(&str, config, "branch.master.remote")); cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s("origin", str); cl_assert_equal_s("origin", str);
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "config_helpers.h" #include "config_helpers.h"
#include "repository.h" #include "repository.h"
#include "buffer.h"
void assert_config_entry_existence( void assert_config_entry_existence(
git_repository *repo, git_repository *repo,
...@@ -8,12 +9,13 @@ void assert_config_entry_existence( ...@@ -8,12 +9,13 @@ void assert_config_entry_existence(
bool is_supposed_to_exist) bool is_supposed_to_exist)
{ {
git_config *config; git_config *config;
const char *out; git_config_entry *entry = NULL;
int result; int result;
cl_git_pass(git_repository_config__weakptr(&config, repo)); cl_git_pass(git_repository_config__weakptr(&config, repo));
result = git_config_get_string(&out, config, name); result = git_config_get_entry(&entry, config, name);
git_config_entry_free(entry);
if (is_supposed_to_exist) if (is_supposed_to_exist)
cl_git_pass(result); cl_git_pass(result);
...@@ -27,13 +29,14 @@ void assert_config_entry_value( ...@@ -27,13 +29,14 @@ void assert_config_entry_value(
const char *expected_value) const char *expected_value)
{ {
git_config *config; git_config *config;
const char *out; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_repository_config__weakptr(&config, repo)); cl_git_pass(git_repository_config__weakptr(&config, repo));
cl_git_pass(git_config_get_string(&out, config, name)); cl_git_pass(git_config_get_string_buf(&buf, config, name));
cl_assert_equal_s(expected_value, out); cl_assert_equal_s(expected_value, git_buf_cstr(&buf));
git_buf_free(&buf);
} }
static int count_config_entries_cb( static int count_config_entries_cb(
......
...@@ -22,7 +22,7 @@ void test_config_configlevel__adding_the_same_level_twice_returns_EEXISTS(void) ...@@ -22,7 +22,7 @@ void test_config_configlevel__adding_the_same_level_twice_returns_EEXISTS(void)
void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(void) void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(void)
{ {
git_config *cfg; git_config *cfg;
const char *s; git_buf buf = {0};
cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_new(&cfg));
cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
...@@ -30,9 +30,10 @@ void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(voi ...@@ -30,9 +30,10 @@ void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(voi
cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"), cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"),
GIT_CONFIG_LEVEL_LOCAL, 1)); GIT_CONFIG_LEVEL_LOCAL, 1));
cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.stringglobal"));
cl_assert_equal_s("don't find me!", s); cl_assert_equal_s("don't find me!", buf.ptr);
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -40,7 +41,7 @@ void test_config_configlevel__can_read_from_a_single_level_focused_file_after_pa ...@@ -40,7 +41,7 @@ void test_config_configlevel__can_read_from_a_single_level_focused_file_after_pa
{ {
git_config *cfg; git_config *cfg;
git_config *single_level_cfg; git_config *single_level_cfg;
const char *s; git_buf buf = {0};
cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_new(&cfg));
cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
...@@ -52,9 +53,10 @@ void test_config_configlevel__can_read_from_a_single_level_focused_file_after_pa ...@@ -52,9 +53,10 @@ void test_config_configlevel__can_read_from_a_single_level_focused_file_after_pa
git_config_free(cfg); git_config_free(cfg);
cl_git_pass(git_config_get_string(&s, single_level_cfg, "core.stringglobal")); cl_git_pass(git_config_get_string_buf(&buf, single_level_cfg, "core.stringglobal"));
cl_assert_equal_s("don't find me!", s); cl_assert_equal_s("don't find me!", buf.ptr);
git_buf_free(&buf);
git_config_free(single_level_cfg); git_config_free(single_level_cfg);
} }
......
...@@ -46,8 +46,9 @@ void test_config_global__open_global(void) ...@@ -46,8 +46,9 @@ void test_config_global__open_global(void)
void test_config_global__open_xdg(void) void test_config_global__open_xdg(void)
{ {
git_config *cfg, *xdg, *selected; git_config *cfg, *xdg, *selected;
const char *val, *str = "teststring"; const char *str = "teststring";
const char *key = "this.variable"; const char *key = "this.variable";
git_buf buf = {0};
cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n test = 1\n"); cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n test = 1\n");
...@@ -56,9 +57,10 @@ void test_config_global__open_xdg(void) ...@@ -56,9 +57,10 @@ void test_config_global__open_xdg(void)
cl_git_pass(git_config_open_global(&selected, cfg)); cl_git_pass(git_config_open_global(&selected, cfg));
cl_git_pass(git_config_set_string(xdg, key, str)); cl_git_pass(git_config_set_string(xdg, key, str));
cl_git_pass(git_config_get_string(&val, selected, key)); cl_git_pass(git_config_get_string_buf(&buf, selected, key));
cl_assert_equal_s(str, val); cl_assert_equal_s(str, buf.ptr);
git_buf_free(&buf);
git_config_free(selected); git_config_free(selected);
git_config_free(xdg); git_config_free(xdg);
git_config_free(cfg); git_config_free(cfg);
......
...@@ -5,20 +5,20 @@ ...@@ -5,20 +5,20 @@
void test_config_include__relative(void) void test_config_include__relative(void)
{ {
git_config *cfg; git_config *cfg;
const char *str; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-include"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-include")));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.baz")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
cl_assert_equal_s(str, "huzzah"); cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
void test_config_include__absolute(void) void test_config_include__absolute(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_printf(&buf, "[include]\npath = %s/config-included", cl_fixture("config"))); cl_git_pass(git_buf_printf(&buf, "[include]\npath = %s/config-included", cl_fixture("config")));
...@@ -27,25 +27,27 @@ void test_config_include__absolute(void) ...@@ -27,25 +27,27 @@ void test_config_include__absolute(void)
git_buf_free(&buf); git_buf_free(&buf);
cl_git_pass(git_config_open_ondisk(&cfg, "config-include-absolute")); cl_git_pass(git_config_open_ondisk(&cfg, "config-include-absolute"));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.baz")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
cl_assert_equal_s(str, "huzzah"); cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
void test_config_include__homedir(void) void test_config_include__homedir(void)
{ {
git_config *cfg; git_config *cfg;
const char *str; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config"))); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
cl_git_mkfile("config-include-homedir", "[include]\npath = ~/config-included"); cl_git_mkfile("config-include-homedir", "[include]\npath = ~/config-included");
cl_git_pass(git_config_open_ondisk(&cfg, "config-include-homedir")); cl_git_pass(git_config_open_ondisk(&cfg, "config-include-homedir"));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.baz")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
cl_assert_equal_s(str, "huzzah"); cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
cl_sandbox_set_search_path_defaults(); cl_sandbox_set_search_path_defaults();
...@@ -55,7 +57,7 @@ void test_config_include__homedir(void) ...@@ -55,7 +57,7 @@ void test_config_include__homedir(void)
void test_config_include__ordering(void) void test_config_include__ordering(void)
{ {
git_config *cfg; git_config *cfg;
const char *str; git_buf buf = GIT_BUF_INIT;
cl_git_mkfile("included", "[foo \"bar\"]\nbaz = hurrah\nfrotz = hiya"); cl_git_mkfile("included", "[foo \"bar\"]\nbaz = hurrah\nfrotz = hiya");
cl_git_mkfile("including", cl_git_mkfile("including",
...@@ -65,11 +67,13 @@ void test_config_include__ordering(void) ...@@ -65,11 +67,13 @@ void test_config_include__ordering(void)
cl_git_pass(git_config_open_ondisk(&cfg, "including")); cl_git_pass(git_config_open_ondisk(&cfg, "including"));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.frotz")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.frotz"));
cl_assert_equal_s(str, "hiya"); cl_assert_equal_s("hiya", git_buf_cstr(&buf));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar.baz")); git_buf_clear(&buf);
cl_assert_equal_s(str, "huzzah"); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz"));
cl_assert_equal_s("huzzah", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -90,16 +94,17 @@ void test_config_include__depth(void) ...@@ -90,16 +94,17 @@ void test_config_include__depth(void)
void test_config_include__missing(void) void test_config_include__missing(void)
{ {
git_config *cfg; git_config *cfg;
const char *str; git_buf buf = GIT_BUF_INIT;
cl_git_mkfile("including", "[include]\npath = nonexistentfile\n[foo]\nbar = baz"); cl_git_mkfile("including", "[include]\npath = nonexistentfile\n[foo]\nbar = baz");
giterr_clear(); giterr_clear();
cl_git_pass(git_config_open_ondisk(&cfg, "including")); cl_git_pass(git_config_open_ondisk(&cfg, "including"));
cl_assert(giterr_last() == NULL); cl_assert(giterr_last() == NULL);
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
cl_assert_equal_s(str, "baz"); cl_assert_equal_s("baz", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -107,7 +112,7 @@ void test_config_include__missing(void) ...@@ -107,7 +112,7 @@ void test_config_include__missing(void)
void test_config_include__depth2(void) void test_config_include__depth2(void)
{ {
git_config *cfg; git_config *cfg;
const char *str; git_buf buf = GIT_BUF_INIT;
const char *content = "[include]\n" replicate10(replicate10("path=bottom\n")); const char *content = "[include]\n" replicate10(replicate10("path=bottom\n"));
cl_git_mkfile("top-level", "[include]\npath = middle\n[foo]\nbar = baz"); cl_git_mkfile("top-level", "[include]\npath = middle\n[foo]\nbar = baz");
...@@ -116,11 +121,12 @@ void test_config_include__depth2(void) ...@@ -116,11 +121,12 @@ void test_config_include__depth2(void)
cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
cl_assert_equal_s(str, "baz"); cl_assert_equal_s("baz", git_buf_cstr(&buf));
cl_git_pass(git_config_get_string(&str, cfg, "foo.bar2")); git_buf_clear(&buf);
cl_assert_equal_s(str, "baz2"); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar2"));
cl_assert_equal_s("baz2", git_buf_cstr(&buf));
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
void test_config_new__write_new_config(void) void test_config_new__write_new_config(void)
{ {
const char *out;
git_config *config; git_config *config;
git_buf buf = GIT_BUF_INIT;
cl_git_mkfile(TEST_CONFIG, ""); cl_git_mkfile(TEST_CONFIG, "");
cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
...@@ -21,11 +21,13 @@ void test_config_new__write_new_config(void) ...@@ -21,11 +21,13 @@ void test_config_new__write_new_config(void)
cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_get_string(&out, config, "color.ui")); cl_git_pass(git_config_get_string_buf(&buf, config, "color.ui"));
cl_assert_equal_s(out, "auto"); cl_assert_equal_s("auto", git_buf_cstr(&buf));
cl_git_pass(git_config_get_string(&out, config, "core.editor")); git_buf_clear(&buf);
cl_assert_equal_s(out, "ed"); cl_git_pass(git_config_get_string_buf(&buf, config, "core.editor"));
cl_assert_equal_s("ed", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(config); git_config_free(config);
p_unlink(TEST_CONFIG); p_unlink(TEST_CONFIG);
......
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
#include "buffer.h" #include "buffer.h"
#include "path.h" #include "path.h"
static git_buf buf = GIT_BUF_INIT;
void test_config_read__cleanup(void)
{
git_buf_free(&buf);
}
void test_config_read__simple_read(void) void test_config_read__simple_read(void)
{ {
git_config *cfg; git_config *cfg;
...@@ -25,14 +32,15 @@ void test_config_read__case_sensitive(void) ...@@ -25,14 +32,15 @@ void test_config_read__case_sensitive(void)
{ {
git_config *cfg; git_config *cfg;
int i; int i;
const char *str;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1")));
cl_git_pass(git_config_get_string(&str, cfg, "this.that.other")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.that.other"));
cl_assert_equal_s(str, "true"); cl_assert_equal_s("true", git_buf_cstr(&buf));
cl_git_pass(git_config_get_string(&str, cfg, "this.That.other")); git_buf_clear(&buf);
cl_assert_equal_s(str, "yes");
cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.other"));
cl_assert_equal_s("yes", git_buf_cstr(&buf));
cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other")); cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other"));
cl_assert(i == 1); cl_assert(i == 1);
...@@ -52,12 +60,11 @@ void test_config_read__case_sensitive(void) ...@@ -52,12 +60,11 @@ void test_config_read__case_sensitive(void)
void test_config_read__multiline_value(void) void test_config_read__multiline_value(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2")));
cl_git_pass(git_config_get_string(&str, cfg, "this.That.and")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.and"));
cl_assert_equal_s(str, "one one one two two three three"); cl_assert_equal_s("one one one two two three three", git_buf_cstr(&buf));
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -68,15 +75,14 @@ void test_config_read__multiline_value(void) ...@@ -68,15 +75,14 @@ void test_config_read__multiline_value(void)
void test_config_read__subsection_header(void) void test_config_read__subsection_header(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3")));
cl_git_pass(git_config_get_string(&str, cfg, "section.subsection.var")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "section.subsection.var"));
cl_assert_equal_s(str, "hello"); cl_assert_equal_s("hello", git_buf_cstr(&buf));
/* The subsection is transformed to lower-case */ /* The subsection is transformed to lower-case */
cl_must_fail(git_config_get_string(&str, cfg, "section.subSectIon.var")); cl_must_fail(git_config_get_string_buf(&buf, cfg, "section.subSectIon.var"));
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -84,21 +90,21 @@ void test_config_read__subsection_header(void) ...@@ -84,21 +90,21 @@ void test_config_read__subsection_header(void)
void test_config_read__lone_variable(void) void test_config_read__lone_variable(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
int i; int i;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4")));
cl_git_fail(git_config_get_int32(&i, cfg, "some.section.variable")); cl_git_fail(git_config_get_int32(&i, cfg, "some.section.variable"));
cl_git_pass(git_config_get_string(&str, cfg, "some.section.variable")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variable"));
cl_assert_equal_s(str, ""); cl_assert_equal_s("", git_buf_cstr(&buf));
git_buf_clear(&buf);
cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable")); cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable"));
cl_assert(i == 1); cl_assert(i == 1);
cl_git_pass(git_config_get_string(&str, cfg, "some.section.variableeq")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variableeq"));
cl_assert_equal_s(str, ""); cl_assert_equal_s("", git_buf_cstr(&buf));
cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variableeq")); cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variableeq"));
cl_assert(i == 0); cl_assert(i == 0);
...@@ -184,14 +190,14 @@ void test_config_read__header_in_last_line(void) ...@@ -184,14 +190,14 @@ void test_config_read__header_in_last_line(void)
void test_config_read__prefixes(void) void test_config_read__prefixes(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9")));
cl_git_pass(git_config_get_string(&str, cfg, "remote.ab.url")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.ab.url"));
cl_assert_equal_s(str, "http://example.com/git/ab"); cl_assert_equal_s("http://example.com/git/ab", git_buf_cstr(&buf));
git_buf_clear(&buf);
cl_git_pass(git_config_get_string(&str, cfg, "remote.abba.url")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.abba.url"));
cl_assert_equal_s(str, "http://example.com/git/abba"); cl_assert_equal_s("http://example.com/git/abba", git_buf_cstr(&buf));
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -199,11 +205,10 @@ void test_config_read__prefixes(void) ...@@ -199,11 +205,10 @@ void test_config_read__prefixes(void)
void test_config_read__escaping_quotes(void) void test_config_read__escaping_quotes(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13")));
cl_git_pass(git_config_get_string(&str, cfg, "core.editor")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.editor"));
cl_assert_equal_s("\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"", str); cl_assert_equal_s("\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"", git_buf_cstr(&buf));
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -363,15 +368,15 @@ void test_config_read__iterator_glob(void) ...@@ -363,15 +368,15 @@ void test_config_read__iterator_glob(void)
void test_config_read__whitespace_not_required_around_assignment(void) void test_config_read__whitespace_not_required_around_assignment(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config14"))); cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config14")));
cl_git_pass(git_config_get_string(&str, cfg, "a.b")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "a.b"));
cl_assert_equal_s(str, "c"); cl_assert_equal_s("c", git_buf_cstr(&buf));
git_buf_clear(&buf);
cl_git_pass(git_config_get_string(&str, cfg, "d.e")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "d.e"));
cl_assert_equal_s(str, "f"); cl_assert_equal_s("f", git_buf_cstr(&buf));
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -379,7 +384,7 @@ void test_config_read__whitespace_not_required_around_assignment(void) ...@@ -379,7 +384,7 @@ void test_config_read__whitespace_not_required_around_assignment(void)
void test_config_read__read_git_config_entry(void) void test_config_read__read_git_config_entry(void)
{ {
git_config *cfg; git_config *cfg;
const git_config_entry *entry; git_config_entry *entry;
cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_new(&cfg));
cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"),
...@@ -390,6 +395,7 @@ void test_config_read__read_git_config_entry(void) ...@@ -390,6 +395,7 @@ void test_config_read__read_git_config_entry(void)
cl_assert_equal_s("42", entry->value); cl_assert_equal_s("42", entry->value);
cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level); cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level);
git_config_entry_free(entry);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -480,7 +486,6 @@ void test_config_read__simple_read_from_specific_level(void) ...@@ -480,7 +486,6 @@ void test_config_read__simple_read_from_specific_level(void)
git_config *cfg, *cfg_specific; git_config *cfg, *cfg_specific;
int i; int i;
int64_t l, expected = +9223372036854775803; int64_t l, expected = +9223372036854775803;
const char *s;
cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_new(&cfg));
cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"),
...@@ -496,8 +501,8 @@ void test_config_read__simple_read_from_specific_level(void) ...@@ -496,8 +501,8 @@ void test_config_read__simple_read_from_specific_level(void)
cl_assert(l == expected); cl_assert(l == expected);
cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal")); cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal"));
cl_assert_equal_b(true, i); cl_assert_equal_b(true, i);
cl_git_pass(git_config_get_string(&s, cfg_specific, "core.stringglobal")); cl_git_pass(git_config_get_string_buf(&buf, cfg_specific, "core.stringglobal"));
cl_assert_equal_s("I'm a global config value!", s); cl_assert_equal_s("I'm a global config value!", git_buf_cstr(&buf));
git_config_free(cfg_specific); git_config_free(cfg_specific);
git_config_free(cfg); git_config_free(cfg);
...@@ -558,14 +563,13 @@ void test_config_read__corrupt_header3(void) ...@@ -558,14 +563,13 @@ void test_config_read__corrupt_header3(void)
void test_config_read__override_variable(void) void test_config_read__override_variable(void)
{ {
git_config *cfg; git_config *cfg;
const char *str;
cl_set_cleanup(&clean_test_config, NULL); cl_set_cleanup(&clean_test_config, NULL);
cl_git_mkfile("./testconfig", "[some] var = one\nvar = two"); cl_git_mkfile("./testconfig", "[some] var = one\nvar = two");
cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig"));
cl_git_pass(git_config_get_string(&str, cfg, "some.var")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var"));
cl_assert_equal_s(str, "two"); cl_assert_equal_s("two", git_buf_cstr(&buf));
git_config_free(cfg); git_config_free(cfg);
} }
......
...@@ -21,11 +21,12 @@ void test_config_rename__cleanup(void) ...@@ -21,11 +21,12 @@ void test_config_rename__cleanup(void)
void test_config_rename__can_rename(void) void test_config_rename__can_rename(void)
{ {
const git_config_entry *ce; git_config_entry *ce;
cl_git_pass(git_config_get_entry( cl_git_pass(git_config_get_entry(
&ce, g_config, "branch.track-local.remote")); &ce, g_config, "branch.track-local.remote"));
cl_assert_equal_s(".", ce->value); cl_assert_equal_s(".", ce->value);
git_config_entry_free(ce);
cl_git_fail(git_config_get_entry( cl_git_fail(git_config_get_entry(
&ce, g_config, "branch.local-track.remote")); &ce, g_config, "branch.local-track.remote"));
...@@ -36,6 +37,7 @@ void test_config_rename__can_rename(void) ...@@ -36,6 +37,7 @@ void test_config_rename__can_rename(void)
cl_git_pass(git_config_get_entry( cl_git_pass(git_config_get_entry(
&ce, g_config, "branch.local-track.remote")); &ce, g_config, "branch.local-track.remote"));
cl_assert_equal_s(".", ce->value); cl_assert_equal_s(".", ce->value);
git_config_entry_free(ce);
cl_git_fail(git_config_get_entry( cl_git_fail(git_config_get_entry(
&ce, g_config, "branch.track-local.remote")); &ce, g_config, "branch.track-local.remote"));
...@@ -43,7 +45,7 @@ void test_config_rename__can_rename(void) ...@@ -43,7 +45,7 @@ void test_config_rename__can_rename(void)
void test_config_rename__prevent_overwrite(void) void test_config_rename__prevent_overwrite(void)
{ {
const git_config_entry *ce; git_config_entry *ce;
cl_git_pass(git_config_set_string( cl_git_pass(git_config_set_string(
g_config, "branch.local-track.remote", "yellow")); g_config, "branch.local-track.remote", "yellow"));
...@@ -51,6 +53,7 @@ void test_config_rename__prevent_overwrite(void) ...@@ -51,6 +53,7 @@ void test_config_rename__prevent_overwrite(void)
cl_git_pass(git_config_get_entry( cl_git_pass(git_config_get_entry(
&ce, g_config, "branch.local-track.remote")); &ce, g_config, "branch.local-track.remote"));
cl_assert_equal_s("yellow", ce->value); cl_assert_equal_s("yellow", ce->value);
git_config_entry_free(ce);
cl_git_pass(git_config_rename_section( cl_git_pass(git_config_rename_section(
g_repo, "branch.track-local", "branch.local-track")); g_repo, "branch.track-local", "branch.local-track"));
...@@ -58,6 +61,7 @@ void test_config_rename__prevent_overwrite(void) ...@@ -58,6 +61,7 @@ void test_config_rename__prevent_overwrite(void)
cl_git_pass(git_config_get_entry( cl_git_pass(git_config_get_entry(
&ce, g_config, "branch.local-track.remote")); &ce, g_config, "branch.local-track.remote"));
cl_assert_equal_s(".", ce->value); cl_assert_equal_s(".", ce->value);
git_config_entry_free(ce);
/* so, we don't currently prevent overwrite... */ /* so, we don't currently prevent overwrite... */
/* { /* {
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#define TEST_CONFIG "git-test-config" #define TEST_CONFIG "git-test-config"
static git_buf buf = GIT_BUF_INIT;
void test_config_stress__initialize(void) void test_config_stress__initialize(void)
{ {
git_filebuf file = GIT_FILEBUF_INIT; git_filebuf file = GIT_FILEBUF_INIT;
...@@ -20,50 +22,43 @@ void test_config_stress__initialize(void) ...@@ -20,50 +22,43 @@ void test_config_stress__initialize(void)
void test_config_stress__cleanup(void) void test_config_stress__cleanup(void)
{ {
git_buf_free(&buf);
p_unlink(TEST_CONFIG); p_unlink(TEST_CONFIG);
} }
void test_config_stress__dont_break_on_invalid_input(void) void test_config_stress__dont_break_on_invalid_input(void)
{ {
const char *editor, *color;
git_config *config; git_config *config;
cl_assert(git_path_exists(TEST_CONFIG)); cl_assert(git_path_exists(TEST_CONFIG));
cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_get_string(&color, config, "color.ui")); cl_git_pass(git_config_get_string_buf(&buf, config, "color.ui"));
cl_git_pass(git_config_get_string(&editor, config, "core.editor")); cl_git_pass(git_config_get_string_buf(&buf, config, "core.editor"));
git_config_free(config); git_config_free(config);
} }
void assert_config_value(git_config *config, const char *key, const char *value)
{
git_buf_clear(&buf);
cl_git_pass(git_config_get_string_buf(&buf, config, key));
cl_assert_equal_s(value, git_buf_cstr(&buf));
}
void test_config_stress__comments(void) void test_config_stress__comments(void)
{ {
git_config *config; git_config *config;
const char *str;
cl_git_pass(git_config_open_ondisk(&config, cl_fixture("config/config12"))); cl_git_pass(git_config_open_ondisk(&config, cl_fixture("config/config12")));
cl_git_pass(git_config_get_string(&str, config, "some.section.test2")); assert_config_value(config, "some.section.test2", "hello");
cl_assert_equal_s("hello", str); assert_config_value(config, "some.section.test3", "welcome");
assert_config_value(config, "some.section.other", "hello! \" ; ; ; ");
cl_git_pass(git_config_get_string(&str, config, "some.section.test3")); assert_config_value(config, "some.section.other2", "cool! \" # # # ");
cl_assert_equal_s("welcome", str); assert_config_value(config, "some.section.multi", "hi, this is a ; multiline comment # with ;\n special chars and other stuff !@#");
assert_config_value(config, "some.section.multi2", "good, this is a ; multiline comment # with ;\n special chars and other stuff !@#");
cl_git_pass(git_config_get_string(&str, config, "some.section.other")); assert_config_value(config, "some.section.back", "this is \ba phrase");
cl_assert_equal_s("hello! \" ; ; ; ", str);
cl_git_pass(git_config_get_string(&str, config, "some.section.other2"));
cl_assert_equal_s("cool! \" # # # ", str);
cl_git_pass(git_config_get_string(&str, config, "some.section.multi"));
cl_assert_equal_s("hi, this is a ; multiline comment # with ;\n special chars and other stuff !@#", str);
cl_git_pass(git_config_get_string(&str, config, "some.section.multi2"));
cl_assert_equal_s("good, this is a ; multiline comment # with ;\n special chars and other stuff !@#", str);
cl_git_pass(git_config_get_string(&str, config, "some.section.back"));
cl_assert_equal_s("this is \ba phrase", str);
git_config_free(config); git_config_free(config);
} }
...@@ -71,7 +66,6 @@ void test_config_stress__comments(void) ...@@ -71,7 +66,6 @@ void test_config_stress__comments(void)
void test_config_stress__escape_subsection_names(void) void test_config_stress__escape_subsection_names(void)
{ {
git_config *config; git_config *config;
const char *str;
cl_assert(git_path_exists("git-test-config")); cl_assert(git_path_exists("git-test-config"));
cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
...@@ -81,15 +75,14 @@ void test_config_stress__escape_subsection_names(void) ...@@ -81,15 +75,14 @@ void test_config_stress__escape_subsection_names(void)
cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_get_string(&str, config, "some.sec\\tion.other")); assert_config_value(config, "some.sec\\tion.other", "foo");
cl_assert_equal_s("foo", str);
git_config_free(config); git_config_free(config);
} }
void test_config_stress__trailing_backslash(void) void test_config_stress__trailing_backslash(void)
{ {
git_config *config; git_config *config;
const char *str;
const char *path = "C:\\iam\\some\\windows\\path\\"; const char *path = "C:\\iam\\some\\windows\\path\\";
cl_assert(git_path_exists("git-test-config")); cl_assert(git_path_exists("git-test-config"));
...@@ -98,20 +91,19 @@ void test_config_stress__trailing_backslash(void) ...@@ -98,20 +91,19 @@ void test_config_stress__trailing_backslash(void)
git_config_free(config); git_config_free(config);
cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG));
cl_git_pass(git_config_get_string(&str, config, "windows.path")); assert_config_value(config, "windows.path", path);
cl_assert_equal_s(path, str);
git_config_free(config); git_config_free(config);
} }
void test_config_stress__complex(void) void test_config_stress__complex(void)
{ {
git_config *config; git_config *config;
const char *str;
const char *path = "./config-immediate-multiline"; const char *path = "./config-immediate-multiline";
cl_git_mkfile(path, "[imm]\n multi = \"\\\nfoo\""); cl_git_mkfile(path, "[imm]\n multi = \"\\\nfoo\"");
cl_git_pass(git_config_open_ondisk(&config, path)); cl_git_pass(git_config_open_ondisk(&config, path));
cl_git_pass(git_config_get_string(&str, config, "imm.multi")); assert_config_value(config, "imm.multi", "foo");
cl_assert_equal_s(str, "foo");
git_config_free(config); git_config_free(config);
} }
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include "config.h" #include "config.h"
static git_config *cfg; static git_config *cfg;
static const char *value;
void test_config_validkeyname__initialize(void) void test_config_validkeyname__initialize(void)
{ {
...@@ -22,7 +21,9 @@ void test_config_validkeyname__cleanup(void) ...@@ -22,7 +21,9 @@ void test_config_validkeyname__cleanup(void)
static void assert_invalid_config_key_name(const char *name) static void assert_invalid_config_key_name(const char *name)
{ {
cl_git_fail_with(git_config_get_string(&value, cfg, name), git_buf buf = GIT_BUF_INIT;
cl_git_fail_with(git_config_get_string_buf(&buf, cfg, name),
GIT_EINVALIDSPEC); GIT_EINVALIDSPEC);
cl_git_fail_with(git_config_set_string(cfg, name, "42"), cl_git_fail_with(git_config_set_string(cfg, name, "42"),
GIT_EINVALIDSPEC); GIT_EINVALIDSPEC);
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "buffer.h"
void test_config_write__initialize(void) void test_config_write__initialize(void)
{ {
...@@ -108,15 +109,17 @@ void test_config_write__delete_value_at_specific_level(void) ...@@ -108,15 +109,17 @@ void test_config_write__delete_value_at_specific_level(void)
void test_config_write__write_subsection(void) void test_config_write__write_subsection(void)
{ {
git_config *cfg; git_config *cfg;
const char *str; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_set_string(cfg, "my.own.var", "works")); cl_git_pass(git_config_set_string(cfg, "my.own.var", "works"));
git_config_free(cfg); git_config_free(cfg);
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_get_string(&str, cfg, "my.own.var")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "my.own.var"));
cl_assert_equal_s("works", str); cl_assert_equal_s("works", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -132,46 +135,52 @@ void test_config_write__delete_inexistent(void) ...@@ -132,46 +135,52 @@ void test_config_write__delete_inexistent(void)
void test_config_write__value_containing_quotes(void) void test_config_write__value_containing_quotes(void)
{ {
git_config *cfg; git_config *cfg;
const char* str; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes"));
cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar"));
cl_assert_equal_s(str, "this \"has\" quotes"); cl_assert_equal_s("this \"has\" quotes", git_buf_cstr(&buf));
git_buf_clear(&buf);
git_config_free(cfg); git_config_free(cfg);
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar"));
cl_assert_equal_s(str, "this \"has\" quotes"); cl_assert_equal_s("this \"has\" quotes", git_buf_cstr(&buf));
git_buf_clear(&buf);
git_config_free(cfg); git_config_free(cfg);
/* The code path for values that already exist is different, check that one as well */ /* The code path for values that already exist is different, check that one as well */
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_set_string(cfg, "core.somevar", "this also \"has\" quotes")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this also \"has\" quotes"));
cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar"));
cl_assert_equal_s(str, "this also \"has\" quotes"); cl_assert_equal_s("this also \"has\" quotes", git_buf_cstr(&buf));
git_buf_clear(&buf);
git_config_free(cfg); git_config_free(cfg);
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar"));
cl_assert_equal_s(str, "this also \"has\" quotes"); cl_assert_equal_s("this also \"has\" quotes", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
void test_config_write__escape_value(void) void test_config_write__escape_value(void)
{ {
git_config *cfg; git_config *cfg;
const char* str; git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t"));
cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar"));
cl_assert_equal_s(str, "this \"has\" quotes and \t"); cl_assert_equal_s("this \"has\" quotes and \t", git_buf_cstr(&buf));
git_buf_clear(&buf);
git_config_free(cfg); git_config_free(cfg);
cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar"));
cl_assert_equal_s(str, "this \"has\" quotes and \t"); cl_assert_equal_s("this \"has\" quotes and \t", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -180,7 +189,7 @@ void test_config_write__add_value_at_specific_level(void) ...@@ -180,7 +189,7 @@ void test_config_write__add_value_at_specific_level(void)
git_config *cfg, *cfg_specific; git_config *cfg, *cfg_specific;
int i; int i;
int64_t l, expected = +9223372036854775803; int64_t l, expected = +9223372036854775803;
const char *s; git_buf buf = GIT_BUF_INIT;
// open config15 as global level config file // open config15 as global level config file
cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_new(&cfg));
...@@ -207,9 +216,10 @@ void test_config_write__add_value_at_specific_level(void) ...@@ -207,9 +216,10 @@ void test_config_write__add_value_at_specific_level(void)
cl_assert(l == expected); cl_assert(l == expected);
cl_git_pass(git_config_get_bool(&i, cfg, "core.boolglobal")); cl_git_pass(git_config_get_bool(&i, cfg, "core.boolglobal"));
cl_assert_equal_b(true, i); cl_assert_equal_b(true, i);
cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.stringglobal"));
cl_assert_equal_s("I'm a global config value!", s); cl_assert_equal_s("I'm a global config value!", git_buf_cstr(&buf));
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
...@@ -247,7 +257,7 @@ void test_config_write__add_section_at_file_with_no_clrf_at_the_end(void) ...@@ -247,7 +257,7 @@ void test_config_write__add_section_at_file_with_no_clrf_at_the_end(void)
void test_config_write__add_value_which_needs_quotes(void) void test_config_write__add_value_which_needs_quotes(void)
{ {
git_config *cfg; git_config *cfg, *base;
const char* str1; const char* str1;
const char* str2; const char* str2;
const char* str3; const char* str3;
...@@ -262,7 +272,8 @@ void test_config_write__add_value_which_needs_quotes(void) ...@@ -262,7 +272,8 @@ void test_config_write__add_value_which_needs_quotes(void)
cl_git_pass(git_config_set_string(cfg, "core.startwhithsapceandcontainsdoublequote", " some\"thing")); cl_git_pass(git_config_set_string(cfg, "core.startwhithsapceandcontainsdoublequote", " some\"thing"));
git_config_free(cfg); git_config_free(cfg);
cl_git_pass(git_config_open_ondisk(&cfg, "config17")); cl_git_pass(git_config_open_ondisk(&base, "config17"));
cl_git_pass(git_config_snapshot(&cfg, base));
cl_git_pass(git_config_get_string(&str1, cfg, "core.startwithspace")); cl_git_pass(git_config_get_string(&str1, cfg, "core.startwithspace"));
cl_assert_equal_s(" Something", str1); cl_assert_equal_s(" Something", str1);
cl_git_pass(git_config_get_string(&str2, cfg, "core.endwithspace")); cl_git_pass(git_config_get_string(&str2, cfg, "core.endwithspace"));
...@@ -274,6 +285,7 @@ void test_config_write__add_value_which_needs_quotes(void) ...@@ -274,6 +285,7 @@ void test_config_write__add_value_which_needs_quotes(void)
cl_git_pass(git_config_get_string(&str5, cfg, "core.startwhithsapceandcontainsdoublequote")); cl_git_pass(git_config_get_string(&str5, cfg, "core.startwhithsapceandcontainsdoublequote"));
cl_assert_equal_s(" some\"thing", str5); cl_assert_equal_s(" some\"thing", str5);
git_config_free(cfg); git_config_free(cfg);
git_config_free(base);
} }
void test_config_write__can_set_a_value_to_NULL(void) void test_config_write__can_set_a_value_to_NULL(void)
...@@ -294,15 +306,16 @@ void test_config_write__can_set_an_empty_value(void) ...@@ -294,15 +306,16 @@ void test_config_write__can_set_an_empty_value(void)
{ {
git_repository *repository; git_repository *repository;
git_config *config; git_config *config;
const char * str; git_buf buf = {0};
repository = cl_git_sandbox_init("testrepo.git"); repository = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_config(&config, repository)); cl_git_pass(git_repository_config(&config, repository));
cl_git_pass(git_config_set_string(config, "core.somevar", "")); cl_git_pass(git_config_set_string(config, "core.somevar", ""));
cl_git_pass(git_config_get_string(&str, config, "core.somevar")); cl_git_pass(git_config_get_string_buf(&buf, config, "core.somevar"));
cl_assert_equal_s(str, ""); cl_assert_equal_s("", buf.ptr);
git_buf_free(&buf);
git_config_free(config); git_config_free(config);
cl_git_sandbox_cleanup(); cl_git_sandbox_cleanup();
} }
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "config/config_helpers.h"
#include "buffer.h" #include "buffer.h"
#include "refspec.h" #include "refspec.h"
#include "remote.h" #include "remote.h"
...@@ -385,26 +386,17 @@ void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void) ...@@ -385,26 +386,17 @@ void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
void test_network_remote_remotes__tagopt(void) void test_network_remote_remotes__tagopt(void)
{ {
const char *opt;
git_config *cfg;
cl_git_pass(git_repository_config(&cfg, _repo));
git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL); git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
cl_git_pass(git_remote_save(_remote)); cl_git_pass(git_remote_save(_remote));
cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); assert_config_entry_value(_repo, "remote.test.tagopt", "--tags");
cl_assert_equal_s("--tags", opt);
git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE); git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
cl_git_pass(git_remote_save(_remote)); cl_git_pass(git_remote_save(_remote));
cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); assert_config_entry_value(_repo, "remote.test.tagopt", "--no-tags");
cl_assert_equal_s("--no-tags", opt);
git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO); git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
cl_git_pass(git_remote_save(_remote)); cl_git_pass(git_remote_save(_remote));
cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND); assert_config_entry_existence(_repo, "remote.test.tagopt", false);
git_config_free(cfg);
} }
void test_network_remote_remotes__can_load_with_an_empty_url(void) void test_network_remote_remotes__can_load_with_an_empty_url(void)
......
...@@ -67,16 +67,17 @@ void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_coll ...@@ -67,16 +67,17 @@ void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_coll
{ {
git_reference *original_ref, *new_ref; git_reference *original_ref, *new_ref;
git_config *config; git_config *config;
const git_config_entry *ce; git_buf buf = GIT_BUF_INIT;
char *original_remote, *original_merge; char *original_remote, *original_merge;
const char *str;
cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
original_remote = strdup(ce->value);
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
original_merge = strdup(ce->value);
cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.remote"));
original_remote = git_buf_detach(&buf);
cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.merge"));
original_merge = git_buf_detach(&buf);
git_config_free(config);
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2")); cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
...@@ -84,20 +85,25 @@ void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_coll ...@@ -84,20 +85,25 @@ void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_coll
git_branch_move(&new_ref, original_ref, "master", 0)); git_branch_move(&new_ref, original_ref, "master", 0));
cl_assert(giterr_last()->message != NULL); cl_assert(giterr_last()->message != NULL);
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, ce->value);
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, ce->value);
cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, str);
git_config_free(config);
cl_assert_equal_i(GIT_EEXISTS, cl_assert_equal_i(GIT_EEXISTS,
git_branch_move(&new_ref, original_ref, "cannot-fetch", 0)); git_branch_move(&new_ref, original_ref, "cannot-fetch", 0));
cl_assert(giterr_last()->message != NULL); cl_assert(giterr_last()->message != NULL);
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, ce->value); cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge")); cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s(original_merge, ce->value); cl_assert_equal_s(original_remote, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, str);
git_config_free(config);
git_reference_free(original_ref); git_reference_free(original_ref);
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local")); cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local"));
...@@ -106,12 +112,14 @@ void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_coll ...@@ -106,12 +112,14 @@ void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_coll
git_branch_move(&new_ref, original_ref, "master", 0)); git_branch_move(&new_ref, original_ref, "master", 0));
cl_assert(giterr_last()->message != NULL); cl_assert(giterr_last()->message != NULL);
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, ce->value);
cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, ce->value);
free(original_remote); free(original_merge); cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, str);
git__free(original_remote); git__free(original_merge);
git_reference_free(original_ref); git_reference_free(original_ref);
git_config_free(config); git_config_free(config);
} }
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "config/config_helpers.h"
#include "refs.h" #include "refs.h"
static git_repository *repo; static git_repository *repo;
...@@ -123,8 +124,6 @@ void test_refs_branches_upstream__set_unset_upstream(void) ...@@ -123,8 +124,6 @@ void test_refs_branches_upstream__set_unset_upstream(void)
{ {
git_reference *branch; git_reference *branch;
git_repository *repository; git_repository *repository;
const char *value;
git_config *config;
repository = cl_git_sandbox_init("testrepo.git"); repository = cl_git_sandbox_init("testrepo.git");
...@@ -132,11 +131,8 @@ void test_refs_branches_upstream__set_unset_upstream(void) ...@@ -132,11 +131,8 @@ void test_refs_branches_upstream__set_unset_upstream(void)
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test")); cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
cl_git_pass(git_branch_set_upstream(branch, "test/master")); cl_git_pass(git_branch_set_upstream(branch, "test/master"));
cl_git_pass(git_repository_config(&config, repository)); assert_config_entry_value(repository, "branch.test.remote", "test");
cl_git_pass(git_config_get_string(&value, config, "branch.test.remote")); assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
cl_assert_equal_s(value, "test");
cl_git_pass(git_config_get_string(&value, config, "branch.test.merge"));
cl_assert_equal_s(value, "refs/heads/master");
git_reference_free(branch); git_reference_free(branch);
...@@ -144,25 +140,22 @@ void test_refs_branches_upstream__set_unset_upstream(void) ...@@ -144,25 +140,22 @@ void test_refs_branches_upstream__set_unset_upstream(void)
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test")); cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
cl_git_pass(git_branch_set_upstream(branch, "master")); cl_git_pass(git_branch_set_upstream(branch, "master"));
cl_git_pass(git_config_get_string(&value, config, "branch.test.remote")); assert_config_entry_value(repository, "branch.test.remote", ".");
cl_assert_equal_s(value, "."); assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
cl_git_pass(git_config_get_string(&value, config, "branch.test.merge"));
cl_assert_equal_s(value, "refs/heads/master");
/* unset */ /* unset */
cl_git_pass(git_branch_set_upstream(branch, NULL)); cl_git_pass(git_branch_set_upstream(branch, NULL));
cl_git_fail_with(git_config_get_string(&value, config, "branch.test.merge"), GIT_ENOTFOUND); assert_config_entry_existence(repository, "branch.test.remote", false);
cl_git_fail_with(git_config_get_string(&value, config, "branch.test.remote"), GIT_ENOTFOUND); assert_config_entry_existence(repository, "branch.test.merge", false);
git_reference_free(branch); git_reference_free(branch);
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master")); cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master"));
cl_git_pass(git_branch_set_upstream(branch, NULL)); cl_git_pass(git_branch_set_upstream(branch, NULL));
cl_git_fail_with(git_config_get_string(&value, config, "branch.master.merge"), GIT_ENOTFOUND); assert_config_entry_existence(repository, "branch.test.remote", false);
cl_git_fail_with(git_config_get_string(&value, config, "branch.master.remote"), GIT_ENOTFOUND); assert_config_entry_existence(repository, "branch.test.merge", false);
git_reference_free(branch); git_reference_free(branch);
git_config_free(config);
cl_git_sandbox_cleanup(); cl_git_sandbox_cleanup();
} }
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "repository.h" #include "repository.h"
#include "config.h" #include "config.h"
#include "path.h" #include "path.h"
#include "config/config_helpers.h"
enum repo_mode { enum repo_mode {
STANDARD_REPOSITORY = 0, STANDARD_REPOSITORY = 0,
...@@ -370,8 +371,6 @@ void test_repo_init__extended_1(void) ...@@ -370,8 +371,6 @@ void test_repo_init__extended_1(void)
void test_repo_init__relative_gitdir(void) void test_repo_init__relative_gitdir(void)
{ {
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
git_config *cfg;
const char *worktree_path;
git_buf dot_git_content = GIT_BUF_INIT; git_buf dot_git_content = GIT_BUF_INIT;
opts.workdir_path = "../c_wd"; opts.workdir_path = "../c_wd";
...@@ -391,24 +390,19 @@ void test_repo_init__relative_gitdir(void) ...@@ -391,24 +390,19 @@ void test_repo_init__relative_gitdir(void)
/* Verify that the gitlink and worktree entries are relative */ /* Verify that the gitlink and worktree entries are relative */
/* Verify worktree */ /* Verify worktree */
cl_git_pass(git_repository_config(&cfg, _repo)); assert_config_entry_value(_repo, "core.worktree", "../c_wd/");
cl_git_pass(git_config_get_string(&worktree_path, cfg, "core.worktree"));
cl_assert_equal_s("../c_wd/", worktree_path);
/* Verify gitlink */ /* Verify gitlink */
cl_git_pass(git_futils_readbuffer(&dot_git_content, "root/b/c_wd/.git")); cl_git_pass(git_futils_readbuffer(&dot_git_content, "root/b/c_wd/.git"));
cl_assert_equal_s("gitdir: ../my_repository/", dot_git_content.ptr); cl_assert_equal_s("gitdir: ../my_repository/", dot_git_content.ptr);
git_buf_free(&dot_git_content); git_buf_free(&dot_git_content);
git_config_free(cfg);
cleanup_repository("root"); cleanup_repository("root");
} }
void test_repo_init__relative_gitdir_2(void) void test_repo_init__relative_gitdir_2(void)
{ {
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT; git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
git_config *cfg;
const char *worktree_path;
git_buf dot_git_content = GIT_BUF_INIT; git_buf dot_git_content = GIT_BUF_INIT;
git_buf full_path = GIT_BUF_INIT; git_buf full_path = GIT_BUF_INIT;
...@@ -433,16 +427,13 @@ void test_repo_init__relative_gitdir_2(void) ...@@ -433,16 +427,13 @@ void test_repo_init__relative_gitdir_2(void)
/* Verify that the gitlink and worktree entries are relative */ /* Verify that the gitlink and worktree entries are relative */
/* Verify worktree */ /* Verify worktree */
cl_git_pass(git_repository_config(&cfg, _repo)); assert_config_entry_value(_repo, "core.worktree", "../c_wd/");
cl_git_pass(git_config_get_string(&worktree_path, cfg, "core.worktree"));
cl_assert_equal_s("../c_wd/", worktree_path);
/* Verify gitlink */ /* Verify gitlink */
cl_git_pass(git_futils_readbuffer(&dot_git_content, "root/b/c_wd/.git")); cl_git_pass(git_futils_readbuffer(&dot_git_content, "root/b/c_wd/.git"));
cl_assert_equal_s("gitdir: ../my_repository/", dot_git_content.ptr); cl_assert_equal_s("gitdir: ../my_repository/", dot_git_content.ptr);
git_buf_free(&dot_git_content); git_buf_free(&dot_git_content);
git_config_free(cfg);
cleanup_repository("root"); cleanup_repository("root");
} }
......
...@@ -46,7 +46,7 @@ void test_repo_setters__setting_a_workdir_prettifies_its_path(void) ...@@ -46,7 +46,7 @@ void test_repo_setters__setting_a_workdir_prettifies_its_path(void)
void test_repo_setters__setting_a_workdir_creates_a_gitlink(void) void test_repo_setters__setting_a_workdir_creates_a_gitlink(void)
{ {
git_config *cfg; git_config *cfg;
const char *val; git_buf buf = GIT_BUF_INIT;
git_buf content = GIT_BUF_INIT; git_buf content = GIT_BUF_INIT;
cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", true)); cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", true));
...@@ -59,8 +59,10 @@ void test_repo_setters__setting_a_workdir_creates_a_gitlink(void) ...@@ -59,8 +59,10 @@ void test_repo_setters__setting_a_workdir_creates_a_gitlink(void)
git_buf_free(&content); git_buf_free(&content);
cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_repository_config(&cfg, repo));
cl_git_pass(git_config_get_string(&val, cfg, "core.worktree")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.worktree"));
cl_assert(git__suffixcmp(val, "new_workdir/") == 0); cl_assert(git__suffixcmp(git_buf_cstr(&buf), "new_workdir/") == 0);
git_buf_free(&buf);
git_config_free(cfg); git_config_free(cfg);
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "posix.h" #include "posix.h"
#include "path.h" #include "path.h"
#include "submodule_helpers.h" #include "submodule_helpers.h"
#include "config/config_helpers.h"
#include "fileops.h" #include "fileops.h"
static git_repository *g_repo = NULL; static git_repository *g_repo = NULL;
...@@ -13,26 +14,19 @@ void test_submodule_add__cleanup(void) ...@@ -13,26 +14,19 @@ void test_submodule_add__cleanup(void)
static void assert_submodule_url(const char* name, const char *url) static void assert_submodule_url(const char* name, const char *url)
{ {
git_config *cfg;
const char *s;
git_buf key = GIT_BUF_INIT; git_buf key = GIT_BUF_INIT;
cl_git_pass(git_repository_config(&cfg, g_repo));
cl_git_pass(git_buf_printf(&key, "submodule.%s.url", name)); cl_git_pass(git_buf_printf(&key, "submodule.%s.url", name));
cl_git_pass(git_config_get_string(&s, cfg, git_buf_cstr(&key))); assert_config_entry_value(g_repo, git_buf_cstr(&key), url);
cl_assert_equal_s(s, url);
git_config_free(cfg);
git_buf_free(&key); git_buf_free(&key);
} }
void test_submodule_add__url_absolute(void) void test_submodule_add__url_absolute(void)
{ {
git_submodule *sm; git_submodule *sm;
git_config *cfg;
git_repository *repo; git_repository *repo;
const char *worktree_path;
git_buf dot_git_content = GIT_BUF_INIT; git_buf dot_git_content = GIT_BUF_INIT;
g_repo = setup_fixture_submod2(); g_repo = setup_fixture_submod2();
...@@ -59,15 +53,12 @@ void test_submodule_add__url_absolute(void) ...@@ -59,15 +53,12 @@ void test_submodule_add__url_absolute(void)
cl_git_pass(git_repository_open(&repo, "submod2/" "sm_libgit2")); cl_git_pass(git_repository_open(&repo, "submod2/" "sm_libgit2"));
/* Verify worktree path is relative */ /* Verify worktree path is relative */
cl_git_pass(git_repository_config(&cfg, repo)); assert_config_entry_value(repo, "core.worktree", "../../../sm_libgit2/");
cl_git_pass(git_config_get_string(&worktree_path, cfg, "core.worktree"));
cl_assert_equal_s("../../../sm_libgit2/", worktree_path);
/* Verify gitdir path is relative */ /* Verify gitdir path is relative */
cl_git_pass(git_futils_readbuffer(&dot_git_content, "submod2/" "sm_libgit2" "/.git")); cl_git_pass(git_futils_readbuffer(&dot_git_content, "submod2/" "sm_libgit2" "/.git"));
cl_assert_equal_s("gitdir: ../.git/modules/sm_libgit2/", dot_git_content.ptr); cl_assert_equal_s("gitdir: ../.git/modules/sm_libgit2/", dot_git_content.ptr);
git_config_free(cfg);
git_repository_free(repo); git_repository_free(repo);
git_buf_free(&dot_git_content); git_buf_free(&dot_git_content);
......
...@@ -34,9 +34,9 @@ void test_submodule_init__absolute_url(void) ...@@ -34,9 +34,9 @@ void test_submodule_init__absolute_url(void)
/* init and verify that absolute path is written to .git/config */ /* init and verify that absolute path is written to .git/config */
cl_git_pass(git_submodule_init(sm, false)); cl_git_pass(git_submodule_init(sm, false));
cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
git_config_get_string(&config_url, cfg, "submodule.testrepo.url"); cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
cl_assert_equal_s(absolute_url.ptr, config_url); cl_assert_equal_s(absolute_url.ptr, config_url);
git_buf_free(&absolute_url); git_buf_free(&absolute_url);
...@@ -64,9 +64,9 @@ void test_submodule_init__relative_url(void) ...@@ -64,9 +64,9 @@ void test_submodule_init__relative_url(void)
/* init and verify that absolute path is written to .git/config */ /* init and verify that absolute path is written to .git/config */
cl_git_pass(git_submodule_init(sm, false)); cl_git_pass(git_submodule_init(sm, false));
cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
git_config_get_string(&config_url, cfg, "submodule.testrepo.url"); cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
cl_assert_equal_s(absolute_url.ptr, config_url); cl_assert_equal_s(absolute_url.ptr, config_url);
git_buf_free(&absolute_url); git_buf_free(&absolute_url);
...@@ -102,9 +102,9 @@ void test_submodule_init__relative_url_detached_head(void) ...@@ -102,9 +102,9 @@ void test_submodule_init__relative_url_detached_head(void)
/* init and verify that absolute path is written to .git/config */ /* init and verify that absolute path is written to .git/config */
cl_git_pass(git_submodule_init(sm, false)); cl_git_pass(git_submodule_init(sm, false));
cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
git_config_get_string(&config_url, cfg, "submodule.testrepo.url"); cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url"));
cl_assert_equal_s(absolute_url.ptr, config_url); cl_assert_equal_s(absolute_url.ptr, config_url);
git_buf_free(&absolute_url); git_buf_free(&absolute_url);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "posix.h" #include "posix.h"
#include "path.h" #include "path.h"
#include "submodule_helpers.h" #include "submodule_helpers.h"
#include "config/config_helpers.h"
static git_repository *g_repo = NULL; static git_repository *g_repo = NULL;
...@@ -51,7 +52,7 @@ void test_submodule_modify__init(void) ...@@ -51,7 +52,7 @@ void test_submodule_modify__init(void)
git_submodule_reload_all(g_repo, 1); git_submodule_reload_all(g_repo, 1);
/* confirm submodule data in config */ /* confirm submodule data in config */
cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url")); cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
cl_assert(git__suffixcmp(str, "/submod2_target") == 0); cl_assert(git__suffixcmp(str, "/submod2_target") == 0);
cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url")); cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
...@@ -72,20 +73,12 @@ static int sync_one_submodule( ...@@ -72,20 +73,12 @@ static int sync_one_submodule(
static void assert_submodule_url_is_synced( static void assert_submodule_url_is_synced(
git_submodule *sm, const char *parent_key, const char *child_key) git_submodule *sm, const char *parent_key, const char *child_key)
{ {
git_config *cfg;
const char *str;
git_repository *smrepo; git_repository *smrepo;
cl_git_pass(git_repository_config(&cfg, g_repo)); assert_config_entry_value(g_repo, parent_key, git_submodule_url(sm));
cl_git_pass(git_config_get_string(&str, cfg, parent_key));
cl_assert_equal_s(git_submodule_url(sm), str);
git_config_free(cfg);
cl_git_pass(git_submodule_open(&smrepo, sm)); cl_git_pass(git_submodule_open(&smrepo, sm));
cl_git_pass(git_repository_config(&cfg, smrepo)); assert_config_entry_value(smrepo, child_key, git_submodule_url(sm));
cl_git_pass(git_config_get_string(&str, cfg, child_key));
cl_assert_equal_s(git_submodule_url(sm), str);
git_config_free(cfg);
git_repository_free(smrepo); git_repository_free(smrepo);
} }
...@@ -111,7 +104,7 @@ void test_submodule_modify__sync(void) ...@@ -111,7 +104,7 @@ void test_submodule_modify__sync(void)
*/ */
/* check submodule info does not match before sync */ /* check submodule info does not match before sync */
cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url")); cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url"));
cl_assert(strcmp(git_submodule_url(sm1), str) != 0); cl_assert(strcmp(git_submodule_url(sm1), str) != 0);
cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url")); cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url"));
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "posix.h" #include "posix.h"
#include "path.h" #include "path.h"
#include "submodule_helpers.h" #include "submodule_helpers.h"
#include "config/config_helpers.h"
#include "fileops.h" #include "fileops.h"
static git_repository *g_repo = NULL; static git_repository *g_repo = NULL;
...@@ -10,8 +11,6 @@ void test_submodule_repository_init__basic(void) ...@@ -10,8 +11,6 @@ void test_submodule_repository_init__basic(void)
{ {
git_submodule *sm; git_submodule *sm;
git_repository *repo; git_repository *repo;
git_config *cfg;
const char *worktree_path;
git_buf dot_git_content = GIT_BUF_INIT; git_buf dot_git_content = GIT_BUF_INIT;
g_repo = setup_fixture_submod2(); g_repo = setup_fixture_submod2();
...@@ -21,9 +20,7 @@ void test_submodule_repository_init__basic(void) ...@@ -21,9 +20,7 @@ void test_submodule_repository_init__basic(void)
cl_git_pass(git_submodule_repo_init(&repo, sm, 1)); cl_git_pass(git_submodule_repo_init(&repo, sm, 1));
/* Verify worktree */ /* Verify worktree */
cl_git_pass(git_repository_config(&cfg, repo)); assert_config_entry_value(repo, "core.worktree", "../../../sm_gitmodules_only/");
cl_git_pass(git_config_get_string(&worktree_path, cfg, "core.worktree"));
cl_assert_equal_s("../../../sm_gitmodules_only/", worktree_path);
/* Verify gitlink */ /* Verify gitlink */
cl_git_pass(git_futils_readbuffer(&dot_git_content, "submod2/" "sm_gitmodules_only" "/.git")); cl_git_pass(git_futils_readbuffer(&dot_git_content, "submod2/" "sm_gitmodules_only" "/.git"));
...@@ -35,7 +32,6 @@ void test_submodule_repository_init__basic(void) ...@@ -35,7 +32,6 @@ void test_submodule_repository_init__basic(void)
cl_assert(git_path_isdir("submod2/.git/modules/" "sm_gitmodules_only")); cl_assert(git_path_isdir("submod2/.git/modules/" "sm_gitmodules_only"));
cl_assert(git_path_isfile("submod2/.git/modules/" "sm_gitmodules_only" "/HEAD")); cl_assert(git_path_isfile("submod2/.git/modules/" "sm_gitmodules_only" "/HEAD"));
git_config_free(cfg);
git_submodule_free(sm); git_submodule_free(sm);
git_repository_free(repo); git_repository_free(repo);
git_buf_free(&dot_git_content); git_buf_free(&dot_git_content);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment