Skip to content

Commit

Permalink
build: use the timestamp as version number to avoid downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
NextAlone committed Dec 2, 2023
1 parent 5e03349 commit f8ab0d6
Showing 1 changed file with 3 additions and 34 deletions.
37 changes: 3 additions & 34 deletions buildSrc/src/main/kotlin/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,8 @@ object Common {

@JvmStatic
fun getBuildVersionCode(project: Project): Int {
// .git/HEAD描述当前目录所指向的分支信息,内容示例:"ref: refs/heads/master\n"
val headFile = File(project.rootProject.projectDir, ".git" + File.separator + "HEAD")
if (headFile.exists()) {
var commitHash: String
val strings = headFile.readText(Charsets.UTF_8).split(" ")
if (strings.size > 1) {
val refFilePath = ".git" + File.separator + strings[1];
// 根据HEAD读取当前指向的hash值,路径示例为:".git/refs/heads/master"
val refFile = File(project.rootProject.projectDir, refFilePath.replace("\n", "").replace("\r", ""));
// 索引文件内容为hash值+"\n",
commitHash = refFile.readText(Charsets.UTF_8)
} else {
commitHash = strings[0]
}
commitHash = commitHash.trim()
val repo = FileRepository(project.rootProject.file(".git"))
val refId = repo.resolve(commitHash)
val iterator = Git(repo).log().add(refId).call().iterator()
var commitCount = 0
while (iterator.hasNext()) {
commitCount++
iterator.next()
}
repo.close()
println("commit Count: $commitCount")
val targetVersionCode = commitCount + 1645300000
println("target versionCode: $targetVersionCode")
return targetVersionCode
} else {
println("WARN: .git/HEAD does NOT exist")
return 1
}
val currentTimestamp = (System.currentTimeMillis() / 1000L).toInt()
println("currentVersionCode: $currentTimestamp")
return currentTimestamp
}


}

0 comments on commit f8ab0d6

Please sign in to comment.