Skip to content

Commit 905d5ac

Browse files
committed
feat: add git exclude path (#90)
1 parent 40d0683 commit 905d5ac

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.huayi.intellijplatform.gitstats.models
2+
3+
data class SettingModel (
4+
var mode: String = "Top-speed",
5+
var exclude: String = ""
6+
)

src/main/kotlin/com/huayi/intellijplatform/gitstats/services/GitStatsService.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.huayi.intellijplatform.gitstats.services
22

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

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

22-
fun getUserStats(startTime: Date, endTime: Date): StatsTableModel {
23+
fun getUserStats(startTime: Date, endTime: Date, settingModel: SettingModel): StatsTableModel {
2324
if (!Utils.checkDirectoryExists(project.basePath)) {
2425
return StatsTableModel(arrayOf(), arrayOf())
2526
}
2627
val gitUtils = GitUtils(project)
2728
val userStats = gitUtils.getUserStats(
2829
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
29-
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
30+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime),
31+
settingModel
3032
)
3133
val data = userStats.map { item ->
3234
arrayOf(
@@ -43,14 +45,15 @@ class GitStatsService(p: Project) {
4345
)
4446
}
4547

46-
fun getTopSpeedUserStats(startTime: Date, endTime: Date): StatsTableModel {
48+
fun getTopSpeedUserStats(startTime: Date, endTime: Date, settingModel: SettingModel): StatsTableModel {
4749
if (!Utils.checkDirectoryExists(project.basePath)) {
4850
return StatsTableModel(arrayOf(), arrayOf())
4951
}
5052
val gitUtils = GitUtils(project)
5153
val userStats = gitUtils.getTopSpeedUserStats(
5254
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime),
53-
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime)
55+
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime),
56+
settingModel
5457
)
5558
val data = userStats.map { item ->
5659
arrayOf(

src/main/kotlin/com/huayi/intellijplatform/gitstats/toolWindow/GitStatsWindowFactory.kt

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.huayi.intellijplatform.gitstats.toolWindow
22

33
import com.huayi.intellijplatform.gitstats.MyBundle
44
import com.huayi.intellijplatform.gitstats.components.SettingAction
5+
import com.huayi.intellijplatform.gitstats.models.SettingModel
56
import com.huayi.intellijplatform.gitstats.services.GitStatsService
67
import com.huayi.intellijplatform.gitstats.utils.Utils
78
import com.intellij.icons.AllIcons
@@ -46,7 +47,10 @@ class GitStatsWindowFactory : ToolWindowFactory {
4647

4748
fun getContent(toolWindow: ToolWindow) = JBPanel<JBPanel<*>>(BorderLayout()).apply {
4849
var (startTime, endTime) = Utils.getThisWeekDateTimeRange()
49-
var defaultMode = "Top-speed"
50+
val settingModel = SettingModel().apply {
51+
mode = "Top-speed"
52+
exclude = ""
53+
}
5054
val table = JBTable().apply {
5155
font = Font("Microsoft YaHei", Font.PLAIN, 14)
5256
tableHeader.font = Font("Microsoft YaHei", Font.BOLD, 14)
@@ -134,10 +138,10 @@ class GitStatsWindowFactory : ToolWindowFactory {
134138
isEnabled = false
135139
text = MyBundle.message("refreshButtonLoadingLabel")
136140
thread {
137-
if (defaultMode === "Top-speed") {
138-
table.model = service.getTopSpeedUserStats(startTime, endTime)
141+
if (settingModel.mode === "Top-speed") {
142+
table.model = service.getTopSpeedUserStats(startTime, endTime, settingModel)
139143
} else {
140-
table.model = service.getUserStats(startTime, endTime)
144+
table.model = service.getUserStats(startTime, endTime, settingModel)
141145
}
142146
SwingUtilities.invokeLater {
143147
(contentPanel.layout as CardLayout).show(contentPanel, "content_table")
@@ -163,8 +167,9 @@ class GitStatsWindowFactory : ToolWindowFactory {
163167

164168
val actionList: MutableList<AnAction> = ArrayList()
165169
val settingAction =
166-
SettingAction(MyBundle.message("settingButtonTooltipText"), defaultMode) { selectedMode ->
167-
defaultMode = selectedMode
170+
SettingAction(MyBundle.message("settingButtonTooltipText"), settingModel) { value ->
171+
settingModel.mode = value.mode
172+
settingModel.exclude = value.exclude
168173
refreshButton.doClick()
169174
}
170175
actionList.add(settingAction)

src/main/kotlin/com/huayi/intellijplatform/gitstats/utils/GitUtils.kt

+26-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import git4idea.config.GitExecutableManager
44
import git4idea.config.GitExecutableDetector
55
import java.util.concurrent.TimeUnit
66
import com.intellij.openapi.project.Project
7+
import com.huayi.intellijplatform.gitstats.models.SettingModel
78

89
data class UserStats(
910
val author: String,
@@ -29,8 +30,8 @@ data class CommitFilesStats(
2930

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

3637
// init {
@@ -42,15 +43,18 @@ class GitUtils(project: Project) {
4243
// }
4344

4445
fun getTopSpeedUserStats(
45-
startDate: String, endDate: String, timeoutAmount: Long = 60L, timeUnit: TimeUnit = TimeUnit.SECONDS
46+
startDate: String, endDate: String, settingModel: SettingModel
4647
): Array<UserStats> {
48+
val timeoutAmount = 60L
49+
val timeUnit = TimeUnit.SECONDS
4750
val os = Utils.getOS()
4851
val commands = mutableListOf<String>()
52+
val folder = if (settingModel.exclude.isEmpty()) "." else ". ':(exclude)${settingModel.exclude}'"
4953
when {
5054
os == "Windows" && gitBashExecutablePath?.isNotEmpty() ?: false -> {
5155
commands += gitBashExecutablePath!!
5256
commands += "-c"
53-
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"
57+
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"
5458
}
5559
os == "Windows" && gitBashExecutablePath?.isEmpty() ?: false -> {
5660
commands += "powershell"
@@ -60,7 +64,7 @@ class GitUtils(project: Project) {
6064
else -> {
6165
commands += "/bin/sh"
6266
commands += "-c"
63-
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"
67+
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"
6468
}
6569
}
6670
val process = Utils.runCommand(basePath, commands, timeoutAmount, timeUnit)
@@ -75,21 +79,25 @@ class GitUtils(project: Project) {
7579
fun getUserStats(
7680
startDate: String,
7781
endDate: String,
78-
timeoutAmount: Long = 60L,
79-
timeUnit: TimeUnit = TimeUnit.SECONDS,
80-
separator: String = "--"
82+
settingModel: SettingModel
8183
): Array<UserStats> {
82-
val gitCommand = listOf(
83-
"git",
84-
"log",
85-
"--numstat",
86-
"--date=iso",
87-
"--pretty=format:${separator}%h${separator}%ad${separator}%aN",
88-
"--since=$startDate",
89-
"--until=$endDate"
90-
)
84+
val timeoutAmount = 60L
85+
val timeUnit = TimeUnit.SECONDS
86+
val separator = "--"
87+
val os = Utils.getOS()
88+
val commands = mutableListOf<String>()
89+
val folder = if (settingModel.exclude.isEmpty()) "." else ". ':(exclude)${settingModel.exclude}'"
90+
if (os == "Windows" && gitBashExecutablePath?.isNotEmpty() == true) {
91+
commands += gitBashExecutablePath
92+
commands += "-c"
93+
commands += "git log --numstat --pretty=\"format:${separator}%h${separator}%ad${separator}%aN\" --since=\\\"${startDate}\\\" --until=\\\"${endDate}\\\" -- $folder"
94+
} else {
95+
commands += "/bin/sh"
96+
commands += "-c"
97+
commands += "$gitExecutablePath log --numstat --pretty=\"format:${separator}%h${separator}%ad${separator}%aN\" --since=\"$startDate\" --until=\"$endDate\" -- $folder"
98+
}
9199

92-
val process = Utils.runCommand(basePath, gitCommand, timeoutAmount, timeUnit)
100+
val process = Utils.runCommand(basePath, commands, timeoutAmount, timeUnit)
93101

94102
val userStatsData = mutableMapOf<String, UserStats>()
95103
process!!.inputStream.bufferedReader().use { reader ->

src/main/resources/messages/MyBundle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ refreshButtonLabel=Refresh
88
refreshButtonLoadingLabel=Loading
99
settingButtonTooltipText=Show Setting
1010
settingDialogModeLabel=Mode:
11+
settingDialogExcludeLabel=Exclude:

0 commit comments

Comments
 (0)