Skip to content

More options in settings #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pygit2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
from _pygit2 import option
from _pygit2 import GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH
from _pygit2 import GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE
from _pygit2 import GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, GIT_OPT_SET_MWINDOW_MAPPED_LIMIT
from _pygit2 import GIT_OPT_SET_CACHE_OBJECT_LIMIT
from _pygit2 import GIT_OPT_GET_CACHED_MEMORY
from _pygit2 import GIT_OPT_ENABLE_CACHING
from _pygit2 import GIT_OPT_SET_CACHE_MAX_SIZE


class SearchPathList(object):
Expand Down Expand Up @@ -64,3 +69,36 @@ def mwindow_size(self):
@mwindow_size.setter
def mwindow_size(self, value):
option(GIT_OPT_SET_MWINDOW_SIZE, value)

@property
def mwindow_mapped_limit(self):
"""Mwindow mapped limit"""
return option(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT)

@mwindow_mapped_limit.setter
def mwindow_mapped_limit(self, value):
"""Mwindow mapped limit"""
return option(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, value)

@property
def cached_memory(self):
"""Maximum mmap window size"""
return option(GIT_OPT_GET_CACHED_MEMORY)

def enable_caching(self, value=True):
return option(GIT_OPT_ENABLE_CACHING, value)

def cache_max_size(self, value):
return option(GIT_OPT_SET_CACHE_MAX_SIZE, value)

def cache_object_limit(self, object_type, value):
"""Set the maximum data size for the given type of object to be
considered eligible for caching in memory.

Setting to value to zero means that that type of object will not
be cached. Defaults to 0 for GIT_OBJ_BLOB (i.e. won't cache
blobs) and 4k for GIT_OBJ_COMMIT, GIT_OBJ_TREE, and GIT_OBJ_TAG.
"""
return option(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, value)


132 changes: 132 additions & 0 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,138 @@ option(PyObject *self, PyObject *args)
Py_RETURN_NONE;
break;
}

case GIT_OPT_GET_MWINDOW_MAPPED_LIMIT:
{
size_t limit;

error = git_libgit2_opts(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, &limit);
if (error < 0) {
Error_set(error);
return NULL;
}

return PyLong_FromSize_t(limit);

break;
}

case GIT_OPT_SET_MWINDOW_MAPPED_LIMIT:
{
size_t limit;
PyObject *py_limit;

py_limit = PyTuple_GetItem(args, 1);
if (!py_limit)
return NULL;

if (!PyLong_Check(py_limit))
goto on_non_integer;

limit = PyLong_AsSize_t(py_limit);
error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, limit);
if (error < 0) {
Error_set(error);
return NULL;
}

Py_RETURN_NONE;
break;
}

case GIT_OPT_SET_CACHE_OBJECT_LIMIT:
{
size_t limit;
int object_type;
PyObject *py_object_type, *py_limit;

py_object_type = PyTuple_GetItem(args, 1);
if (!py_object_type)
return NULL;

py_limit = PyTuple_GetItem(args, 2);
if (!py_limit)
return NULL;

if (!PyLong_Check(py_limit))
goto on_non_integer;

object_type = PyLong_AsLong(py_object_type);
limit = PyLong_AsSize_t(py_limit);
error = git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, limit);

if (error < 0) {
Error_set(error);
return NULL;
}

Py_RETURN_NONE;
break;
}

case GIT_OPT_SET_CACHE_MAX_SIZE:
{
size_t max_size;
PyObject *py_max_size;

py_max_size = PyTuple_GetItem(args, 1);
if (!py_max_size)
return NULL;

if (!PyLong_Check(py_max_size))
goto on_non_integer;

max_size = PyLong_AsSize_t(py_max_size);
error = git_libgit2_opts(GIT_OPT_SET_CACHE_MAX_SIZE, max_size);
if (error < 0) {
Error_set(error);
return NULL;
}

Py_RETURN_NONE;
break;
}

case GIT_OPT_ENABLE_CACHING:
{
int flag;
PyObject *py_flag;

py_flag = PyTuple_GetItem(args, 1);

if (!PyLong_Check(py_flag))
goto on_non_integer;

flag = PyLong_AsSize_t(py_flag);
error = git_libgit2_opts(GIT_OPT_ENABLE_CACHING, flag);
if (error < 0) {
Error_set(error);
return NULL;
}

Py_RETURN_NONE;
break;
}

