-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
114 lines (82 loc) · 3 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.text.SimpleDateFormat
def purgeBranchesOlderThan(days) {
println("Trying to purge branches that are older than `$days`. Updating remote: `${updateRemote()}`")
"git config core.sparsecheckout false".execute()
// fetch on CI
"git config core.sparsecheckout false".execute()
// checkout master
"git checkout master".execute()
"git pull".execute()
// get all branches
def branches = "git branch -r".execute().text.split("\n")
println("branches $branches")
for (branch in branches) {
println("branches $branch")
}
println("\nStale Branches (older than $days days)\n")
// run through all branches
for (branch in branches) {
// remove white spaces at the edges
branch = branch.trim()
// ignore master and develop
if (branch.contains("master")) continue
if (branch.contains("develop")) continue
// figure out if there are any commits ahead master since x days
def logs = "git log master..$branch --pretty=oneline --since=${days}.days.ago --stat".execute().text
// tag and remove branches where there were no commits since x days
if (logs.isEmpty()) {
printStatsFor(branch)
def remote = updateRemote()
tag(branch, remote)
close(branch, remote)
}
}
}
static def tag(branch, updateRemote) {
branch = branch.replace("origin/", "")
// tag
"git tag stale/$branch".execute().text
if (!updateRemote) return
// push to origin
"git push origin stale/$branch".execute()
}
static def close(branch, updateRemote) {
branch = branch.replace("origin/", "")
if (!updateRemote) return
"git push origin --delete $branch".execute()
}
def days() {
if (System.hasProperty("days")) {
return System.getProperty("days")
} else if (project.hasProperty("days")) {
return project.getProperty("days")
} else {
// everything higher than ~ 1643384300 will cause overflow
return 1600000000
}
}
def updateRemote() {
if (System.hasProperty("updateRemote")) {
return System.getProperty("updateRemote") == "true"
} else if (project.hasProperty("updateRemote")) {
return project.getProperty("updateRemote") == "true"
} else {
return false
}
}
def printStatsFor(branch) {
// 2022-01-30 14:45:33 +0100
def dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z")
def lastCommit = "git log master..$branch -1 --format=%ai".execute().text.trim()
if (lastCommit == "") return
def commitDate = dateFormatter.parse(lastCommit)
def duration = groovy.time.TimeCategory.minus(new Date(), commitDate)
def behind = "git rev-list $branch..master".execute().text.split('\n').size()
def ahead = "git rev-list master..$branch".execute().text.split('\n').size()
println("Last commit before $duration | Ahead: $ahead | Behind: $behind | `$branch`")
}
task purge() {
doLast {
purgeBranchesOlderThan(days())
}
}