Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions src/main/kotlin/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
Main(argv)
}

class Main {
class Main(argv: Array<String>) {
private val configurator = FileConfigurator()
private val api = ServerApi(configurator)

constructor(argv: Array<String>) {
init {
val options = Options()
val commandAdd = CommandAdd()
val commandConfig = CommandConfig()
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
42 changes: 21 additions & 21 deletions src/main/kotlin/app/api/ServerApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/app/extractors/CSharpExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package app.extractors

import app.model.CommitStats
import app.model.DiffFile
import java.io.File

class CSharpExtractor : ExtractorInterface {
companion object {
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/app/extractors/JavaExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ 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",
"case", "enum", "instanceof", "return", "transient", "catch",
"extends", "int", "short", "try", "char", "final", "interface",
"static", "void", "class", "finally", "long", "strictfp",
"volatile", "const", "float", "native", "super", "while")
}

override fun extract(files: List<DiffFile>): List<CommitStats> {
files.map { file -> file.language = GoExtractor.LANGUAGE_NAME }
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/extractors/RubyExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
27 changes: 13 additions & 14 deletions src/main/kotlin/app/hashers/CodeLongevity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand All @@ -129,7 +129,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
val totals: MutableMap<String, Int> = emails.associate { Pair(it, 0) }
.toMutableMap()

var repoTotal: Int = 0
var repoTotal = 0
var repoSum: Long = 0
getLinesObservable().blockingSubscribe { line ->
repoTotal++
Expand Down Expand Up @@ -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<RevCommitLine>(fileText.size())
val lines = ArrayList<RevCommitLine>(fileText.size())
for (idx in 0 .. fileText.size() - 1) {
lines.add(RevCommitLine(head, fileId, path, idx, false))
}
Expand All @@ -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
}
Expand All @@ -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<RevCommitLine>(fileText.size()))
files.put(oldPath, ArrayList(fileText.size()))
}

// If a file was deleted, then the new path is /dev/null.
Expand All @@ -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()
Expand All @@ -286,7 +285,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
val delEnd = edit.getEndA()
Logger.debug("del ($delStart, $delEnd)")

var tmpLines = ArrayList<RevCommitLine>(delCount)
val tmpLines = ArrayList<RevCommitLine>(delCount)
for (idx in delStart .. delEnd - 1) {
tmpLines.add(RevCommitLine(commit, oldId,
oldPath, idx, true))
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/hashers/CommitCrawler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Commit> = Observable
.create<Commit> { subscriber ->
try {
val revWalk = RevWalk(git.repository)
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/app/hashers/FactHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class FactHasher(private val localRepo: LocalRepo,

val throwables = mutableListOf<Throwable>()

// TODO(anatoly): Filter hashing by email as in CommitHasher.
observable
.subscribe({ commit -> // OnNext.
// Calculate facts.
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/hashers/RepoHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/ui/AuthState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/main/kotlin/app/ui/CloseState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/app/ui/ConsoleUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/ui/UpdateRepoState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class UpdateRepoState constructor(private val context: Context,
}

override fun next() {
context.changeState(CloseState(context, api, configurator))
context.changeState(CloseState())
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/app/utils/RepoHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/app/utils/RequestException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading