Skip to content

Commit 1fe2aab

Browse files
committed
Reset instead of merge to update branches
Closes #3218
1 parent 43c917d commit 1fe2aab

File tree

4 files changed

+38
-86
lines changed

4 files changed

+38
-86
lines changed

modules/core/src/main/scala/org/scalasteward/core/git/FileGitAlg.scala

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit
9797

9898
override def hasConflicts(repo: File, branch: Branch, base: Branch): F[Boolean] = {
9999
val tryMerge = git_("merge", "--no-commit", "--no-ff", branch.name)(repo)
100-
val abortMerge = git_("merge", "--abort")(repo).void
100+
val abortMerge = git_("merge", "--abort")(repo).attempt.void
101101

102102
returnToCurrentBranch(repo) {
103103
checkoutBranch(repo, base) >> F.guarantee(tryMerge, abortMerge).attempt.map(_.isLeft)
@@ -114,41 +114,14 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit
114114
git("rev-parse", "--verify", branch.name)(repo)
115115
.flatMap(lines => F.fromEither(Sha1.from(lines.mkString("").trim)))
116116

117-
override def mergeTheirs(repo: File, branch: Branch): F[Option[Commit]] =
118-
for {
119-
before <- latestSha1(repo, Branch.head)
120-
_ <- git_("merge", "--strategy-option=theirs", sign, branch.name)(repo).void
121-
.handleErrorWith { throwable =>
122-
// Resolve CONFLICT (modify/delete) by deleting unmerged files:
123-
for {
124-
unmergedFiles <- git("diff", "--name-only", "--diff-filter=U")(repo)
125-
_ <- Nel
126-
.fromList(unmergedFiles.filter(_.nonEmpty))
127-
.fold(F.raiseError[Unit](throwable))(_.traverse_(file => git_("rm", file)(repo)))
128-
_ <- git_("commit", "--all", "--no-edit", sign)(repo)
129-
} yield ()
130-
}
131-
after <- latestSha1(repo, Branch.head)
132-
} yield Option.when(before =!= after)(Commit(after))
133-
134117
override def push(repo: File, branch: Branch): F[Unit] =
135118
git_("push", "--force", "--set-upstream", "origin", branch.name)(repo).void
136119

137120
override def removeClone(repo: File): F[Unit] =
138121
fileAlg.deleteForce(repo)
139122

140-
override def revertChanges(repo: File, base: Branch): F[Option[Commit]] = {
141-
val range = dotdot(base, Branch.head)
142-
git("log", "--pretty=format:%h %p", range)(repo).flatMap { commitsWithParents =>
143-
val commitsUntilMerge = commitsWithParents.map(_.split(' ')).takeWhile(_.length === 2)
144-
val commits = commitsUntilMerge.flatMap(_.headOption)
145-
if (commits.isEmpty) F.pure(None)
146-
else {
147-
val msg = CommitMsg(s"Revert commit(s) " + commits.mkString(", "))
148-
git_("revert" :: "--no-commit" :: commits: _*)(repo) >> commitAllIfDirty(repo, msg)
149-
}
150-
}
151-
}
123+
override def resetHard(repo: File, base: Branch): F[Unit] =
124+
git_("reset", "--hard", base.name)(repo).void
152125

153126
override def setAuthor(repo: File, author: Author): F[Unit] =
154127
for {

modules/core/src/main/scala/org/scalasteward/core/git/GenGitAlg.scala

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,11 @@ trait GenGitAlg[F[_], Repo] {
6666

6767
def latestSha1(repo: Repo, branch: Branch): F[Sha1]
6868

69-
/** Merges `branch` into the current branch using `theirs` as merge strategy option. */
70-
def mergeTheirs(repo: Repo, branch: Branch): F[Option[Commit]]
71-
7269
def push(repo: Repo, branch: Branch): F[Unit]
7370

7471
def removeClone(repo: Repo): F[Unit]
7572

76-
def revertChanges(repo: Repo, base: Branch): F[Option[Commit]]
73+
def resetHard(repo: Repo, base: Branch): F[Unit]
7774

7875
def setAuthor(repo: Repo, author: Author): F[Unit]
7976

@@ -152,17 +149,14 @@ trait GenGitAlg[F[_], Repo] {
152149
override def latestSha1(repo: A, branch: Branch): F[Sha1] =
153150
f(repo).flatMap(self.latestSha1(_, branch))
154151

155-
override def mergeTheirs(repo: A, branch: Branch): F[Option[Commit]] =
156-
f(repo).flatMap(self.mergeTheirs(_, branch))
157-
158152
override def push(repo: A, branch: Branch): F[Unit] =
159153
f(repo).flatMap(self.push(_, branch))
160154

161155
override def removeClone(repo: A): F[Unit] =
162156
f(repo).flatMap(self.removeClone)
163157

164-
override def revertChanges(repo: A, base: Branch): F[Option[Commit]] =
165-
f(repo).flatMap(self.revertChanges(_, base))
158+
override def resetHard(repo: A, base: Branch): F[Unit] =
159+
f(repo).flatMap(self.resetHard(_, base))
166160

167161
override def setAuthor(repo: A, author: Author): F[Unit] =
168162
f(repo).flatMap(self.setAuthor(_, author))

modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ final class NurtureAlg[F[_]](config: ForgeCfg)(implicit
267267
gitAlg.returnToCurrentBranch(data.repo) {
268268
gitAlg.checkoutBranch(data.repo, data.updateBranch) >>
269269
shouldBeUpdated(data).ifM(
270-
ifTrue = mergeAndApplyAgain(number, data),
270+
ifTrue = resetAndApplyAgain(number, data),
271271
ifFalse = (Ignored: ProcessResult).pure
272272
)
273273
}
@@ -293,22 +293,19 @@ final class NurtureAlg[F[_]](config: ForgeCfg)(implicit
293293
result.flatMap { case (update, msg) => logger.info(msg).as(update) }
294294
}
295295

296-
private def mergeAndApplyAgain(number: PullRequestNumber, data: UpdateData): F[ProcessResult] =
296+
private def resetAndApplyAgain(number: PullRequestNumber, data: UpdateData): F[ProcessResult] =
297297
for {
298298
_ <- logger.info(
299-
s"Merge branch ${data.baseBranch.name} into ${data.updateBranch.name} and apply again"
299+
s"Reset ${data.updateBranch.name} to ${data.baseBranch.name} and apply again"
300300
)
301-
maybeRevertCommit <- gitAlg.revertChanges(data.repo, data.baseBranch)
302-
maybeMergeCommit <- gitAlg.mergeTheirs(data.repo, data.baseBranch)
301+
_ <- gitAlg.resetHard(data.repo, data.baseBranch)
303302
edits <- data.update.on(
304303
update = editAlg.applyUpdate(data.repoData, _),
305304
grouped = _.updates.flatTraverse(editAlg.applyUpdate(data.repoData, _))
306305
)
307306
editCommits = edits.flatMap(_.commits)
308-
commits = maybeRevertCommit.toList ++ maybeMergeCommit.toList ++ editCommits
309-
result <- pushCommits(data, commits)
307+
result <- pushCommits(data, editCommits)
310308
requestData <- preparePullRequest(data, edits)
311309
_ <- forgeApiAlg.updatePullRequest(number: PullRequestNumber, data.repo, requestData)
312310
} yield result
313-
314311
}

modules/core/src/test/scala/org/scalasteward/core/git/FileGitAlgTest.scala

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import cats.effect.IO
66
import cats.syntax.all._
77
import munit.CatsEffectSuite
88
import org.scalasteward.core.TestInstances.ioLogger
9-
import org.scalasteward.core.git.FileGitAlgTest.{ioAuxGitAlg, ioGitAlg, master}
9+
import org.scalasteward.core.git.FileGitAlgTest.{
10+
conflictsNo,
11+
conflictsYes,
12+
ioAuxGitAlg,
13+
ioGitAlg,
14+
master
15+
}
1016
import org.scalasteward.core.io.FileAlgTest.ioFileAlg
1117
import org.scalasteward.core.io.ProcessAlgTest.ioProcessAlg
1218
import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
@@ -47,7 +53,7 @@ class FileGitAlgTest extends CatsEffectSuite {
4753
for {
4854
_ <- ioAuxGitAlg.createRepo(repo)
4955
_ <- ioAuxGitAlg.createConflict(repo)
50-
authors <- ioGitAlg.branchAuthors(repo, Branch("conflicts-no"), master)
56+
authors <- ioGitAlg.branchAuthors(repo, conflictsNo, master)
5157
_ = assertEquals(authors, List("'Bot Doe'"))
5258
} yield ()
5359
}
@@ -157,57 +163,37 @@ class FileGitAlgTest extends CatsEffectSuite {
157163
for {
158164
_ <- ioAuxGitAlg.createRepo(repo)
159165
_ <- ioAuxGitAlg.createConflict(repo)
160-
c1 <- ioGitAlg.hasConflicts(repo, Branch("conflicts-yes"), master)
161-
c2 <- ioGitAlg.hasConflicts(repo, Branch("conflicts-no"), master)
166+
c1 <- ioGitAlg.hasConflicts(repo, conflictsYes, master)
167+
c2 <- ioGitAlg.hasConflicts(repo, conflictsNo, master)
162168
_ = assertEquals((c1, c2), (true, false))
163169
} yield ()
164170
}
165171

166-
test("mergeTheirs") {
167-
val repo = rootDir / "mergeTheirs"
172+
test("isMerged") {
173+
val repo = rootDir / "isMerged"
168174
for {
169175
_ <- ioAuxGitAlg.createRepo(repo)
170176
_ <- ioAuxGitAlg.createConflict(repo)
171-
branch = Branch("conflicts-yes")
172-
c1 <- ioGitAlg.hasConflicts(repo, branch, master)
173-
m1 <- ioGitAlg.isMerged(repo, master, branch)
174-
_ <- ioGitAlg.checkoutBranch(repo, branch)
175-
_ <- ioGitAlg.mergeTheirs(repo, master)
176-
c2 <- ioGitAlg.hasConflicts(repo, branch, master)
177-
m2 <- ioGitAlg.isMerged(repo, master, branch)
178-
_ = assertEquals((c1, m1, c2, m2), (true, false, false, true))
179-
} yield ()
180-
}
181-
182-
test("mergeTheirs: CONFLICT (modify/delete)") {
183-
val repo = rootDir / "mergeTheirs-modify-delete"
184-
for {
185-
_ <- ioAuxGitAlg.createRepo(repo)
186-
_ <- ioAuxGitAlg.createConflictFileRemovedOnMaster(repo)
187-
branch = Branch("conflicts-yes")
188-
c1 <- ioGitAlg.hasConflicts(repo, branch, master)
189-
m1 <- ioGitAlg.isMerged(repo, master, branch)
190-
_ <- ioGitAlg.checkoutBranch(repo, branch)
191-
_ <- ioGitAlg.mergeTheirs(repo, master)
192-
c2 <- ioGitAlg.hasConflicts(repo, branch, master)
193-
m2 <- ioGitAlg.isMerged(repo, master, branch)
194-
_ = assertEquals((c1, m1, c2, m2), (true, false, false, true))
177+
m1 <- ioGitAlg.isMerged(repo, conflictsNo, master)
178+
_ <- ioAuxGitAlg.git("merge", conflictsNo.name)(repo)
179+
m2 <- ioGitAlg.isMerged(repo, conflictsNo, master)
180+
_ = assertEquals((m1, m2), (false, true))
195181
} yield ()
196182
}
197183

198-
test("revertChanges") {
199-
val repo = rootDir / "revertChanges"
184+
test("resetHard") {
185+
val repo = rootDir / "resetHard"
200186
for {
201187
_ <- ioAuxGitAlg.createRepo(repo)
202188
_ <- ioAuxGitAlg.createConflict(repo)
203-
branch = Branch("conflicts-yes")
189+
branch = conflictsYes
204190
c1 <- ioGitAlg.hasConflicts(repo, branch, master)
205191
d1 <- ioGitAlg.branchesDiffer(repo, master, branch)
206192
_ <- ioGitAlg.checkoutBranch(repo, branch)
207-
_ <- ioGitAlg.revertChanges(repo, master)
193+
_ <- ioGitAlg.resetHard(repo, master)
208194
c2 <- ioGitAlg.hasConflicts(repo, branch, master)
209195
d2 <- ioGitAlg.branchesDiffer(repo, master, branch)
210-
_ = assertEquals((c1, d1, c2, d2), (true, true, false, true))
196+
_ = assertEquals((c1, d1, c2, d2), (true, true, false, false))
211197
} yield ()
212198
}
213199

@@ -217,7 +203,9 @@ class FileGitAlgTest extends CatsEffectSuite {
217203
}
218204

219205
object FileGitAlgTest {
220-
val master: Branch = Branch("master")
206+
private val master: Branch = Branch("master")
207+
private val conflictsNo: Branch = Branch("conflicts-no")
208+
private val conflictsYes: Branch = Branch("conflicts-yes")
221209

222210
final class AuxGitAlg[F[_]](implicit
223211
fileAlg: FileAlg[F],
@@ -250,13 +238,13 @@ object FileGitAlgTest {
250238
_ <- fileAlg.writeFile(repo / "file2", "file2, line1")
251239
_ <- addFiles(repo, repo / "file1", repo / "file2")
252240
// work on conflicts-no
253-
_ <- gitAlg.createBranch(repo, Branch("conflicts-no"))
241+
_ <- gitAlg.createBranch(repo, conflictsNo)
254242
_ <- fileAlg.writeFile(repo / "file3", "file3, line1")
255243
_ <- git("add", "file3")(repo)
256244
_ <- gitAlg.commitAll(repo, CommitMsg("Add file3 on conflicts-no"))
257245
_ <- gitAlg.checkoutBranch(repo, master)
258246
// work on conflicts-yes
259-
_ <- gitAlg.createBranch(repo, Branch("conflicts-yes"))
247+
_ <- gitAlg.createBranch(repo, conflictsYes)
260248
_ <- fileAlg.writeFile(repo / "file2", "file2, line1\nfile2, line2 on conflicts-yes")
261249
_ <- git("add", "file2")(repo)
262250
_ <- gitAlg.commitAll(repo, CommitMsg("Modify file2 on conflicts-yes"))
@@ -274,7 +262,7 @@ object FileGitAlgTest {
274262
_ <- fileAlg.writeFile(repo / "file2", "file2, line1")
275263
_ <- addFiles(repo, repo / "file1", repo / "file2")
276264
// work on conflicts-yes
277-
_ <- gitAlg.createBranch(repo, Branch("conflicts-yes"))
265+
_ <- gitAlg.createBranch(repo, conflictsYes)
278266
_ <- fileAlg.writeFile(repo / "file2", "file2, line1\nfile2, line2 on conflicts-yes")
279267
_ <- git("add", "file2")(repo)
280268
_ <- gitAlg.commitAll(repo, CommitMsg("Modify file2 on conflicts-yes"))

0 commit comments

Comments
 (0)