Skip to content

Commit 5121eb0

Browse files
authored
Merge pull request #2 from zhensherlock/support_windows
Support windows
2 parents f2923e9 + 053c70f commit 5121eb0

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.huayi.intellijplatform.gitstats.utils
22

33
import git4idea.config.GitExecutableManager
4+
import git4idea.config.GitExecutableDetector
45
import java.util.concurrent.TimeUnit
56
import com.intellij.openapi.project.Project
67

@@ -29,7 +30,10 @@ data class CommitFilesStats(
2930
class GitUtils(project: Project) {
3031
private val gitExecutablePath: String = GitExecutableManager.getInstance().getExecutable(project).exePath
3132
private val basePath: String = project.basePath as String
33+
private val gitBashExecutablePath: String? = GitExecutableDetector.getBashExecutablePath(gitExecutablePath)
3234

35+
// init {
36+
// }
3337
// companion object {
3438
// fun getGitExecutablePath(project: Project): String {
3539
// return GitExecutableManager.getInstance().getExecutable(project).exePath
@@ -40,12 +44,25 @@ class GitUtils(project: Project) {
4044
startDate: String, endDate: String, timeoutAmount: Long = 60L, timeUnit: TimeUnit = TimeUnit.SECONDS
4145
): Array<UserStats> {
4246
val os = Utils.getOS()
43-
val command = listOf(
44-
if (os == "Windows") "cmd" else "/bin/sh",
45-
if (os == "Windows") "/c" else "-c",
46-
"$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"
47-
)
48-
val process = Utils.runCommand(basePath, command, timeoutAmount, timeUnit)
47+
val commands = mutableListOf<String>()
48+
when {
49+
os == "Windows" && gitBashExecutablePath?.isNotEmpty() ?: false -> {
50+
commands += gitBashExecutablePath!!
51+
commands += "-c"
52+
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"
53+
}
54+
os == "Windows" && gitBashExecutablePath?.isEmpty() ?: false -> {
55+
commands += "powershell"
56+
commands += "/c"
57+
// commands += "C:\\\"Program Files\"\\Git\\cmd\\git.exe log --format='%aN' | sort -u | % { $name=$_; Write-Output $name; git log --author=$name --pretty=tformat: --since='2023-05-15 00:00:00' --until='2023-05-21 23:59:59' --numstat | ? { $_ -match '\\d' } | % { $add += [int]$_.Split()[0]; $subs += [int]$_.Split()[1]; $files++ } ; Write-Output ( 'added lines: ' + $add + ', removed lines: ' + $subs + ', modified files: ' + $files ) }"
58+
}
59+
else -> {
60+
commands += "/bin/sh"
61+
commands += "-c"
62+
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"
63+
}
64+
}
65+
val process = Utils.runCommand(basePath, commands, timeoutAmount, timeUnit)
4966
val regex = Regex("(.+)\\n+added lines: (\\d*), removed lines: (\\d+), modified files: (\\d+)")
5067
return regex.findAll(process!!.inputStream.bufferedReader().readText())
5168
.map { result ->
Lines changed: 6 additions & 7 deletions
Loading
Lines changed: 6 additions & 7 deletions
Loading

0 commit comments

Comments
 (0)