diff options
author | Diana Picus <diana.picus@linaro.org> | 2016-10-05 05:29:25 +0300 |
---|---|---|
committer | Diana Picus <diana.picus@linaro.org> | 2016-10-05 05:29:25 +0300 |
commit | 235731c6cb4cd54661d2c496162f8b8b1c5a626a (patch) | |
tree | 8da6937cd43774a10b8c3321703b5d90f1e0c944 | |
parent | 380ed33cd3b063f728296c5eb901d0e50f5fe722 (diff) |
Move branch deletion functionality to GitRepodiana_picus/worktree
Add support for branch deletion to GitRepo and get Worktree to use that instead
of deleting branches by itself.
-rw-r--r-- | linaropy/git/gitrepo.py | 115 | ||||
-rw-r--r-- | linaropy/git/worktree.py | 14 |
2 files changed, 120 insertions, 9 deletions
diff --git a/linaropy/git/gitrepo.py b/linaropy/git/gitrepo.py index f0b1f85..2081361 100644 --- a/linaropy/git/gitrepo.py +++ b/linaropy/git/gitrepo.py @@ -2,6 +2,8 @@ import unittest import logging import os +import uuid + from sh import git from sh import ErrorReturnCode from sh import ErrorReturnCode_128 @@ -120,6 +122,18 @@ class GitRepo(object): raise EnvironmentError( "Unable to return to the previous branch: %s" % exc.stderr) + def deleteBranch(self, branch, force): + deleteFlag = "-d" + if force: + deleteFlag = "-D" + + try: + git("branch", deleteFlag, branch) + logging.info("Deleted branch %s in %s" % (branch, self.repodir)) + except ErrorReturnCode as exc: + raise EnvironmentError( + 'Unable to delete branch: %s' % str(exc)) + # TODO: Write a unit test for this. def add(self, filetogitadd): # TODO: Determine if filetogitadd is relative or absolute. @@ -205,6 +219,17 @@ class GitRepo(object): class TestGitRepo(unittest.TestCase): repoprefix = "GitRepoUT" + def __create_dummy_commit(self): + filename = "file" + str(uuid.uuid4()) + open(filename, "a").close() + git("add", filename) + git("commit", "-m", "Branches without commits confuse git") + + def __get_current_branch(self): + # git rev-parse returns a trailing newline that we must get rid of + branch = str(git("rev-parse", "--abbrev-ref", "HEAD")) + return branch[:-1] + # This class is only used to test the GitRepo functions since, as an, # abstract-baseclass, GitRepo doesn't define repodir, and we need an # actual repository to test git based functions. @@ -268,6 +293,96 @@ class TestGitRepo(unittest.TestCase): # TODO: Test checkoutbranch with various combinations of polluted # directories. + def test_delete_merged_branch(self): + with cd(self.dgr.repodir): + branchToDelete = "delete_me" + try: + previousBranch = self.__get_current_branch() + + # Create a new branch and don't commit anything to it + git("checkout", "-b", branchToDelete) + + # Move somewhere safe and then remove the branch + git("checkout", previousBranch) + self.dgr.deleteBranch(branchToDelete, False) + + with self.assertRaises(ErrorReturnCode) as context: + git("rev-parse", branchToDelete) + except ErrorReturnCode as exc: + raise EnvironmentError("Failed to setup test: %s" % str(exc)) + + def test_delete_unmerged_branch(self): + with cd(self.dgr.repodir): + branchToDelete = "delete_me" + try: + previousBranch = self.__get_current_branch() + + # Create a new branch and commit to it + git("checkout", "-b", branchToDelete) + self.__create_dummy_commit() + + # Move somewhere safe and then try to remove the branch + git("checkout", previousBranch) + + with self.assertRaises(EnvironmentError) as context: + self.dgr.deleteBranch(branchToDelete, False) + + self.assertRegexpMatches(str(context.exception), + "Unable to delete branch:") + + # Make sure the branch still exists + git("rev-parse", branchToDelete) + except ErrorReturnCode as exc: + raise EnvironmentError("Failed to setup test: %s" % str(exc)) + + def test_force_delete_branch(self): + with cd(self.dgr.repodir): + branchToDelete = "force_delete_me" + try: + previousBranch = self.__get_current_branch() + + # Create a new branch and commit to it + git("checkout", "-b", branchToDelete) + self.__create_dummy_commit() + + # Move somewhere safe and then try to remove the branch + git("checkout", previousBranch) + self.dgr.deleteBranch(branchToDelete, True) + + with self.assertRaises(ErrorReturnCode) as context: + git("rev-parse", branchToDelete) + except ErrorReturnCode as exc: + raise EnvironmentError("Failed to setup test: %s" % str(exc)) + + def test_delete_current_branch(self): + with cd(self.dgr.repodir): + try: + currentBranch = self.__get_current_branch() + with self.assertRaises(EnvironmentError) as context: + self.dgr.deleteBranch(currentBranch, False) + + self.assertRegexpMatches(str(context.exception), + "Unable to delete branch:") + + # Make sure the branch still exists + git("rev-parse", currentBranch) + except ErrorReturnCode as exc: + raise EnvironmentError("Failed to setup test: %s" % str(exc)) + + def test_delete_inexistent_branch(self): + with cd(self.dgr.repodir): + inexistentBranch = "should_not_exist" + + # First, make sure that the branch really doesn't exist + with self.assertRaises(ErrorReturnCode) as context: + git("rev-parse", inexistentBranch) + + with self.assertRaises(EnvironmentError) as context: + self.dgr.deleteBranch(inexistentBranch, False) + + self.assertRegexpMatches(str(context.exception), + "Unable to delete branch:") + if __name__ == '__main__': # logging.basicConfig(level="INFO") unittest.main() diff --git a/linaropy/git/worktree.py b/linaropy/git/worktree.py index 2ff2359..95e0300 100644 --- a/linaropy/git/worktree.py +++ b/linaropy/git/worktree.py @@ -157,24 +157,20 @@ class Worktree(GitRepo): raise EnvironmentError( 'Worktree directory was removed, but git prune failed') - # TODO: move this functionality into GitRepo if deleteBranch: - deleteFlag = "-d" - if forceBranchDelete: - deleteFlag = "-D" - try: - git("branch", deleteFlag, branch) + self.deleteBranch(branch, forceBranchDelete) logging.info("Worktree clean: deleted branch %s" % branch) - except ErrorReturnCode as exc: + except EnvironmentError as exc: raise EnvironmentError( - 'Worktree directory was removed, but branch deletion failed: %s' % - str(exc)) + 'Worktree directory %s was removed, but branch deletion failed: %s' % + (self.repodir, str(exc))) class TestWorktree(unittest.TestCase): testdirprefix = "WorktreeUT" + # TODO: these are duplicated in the GitRepo tests - reuse them def __create_dummy_commit(self): filename = "file" + str(uuid.uuid4()) open(filename, "a").close() |