Skip to content

Commit

Permalink
Exclude current commit's tag for includeHistorySinceLastTag variabl…
Browse files Browse the repository at this point in the history
…e. (#26)
  • Loading branch information
mirland authored Apr 17, 2019
1 parent f7ef0b2 commit 60e6ca9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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"
}
Expand Down
27 changes: 16 additions & 11 deletions src/test/kotlin/com/xmartlabs/snapshotpublisher/ReleaseNotesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 60e6ca9

Please sign in to comment.