diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle deleted file mode 100644 index bbfeb03c22323e48f71748532b9b1c37d812fc9a..0000000000000000000000000000000000000000 --- a/buildSrc/build.gradle +++ /dev/null @@ -1 +0,0 @@ -apply plugin: 'java' diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts new file mode 100644 index 0000000000000000000000000000000000000000..0ab4d06fab24683db01ed3bc2b732c01d88a6e5a --- /dev/null +++ b/buildSrc/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + id("java") +} + +repositories { + mavenCentral() +} + +dependencies { + implementation(gradleApi()) + implementation(localGroovy()) + implementation("org.eclipse.jgit:org.eclipse.jgit:6.0.0.202111291000-r") +} diff --git a/buildSrc/src/main/java/org/briarproject/briar/desktop/builddata/GenerateBuildDataSourceTask.java b/buildSrc/src/main/java/org/briarproject/briar/desktop/builddata/GenerateBuildDataSourceTask.java index 4a163d76a996e1e20cfd792076684b920b67217c..0dc1deacf617da9224068a1374632c3e11cb2951 100644 --- a/buildSrc/src/main/java/org/briarproject/briar/desktop/builddata/GenerateBuildDataSourceTask.java +++ b/buildSrc/src/main/java/org/briarproject/briar/desktop/builddata/GenerateBuildDataSourceTask.java @@ -1,15 +1,25 @@ package org.briarproject.briar.desktop.builddata; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.gradle.api.GradleScriptException; import org.gradle.api.Project; import org.gradle.api.tasks.TaskAction; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Iterator; +import java.util.NoSuchElementException; public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { @@ -27,10 +37,33 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { className = "BuildData"; } + /* + * Get version from Gradle project information + */ String version = project.getVersion().toString(); - long buildTime = System.currentTimeMillis(); - // TODO: fetch correct current git hash using JGit in a platform-independent way - String gitHash = "abcdefgh"; + + /* + * Get Git hash and last commit time using JGit + */ + // Open git repository + File dir = project.getProjectDir(); + Git git = Git.open(dir); + Repository repository = git.getRepository(); + + // Get head ref and it's name => current hash + ObjectId head = repository.resolve(Constants.HEAD); + String gitHash = head.getName(); + + // Get latest commit and its commit time + RevCommit first; + try { + first = getLastCommit(git); + } catch (GitAPIException | NoSuchElementException e) { + throw new GradleScriptException("Error while fetching commits", e); + } + + // Convert from seconds to milliseconds + long commitTime = first.getCommitTime() * 1000L; if (packageName == null) { throw new IllegalStateException("Please specify 'packageName'."); @@ -49,17 +82,26 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { Files.createDirectories(path); Path file = path.resolve(className + ".java"); - String content = - createSource(packageName, className, version, buildTime, - gitHash); + String content = createSource(packageName, className, version, + commitTime, gitHash); InputStream in = new ByteArrayInputStream( content.getBytes(StandardCharsets.UTF_8)); Files.copy(in, file, StandardCopyOption.REPLACE_EXISTING); } + private RevCommit getLastCommit(Git git) throws GitAPIException { + Iterable<RevCommit> commits = git.log().call(); + + Iterator<RevCommit> iterator = commits.iterator(); + if (!iterator.hasNext()) { + throw new NoSuchElementException(); + } + return iterator.next(); + } + private String createSource(String packageName, String className, - String version, long timestamp, String gitHash) { + String version, long gitTime, String gitHash) { FileBuilder buffer = new FileBuilder(); // // this file is generated, do not edit // package org.briarproject.briar.desktop; @@ -76,11 +118,11 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { buffer.line(" public static String getVersion() {"); buffer.line(" return \"" + version + "\";"); buffer.line(" }"); - // public static long getBuildTime() { + // public static long getGitTime() { // return 1641645802088L; // } - buffer.line(" public static long getBuildTime() {"); - buffer.line(" return " + timestamp + "L;"); + buffer.line(" public static long getGitTime() {"); + buffer.line(" return " + gitTime + "L;"); buffer.line(" }"); // public static long getGitHash() { // return "749dda081c3e7862050255817bc239b9255b1582"; diff --git a/src/main/kotlin/org/briarproject/briar/desktop/Main.kt b/src/main/kotlin/org/briarproject/briar/desktop/Main.kt index f45e31ce789b8bab70dd8fc1298b72b05b86e750..f697bceb49a0cb58f7d2c2775a34f36e369fcb66 100644 --- a/src/main/kotlin/org/briarproject/briar/desktop/Main.kt +++ b/src/main/kotlin/org/briarproject/briar/desktop/Main.kt @@ -74,11 +74,11 @@ private class Main : CliktCommand( LogUtils.setupLogging(level) - val buildTime = Instant.ofEpochMilli(BuildData.getBuildTime()).atZone(ZoneId.systemDefault()).toLocalDateTime() + val buildTime = Instant.ofEpochMilli(BuildData.getGitTime()).atZone(ZoneId.systemDefault()).toLocalDateTime() val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") LOG.info { "This is briar-desktop version ${BuildData.getVersion()}" } - LOG.info { "Build time ${formatter.format(buildTime)}" } LOG.info { "Built from Git hash ${BuildData.getGitHash()}" } + LOG.info { "Last commit time ${formatter.format(buildTime)}" } val dataDir = getDataDir() val app =