Skip to content

Commit 14c33a8

Browse files
feat: add sending of meta data for cloud hashing, refactoring (#210)
* feat: add meta model * chore: fix indent size * wip: refactor code * chore: add more codes * chore: fix todo text * fix: hidding of commit details with flag
1 parent 3a53dbf commit 14c33a8

File tree

14 files changed

+163
-81
lines changed

14 files changed

+163
-81
lines changed

src/main/kotlin/app/Logger.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,12 @@ object Logger {
150150
private val commitDetailIndent = generateIndent(10) + "|" +
151151
generateIndent(8)
152152
fun printCommitDetail(message: String) {
153-
val messageTrim = if (message.length > 59) {
154-
message.substring(0, 56).plus("...")
155-
} else message
156-
println(commitDetailIndent + messageTrim)
153+
if (!SILENT) {
154+
val messageTrim = if (message.length > 59) {
155+
message.substring(0, 56).plus("...")
156+
} else message
157+
println(commitDetailIndent + messageTrim)
158+
}
157159
}
158160

159161
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ import app.model.User
1414
interface Api {
1515
companion object {
1616
val OUT_OF_DATE = 1
17+
val STATUS_CLONING = 80
1718
val PROCESS_STATUS_START = 100
1819
val PROCESS_STATUS_COMPLETE = 200
1920
val PROCESS_STATUS_FAIL = 1000
21+
val CODE_SUCCESS = 0
22+
val PROCESS_ERROR_TOO_MUCH_COMMITS = 4
23+
val PROCESS_ERROR_NO_COMMITS = 5
24+
val PROCESS_ERROR_PROCESSOR = 6
2025
val PROCESS_ERROR_EMPTY_REPO = 8
2126
}
2227

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

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,39 @@ import java.io.File
2020
import java.io.IOException
2121
import kotlin.collections.HashSet
2222

23-
class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
24-
private val configurator: Configurator,
25-
private val processEntryId: Int? = null) {
26-
var serverRepo: Repo = Repo()
23+
class RepoHasher(private val api: Api,
24+
private val configurator: Configurator) {
25+
fun update(localRepo: LocalRepo) {
26+
Logger.debug { "RepoHasher.update call: " + localRepo }
27+
val processEntryId = localRepo.processEntryId
2728

28-
init {
2929
if (!RepoHelper.isValidRepo(localRepo.path.toPath())) {
30+
// TODO(anatoly): Send empty repo.
3031
throw IllegalArgumentException("Invalid repo $localRepo")
3132
}
32-
}
3333

34-
fun update() {
35-
Logger.info { "Hashing of repo started" }
3634
val git = loadGit(localRepo.path)
3735
try {
36+
Logger.info { "Hashing of repo started" }
3837
updateProcess(processEntryId, Api.PROCESS_STATUS_START)
39-
val (rehashes, authors) = CommitCrawler.fetchRehashesAndAuthors(git)
4038

39+
val (rehashes, authors) = CommitCrawler.fetchRehashesAndAuthors(git)
4140
localRepo.parseGitConfig(git.repository.config)
42-
initServerRepo(rehashes.last)
43-
Logger.debug { "Local repo path: ${localRepo.path}" }
44-
Logger.debug { "Repo remote: ${localRepo.remoteOrigin}" }
45-
Logger.debug { "Repo rehash: ${serverRepo.rehash}" }
41+
val serverRepo = initServerRepo(localRepo, rehashes.last)
4642

4743
// Get repo setup (commits, emails to hash) from server.
48-
postRepoFromServer()
44+
postRepoFromServer(serverRepo)
4945

5046
// Send all repo emails for invites.
51-
postAuthorsToServer(authors)
47+
postAuthorsToServer(authors, serverRepo)
5248

49+
// Choose emails to filter commits with.
5350
val emails = authors.map { author -> author.email }.toHashSet()
54-
val filteredEmails = filterEmails(emails)
51+
val filteredEmails = if (localRepo.hashAllContributors) {
52+
emails
53+
} else {
54+
filterEmails(emails, serverRepo)
55+
}
5556

5657
// Common error handling for subscribers.
5758
// Exceptions can't be thrown out of reactive chain.
@@ -124,7 +125,7 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
124125
git.close()
125126
}
126127

127-
private fun postRepoFromServer() {
128+
private fun postRepoFromServer(serverRepo: Repo) {
128129
val repo = api.postRepo(serverRepo).getOrThrow()
129130
serverRepo.commits = repo.commits
130131
Logger.info {
@@ -133,24 +134,26 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
133134
Logger.debug { serverRepo.toString() }
134135
}
135136

136-
private fun postAuthorsToServer(authors: HashSet<Author>) {
137-
api.postAuthors(authors.map { author ->
138-
author.repo = serverRepo
139-
author
140-
}).onErrorThrow()
137+
private fun postAuthorsToServer(authors: HashSet<Author>,
138+
serverRepo: Repo) {
139+
authors.forEach { author -> author.repo = serverRepo }
140+
api.postAuthors(authors.toList()).onErrorThrow()
141141
}
142142

143-
private fun initServerRepo(initCommitRehash: String) {
144-
serverRepo = Repo(initialCommitRehash = initCommitRehash,
145-
rehash = RepoHelper.calculateRepoRehash(
146-
initCommitRehash, localRepo))
143+
private fun initServerRepo(localRepo: LocalRepo,
144+
initCommitRehash: String): Repo {
145+
val rehash = RepoHelper.calculateRepoRehash(initCommitRehash, localRepo)
146+
val repo = Repo(initialCommitRehash = initCommitRehash,
147+
rehash = rehash,
148+
meta = localRepo.meta)
149+
Logger.debug { "Local repo path: ${localRepo.path}" }
150+
Logger.debug { "Repo remote: ${localRepo.remoteOrigin}" }
151+
Logger.debug { "Repo rehash: $rehash" }
152+
return repo
147153
}
148154

149-
private fun filterEmails(emails: HashSet<String>): HashSet<String> {
150-
if (localRepo.hashAllContributors) {
151-
return emails
152-
}
153-
155+
private fun filterEmails(emails: HashSet<String>,
156+
serverRepo: Repo): HashSet<String> {
154157
val knownEmails = hashSetOf<String>()
155158
knownEmails.addAll(configurator.getUser().emails.map { it.email })
156159
knownEmails.addAll(serverRepo.emails)

src/main/kotlin/app/model/AuthorGroup.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ data class AuthorGroup(
2222

2323
fun getProto(): Protos.AuthorGroup {
2424
return Protos.AuthorGroup.newBuilder()
25-
.addAllAuthors(authors.map { it -> it.getProto() })
26-
.build()
25+
.addAllAuthors(authors.map { it -> it.getProto() })
26+
.build()
2727
}
2828

2929
fun serialize(): ByteArray {

src/main/kotlin/app/model/Commit.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ data class Commit(
6060

6161
fun getProto(): Protos.Commit {
6262
return Protos.Commit.newBuilder()
63-
.setRehash(rehash)
64-
.setRepoRehash(repo.rehash)
65-
.setTreeRehash(treeRehash)
66-
.setAuthorName(author.name)
67-
.setAuthorEmail(author.email)
68-
.setDate(dateTimestamp)
69-
.setIsQommit(isQommit)
70-
.setNumLinesAdded(numLinesAdded)
71-
.setNumLinesDeleted(numLinesDeleted)
72-
.addAllStats(stats.map { it.getProto() })
73-
.build()
63+
.setRehash(rehash)
64+
.setRepoRehash(repo.rehash)
65+
.setTreeRehash(treeRehash)
66+
.setAuthorName(author.name)
67+
.setAuthorEmail(author.email)
68+
.setDate(dateTimestamp)
69+
.setIsQommit(isQommit)
70+
.setNumLinesAdded(numLinesAdded)
71+
.setNumLinesDeleted(numLinesDeleted)
72+
.addAllStats(stats.map { it.getProto() })
73+
.build()
7474
}
7575

7676
fun serialize(): ByteArray {

src/main/kotlin/app/model/CommitGroup.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ data class CommitGroup(
2525

2626
fun getProto(): Protos.CommitGroup {
2727
return Protos.CommitGroup.newBuilder()
28-
.addAllCommits(commits.map { it -> it.getProto() })
29-
.build()
28+
.addAllCommits(commits.map { it -> it.getProto() })
29+
.build()
3030
}
3131

3232
fun serialize(): ByteArray {

src/main/kotlin/app/model/CommitStats.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ data class CommitStats(
3131

3232
fun getProto(): Protos.CommitStats {
3333
return Protos.CommitStats.newBuilder()
34-
.setNumLinesAdded(numLinesAdded)
35-
.setNumLinesDeleted(numLinesDeleted)
36-
.setType(type)
37-
.setTech(tech)
38-
.build()
34+
.setNumLinesAdded(numLinesAdded)
35+
.setNumLinesDeleted(numLinesDeleted)
36+
.setType(type)
37+
.setTech(tech)
38+
.build()
3939
}
4040

4141
fun serialize(): ByteArray {

src/main/kotlin/app/model/FactGroup.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ data class FactGroup(
2525

2626
fun getProto(): Protos.FactGroup {
2727
return Protos.FactGroup.newBuilder()
28-
.addAllFacts(stats.map { it -> it.getProto() })
29-
.build()
28+
.addAllFacts(stats.map { it -> it.getProto() })
29+
.build()
3030
}
3131

3232
fun serialize(): ByteArray {

src/main/kotlin/app/model/LocalRepo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ data class LocalRepo(var path: String = "") {
1111

1212
@JsonIgnore var author: Author = Author()
1313
@JsonIgnore var remoteOrigin: String = ""
14+
@JsonIgnore var meta = RepoMeta()
15+
@JsonIgnore var processEntryId: Int? = 0
1416

1517
fun parseGitConfig(config: Config) {
1618
author = Author(

src/main/kotlin/app/model/Repo.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ import java.security.InvalidParameterException
1111
* Repository.
1212
*/
1313
data class Repo(
14-
// Basic info.
15-
var rehash: String = "",
16-
var initialCommitRehash: String = "",
14+
// Basic info.
15+
var rehash: String = "",
16+
var initialCommitRehash: String = "",
1717

18-
// Authors' email filter for hashed commits. If empty list then hash
19-
// only commits that created by current user.
20-
var emails: List<String> = listOf(),
18+
// Authors' email filter for hashed commits. If empty list then hash
19+
// only commits that created by current user.
20+
var emails: List<String> = listOf(),
2121

22-
// Raw commits server history. Used to find overlap of commits.
23-
var commits: List<Commit> = listOf()
22+
// Raw commits server history. Used to find overlap of commits.
23+
var commits: List<Commit> = listOf(),
24+
25+
// Meta info about repo. Is not used for a locally run app.
26+
var meta: RepoMeta = RepoMeta()
2427
) {
2528
@Throws(InvalidParameterException::class)
2629
constructor(proto: Protos.Repo) : this() {
@@ -37,11 +40,12 @@ data class Repo(
3740

3841
fun getProto(): Protos.Repo {
3942
return Protos.Repo.newBuilder()
40-
.setRehash(rehash)
41-
.setInitialCommitRehash(rehash)
42-
.addAllEmails(emails)
43-
.addAllCommits(commits.map { it.getProto() })
44-
.build()
43+
.setRehash(rehash)
44+
.setInitialCommitRehash(rehash)
45+
.addAllEmails(emails)
46+
.addAllCommits(commits.map { it.getProto() })
47+
.setMeta(meta.getProto())
48+
.build()
4549
}
4650

4751
fun serialize(): ByteArray {

0 commit comments

Comments
 (0)