Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
feat: add line added and line deleted support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 24, 2022
1 parent b7811b1 commit 75c9696
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory
import java.io.File
import java.nio.charset.StandardCharsets


/**
* @param path repository location
* @param branch branch name, default is master
Expand Down Expand Up @@ -54,7 +55,7 @@ class JGitAdapter(private val cognitiveComplexityParser: CognitiveComplexityPars
private fun toChangeEntry(repository: Repository, revCommit: RevCommit, systemId: Long): List<ChangeEntry> {
val diffFormatter = DiffFormatter(DisabledOutputStream.INSTANCE).config(repository)
return diffFormatter.scan(getParent(revCommit)?.tree, revCommit.tree)
.map { d -> doConvertToChangeEntry(d, repository, revCommit, systemId) }
.map { d -> doConvertToChangeEntry(d, repository, revCommit, systemId, diffFormatter) }
}

private fun getParent(revCommit: RevCommit): RevCommit? {
Expand All @@ -68,11 +69,19 @@ class JGitAdapter(private val cognitiveComplexityParser: CognitiveComplexityPars
diffEntry: DiffEntry,
repository: Repository,
revCommit: RevCommit,
systemId: Long
systemId: Long,
df: DiffFormatter
): ChangeEntry {
try {
var linesDeleted = 0;
var linesAdded = 0;
for (edit in df.toFileHeader(diffEntry).toEditList()) {
linesDeleted += edit.endA - edit.beginA
linesAdded += edit.endB - edit.beginB
}

var classComplexity: Int = 0
if(this.language == "java") {
if (this.language == "java") {
classComplexity = cognitiveComplexityForJavaFile(diffEntry, repository, revCommit)
}
return ChangeEntry(
Expand All @@ -84,7 +93,10 @@ class JGitAdapter(private val cognitiveComplexityParser: CognitiveComplexityPars
systemId = systemId,
commitId = revCommit.name,
// for knowledge map
committer = revCommit.authorIdent.name
committer = revCommit.authorIdent.name,
lineAdded = linesAdded,
lineDeleted = linesDeleted,

)
} catch (ex: Exception) {
throw ex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ data class ChangeEntry(
@Sql("cognitive_complexity") val cognitiveComplexity: Int,
@Sql("change_mode") val changeMode: String,
@Sql("commit_id") val commitId: String,
@Sql("committer") val committer: String
@Sql("committer") val committer: String,
@Sql("line_added") val lineAdded: Int,
@Sql("line_deleted") val lineDeleted: Int
)

@Sql("scm_path_change_count")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ internal class JGitAdapterTest {

// this test would not pass in CI
@Test
@Disabled
internal fun should_calculate_self() {
val jGitAdapter = JGitAdapter(CognitiveComplexityParser(), "java")
val (_, changeEntries) = jGitAdapter.scan("../", repoId = "0", systemId = 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ internal class RunnerTest {
@Disabled
fun run() {
val file = File("output.sql")
// file.deleteOnExit();
// val calendar = Calendar.getInstance()
// calendar.set(2019, 1, 1)
// Runner().main(arrayOf("--git-path=/Users/ygdong/git/spring-framework", "--branch=master","--after=${calendar.timeInMillis}"))
Runner().main(arrayOf("--path=../", "--branch=master"))
assertTrue(file.exists())
}
Expand All @@ -23,11 +19,7 @@ internal class RunnerTest {
@Disabled
fun run_loc() {
val file = File("loc_output.sql")
// file.deleteOnExit();
// val calendar = Calendar.getInstance()
// calendar.set(2019, 1, 1)
// Runner().main(arrayOf("--git-path=/Users/ygdong/git/spring-framework", "--branch=master","--after=${calendar.timeInMillis}"))
Runner().main(arrayOf("--path=/Users/le.hu/workspace/dubbo-samples/dubbo-samples-configcenter/dubbo-samples-configcenter-xml", "--loc=true"))
Runner().main(arrayOf("--path=../", "--branch=master", "--loc=true"))
assertTrue(file.exists())
}
}

0 comments on commit 75c9696

Please sign in to comment.