Пример #1
0
 def save_patch_file(self):
     diff = self.repo.diff(self.base_branch)
     result = PatchUtils.save_diff_to_patch_file(diff,
                                                 self.new_patch_filename)
     if not result:
         raise ValueError(
             "Failed to save patch file. See previous error messages for details."
         )
     LOG.info(
         "Created patch file: '%s' [size: %s]",
         self.new_patch_filename,
         FileUtils.get_file_size(self.new_patch_filename),
     )
    def run(self):
        branch_results = {}
        for branch in self.branches:
            LOG.info("Processing branch: %s", branch)

            exists = self.upstream_repo.is_branch_exist(branch)
            commits = self.upstream_repo.log(branch,
                                             grep=self.jira_id,
                                             oneline=True)
            commit_hashes = GitWrapper.extract_commit_hash_from_gitlog_results(
                commits)
            branch_result = BranchResults(branch, exists, commits,
                                          commit_hashes)
            branch_results[branch] = branch_result

            # Only store diff if number of matched commits for this branch is 1
            if branch_result.number_of_commits == 1:
                commit_hash = branch_result.single_commit_hash
                # TODO create diff_with_parent helper method to GitWrapper
                diff = self.upstream_repo.diff_between_refs(
                    commit_hash + "^", commit_hash)
                branch_result.git_diff = diff
                PatchUtils.save_diff_to_patch_file(
                    diff,
                    FileUtils.join_path(self.basedir,
                                        f"{self.jira_id}-{branch}.diff"))

        # Validate results
        branch_does_not_exist = [
            b_res.branch_name for br, b_res in branch_results.items()
            if not b_res.exists
        ]
        zero_commit = [
            b_res.branch_name for br, b_res in branch_results.items()
            if b_res.number_of_commits == 0
        ]
        multiple_commits = [
            b_res.branch_name for br, b_res in branch_results.items()
            if b_res.number_of_commits > 1
        ]

        LOG.debug("Branch result objects: %s", branch_results)
        if branch_does_not_exist:
            raise ValueError(
                "The following branches are not existing for Jira ID '{}': {}",
                branch_does_not_exist)

        if zero_commit:
            raise ValueError(
                "The following branches do not contain commit for Jira ID '{}': {}",
                self.jira_id, zero_commit)

        if multiple_commits:
            raise ValueError(
                "The following branches contain multiple commits for Jira ID '{}': {}",
                self.jira_id, multiple_commits)

        LOG.info("Generated diff files: ")
        diff_files = FileUtils.find_files(self.basedir,
                                          self.jira_id + "-.*",
                                          single_level=True,
                                          full_path_result=True)
        for f in diff_files:
            LOG.info("%s: %s", f, FileUtils.get_file_size(f))