Skip to content
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

Cleanup a little bit the code #392

Merged
merged 1 commit into from
Jul 15, 2014
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
14 changes: 10 additions & 4 deletions pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from .errors import check_error
from .ffi import ffi, C, to_str


def init_repository(path, bare=False,
flags=C.GIT_REPOSITORY_INIT_MKPATH,
mode=0,
Expand Down Expand Up @@ -98,7 +99,9 @@ def init_repository(path, bare=False,
return Repository(path)


@ffi.callback('int (*credentials)(git_cred **cred, const char *url, const char *username_from_url, unsigned int allowed_types, void *data)')
@ffi.callback('int (*credentials)(git_cred **cred, const char *url,'
'const char *username_from_url, unsigned int allowed_types,'
'void *data)')
def _credentials_cb(cred_out, url, username_from_url, allowed, data):
d = ffi.from_handle(data)

Expand All @@ -111,6 +114,7 @@ def _credentials_cb(cred_out, url, username_from_url, allowed, data):

return 0


def clone_repository(
url, path, bare=False, ignore_cert_errors=False,
remote_name="origin", checkout_branch=None, credentials=None):
Expand Down Expand Up @@ -174,6 +178,7 @@ def clone_repository(

return Repository(path)


def clone_into(repo, remote, branch=None):
"""Clone into an empty repository from the specified remote

Expand All @@ -186,11 +191,12 @@ def clone_into(repo, remote, branch=None):

This allows you specify arbitrary repository and remote configurations
before performing the clone step itself. E.g. you can replicate git-clone's
'--mirror' option by setting a refspec of '+refs/*:refs/*', 'core.mirror' to true
and calling this function.
'--mirror' option by setting a refspec of '+refs/*:refs/*', 'core.mirror'
to true and calling this function.
"""

err = C.git_clone_into(repo._repo, remote._remote, ffi.NULL, to_str(branch), ffi.NULL)
err = C.git_clone_into(repo._repo, remote._remote, ffi.NULL,
to_str(branch), ffi.NULL)

if remote._stored_exception:
raise remote._stored_exception
Expand Down
43 changes: 25 additions & 18 deletions pygit2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
# Import from the future
from __future__ import absolute_import, unicode_literals

from _pygit2 import Oid

from .ffi import ffi, C, to_str, is_string
from .errors import check_error, GitError
from .refspec import Refspec
from .errors import check_error


def assert_string(v, desc):
if not is_string(v):
raise TypeError("%s must be a string" % desc)


class ConfigIterator(object):

def __init__(self, config, ptr):
Expand Down Expand Up @@ -67,12 +66,14 @@ def __next__(self):

return name, value


class ConfigMultivarIterator(ConfigIterator):
def __next__(self):
entry = self._next_entry()

return ffi.string(entry.value).decode('utf-8')


class Config(object):
"""Git configuration management"""

Expand Down Expand Up @@ -140,7 +141,8 @@ def __setitem__(self, key, value):
elif isinstance(value, int):
err = C.git_config_set_int64(self._config, to_str(key), value)
else:
err = C.git_config_set_string(self._config, to_str(key), to_str(value))
err = C.git_config_set_string(self._config, to_str(key),
to_str(value))

check_error(err)

Expand All @@ -161,34 +163,37 @@ def get_multivar(self, name, regex=None):
"""get_multivar(name[, regex]) -> [str, ...]

Get each value of a multivar ''name'' as a list. The optional ''regex''
parameter is expected to be a regular expression to filter the variables
we're interested in."""
parameter is expected to be a regular expression to filter the
variables we're interested in."""

assert_string(name, "name")

citer = ffi.new('git_config_iterator **')
err = C.git_config_multivar_iterator_new(citer, self._config, to_str(name), to_str(regex))
err = C.git_config_multivar_iterator_new(citer, self._config,
to_str(name), to_str(regex))
check_error(err)

return ConfigMultivarIterator(self, citer[0])

def set_multivar(self, name, regex, value):
"""set_multivar(name, regex, value)

Set a multivar ''name'' to ''value''. ''regexp'' is a regular expression
to indicate which values to replace"""
Set a multivar ''name'' to ''value''. ''regexp'' is a regular
expression to indicate which values to replace"""

assert_string(name, "name")
assert_string(regex, "regex")
assert_string(value, "value")

err = C.git_config_set_multivar(self._config, to_str(name), to_str(regex), to_str(value))
err = C.git_config_set_multivar(self._config, to_str(name),
to_str(regex), to_str(value))
check_error(err)

def get_bool(self, key):
"""get_bool(key) -> Bool

Look up *key* and parse its value as a boolean as per the git-config rules
Look up *key* and parse its value as a boolean as per the git-config
rules

Truthy values are: 'true', 1, 'on' or 'yes'. Falsy values are: 'false',
0, 'off' and 'no'"""
Expand All @@ -203,10 +208,11 @@ def get_bool(self, key):
def get_int(self, key):
"""get_int(key) -> int

Look up *key* and parse its value as an integer as per the git-config rules.
Look up *key* and parse its value as an integer as per the git-config
rules.

A value can have a suffix 'k', 'm' or 'g' which stand for 'kilo', 'mega' and
'giga' respectively"""
A value can have a suffix 'k', 'm' or 'g' which stand for 'kilo',
'mega' and 'giga' respectively"""

val = self._get_string(key)
res = ffi.new('int64_t *')
Expand All @@ -220,7 +226,8 @@ def add_file(self, path, level=0, force=0):

Add a config file instance to an existing config."""

err = C.git_config_add_file_ondisk(self._config, to_str(path), level, force)
err = C.git_config_add_file_ondisk(self._config, to_str(path), level,
force)
check_error(err)

def snapshot(self):
Expand All @@ -231,7 +238,7 @@ def snapshot(self):
"""

ccfg = ffi.new('git_config **')
err = C.git_config_snapshot(cfg, self._config)
err = C.git_config_snapshot(ccfg, self._config)
check_error(err)

return Config.from_c(self._repo, ccfg[0])
Expand All @@ -243,7 +250,7 @@ def snapshot(self):
@staticmethod
def parse_bool(text):
res = ffi.new('int *')
err = C.git_config_parse_bool(res, to_str(text))
C.git_config_parse_bool(res, to_str(text))

return res[0] != 0

Expand Down
4 changes: 3 additions & 1 deletion pygit2/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

from .ffi import ffi, C
from .ffi import C

GIT_CREDTYPE_USERPASS_PLAINTEXT = C.GIT_CREDTYPE_USERPASS_PLAINTEXT
GIT_CREDTYPE_SSH_KEY = C.GIT_CREDTYPE_SSH_KEY


class UserPass(object):
"""Username/Password credentials

Expand All @@ -53,6 +54,7 @@ def credential_tuple(self):
def __call__(self, _url, _username, _allowed):
return self


class Keypair(object):
"""SSH key pair credentials

Expand Down
8 changes: 3 additions & 5 deletions pygit2/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

# Import from the Standard Library
from string import hexdigits

# ffi
from .ffi import ffi, C

from _pygit2 import GitError


def check_error(err, io=False):
if err >= 0:
return
Expand All @@ -42,7 +40,8 @@ def check_error(err, io=False):
if giterr != ffi.NULL:
message = ffi.string(giterr.message).decode()

if err in [C.GIT_EEXISTS, C.GIT_EINVALIDSPEC, C.GIT_EEXISTS, C.GIT_EAMBIGUOUS]:
if err in [C.GIT_EEXISTS, C.GIT_EINVALIDSPEC, C.GIT_EEXISTS,
C.GIT_EAMBIGUOUS]:
raise ValueError(message)
elif err == C.GIT_ENOTFOUND:
if io:
Expand All @@ -55,4 +54,3 @@ def check_error(err, io=False):
raise StopIteration()

raise GitError(message)

2 changes: 2 additions & 0 deletions pygit2/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ def is_string(s):

ffi = FFI()


def strarray_to_strings(arr):
l = [None] * arr.count
for i in range(arr.count):
l[i] = ffi.string(arr.strings[i]).decode()

return l


def strings_to_strarray(l):
"""Convert a list of strings to a git_strarray

Expand Down
Loading