From 5b6de6bd42c2df512de067c6e6bfd12e5f661bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=BChn?= Date: Tue, 10 Nov 2026 09:00:11 +0100 Subject: [PATCH 1/2] feat(analysis): write who made each commit and its type into commit changes gitlogparser writes each commit's author, co-authors, type and hotfix flag into commit changes, under the same invented names as author ownership, or the real names with --with-author-names. dependencyparser reads a Kotlin class after annotations with arguments as the class it is. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YRW3WswmkYHuNdjyiNBTJX --- analysis/CHANGELOG.md | 10 ++ .../analysers/parsers/GitLogParser/README.md | 22 ++- .../parsers/gitlog/RepoScanCommand.kt | 11 +- .../parsers/gitlog/authors/AuthorHistory.kt | 52 +++++++ .../AuthorPseudonyms.kt | 13 +- .../gitlog/commitchanges/CommitChangesLens.kt | 51 ++++-- .../gitlog/commitchanges/CommitChangesScan.kt | 36 +++-- .../metrics/DefaultSemanticCommitStyle.kt | 2 + .../gitlog/input/metrics/HotfixCommits.kt | 8 +- .../gitlog/ownership/AuthorOwnershipLens.kt | 16 +- .../gitlog/ownership/AuthorOwnershipScan.kt | 44 +----- .../AuthorPseudonymsTest.kt | 2 +- .../commitchanges/CommitChangesLensTest.kt | 99 +++++++++++- .../commitchanges/CommitChangesScanTest.kt | 147 ++++++++++++++++-- .../metrics/DefaultSemanticCommitStyleTest.kt | 12 ++ .../gitlog/input/metrics/HotfixCommitsTest.kt | 12 ++ .../ownership/AuthorOwnershipLensTest.kt | 2 + .../ownership/AuthorOwnershipScanTest.kt | 1 + .../analysers/tools/analyse/AnalyseTool.kt | 5 +- analysis/test/golden_test.sh | 14 +- .../kotlin/extractors/DeclarationExtractor.kt | 24 ++- .../kotlin/extractors/KotlinScopes.kt | 8 +- .../kotlin/extractors/MisparsedClasses.kt | 45 ++++++ .../languages/kotlin/KotlinDependencyTest.kt | 54 +++++++ .../src/content/docs/docs/analysis/analyse.md | 12 +- .../src/content/docs/docs/parser/git-log.md | 25 ++- .../expected.txt | 8 + .../src/zoo/Keeper.kt | 3 + .../src/zoo/Shift.kt | 5 + .../src/zoo/Watch.kt | 13 ++ 30 files changed, 634 insertions(+), 122 deletions(-) create mode 100644 analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorHistory.kt rename analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/{ownership => authors}/AuthorPseudonyms.kt (80%) rename analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/{ownership => authors}/AuthorPseudonymsTest.kt (97%) create mode 100644 analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/MisparsedClasses.kt create mode 100644 sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/expected.txt create mode 100644 sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Keeper.kt create mode 100644 sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Shift.kt create mode 100644 sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Watch.kt diff --git a/analysis/CHANGELOG.md b/analysis/CHANGELOG.md index f53764f348c..3ef3aca5047 100644 --- a/analysis/CHANGELOG.md +++ b/analysis/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/) ## [unreleased] (Added 🚀 | Changed | Removed | Fixed 🐞 | Chore 👨‍💻 👩‍💻) +### Added 🚀 + +- **Who made each commit**: commit changes also hold each commit's type, hotfix flag, author and co-authors, under the + same invented names as in author ownership, or the real names with `--with-author-names`. + +### Fixed 🐞 + +- **Kotlin classes after annotations with arguments**: `dependencyparser` reads a class written after several + annotations, one of them with arguments such as `@Timeout(120)`, as the class it is instead of losing it. + ## [2.4.0] - 2026-11-10 ### Added 🚀 diff --git a/analysis/analysers/parsers/GitLogParser/README.md b/analysis/analysers/parsers/GitLogParser/README.md index 41f35b38b9c..0097adc578d 100644 --- a/analysis/analysers/parsers/GitLogParser/README.md +++ b/analysis/analysers/parsers/GitLogParser/README.md @@ -43,7 +43,7 @@ The names are pseudonymous, not anonymous: anyone with the repository can match still be personal data under GDPR. Treat and share it accordingly. `--with-author-names`, next to `--author-ownership`, opts out of this: the map then holds real names as git has them, under -a clustering titled **Author ownership (real names)**, and still no emails. The command warns about it last, even with +a clustering titled **Author ownership (real names)** and in the commit changes, and still no emails. The command warns about it last, even with `--silent`, and the Explorer's Clusters tab shows a warning for such a map. @@ -58,11 +58,17 @@ git 2.37 or newer. empty file counts as wholly changed. - Merge commits are left out, as their commits are listed themselves. Changes follow a renamed file to its path today, and a pure rename changes nothing. A commit with no file left today is not listed. -- Each commit also carries its date and how many files and lines it changed in all, including files gone today. - -Commit subjects enter the map, and anyone with the repository can match a hash to its author, so a map may still be -personal data under GDPR. The command says so last. On a shallow clone the oldest commit counts every file as changed, -and the command warns about it. +- Each commit also carries its date, its author, its co-authors and how many files and lines it changed in all, + including files gone today. The author is the one git records, and the co-authors are everyone else its + `Co-authored-by` trailers name, in their order, each once and joined by `; `, as in `Teal Falcon; Jade Owl`, both after + applying `.mailmap`. A commit without co-authors carries none. Everyone appears under the same invented name the + author ownership gives them, whether or not the map has author ownership. With `--with-author-names`, next to + `--author-ownership`, they appear under their real names. + +The invented names are pseudonymous, not anonymous: anyone with the repository can match them and the hashes to people, +and commit subjects are free text, so a map may still be personal data under GDPR. The command says so last. On a +shallow clone the oldest commit counts every file as changed and the invented names are not stable, and the command +warns about it. ## Usage and Parameters As this parser can generate metrics based on a given git-log file or given a git repository (where the git-log is then generated on the fly), some parameters are only available in the log-scan mode or the repo-scan mode. @@ -73,8 +79,8 @@ As this parser can generate metrics based on a given git-log file or given a git | `repo-scan` | use repo-scan mode - generates cc.json from an automatically generated git-log file | | `--add-author` | add an array of authors to every file | | `--author-ownership` | add each author's share of each file under an invented name, from `git blame` (repo-scan only) | -| `--with-author-names` | write real author names instead of numbers (needs `--author-ownership`) | -| `--commit-changes-since YYYY-MM-DD` | add what each commit since this day changed in each file (repo-scan only) | +| `--with-author-names` | write real author names instead of invented ones, also for commit changes (needs `--author-ownership`) | +| `--commit-changes-since YYYY-MM-DD` | add what each commit since this day changed in each file, and who made it (repo-scan only) | | `--git-log=FILE` | git-log file to parse (only available for log-scan mode!) | | `-h, --help` | displays help and exits | | `-nc, --not-compressed` | save uncompressed output File | diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/RepoScanCommand.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/RepoScanCommand.kt index 96c20287f74..5d1905d8858 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/RepoScanCommand.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/RepoScanCommand.kt @@ -4,6 +4,7 @@ import de.maibornwolff.codecharta.analysers.analyserinterface.AnalyserDialogInte import de.maibornwolff.codecharta.analysers.analyserinterface.AnalyserInterface import de.maibornwolff.codecharta.analysers.analyserinterface.commit.CommitAnalysisContext import de.maibornwolff.codecharta.analysers.analyserinterface.commit.GitWorktreeManager +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorHistory import de.maibornwolff.codecharta.analysers.parsers.gitlog.commitchanges.CommitChangesScan import de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership.AuthorOwnershipScan import de.maibornwolff.codecharta.analysers.parsers.gitlog.util.GitAdapter @@ -61,7 +62,10 @@ class RepoScanCommand(private val printsClusterHints: Boolean = true) : Analyser @CommandLine.Option( names = [AuthorOwnershipScan.NAMES_FLAG], - description = ["write the real names of the authors into the map instead (personal data, needs --author-ownership)"] + description = [ + "write the real names of the authors into the map instead, also for commit changes " + + "(personal data, needs ${AuthorOwnershipScan.FLAG})" + ] ) private var withAuthorNames = false @@ -101,11 +105,12 @@ class RepoScanCommand(private val printsClusterHints: Boolean = true) : Analyser if (!silent) println("Creating git-ls file...") val gitLsFile = createGitLsFile(context.inputDir.toPath()) - val ownershipScan = AuthorOwnershipScan(context.inputDir, withAuthorNames).takeIf { authorOwnership } + val authorHistory = AuthorHistory(context.inputDir) + val ownershipScan = AuthorOwnershipScan(context.inputDir, withAuthorNames, authorHistory).takeIf { authorOwnership } if (!silent && ownershipScan != null) println("Blaming files...") val ownership = ownershipScan?.lens() ?: ClustersLens() - val commitChangesScan = commitChangesSince?.let { CommitChangesScan(context.inputDir, it) } + val commitChangesScan = commitChangesSince?.let { CommitChangesScan(context.inputDir, it, withAuthorNames, authorHistory) } if (!silent && commitChangesScan != null) println("Reading the changes of each commit...") val commitChanges = commitChangesScan?.lens() ?: ClustersLens() diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorHistory.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorHistory.kt new file mode 100644 index 00000000000..4c969322d3a --- /dev/null +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorHistory.kt @@ -0,0 +1,52 @@ +package de.maibornwolff.codecharta.analysers.parsers.gitlog.authors + +import de.maibornwolff.codecharta.analysers.parsers.gitlog.util.GitCommand +import java.io.File + +/** A commit's author and co-authors, the author first. */ +internal data class CommitPeople(val hash: String, val people: List) + +/** Reads who made each commit of the repository's history, with the mailmap applied, and reads it only once. */ +class AuthorHistory(repositoryFolder: File) { + private val git = GitCommand(repositoryFolder) + + internal val commitsOldestFirst: List by lazy { readCommitsOldestFirst() } + + private fun readCommitsOldestFirst(): List { + val format = "--format=%H%x00%aN%x00%(trailers:key=Co-authored-by,valueonly,separator=%x01)" + val rows = git + .outputOrNull("-c", "i18n.logOutputEncoding=UTF-8", "log", "--reverse", "--topo-order", format) { output -> + output.bufferedReader(Charsets.UTF_8).useLines { lines -> lines.map { it.split(NUL) }.toList() } + }.orEmpty() + val nameByCoAuthor = mappedNames(rows.flatMap { coAuthorsIn(it) }.distinct()) + return rows.map { row -> + CommitPeople(row[0], (listOf(row[1]) + coAuthorsIn(row).map { nameByCoAuthor.getValue(it) }).distinct()) + } + } + + private fun coAuthorsIn(logRow: List): List = + logRow[2].split(TRAILER_SEPARATOR).map { it.trim() }.filter { it.isNotEmpty() } + + // Git applies the mailmap to a commit's author but not to its trailers, so the co-authors are mapped separately + private fun mappedNames(coAuthors: List): Map { + val withEmail = coAuthors.filter { CONTACT_WITH_EMAIL.matches(it) } + val input = File.createTempFile("co-authors", ".txt") + try { + input.writeText(withEmail.joinToString("\n", postfix = "\n"), Charsets.UTF_8) + val mapped = git.outputOrNull("check-mailmap", "--stdin", input = input) { it.bufferedReader(Charsets.UTF_8).readLines() } + val contacts = if (mapped?.size == withEmail.size) withEmail.zip(mapped).toMap() else emptyMap() + return coAuthors.associateWith { nameOf(contacts[it] ?: it) } + } finally { + if (!input.delete()) input.deleteOnExit() + } + } + + private fun nameOf(contact: String): String = contact.substringBefore(EMAIL_START).trim() + + companion object { + private const val NUL = "\u0000" + private const val TRAILER_SEPARATOR = "\u0001" + private const val EMAIL_START = "<" + private val CONTACT_WITH_EMAIL = Regex(".+<.+>") + } +} diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorPseudonyms.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorPseudonyms.kt similarity index 80% rename from analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorPseudonyms.kt rename to analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorPseudonyms.kt index d653ddbe79c..45fe28740ea 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorPseudonyms.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorPseudonyms.kt @@ -1,4 +1,4 @@ -package de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership +package de.maibornwolff.codecharta.analysers.parsers.gitlog.authors import java.util.Random @@ -26,6 +26,17 @@ internal class AuthorPseudonyms(firstCommitHash: String) { private fun String.capitalised() = replaceFirstChar { it.uppercase() } companion object { + /** The pseudonym of everyone in [commitsOldestFirst], by their position in the order of first commit. */ + fun byPerson(commitsOldestFirst: List): Map { + val firstCommitHash = commitsOldestFirst.firstOrNull()?.hash ?: return emptyMap() + val pseudonyms = AuthorPseudonyms(firstCommitHash) + return commitsOldestFirst + .flatMap { it.people } + .distinct() + .withIndex() + .associate { (position, person) -> person to pseudonyms.nameOf(position) } + } + val COLOURS = listOf( "amber", "apricot", "aqua", "azure", "bronze", "burgundy", "cerise", "cerulean", "claret", "cobalt", "copper", "coral", "crimson", "cyan", "denim", "emerald", "fern", "flax", "fuchsia", "garnet", "gold", "heather", "indigo", "iris", "jade", diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLens.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLens.kt index 057232c75b3..b3072fe3d97 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLens.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLens.kt @@ -1,5 +1,7 @@ package de.maibornwolff.codecharta.analysers.parsers.gitlog.commitchanges +import de.maibornwolff.codecharta.analysers.parsers.gitlog.input.metrics.DefaultSemanticCommitStyle +import de.maibornwolff.codecharta.analysers.parsers.gitlog.input.metrics.HotfixCommits import de.maibornwolff.codecharta.model.AttributeDescriptor import de.maibornwolff.codecharta.model.AttributeType import de.maibornwolff.codecharta.model.Cluster @@ -24,20 +26,37 @@ internal object CommitChangesLens { private const val SHORT_HASH_LENGTH = 7 private const val PATH_SEPARATOR = "/" private const val DATE_ATTRIBUTE = "date" + private const val AUTHOR_ATTRIBUTE = "author" + private const val CO_AUTHORS_ATTRIBUTE = "co_authors" + private const val CO_AUTHOR_SEPARATOR = "; " private const val CHANGED_FILES_ATTRIBUTE = "changed_files" private const val CHANGED_LINES_ATTRIBUTE = "changed_lines" + private const val COMMIT_TYPE_ATTRIBUTE = "commit_type" + private const val HOTFIX_ATTRIBUTE = "hotfix" + private const val IS_HOTFIX = 1 private const val WEIGHT_STEPS_PER_UNIT = 10_000.0 private const val SMALLEST_WEIGHT = 1 / WEIGHT_STEPS_PER_UNIT private val ATTRIBUTE_TYPES = mapOf( CHANGED_FILES_ATTRIBUTE to AttributeType.ABSOLUTE, - CHANGED_LINES_ATTRIBUTE to AttributeType.ABSOLUTE + CHANGED_LINES_ATTRIBUTE to AttributeType.ABSOLUTE, + HOTFIX_ATTRIBUTE to AttributeType.ABSOLUTE ) private val ATTRIBUTE_DESCRIPTORS = mapOf( DATE_ATTRIBUTE to descriptor("Date", "Day the commit was made, in UTC"), + AUTHOR_ATTRIBUTE to + descriptor("Author", "Who made the commit, under an invented name unless real names were asked for"), + CO_AUTHORS_ATTRIBUTE to + descriptor("Co-authors", "Who the commit names as co-authors, under invented names unless real names were asked for"), CHANGED_FILES_ATTRIBUTE to descriptor("Files changed", "Files the whole commit changed, including those gone today"), CHANGED_LINES_ATTRIBUTE to - descriptor("Lines changed", "Lines the whole commit added and removed, including those of files gone today") + descriptor("Lines changed", "Lines the whole commit added and removed, including those of files gone today"), + COMMIT_TYPE_ATTRIBUTE to + descriptor( + "Commit type", + "Type the subject starts with (feat, fix, docs, style, refactor, test), as the *_commits metrics count" + ), + HOTFIX_ATTRIBUTE to descriptor("Hotfix", "1 when the subject names a hotfix, as the hotfix_commits metric counts it") ) private fun descriptor(title: String, description: String) = AttributeDescriptor( @@ -46,15 +65,22 @@ internal object CommitChangesLens { analyzers = setOf(ANALYZER) ) - /** [linesByPathToday] holds every file at HEAD, with null for a binary one. */ - fun of(commitsNewestFirst: List, linesByPathToday: Map): ClustersLens { + /** + * [linesByPathToday] holds every file at HEAD, with null for a binary one, and [peopleByHash] the names to write for each + * commit, its author first and then its co-authors. + */ + fun of( + commitsNewestFirst: List, + linesByPathToday: Map, + peopleByHash: Map> + ): ClustersLens { val pathTodayByOldPath = HashMap() val clusters = commitsNewestFirst.mapNotNull { commit -> val members = membersOf(commit, pathTodayByOldPath, linesByPathToday) commit.changes.forEach { change -> change.pathBefore?.let { pathTodayByOldPath[it] = pathTodayByOldPath[change.path] ?: change.path } } - if (members.isEmpty()) null else clusterOf(commit, members) + if (members.isEmpty()) null else clusterOf(commit, members, peopleByHash[commit.hash].orEmpty()) } if (clusters.isEmpty()) return ClustersLens() val clustering = Clustering( @@ -93,17 +119,24 @@ internal object CommitChangesLens { return max(SMALLEST_WEIGHT, floor(cappedShare * WEIGHT_STEPS_PER_UNIT) / WEIGHT_STEPS_PER_UNIT) } - private fun clusterOf(commit: LoggedCommit, members: List): Cluster = Cluster( + private fun clusterOf(commit: LoggedCommit, members: List, people: List): Cluster = Cluster( id = commit.hash, name = "${commit.hash.take(SHORT_HASH_LENGTH)} ${commit.subject}".trim(), members = members, - attributes = mapOf( + attributes = listOfNotNull( DATE_ATTRIBUTE to dateOf(commit), + people.firstOrNull()?.let { AUTHOR_ATTRIBUTE to it }, + coAuthorsOf(people)?.let { CO_AUTHORS_ATTRIBUTE to it }, CHANGED_FILES_ATTRIBUTE to commit.changes.size, - CHANGED_LINES_ATTRIBUTE to commit.changes.sumOf { it.changedLines ?: 0 } - ) + CHANGED_LINES_ATTRIBUTE to commit.changes.sumOf { it.changedLines ?: 0 }, + DefaultSemanticCommitStyle.typeOf(commit.subject)?.let { COMMIT_TYPE_ATTRIBUTE to it.name }, + (HOTFIX_ATTRIBUTE to IS_HOTFIX).takeIf { HotfixCommits.isHotfix(commit.subject) } + ).toMap() ) + private fun coAuthorsOf(people: List): String? = + people.distinct().drop(1).takeIf { it.isNotEmpty() }?.joinToString(CO_AUTHOR_SEPARATOR) + private fun dateOf(commit: LoggedCommit): String = Instant.ofEpochSecond(commit.committedAtEpochSecond).atOffset(ZoneOffset.UTC).toLocalDate().toString() } diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScan.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScan.kt index 22eed97457b..cc7c69f021d 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScan.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScan.kt @@ -1,5 +1,7 @@ package de.maibornwolff.codecharta.analysers.parsers.gitlog.commitchanges +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorHistory +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorPseudonyms import de.maibornwolff.codecharta.analysers.parsers.gitlog.util.GitCommand import de.maibornwolff.codecharta.model.ClustersLens import java.io.File @@ -7,11 +9,16 @@ import java.time.LocalDate import java.time.ZoneOffset import java.time.format.DateTimeParseException -/** Measures what each non-merge commit made since a day changed in the files of today. */ -class CommitChangesScan(repositoryFolder: File, private val since: LocalDate) { +/** Measures what each non-merge commit made since a day changed in the files of today, and who made it with whom. */ +class CommitChangesScan( + repositoryFolder: File, + private val since: LocalDate, + private val withAuthorNames: Boolean = false, + private val history: AuthorHistory = AuthorHistory(repositoryFolder) +) { private val git = GitCommand(repositoryFolder) - fun lens(): ClustersLens = CommitChangesLens.of(commitsNewestFirst(), linesByPathToday()) + fun lens(): ClustersLens = CommitChangesLens.of(commitsNewestFirst(), linesByPathToday(), peopleByHash()) fun isShallowClone(): Boolean = git.isShallowClone() @@ -35,6 +42,13 @@ class CommitChangesScan(repositoryFolder: File, private val since: LocalDate) { return CommitLogOutput.parse(output).filter { it.committedAtEpochSecond >= sinceEpochSecond } } + private fun peopleByHash(): Map> { + val commits = history.commitsOldestFirst + if (withAuthorNames) return commits.associate { it.hash to it.people } + val pseudonymByPerson = AuthorPseudonyms.byPerson(commits) + return commits.associate { commit -> commit.hash to commit.people.map { pseudonymByPerson.getValue(it) } } + } + private fun linesByPathToday(): Map = git.numstatOfHead().associate { row -> val (added, _, path) = row.split(TAB, limit = NUMSTAT_FIELD_COUNT) path to added.toIntOrNull() @@ -44,21 +58,23 @@ class CommitChangesScan(repositoryFolder: File, private val since: LocalDate) { const val FLAG = "--commit-changes-since" const val DATE_LABEL = "YYYY-MM-DD" const val OPTION_DESCRIPTION = - "adds one cluster per commit since this day (UTC) with what it changed in each file; " + - "subjects enter the map (needs the git history)" + "adds one cluster per commit since this day (UTC) with what it changed in each file and who made it, " + + "under an invented name; subjects enter the map (needs the git history)" const val DIALOG_QUESTION = - "Add what each commit since a day changed? Their subjects enter the map, and a hash leads to its author." + "Add what each commit since a day changed and who made it? Their subjects and authors enter the map, " + + "the authors under invented names unless you chose real ones." const val DIALOG_HINT = "$DATE_LABEL (leave empty for no)" const val DIALOG_INVALID_DATE = "Please enter a date as $DATE_LABEL." const val NEEDS_REPOSITORY_HINT = "$FLAG needs the repository to read each commit's changes, which a log file cannot provide. " + "Use 'gitlogparser repo-scan $FLAG' instead." const val PERSONAL_DATA_NOTE = - "Note: commit subjects are free text and anyone with the repository can match a hash to its author, " + - "so the map may still be personal data under GDPR." + "Note: each commit in this map names its author and co-authors, under invented names unless real names were " + + "asked for, and its subject is free text. Invented names are pseudonymous, not anonymous: anyone with the " + + "repository can match them to people, so the map may still be personal data under GDPR." const val SHALLOW_CLONE_WARNING = - "Warning: this repository is a shallow clone, so its oldest commit counts every file as changed. " + - "Fetch the full history to get its real changes." + "Warning: this repository is a shallow clone, so its oldest commit counts every file as changed " + + "and its invented author names are not stable. Fetch the full history to get its real changes and stable names." const val GIT_TOO_OLD_ERROR = "git could not list the commits for $FLAG, which needs git 2.37 or newer." private const val TAB = "\t" diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyle.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyle.kt index ac4c9add861..f33301918ab 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyle.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyle.kt @@ -41,4 +41,6 @@ object DefaultSemanticCommitStyle { ) fun getAllTypes(): List = defaultCommitTypes + + fun typeOf(message: String): SemanticCommitType? = defaultCommitTypes.firstOrNull { it.matchPattern.matches(message) } } diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommits.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommits.kt index 23c2c1c42c0..468aa4e1ad2 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommits.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommits.kt @@ -10,10 +10,16 @@ class HotfixCommits : Metric { override fun metricName(): String = "hotfix_commits" override fun registerCommit(commit: Commit) { - if (commit.message.contains("hotfix", ignoreCase = true)) { + if (isHotfix(commit.message)) { count++ } } override fun value(): Number = count + + companion object { + private const val HOTFIX = "hotfix" + + fun isHotfix(message: String): Boolean = message.contains(HOTFIX, ignoreCase = true) + } } diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLens.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLens.kt index 410f186af94..20f48f891ca 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLens.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLens.kt @@ -1,5 +1,7 @@ package de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorPseudonyms +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.CommitPeople import de.maibornwolff.codecharta.model.Cluster import de.maibornwolff.codecharta.model.ClusterMember import de.maibornwolff.codecharta.model.ClusterMembership @@ -9,9 +11,6 @@ import de.maibornwolff.codecharta.model.NodeId import de.maibornwolff.codecharta.model.NodeType import java.math.BigInteger -/** A commit's author and co-authors, the author first. */ -internal data class CommitPeople(val hash: String, val people: List) - /** Writes authors by pseudonym, unless real names were asked for; an email or per-author commit data never reaches the map. */ internal object AuthorOwnershipLens { private const val CLUSTERING_KEY = "author-ownership" @@ -37,12 +36,11 @@ internal object AuthorOwnershipLens { membersByPerson.getOrPut(person) { mutableListOf() }.add(ClusterMember(nodeId, weight)) } } - val pseudonyms = AuthorPseudonyms(firstCommitHash) // Sorted by id, as the order of first commits would otherwise show through the pseudonyms - val clusters = peopleInOrderOfFirstCommit(commitsOldestFirst) - .mapIndexedNotNull { position, person -> + val clusters = AuthorPseudonyms + .byPerson(commitsOldestFirst) + .mapNotNull { (person, pseudonym) -> membersByPerson[person]?.let { members -> - val pseudonym = pseudonyms.nameOf(position) clusterOf(firstCommitHash, pseudonym, if (withNames) person else pseudonym, members) } }.sortedBy { it.id } @@ -71,10 +69,6 @@ internal object AuthorOwnershipLens { .filterValues { it > 0.0 } } - // Someone with no surviving line still takes up a position, so the pseudonyms stay put as lines come and go - private fun peopleInOrderOfFirstCommit(commitsOldestFirst: List): List = - commitsOldestFirst.flatMap { it.people }.distinct() - private fun clusterOf(firstCommitHash: String, pseudonym: String, name: String, members: List): Cluster = Cluster( id = "${firstCommitHash.take(REPOSITORY_MARKER_LENGTH)}-author-${pseudonym.lowercase().replace(' ', '-')}", name = name, diff --git a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScan.kt b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScan.kt index e69091d4e08..38ff102b501 100644 --- a/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScan.kt +++ b/analysis/analysers/parsers/GitLogParser/src/main/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScan.kt @@ -1,5 +1,6 @@ package de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorHistory import de.maibornwolff.codecharta.analysers.parsers.gitlog.util.GitCommand import de.maibornwolff.codecharta.model.ClustersLens import de.maibornwolff.codecharta.util.Logger @@ -8,44 +9,17 @@ import java.util.concurrent.Callable import java.util.concurrent.Executors /** Measures who made the commits that last changed the lines each file holds today, and gives them pseudonyms by their first commit. */ -class AuthorOwnershipScan(private val repositoryFolder: File, private val withNames: Boolean = false) { +class AuthorOwnershipScan( + private val repositoryFolder: File, + private val withNames: Boolean = false, + private val history: AuthorHistory = AuthorHistory(repositoryFolder) +) { private val git = GitCommand(repositoryFolder) - fun lens(): ClustersLens = AuthorOwnershipLens.of(commitsOldestFirst(), blameByPath(), withNames) + fun lens(): ClustersLens = AuthorOwnershipLens.of(history.commitsOldestFirst, blameByPath(), withNames) fun isShallowClone(): Boolean = git.isShallowClone() - private fun commitsOldestFirst(): List { - val format = "--format=%H%x00%aN%x00%(trailers:key=Co-authored-by,valueonly,separator=%x01)" - val rows = git - .outputOrNull("-c", "i18n.logOutputEncoding=UTF-8", "log", "--reverse", "--topo-order", format) { output -> - output.bufferedReader(Charsets.UTF_8).useLines { lines -> lines.map { it.split(NUL) }.toList() } - }.orEmpty() - val nameByCoAuthor = mappedNames(rows.flatMap { coAuthorsIn(it) }.distinct()) - return rows.map { row -> - CommitPeople(row[0], (listOf(row[1]) + coAuthorsIn(row).map { nameByCoAuthor.getValue(it) }).distinct()) - } - } - - private fun coAuthorsIn(logRow: List): List = - logRow[2].split(TRAILER_SEPARATOR).map { it.trim() }.filter { it.isNotEmpty() } - - // Git applies the mailmap to a commit's author but not to its trailers, so the co-authors are mapped separately - private fun mappedNames(coAuthors: List): Map { - val withEmail = coAuthors.filter { CONTACT_WITH_EMAIL.matches(it) } - val input = File.createTempFile("co-authors", ".txt") - try { - input.writeText(withEmail.joinToString("\n", postfix = "\n"), Charsets.UTF_8) - val mapped = git.outputOrNull("check-mailmap", "--stdin", input = input) { it.bufferedReader(Charsets.UTF_8).readLines() } - val contacts = if (mapped?.size == withEmail.size) withEmail.zip(mapped).toMap() else emptyMap() - return coAuthors.associateWith { nameOf(contacts[it] ?: it) } - } finally { - if (!input.delete()) input.deleteOnExit() - } - } - - private fun nameOf(contact: String): String = contact.substringBefore(EMAIL_START).trim() - private fun blameByPath(): Map { val binaryPaths = binaryPaths() val paths = trackedPaths().filterNot { it in binaryPaths } @@ -106,11 +80,7 @@ class AuthorOwnershipScan(private val repositoryFolder: File, private val withNa const val SHALLOW_CLONE_WARNING = "Warning: this repository is a shallow clone, so its author names are not stable. Fetch the full history to keep them stable." - private const val NUL = "\u0000" private const val TAB = "\t" - private const val TRAILER_SEPARATOR = "\u0001" - private const val EMAIL_START = "<" - private val CONTACT_WITH_EMAIL = Regex(".+<.+>") private const val BLOB_TYPE = "blob" private const val BINARY_NUMSTAT_PREFIX = "-\t-\t" private const val IGNORE_REVISIONS_FILE = ".git-blame-ignore-revs" diff --git a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorPseudonymsTest.kt b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorPseudonymsTest.kt similarity index 97% rename from analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorPseudonymsTest.kt rename to analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorPseudonymsTest.kt index 73667703578..de7ad622906 100644 --- a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorPseudonymsTest.kt +++ b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/authors/AuthorPseudonymsTest.kt @@ -1,4 +1,4 @@ -package de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership +package de.maibornwolff.codecharta.analysers.parsers.gitlog.authors import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test diff --git a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLensTest.kt b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLensTest.kt index 874f8fa7e45..69e1fea0716 100644 --- a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLensTest.kt +++ b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesLensTest.kt @@ -14,8 +14,11 @@ class CommitChangesLensTest { private fun commit(vararg changes: FileChange, subject: String = "Fix login", hash: String = this.hash) = LoggedCommit(hash, noon20261031, subject, changes.toList()) - private fun clusteringOf(commits: List, linesToday: Map): Clustering = - CommitChangesLens.of(commits, linesToday).clusterings.getValue(CommitChangesLens.CLUSTERING_KEY) + private fun clusteringOf( + commits: List, + linesToday: Map, + peopleByHash: Map> = commits.associate { it.hash to listOf("Magenta Rabbit", "Teal Falcon") } + ): Clustering = CommitChangesLens.of(commits, linesToday, peopleByHash).clusterings.getValue(CommitChangesLens.CLUSTERING_KEY) private fun idOf(path: String) = NodeId.fromSegments(path.split("/")) @@ -102,7 +105,7 @@ class CommitChangesLensTest { val commits = listOf(commit(FileChange("new.kt", 0, pathBefore = "old.kt"))) // Act - val lens = CommitChangesLens.of(commits, mapOf("new.kt" to 10)) + val lens = CommitChangesLens.of(commits, mapOf("new.kt" to 10), emptyMap()) // Assert assertThat(lens.carriesData).isFalse() @@ -139,22 +142,106 @@ class CommitChangesLensTest { } @Test - fun `should describe every attribute it writes`() { + fun `should write the author of each commit`() { + // Arrange + val commits = listOf(commit(FileChange("a.kt", 1))) + + // Act + val clustering = clusteringOf(commits, mapOf("a.kt" to 10), mapOf(hash to listOf("Ochre Elk"))) + + // Assert + assertThat(clustering.clusters.single().attributes).containsEntry("author", "Ochre Elk") + } + + @Test + fun `should write the co-authors of a commit in their order, joined by semicolons`() { + // Arrange + val commits = listOf(commit(FileChange("a.kt", 1))) + + // Act + val clustering = clusteringOf(commits, mapOf("a.kt" to 10), mapOf(hash to listOf("Ochre Elk", "Teal Falcon", "Jade Owl"))) + + // Assert + assertThat(clustering.clusters.single().attributes) + .containsEntry("author", "Ochre Elk") + .containsEntry("co_authors", "Teal Falcon; Jade Owl") + } + + @Test + fun `should name the author and each co-author only once`() { + // Arrange + val commits = listOf(commit(FileChange("a.kt", 1))) + val people = listOf("Ochre Elk", "Teal Falcon", "Ochre Elk", "Jade Owl", "Teal Falcon") + + // Act + val clustering = clusteringOf(commits, mapOf("a.kt" to 10), mapOf(hash to people)) + + // Assert + assertThat(clustering.clusters.single().attributes).containsEntry("co_authors", "Teal Falcon; Jade Owl") + } + + @Test + fun `should leave out the co-authors of a commit that has none`() { + // Arrange + val commits = listOf(commit(FileChange("a.kt", 1))) + + // Act + val clustering = clusteringOf(commits, mapOf("a.kt" to 10), mapOf(hash to listOf("Ochre Elk"))) + + // Assert + assertThat(clustering.clusters.single().attributes).doesNotContainKey("co_authors").containsEntry("author", "Ochre Elk") + } + + @Test + fun `should leave out the author of a commit the history does not hold`() { // Arrange val commits = listOf(commit(FileChange("a.kt", 1))) + // Act + val clustering = clusteringOf(commits, mapOf("a.kt" to 10), emptyMap()) + + // Assert + assertThat(clustering.clusters.single().attributes) + .doesNotContainKeys("author", "co_authors") + .containsEntry("changed_files", 1) + } + + @Test + fun `should describe every attribute it writes`() { + // Arrange + val commits = listOf(commit(FileChange("a.kt", 1), subject = "fix: hotfix for the login")) + // Act val clustering = clusteringOf(commits, mapOf("a.kt" to 10)) // Assert assertThat(clustering.attributeDescriptors.keys).isEqualTo(clustering.clusters.single().attributes.keys) - assertThat(clustering.attributeDescriptors.values.map { it.title }).containsExactly("Date", "Files changed", "Lines changed") + assertThat( + clustering.attributeDescriptors.values.map { it.title } + ).containsExactly("Date", "Author", "Co-authors", "Files changed", "Lines changed", "Commit type", "Hotfix") + } + + @Test + fun `should write the type the subject starts with and flag a hotfix as the commit metrics count them`() { + // Arrange + val commits = listOf( + commit(FileChange("a.kt", 1), subject = "feat(login): remember the user", hash = "f1"), + commit(FileChange("a.kt", 1), subject = "Hotfix the broken login", hash = "f2") + ) + + // Act + val clustering = clusteringOf(commits, mapOf("a.kt" to 10)) + + // Assert + val (feature, hotfix) = clustering.clusters + assertThat(feature.attributes).containsEntry("commit_type", "feat").doesNotContainKey("hotfix") + assertThat(hotfix.attributes).containsEntry("hotfix", 1).doesNotContainKey("commit_type") } @Test fun `should write nothing when no commit touched a file of today`() { // Arrange & Act - val lens = CommitChangesLens.of(emptyList(), mapOf("a.kt" to 10)) + val lens = CommitChangesLens.of(emptyList(), mapOf("a.kt" to 10), emptyMap()) // Assert assertThat(lens.clusterings).isEmpty() diff --git a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScanTest.kt b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScanTest.kt index 8093593b97c..083641e2619 100644 --- a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScanTest.kt +++ b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/commitchanges/CommitChangesScanTest.kt @@ -1,5 +1,7 @@ package de.maibornwolff.codecharta.analysers.parsers.gitlog.commitchanges +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorPseudonyms +import de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership.AuthorOwnershipScan import de.maibornwolff.codecharta.model.Cluster import de.maibornwolff.codecharta.model.NodeId import org.assertj.core.api.Assertions.assertThat @@ -22,36 +24,55 @@ class CommitChangesScanTest { git("config", "commit.gpgsign", "false") } - private fun git(vararg arguments: String, committedAt: String = "2026-06-01T12:00:00Z"): String { - val process = ProcessBuilder(listOf("git", "-c", "user.name=Ann", "-c", "user.email=a@x.org") + arguments) + private fun git(vararg arguments: String, committedAt: String = "2026-06-01T12:00:00Z", author: String = ANN): String { + val process = ProcessBuilder(listOf("git", "-c", "user.name=Committer", "-c", "user.email=c@x.org") + arguments) .directory(repository) .redirectErrorStream(true) process.environment()["GIT_COMMITTER_DATE"] = committedAt process.environment()["GIT_AUTHOR_DATE"] = committedAt + process.environment()["GIT_AUTHOR_NAME"] = author.substringBefore(" <") + process.environment()["GIT_AUTHOR_EMAIL"] = author.substringAfter("<").removeSuffix(">") val running = process.start() val output = running.inputStream.bufferedReader().readText() check(running.waitFor() == 0) { output } return output.trim() } - private fun commitFile(path: String, content: String, message: String = "change", committedAt: String = "2026-06-01T12:00:00Z") { + private fun commitFile( + path: String, + content: String, + message: String = "change", + committedAt: String = "2026-06-01T12:00:00Z", + author: String = ANN + ) { File(repository, path).apply { parentFile.mkdirs() writeText(content) } - commitAll(message, committedAt) + commitAll(message, committedAt, author) } - private fun commitAll(message: String, committedAt: String = "2026-06-01T12:00:00Z") { + private fun commitAll(message: String, committedAt: String = "2026-06-01T12:00:00Z", author: String = ANN) { git("add", "-A") - git("commit", "-q", "-m", message, committedAt = committedAt) + git("commit", "-q", "-m", message, committedAt = committedAt, author = author) } - private fun clusters(from: File = repository, sinceDay: LocalDate = since): List = CommitChangesScan(from, sinceDay) - .lens() - .clusterings[CommitChangesLens.CLUSTERING_KEY] - ?.clusters - .orEmpty() + private fun clusters(from: File = repository, sinceDay: LocalDate = since, withAuthorNames: Boolean = false): List = + CommitChangesScan(from, sinceDay, withAuthorNames) + .lens() + .clusterings[CommitChangesLens.CLUSTERING_KEY] + ?.clusters + .orEmpty() + + private fun authors(withAuthorNames: Boolean = false): List = clusters(withAuthorNames = withAuthorNames).map { + it.attributes["author"] + } + + private fun coAuthors(withAuthorNames: Boolean = false): List = clusters(withAuthorNames = withAuthorNames).map { + it.attributes["co_authors"] + } + + private fun pseudonym(position: Int) = AuthorPseudonyms(git("rev-list", "--max-parents=0", "HEAD")).nameOf(position) private fun idOf(vararg segments: String) = NodeId.fromSegments(segments.toList()) @@ -76,6 +97,103 @@ class CommitChangesScanTest { .containsEntry("changed_lines", 2) } + @Test + fun `should name each author by the pseudonym the author ownership gives them, counting people before the day`() { + // Arrange + commitFile("z.kt", "1\n", committedAt = "2025-06-01T12:00:00Z", author = ZED) + commitFile("a.kt", "1\n2\n3\n4\n") + commitFile("a.kt", "1\n2\n3\nfour\n", author = BOB) + val ownership = AuthorOwnershipScan(repository) + .lens() + .clusterings.values + .single() + .clusters + val ownerOfOneQuarter = ownership.single { it.members.single().weight == 0.25 }.name + val ownerOfThreeQuarters = ownership.single { it.members.single().weight == 0.75 }.name + + // Act + val authors = authors() + + // Assert + assertThat(authors).containsExactly(ownerOfOneQuarter, ownerOfThreeQuarters) + assertThat(authors).containsExactly(pseudonym(2), pseudonym(1)) + } + + @Test + fun `should name the author of a commit, not its co-author`() { + // Arrange + commitFile("a.kt", "1\n", message = "change\n\nCo-authored-by: Bob ") + + // Act + val pseudonymous = authors() + val named = authors(withAuthorNames = true) + + // Assert + assertThat(pseudonymous).containsExactly(pseudonym(0)) + assertThat(named).containsExactly("Ann") + } + + @Test + fun `should name each co-author by the pseudonym the author ownership gives them`() { + // Arrange + commitFile("a.kt", "1\n", message = "change\n\nCo-authored-by: Bob ") + val owners = AuthorOwnershipScan(repository) + .lens() + .clusterings.values + .single() + .clusters + .map { it.name } + + // Act + val coAuthors = coAuthors() + + // Assert + assertThat(coAuthors).containsExactly(pseudonym(1)) + assertThat(owners).containsExactlyInAnyOrder(pseudonym(0), pseudonym(1)) + } + + @Test + fun `should name the co-authors in the order git lists them, without the author and without repeats`() { + // Arrange + val trailers = listOf(CY, ANN, BOB, CY).joinToString("\n") { "Co-authored-by: $it" } + commitFile("a.kt", "1\n", message = "change\n\n$trailers") + + // Act + val pseudonymous = coAuthors() + val named = coAuthors(withAuthorNames = true) + + // Assert + assertThat(pseudonymous).containsExactly("${pseudonym(1)}; ${pseudonym(2)}") + assertThat(named).containsExactly("Cy; Bob") + } + + @Test + fun `should write no co-authors for a commit that has none`() { + // Arrange + commitFile("a.kt", "1\n") + + // Act + val attributes = clusters().single().attributes + + // Assert + assertThat(attributes).doesNotContainKey("co_authors").containsKey("author") + } + + @Test + fun `should name two identities a mailmap joins as one author`() { + // Arrange + commitFile(".mailmap", "Ann Old Ann \n") + commitFile("a.kt", "old\n", author = "Old Ann ") + + // Act + val pseudonymous = authors() + val named = authors(withAuthorNames = true) + + // Assert + assertThat(pseudonymous).containsOnly(pseudonym(0)) + assertThat(named).containsOnly("Ann") + } + @Test fun `should leave out commits before the day, counted in UTC whatever the commit's own offset`() { // Arrange @@ -204,4 +322,11 @@ class CommitChangesScanTest { assertThat(CommitChangesScan.isDate("2026-02-31")).isFalse() assertThat(CommitChangesScan.isDate("6 months ago")).isFalse() } + + companion object { + private const val ANN = "Ann " + private const val BOB = "Bob " + private const val ZED = "Zed " + private const val CY = "Cy " + } } diff --git a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyleTest.kt b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyleTest.kt index ca98d56345c..c026887fd7b 100644 --- a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyleTest.kt +++ b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/DefaultSemanticCommitStyleTest.kt @@ -80,4 +80,16 @@ class DefaultSemanticCommitStyleTest { // Assert assertThat(matches).isEqualTo(expectedMatch) } + + @Test + fun `should give the first type a message starts with and none for another message`() { + // Arrange + val messages = listOf("fix(login): retry", "Refactor the parser", "Update readme") + + // Act + val types = messages.map { DefaultSemanticCommitStyle.typeOf(it)?.name } + + // Assert + assertThat(types).containsExactly("fix", "refactor", null) + } } diff --git a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommitsTest.kt b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommitsTest.kt index 0c5695123d6..e204ab9f75b 100644 --- a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommitsTest.kt +++ b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/input/metrics/HotfixCommitsTest.kt @@ -6,6 +6,18 @@ import org.junit.jupiter.api.Test import java.time.OffsetDateTime class HotfixCommitsTest { + @Test + fun `should tell a hotfix by the word anywhere in the message, in any case`() { + // Arrange + val messages = listOf("HOTFIX: login", "apply hotfix", "fix login") + + // Act + val results = messages.map { HotfixCommits.isHotfix(it) } + + // Assert + assertThat(results).containsExactly(true, true, false) + } + @Test fun should_have_initial_value_zero() { // Arrange diff --git a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLensTest.kt b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLensTest.kt index 18ca7fec933..c7599d930c4 100644 --- a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLensTest.kt +++ b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipLensTest.kt @@ -1,5 +1,7 @@ package de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorPseudonyms +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.CommitPeople import de.maibornwolff.codecharta.model.ClusterMember import de.maibornwolff.codecharta.model.ClusterMembership import de.maibornwolff.codecharta.model.NodeId diff --git a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScanTest.kt b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScanTest.kt index 5624fd57e56..f3d6795ee5d 100644 --- a/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScanTest.kt +++ b/analysis/analysers/parsers/GitLogParser/src/test/kotlin/de/maibornwolff/codecharta/analysers/parsers/gitlog/ownership/AuthorOwnershipScanTest.kt @@ -1,5 +1,6 @@ package de.maibornwolff.codecharta.analysers.parsers.gitlog.ownership +import de.maibornwolff.codecharta.analysers.parsers.gitlog.authors.AuthorPseudonyms import de.maibornwolff.codecharta.model.ClusterMember import de.maibornwolff.codecharta.model.NodeId import org.assertj.core.api.Assertions.assertThat diff --git a/analysis/analysers/tools/AnalyseTool/src/main/kotlin/de/maibornwolff/codecharta/analysers/tools/analyse/AnalyseTool.kt b/analysis/analysers/tools/AnalyseTool/src/main/kotlin/de/maibornwolff/codecharta/analysers/tools/analyse/AnalyseTool.kt index b536b8cb1fc..0e2e29e7d83 100644 --- a/analysis/analysers/tools/AnalyseTool/src/main/kotlin/de/maibornwolff/codecharta/analysers/tools/analyse/AnalyseTool.kt +++ b/analysis/analysers/tools/AnalyseTool/src/main/kotlin/de/maibornwolff/codecharta/analysers/tools/analyse/AnalyseTool.kt @@ -77,7 +77,10 @@ class AnalyseTool( @CommandLine.Option( names = [AuthorOwnershipScan.NAMES_FLAG], - description = ["writes the real names of the authors into the map instead (personal data, needs --author-ownership)"] + description = [ + "writes the real names of the authors into the map instead, also for commit changes " + + "(personal data, needs --author-ownership)" + ] ) private var withAuthorNames = false diff --git a/analysis/test/golden_test.sh b/analysis/test/golden_test.sh index 192315793db..f2eb2d27eaa 100755 --- a/analysis/test/golden_test.sh +++ b/analysis/test/golden_test.sh @@ -87,11 +87,13 @@ check_gitlogparser_repo_scan() { commit_ownership_fixture_file() { local author="$1" local content="$2" + local message + message="$(printf "%b" "${3:-change}")" printf "%b" "$content" >"${OWNERSHIP_REPO}/app.kt" git -C "${OWNERSHIP_REPO}" add -A GIT_AUTHOR_NAME="$author" GIT_AUTHOR_EMAIL="$author@example.org" GIT_AUTHOR_DATE="2024-01-01T12:00:00Z" \ GIT_COMMITTER_NAME="$author" GIT_COMMITTER_EMAIL="$author@example.org" GIT_COMMITTER_DATE="2024-01-01T12:00:00Z" \ - git -C "${OWNERSHIP_REPO}" -c commit.gpgsign=false commit -q -m "change" + git -C "${OWNERSHIP_REPO}" -c commit.gpgsign=false commit -q -m "$message" } check_gitlogparser_author_ownership() { @@ -138,7 +140,7 @@ check_gitlogparser_commit_changes() { commit_ownership_fixture_file "Golden First" "1\n2\n3\n4\n" mkdir "${COMMIT_CHANGES_REPO}/src" printf "1\n" >"${COMMIT_CHANGES_REPO}/src/util.kt" - commit_ownership_fixture_file "Golden Second" "1\n2\n3\nfour\n" + commit_ownership_fixture_file "Golden Second" "1\n2\n3\nfour\n" "change\n\nCo-authored-by: Golden Third " "${CCSH}" gitlogparser repo-scan --repo-path "${COMMIT_CHANGES_REPO}" --commit-changes-since=2000-01-01 -o "${ACTUAL_COMMIT_CHANGES_FIRST}" --silent=true -nc "${CCSH}" gitlogparser repo-scan --repo-path "${COMMIT_CHANGES_REPO}" --commit-changes-since=2000-01-01 -o "${ACTUAL_COMMIT_CHANGES_SECOND}" --silent=true -nc @@ -152,10 +154,16 @@ check_gitlogparser_commit_changes() { "${CCSH}" modify "${ACTUAL_COMMIT_CHANGES_FIRST}" --move-from=root/src --move-to=root/moved -o "${ACTUAL_COMMIT_CHANGES_MOVED}" validate "${ACTUAL_COMMIT_CHANGES_MOVED}" for actual in "${ACTUAL_COMMIT_CHANGES_FIRST}" "${ACTUAL_COMMIT_CHANGES_MOVED}"; do - if ! grep -qF -- '"changed_files":2,"changed_lines":3' "${actual}"; then + if ! grep -qF -- '"author":"Ochre Elk","co_authors":"Lime Wolf","changed_files":2,"changed_lines":3' "${actual}"; then exit_with_err "${actual} lost the attributes of a commit" fi done + ACTUAL_COMMIT_CHANGES_NAMES="${TEMP_DIR}/actual_commit_changes_names.cc.json" + "${CCSH}" gitlogparser repo-scan --repo-path "${COMMIT_CHANGES_REPO}" --author-ownership --with-author-names --commit-changes-since=2000-01-01 -o "${ACTUAL_COMMIT_CHANGES_NAMES}" --silent=true -nc + validate "${ACTUAL_COMMIT_CHANGES_NAMES}" + if ! grep -qF -- '"author":"Golden Second","co_authors":"Golden Third","changed_files":2' "${ACTUAL_COMMIT_CHANGES_NAMES}"; then + exit_with_err "--with-author-names wrote no real author into the commit changes" + fi } check_csvexporter() { diff --git a/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/DeclarationExtractor.kt b/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/DeclarationExtractor.kt index 5a748ab7dc8..2ee2c85bc4b 100644 --- a/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/DeclarationExtractor.kt +++ b/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/DeclarationExtractor.kt @@ -15,6 +15,7 @@ internal object DeclarationExtractor { private const val CLASS_DECLARATION = "class_declaration" private const val OBJECT_DECLARATION = "object_declaration" private const val TYPE_IDENTIFIER = "type_identifier" + private const val INFIX_EXPRESSION = "infix_expression" // Kotlin uses keyword children to distinguish class types private const val ENUM_KEYWORD = "enum" @@ -39,7 +40,8 @@ internal object DeclarationExtractor { private fun typeDeclarations(rootNode: SyntaxNode, importedNames: Set): List { val allDeclarations = TreeTraversal - .findAllDescendantsOfType(rootNode, *DECLARATION_TYPES.toTypedArray()) + .findAllDescendantsOfType(rootNode, *DECLARATION_TYPES.toTypedArray(), INFIX_EXPRESSION) + .filter { it.type != INFIX_EXPRESSION || MisparsedClasses.isMisparsedClass(it) } .filterNot { KotlinScopes.LOCAL_TYPES.isLocalClass(it) } val namesByStartByte = allDeclarations.associate { node -> node.startByte to extractName(node) @@ -50,7 +52,9 @@ internal object DeclarationExtractor { if (name.isNullOrBlank()) return@mapNotNull null val type = declarationType(declarationNode) val parentPath = findParentPath(declarationNode, namesByStartByte) - val usedTypes = UsedTypeExtractor.extract(declarationNode, importedNames) + hostOfInnerClass(declarationNode, parentPath) + val usedTypes = UsedTypeExtractor.extract(declarationNode, importedNames) + + misparsedAnnotationTypes(declarationNode, importedNames) + + hostOfInnerClass(declarationNode, parentPath) Declaration( name = name, type = type, @@ -61,6 +65,11 @@ internal object DeclarationExtractor { }.toList() } + private fun misparsedAnnotationTypes(declaration: SyntaxNode, importedNames: Set): Set { + if (!MisparsedClasses.isMisparsedClass(declaration)) return emptySet() + return MisparsedClasses.annotationsOf(declaration).flatMap { UsedTypeExtractor.extract(it, importedNames) }.toSet() + } + // An inner class holds the instance of the class around it, whether it names that class or not private fun hostOfInnerClass(declaration: SyntaxNode, parentPath: List): Set { val host = parentPath.lastOrNull() ?: return emptySet() @@ -74,7 +83,13 @@ internal object DeclarationExtractor { ?.any { modifier -> modifier.type == CLASS_MODIFIER && modifier.children().any { it.type == keyword } } ?: false - private fun extractName(node: SyntaxNode): String = node + private fun extractName(node: SyntaxNode): String = if (MisparsedClasses.isMisparsedClass(node)) { + MisparsedClasses.nameOf(node) + } else { + typeIdentifierOf(node) + } + + private fun typeIdentifierOf(node: SyntaxNode): String = node .children() .firstOrNull { it.type == TYPE_IDENTIFIER } ?.text @@ -85,7 +100,7 @@ internal object DeclarationExtractor { val parents = mutableListOf() var current = node.parent while (!current.isNull) { - if (current.type in DECLARATION_TYPES) { + if (current.type in DECLARATION_TYPES || MisparsedClasses.isMisparsedClass(current)) { val parentName = namesByStartByte[current.startByte] ?: extractName(current) if (parentName.isNotBlank()) { parents.add(0, parentName) @@ -97,6 +112,7 @@ internal object DeclarationExtractor { } private fun declarationType(node: SyntaxNode): DeclarationType { + if (MisparsedClasses.isMisparsedClass(node)) return MisparsedClasses.typeOf(node) if (node.type == OBJECT_DECLARATION) return DeclarationType.CLASS // class_declaration: check for enum/interface keywords as direct children diff --git a/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/KotlinScopes.kt b/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/KotlinScopes.kt index 642b623d828..bf9879e943e 100644 --- a/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/KotlinScopes.kt +++ b/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/KotlinScopes.kt @@ -9,7 +9,7 @@ internal object KotlinScopes { // Statements directly in a script file are its top level, not a block val LOCAL_TYPES = LocalTypeScopes( typeParameterOwners = setOf("class_declaration", "function_declaration", "property_declaration"), - isBlock = { it.type == "statements" && it.parent.type != "source_file" }, + isBlock = { it.type == "statements" && it.parent.type != "source_file" && !MisparsedClasses.isBody(it) }, localClass = "class_declaration", nameOf = { declaration -> declaration.children().firstOrNull { it.type == "type_identifier" }?.text } ) @@ -32,10 +32,12 @@ internal object KotlinScopes { /** Whether a node belongs to the declaration and not to a nested class of it; a local class belongs to its block. */ fun belongsTo(node: SyntaxNode, declaration: SyntaxNode): Boolean = - classOf(node) == if (declaration.type in TYPE_DECLARATIONS) declaration else classOf(declaration) + classOf(node) == if (isTypeDeclaration(declaration)) declaration else classOf(declaration) private fun classOf(node: SyntaxNode): SyntaxNode? = node.ancestors().firstOrNull { - it.type in TYPE_DECLARATIONS && + isTypeDeclaration(it) && !LOCAL_TYPES.isLocalClass(it) } + + private fun isTypeDeclaration(node: SyntaxNode): Boolean = node.type in TYPE_DECLARATIONS || MisparsedClasses.isMisparsedClass(node) } diff --git a/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/MisparsedClasses.kt b/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/MisparsedClasses.kt new file mode 100644 index 00000000000..927e8e013c3 --- /dev/null +++ b/analysis/treeSitterExcavationSite/src/main/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/extractors/MisparsedClasses.kt @@ -0,0 +1,45 @@ +package de.maibornwolff.treesitter.excavationsite.languages.kotlin.extractors + +import de.maibornwolff.treesitter.excavationsite.shared.domain.DeclarationType +import de.maibornwolff.treesitter.excavationsite.shared.domain.SyntaxNode +import de.maibornwolff.treesitter.excavationsite.shared.domain.ancestors + +/** + * tree-sitter-kotlin reads a class with a body but without constructor, supertype or modifier keyword, written after + * annotations of which one takes arguments (`@Timeout(1)`, `@Tag`, `class Arena { … }`), as an expression: each + * annotation a prefix expression around an infix call `class Arena { … }` whose trailing lambda holds the class body. + */ +internal object MisparsedClasses { + private const val INFIX_EXPRESSION = "infix_expression" + private const val PREFIX_EXPRESSION = "prefix_expression" + private const val SIMPLE_IDENTIFIER = "simple_identifier" + private const val LAMBDA_LITERAL = "lambda_literal" + private const val ANNOTATION = "annotation" + private const val INTERFACE_KEYWORD = "interface" + private val KEYWORDS = setOf("class", INTERFACE_KEYWORD, "object") + private const val CHILDREN_OF_A_CLASS = 3 + + fun isMisparsedClass(node: SyntaxNode): Boolean { + if (node.type != INFIX_EXPRESSION || node.childCount != CHILDREN_OF_A_CLASS || node.parent.type != PREFIX_EXPRESSION) return false + val (keyword, name, body) = (0 until CHILDREN_OF_A_CLASS).map { node.getChild(it) } + return keyword.type == SIMPLE_IDENTIFIER && + keyword.text in KEYWORDS && + name.type == SIMPLE_IDENTIFIER && + body.type == LAMBDA_LITERAL + } + + fun nameOf(misparsedClass: SyntaxNode): String = misparsedClass.getChild(1).text.trim() + + fun typeOf(misparsedClass: SyntaxNode): DeclarationType = + if (misparsedClass.getChild(0).text == INTERFACE_KEYWORD) DeclarationType.INTERFACE else DeclarationType.CLASS + + /** The annotations written before the class, which the misparse puts into the prefix expressions around it. */ + fun annotationsOf(misparsedClass: SyntaxNode): List = misparsedClass + .ancestors() + .takeWhile { it.type == PREFIX_EXPRESSION } + .flatMap { prefix -> prefix.children().filter { it.type == ANNOTATION } } + .toList() + + /** Whether [statements] are the body of a misparsed class rather than the block of a lambda. */ + fun isBody(statements: SyntaxNode): Boolean = statements.parent.type == LAMBDA_LITERAL && isMisparsedClass(statements.parent.parent) +} diff --git a/analysis/treeSitterExcavationSite/src/test/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/KotlinDependencyTest.kt b/analysis/treeSitterExcavationSite/src/test/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/KotlinDependencyTest.kt index fcc965e2fb1..30f89bf8894 100644 --- a/analysis/treeSitterExcavationSite/src/test/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/KotlinDependencyTest.kt +++ b/analysis/treeSitterExcavationSite/src/test/kotlin/de/maibornwolff/treesitter/excavationsite/languages/kotlin/KotlinDependencyTest.kt @@ -1325,4 +1325,58 @@ class KotlinDependencyTest { assertThat(result.declarations.single().usedTypes).containsExactly(UsedType("Repository", usage = TypeUsage.INHERITANCE)) } } + + @Nested + inner class ClassesAfterAnnotationsWithArguments { + // tree-sitter-kotlin reads these as an infix call `class Arena { … }` inside prefix expressions + private val code = """ + package zoo + + @Timeout(120) + @TestInstance(TestInstance.Lifecycle.PER_CLASS) + class ArenaTest { + fun opens() { + Keeper() + } + + class Ticket { + val gate: Gate? = null + } + } + + @Retry(3) + @Tag + interface Port { + fun open(): Gate + } + """.trimIndent() + + @Test + fun `should read a class after annotations with arguments as a class using its annotations and body`() { + // Act + val declarations = declarationsByName(code) + + // Assert + val arena = declarations.getValue("ArenaTest") + assertThat(arena.type).isEqualTo(DeclarationType.CLASS) + val usedNames = arena.usedTypes.map { it.name } + assertThat(usedNames).containsExactlyInAnyOrder("Timeout", "TestInstance", "TestInstance.Lifecycle", "Keeper") + } + + @Test + fun `should keep a class nested in such a class below it, and read an interface there as an interface`() { + // Act + val declarations = declarationsByName(code) + + // Assert + val ticket = declarations.getValue("Ticket") + assertThat(ticket.parentPath).containsExactly("ArenaTest") + assertThat(ticket.usedTypes.map { it.name }).containsExactly("Gate") + assertThat(declarations.getValue("Port").type).isEqualTo(DeclarationType.INTERFACE) + assertThat(declarations.getValue("Port").usedTypes.map { it.name }).containsExactlyInAnyOrder("Retry", "Tag", "Gate") + } + } + + private fun declarationsByName(code: String): Map = + TreeSitterDependencies.analyze(code, Language.KOTLIN).declarations.associateBy { it.name } } diff --git a/gh-pages/src/content/docs/docs/analysis/analyse.md b/gh-pages/src/content/docs/docs/analysis/analyse.md index b083cabc4fd..e642cdc1fc3 100644 --- a/gh-pages/src/content/docs/docs/analysis/analyse.md +++ b/gh-pages/src/content/docs/docs/analysis/analyse.md @@ -39,8 +39,8 @@ back through it. | `--without=NAME[,NAME...]` | parsers or histories to leave out, such as `--without=dependencyparser,svn` | | `--local-changes` | maps only the files that differ from the remote tracking branch | | `--author-ownership` | adds each author's share of each file under an invented name, from `git blame` | -| `--with-author-names` | writes real author names instead of numbers (needs `--author-ownership`) | -| `--commit-changes-since YYYY-MM-DD` | adds what each commit since this day changed in each file | +| `--with-author-names` | writes real author names instead of invented ones, also for commit changes (needs `--author-ownership`) | +| `--commit-changes-since YYYY-MM-DD` | adds what each commit since this day changed in each file, and who made it | | `--commit=REF` | maps the codebase at a git commit, tag, branch or date | | `-p, --parallel` | runs the analysers at the same time | | `-nc, --not-compressed` | writes the map uncompressed | @@ -73,15 +73,17 @@ The names are pseudonymous, not anonymous: anyone with the repository can match still be personal data under GDPR. Treat and share it accordingly. `--with-author-names`, next to `--author-ownership`, opts out of this: the map then holds real names as git has them, under -a clustering titled **Author ownership (real names)**, and still no emails. The command warns about it last, even with +a clustering titled **Author ownership (real names)** and in the commit changes, and still no emails. The command warns about it last, even with `--silent`, and the Explorer's Clusters tab shows a warning for such a map. ## Commit changes `--commit-changes-since YYYY-MM-DD` adds what each commit since that day changed to the map, as described in the -[Git Log parser](/docs/parser/git-log). Commit subjects enter the map, and anyone with the repository can match a hash to -its author, so a map may still be personal data under GDPR. The note is the last thing the command prints. +[Git Log parser](/docs/parser/git-log), with each commit's author and co-authors under the same invented names as in +the author ownership, or their real names with `--with-author-names`. Invented names are pseudonymous, not anonymous: +anyone with the repository can match them and the hashes to people, and commit subjects are free text, so a map may +still be personal data under GDPR. The note is the last thing the command prints. ## What is not in the map The last lines of the output, after the `Map:` line, name every parser and history that is not in the map, and why: diff --git a/gh-pages/src/content/docs/docs/parser/git-log.md b/gh-pages/src/content/docs/docs/parser/git-log.md index 96610789a06..7786b6c32e2 100644 --- a/gh-pages/src/content/docs/docs/parser/git-log.md +++ b/gh-pages/src/content/docs/docs/parser/git-log.md @@ -32,8 +32,8 @@ and a file list you created. | `repo-scan` | use repo-scan mode - generates cc.json from an automatically generated git-log file | | `--add-author` | add an array of authors to every file | | `--author-ownership` | add each author's share of each file under an invented name, from `git blame` (repo-scan only) | -| `--with-author-names` | write real author names instead of numbers (needs `--author-ownership`) | -| `--commit-changes-since YYYY-MM-DD` | add what each commit since this day changed in each file (repo-scan only) | +| `--with-author-names` | write real author names instead of invented ones, also for commit changes (needs `--author-ownership`) | +| `--commit-changes-since YYYY-MM-DD` | add what each commit since this day changed in each file, and who made it (repo-scan only) | | `--commit=` | analyze the repository at a specific git commit, tag, branch, or date expression (creates a temporary worktree, repo-scan only). See [Commit-Based Analysis](/docs/analysis/commit-analysis) | | `--git-log=FILE` | git-log file to parse (log-scan only, required) | | `-h, --help` | displays help and exits | @@ -99,7 +99,7 @@ The names are pseudonymous, not anonymous: anyone with the repository can match still be personal data under GDPR. Treat and share it accordingly. `--with-author-names`, next to `--author-ownership`, opts out of this: the map then holds real names as git has them, under -a clustering titled **Author ownership (real names)**, and still no emails. The command warns about it last, even with +a clustering titled **Author ownership (real names)** and in the commit changes, and still no emails. The command warns about it last, even with `--silent`, and the Explorer's Clusters tab shows a warning for such a map. @@ -114,11 +114,20 @@ git 2.37 or newer. empty file counts as wholly changed. - Merge commits are left out, as their commits are listed themselves. Changes follow a renamed file to its path today, and a pure rename changes nothing. A commit with no file left today is not listed. -- Each commit also carries its date and how many files and lines it changed in all, including files gone today. - -Commit subjects enter the map, and anyone with the repository can match a hash to its author, so a map may still be -personal data under GDPR. The command says so last. On a shallow clone the oldest commit counts every file as changed, -and the command warns about it. +- Each commit also carries its date, its author, its co-authors and how many files and lines it changed in all, + including files gone today. The author is the one git records, and the co-authors are everyone else its + `Co-authored-by` trailers name, in their order, each once and joined by `; `, as in `Teal Falcon; Jade Owl`, both after + applying `.mailmap`. A commit without co-authors carries none. Everyone appears under the same invented name the + author ownership gives them, whether or not the map has author ownership. With `--with-author-names`, next to + `--author-ownership`, they appear under their real names. +- A commit whose subject starts with a commit type carries it as `commit_type` (`feat`, `fix`, `docs`, `style`, + `refactor` or `test`), and one whose subject names a hotfix carries `hotfix` 1, the same rules by which the + `*_commits` and `hotfix_commits` metrics count a file's commits. + +The invented names are pseudonymous, not anonymous: anyone with the repository can match them and the hashes to people, +and commit subjects are free text, so a map may still be personal data under GDPR. The command says so last. On a +shallow clone the oldest commit counts every file as changed and the invented names are not stable, and the command +warns about it. ## Scan a repository with repo-scan `repo-scan` writes a git log and the list of tracked files of the repository at `--repo-path` into your temp folder diff --git a/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/expected.txt b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/expected.txt new file mode 100644 index 00000000000..b134ab7624f --- /dev/null +++ b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/expected.txt @@ -0,0 +1,8 @@ +# A class written after annotations of which one takes arguments, without constructor, supertype or modifier, +# which tree-sitter-kotlin reads as an expression: it is still a class using its annotations and body +file zoo/Watch.kt -> zoo/Keeper.kt +file zoo/Watch.kt -> zoo/Shift.kt +leaf zoo/Watch.kt#Round -> zoo/Keeper.kt#Keeper +leaf zoo/Watch.kt#Watch -> zoo/Keeper.kt#Keeper +leaf zoo/Watch.kt#Watch -> zoo/Shift.kt#Night +leaf zoo/Watch.kt#Watch -> zoo/Shift.kt#Shift diff --git a/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Keeper.kt b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Keeper.kt new file mode 100644 index 00000000000..734333aed7c --- /dev/null +++ b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Keeper.kt @@ -0,0 +1,3 @@ +package zoo + +class Keeper diff --git a/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Shift.kt b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Shift.kt new file mode 100644 index 00000000000..e6ef0022520 --- /dev/null +++ b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Shift.kt @@ -0,0 +1,5 @@ +package zoo + +annotation class Shift(val hours: Int) + +annotation class Night diff --git a/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Watch.kt b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Watch.kt new file mode 100644 index 00000000000..a5e46500c21 --- /dev/null +++ b/sample-projects/regression/kotlin/spec-class-after-annotations-with-arguments/src/zoo/Watch.kt @@ -0,0 +1,13 @@ +package zoo + +@Shift(8) +@Night +class Watch { + fun start() { + Keeper() + } + + class Round { + val keeper: Keeper? = null + } +} From 5ca2a2a1c64fcd4df3695545cff1ba69231190f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=BChn?= Date: Tue, 10 Nov 2026 09:10:47 +0100 Subject: [PATCH 2/2] docs: describe who made each commit and its type in the clusters lens Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YRW3WswmkYHuNdjyiNBTJX --- dev_docs/cc-json-2.0-clusters-lens.md | 23 ++++++++++++++----- dev_docs/cc-json-2.0.schema.json | 5 +++- .../app/codeCharta/util/ccJson2Schema.json | 5 +++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/dev_docs/cc-json-2.0-clusters-lens.md b/dev_docs/cc-json-2.0-clusters-lens.md index afc005a878c..d8221dc7886 100644 --- a/dev_docs/cc-json-2.0-clusters-lens.md +++ b/dev_docs/cc-json-2.0-clusters-lens.md @@ -146,8 +146,9 @@ removed nodes; a cluster left without members goes too, unless a surviving clust first commits, so its names are not stable, and the producer warns about it. - **No hashing**: a plain hash of a name can be recomputed from a list of employees, a keyed one needs a key kept safe, and a hash picking from a word list collides. -- **Real names stay out of the file by default**: no name, email or per-author commit data, and no cluster `attributes`. - `--with-author-names` opts out for names alone: the cluster `name` is then the person's name, under the clustering +- **Real names stay out of the file by default**: no name or email, and no cluster `attributes`; commit changes name + people only by these pseudonyms. `--with-author-names` opts out for names alone, here and in commit changes: the + cluster `name` is then the person's name, under the clustering `author-ownership-with-names` titled "Author ownership (real names)", so a merge never mixes it with pseudonymous clusters. The CLI warns last, even with `--silent`, and the Clusters tab warns whenever a loaded map holds that clustering. The pseudonyms are not anonymous: anyone with the repository can match them to people by comparing `git blame`, @@ -166,11 +167,14 @@ removed nodes; a cluster left without members goes too, unless a surviving clust "analyzers": ["gitlogparser"], "clusters": [ { "id": "", "name": "a1b2c3d Fix login", - "attributes": { "date": "2026-10-31", "changed_files": 2, "changed_lines": 14 }, + "attributes": { "date": "2026-10-31", "author": "Teal Falcon", "co_authors": "Jade Owl; Amber Lynx", + "changed_files": 2, "changed_lines": 14, "commit_type": "fix", "hotfix": 1 }, "members": [ { "nodeId": "", "weight": 0.25 } ] } ], - "attributeDescriptors": { "date": { "title": "Date" }, "changed_files": { "title": "Files changed" }, "changed_lines": { "title": "Lines changed" } }, - "attributeTypes": { "changed_files": "absolute", "changed_lines": "absolute" } + "attributeDescriptors": { "date": { "title": "Date" }, "author": { "title": "Author" }, "co_authors": { "title": "Co-authors" }, + "changed_files": { "title": "Files changed" }, "changed_lines": { "title": "Lines changed" }, + "commit_type": { "title": "Commit type" }, "hotfix": { "title": "Hotfix" } }, + "attributeTypes": { "changed_files": "absolute", "changed_lines": "absolute", "hotfix": "absolute" } } } } ``` @@ -184,7 +188,14 @@ removed nodes; a cluster left without members goes too, unless a surviving clust - **Files** follow renames to their path today (`git log -M`), walking from the newest commit; a commit with no member left is not written, and merge commits never are. - **The attributes** describe the whole commit, including files gone today, so they survive filters unchanged. -- **Personal data**: subjects are free text and a hash leads to its author, so the map may still be personal data. + - `author` is the one git records, `co_authors` everyone else its `Co-authored-by` trailers name, in their order, + each once and joined by `; `, both after applying `.mailmap`; a commit without co-authors omits the key. Both use the + pseudonyms of author ownership, whether or not the map holds it, or real names with `--with-author-names`. + - `commit_type` is the type the subject starts with (`feat`, `fix`, `docs`, `style`, `refactor`, `test`), omitted + for any other subject; `hotfix` is 1 when the subject names a hotfix and omitted otherwise. Both count as the + `*_commits` and `hotfix_commits` metrics do. +- **Personal data**: subjects are free text, and the pseudonyms and hashes lead to people, so the map may still be + personal data. A shallow clone's pseudonyms are not stable, and the producer warns about it. Further producers could be a cluster filter running community detection over co-change data, an issue importer or a coverage importer, the last two as `overlay`. diff --git a/dev_docs/cc-json-2.0.schema.json b/dev_docs/cc-json-2.0.schema.json index 9c7129fd9eb..a1525ce4d8b 100644 --- a/dev_docs/cc-json-2.0.schema.json +++ b/dev_docs/cc-json-2.0.schema.json @@ -296,7 +296,10 @@ "id": { "type": "string" }, "name": { "type": "string" }, "parentId": { "type": ["string", "null"] }, - "attributes": { "type": "object" }, + "attributes": { + "description": "Values are numbers or strings, keyed like the clustering's `attributeDescriptors`; `attributeTypes` describes the numbers only. Commit changes write `date`, `author`, `co_authors`, `changed_files`, `changed_lines`, `commit_type` and `hotfix`.", + "type": "object" + }, "members": { "type": "array", "items": { "$ref": "#/definitions/ClusterMember" } diff --git a/visualization/app/codeCharta/util/ccJson2Schema.json b/visualization/app/codeCharta/util/ccJson2Schema.json index 9c7129fd9eb..a1525ce4d8b 100644 --- a/visualization/app/codeCharta/util/ccJson2Schema.json +++ b/visualization/app/codeCharta/util/ccJson2Schema.json @@ -296,7 +296,10 @@ "id": { "type": "string" }, "name": { "type": "string" }, "parentId": { "type": ["string", "null"] }, - "attributes": { "type": "object" }, + "attributes": { + "description": "Values are numbers or strings, keyed like the clustering's `attributeDescriptors`; `attributeTypes` describes the numbers only. Commit changes write `date`, `author`, `co_authors`, `changed_files`, `changed_lines`, `commit_type` and `hotfix`.", + "type": "object" + }, "members": { "type": "array", "items": { "$ref": "#/definitions/ClusterMember" }