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

Gracefully handle gpg signature info in git log output #833

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
68 changes: 67 additions & 1 deletion node-src/git/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
import { execaCommand } from 'execa';
import { describe, expect, it, vi } from 'vitest';

import { getSlug } from './git';
import { getCommit, getSlug, hasPreviousCommit } from './git';

vi.mock('execa');

const command = vi.mocked(execaCommand);

describe('getCommit', () => {
it('parses log output', async () => {
command.mockImplementation(
() =>
Promise.resolve({
all: `19b6c9c5b3d34d9fc55627fcaf8a85bd5d5e5b2a ## 1696588814 ## info@ghengeveld.nl ## Gert Hengeveld`,
}) as any
);
expect(await getCommit()).toEqual({
commit: '19b6c9c5b3d34d9fc55627fcaf8a85bd5d5e5b2a',
committedAt: 1696588814 * 1000,
committerEmail: 'info@ghengeveld.nl',
committerName: 'Gert Hengeveld',
});
});

it('ignores gpg signature information', async () => {
command.mockImplementation(
() =>
Promise.resolve({
all: `
gpg: Signature made Fri Oct 6 12:40:14 2023 CEST
gpg: using RSA key 4AEE18F83AFDEB23
gpg: Can't check signature: No public key
19b6c9c5b3d34d9fc55627fcaf8a85bd5d5e5b2a ## 1696588814 ## info@ghengeveld.nl ## Gert Hengeveld
`.trim(),
}) as any
);
expect(await getCommit()).toEqual({
commit: '19b6c9c5b3d34d9fc55627fcaf8a85bd5d5e5b2a',
committedAt: 1696588814 * 1000,
committerEmail: 'info@ghengeveld.nl',
committerName: 'Gert Hengeveld',
});
});
});

describe('getSlug', () => {
it('returns the slug portion of the git url', async () => {
command.mockImplementation(
Expand All @@ -25,3 +62,32 @@ describe('getSlug', () => {
expect(await getSlug()).toBe('foo/bar.baz');
});
});

describe('hasPreviousCommit', () => {
it('returns true if a commit is found', async () => {
command.mockImplementation(
() => Promise.resolve({ all: `19b6c9c5b3d34d9fc55627fcaf8a85bd5d5e5b2a` }) as any
);
expect(await hasPreviousCommit()).toEqual(true);
});

it('returns false if no commit is found', async () => {
command.mockImplementation(() => Promise.resolve({ all: `` }) as any);
expect(await hasPreviousCommit()).toEqual(false);
});

it('ignores gpg signature information', async () => {
command.mockImplementation(
() =>
Promise.resolve({
all: `
gpg: Signature made Fri Oct 6 12:40:14 2023 CEST
gpg: using RSA key 4AEE18F83AFDEB23
gpg: Can't check signature: No public key
19b6c9c5b3d34d9fc55627fcaf8a85bd5d5e5b2a
`.trim(),
}) as any
);
expect(await hasPreviousCommit()).toEqual(true);
});
});
12 changes: 10 additions & 2 deletions node-src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ export async function getCommit(revision = '') {
// Technically this yields the author info, not committer info
`git --no-pager log -n 1 --format="%H ## %ct ## %ae ## %an" ${revision}`
);
const [commit, committedAtSeconds, committerEmail, committerName] = result.split(' ## ');

// Ignore lines that don't match the expected format (e.g. gpg signature info)
const format = new RegExp('^[a-f0-9]+ ## ');
const data = result.split('\n').find((line: string) => format.test(line));

const [commit, committedAtSeconds, committerEmail, committerName] = data.split(' ## ');
const committedAt = Number(committedAtSeconds) * 1000;
return { commit, committedAt, committerEmail, committerName };
}
Expand Down Expand Up @@ -119,7 +124,10 @@ export async function getUncommittedHash() {

export async function hasPreviousCommit() {
const result = await execGitCommand(`git --no-pager log -n 1 --skip=1 --format="%H"`);
return !!result.trim();

// Ignore lines that don't match the expected format (e.g. gpg signature info)
const allhex = new RegExp('^[a-f0-9]+$');
return result.split('\n').some((line: string) => allhex.test(line));
}

// Check if a commit exists in the repository
Expand Down
Loading