# Path: vcs/backends/base.py
# class BaseWorkdir(object):
#     """
#     Working directory representation of single repository.
# 
#     :attribute: repository: repository object of working directory
#     """
# 
#     def __init__(self, repository):
#         self.repository = repository
# 
#     def get_branch(self):
#         """
#         Returns name of current branch.
#         """
#         raise NotImplementedError
# 
#     def get_changeset(self):
#         """
#         Returns current changeset.
#         """
#         raise NotImplementedError
# 
#     def get_added(self):
#         """
#         Returns list of ``FileNode`` objects marked as *new* in working
#         directory.
#         """
#         raise NotImplementedError
# 
#     def get_changed(self):
#         """
#         Returns list of ``FileNode`` objects *changed* in working directory.
#         """
#         raise NotImplementedError
# 
#     def get_removed(self):
#         """
#         Returns list of ``RemovedFileNode`` objects marked as *removed* in
#         working directory.
#         """
#         raise NotImplementedError
# 
#     def get_untracked(self):
#         """
#         Returns list of ``FileNode`` objects which are present within working
#         directory however are not tracked by repository.
#         """
#         raise NotImplementedError
# 
#     def get_status(self):
#         """
#         Returns dict with ``added``, ``changed``, ``removed`` and ``untracked``
#         lists.
#         """
#         raise NotImplementedError
# 
#     def commit(self, message, **kwargs):
#         """
#         Commits local (from working directory) changes and returns newly
#         created
#         ``Changeset``. Updates repository's ``revisions`` list.
# 
#         :raises ``CommitError``: if any error occurs while committing
#         """
#         raise NotImplementedError
# 
#     def update(self, revision=None):
#         """
#         Fetches content of the given revision and populates it within working
#         directory.
#         """
#         raise NotImplementedError
# 
#     def checkout_branch(self, branch=None):
#         """
#         Checks out ``branch`` or the backend's default branch.
# 
#         Raises ``BranchDoesNotExistError`` if the branch does not exist.
#         """
#         raise NotImplementedError
# 
# Path: vcs/exceptions.py
# class RepositoryError(VCSError):
#     pass
# 
# Path: vcs/exceptions.py
# class BranchDoesNotExistError(RepositoryError):
#     pass


import re
from vcs.backends.base import BaseWorkdir
from vcs.exceptions import RepositoryError
from vcs.exceptions import BranchDoesNotExistError


class GitWorkdir(BaseWorkdir):

    def get_branch(self):
        headpath = self.repository._repo.refs.refpath('HEAD')
        try:
            content = open(headpath).read()
            match = re.match(r'^ref: refs/heads/(?P<branch>.+)\n$', content)
            if match:
                return match.groupdict()['branch']
            else:
                raise RepositoryError("Couldn't compute workdir's branch")
        except IOError:
            # Try naive way...

# Path: vcs/backends/base.py
# class BaseWorkdir(object):
#     """
#     Working directory representation of single repository.
# 
#     :attribute: repository: repository object of working directory
#     """
# 
#     def __init__(self, repository):
#         self.repository = repository
# 
#     def get_branch(self):
#         """
#         Returns name of current branch.
#         """
#         raise NotImplementedError
# 
#     def get_changeset(self):
#         """
#         Returns current changeset.
#         """
#         raise NotImplementedError
# 
#     def get_added(self):
#         """
#         Returns list of ``FileNode`` objects marked as *new* in working
#         directory.
#         """
#         raise NotImplementedError
# 
#     def get_changed(self):
#         """
#         Returns list of ``FileNode`` objects *changed* in working directory.
#         """
#         raise NotImplementedError
# 
#     def get_removed(self):
#         """
#         Returns list of ``RemovedFileNode`` objects marked as *removed* in
#         working directory.
#         """
#         raise NotImplementedError
# 
#     def get_untracked(self):
#         """
#         Returns list of ``FileNode`` objects which are present within working
#         directory however are not tracked by repository.
#         """
#         raise NotImplementedError
# 
#     def get_status(self):
#         """
#         Returns dict with ``added``, ``changed``, ``removed`` and ``untracked``
#         lists.
#         """
#         raise NotImplementedError
# 
#     def commit(self, message, **kwargs):
#         """
#         Commits local (from working directory) changes and returns newly
#         created
#         ``Changeset``. Updates repository's ``revisions`` list.
# 
#         :raises ``CommitError``: if any error occurs while committing
#         """
#         raise NotImplementedError
# 
#     def update(self, revision=None):
#         """
#         Fetches content of the given revision and populates it within working
#         directory.
#         """
#         raise NotImplementedError
# 
#     def checkout_branch(self, branch=None):
#         """
#         Checks out ``branch`` or the backend's default branch.
# 
#         Raises ``BranchDoesNotExistError`` if the branch does not exist.
#         """
#         raise NotImplementedError
# 
# Path: vcs/exceptions.py
# class RepositoryError(VCSError):
#     pass
# 
# Path: vcs/exceptions.py
# class BranchDoesNotExistError(RepositoryError):
#     pass


# Path: vcs/backends/git/workdir.py
import re
from vcs.backends.base import BaseWorkdir
from vcs.exceptions import RepositoryError
from vcs.exceptions import BranchDoesNotExistError



class GitWorkdir(BaseWorkdir):

    def get_branch(self):
        headpath = self.repository._repo.refs.refpath('HEAD')
        try:
            content = open(headpath).read()
            match = re.match(r'^ref: refs/heads/(?P<branch>.+)\n$', content)
            if match:
                return match.groupdict()['branch']
            else:
                raise RepositoryError("Couldn't compute workdir's branch")
        except IOError:
            # Try naive way...

            raise RepositoryError("Couldn't compute workdir's branch")