Skip to content

Commit

Permalink
check_bad_merge: fix detecting bad merges by ignoring commits that ar…
Browse files Browse the repository at this point in the history
…e already on `release`

Fixes gradle#32133

Signed-off-by: Christoph Obexer <[email protected]>
  • Loading branch information
cobexer committed Jan 24, 2025
1 parent 826dff0 commit bf29a5f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/CheckBadMerge.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class CheckBadMerge {
}

List<String> commits = Files.readAllLines(Paths.get(args[0]))
println("Commits to check: ${Arrays.toString(commits)}")
try {
commits.each { checkCommit(it) }
} finally {
Expand All @@ -64,6 +63,13 @@ class CheckBadMerge {
return
}

List<String> commitBranches = branchesOf(commit)
if (commitBranches.contains("origin/release")) {
println("$commit is a merge commit already on release, ignoring.")
println(" Branches: $commitBranches")
return
}

// The correct state we are looking for is:
// 1. It's a merge commit.
// 2. One of its parent commits is from master only.
Expand All @@ -72,15 +78,23 @@ class CheckBadMerge {
List<String> p1Branches = branchesOf(parentCommits[0])
List<String> p2Branches = branchesOf(parentCommits[1])

println("$commit parents: $parentCommits")
println(" p1Branches: $p1Branches")
println(" p2Branches: $p2Branches")
if (p1Branches.contains("origin/master") && !p2Branches.contains("origin/master") && p2Branches.any { it.startsWith("origin/release") }) {
List<String> badFiles = filesFromMerge(commit).findAll {gitFile -> MONITORED_PATHS.any { forbiddenPath -> gitFile.startsWith(forbiddenPath)} }
if (!badFiles.empty) {
throw new RuntimeException("Found bad files in merge commit $commit: $badFiles")
System.err.println("Found bad files in merge commit $commit, run the listed commands:")
badFiles.each {
System.err.println("git restore --source=master -SW -- '$it'")
}
System.err.println("And then amend the merge commit to remove all offending changes.")
System.exit(1)
} else {
println("No bad files found in $commit")
println(" -> No bad files found")
}
} else {
println("$commit is not a merge commit we're looking for. Parents: $parentCommits, p1Branches: $p1Branches, p2Branches: $p2Branches")
println(" -> is not a merge commit we're looking for.")
}
}

Expand Down

0 comments on commit bf29a5f

Please sign in to comment.