Skip to content

Commit b78ee1d

Browse files
osillaanatolystansler
authored andcommitted
feat: relative path support (#149)
1 parent 7e5dfc1 commit b78ee1d

File tree

9 files changed

+62
-19
lines changed

9 files changed

+62
-19
lines changed

src/main/kotlin/app/Logger.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ object Logger {
9393
}
9494

9595
private fun configLevelValue() : Int {
96-
val a = mapOf("trace" to TRACE, "debug" to DEBUG, "info" to INFO, "warn" to WARN, "error" to ERROR)
96+
val a = mapOf("trace" to TRACE, "debug" to DEBUG, "info" to INFO,
97+
"warn" to WARN, "error" to ERROR)
9798
return a.getValue(BuildConfig.LOG_LEVEL)
9899
}
99100

src/main/kotlin/app/Main.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import app.utils.CommandConfig
1111
import app.utils.CommandAdd
1212
import app.utils.CommandList
1313
import app.utils.CommandRemove
14+
import app.utils.FileHelper.toPath
1415
import app.utils.Options
1516
import app.utils.PasswordHelper
1617
import app.utils.RepoHelper
@@ -75,9 +76,9 @@ class Main(argv: Array<String>) {
7576
}
7677

7778
private fun doAdd(commandAdd: CommandAdd) {
78-
val path = commandAdd.path
79+
val path = commandAdd.path?.toPath()
7980
if (path != null && RepoHelper.isValidRepo(path)) {
80-
val localRepo = LocalRepo(path)
81+
val localRepo = LocalRepo(path.toString())
8182
localRepo.hashAllContributors = commandAdd.hashAll
8283
configurator.addLocalRepoPersistent(localRepo)
8384
configurator.saveToFile()

src/main/kotlin/app/config/FileConfigurator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class FileConfigurator : Configurator {
212212

213213
try {
214214
loadConfig = Files.newBufferedReader(FileHelper
215-
.getPath(CONFIG_FILE_NAME)).use {
215+
.toPath(CONFIG_FILE_NAME)).use {
216216
mapper.readValue(it, Config::class.java)
217217
}
218218
} catch (e: IOException) {
@@ -241,7 +241,7 @@ class FileConfigurator : Configurator {
241241
*/
242242
override fun saveToFile() {
243243
try {
244-
Files.newBufferedWriter(FileHelper.getPath(CONFIG_FILE_NAME)).use {
244+
Files.newBufferedWriter(FileHelper.toPath(CONFIG_FILE_NAME)).use {
245245
mapper.writeValue(it, persistent)
246246
}
247247
} catch (e: IOException) {

src/main/kotlin/app/hashers/CodeLongevity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class CodeLongevity(private val serverRepo: Repo,
151151
catch(e: Exception) { throw Exception("No branch") }
152152

153153
val df = DiffFormatter(DisabledOutputStream.INSTANCE)
154-
val dataPath = FileHelper.getPath(serverRepo.rehash, "longevity")
154+
val dataPath = FileHelper.toPath(serverRepo.rehash, "longevity")
155155

156156
init {
157157
df.setRepository(repo)

src/main/kotlin/app/hashers/RepoHasher.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import app.config.Configurator
99
import app.model.Author
1010
import app.model.LocalRepo
1111
import app.model.Repo
12+
import app.utils.FileHelper.toPath
1213
import app.utils.HashingException
1314
import app.utils.RepoHelper
1415
import org.eclipse.jgit.api.Git
@@ -17,11 +18,11 @@ import java.io.IOException
1718
import kotlin.collections.HashSet
1819

1920
class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
20-
private val configurator: Configurator) {
21+
private val configurator: Configurator) {
2122
var serverRepo: Repo = Repo()
2223

2324
init {
24-
if (!RepoHelper.isValidRepo(localRepo.path)) {
25+
if (!RepoHelper.isValidRepo(localRepo.path.toPath())) {
2526
throw IllegalArgumentException("Invalid repo $localRepo")
2627
}
2728
}

src/main/kotlin/app/ui/AddRepoState.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import app.Logger
77
import app.api.Api
88
import app.config.Configurator
99
import app.model.LocalRepo
10+
import app.utils.FileHelper.toPath
1011
import app.utils.RepoHelper
1112
import app.utils.UiHelper
1213

@@ -28,12 +29,13 @@ class AddRepoState constructor(private val context: Context,
2829
if (configurator.getLocalRepos().isEmpty()) {
2930
Logger.print("Add at least one valid repository.")
3031
} else {
31-
break // User finished to add repos.
32+
break // User finished to add repos.
3233
}
3334
} else {
34-
if (RepoHelper.isValidRepo(pathString)) {
35-
Logger.print("Added git repository at $pathString.")
36-
val localRepo = LocalRepo(pathString)
35+
val path = pathString.toPath()
36+
if (RepoHelper.isValidRepo(path)) {
37+
Logger.print("Added git repository at $path.")
38+
val localRepo = LocalRepo(path.toString())
3739
localRepo.hashAllContributors = UiHelper.confirm("Do you "
3840
+ "want to hash commits of all contributors?",
3941
defaultIsYes = true)

src/main/kotlin/app/utils/FileHelper.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package app.utils
55

6+
import app.Logger
67
import java.io.File
78
import java.net.URLDecoder
89
import java.nio.file.Files
@@ -17,7 +18,7 @@ object FileHelper {
1718
private val jarPath = getJarPath()
1819
private val settingsPath = jarPath.resolve(dirName)
1920

20-
fun getPath(name: String, vararg parts: String): Path {
21+
fun toPath(name: String, vararg parts: String): Path {
2122
val path = settingsPath.resolve(Paths.get("", *parts))
2223
if (Files.notExists(path)) {
2324
Files.createDirectories(path)
@@ -26,11 +27,11 @@ object FileHelper {
2627
}
2728

2829
fun getFile(name: String, vararg parts: String): File {
29-
return getPath(name, *parts).toFile()
30+
return toPath(name, *parts).toFile()
3031
}
3132

3233
fun notExists(name:String, vararg parts: String): Boolean {
33-
return Files.notExists(getPath(name, *parts))
34+
return Files.notExists(toPath(name, *parts))
3435
}
3536

3637
fun getFileExtension(path: String): String {
@@ -47,4 +48,13 @@ object FileHelper {
4748
// Removing jar filename.
4849
return root.resolve(fullPath.subpath(0, fullPath.nameCount - 1))
4950
}
51+
52+
fun String.toPath(): Path {
53+
val substitutePath = if (this.startsWith("~" + File.separator)) {
54+
System.getProperty("user.home") + this.substring(1)
55+
} else { this }
56+
val pathTemp = Paths.get(substitutePath).toAbsolutePath().normalize()
57+
println(pathTemp.toString())
58+
return pathTemp
59+
}
5060
}

src/main/kotlin/app/utils/RepoHelper.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import org.eclipse.jgit.lib.ObjectId
1212
import org.eclipse.jgit.lib.Repository
1313
import java.io.File
1414
import java.nio.file.InvalidPathException
15+
import java.nio.file.Path
1516
import java.nio.file.Paths
1617

1718
/**
1819
* Class for utility functions on repos.
1920
*/
2021
object RepoHelper {
21-
fun isValidRepo(path: String): Boolean {
22+
fun isValidRepo(path: Path): Boolean {
2223
if (!isDirectory(path)) {
2324
return false
2425
}
@@ -27,7 +28,7 @@ object RepoHelper {
2728
var repository: Repository? = null
2829
val commitId: ObjectId?
2930
try {
30-
git = Git.open(File(path))
31+
git = Git.open(path.toFile())
3132
repository = git.repository
3233
commitId = CommitCrawler.getDefaultBranchHead(git)
3334
} catch (e: Exception) {
@@ -44,9 +45,9 @@ object RepoHelper {
4445
return false
4546
}
4647

47-
fun isDirectory(path: String): Boolean {
48+
fun isDirectory(path: Path): Boolean {
4849
return try {
49-
Paths.get(path).toFile().isDirectory
50+
path.toFile().isDirectory
5051
} catch (e: InvalidPathException) {
5152
Logger.error(e, "Invalid path")
5253
false
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 Sourcerer Inc. All Rights Reserved.
2+
// Author: Ryan Osilla ([email protected])
3+
4+
package test.tests.utils
5+
6+
import org.jetbrains.spek.api.Spek
7+
import org.jetbrains.spek.api.dsl.given
8+
import org.jetbrains.spek.api.dsl.it
9+
import kotlin.test.assertEquals
10+
import app.utils.FileHelper.toPath
11+
import java.nio.file.Paths
12+
13+
fun testPath(expectedPath: String, actualPath: String) {
14+
assertEquals(Paths.get(expectedPath), actualPath.toPath())
15+
}
16+
17+
class FileHelperTest : Spek({
18+
given("relative path test") {
19+
it("basic") {
20+
val home = System.getProperty("user.home")
21+
testPath("/Users/user/repo", "/Users/user/repo")
22+
testPath("/Users/user/repo", "/Users/user/../user/repo/../repo")
23+
testPath("$home/test", "~/test")
24+
testPath("$home/test1", "~/test/../test1")
25+
}
26+
}
27+
})

0 commit comments

Comments
 (0)