Skip to content

Commit 133fa9f

Browse files
authored
fix: warnings and bugs (APP-99, APP-107, APP-108) (#39)
* fix: warnings (APP-99) * fix: missing command (APP-107) * fix: forked repo test (APP-108)
1 parent 8361499 commit 133fa9f

File tree

16 files changed

+90
-99
lines changed

16 files changed

+90
-99
lines changed

src/main/kotlin/app/Main.kt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ import app.utils.PasswordHelper
1616
import app.utils.RepoHelper
1717
import app.utils.UiHelper
1818
import com.beust.jcommander.JCommander
19+
import com.beust.jcommander.MissingCommandException
1920

2021
fun main(argv : Array<String>) {
2122
Main(argv)
2223
}
2324

24-
class Main {
25+
class Main(argv: Array<String>) {
2526
private val configurator = FileConfigurator()
2627
private val api = ServerApi(configurator)
2728

28-
constructor(argv: Array<String>) {
29+
init {
2930
val options = Options()
3031
val commandAdd = CommandAdd()
3132
val commandConfig = CommandConfig()
@@ -40,32 +41,29 @@ class Main {
4041
.addCommand(commandRemove.name, commandRemove)
4142
.build()
4243

43-
jc.parse(*argv)
44-
45-
options.password = PasswordHelper.hashPassword(options.password)
46-
configurator.setOptions(options)
47-
48-
if (options.help) {
49-
showHelp(jc)
50-
return
51-
}
52-
53-
if (options.setup) {
54-
doSetup()
55-
return
56-
}
57-
58-
when (jc.parsedCommand) {
59-
commandAdd.name -> doAdd(commandAdd)
60-
commandConfig.name -> doConfig(commandConfig)
61-
commandList.name -> doList(commandList)
62-
commandRemove.name -> doRemove(commandRemove)
63-
else -> startUi()
44+
try {
45+
jc.parse(*argv)
46+
options.password = PasswordHelper.hashPassword(options.password)
47+
configurator.setOptions(options)
48+
49+
if (options.help) {
50+
showHelp(jc)
51+
} else if (options.setup) {
52+
doSetup()
53+
} else when (jc.parsedCommand) {
54+
commandAdd.name -> doAdd(commandAdd)
55+
commandConfig.name -> doConfig(commandConfig)
56+
commandList.name -> doList()
57+
commandRemove.name -> doRemove(commandRemove)
58+
else -> startUi()
59+
}
60+
} catch (e: MissingCommandException) {
61+
Logger.error("No such command: ${e.unknownCommand}")
6462
}
6563
}
6664

6765
private fun startUi() {
68-
val consoleUi = ConsoleUi(api, configurator)
66+
ConsoleUi(api, configurator)
6967
}
7068

7169
private fun doAdd(commandAdd: CommandAdd) {
@@ -97,7 +95,7 @@ class Main {
9795
configurator.saveToFile()
9896
}
9997

100-
private fun doList(commandList: CommandList) {
98+
private fun doList() {
10199
RepoHelper.printRepos(configurator.getLocalRepos(),
102100
"Tracked repositories:",
103101
"No tracked repositories")

src/main/kotlin/app/api/ServerApi.kt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ import com.google.protobuf.InvalidProtocolBufferException
2121
import java.security.InvalidParameterException
2222

2323
class ServerApi (private val configurator: Configurator) : Api {
24-
private val HEADER_VERSION_CODE = "app-version-code"
25-
private val HEADER_CONTENT_TYPE = "Content-Type"
26-
private val HEADER_CONTENT_TYPE_PROTO = "application/octet-stream"
27-
private val HEADER_COOKIE = "Cookie"
28-
private val HEADER_SET_COOKIE = "Set-Cookie"
29-
private val KEY_TOKEN = "Token="
24+
companion object {
25+
private val HEADER_VERSION_CODE = "app-version-code"
26+
private val HEADER_CONTENT_TYPE = "Content-Type"
27+
private val HEADER_CONTENT_TYPE_PROTO = "application/octet-stream"
28+
private val HEADER_COOKIE = "Cookie"
29+
private val HEADER_SET_COOKIE = "Set-Cookie"
30+
private val KEY_TOKEN = "Token="
31+
}
3032

3133
private var token = ""
3234

33-
private fun cookieRequestInterceptor(): (Request) -> Request =
34-
{ request: Request ->
35-
if (token.isNotEmpty()) {
36-
request.header(Pair(HEADER_COOKIE, KEY_TOKEN + token))
37-
}
38-
request
35+
private fun cookieRequestInterceptor() = { req: Request ->
36+
if (token.isNotEmpty()) {
37+
req.header(Pair(HEADER_COOKIE, KEY_TOKEN + token))
3938
}
39+
req
40+
}
4041

41-
private fun cookieResponseInterceptor(): (Request, Response) -> Response =
42-
{ request: Request, response: Response ->
43-
val newToken = response.httpResponseHeaders[HEADER_SET_COOKIE]
44-
?.find { it.startsWith(KEY_TOKEN) }
45-
if (newToken != null && newToken.isNotBlank()) {
46-
token = newToken.substringAfter(KEY_TOKEN)
47-
.substringBefore(';')
48-
}
49-
response
42+
private fun cookieResponseInterceptor() = { _: Request, res: Response ->
43+
val newToken = res.httpResponseHeaders[HEADER_SET_COOKIE]
44+
?.find { it.startsWith(KEY_TOKEN) }
45+
if (newToken != null && newToken.isNotBlank()) {
46+
token = newToken.substringAfter(KEY_TOKEN)
47+
.substringBefore(';')
5048
}
49+
res
50+
}
5151

5252
init {
5353
val fuelManager = FuelManager.instance

src/main/kotlin/app/extractors/CSharpExtractor.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package app.extractors
66

77
import app.model.CommitStats
88
import app.model.DiffFile
9-
import java.io.File
109

1110
class CSharpExtractor : ExtractorInterface {
1211
companion object {

src/main/kotlin/app/extractors/JavaExtractor.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ package app.extractors
66

77
import app.model.CommitStats
88
import app.model.DiffFile
9-
import java.io.File
109

1110
class JavaExtractor : ExtractorInterface {
1211
companion object {
1312
val LANGUAGE_NAME = "java"
1413
val FILE_EXTS = listOf("java")
1514
val LIBRARIES = ExtractorInterface.getLibraries("java")
16-
}
17-
18-
val KEYWORDS = listOf("abstract", "continue", "for", "new", "switch",
15+
val KEYWORDS = listOf("abstract", "continue", "for", "new", "switch",
1916
"assert", "default", "goto", "package", "synchronized", "boolean",
2017
"do", "if", "private", "this", "break", "double", "implements",
2118
"protected", "throw", "byte", "else", "import", "public", "throws",
2219
"case", "enum", "instanceof", "return", "transient", "catch",
2320
"extends", "int", "short", "try", "char", "final", "interface",
2421
"static", "void", "class", "finally", "long", "strictfp",
2522
"volatile", "const", "float", "native", "super", "while")
23+
}
2624

2725
override fun extract(files: List<DiffFile>): List<CommitStats> {
2826
files.map { file -> file.language = GoExtractor.LANGUAGE_NAME }

src/main/kotlin/app/extractors/RubyExtractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RubyExtractor : ExtractorInterface {
2525
fileContent.forEach {
2626
val res = regex.find(it)
2727
if (res != null) {
28-
val lineLib = res.groupValues.last { it -> it != "" }
28+
val lineLib = res.groupValues.last { it != "" }
2929
imports.add(lineLib)
3030
}
3131
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class CodeLine(val repo: Repository,
6868
* The code line's age in seconds.
6969
*/
7070
val age : Long
71-
get() = (to.commit.getCommitTime() - from.commit.getCommitTime()).toLong()
71+
get() = (to.commit.commitTime - from.commit.commitTime).toLong()
7272

7373
/**
7474
* The code line text.
@@ -109,7 +109,7 @@ class CodeLine(val repo: Repository,
109109
class CodeLongevity(private val localRepo: LocalRepo,
110110
private val serverRepo: Repo,
111111
private val api: Api,
112-
private val git: Git) {
112+
git: Git) {
113113
val repo: Repository = git.repository
114114
val head: RevCommit =
115115
RevWalk(repo).parseCommit(repo.resolve(RepoHelper.MASTER_BRANCH))
@@ -129,7 +129,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
129129
val totals: MutableMap<String, Int> = emails.associate { Pair(it, 0) }
130130
.toMutableMap()
131131

132-
var repoTotal: Int = 0
132+
var repoTotal = 0
133133
var repoSum: Long = 0
134134
getLinesObservable().blockingSubscribe { line ->
135135
repoTotal++
@@ -207,7 +207,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
207207
val fileLoader = repo.open(fileId)
208208
if (!RawText.isBinary(fileLoader.openStream())) {
209209
val fileText = RawText(fileLoader.getBytes())
210-
var lines = ArrayList<RevCommitLine>(fileText.size())
210+
val lines = ArrayList<RevCommitLine>(fileText.size())
211211
for (idx in 0 .. fileText.size() - 1) {
212212
lines.add(RevCommitLine(head, fileId, path, idx, false))
213213
}
@@ -226,7 +226,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
226226
Logger.debug("old: '$oldPath', new: '$newPath'")
227227

228228
// Skip binary files.
229-
var fileId = if (newPath != DiffEntry.DEV_NULL) newId else oldId
229+
val fileId = if (newPath != DiffEntry.DEV_NULL) newId else oldId
230230
if (RawText.isBinary(repo.open(fileId).openStream())) {
231231
continue
232232
}
@@ -240,8 +240,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
240240
if (diff.changeType == DiffEntry.ChangeType.DELETE) {
241241
val fileLoader = repo.open(oldId)
242242
val fileText = RawText(fileLoader.getBytes())
243-
files.put(oldPath,
244-
ArrayList<RevCommitLine>(fileText.size()))
243+
files.put(oldPath, ArrayList(fileText.size()))
245244
}
246245

247246
// If a file was deleted, then the new path is /dev/null.
@@ -260,16 +259,16 @@ class CodeLongevity(private val localRepo: LocalRepo,
260259
// Insertion case: track the lines.
261260
val insCount = edit.getLengthB()
262261
if (insCount > 0) {
263-
var insStart = edit.getBeginB()
264-
var insEnd = edit.getEndB()
262+
val insStart = edit.getBeginB()
263+
val insEnd = edit.getEndB()
265264
Logger.debug("ins ($insStart, $insEnd)")
266265

267266
for (idx in insStart .. insEnd - 1) {
268267
val from = RevCommitLine(commit, newId,
269268
newPath, idx, false)
270-
var to = lines.get(idx)
269+
val to = lines.get(idx)
271270
val cl = CodeLine(repo, from, to)
272-
Logger.debug("Collected: ${cl.toString()}")
271+
Logger.debug("Collected: ${cl}")
273272
subscriber.onNext(cl)
274273
}
275274
lines.subList(insStart, insEnd).clear()
@@ -286,7 +285,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
286285
val delEnd = edit.getEndA()
287286
Logger.debug("del ($delStart, $delEnd)")
288287

289-
var tmpLines = ArrayList<RevCommitLine>(delCount)
288+
val tmpLines = ArrayList<RevCommitLine>(delCount)
290289
for (idx in delStart .. delEnd - 1) {
291290
tmpLines.add(RevCommitLine(commit, oldId,
292291
oldPath, idx, true))
@@ -320,7 +319,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
320319
val from = RevCommitLine(tail, fileId,
321320
filePath, idx, false)
322321
val cl = CodeLine(repo, from, lines[idx])
323-
Logger.debug("Collected (tail): ${cl.toString()}")
322+
Logger.debug("Collected (tail): $cl")
324323
subscriber.onNext(cl)
325324
}
326325
}
@@ -340,7 +339,7 @@ class CodeLongevity(private val localRepo: LocalRepo,
340339
val revWalk = RevWalk(repo)
341340
revWalk.markStart(head)
342341

343-
var commit: RevCommit? = revWalk.next() // move the walker to the head
342+
var commit: RevCommit? = revWalk.next() // Move the walker to the head.
344343
while (commit != null && commit != tail) {
345344
val parentCommit: RevCommit? = revWalk.next()
346345

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.eclipse.jgit.revwalk.RevWalk
2222
import org.eclipse.jgit.util.io.DisabledOutputStream
2323

2424
object CommitCrawler {
25-
fun getObservable(git: Git, repo: Repo) = Observable
25+
fun getObservable(git: Git, repo: Repo): Observable<Commit> = Observable
2626
.create<Commit> { subscriber ->
2727
try {
2828
val revWalk = RevWalk(git.repository)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class FactHasher(private val localRepo: LocalRepo,
3535

3636
val throwables = mutableListOf<Throwable>()
3737

38+
// TODO(anatoly): Filter hashing by email as in CommitHasher.
3839
observable
3940
.subscribe({ commit -> // OnNext.
4041
// Calculate facts.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
3030
println("Hashing $localRepo...")
3131
val git = loadGit(localRepo.path)
3232
try {
33-
val (rehashes, authors) = fetchRehashesAndAuthors(git)
33+
val (rehashes, _) = fetchRehashesAndAuthors(git)
3434

3535
localRepo.parseGitConfig(git.repository.config)
3636
if (localRepo.author.email.isBlank()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AuthState constructor(private val context: Context,
3535
if (!connectionError) {
3636
context.changeState(ListRepoState(context, api, configurator))
3737
} else {
38-
context.changeState(CloseState(context, api, configurator))
38+
context.changeState(CloseState())
3939
}
4040
}
4141

0 commit comments

Comments
 (0)