Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --sort-commits-by-date option #97

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ npm install -g auto-changelog

### Usage

Simply run `auto-changelog` in the root folder of a git repository. `git log` is run behind the scenes in order to parse the commit history.
Simply run `auto-changelog` in the root folder of a git repository.

Behind the scenes, `git log` is run in order to parse the commit history. Commits are ordered by relevance by default (i.e. breaking changes first, followed by commits that made the most changes). To order by date instead, see the `--sort-commits-by-date` option below.

```bash
Usage: auto-changelog [options]
Expand All @@ -37,6 +39,7 @@ Options:
--tag-pattern [regex] # override regex pattern for release tags
--tag-prefix [prefix] # prefix used in version tags, default: v
--starting-commit [hash] # starting commit to use for changelog generation
--sort-commits-by-date # sort commits by date (descending) instead of sorting by relevance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be better as a generic option that took a value of “date“, for extensibility?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick review! Yeah, that makes sense. I have no time to do it now though. Feel free to suggest an edit or commit yourself or whatever :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah a generic --sort-commits sounds good. I'll give it a shot.

--include-branch [branch] # one or more branches to include commits from, comma separated
--release-summary # display tagged commit message body as release summary
--stdout # output changelog to stdout
Expand Down
7 changes: 6 additions & 1 deletion src/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const COMMIT_MESSAGE_PATTERN = /\n+([\S\s]+)/
const NUMERIC_PATTERN = /^\d+(\.\d+)?$/

export function parseReleases (commits, remote, latestVersion, options) {
const sortCommits = options.sortCommitsByDate ? sortCommitsByDateDescending : sortCommitsByRelevance
let release = newRelease(latestVersion)
const releases = []
for (let commit of commits) {
Expand Down Expand Up @@ -117,12 +118,16 @@ function getSummary (message, releaseSummary) {
return null
}

function sortCommits (a, b) {
function sortCommitsByRelevance (a, b) {
if (!a.breaking && b.breaking) return 1
if (a.breaking && !b.breaking) return -1
return (b.insertions + b.deletions) - (a.insertions + a.deletions)
}

function sortCommitsByDateDescending (a, b) {
return a.date < b.date ? 1 : -1
}

function sliceCommits (commits, options, release) {
if (options.commitLimit === false) {
return commits
Expand Down
1 change: 1 addition & 0 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function getOptions (argv, pkg, dotOptions) {
.option('--tag-pattern [regex]', 'override regex pattern for release tags')
.option('--tag-prefix [prefix]', 'prefix used in version tags')
.option('--starting-commit [hash]', 'starting commit to use for changelog generation')
.option('--sort-commits-by-date', 'sort commits by date (descending) instead of sorting by relevance')
.option('--include-branch [branch]', 'one or more branches to include commits from, comma separated', str => str.split(','))
.option('--release-summary', 'use tagged commit message body as release summary')
.option('--stdout', 'output changelog to stdout')
Expand Down