Skip to content

Commit b53f6e7

Browse files
committed
refactor(mercurial): Introduce a runHg() helper extension function
Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 3a346df commit b53f6e7

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

plugins/version-control-systems/mercurial/src/main/kotlin/Mercurial.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ internal object MercurialCommand : CommandLineTool {
4747
}.orEmpty()
4848
}
4949

50+
internal fun MercurialWorkingTree.runHg(vararg args: String) =
51+
MercurialCommand.run(*args, workingDir = workingDir).requireSuccess()
52+
5053
@OrtPlugin(
5154
displayName = "Mercurial",
5255
description = "A VCS implementation to interact with Mercurial repositories.",
@@ -103,15 +106,17 @@ class Mercurial(override val descriptor: PluginDescriptor = MercurialFactory.des
103106

104107
override fun updateWorkingTree(workingTree: WorkingTree, revision: String, path: String, recursive: Boolean) =
105108
runCatching {
109+
check(workingTree is MercurialWorkingTree)
110+
106111
// To safe network bandwidth, only pull exactly the revision we want. Do not use "-u" to update the
107112
// working tree just yet, as Mercurial would only update if new changesets were pulled. But that might
108113
// not be the case if the requested revision is already available locally.
109-
MercurialCommand.run(workingTree.getRootPath(), "pull", "-r", revision).requireSuccess()
114+
workingTree.runHg("pull", "-r", revision)
110115

111116
// TODO: Implement updating of subrepositories.
112117

113118
// Explicitly update the working tree to the desired revision.
114-
MercurialCommand.run(workingTree.getRootPath(), "update", revision).isSuccess
119+
workingTree.runHg("update", revision).isSuccess
115120
}.map {
116121
revision
117122
}

plugins/version-control-systems/mercurial/src/main/kotlin/MercurialWorkingTree.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ internal class MercurialWorkingTree(workingDir: File, vcsType: VcsType) : Workin
3434

3535
override fun isShallow() = false
3636

37-
override fun getRemoteUrl() = MercurialCommand.run(workingDir, "paths", "default").requireSuccess().stdout.trimEnd()
37+
override fun getRemoteUrl() = runHg("paths", "default").stdout.trimEnd()
3838

39-
override fun getRevision() =
40-
MercurialCommand.run(workingDir, "--debug", "id", "-i").requireSuccess().stdout.trimEnd()
39+
override fun getRevision() = runHg("--debug", "id", "-i").stdout.trimEnd()
4140

42-
override fun getRootPath() = File(MercurialCommand.run(workingDir, "root").requireSuccess().stdout.trimEnd())
41+
override fun getRootPath() = File(runHg("root").stdout.trimEnd())
4342

4443
override fun listRemoteBranches(): List<String> {
45-
val branches = MercurialCommand.run(workingDir, "branches").requireSuccess().stdout.trimEnd()
44+
val branches = runHg("branches").stdout.trimEnd()
4645
return branches.lines().map {
4746
it.substringBefore(' ')
4847
}.sorted()
@@ -51,8 +50,8 @@ internal class MercurialWorkingTree(workingDir: File, vcsType: VcsType) : Workin
5150
override fun listRemoteTags(): List<String> {
5251
// Mercurial does not have the concept of global remote tags. Its "regular tags" are defined per
5352
// branch as part of the committed ".hgtags" file. See https://stackoverflow.com/a/2059189/1127485.
54-
MercurialCommand.run(workingDir, "pull", "-r", "default").requireSuccess()
55-
val tags = MercurialCommand.run(workingDir, "cat", "-r", "default", ".hgtags").requireSuccess().stdout.trimEnd()
53+
runHg("pull", "-r", "default")
54+
val tags = runHg("cat", "-r", "default", ".hgtags").stdout.trimEnd()
5655
return tags.lines().map {
5756
it.substringAfterLast(' ')
5857
}.sorted()

0 commit comments

Comments
 (0)