refactor(dev-infra): move parseCommitMessagesForRange into the parse file (#39747)

When attempting to actually rely on `parseCommitMessagesForRange`, it became apparent
that the function really belongs in the parse file, rather than utils.

PR Close #39747
This commit is contained in:
Joey Perrott
2020-11-18 10:57:24 -08:00
committed by Andrew Kushnir
parent e2ce9af115
commit 881b77ef46
3 changed files with 30 additions and 40 deletions
+29
View File
@@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {exec} from '../utils/shelljs';
/** A parsed commit message. */
export interface ParsedCommitMessage {
header: string;
@@ -75,3 +77,30 @@ export function parseCommitMessage(commitMsg: string): ParsedCommitMessage {
isRevert: REVERT_PREFIX_RE.test(commitMsg),
};
}
/** Retrieve and parse each commit message in a provide range. */
export function parseCommitMessagesForRange(range: string): ParsedCommitMessage[] {
/** A random number used as a split point in the git log result. */
const randomValueSeparator = `${Math.random()}`;
/**
* Custom git log format that provides the commit header and body, separated as expected with the
* custom separator as the trailing value.
*/
const gitLogFormat = `%s%n%n%b${randomValueSeparator}`;
// Retrieve the commits in the provided range.
const result = exec(`git log --reverse --format=${gitLogFormat} ${range}`);
if (result.code) {
throw new Error(`Failed to get all commits in the range:\n ${result.stderr}`);
}
return result
// Separate the commits from a single string into individual commits.
.split(randomValueSeparator)
// Remove extra space before and after each commit message.
.map(l => l.trim())
// Remove any superfluous lines which remain from the split.
.filter(line => !!line)
// Parse each commit message.
.map(commit => parseCommitMessage(commit));
}