workdir.py
32 lines
| 1.2 KiB
| text/x-python
|
PythonLexer
|
r4187 | import re | ||
|
r7718 | |||
|
r7960 | from kallithea.lib.utils2 import ascii_str | ||
|
r4187 | from kallithea.lib.vcs.backends.base import BaseWorkdir | ||
|
r7718 | from kallithea.lib.vcs.exceptions import BranchDoesNotExistError, RepositoryError | ||
|
r4187 | |||
class GitWorkdir(BaseWorkdir): | ||||
def get_branch(self): | ||||
|
r7958 | headpath = self.repository._repo.refs.refpath(b'HEAD') | ||
|
r4187 | 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") | ||||
def get_changeset(self): | ||||
|
r7960 | wk_dir_id = ascii_str(self.repository._repo.refs.as_dict().get(b'HEAD')) | ||
|
r4187 | return self.repository.get_changeset(wk_dir_id) | ||
def checkout_branch(self, branch=None): | ||||
if branch is None: | ||||
branch = self.repository.DEFAULT_BRANCH_NAME | ||||
if branch not in self.repository.branches: | ||||
raise BranchDoesNotExistError | ||||
self.repository.run_git_command(['checkout', branch]) | ||||