-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.gradle
46 lines (44 loc) · 1.72 KB
/
common.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
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.internal.storage.file.FileRepository
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("org.eclipse.jgit:org.eclipse.jgit:5.10.0.202012080955-r")
}
}
gradle.allprojects {
ext.getGitHeadRefsSuffix = {
// .git/HEAD描述当前目录所指向的分支信息,内容示例:"ref: refs/heads/master\n"
def headFile = new File(rootProject.projectDir, '.git' + File.separator + 'HEAD')
if (headFile.exists()) {
def commitHash
String[] strings = headFile.getText('UTF-8').split(" ");
if (strings.size() > 1) {
String refFilePath = '.git' + File.separator + strings[1];
// 根据HEAD读取当前指向的hash值,路径示例为:".git/refs/heads/master"
def refFile = new File(rootProject.projectDir, refFilePath.replace("\n", "").replace("\r", ""));
// 索引文件内容为hash值+"\n",
commitHash = refFile.getText('UTF-8')
} else {
commitHash = strings[0]
}
commitHash = commitHash.trim()
def repo = new FileRepository(rootProject.file(".git"))
def refId = repo.resolve(commitHash)
def iterator = new Git(repo).log().add(refId).call().iterator()
def commitCount = 0
while (iterator.hasNext()) {
commitCount++
iterator.next()
}
repo.close()
return ".r" + commitCount + "." + commitHash.substring(0, 7)
} else {
println("WARN: .git/HEAD does NOT exist")
return ""
}
}
}