Skip to content

Commit

Permalink
Prevent PRs with empty diffs
Browse files Browse the repository at this point in the history
Closes: #2154
  • Loading branch information
fthomas committed Oct 26, 2021
1 parent dc98c0f commit fbba3c7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit
override def branchExists(repo: File, branch: Branch): F[Boolean] =
git("branch", "--list", "--no-color", "--all", branch.name)(repo).map(_.mkString.trim.nonEmpty)

override def branchesDiffer(repo: File, b1: Branch, b2: Branch): F[Boolean] =
git("diff", b1.name, b2.name)(repo).map(_.nonEmpty)

override def checkoutBranch(repo: File, branch: Branch): F[Unit] =
git("checkout", branch.name)(repo).void

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ trait GenGitAlg[F[_], Repo] {

def branchExists(repo: Repo, branch: Branch): F[Boolean]

def branchesDiffer(repo: Repo, b1: Branch, b2: Branch): F[Boolean]

def checkoutBranch(repo: Repo, branch: Branch): F[Unit]

def clone(repo: Repo, url: Uri): F[Unit]
Expand Down Expand Up @@ -88,6 +90,9 @@ trait GenGitAlg[F[_], Repo] {
override def branchExists(repo: A, branch: Branch): F[Boolean] =
f(repo).flatMap(self.branchExists(_, branch))

override def branchesDiffer(repo: A, b1: Branch, b2: Branch): F[Boolean] =
f(repo).flatMap(self.branchesDiffer(_, b1, b2))

override def checkoutBranch(repo: A, branch: Branch): F[Unit] =
f(repo).flatMap(self.checkoutBranch(_, branch))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ final class NurtureAlg[F[_]](config: VCSCfg)(implicit
editAlg.applyUpdate(data.repoData, data.update, createBranch).flatMap { edits =>
val editCommits = edits.flatMap(_.maybeCommit)
if (editCommits.isEmpty) logger.warn("No commits created").as(Ignored)
else pushCommits(data, editCommits) >> createPullRequest(data, edits)
else
gitAlg.branchesDiffer(data.repo, data.baseBranch, data.updateBranch).flatMap {
case true => pushCommits(data, editCommits) >> createPullRequest(data, edits)
case false => logger.warn("No diff between base and update branch").as(Ignored)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ class FileGitAlgTest extends CatsEffectSuite {
} yield ()
}

test("branchesDiffer") {
val repo = rootDir / "branchesDiffer"
val (foo, bar) = (Branch("foo"), Branch("bar"))
for {
_ <- ioAuxGitAlg.createRepo(repo)
_ <- ioGitAlg.createBranch(repo, foo)
_ <- ioGitAlg.createBranch(repo, bar)
b1 <- ioGitAlg.branchesDiffer(repo, bar, foo)
_ <- ioFileAlg.writeFile(repo / "test.txt", "hello")
_ <- ioAuxGitAlg.git("add", "test.txt")(repo)
_ <- ioGitAlg.commitAll(repo, CommitMsg("Add test.txt"))
b2 <- ioGitAlg.branchesDiffer(repo, foo, bar)
_ = assertEquals((b1, b2), (false, true))
} yield ()
}

test("cloneExists") {
val repo = rootDir / "cloneExists"
for {
Expand Down

0 comments on commit fbba3c7

Please sign in to comment.