case GIT_OPT_GET_CACHED_MEMORY:
{
size_t current;
size_t allowed;
PyObject* tup = PyTuple_New(2);

error = git_libgit2_opts(GIT_OPT_GET_CACHED_MEMORY, &current, &allowed);
if (error < 0) {
Error_set(error);
return NULL;
}
PyTuple_SetItem(tup, 0, PyLong_FromLong(current));
PyTuple_SetItem(tup, 1, PyLong_FromLong(allowed));

return tup;

break;
}

}

PyErr_SetString(PyExc_ValueError, "unknown/unsupported option value");
Expand Down
6 changes: 6 additions & 0 deletions src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ moduleinit(PyObject* m)
ADD_CONSTANT_INT(m, GIT_OPT_SET_SEARCH_PATH);
ADD_CONSTANT_INT(m, GIT_OPT_GET_MWINDOW_SIZE);
ADD_CONSTANT_INT(m, GIT_OPT_SET_MWINDOW_SIZE);
ADD_CONSTANT_INT(m, GIT_OPT_GET_MWINDOW_MAPPED_LIMIT);
ADD_CONSTANT_INT(m, GIT_OPT_SET_MWINDOW_MAPPED_LIMIT);
ADD_CONSTANT_INT(m, GIT_OPT_SET_CACHE_OBJECT_LIMIT);
ADD_CONSTANT_INT(m, GIT_OPT_GET_CACHED_MEMORY);
ADD_CONSTANT_INT(m, GIT_OPT_ENABLE_CACHING);
ADD_CONSTANT_INT(m, GIT_OPT_SET_CACHE_MAX_SIZE);

/* Errors */
GitError = PyErr_NewException("_pygit2.GitError", NULL, NULL);
Expand Down
45 changes: 45 additions & 0 deletions test/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@
from __future__ import unicode_literals
import unittest
import pygit2
from pygit2 import GIT_OBJ_BLOB
from pygit2 import GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE
from pygit2 import GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH
from pygit2 import GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, GIT_OPT_SET_MWINDOW_MAPPED_LIMIT
from pygit2 import GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_GLOBAL
from pygit2 import GIT_OPT_SET_CACHE_OBJECT_LIMIT
from pygit2 import GIT_OPT_GET_CACHED_MEMORY
from pygit2 import GIT_OPT_ENABLE_CACHING
from pygit2 import option
from . import utils

Expand All @@ -50,6 +55,46 @@ def test_mwindow_size_proxy(self):

self.assertEqual(new_size, pygit2.settings.mwindow_size)

def test_mwindow_mapped_limit(self):
new_limit = 200 * 1024
option(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, new_limit)
self.assertEqual(new_limit, option(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT))

def test_mwindow_mapped_limit(self):
new_limit = 300 * 1024
pygit2.settings.mwindow_mapped_limit = new_limit

self.assertEqual(new_limit, pygit2.settings.mwindow_mapped_limit)

def test_cache_object_limit(self):
new_limit = 2 * 1024
option(GIT_OPT_SET_CACHE_OBJECT_LIMIT, GIT_OBJ_BLOB, new_limit)

def test_cache_object_limit_proxy(self):
new_limit = 4 * 1024
pygit2.settings.cache_object_limit(GIT_OBJ_BLOB, new_limit)

def test_cached_memory(self):
value = option(GIT_OPT_GET_CACHED_MEMORY)
self.assertEqual(value[1], 256 * 1024**2)

def test_cached_memory_proxy(self):
self.assertEqual(pygit2.settings.cached_memory[1], 256 * 1024**2)

def test_enable_cache(self):
option(GIT_OPT_ENABLE_CACHING, False)
option(GIT_OPT_ENABLE_CACHING, True)

def test_enable_cache_proxy(self):
pygit2.settings.enable_caching(False)
pygit2.settings.enable_caching(True)

def test_cache_max_size_proxy(self):
pygit2.settings.cache_max_size(128 * 1024**2)
self.assertEqual(pygit2.settings.cached_memory[1], 128 * 1024**2)
pygit2.settings.cache_max_size(256 * 1024**2)
self.assertEqual(pygit2.settings.cached_memory[1], 256 * 1024**2)

def test_search_path(self):
paths = [(GIT_CONFIG_LEVEL_GLOBAL, '/tmp/global'),
(GIT_CONFIG_LEVEL_XDG, '/tmp/xdg'),
Expand Down