@@ -2,8 +2,10 @@ package versioning
22
33import groovy.transform.CompileStatic
44import groovy.transform.TypeChecked
5+ import org.eclipse.jgit.api.Git
56import org.eclipse.jgit.lib.ObjectId
67import org.eclipse.jgit.lib.Repository
8+ import org.eclipse.jgit.revwalk.RevCommit
79import org.eclipse.jgit.revwalk.RevWalk
810import org.eclipse.jgit.storage.file.FileRepositoryBuilder
911import org.joda.time.DateTime
@@ -12,30 +14,41 @@ import org.joda.time.DateTimeZone
1214@CompileStatic @TypeChecked
1315class GitVersioner {
1416
15- static GitInfo versionForDir (String dir ) {
16- versionForDir(new File (dir))
17- }
18-
19- static GitInfo versionForDir (File dir ) {
20- FileRepositoryBuilder builder = new FileRepositoryBuilder ()
21- Repository repo = builder. setWorkTree(dir)
22- .findGitDir()
23- .build()
24-
25- ObjectId head = repo. resolve(' HEAD' )
26- if (! head) {
27- return null
28- }
29-
30- def commit = new RevWalk (repo). parseCommit(head)
31- def branch = repo. getBranch()
32- def commitDate = new DateTime (1000L * commit. commitTime, DateTimeZone . UTC )
33- String tag = repo. tags. find { kv -> kv. value. objectId == commit. id }?. key
34-
35- return new GitInfo (
36- lastCommitDate : commitDate,
37- branch : branch,
38- tag : tag
39- )
40- }
17+ static GitInfo versionForDir (String dir ) {
18+ versionForDir(new File (dir))
19+ }
20+ static int getCountCommit (Repository repo ) {
21+ Iterable<RevCommit > commits = Git . wrap(repo). log(). call()
22+ int count = 0 ;
23+ commits. each {
24+ count++ ;
25+ }
26+
27+ return count;
28+ }
29+ static GitInfo versionForDir (File dir ) {
30+ FileRepositoryBuilder builder = new FileRepositoryBuilder ()
31+ Repository repo = builder. setWorkTree(dir)
32+ .findGitDir()
33+ .build()
34+
35+ ObjectId head = repo. resolve(' HEAD' )
36+ if (! head) {
37+ return null
38+ }
39+
40+ def commit = new RevWalk (repo). parseCommit(head)
41+ def branch = repo. getBranch()
42+ def commitDate = new DateTime (1000L * commit. commitTime, DateTimeZone . UTC )
43+ int commitCount = getCountCommit(repo);
44+
45+ String tag = repo. tags. find { kv -> kv. value. objectId == commit. id }?. key
46+
47+ return new GitInfo (
48+ lastCommitDate : commitDate,
49+ branch : branch,
50+ tag : tag,
51+ countCommit : commitCount
52+ )
53+ }
4154}
0 commit comments