Skip to content

Commit

Permalink
feat: add git exclude path (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhensherlock committed Nov 16, 2024
1 parent 40d0683 commit 905d5ac
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.huayi.intellijplatform.gitstats.models

data class SettingModel (
var mode: String = "Top-speed",
var exclude: String = ""
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.huayi.intellijplatform.gitstats.services

import com.huayi.intellijplatform.gitstats.models.SettingModel
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.huayi.intellijplatform.gitstats.toolWindow.StatsTableModel
Expand All @@ -19,14 +20,15 @@ class GitStatsService(p: Project) {

// fun getRandomNumber() = (1..100).random()

fun getUserStats(startTime: Date, endTime: Date): StatsTableModel {
fun getUserStats(startTime: Date, endTime: Date, settingModel: SettingModel): StatsTableModel {
if (!Utils.checkDirectoryExists(project.basePath)) {
return StatsTableModel(arrayOf(), arrayOf())
}
val gitUtils = GitUtils(project)
val userStats = gitUtils.getUserStats(
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime),
settingModel
)
val data = userStats.map { item ->
arrayOf(
Expand All @@ -43,14 +45,15 @@ class GitStatsService(p: Project) {
)
}

fun getTopSpeedUserStats(startTime: Date, endTime: Date): StatsTableModel {
fun getTopSpeedUserStats(startTime: Date, endTime: Date, settingModel: SettingModel): StatsTableModel {
if (!Utils.checkDirectoryExists(project.basePath)) {
return StatsTableModel(arrayOf(), arrayOf())
}
val gitUtils = GitUtils(project)
val userStats = gitUtils.getTopSpeedUserStats(
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime),
settingModel
)
val data = userStats.map { item ->
arrayOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.huayi.intellijplatform.gitstats.toolWindow

import com.huayi.intellijplatform.gitstats.MyBundle
import com.huayi.intellijplatform.gitstats.components.SettingAction
import com.huayi.intellijplatform.gitstats.models.SettingModel
import com.huayi.intellijplatform.gitstats.services.GitStatsService
import com.huayi.intellijplatform.gitstats.utils.Utils
import com.intellij.icons.AllIcons
Expand Down Expand Up @@ -46,7 +47,10 @@ class GitStatsWindowFactory : ToolWindowFactory {

fun getContent(toolWindow: ToolWindow) = JBPanel<JBPanel<*>>(BorderLayout()).apply {
var (startTime, endTime) = Utils.getThisWeekDateTimeRange()
var defaultMode = "Top-speed"
val settingModel = SettingModel().apply {
mode = "Top-speed"
exclude = ""
}
val table = JBTable().apply {
font = Font("Microsoft YaHei", Font.PLAIN, 14)
tableHeader.font = Font("Microsoft YaHei", Font.BOLD, 14)
Expand Down Expand Up @@ -134,10 +138,10 @@ class GitStatsWindowFactory : ToolWindowFactory {
isEnabled = false
text = MyBundle.message("refreshButtonLoadingLabel")
thread {
if (defaultMode === "Top-speed") {
table.model = service.getTopSpeedUserStats(startTime, endTime)
if (settingModel.mode === "Top-speed") {
table.model = service.getTopSpeedUserStats(startTime, endTime, settingModel)
} else {
table.model = service.getUserStats(startTime, endTime)
table.model = service.getUserStats(startTime, endTime, settingModel)
}
SwingUtilities.invokeLater {
(contentPanel.layout as CardLayout).show(contentPanel, "content_table")
Expand All @@ -163,8 +167,9 @@ class GitStatsWindowFactory : ToolWindowFactory {

val actionList: MutableList<AnAction> = ArrayList()
val settingAction =
SettingAction(MyBundle.message("settingButtonTooltipText"), defaultMode) { selectedMode ->
defaultMode = selectedMode
SettingAction(MyBundle.message("settingButtonTooltipText"), settingModel) { value ->
settingModel.mode = value.mode
settingModel.exclude = value.exclude
refreshButton.doClick()
}
actionList.add(settingAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import git4idea.config.GitExecutableManager
import git4idea.config.GitExecutableDetector
import java.util.concurrent.TimeUnit
import com.intellij.openapi.project.Project
import com.huayi.intellijplatform.gitstats.models.SettingModel

data class UserStats(
val author: String,
Expand All @@ -29,8 +30,8 @@ data class CommitFilesStats(

class GitUtils(project: Project) {
private val gitExecutablePath: String = GitExecutableManager.getInstance().getExecutable(project).exePath
private val basePath: String = project.basePath as String
// private val basePath: String = "/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front"
// private val basePath: String = project.basePath as String
private val basePath: String = "/Users/sunzhenxuan/work/qcc/code/qcc_pro/pro-front"
private val gitBashExecutablePath: String? = GitExecutableDetector.getBashExecutablePath(gitExecutablePath)

// init {
Expand All @@ -42,15 +43,18 @@ class GitUtils(project: Project) {
// }

fun getTopSpeedUserStats(
startDate: String, endDate: String, timeoutAmount: Long = 60L, timeUnit: TimeUnit = TimeUnit.SECONDS
startDate: String, endDate: String, settingModel: SettingModel
): Array<UserStats> {
val timeoutAmount = 60L
val timeUnit = TimeUnit.SECONDS
val os = Utils.getOS()
val commands = mutableListOf<String>()
val folder = if (settingModel.exclude.isEmpty()) "." else ". ':(exclude)${settingModel.exclude}'"
when {
os == "Windows" && gitBashExecutablePath?.isNotEmpty() ?: false -> {
commands += gitBashExecutablePath!!
commands += "-c"
commands += "git log --format=\"%aN\" | sort -u | while read name; do echo \"\$name\"; git log --author=\\\"\$name\\\" --pretty=tformat: --since=\\\"${startDate}\\\" --until=\\\"${endDate}\\\" --numstat | awk '{ add += \$1; subs += \$2; file++ } END { printf(\\\"added lines: %s, removed lines: %s, modified files: %s\\n\\\", add ? add : 0, subs ? subs : 0, file ? file : 0) }' -; done"
commands += "git log --format=\"%aN\" | sort -u | while read name; do echo \"\$name\"; git log --author=\\\"\$name\\\" --pretty=tformat: --since=\\\"${startDate}\\\" --until=\\\"${endDate}\\\" --numstat -- $folder | awk '{ add += \$1; subs += \$2; file++ } END { printf(\\\"added lines: %s, removed lines: %s, modified files: %s\\n\\\", add ? add : 0, subs ? subs : 0, file ? file : 0) }' -; done"
}
os == "Windows" && gitBashExecutablePath?.isEmpty() ?: false -> {
commands += "powershell"
Expand All @@ -60,7 +64,7 @@ class GitUtils(project: Project) {
else -> {
commands += "/bin/sh"
commands += "-c"
commands += "$gitExecutablePath log --format=\"%aN\" | sort -u | while read name; do echo \"\$name\"; git log --author=\"\$name\" --pretty=\"tformat:\" --since=\"$startDate\" --until=\"$endDate\" --numstat | awk '{ add += \$1; subs += \$2; file++ } END { printf \"added lines: %s, removed lines: %s, modified files: %s\\n\", add ? add : 0, subs ? subs : 0, file ? file : 0 }' -; done"
commands += "$gitExecutablePath log --format=\"%aN\" | sort -u | while read name; do echo \"\$name\"; git log --author=\"\$name\" --pretty=\"tformat:\" --since=\"$startDate\" --until=\"$endDate\" --numstat -- $folder | awk '{ add += \$1; subs += \$2; file++ } END { printf \"added lines: %s, removed lines: %s, modified files: %s\\n\", add ? add : 0, subs ? subs : 0, file ? file : 0 }' -; done"
}
}
val process = Utils.runCommand(basePath, commands, timeoutAmount, timeUnit)
Expand All @@ -75,21 +79,25 @@ class GitUtils(project: Project) {
fun getUserStats(
startDate: String,
endDate: String,
timeoutAmount: Long = 60L,
timeUnit: TimeUnit = TimeUnit.SECONDS,
separator: String = "--"
settingModel: SettingModel
): Array<UserStats> {
val gitCommand = listOf(
"git",
"log",
"--numstat",
"--date=iso",
"--pretty=format:${separator}%h${separator}%ad${separator}%aN",
"--since=$startDate",
"--until=$endDate"
)
val timeoutAmount = 60L
val timeUnit = TimeUnit.SECONDS
val separator = "--"
val os = Utils.getOS()
val commands = mutableListOf<String>()
val folder = if (settingModel.exclude.isEmpty()) "." else ". ':(exclude)${settingModel.exclude}'"
if (os == "Windows" && gitBashExecutablePath?.isNotEmpty() == true) {
commands += gitBashExecutablePath
commands += "-c"
commands += "git log --numstat --pretty=\"format:${separator}%h${separator}%ad${separator}%aN\" --since=\\\"${startDate}\\\" --until=\\\"${endDate}\\\" -- $folder"
} else {
commands += "/bin/sh"
commands += "-c"
commands += "$gitExecutablePath log --numstat --pretty=\"format:${separator}%h${separator}%ad${separator}%aN\" --since=\"$startDate\" --until=\"$endDate\" -- $folder"
}

val process = Utils.runCommand(basePath, gitCommand, timeoutAmount, timeUnit)
val process = Utils.runCommand(basePath, commands, timeoutAmount, timeUnit)

val userStatsData = mutableMapOf<String, UserStats>()
process!!.inputStream.bufferedReader().use { reader ->
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/MyBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ refreshButtonLabel=Refresh
refreshButtonLoadingLabel=Loading
settingButtonTooltipText=Show Setting
settingDialogModeLabel=Mode:
settingDialogExcludeLabel=Exclude:

0 comments on commit 905d5ac

Please sign in to comment.