Commit eac76c230c92594eaa528e50746119bd3c33ffbb

Russell Belfer 2013-04-22T14:27:36

Use config cache where possible This converts many of the config lookups that are done around the library to use the repository config cache. This was everything I could find that wasn't part of diff (which requires a larger fix).

diff --git a/src/checkout.c b/src/checkout.c
index 62a73d6..e29fccd 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1119,7 +1119,6 @@ static int checkout_data_init(
 	git_checkout_opts *proposed)
 {
 	int error = 0;
-	git_config *cfg;
 	git_repository *repo = git_iterator_owner(target);
 
 	memset(data, 0, sizeof(*data));
@@ -1132,9 +1131,6 @@ static int checkout_data_init(
 	if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
 		return error;
 
-	if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
-		return error;
-
 	data->repo = repo;
 
 	GITERR_CHECK_VERSION(
@@ -1147,7 +1143,10 @@ static int checkout_data_init(
 
 	/* refresh config and index content unless NO_REFRESH is given */
 	if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) {
-		if ((error = git_config_refresh(cfg)) < 0)
+		git_config *cfg;
+
+		if ((error = git_repository_config__weakptr(&cfg, repo)) < 0 ||
+			(error = git_config_refresh(cfg)) < 0)
 			goto cleanup;
 
 		/* if we are checking out the index, don't reload,
@@ -1184,19 +1183,13 @@ static int checkout_data_init(
 
 	data->pfx = git_pathspec_prefix(&data->opts.paths);
 
-	error = git_config_get_bool(&data->can_symlink, cfg, "core.symlinks");
-	if (error < 0) {
-		if (error != GIT_ENOTFOUND)
-			goto cleanup;
-
-		/* If "core.symlinks" is not found anywhere, default to true. */
-		data->can_symlink = true;
-		giterr_clear();
-		error = 0;
-	}
+	if ((error = git_repository__cvar(
+			 &data->can_symlink, repo, GIT_CVAR_SYMLINKS)) < 0)
+		goto cleanup;
 
 	if (!data->opts.baseline) {
 		data->opts_free_baseline = true;
+
 		error = checkout_lookup_head_tree(&data->opts.baseline, repo);
 
 		if (error == GIT_EORPHANEDHEAD) {
diff --git a/src/ignore.c b/src/ignore.c
index 1777952..e150b95 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -15,24 +15,14 @@ static int parse_ignore_file(
 	git_attr_fnmatch *match = NULL;
 	const char *scan = NULL;
 	char *context = NULL;
-	bool ignore_case = false;
-	git_config *cfg = NULL;
-	int val;
-
-	/* Prefer to have the caller pass in a git_ignores as the parsedata object.
-	 * If they did not, then we can (much more slowly) find the value of
-	 * ignore_case by using the repository object. */
-	if (parsedata != NULL) {
-		ignore_case = ((git_ignores *)parsedata)->ignore_case;
-	} else {
-		if ((error = git_repository_config(&cfg, repo)) < 0)
-			return error;
-
-		if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0)
-			ignore_case = (val != 0);
+	int ignore_case = false;
 
-		git_config_free(cfg);
-	}
+	/* Prefer to have the caller pass in a git_ignores as the parsedata
+	 * object.  If they did not, then look up the value of ignore_case */
+	if (parsedata != NULL)
+		ignore_case = ((git_ignores *)parsedata)->ignore_case;
+	else if (git_repository__cvar(&ignore_case, repo, GIT_CVAR_IGNORECASE) < 0)
+		return error;
 
 	if (ignores->key && git__suffixcmp(ignores->key, "/" GIT_IGNORE_FILE) == 0) {
 		context = ignores->key + 2;
@@ -109,8 +99,6 @@ int git_ignore__for_path(
 {
 	int error = 0;
 	const char *workdir = git_repository_workdir(repo);
-	git_config *cfg = NULL;
-	int val;
 
 	assert(ignores);
 
@@ -118,17 +106,11 @@ int git_ignore__for_path(
 	git_buf_init(&ignores->dir, 0);
 	ignores->ign_internal = NULL;
 
-	/* Set the ignore_case flag appropriately */
-	if ((error = git_repository_config(&cfg, repo)) < 0)
+	/* Read the ignore_case flag */
+	if ((error = git_repository__cvar(
+			&ignores->ignore_case, repo, GIT_CVAR_IGNORECASE)) < 0)
 		goto cleanup;
 
-	if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0)
-		ignores->ignore_case = (val != 0);
-	else
-		ignores->ignore_case = 0;
-
-	git_config_free(cfg);
-
 	if ((error = git_vector_init(&ignores->ign_path, 8, NULL)) < 0 ||
 		(error = git_vector_init(&ignores->ign_global, 2, NULL)) < 0 ||
 		(error = git_attr_cache__init(repo)) < 0)
diff --git a/src/ignore.h b/src/ignore.h
index 5af8e8e..e00e4a8 100644
--- a/src/ignore.h
+++ b/src/ignore.h
@@ -28,7 +28,7 @@ typedef struct {
 	git_attr_file *ign_internal;
 	git_vector ign_path;
 	git_vector ign_global;
-	unsigned int ignore_case:1;
+	int ignore_case;
 } git_ignores;
 
 extern int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ign);
diff --git a/src/index.c b/src/index.c
index 2afd281..d8ca78e 100644
--- a/src/index.c
+++ b/src/index.c
@@ -330,7 +330,7 @@ void git_index_clear(git_index *index)
 	git_vector_clear(&index->entries);
 
 	git_index_reuc_clear(index);
-	
+
 	git_futils_filestamp_set(&index->stamp, NULL);
 
 	git_tree_cache_free(index->tree);
@@ -352,19 +352,18 @@ int git_index_set_caps(git_index *index, unsigned int caps)
 	old_ignore_case = index->ignore_case;
 
 	if (caps == GIT_INDEXCAP_FROM_OWNER) {
-		git_config *cfg;
+		git_repository *repo = INDEX_OWNER(index);
 		int val;
 
-		if (INDEX_OWNER(index) == NULL ||
-			git_repository_config__weakptr(&cfg, INDEX_OWNER(index)) < 0)
-				return create_index_error(-1,
-					"Cannot get repository config to set index caps");
+		if (!repo)
+			return create_index_error(
+				-1, "Cannot access repository to set index caps");
 
-		if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0)
+		if (!git_repository__cvar(&val, repo, GIT_CVAR_IGNORECASE))
 			index->ignore_case = (val != 0);
-		if (git_config_get_bool(&val, cfg, "core.filemode") == 0)
+		if (!git_repository__cvar(&val, repo, GIT_CVAR_FILEMODE))
 			index->distrust_filemode = (val == 0);
-		if (git_config_get_bool(&val, cfg, "core.symlinks") == 0)
+		if (!git_repository__cvar(&val, repo, GIT_CVAR_SYMLINKS))
 			index->no_symlinks = (val == 0);
 	}
 	else {