Skip to content

Commit

Permalink
feat: --excludeCommits ignores commits which message match this pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
shybyte committed Aug 4, 2018
1 parent 6a9908d commit 4ac8d65
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/lib/cli.ts
Expand Up @@ -19,7 +19,8 @@ export function runCli(argv: string[], version = 'unknown'): ExitCode {
.option('-i, --include [paths]', 'include regexp file filter', parseList, [])
.option('-e, --exclude [paths]', 'exclude regexp file filter', parseList, [])
.option('--fix', 'adds or updates copyright header to files', false)
.option('--copyrightHolder <name>', 'Copyright Holder');
.option('--copyrightHolder <name>', 'Copyright Holder')
.option('--excludeCommits [pattern]', 'ignores commits which message match this pattern');

const options: Options = commander.parse(argv) as any;

Expand All @@ -32,7 +33,8 @@ export function runCli(argv: string[], version = 'unknown'): ExitCode {
include: options.include,
fix: options.fix,
exclude: options.exclude,
copyrightHolder: options.copyrightHolder
copyrightHolder: options.copyrightHolder,
excludeCommits: options.excludeCommits,
});

return result.unFixedFiles.length ? ExitCode.ERROR : ExitCode.OK;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/copyright-header.ts
Expand Up @@ -20,6 +20,7 @@ export interface FileFilter {
export interface Options extends FileFilter {
readonly copyrightHolder: string;
readonly fix: boolean;
readonly excludeCommits?: string;
}

interface ValidationResult {
Expand All @@ -28,7 +29,7 @@ interface ValidationResult {

export function ensureUpdatedCopyrightHeader(opts: Options): ValidationResult {
const files = collectFiles(opts);
const fileInfos: FileInfo[] = files.map(getFileInfoFromGit);
const fileInfos: FileInfo[] = files.map(f => getFileInfoFromGit(f, opts.excludeCommits));
const unFixedFiles = [];

for (const fileInfo of fileInfos) {
Expand Down
9 changes: 7 additions & 2 deletions src/lib/git.spec.ts
Expand Up @@ -3,13 +3,18 @@
// tslint:disable:no-expression-statement no-object-mutation

import { test } from 'ava';
import { getFileInfoFromGit } from './git';
import { getFileInfoFromGit, testExports } from './git';

test.only('getFileInfoFromGit', t => {
test('getFileInfoFromGit', t => {
const fileinfo = getFileInfoFromGit('README.md');

t.is(fileinfo.filename, 'README.md');
t.is(fileinfo.createdYear, 2018);
t.true(!isNaN(fileinfo.updatedYear));
t.true(fileinfo.updatedYear >= 2018);
});

test('invertedGrepOptions', t => {
t.is(testExports.invertedGrepOptions('pattern'), '--invert-grep --grep=pattern');
t.is(testExports.invertedGrepOptions(), '');
});
14 changes: 12 additions & 2 deletions src/lib/git.ts
Expand Up @@ -14,12 +14,22 @@ export function getGitFiles(): string[] {
return execToLines('git ls-files');
}

export function getFileInfoFromGit(filename: string): FileInfo {
const logDates = execToLines(`git log --format=%aD --follow -- ${filename}`);

function invertedGrepOptions(excludeCommitPattern?: string): string {
return excludeCommitPattern ? '--invert-grep --grep=' + excludeCommitPattern : '';
}

export function getFileInfoFromGit(filename: string, excludeCommits?: string): FileInfo {
const grepFlag = invertedGrepOptions(excludeCommits);
const logDates = execToLines(`git log --format=%aD --follow ${grepFlag} -- ${filename}`);

return {
filename,
createdYear: new Date(logDates[logDates.length - 1]).getFullYear(),
updatedYear: new Date(logDates[0]).getFullYear()
};
}

export const testExports = {
invertedGrepOptions
};

0 comments on commit 4ac8d65

Please sign in to comment.