Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #60 from watermelontools/chore/simplify-git-blame-…
Browse files Browse the repository at this point in the history
…getter

Simplify code
  • Loading branch information
Esteban Dalel R authored Oct 31, 2023
2 parents 572f70c + 08e7c6f commit 07c5b15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 63 deletions.
65 changes: 10 additions & 55 deletions src/main/kotlin/com/watermelon/context/services/MyProjectService.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import com.intellij.openapi.components.Service
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.vcsUtil.VcsUtil
import com.intellij.openapi.fileEditor.FileEditorManager
import git4idea.commands.Git
import git4idea.commands.GitCommand
import git4idea.commands.GitCommandResult
Expand All @@ -23,73 +23,30 @@ class MyProjectService(private val project: Project) {
return Git.getInstance().runCommand(lineHandler)
}

fun getGitBlame(): List<CommitDetails>? {
fun getGitBlame(startLine: Int? = null, endLine: Int? = null): List<CommitDetails>? {
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null
val document = editor.document
val file = FileDocumentManager.getInstance().getFile(document) ?: return null
val directory = file.parent ?: return null
val filePath = VcsUtil.getFilePath(file)
println("File path : $filePath")
val blameRun = GitLineHandler(project, directory, GitCommand.BLAME).apply {
addParameters(filePath.path)
}

val blameCommandResponse = runCommand(blameRun)

// Use regex to extract details from git blame
val regex = """(\w+) \((.+?)\s+([\d-]+ [\d:]+ [+-]\d+)\s+(\d+)\)\s*(.*)""".toRegex()

val commitsDetailsList = mutableListOf<CommitDetails>()

// Extract details and map to a list of CommitDetails
blameCommandResponse.output.forEach { line ->
regex.find(line)?.let { matchResult ->
val hash = matchResult.groups[1]?.value.orEmpty()
val author = matchResult.groups[2]?.value.orEmpty()
val date = matchResult.groups[3]?.value.orEmpty()
val lineNumber = matchResult.groups[4]?.value?.toIntOrNull() ?: 0
val content = matchResult.groups[5]?.value.orEmpty()

// Fetch commit message for unique hashes
val logRun = GitLineHandler(project, directory, GitCommand.LOG).apply {
addParameters("-1", "--pretty=format:%s", hash)
}
val logCommandResponse = runCommand(logRun)
val message = logCommandResponse.output.firstOrNull().orEmpty()

commitsDetailsList.add(
CommitDetails(hash, author, date, lineNumber, message, content)
)
}
val blameParameters = mutableListOf<String>()
if (startLine != null && endLine != null) {
val adjustedStartLine = startLine + 1
val adjustedEndLine = endLine + 1
blameParameters.add("-L$adjustedStartLine,$adjustedEndLine")
}

return commitsDetailsList.distinctBy { it.hash }

}

fun getPartialGitBlame(startLine: Int, endLine: Int): List<CommitDetails>? {
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null
val document = editor.document
val file = FileDocumentManager.getInstance().getFile(document) ?: return null
val directory = file.parent ?: return null
val filePath = VcsUtil.getFilePath(file)
// Adjust the line numbers to be 1-indexed
val adjustedStartLine = startLine + 1
val adjustedEndLine = endLine + 1
blameParameters.add(filePath.path)

val blameRun = GitLineHandler(project, directory, GitCommand.BLAME).apply {
addParameters("-L$adjustedStartLine,$adjustedEndLine", filePath.path)
addParameters(*blameParameters.toTypedArray())
}

val blameCommandResponse = runCommand(blameRun)


// Use regex to extract details from git blame
val regex = """(\w+) \((.+?)\s+([\d-]+ [\d:]+ [+-]\d+)\s+(\d+)\)\s*(.*)""".toRegex()

val commitsDetailsList = mutableListOf<CommitDetails>()

// Extract details and map to a list of CommitDetails
blameCommandResponse.output.forEach { line ->
regex.find(line)?.let { matchResult ->
val hash = matchResult.groups[1]?.value.orEmpty()
Expand All @@ -98,7 +55,6 @@ class MyProjectService(private val project: Project) {
val lineNumber = matchResult.groups[4]?.value?.toIntOrNull() ?: 0
val content = matchResult.groups[5]?.value.orEmpty()

// Fetch commit message for unique hashes
val logRun = GitLineHandler(project, directory, GitCommand.LOG).apply {
addParameters("-1", "--pretty=format:%s", hash)
}
Expand All @@ -112,7 +68,6 @@ class MyProjectService(private val project: Project) {
}

return commitsDetailsList.distinctBy { it.hash }


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ package com.watermelon.context.toolWindow
import MyProjectService
import com.intellij.credentialStore.CredentialAttributes
import com.intellij.ide.BrowserUtil
import com.intellij.ide.passwordSafe.PasswordSafe
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBPanel
import com.intellij.ui.content.ContentFactory
import com.intellij.ide.passwordSafe.PasswordSafe
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.content.ContentFactory
import git4idea.GitUtil
import git4idea.repo.GitRepository
import kotlinx.serialization.json.*
import java.awt.*
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import javax.swing.*
import git4idea.GitUtil
import git4idea.repo.GitRepository
import java.awt.Font
import java.io.IOException
import java.awt.FontFormatException


class MyToolWindowFactory : ToolWindowFactory {
Expand Down Expand Up @@ -221,7 +219,7 @@ class MyToolWindowFactory : ToolWindowFactory {
val commitHashes = if (startLine == 0 && endLine == 0) {
service.getGitBlame()
} else {
service.getPartialGitBlame(startLine, endLine)
service.getGitBlame(startLine, endLine)
}

fun getCurrentGitRepo(project: Project): GitRepository? {
Expand Down

0 comments on commit 07c5b15

Please sign in to comment.