Commit ee12272d170d6a9d60f13d6de6129f56bfb2fbf6

Vicent Marti 2013-04-05T22:48:39

Global option setters

diff --git a/include/git2/common.h b/include/git2/common.h
index b8c3e42..80d83d3 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -131,6 +131,8 @@ enum {
 	GIT_OPT_SET_MWINDOW_MAPPED_LIMIT,
 	GIT_OPT_GET_SEARCH_PATH,
 	GIT_OPT_SET_SEARCH_PATH,
+	GIT_OPT_SET_CACHE_LIMIT,
+	GIT_OPT_ENABLE_CACHING
 };
 
 /**
diff --git a/src/cache.c b/src/cache.c
index 9f3290f..f8cddea 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -17,6 +17,8 @@
 
 GIT__USE_OIDMAP
 
+bool git_cache__enabled = true;
+
 size_t git_cache__max_object_size[8] = {
 	0, /* GIT_OBJ__EXT1 */
 	4096,  /* GIT_OBJ_COMMIT */
@@ -109,11 +111,7 @@ static void cache_evict_entries(git_cache *cache, size_t evict_count)
 static bool cache_should_store(git_otype object_type, size_t object_size)
 {
 	size_t max_size = git_cache__max_object_size[object_type];
-
-	if (max_size == 0 || object_size > max_size)
-		return false;
-
-	return true;
+	return git_cache__enabled && object_size < max_size;
 }
 
 static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
@@ -121,7 +119,7 @@ static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
 	khiter_t pos;
 	git_cached_obj *entry = NULL;
 
-	if (git_mutex_lock(&cache->lock) < 0)
+	if (!git_cache__enabled || git_mutex_lock(&cache->lock) < 0)
 		return NULL;
 
 	pos = kh_get(oid, cache->map, oid);
diff --git a/src/cache.h b/src/cache.h
index 65a6e57..8b2aa1f 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -20,6 +20,9 @@ enum {
 	GIT_CACHE_STORE_PARSED = 2
 };
 
+extern bool git_cache__enabled;
+extern size_t git_cache__max_object_size[8];
+
 typedef struct {
 	git_oid oid;
 	int16_t type;
diff --git a/src/util.c b/src/util.c
index c4a8c78..0b5fbdc 100644
--- a/src/util.c
+++ b/src/util.c
@@ -11,6 +11,7 @@
 #include <ctype.h>
 #include "posix.h"
 #include "fileops.h"
+#include "cache.h"
 
 #ifdef _MSC_VER
 # include <Shlwapi.h>
@@ -93,6 +94,16 @@ int git_libgit2_opts(int key, ...)
 		if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0)
 			error = git_futils_dirs_set(error, va_arg(ap, const char *));
 		break;
+
+	case GIT_OPT_SET_CACHE_LIMIT: {
+			git_otype type = (git_otype)va_arg(ap, int);
+			git_cache__max_object_size[type] = va_arg(ap, size_t);
+			break;
+		}
+
+	case GIT_OPT_ENABLE_CACHING:
+		git_cache__enabled = va_arg(ap, int);
+		break;
 	}
 
 	va_end(ap);