def get_modified_files_patch(self, untracked_files_before, patch): """ Get modified files after a patch application. :param untracked_files_before: List of files untracked previous to patch application. :param patch: Path to the patch file. Unused in the svn implementation. """ untracked_files = self.vcs.get_unknown_files() modified_files = self.vcs.get_modified_files() add_to_vcs = [] for untracked_file in untracked_files: if untracked_file not in untracked_files_before: add_to_vcs.append(untracked_file) if add_to_vcs: self.log.info("The files: ") for untracked_file in add_to_vcs: self.log.info(untracked_file) self.log.info("Might need to be added to svn") answer = utils.ask("Would you like to add them to svn ?") if answer == "y": for untracked_file in add_to_vcs: self.add_untracked_file(untracked_file) modified_files.append(untracked_file) elif answer == "n": pass return modified_files
def validate(self): if not os.path.isfile(self.patch): logging.error("Invalid patch file %s provided. Aborting.", self.patch) return 1 changed_files_before = self.vcs.get_modified_files() if changed_files_before: log.error("Repository has changed files prior to patch " "application") answer = utils.ask("Would you like to revert them?") if answer == "n": log.error("Not safe to proceed without reverting files.") return 1 else: for changed_file in changed_files_before: self.vcs.revert_file(changed_file) self.untracked_files = self.vcs.get_unknown_files()
def apply_patch(self, patch): """ Apply a patch to the code base using git am. A new branch will be created with the patch name. :param patch: Path to the patch file. """ def branch_suffix(name): suffix = 1 while not process.run("git show-ref --verify --quiet " "refs/heads/%s_%s" % (branch, suffix), verbose=False, ignore_status=True).exit_status: suffix += 1 return "%s_%s" % (name, suffix) process.run("git checkout master", verbose=False) branch = os.path.basename(patch).rstrip(".patch") try: process.run("git checkout -b %s" % branch, verbose=False) except exceptions.CmdError: self.log.error("branch %s already exists!" % branch) answer = utils.ask("What would you like to do?", options="Abort/Delete/Rename/OldBase/NewBase") if not answer: answer = "A" answer = answer[0].upper() if answer == "A": self.log.info("Aborting check") return 1 elif answer == "D": self.log.info("Deleting branch %s", branch) process.run("git branch -D %s" % branch, verbose=False) process.run("git checkout -b %s" % branch, verbose=False) elif answer == "R": # Rename the old branch old_branch = branch_suffix(branch) self.log.info("Moving branch %s to %s", branch, old_branch) process.run("git branch -M %s %s" % (branch, old_branch), verbose=False) process.run("git checkout -b %s" % branch, verbose=False) elif answer == "O": # Rename the old branch and use old master old_branch = branch_suffix(branch) self.log.info("Moving branch %s to %s", branch, old_branch) process.run("git branch -M %s %s" % (branch, old_branch), verbose=False) process.run("git checkout -b %s" % branch, verbose=False) base = process.run("git merge-base %s %s" % (branch, old_branch), verbose=False).stdout.strip() self.log.info("Last common base is %s", base) process.run("git reset --hard %s" % base, verbose=False) elif answer == "N": # Rename and rebase the old branch old_branch = branch_suffix(branch) self.log.info( "Moving branch %s to %s and rebasing it to " "the current master", branch, old_branch) process.run("git branch -M %s %s" % (branch, old_branch), verbose=False) process.run("git checkout %s" % old_branch, verbose=False) ret = process.run("git rebase master", verbose=False, ignore_status=True) if ret.exit_status: self.log.error( "Fail to automatically rebase old branch " "%s to the current master. Continuing " "without automatic rebase (as if you'd " "chosen 'Rename')", old_branch) process.run("git rebase --abort", verbose=False, ignore_status=True) process.run("git checkout master", verbose=False) process.run("git checkout -b %s" % branch, verbose=False) try: process.run("git am -3 %s" % patch, verbose=False) except exceptions.CmdError, e: self.log.error("Failed to apply patch to the git repo: %s" % e) return 1
def apply_patch(self, patch): """ Apply a patch to the code base using git am. A new branch will be created with the patch name. :param patch: Path to the patch file. """ def branch_suffix(name): suffix = 1 while not process.run("git show-ref --verify --quiet " "refs/heads/%s_%s" % (branch, suffix), verbose=False, ignore_status=True).exit_status: suffix += 1 return "%s_%s" % (name, suffix) process.run("git checkout master", verbose=False) branch = os.path.basename(patch).rstrip(".patch") try: process.run("git checkout -b %s" % branch, verbose=False) except exceptions.CmdError: self.log.error("branch %s already exists!" % branch) answer = utils.ask("What would you like to do?", options="Abort/Delete/Rename/OldBase/NewBase") if not answer: answer = "A" answer = answer[0].upper() if answer == "A": self.log.info("Aborting check") return 1 elif answer == "D": self.log.info("Deleting branch %s", branch) process.run("git branch -D %s" % branch, verbose=False) process.run("git checkout -b %s" % branch, verbose=False) elif answer == "R": # Rename the old branch old_branch = branch_suffix(branch) self.log.info("Moving branch %s to %s", branch, old_branch) process.run("git branch -M %s %s" % (branch, old_branch), verbose=False) process.run("git checkout -b %s" % branch, verbose=False) elif answer == "O": # Rename the old branch and use old master old_branch = branch_suffix(branch) self.log.info("Moving branch %s to %s", branch, old_branch) process.run("git branch -M %s %s" % (branch, old_branch), verbose=False) process.run("git checkout -b %s" % branch, verbose=False) base = process.run("git merge-base %s %s" % (branch, old_branch), verbose=False).stdout.strip() self.log.info("Last common base is %s", base) process.run("git reset --hard %s" % base, verbose=False) elif answer == "N": # Rename and rebase the old branch old_branch = branch_suffix(branch) self.log.info("Moving branch %s to %s and rebasing it to " "the current master", branch, old_branch) process.run("git branch -M %s %s" % (branch, old_branch), verbose=False) process.run("git checkout %s" % old_branch, verbose=False) ret = process.run("git rebase master", verbose=False, ignore_status=True) if ret.exit_status: self.log.error("Fail to automatically rebase old branch " "%s to the current master. Continuing " "without automatic rebase (as if you'd " "chosen 'Rename')", old_branch) process.run("git rebase --abort", verbose=False, ignore_status=True) process.run("git checkout master", verbose=False) process.run("git checkout -b %s" % branch, verbose=False) try: process.run("git am -3 %s" % patch, verbose=False) except exceptions.CmdError, e: self.log.error("Failed to apply patch to the git repo: %s" % e) return 1