diff --git a/windowPatchsets.go b/windowPatchsets.go index 9c005fe..e62d601 100644 --- a/windowPatchsets.go +++ b/windowPatchsets.go @@ -307,6 +307,27 @@ func cleanSubject(line string) string { return strings.TrimSpace(cleaned) } +func findCommitByHash(hash string, subject string) (string, error) { + cmd := exec.Command("git", "log", "--pretty=format:%H %s") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return "", err + } + + lines := strings.Split(out.String(), "\n") + for _, line := range lines { + if strings.Contains(strings.ToLower(line), strings.ToLower(subject)) { + return strings.Fields(line)[0], nil // return the commit hash + } + if strings.Fields(line)[0] == hash { + return "", fmt.Errorf("start commit found: %s", hash) + } + } + return "", fmt.Errorf("no commit found for subject: %s", subject) +} + func findCommitBySubject(subject string) (string, error) { cmd := exec.Command("git", "log", "--pretty=format:%H %s", "--grep="+subject, "-i") var out bytes.Buffer @@ -361,7 +382,7 @@ func AddNotDonePatches(notdone *forgepb.Patches, pset *forgepb.Patchset, full bo comment := cleanSubject(patch.Comment) if found := notdone.FindByCommitHash(patch.CommitHash); found != nil { - log.Info("duplicate notdone patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.RepoNamespace, comment) + log.Info("duplicate notdone", patch.RepoNamespace, "patch:", patch.NewHash, "commithash:", patch.CommitHash, comment) continue } @@ -375,19 +396,27 @@ func AddNotDonePatches(notdone *forgepb.Patches, pset *forgepb.Patchset, full bo } if patch.NewHash != "na" { - log.Info("already applied patch: newhash:", patch.NewHash, "commithash:", patch.CommitHash, patch.RepoNamespace, comment) + log.Info("already applied patch", patch.RepoNamespace, ": newhash:", patch.NewHash, "commithash:", patch.CommitHash, comment) continue } os.Chdir(repo.GetFullPath()) - newhash, err := findCommitBySubject(comment) + newhash, err := findCommitByHash(patch.StartHash, comment) + if err != nil { + // this patch has not been applied yet + log.Info("patch: not found hash:", patch.RepoNamespace, patch.CommitHash, comment, err) + notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice + continue + } + + newhash, err = findCommitBySubject(comment) if err == nil { patch.NewHash = newhash - log.Info("patch: found hash:", patch.CommitHash, newhash, patch.RepoNamespace, comment) + log.Info("patch: found hash:", patch.RepoNamespace, "commit patch", patch.CommitHash, "new hash", newhash, "start hash", patch.StartHash, comment) continue } // this patch has not been applied yet - log.Info("patch: not found hash:", patch.CommitHash, patch.RepoNamespace, comment, newhash, err) + log.Info("patch: not found hash:", patch.RepoNamespace, patch.CommitHash, comment, newhash, err) notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice } }