diff --git a/src/main/kotlin/app/Main.kt b/src/main/kotlin/app/Main.kt index b14c5572..88122558 100644 --- a/src/main/kotlin/app/Main.kt +++ b/src/main/kotlin/app/Main.kt @@ -16,16 +16,17 @@ import app.utils.PasswordHelper import app.utils.RepoHelper import app.utils.UiHelper import com.beust.jcommander.JCommander +import com.beust.jcommander.MissingCommandException fun main(argv : Array) { Main(argv) } -class Main { +class Main(argv: Array) { private val configurator = FileConfigurator() private val api = ServerApi(configurator) - constructor(argv: Array) { + init { val options = Options() val commandAdd = CommandAdd() val commandConfig = CommandConfig() @@ -40,32 +41,29 @@ class Main { .addCommand(commandRemove.name, commandRemove) .build() - jc.parse(*argv) - - options.password = PasswordHelper.hashPassword(options.password) - configurator.setOptions(options) - - if (options.help) { - showHelp(jc) - return - } - - if (options.setup) { - doSetup() - return - } - - when (jc.parsedCommand) { - commandAdd.name -> doAdd(commandAdd) - commandConfig.name -> doConfig(commandConfig) - commandList.name -> doList(commandList) - commandRemove.name -> doRemove(commandRemove) - else -> startUi() + try { + jc.parse(*argv) + options.password = PasswordHelper.hashPassword(options.password) + configurator.setOptions(options) + + if (options.help) { + showHelp(jc) + } else if (options.setup) { + doSetup() + } else when (jc.parsedCommand) { + commandAdd.name -> doAdd(commandAdd) + commandConfig.name -> doConfig(commandConfig) + commandList.name -> doList() + commandRemove.name -> doRemove(commandRemove) + else -> startUi() + } + } catch (e: MissingCommandException) { + Logger.error("No such command: ${e.unknownCommand}") } } private fun startUi() { - val consoleUi = ConsoleUi(api, configurator) + ConsoleUi(api, configurator) } private fun doAdd(commandAdd: CommandAdd) { @@ -97,7 +95,7 @@ class Main { configurator.saveToFile() } - private fun doList(commandList: CommandList) { + private fun doList() { RepoHelper.printRepos(configurator.getLocalRepos(), "Tracked repositories:", "No tracked repositories") diff --git a/src/main/kotlin/app/api/ServerApi.kt b/src/main/kotlin/app/api/ServerApi.kt index e8624013..a375b046 100644 --- a/src/main/kotlin/app/api/ServerApi.kt +++ b/src/main/kotlin/app/api/ServerApi.kt @@ -21,33 +21,33 @@ import com.google.protobuf.InvalidProtocolBufferException import java.security.InvalidParameterException class ServerApi (private val configurator: Configurator) : Api { - private val HEADER_VERSION_CODE = "app-version-code" - private val HEADER_CONTENT_TYPE = "Content-Type" - private val HEADER_CONTENT_TYPE_PROTO = "application/octet-stream" - private val HEADER_COOKIE = "Cookie" - private val HEADER_SET_COOKIE = "Set-Cookie" - private val KEY_TOKEN = "Token=" + companion object { + private val HEADER_VERSION_CODE = "app-version-code" + private val HEADER_CONTENT_TYPE = "Content-Type" + private val HEADER_CONTENT_TYPE_PROTO = "application/octet-stream" + private val HEADER_COOKIE = "Cookie" + private val HEADER_SET_COOKIE = "Set-Cookie" + private val KEY_TOKEN = "Token=" + } private var token = "" - private fun cookieRequestInterceptor(): (Request) -> Request = - { request: Request -> - if (token.isNotEmpty()) { - request.header(Pair(HEADER_COOKIE, KEY_TOKEN + token)) - } - request + private fun cookieRequestInterceptor() = { req: Request -> + if (token.isNotEmpty()) { + req.header(Pair(HEADER_COOKIE, KEY_TOKEN + token)) } + req + } - private fun cookieResponseInterceptor(): (Request, Response) -> Response = - { request: Request, response: Response -> - val newToken = response.httpResponseHeaders[HEADER_SET_COOKIE] - ?.find { it.startsWith(KEY_TOKEN) } - if (newToken != null && newToken.isNotBlank()) { - token = newToken.substringAfter(KEY_TOKEN) - .substringBefore(';') - } - response + private fun cookieResponseInterceptor() = { _: Request, res: Response -> + val newToken = res.httpResponseHeaders[HEADER_SET_COOKIE] + ?.find { it.startsWith(KEY_TOKEN) } + if (newToken != null && newToken.isNotBlank()) { + token = newToken.substringAfter(KEY_TOKEN) + .substringBefore(';') } + res + } init { val fuelManager = FuelManager.instance diff --git a/src/main/kotlin/app/extractors/CSharpExtractor.kt b/src/main/kotlin/app/extractors/CSharpExtractor.kt index 65f9d6ea..95395eba 100644 --- a/src/main/kotlin/app/extractors/CSharpExtractor.kt +++ b/src/main/kotlin/app/extractors/CSharpExtractor.kt @@ -6,7 +6,6 @@ package app.extractors import app.model.CommitStats import app.model.DiffFile -import java.io.File class CSharpExtractor : ExtractorInterface { companion object { diff --git a/src/main/kotlin/app/extractors/JavaExtractor.kt b/src/main/kotlin/app/extractors/JavaExtractor.kt index f36d5ead..f7a57e58 100644 --- a/src/main/kotlin/app/extractors/JavaExtractor.kt +++ b/src/main/kotlin/app/extractors/JavaExtractor.kt @@ -6,16 +6,13 @@ package app.extractors import app.model.CommitStats import app.model.DiffFile -import java.io.File class JavaExtractor : ExtractorInterface { companion object { val LANGUAGE_NAME = "java" val FILE_EXTS = listOf("java") val LIBRARIES = ExtractorInterface.getLibraries("java") - } - - val KEYWORDS = listOf("abstract", "continue", "for", "new", "switch", + val KEYWORDS = listOf("abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized", "boolean", "do", "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte", "else", "import", "public", "throws", @@ -23,6 +20,7 @@ class JavaExtractor : ExtractorInterface { "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while") + } override fun extract(files: List): List { files.map { file -> file.language = GoExtractor.LANGUAGE_NAME } diff --git a/src/main/kotlin/app/extractors/RubyExtractor.kt b/src/main/kotlin/app/extractors/RubyExtractor.kt index 5c9518df..95ec6a81 100644 --- a/src/main/kotlin/app/extractors/RubyExtractor.kt +++ b/src/main/kotlin/app/extractors/RubyExtractor.kt @@ -25,7 +25,7 @@ class RubyExtractor : ExtractorInterface { fileContent.forEach { val res = regex.find(it) if (res != null) { - val lineLib = res.groupValues.last { it -> it != "" } + val lineLib = res.groupValues.last { it != "" } imports.add(lineLib) } } diff --git a/src/main/kotlin/app/hashers/CodeLongevity.kt b/src/main/kotlin/app/hashers/CodeLongevity.kt index f359d29d..f1753eac 100644 --- a/src/main/kotlin/app/hashers/CodeLongevity.kt +++ b/src/main/kotlin/app/hashers/CodeLongevity.kt @@ -68,7 +68,7 @@ class CodeLine(val repo: Repository, * The code line's age in seconds. */ val age : Long - get() = (to.commit.getCommitTime() - from.commit.getCommitTime()).toLong() + get() = (to.commit.commitTime - from.commit.commitTime).toLong() /** * The code line text. @@ -109,7 +109,7 @@ class CodeLine(val repo: Repository, class CodeLongevity(private val localRepo: LocalRepo, private val serverRepo: Repo, private val api: Api, - private val git: Git) { + git: Git) { val repo: Repository = git.repository val head: RevCommit = RevWalk(repo).parseCommit(repo.resolve(RepoHelper.MASTER_BRANCH)) @@ -129,7 +129,7 @@ class CodeLongevity(private val localRepo: LocalRepo, val totals: MutableMap = emails.associate { Pair(it, 0) } .toMutableMap() - var repoTotal: Int = 0 + var repoTotal = 0 var repoSum: Long = 0 getLinesObservable().blockingSubscribe { line -> repoTotal++ @@ -207,7 +207,7 @@ class CodeLongevity(private val localRepo: LocalRepo, val fileLoader = repo.open(fileId) if (!RawText.isBinary(fileLoader.openStream())) { val fileText = RawText(fileLoader.getBytes()) - var lines = ArrayList(fileText.size()) + val lines = ArrayList(fileText.size()) for (idx in 0 .. fileText.size() - 1) { lines.add(RevCommitLine(head, fileId, path, idx, false)) } @@ -226,7 +226,7 @@ class CodeLongevity(private val localRepo: LocalRepo, Logger.debug("old: '$oldPath', new: '$newPath'") // Skip binary files. - var fileId = if (newPath != DiffEntry.DEV_NULL) newId else oldId + val fileId = if (newPath != DiffEntry.DEV_NULL) newId else oldId if (RawText.isBinary(repo.open(fileId).openStream())) { continue } @@ -240,8 +240,7 @@ class CodeLongevity(private val localRepo: LocalRepo, if (diff.changeType == DiffEntry.ChangeType.DELETE) { val fileLoader = repo.open(oldId) val fileText = RawText(fileLoader.getBytes()) - files.put(oldPath, - ArrayList(fileText.size())) + files.put(oldPath, ArrayList(fileText.size())) } // If a file was deleted, then the new path is /dev/null. @@ -260,16 +259,16 @@ class CodeLongevity(private val localRepo: LocalRepo, // Insertion case: track the lines. val insCount = edit.getLengthB() if (insCount > 0) { - var insStart = edit.getBeginB() - var insEnd = edit.getEndB() + val insStart = edit.getBeginB() + val insEnd = edit.getEndB() Logger.debug("ins ($insStart, $insEnd)") for (idx in insStart .. insEnd - 1) { val from = RevCommitLine(commit, newId, newPath, idx, false) - var to = lines.get(idx) + val to = lines.get(idx) val cl = CodeLine(repo, from, to) - Logger.debug("Collected: ${cl.toString()}") + Logger.debug("Collected: ${cl}") subscriber.onNext(cl) } lines.subList(insStart, insEnd).clear() @@ -286,7 +285,7 @@ class CodeLongevity(private val localRepo: LocalRepo, val delEnd = edit.getEndA() Logger.debug("del ($delStart, $delEnd)") - var tmpLines = ArrayList(delCount) + val tmpLines = ArrayList(delCount) for (idx in delStart .. delEnd - 1) { tmpLines.add(RevCommitLine(commit, oldId, oldPath, idx, true)) @@ -320,7 +319,7 @@ class CodeLongevity(private val localRepo: LocalRepo, val from = RevCommitLine(tail, fileId, filePath, idx, false) val cl = CodeLine(repo, from, lines[idx]) - Logger.debug("Collected (tail): ${cl.toString()}") + Logger.debug("Collected (tail): $cl") subscriber.onNext(cl) } } @@ -340,7 +339,7 @@ class CodeLongevity(private val localRepo: LocalRepo, val revWalk = RevWalk(repo) revWalk.markStart(head) - var commit: RevCommit? = revWalk.next() // move the walker to the head + var commit: RevCommit? = revWalk.next() // Move the walker to the head. while (commit != null && commit != tail) { val parentCommit: RevCommit? = revWalk.next() diff --git a/src/main/kotlin/app/hashers/CommitCrawler.kt b/src/main/kotlin/app/hashers/CommitCrawler.kt index d6dbf329..db738c67 100644 --- a/src/main/kotlin/app/hashers/CommitCrawler.kt +++ b/src/main/kotlin/app/hashers/CommitCrawler.kt @@ -22,7 +22,7 @@ import org.eclipse.jgit.revwalk.RevWalk import org.eclipse.jgit.util.io.DisabledOutputStream object CommitCrawler { - fun getObservable(git: Git, repo: Repo) = Observable + fun getObservable(git: Git, repo: Repo): Observable = Observable .create { subscriber -> try { val revWalk = RevWalk(git.repository) diff --git a/src/main/kotlin/app/hashers/FactHasher.kt b/src/main/kotlin/app/hashers/FactHasher.kt index 0154f70e..08de2450 100644 --- a/src/main/kotlin/app/hashers/FactHasher.kt +++ b/src/main/kotlin/app/hashers/FactHasher.kt @@ -35,6 +35,7 @@ class FactHasher(private val localRepo: LocalRepo, val throwables = mutableListOf() + // TODO(anatoly): Filter hashing by email as in CommitHasher. observable .subscribe({ commit -> // OnNext. // Calculate facts. diff --git a/src/main/kotlin/app/hashers/RepoHasher.kt b/src/main/kotlin/app/hashers/RepoHasher.kt index 6123dfcc..19dc2e18 100644 --- a/src/main/kotlin/app/hashers/RepoHasher.kt +++ b/src/main/kotlin/app/hashers/RepoHasher.kt @@ -30,7 +30,7 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, println("Hashing $localRepo...") val git = loadGit(localRepo.path) try { - val (rehashes, authors) = fetchRehashesAndAuthors(git) + val (rehashes, _) = fetchRehashesAndAuthors(git) localRepo.parseGitConfig(git.repository.config) if (localRepo.author.email.isBlank()) { diff --git a/src/main/kotlin/app/ui/AuthState.kt b/src/main/kotlin/app/ui/AuthState.kt index e5f77455..a68f2ccf 100644 --- a/src/main/kotlin/app/ui/AuthState.kt +++ b/src/main/kotlin/app/ui/AuthState.kt @@ -35,7 +35,7 @@ class AuthState constructor(private val context: Context, if (!connectionError) { context.changeState(ListRepoState(context, api, configurator)) } else { - context.changeState(CloseState(context, api, configurator)) + context.changeState(CloseState()) } } diff --git a/src/main/kotlin/app/ui/CloseState.kt b/src/main/kotlin/app/ui/CloseState.kt index 879bb73e..6d91f4f7 100644 --- a/src/main/kotlin/app/ui/CloseState.kt +++ b/src/main/kotlin/app/ui/CloseState.kt @@ -3,16 +3,10 @@ package app.ui -import app.api.Api -import app.config.Configurator - /** * On application close console UI state. */ -class CloseState constructor(private val context: Context, - private val api: Api, - private val configurator: Configurator) - : ConsoleState { +class CloseState : ConsoleState { override fun doAction() { println("You could use console commands to control repositories. To " + "setup again run application with flag --setup. For more " diff --git a/src/main/kotlin/app/ui/ConsoleUi.kt b/src/main/kotlin/app/ui/ConsoleUi.kt index 8ed9cd98..8792d677 100644 --- a/src/main/kotlin/app/ui/ConsoleUi.kt +++ b/src/main/kotlin/app/ui/ConsoleUi.kt @@ -9,8 +9,8 @@ import app.config.Configurator /** * Console user interface. */ -class ConsoleUi(private val api: Api, - private val configurator: Configurator) : Context { +class ConsoleUi(api: Api, + configurator: Configurator) : Context { var state: ConsoleState = OpenState(this, api, configurator) init { diff --git a/src/main/kotlin/app/ui/UpdateRepoState.kt b/src/main/kotlin/app/ui/UpdateRepoState.kt index c4a45859..58feeede 100644 --- a/src/main/kotlin/app/ui/UpdateRepoState.kt +++ b/src/main/kotlin/app/ui/UpdateRepoState.kt @@ -32,6 +32,6 @@ class UpdateRepoState constructor(private val context: Context, } override fun next() { - context.changeState(CloseState(context, api, configurator)) + context.changeState(CloseState()) } } diff --git a/src/main/kotlin/app/utils/RepoHelper.kt b/src/main/kotlin/app/utils/RepoHelper.kt index 33d3a584..ff87f00b 100644 --- a/src/main/kotlin/app/utils/RepoHelper.kt +++ b/src/main/kotlin/app/utils/RepoHelper.kt @@ -26,7 +26,7 @@ object RepoHelper { var git: Git? = null var repository: Repository? = null - var commitId: ObjectId? = null + val commitId: ObjectId? try { git = Git.open(File(path)) repository = git.repository diff --git a/src/main/kotlin/app/utils/RequestException.kt b/src/main/kotlin/app/utils/RequestException.kt index d088aa94..7a6f57db 100644 --- a/src/main/kotlin/app/utils/RequestException.kt +++ b/src/main/kotlin/app/utils/RequestException.kt @@ -8,8 +8,7 @@ import com.google.protobuf.InvalidProtocolBufferException import java.nio.charset.Charset import java.security.InvalidParameterException -class RequestException(val exception: Exception) - : Exception(exception.message) { +class RequestException(exception: Exception) : Exception(exception.message) { private val AUTH_ERROR_CODES = listOf(401, 403) var httpStatusCode: Int = 0 diff --git a/src/test/kotlin/test/tests/hashers/CommitHasherTest.kt b/src/test/kotlin/test/tests/hashers/CommitHasherTest.kt index 875d2798..4405500d 100644 --- a/src/test/kotlin/test/tests/hashers/CommitHasherTest.kt +++ b/src/test/kotlin/test/tests/hashers/CommitHasherTest.kt @@ -20,10 +20,29 @@ import kotlin.test.assertEquals import kotlin.test.assertNotEquals class CommitHasherTest : Spek({ + fun getRepoRehash(git: Git, localRepo: LocalRepo): String { + + val initialRevCommit = stream(git.log().call().spliterator(), false) + .toList().first() + return RepoHelper.calculateRepoRehash(Commit(initialRevCommit).rehash, + localRepo) + } + + fun getLastCommit(git: Git): Commit { + val revCommits = stream(git.log().call().spliterator(), false).toList() + val lastCommit = Commit(revCommits.first()) + return lastCommit + } + + fun cleanRepos() { + Runtime.getRuntime().exec("src/test/delete_repo.sh").waitFor() + } + val userName = "Contributor" val userEmail = "test@domain.com" // Creation of test repo. + cleanRepos() val repoPath = "./tmp_repo/.git" val git = Git.init().setGitDir(File(repoPath)).call() val config = git.repository.config @@ -41,22 +60,6 @@ class CommitHasherTest : Spek({ val repo = Repo(rehash = repoRehash, initialCommitRehash = initialCommit.rehash) - fun getRepoRehash(git: Git, localRepo: LocalRepo): String { - - val initialRevCommit = stream(git.log().call().spliterator(), false) - .toList().first() - val initialCommit = Commit(initialRevCommit) - val repoRehash = RepoHelper.calculateRepoRehash(initialCommit.rehash, - localRepo) - return repoRehash - } - - fun getLastCommit(git: Git): Commit { - val revCommits = stream(git.log().call().spliterator(), false).toList() - val lastCommit = Commit(revCommits.first()) - return lastCommit - } - given("repo with initial commit and no history") { repo.commits = listOf() @@ -210,5 +213,5 @@ class CommitHasherTest : Spek({ } } - Runtime.getRuntime().exec("src/test/delete_repo.sh").waitFor() + cleanRepos() })