diff --git a/powerline/lib/vcs/git.py b/powerline/lib/vcs/git.py index 3bf60562..27a1e71a 100644 --- a/powerline/lib/vcs/git.py +++ b/powerline/lib/vcs/git.py @@ -33,6 +33,8 @@ def git_directory(directory): if not raw.startswith(b'gitdir: '): raise IOError('invalid gitfile format') raw = raw[8:].decode(sys.getfilesystemencoding() or 'utf-8') + if raw[-1] == '\n': + raw = raw[:-1] if not raw: raise IOError('no path in gitfile') return os.path.abspath(os.path.join(directory, raw)) @@ -78,9 +80,10 @@ class GitRepository(object): return self.do_status(self.directory, path) def branch(self): - head = os.path.join(git_directory(self.directory), 'HEAD') + directory = git_directory(self.directory) + head = os.path.join(directory, 'HEAD') return get_branch_name( - directory=self.directory, + directory=directory, config_file=head, get_func=branch_name_from_config_file, create_watcher=self.create_watcher, diff --git a/tests/test_lib.py b/tests/test_lib.py index 0bbae4e4..c69d97b6 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -7,6 +7,7 @@ from powerline.lib.vcs import guess, get_fallback_create_watcher from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment from powerline.lib.monotonic import monotonic from powerline.lib.file_watcher import create_file_watcher, INotifyError +from powerline.lib.vcs.git import git_directory from powerline import get_fallback_logger import threading import os @@ -508,18 +509,34 @@ class TestVCS(TestCase): os.remove(os.path.join(GIT_REPO, 'file')) # Test changing branch self.assertEqual(repo.branch(), 'master') - call(['git', 'branch', 'branch1'], cwd=GIT_REPO) - call(['git', 'checkout', '-q', 'branch1'], cwd=GIT_REPO) - self.do_branch_rename_test(repo, 'branch1') - # For some reason the rest of this test fails on travis and only on - # travis, and I can't figure out why - if 'TRAVIS' in os.environ: - raise SkipTest('Part of this test fails on Travis for unknown reasons') - call(['git', 'branch', 'branch2'], cwd=GIT_REPO) - call(['git', 'checkout', '-q', 'branch2'], cwd=GIT_REPO) - self.do_branch_rename_test(repo, 'branch2') - call(['git', 'checkout', '-q', '--detach', 'branch1'], cwd=GIT_REPO) - self.do_branch_rename_test(repo, lambda b: re.match(r'^[a-f0-9]+$', b)) + try: + call(['git', 'branch', 'branch1'], cwd=GIT_REPO) + call(['git', 'checkout', '-q', 'branch1'], cwd=GIT_REPO) + self.do_branch_rename_test(repo, 'branch1') + call(['git', 'branch', 'branch2'], cwd=GIT_REPO) + call(['git', 'checkout', '-q', 'branch2'], cwd=GIT_REPO) + self.do_branch_rename_test(repo, 'branch2') + call(['git', 'checkout', '-q', '--detach', 'branch1'], cwd=GIT_REPO) + self.do_branch_rename_test(repo, lambda b: re.match(br'^[a-f0-9]+$', b)) + finally: + call(['git', 'checkout', '-q', 'master'], cwd=GIT_REPO) + + def test_git_sym(self): + create_watcher = get_fallback_create_watcher() + dotgit = os.path.join(GIT_REPO, '.git') + spacegit = os.path.join(GIT_REPO, ' .git ') + os.rename(dotgit, spacegit) + try: + with open(dotgit, 'w') as F: + F.write('gitdir: .git \n') + gitdir = git_directory(GIT_REPO) + self.assertTrue(os.path.isdir(gitdir)) + self.assertEqual(gitdir, os.path.abspath(spacegit)) + repo = guess(path=GIT_REPO, create_watcher=create_watcher) + self.assertEqual(repo.branch(), 'master') + finally: + os.remove(dotgit) + os.rename(spacegit, dotgit) if use_mercurial: def test_mercurial(self):