diff --git a/README.md b/README.md index b12b929..c6a6942 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,8 @@ By default this value is `false` because it's used in `{header}` - `includeHistorySinceLastTag`: Indicates the history start point. By default it's `false`, which means that all commits are used to get the history. -However, if its value is `true`, only the commits after the most recent tag will be included in the build history. +However, if its value is `true`, only the commits after the previous tag will be included in the build history. +The previous tag is the latest tag before the current commit. This is useful if you want to include in the history only the changes after the previous release. #### Other diff --git a/src/main/kotlin/com/xmartlabs/snapshotpublisher/utils/GitHelper.kt b/src/main/kotlin/com/xmartlabs/snapshotpublisher/utils/GitHelper.kt index 83b5653..5f67276 100644 --- a/src/main/kotlin/com/xmartlabs/snapshotpublisher/utils/GitHelper.kt +++ b/src/main/kotlin/com/xmartlabs/snapshotpublisher/utils/GitHelper.kt @@ -25,7 +25,7 @@ internal object GitHelper { return proc.inputStream.bufferedReader().readText().trim() } - private fun getLatestTag() = "git rev-list --tags --max-count=1".execute() + private fun getPreviousTag() = "git describe --tags --abbrev=0 HEAD^".execute() fun getCommitHash() = "git rev-parse --short HEAD".execute() @@ -38,7 +38,7 @@ internal object GitHelper { "git rev-list --count $logRange ${commandArg ?: ""}".execute().toInt() private fun getHistoryRange(releaseNotesConfig: ReleaseNotesConfig): String { - val startRange = if (releaseNotesConfig.includeHistorySinceLastTag) getLatestTag() else null + val startRange = if (releaseNotesConfig.includeHistorySinceLastTag) getPreviousTag() else null val endRange = "HEAD" + if (releaseNotesConfig.includeLastCommitInHistory) "" else "^" return if (startRange.isNullOrBlank()) endRange else "$startRange..$endRange" } diff --git a/src/test/kotlin/com/xmartlabs/snapshotpublisher/ReleaseNotesTest.kt b/src/test/kotlin/com/xmartlabs/snapshotpublisher/ReleaseNotesTest.kt index f91075d..1297bfc 100644 --- a/src/test/kotlin/com/xmartlabs/snapshotpublisher/ReleaseNotesTest.kt +++ b/src/test/kotlin/com/xmartlabs/snapshotpublisher/ReleaseNotesTest.kt @@ -6,6 +6,7 @@ import com.xmartlabs.snapshotpublisher.utils.ReleaseNotesGenerator import org.gradle.internal.impldep.org.eclipse.jgit.api.Git import org.gradle.internal.impldep.org.eclipse.jgit.lib.PersonIdent import org.gradle.internal.impldep.org.eclipse.jgit.lib.Repository +import org.gradle.internal.impldep.org.eclipse.jgit.revwalk.RevCommit import org.gradle.internal.impldep.org.eclipse.jgit.storage.file.FileRepositoryBuilder import org.junit.After import org.junit.Before @@ -25,7 +26,8 @@ class ReleaseNotesTest { private val REPO_FOLDER = File("/tmp/${UUID.randomUUID()}/") private val REPO_GIT_FOLDER = File("${REPO_FOLDER.absoluteFile}/.git") private val COMMIT_TIMEZONE = TimeZone.getTimeZone("GMT-3:00") - private const val TAG_POSITION = NUMBER_OF_COMMITS - 3 + private const val PREVIOUS_TAG = NUMBER_OF_COMMITS - 3 + private val TAG_POSITIONS = listOf(PREVIOUS_TAG, NUMBER_OF_COMMITS - 1) private val COMMIT_DATE = Calendar.getInstance() .apply { timeZone = COMMIT_TIMEZONE @@ -54,21 +56,24 @@ class ReleaseNotesTest { .setAllowEmpty(true) .call() + private fun Repository?.tag(revCommit: RevCommit) = Git(this).tag() + .apply { + name = "test.${revCommit.name()}" + objectId = revCommit + call() + } + @Before fun setup() { - val newlyCreatedRepo = FileRepositoryBuilder.create( + val repo = FileRepositoryBuilder.create( REPO_GIT_FOLDER ) - newlyCreatedRepo.create() - val commits = COMMITS.map { newlyCreatedRepo.addCommit(it) } + repo.create() + val commits = COMMITS.map { repo.addCommit(it) } GitHelper.defaultDir = REPO_FOLDER - Git(newlyCreatedRepo).tag() - .apply { - name = "test.tag" - objectId = commits[TAG_POSITION] - call() - } + TAG_POSITIONS.map { commits[it] } + .forEach { repo.tag(it) } } @After @@ -245,7 +250,7 @@ Last Changes: val lastCommit = COMMITS.size + if (config.includeLastCommitInHistory) 0 else -1 var initialCommitIndex = Math.max(COMMITS.size - config.maxCommitHistoryLines - 1, 0) if (config.includeHistorySinceLastTag) { - initialCommitIndex = Math.min(TAG_POSITION, initialCommitIndex) + initialCommitIndex = Math.min(PREVIOUS_TAG, initialCommitIndex) } val realHistory = COMMITS.subList(initialCommitIndex, lastCommit) .reversed()