def apply_patch(self, branch_name, path, keep_square_brackets=False): """Apply a git patch file on top of the specified branch. :param str branch_name: The name of the branch or reference to apply the patch to :param str path: Path to a git-formatted patch file (cf. git format-patch) :param bool keep_square_brackets: Preserve non-[PATCH] brackets in commit subject """ # Expand file (also needed for git-am) and check it exists full_path = self._expand_file_path(path) # Checkout try: self.git_repo.git.checkout(branch_name) except git.GitCommandError as ex: msg = "Could not checkout branch {name}. Error: {error}".format(name=branch_name, error=ex) raise_from(exceptions.CheckoutException(msg), ex) # Apply the patch file try: if keep_square_brackets: self.git_repo.git.am("--keep-non-patch", full_path) else: self.git_repo.git.am(full_path) except git.GitCommandError as ex: msg = "Could not apply patch {path} on branch {name}. Error: {error}".format(path=full_path, name=branch_name, error=ex) raise_from(exceptions.ChangeNotAppliedException(msg), ex)
def cherrypick(self, sha, branch_name): """Apply given sha on given branch :param str sha: The SHA1 of the commit to cherry-pick :param str branch_name: The branch to apply it to """ if self.git_repo.repo.is_dirty(): working_dir = self.git_repo.repo.working_dir msg = (f"Repository {working_dir} is dirty. Please clean " "workspace before proceeding.") raise exceptions.DirtyRepositoryException(msg) # Checkout try: self.git_repo.git.checkout(branch_name) except git.GitCommandError as ex: msg = f"Could not checkout branch {branch_name}. Error: {ex}" raise exceptions.CheckoutException(msg) from ex # Cherry-pick try: self.git_repo.git.cherry_pick(sha) except git.GitCommandError as ex: msg = (f"Could not cherry-pick commit {sha} on {branch_name}. " f"Error: {ex}") raise exceptions.ChangeNotAppliedException(msg) from ex msg = f"Successfully cherry-picked commit {sha} on {branch_name}" self.logger.debug(msg)
def apply_diff(self, branch_name, diff_path, message, signoff=False): """Apply a diff on top of the specified branch. :param str branch_name: The name of the branch or reference to apply the diff to :param str diff_path: Path to the diff file :param str message: Commit message :param bool signoff: Whether to add signed-off-by to commit message """ # Ensure we don't commit more than we mean to if self.git_repo.repo.is_dirty(): msg = ("Repository {repo} contains uncommitted changes. Please clean workspace " "before proceeding.".format(repo=self.git_repo.repo.working_dir)) raise exceptions.DirtyRepositoryException(msg) # Check diff file exists full_path = self._expand_file_path(diff_path) # Checkout try: self.git_repo.git.checkout(branch_name) except git.GitCommandError as ex: msg = "Could not checkout branch {name}. Error: {error}".format(name=branch_name, error=ex) raise_from(exceptions.CheckoutException(msg), ex) # Apply the diff try: self.git_repo.git.apply(full_path) except git.GitCommandError as ex: msg = "Could not apply diff {path} on branch {name}. Error: {error}".format(path=full_path, name=branch_name, error=ex) raise_from(exceptions.ChangeNotAppliedException(msg), ex) # Commit self.git_repo.commit.commit(message, signoff)
def cherrypick(self, sha, branch_name): """Apply given sha on given branch :param str sha: The SHA1 of the commit to cherry-pick :param str branch_name: The branch to apply it to """ if self.git_repo.repo.is_dirty(): msg = ("Repository {0} is dirty. Please clean workspace " "before proceeding.".format(self.git_repo.repo.working_dir)) raise exceptions.DirtyRepositoryException(msg) # Checkout try: self.git_repo.git.checkout(branch_name) except git.GitCommandError as ex: msg = "Could not checkout branch {name}. Error: {error}".format(name=branch_name, error=ex) raise_from(exceptions.CheckoutException(msg), ex) # Cherry-pick try: self.git_repo.git.cherry_pick(sha) except git.GitCommandError as ex: msg = "Could not cherry-pick commit {sha} on {name}. Error: {error}".format(name=branch_name, sha=sha, error=ex) raise_from(exceptions.ChangeNotAppliedException(msg), ex) self.logger.debug("Successfully cherry-picked commit %s on %s", sha, branch_name)
def apply_diff(self, branch_name, diff_path, message, signoff=False): """Apply a diff on top of the specified branch. :param str branch_name: The name of the branch or reference to apply the diff to :param str diff_path: Path to the diff file :param str message: Commit message :param bool signoff: Whether to add signed-off-by to commit message """ # Ensure we don't commit more than we mean to if self.git_repo.repo.is_dirty(untracked_files=True): repo = self.git_repo.repo.working_dir msg = (f"Repository {repo} contains uncommitted changes. Please " "clean workspace before proceeding.") raise exceptions.DirtyRepositoryException(msg) # Check diff file exists full_path = self._expand_file_path(diff_path) # Checkout try: self.git_repo.git.checkout(branch_name) except git.GitCommandError as ex: msg = f"Could not checkout branch {branch_name}. Error: {ex}" raise exceptions.CheckoutException(msg) from ex # Apply the diff try: self.git_repo.git.apply(full_path) except git.GitCommandError as ex: msg = (f"Could not apply diff {full_path} on branch " f"{branch_name}. Error: {ex}") raise exceptions.ChangeNotAppliedException(msg) from ex # The diff may have added new files, ensure they are staged self.git_repo.git.add(".") # Commit self.git_repo.commit.commit(message, signoff)