Skip to content

Commit

Permalink
Added basic Repository.branches implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmr232 committed Apr 12, 2017
1 parent 6226cc4 commit 5541078
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions pygit2/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from string import hexdigits
import sys, tarfile
from time import time

if sys.version_info[0] < 3:
from cStringIO import StringIO
else:
Expand All @@ -57,7 +58,6 @@


class BaseRepository(_Repository):

def __init__(self, backend, *args, **kwargs):
super(BaseRepository, self).__init__(backend, *args, **kwargs)
self._common_init()
Expand Down Expand Up @@ -484,6 +484,7 @@ def index(self):
@staticmethod
def _merge_options(favor):
"""Return a 'git_merge_opts *'"""

def favor_to_enum(favor):
if favor == 'normal':
return C.GIT_MERGE_FILE_FAVOR_NORMAL
Expand Down Expand Up @@ -530,13 +531,13 @@ def merge_file_from_index(self, ancestor, ours, theirs):
theirs._to_c() if theirs is not None else (ffi.NULL, ffi.NULL))

err = C.git_merge_file_from_index(
cmergeresult, self._repo,
cancestor, cours, ctheirs,
ffi.NULL);
cmergeresult, self._repo,
cancestor, cours, ctheirs,
ffi.NULL);
check_error(err)

ret = ffi.string(cmergeresult.ptr,
cmergeresult.len).decode('utf-8')
cmergeresult.len).decode('utf-8')
C.git_merge_file_result_free(cmergeresult)

return ret
Expand Down Expand Up @@ -735,11 +736,12 @@ def describe(self, committish=None, max_candidates_tags=None,
C.git_buf_free(buf)
finally:
C.git_describe_result_free(result[0])

#
# Stash
#
def stash(self, stasher, message=None, keep_index=False,
include_untracked=False, include_ignored=False):
include_untracked=False, include_ignored=False):
"""Save changes to the working directory to the stash.
:param Signature stasher: The identity of the person doing the stashing.
Expand Down Expand Up @@ -817,7 +819,6 @@ def stash_drop(self, index=0):
"""
check_error(C.git_stash_drop(self._repo, index))


def stash_pop(self, index=0, **kwargs):
"""Apply a stashed state and remove it from the stash list.
Expand Down Expand Up @@ -886,11 +887,11 @@ def write_archive(self, treeish, archive, timestamp=None, prefix=''):
info = tarfile.TarInfo(prefix + entry.path)
info.size = len(content)
info.mtime = timestamp
info.uname = info.gname = 'root' # just because git does this
info.uname = info.gname = 'root' # just because git does this
if entry.mode == GIT_FILEMODE_LINK:
info.type = tarfile.SYMTYPE
info.linkname = content.decode("utf-8")
info.mode = 0o777 # symlinks get placeholder
info.mode = 0o777 # symlinks get placeholder
info.size = 0
archive.addfile(info)
else:
Expand Down Expand Up @@ -996,6 +997,34 @@ def set_ident(self, name, email):
check_error(err)


class Branches(object):
def __init__(self, repository):
self._repository = repository

def __getitem__(self, key):
return self._repository.lookup_branch(key)
# This is a bit awkward, as it can raise either ValueError (on empty string), KeyError (name not found) or
# type error (invalid type)

def get(self, key):
try:
return self[key]
except KeyError:
# Maybe catch all exception types? I am not sure what is the expected behaviour here.
return None

def __iter__(self):
for branch_name in self._repository.listall_branches():
yield self._repository.lookup_branch(branch_name)

def create(self, name, commit, force=False):
self._repository.create_branch(name, commit, force)

def delete(self, name, force=False):
# `git_branch_delete` is not currently exposed by pygit2.
raise NotImplementedError()


class Repository(BaseRepository):
def __init__(self, path, *args, **kwargs):
if not isinstance(path, six.string_types):
Expand All @@ -1004,6 +1033,8 @@ def __init__(self, path, *args, **kwargs):
path_backend = init_file_backend(path)
super(Repository, self).__init__(backend=path_backend, *args, **kwargs)

self.branches = Branches(self)

@classmethod
def _from_c(cls, ptr, owned):
cptr = ffi.new('git_repository **')
Expand Down

0 comments on commit 5541078

Please sign in to comment.