Skip to content

Commit bbd8621

Browse files
committed
feat: update user stats logic
1 parent e28ad1e commit bbd8621

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

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

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

3-
import java.io.File
43
import java.util.concurrent.TimeUnit
54

65
data class UserStats(
@@ -22,9 +21,7 @@ data class CommitStats(
2221
)
2322

2423
data class CommitFilesStats(
25-
var addedLines: Int = 0,
26-
var deletedLines: Int = 0,
27-
var fileName: String
24+
var addedLines: Int = 0, var deletedLines: Int = 0, var fileName: String
2825
)
2926

3027
class GitUtil(private val repoPath: String) {
@@ -48,13 +45,16 @@ class GitUtil(private val repoPath: String) {
4845
separator: String = "--"
4946
): Array<UserStats> {
5047
val gitCommand = listOf(
51-
"git", "log", "--numstat", "--date=iso",
48+
"git",
49+
"log",
50+
"--numstat",
51+
"--date=iso",
5252
"--pretty=format:${separator}%h${separator}%ad${separator}%aN",
53-
"--since=${startDate}",
54-
"--until=${endDate}"
53+
"--since=$startDate",
54+
"--until=$endDate"
5555
)
5656

57-
val process = runCommand(gitCommand, timeoutAmount, timeUnit)
57+
val process = Utils.runCommand(repoPath, gitCommand, timeoutAmount, timeUnit)
5858

5959
// val processBuilder = ProcessBuilder(gitCommand)
6060
// .directory(File(repoPath))

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

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

3+
import java.io.File
34
import java.time.DayOfWeek
45
import java.time.LocalDate
56
import java.time.temporal.TemporalAdjusters
67
import java.util.*
8+
import java.util.concurrent.TimeUnit
79

810
object Utils {
11+
fun getOS(): String {
12+
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
13+
return if (os.contains("win")) {
14+
"Windows"
15+
} else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
16+
"Unix"
17+
} else if (os.contains("mac")) {
18+
"OSX"
19+
} else {
20+
"Unknown"
21+
}
22+
}
23+
24+
fun runCommand(
25+
repoPath: String,
26+
cmd: List<String>,
27+
timeoutAmount: Long = 60L,
28+
timeUnit: TimeUnit = TimeUnit.SECONDS
29+
): Process? {
30+
return runCatching {
31+
ProcessBuilder(cmd)
32+
.directory(File(repoPath))
33+
.redirectErrorStream(true)
34+
.redirectOutput(ProcessBuilder.Redirect.PIPE)
35+
.start().also { it.waitFor(timeoutAmount, timeUnit) }
36+
}.onFailure { it.printStackTrace() }.getOrNull()
37+
}
38+
39+
fun runCommand(repoPath: String, vararg cmd: String): Process? = runCommand(repoPath, listOf(*cmd))
40+
941
fun getThisWeekDateRange(): Pair<LocalDate, LocalDate> {
1042
val now = LocalDate.now()
1143
val startOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))

0 commit comments

Comments
 (0)