func applyPatchesToRepo()

in tools/version-tracker/pkg/commands/upgrade/upgrade.go [760:811]


func applyPatchesToRepo(projectRootFilepath, projectRepo string, totalPatchCount int) (int, string, string, error) {
	var patchesApplied int
	var failedPatch, failedFilesInPatch string
	patchApplySucceeded := true

	applyPatchesCommandSequence := fmt.Sprintf("make -C %s patch-repo", projectRootFilepath)
	applyPatchesCmd := exec.Command("bash", "-c", applyPatchesCommandSequence)
	applyPatchesOutput, err := command.ExecCommand(applyPatchesCmd)
	if err != nil {
		if strings.Contains(applyPatchesOutput, constants.FailedPatchApplyMarker) || strings.Contains(applyPatchesOutput, constants.DoesNotExistInIndexMarker) {
			patchApplySucceeded = false
		} else {
			return 0, "", "", fmt.Errorf("running patch-repo Make command: %v", err)
		}
	}

	if patchApplySucceeded {
		patchesApplied = totalPatchCount
	} else {
		failedFiles := []string{}
		gitDescribeRegex := regexp.MustCompile(constants.GitDescribeRegex)
		gitDescribeCmd := exec.Command("git", "-C", filepath.Join(projectRootFilepath, projectRepo), "describe", "--tag")
		gitDescribeOutput, err := command.ExecCommand(gitDescribeCmd)
		if err != nil {
			return 0, "", "", fmt.Errorf("running git describe command: %v", err)
		}
		gitDescribeMatches := gitDescribeRegex.FindStringSubmatch(gitDescribeOutput)
		if gitDescribeMatches[1] != "" {
			patchesApplied, err = strconv.Atoi(gitDescribeMatches[2])
			if err != nil {
				return 0, "", "", fmt.Errorf("converting patch count to integer %v", err)
			}
		}

		failedPatchRegex := regexp.MustCompile(constants.FailedPatchApplyRegex)
		failedPatch = failedPatchRegex.FindString(applyPatchesOutput)

		failedPatchFileRegex := regexp.MustCompile(fmt.Sprintf("%s|%s", constants.FailedPatchFilesRegex, constants.DoesNotExistInIndexFilesRegex))
		applyFailedFiles := failedPatchFileRegex.FindAllStringSubmatch(applyPatchesOutput, -1)
		for _, files := range applyFailedFiles {
			if files[1] != "" {
				failedFiles = append(failedFiles, fmt.Sprintf("`%s`", files[1]))
			} else if files[2] != "" {
				failedFiles = append(failedFiles, fmt.Sprintf("`%s`", files[2]))
			}
		}

		failedFilesInPatch = strings.Join(failedFiles, ",")
	}

	return patchesApplied, failedPatch, failedFilesInPatch, nil
}