Repository: scala-steward-org/scala-steward
Pull request #2371 (2021-11-22 14:54:35)

Delete update branch when syncing a fork
Closes #741.

codecov[bot]: # [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org) Report
> Merging [#2371](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org) (6754096) into [master](https://codecov.io/gh/scala-steward-org/scala-steward/commit/5cf6463c677f8538ffee642b2dfb1cb1f4e87b60?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org) (5cf6463) will **increase** coverage by `0.02%`.
> The diff coverage is `80.00%`.

[![Impacted file tree graph](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371/graphs/tree.svg?width=650&height=150&src=pr&token=udzMsc1Dne&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org)](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org)

```diff
@@            Coverage Diff             @@
##           master    #2371      +/-   ##
==========================================
+ Coverage   80.36%   80.38%   +0.02%     
==========================================
  Files         139      139              
  Lines        2388     2396       +8     
  Branches       55       54       -1     
==========================================
+ Hits         1919     1926       +7     
- Misses        469      470       +1     
```


| [Impacted Files](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org) | Coverage Δ | |
|---|---|---|
| [...n/scala/org/scalasteward/core/git/FileGitAlg.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS9naXQvRmlsZUdpdEFsZy5zY2FsYQ==) | `96.66% <0.00%> (-1.64%)` | :arrow_down: |
| [...in/scala/org/scalasteward/core/git/GenGitAlg.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS9naXQvR2VuR2l0QWxnLnNjYWxh) | `89.28% <0.00%> (-3.31%)` | :arrow_down: |
| [...main/scala/org/scalasteward/core/git/package.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS9naXQvcGFja2FnZS5zY2FsYQ==) | `100.00% <100.00%> (ø)` | |
| [...n/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS92Y3MvVkNTUmVwb0FsZy5zY2FsYQ==) | `81.81% <100.00%> (+5.34%)` | :arrow_up: |
| [.../main/scala/org/scalasteward/core/git/Branch.scala](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org#diff-bW9kdWxlcy9jb3JlL3NyYy9tYWluL3NjYWxhL29yZy9zY2FsYXN0ZXdhcmQvY29yZS9naXQvQnJhbmNoLnNjYWxh) | `100.00% <0.00%> (+25.00%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org). Last update [5cf6463...6754096](https://codecov.io/gh/scala-steward-org/scala-steward/pull/2371?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=scala-steward-org).


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/git/FileGitAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/git/FileGitAlg.scala
index 98e57cb5..941dd5eb 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/git/FileGitAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/git/FileGitAlg.scala
@@ -68,6 +68,9 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit
     git("rev-parse", "--abbrev-ref", Branch.head.name)(repo)
       .map(lines => Branch(lines.mkString.trim))
 
+  override def deleteLocalBranch(repo: File, branch: Branch): F[Unit] =
+    git("branch", "--delete", "--force", branch.name)(repo).void
+
   override def deleteRemoteBranch(repo: File, branch: Branch): F[Unit] =
     git("push", "origin", "--delete", branch.name)(repo).void
 
diff --git a/modules/core/src/main/scala/org/scalasteward/core/git/GenGitAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/git/GenGitAlg.scala
index 3a61c1e6..6f570a6d 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/git/GenGitAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/git/GenGitAlg.scala
@@ -44,6 +44,8 @@ trait GenGitAlg[F[_], Repo] {
 
   def currentBranch(repo: Repo): F[Branch]
 
+  def deleteLocalBranch(repo: Repo, branch: Branch): F[Unit]
+
   def deleteRemoteBranch(repo: Repo, branch: Branch): F[Unit]
 
   /** Discards unstaged changes. */
@@ -114,6 +116,9 @@ trait GenGitAlg[F[_], Repo] {
       override def currentBranch(repo: A): F[Branch] =
         f(repo).flatMap(self.currentBranch)
 
+      override def deleteLocalBranch(repo: A, branch: Branch): F[Unit] =
+        f(repo).flatMap(self.deleteLocalBranch(_, branch))
+
       override def deleteRemoteBranch(repo: A, branch: Branch): F[Unit] =
         f(repo).flatMap(self.deleteRemoteBranch(_, branch))
 
diff --git a/modules/core/src/main/scala/org/scalasteward/core/git/package.scala b/modules/core/src/main/scala/org/scalasteward/core/git/package.scala
index 7d8a4fcf..c56c84f0 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/git/package.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/git/package.scala
@@ -25,9 +25,11 @@ import org.scalasteward.core.vcs.data.Repo
 package object git {
   type GitAlg[F[_]] = GenGitAlg[F, Repo]
 
+  val updateBranchPrefix = "update"
+
   def branchFor(update: Update, baseBranch: Option[Branch]): Branch = {
     val base = baseBranch.fold("")(branch => s"${branch.name}/")
-    Branch(s"update/$base${update.name}-${update.nextVersion}")
+    Branch(s"$updateBranchPrefix/$base${update.name}-${update.nextVersion}")
   }
 
   def commitMsgFor(
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
index 62c24404..85cd0f6d 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
@@ -21,7 +21,7 @@ import cats.syntax.all._
 import org.http4s.Uri
 import org.http4s.Uri.UserInfo
 import org.scalasteward.core.application.Config
-import org.scalasteward.core.git.GitAlg
+import org.scalasteward.core.git.{updateBranchPrefix, Branch, GitAlg}
 import org.scalasteward.core.util
 import org.scalasteward.core.util.logger._
 import org.scalasteward.core.vcs.VCSType.GitHub
@@ -58,9 +58,20 @@ final class VCSRepoAlg[F[_]](config: Config)(implicit
   private def syncFork(repo: Repo, repoOut: RepoOut): F[Unit] =
     repoOut.parentOrRaise[F].flatMap { parent =>
       logger.info(s"Synchronize with ${parent.repo.show}") >>
-        gitAlg.syncFork(repo, withLogin(parent.clone_url), parent.default_branch)
+        gitAlg.syncFork(repo, withLogin(parent.clone_url), parent.default_branch) >>
+        deleteUpdateBranch(repo)
     }
 
+  // We use "update" as prefix for our branches but Git doesn't allow branches named
+  // "update" and "update/..." in the same repo. We therefore delete the "update" branch
+  // in our fork if it exists.
+  private def deleteUpdateBranch(repo: Repo): F[Unit] = {
+    val local = Branch(updateBranchPrefix)
+    val remote = local.withPrefix("origin/")
+    gitAlg.branchExists(repo, local).ifM(gitAlg.deleteLocalBranch(repo, local), F.unit) >>
+      gitAlg.branchExists(repo, remote).ifM(gitAlg.deleteRemoteBranch(repo, remote), F.unit)
+  }
+
   private def initSubmodules(repo: Repo): F[Unit] =
     logger.attemptWarn.log_("Initializing and cloning submodules failed") {
       gitAlg.initSubmodules(repo)


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/git/FileGitAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/git/FileGitAlgTest.scala
index a23d1623..8dbfc1f2 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/git/FileGitAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/git/FileGitAlgTest.scala
@@ -34,7 +34,10 @@ class FileGitAlgTest extends CatsEffectSuite {
       _ <- ioGitAlg.createBranch(repo, foo)
       b1 <- ioGitAlg.branchExists(repo, foo)
       b2 <- ioGitAlg.branchExists(repo, bar)
-      _ = assertEquals((b1, b2), (true, false))
+      _ <- ioGitAlg.checkoutBranch(repo, master)
+      _ <- ioGitAlg.deleteLocalBranch(repo, foo)
+      b3 <- ioGitAlg.branchExists(repo, foo)
+      _ = assertEquals((b1, b2, b3), (true, false, false))
     } yield ()
   }
 
diff --git a/modules/core/src/test/scala/org/scalasteward/core/vcs/VCSRepoAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/vcs/VCSRepoAlgTest.scala
index 1f9a98d2..c73b1ff9 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/vcs/VCSRepoAlgTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/vcs/VCSRepoAlgTest.scala
@@ -45,6 +45,8 @@ class VCSRepoAlgTest extends CatsEffectSuite {
         Cmd(gitCmd(repoDir), "checkout", "-B", "main", "--track", "upstream/main"),
         Cmd(gitCmd(repoDir), "merge", "upstream/main"),
         Cmd(gitCmd(repoDir), "push", "--force", "--set-upstream", "origin", "main"),
+        Cmd(gitCmd(repoDir), "branch", "--list", "--no-color", "--all", "update"),
+        Cmd(gitCmd(repoDir), "branch", "--list", "--no-color", "--all", "origin/update"),
         Cmd(gitCmd(repoDir), "submodule", "update", "--init", "--recursive")
       )
     )