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

fix(toolkit): git log names with spaces error #226

Merged
merged 3 commits into from
Sep 15, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{ (meta.contributors | slice: 3).length }}
</div>
</div>
<div class="dg-contribution-last-time">
<div class="dg-contribution-last-time" *ngIf="meta.lastUpdatedTime">
{{ 'LAST_UPDATED_TIME' | dgTranslate }}
{{ meta.lastUpdatedTime | date: 'yyyy/MM/dd' }}
</div>
Expand Down
44 changes: 44 additions & 0 deletions packages/toolkit/src/git.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as shell from './shell';
import { contributors, lastUpdatedTime } from './git';

describe('git', () => {
it('should get lastUpdatedTime with name quotation', () => {
const time = Math.floor(Date.now() / 1000);
const fileName = `"test a".js`;
spyOn(shell, 'spawnSync').and.callFake((command, args, options) => {
expect(command).toBe('git');
expect(args).toEqual([`log`, `--pretty=format:"%at"`, '-n', '1', fileName]);
return {
stdout: Buffer.from(`"${time}"`)
} as any;
});
const result = lastUpdatedTime(fileName);
expect(result).toBe(time);
});
it('should get lastUpdatedTime with name space ', () => {
const time = Math.floor(Date.now() / 1000);
const fileName = `test a.js`;
spyOn(shell, 'spawnSync').and.callFake((command, args, options) => {
expect(command).toBe('git');
expect(args).toEqual([`log`, `--pretty=format:"%at"`, '-n', '1', fileName]);
return {
stdout: Buffer.from(`"${time}"`)
} as any;
});
const result = lastUpdatedTime(fileName);
expect(result).toBe(time);
});
it('should get contributors', () => {
const fileName = `"test a".js`;
spyOn(shell, 'spawnSync').and.callFake((command, args, options) => {
expect(command).toBe('git');
expect(args).toEqual([`log`, `--pretty=format:"%an"`, fileName]);
return {
stdout: Buffer.from(`"a"
"b"`)
} as any;
});
const result = contributors(fileName);
expect(result).toEqual(['a', 'b']);
});
});
28 changes: 16 additions & 12 deletions packages/toolkit/src/git.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import * as shell from './shell';

export function lastUpdatedTime(filePath: string): number {
return parseInt(shell.execSync(`git log --pretty=format:"%at" -n 1 ${filePath}`, { stdio: 'pipe' }).toString(), 10);
const result = shell.spawnSync(`git`, ['log', '--pretty=format:"%at"', '-n', '1', filePath], { stdio: 'pipe' }).stdout;
return result.length ? parseInt(JSON.parse(result.toString()), 10) : undefined;
}

export function contributors(filePath: string | string[]) {
return Array.from(
new Set(
shell
.execSync(`git log --pretty=format:"%an" ${typeof filePath === 'string' ? filePath : filePath.join(' ')}`, {
stdio: 'pipe'
})
.toString()
.split('\n')
)
);
export function contributors(filePath: string | string[]): string[] {
filePath = typeof filePath === 'string' ? [filePath] : filePath;
const result = shell.spawnSync('git', ['log', `--pretty=format:"%an"`, ...filePath], { stdio: 'pipe' }).stdout;
return result.length
? Array.from(
new Set(
result
.toString()
.split('\n')
.filter(item => item)
.map(item => JSON.parse(item))
)
)
: [];
}
5 changes: 4 additions & 1 deletion packages/toolkit/src/shell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export * from 'shelljs';
import { execSync as _execSync, ExecSyncOptions } from 'child_process';
import { execSync as _execSync, spawnSync as _spawnSync, ExecSyncOptions, SpawnSyncOptions } from 'child_process';

export function execSync(command: string, options: ExecSyncOptions = { stdio: 'inherit' }) {
return _execSync(command, options);
}
export function spawnSync(command: string, args: string[], options: SpawnSyncOptions = { stdio: 'inherit' }) {
return _spawnSync(command, args, options);
}