|
| 1 | +import java.io.File |
1 | 2 | import org.eclipse.jgit.revwalk.RevCommit |
2 | 3 | import org.eclipse.jgit.revwalk.RevWalk |
3 | 4 | import org.eclipse.jgit.storage.file.FileRepositoryBuilder |
4 | | -import java.io.File |
5 | 5 |
|
6 | 6 | private const val UNSPECIFIED = "unspecified" |
7 | 7 |
|
8 | 8 | /** |
9 | | - * Returns a snapshot version string based on the abbreviated Git commit hash of HEAD. |
10 | | - * On error, returns "unspecified". |
| 9 | + * Returns a snapshot version string based on the abbreviated Git commit hash of HEAD. On error, |
| 10 | + * returns "unspecified". |
11 | 11 | * |
12 | 12 | * @param projectRootDir the root directory of the project (containing .git) |
13 | 13 | */ |
14 | 14 | fun getSnapshotVersion(projectRootDir: File): String { |
15 | | - return try { |
16 | | - val builder = FileRepositoryBuilder() |
| 15 | + return try { |
| 16 | + val builder = |
| 17 | + FileRepositoryBuilder() |
17 | 18 | .setGitDir(File(projectRootDir, ".git")) |
18 | 19 | .readEnvironment() |
19 | 20 | .findGitDir() |
20 | 21 |
|
21 | | - builder.build().use { repository -> |
22 | | - val headId = repository.resolve("HEAD") ?: return UNSPECIFIED |
| 22 | + builder.build().use { repository -> |
| 23 | + val headId = repository.resolve("HEAD") ?: return UNSPECIFIED |
23 | 24 |
|
24 | | - RevWalk(repository).use { revWalk -> |
25 | | - val headCommit: RevCommit = revWalk.parseCommit(headId) |
26 | | - headCommit.id.name.substring(0, 7) |
27 | | - } |
28 | | - } |
29 | | - } catch (e: Exception) { |
30 | | - System.err.println("Error determining version: $e") |
31 | | - UNSPECIFIED |
| 25 | + RevWalk(repository).use { revWalk -> |
| 26 | + val headCommit: RevCommit = revWalk.parseCommit(headId) |
| 27 | + headCommit.id.name.substring(0, 7) |
| 28 | + } |
32 | 29 | } |
| 30 | + } catch (e: Exception) { |
| 31 | + System.err.println("Error determining version: $e") |
| 32 | + UNSPECIFIED |
| 33 | + } |
33 | 34 | } |
0 commit comments