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

Allow ; characters in merge commit messages #259

Merged
merged 2 commits into from
Mar 3, 2020
Merged
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
90 changes: 90 additions & 0 deletions src/__snapshots__/git.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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 <App/>",
}
`;

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`;
34 changes: 34 additions & 0 deletions src/git.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { parseLogMessage } from "./git";

const messages = [
"hash<a0000000> ref<> message<fix: some random fix which will be ignored> date<1966-01-01>",
"hash<a0000001> ref<tag: a-new-hope@4.0.0, tag: empire-strikes-back@5.0.0, tag: return-of-the-jedi@6.0.0> message<fix: some random fix which will be ignored> date<1966-01-01>",
"hash<a0000002> ref<tag: v0.1.0> message<Merge pull request #1 from star-wars> date<1977-01-01>",
"hash<a0000003> ref<RC;.;0.1> message<;;fix;;;> date<20001-01-01>",
"hash<a0000004> ref<RC;.;0.1> message<feat: add <App/>> date<20001-01-01>",
"hash<a0000005> ref<a-new-rc<1>> message<feat(app): dev -> rc1> date<2020-01-01>",
"hash<a0000006> ref<a-new-rc<1>> message<feat(app): dev > rc1> date<2020-01-01>",
"hash<a0000007> ref<<>> message<<>> date<<>>",
"hash<a0000008> ref<>> message<>> date<>>",
// nullable results
"hash<a0000009> ref<a-new-rc<1>> message<feat(app): dev > rc1>",
"hash<a00000010> ref<a-new-rc<1>>",
"hash<a0000011>",
"hash<a0000012> 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<a000002 ref<%>")).toEqual(null);
});

messages.forEach((message, i) => {
it("should parse message " + i, function() {
expect(parseLogMessage(message)).toMatchSnapshot();
});
});
});
35 changes: 25 additions & 10 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<short-hash>;<ref-name>;<summary>;<date>"
// Prints "hash<short-hash> ref<ref-name> message<summary> date<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);
}