Skip to content

Commit dc6688d

Browse files
devversionjelbourn
authored andcommitted
build: skip duplicate commit entries in changelog (#12943)
Ensures that commits are not showing up multiple times in the changelog. Commits can show up multiple times, if a changelog has been generated on a publish branch and has been copied over to "master". In that case, the changelog will already contain the commits that have been cherry-picked into the publish branch. These shouldn't be added twice. Closes #12915
1 parent cfb4f58 commit dc6688d

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

tools/gulp/tasks/changelog.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import {task, src, dest} from 'gulp';
1+
import {grey, red, yellow} from 'chalk';
2+
import {readFileSync} from 'fs';
3+
import {dest, src, task} from 'gulp';
24
import {buildConfig} from 'material2-build-tools';
35
import {join} from 'path';
4-
import {yellow, red} from 'chalk';
56

67
// This imports lack of type definitions.
78
const gulpChangelog = require('gulp-conventional-changelog');
@@ -26,7 +27,7 @@ task('changelog', async () => {
2627
}
2728

2829
return src(changelogFile)
29-
.pipe(gulpChangelog(changelogOptions))
30+
.pipe(gulpChangelog(changelogOptions, null, null, null, createDedupeWriterOptions()))
3031
.pipe(dest('./'));
3132
});
3233

@@ -58,3 +59,33 @@ function getLatestSemverTag(): Promise<string> {
5859
return gitSemverTags((err: Error, tags: string[]) => err ? reject(err) : resolve(tags[0]));
5960
});
6061
}
62+
63+
/**
64+
* Creates changelog writer options which ensure that commits are not showing up multiple times.
65+
*
66+
* Commits can show up multiple times, if a changelog has been generated on a publish branch
67+
* and has been copied over to "master". In that case, the changelog will already contain the
68+
* commits that have been cherry-picked into the publish branch. These shouldn't be added twice.
69+
*/
70+
function createDedupeWriterOptions() {
71+
const previousContent = readFileSync(changelogFile, 'utf8');
72+
73+
return {
74+
// Change writer option that can be used to modify the content of a new changelog section.
75+
// See: conventional-changelog/tree/master/packages/conventional-changelog-writer
76+
finalizeContext: (context: any) => {
77+
context.commitGroups.forEach((group: any) => {
78+
group.commits = group.commits.filter((commit: any) => {
79+
// Note that we cannot compare the SHA's because the commits will have a different SHA
80+
// if they are being cherry-picked into a different branch.
81+
if (previousContent.includes(commit.header)) {
82+
console.log(grey(`Skipping: "${commit.header}" (${commit.hash})`));
83+
return false;
84+
}
85+
return true;
86+
});
87+
});
88+
return context;
89+
}
90+
};
91+
}

0 commit comments

Comments
 (0)