diff --git a/src/__snapshots__/git.spec.ts.snap b/src/__snapshots__/git.spec.ts.snap new file mode 100644 index 00000000..aa681880 --- /dev/null +++ b/src/__snapshots__/git.spec.ts.snap @@ -0,0 +1,90 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`parseLogMessage should parse message 0 1`] = ` +Object { + "date": "1966-01-01", + "refName": "", + "sha": "a0000000", + "summary": "fix: some random fix which will be ignored", +} +`; + +exports[`parseLogMessage should parse message 1 1`] = ` +Object { + "date": "1966-01-01", + "refName": "tag: a-new-hope@4.0.0, tag: empire-strikes-back@5.0.0, tag: return-of-the-jedi@6.0.0", + "sha": "a0000001", + "summary": "fix: some random fix which will be ignored", +} +`; + +exports[`parseLogMessage should parse message 2 1`] = ` +Object { + "date": "1977-01-01", + "refName": "tag: v0.1.0", + "sha": "a0000002", + "summary": "Merge pull request #1 from star-wars", +} +`; + +exports[`parseLogMessage should parse message 3 1`] = ` +Object { + "date": "20001-01-01", + "refName": "RC;.;0.1", + "sha": "a0000003", + "summary": ";;fix;;;", +} +`; + +exports[`parseLogMessage should parse message 4 1`] = ` +Object { + "date": "20001-01-01", + "refName": "RC;.;0.1", + "sha": "a0000004", + "summary": "feat: add ", +} +`; + +exports[`parseLogMessage should parse message 5 1`] = ` +Object { + "date": "2020-01-01", + "refName": "a-new-rc<1>", + "sha": "a0000005", + "summary": "feat(app): dev -> rc1", +} +`; + +exports[`parseLogMessage should parse message 6 1`] = ` +Object { + "date": "2020-01-01", + "refName": "a-new-rc<1>", + "sha": "a0000006", + "summary": "feat(app): dev > rc1", +} +`; + +exports[`parseLogMessage should parse message 7 1`] = ` +Object { + "date": "<>", + "refName": "<>", + "sha": "a0000007", + "summary": "<>", +} +`; + +exports[`parseLogMessage should parse message 8 1`] = ` +Object { + "date": ">", + "refName": ">", + "sha": "a0000008", + "summary": ">", +} +`; + +exports[`parseLogMessage should parse message 9 1`] = `null`; + +exports[`parseLogMessage should parse message 10 1`] = `null`; + +exports[`parseLogMessage should parse message 11 1`] = `null`; + +exports[`parseLogMessage should parse message 12 1`] = `null`; diff --git a/src/git.spec.ts b/src/git.spec.ts new file mode 100644 index 00000000..818ac1c0 --- /dev/null +++ b/src/git.spec.ts @@ -0,0 +1,34 @@ +import { parseLogMessage } from "./git"; + +const messages = [ + "hash ref<> message date<1966-01-01>", + "hash ref message date<1966-01-01>", + "hash ref message date<1977-01-01>", + "hash ref message<;;fix;;;> date<20001-01-01>", + "hash ref message> date<20001-01-01>", + "hash ref> message rc1> date<2020-01-01>", + "hash ref> message rc1> date<2020-01-01>", + "hash ref<<>> message<<>> date<<>>", + "hash ref<>> message<>> date<>>", + // nullable results + "hash ref> message rc1>", + "hash ref>", + "hash", + "hash date<2020-01-01>", +]; + +describe("parseLogMessage", function() { + it("should return null for empty message", function() { + expect(parseLogMessage("")).toEqual(null); + }); + + it("should return null for message with invalid format", function() { + expect(parseLogMessage("hash")).toEqual(null); + }); + + messages.forEach((message, i) => { + it("should parse message " + i, function() { + expect(parseLogMessage(message)).toMatchSnapshot(); + }); + }); +}); diff --git a/src/git.ts b/src/git.ts index 7372fa4c..0be7c7f7 100644 --- a/src/git.ts +++ b/src/git.ts @@ -29,19 +29,34 @@ export interface CommitListItem { date: string; } +export function parseLogMessage(commit: string): CommitListItem | null { + const parts = commit.match(/hash<(.+)> ref<(.*)> message<(.*)> date<(.*)>/) || []; + + if (!parts || parts.length === 0) { + return null; + } + + return { + sha: parts[1], + refName: parts[2], + summary: parts[3], + date: parts[4], + }; +} + export function listCommits(from: string, to: string = ""): CommitListItem[] { - // Prints ";;;" + // Prints "hash ref message date" // This format is used in `getCommitInfos` for easily analize the commit. return execa - .sync("git", ["log", "--oneline", "--pretty=%h;%D;%s;%cd", "--date=short", `${from}..${to}`]) + .sync("git", [ + "log", + "--oneline", + "--pretty=hash<%h> ref<%D> message<%s> date<%cd>", + "--date=short", + `${from}..${to}`, + ]) .stdout.split("\n") .filter(Boolean) - .map((commit: string) => { - const parts = commit.split(";"); - const sha = parts[0]; - const refName = parts[1]; - const summary = parts[2]; - const date = parts[3]; - return { sha, refName, summary, date }; - }); + .map(parseLogMessage) + .filter(Boolean); }