def test_detect_worktree_failing_git(self): with self.executable_git() as git: with open(git, "w") as fp: fp.write("#!/bin/sh\n") fp.write("exit 1") self.assertIsNone(Git.detect_worktree()) self.assertIsNone(Git.detect_worktree(git))
def test_detect_worktree_working_git(self): expected_worktree_dir = "/a/fake/worktree/dir" with self.executable_git() as git: with open(git, "w") as fp: fp.write("#!/bin/sh\n") fp.write("echo " + expected_worktree_dir) self.assertEqual(expected_worktree_dir, Git.detect_worktree()) self.assertEqual(expected_worktree_dir, Git.detect_worktree(binary=git))
def worktree_relative_to(some_dir, expected): # Given a directory relative to the worktree, tests that the worktree is detected as 'expected'. subdir = os.path.join(clone, some_dir) if not os.path.isdir(subdir): os.mkdir(subdir) actual = Git.detect_worktree(subdir=subdir) self.assertEqual(expected, actual)
def worktree_relative_to(cwd, expected): # Given a cwd relative to the worktree, tests that the worktree is detected as 'expected'. orig_cwd = os.getcwd() try: abs_cwd = os.path.join(clone, cwd) if not os.path.isdir(abs_cwd): os.mkdir(abs_cwd) os.chdir(abs_cwd) actual = Git.detect_worktree() self.assertEqual(expected, actual) finally: os.chdir(orig_cwd)
def get_git() -> Optional[Git]: """Returns Git, if available.""" global _Git if _Git: return _Git # We know about Git, so attempt an auto-configure worktree = Git.detect_worktree() if worktree and os.path.isdir(worktree): git = Git(worktree=worktree) try: logger.debug(f"Detected git repository at {worktree} on branch {git.branch_name}") _Git = git except GitException as e: logger.info(f"Failed to load git repository at {worktree}: {e!r}") return _Git
def test_detect_worktree_invalid_executable_git(self): with self.executable_git() as git: self.assertIsNone(Git.detect_worktree()) self.assertIsNone(Git.detect_worktree(binary=git))
def test_detect_worktree_no_git(self): with self.empty_path(): self.assertIsNone(Git.detect_worktree())