8000 Pr/908 (#965) · steveukx/git-js@df14065 · GitHub
[go: up one dir, main page]

Skip to content

Commit df14065

Browse files
steveukxVinzent03
andauthored
Pr/908 (#965)
* feat: add status to DiffResult * add changeset * fix: handle rename in log --name-status * fix: add status to DiffResultBinaryFile * Support for renamed files in `--name-status` in `git.log` where the file name is a single character. Add integration test for name-status renames. --------- Co-authored-by: Vinzent <vinzent03@proton.me>
1 parent 79d3262 commit df14065

File tree

10 files changed

+131
-14
lines changed

10 files changed

+131
-14
lines changed

.changeset/silver-impalas-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'simple-git': minor
3+
---
4+
5+
add status to DiffResult when using --name-status

simple-git/src/lib/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import { TaskConfigurationError } from './errors/task-configuration-error';
77
import { CheckRepoActions } from './tasks/check-is-repo';
88
import { CleanOptions } from './tasks/clean';
99
import { GitConfigScope } from './tasks/config';
10+
import { DiffNameStatus } from './tasks/diff-name-status';
1011
import { grepQueryBuilder } from './tasks/grep';
1112
import { ResetMode } from './tasks/reset';
1213

1314
export {
1415
CheckRepoActions,
1516
CleanOptions,
17+
DiffNameStatus,
1618
GitConfigScope,
1719
GitConstructError,
1820
GitError,

simple-git/src/lib/parsers/parse-diff-summary.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { DiffResult } from '../../../typings';
22
import { LogFormat } from '../args/log-format';
33
import { DiffSummary } from '../responses/DiffSummary';
4-
import { asNumber, LineParser, parseStringResponse } from '../utils';
4+
import { isDiffNameStatus } from '../tasks/diff-name-status';
5+
import { asNumber, LineParser, orVoid, parseStringResponse } from '../utils';
56

67
const statParser = [
78
new LineParser<DiffResult>(
@@ -86,16 +87,20 @@ const nameOnlyParser = [
8687
];
8788

8889
const nameStatusParser = [
89-
new LineParser<DiffResult>(/([ACDMRTUXB])\s*(.+)$/, (result, [_status, file]) => {
90-
result.changed++;
91-
result.files.push({
92-
file,
93-
changes: 0,
94-
insertions: 0,
95-
deletions: 0,
96-
binary: false,
97-
});
98-
}),
90+
new LineParser<DiffResult>(
91+
/([ACDMRTUXB])([0-9]{0,3})\t(.[^\t]*)(\t(.[^\t]*))?$/,
92+
(result, [status, _similarity, from, _to, to]) => {
93+
result.changed++;
94+
result.files.push({
95+
file: to ?? from,
96+
changes: 0,
97+
status: orVoid(isDiffNameStatus(status) && status),
98+
insertions: 0,
99+
deletions: 0,
100+
binary: false,
101+
});
102+
}
103+
),
99104
];
100105

101106
const diffSummaryParsers: Record<LogFormat, LineParser<DiffResult>[]> = {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export enum DiffNameStatus {
2+
ADDED = 'A',
3+
COPIED = 'C',
4+
DELETED = 'D',
5+
MODIFIED = 'M',
6+
RENAMED = 'R',
7+
CHANGED = 'T',
8+
UNMERGED = 'U',
9+
UNKNOWN = 'X',
10+
BROKEN = 'B',
11+
}
12+
13+
const diffNameStatus = new Set(Object.values(DiffNameStatus));
14+
15+
export function isDiffNameStatus(input: string): input is DiffNameStatus {
16+
return diffNameStatus.has(input as DiffNameStatus);
17+
}

simple-git/src/lib/utils/util.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,10 @@ export function pick(source: Record<string, any>, properties: string[]) {
157157
export function delay(duration = 0): Promise<void> {
158158
return new Promise((done) => setTimeout(done, duration));
159159
}
160+
161+
export function orVoid<T>(input: T | false) {
162+
if (input === false) {
163+
return undefined;
164+
}
165+
return input;
166+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
createTestContext,
3+
like,
4+
newSimpleGit,
5+
setUpFilesAdded,
6+
setUpInit,
7+
SimpleGitTestContext,
8+
} from '@simple-git/test-utils';
9+
10+
import { DiffNameStatus, DiffResultTextFile } from '../..';
11+
12+
describe('log-name-status', function () {
13+
let context: SimpleGitTestContext;
14+
const steps = ['mv a b', 'commit -m two'];
15+
16+
beforeEach(async () => {
17+
context = await createTestContext();
18+
await setUpInit(context);
19+
await setUpFilesAdded(context, ['a'], '.', 'one');
20+
for (const step of steps) {
21+
await context.git.raw(step.split(' '));
22+
}
23+
});
24+
25+
it('detects files moved with --name-status', async () => {
26+
const actual = await newSimpleGit(context.root).log(['--name-status']);
27+
28+
expect(actual.all).toEqual([
29+
mockListLogLine('two', { b: DiffNameStatus.RENAMED }),
30+
mockListLogLine('one', { a: DiffNameStatus.ADDED }),
31+
]);
32+
});
33+
});
34+
35+
function mockListLogLine(message: string, changes: Record<string, DiffNameStatus>) {
36+
const files: DiffResultTextFile[] = Object.entries(changes).map(([file, status]) => {
37+
return {
38+
binary: false,
39+
changes: 0,
40+
deletions: 0,
41+
file,
42+
insertions: 0,
43+
status,
44+
};
45+
});
46+
return like({
47+
message,
48+
diff: like({ changed: files.length, deletions: 0, insertions: 0, files }),
49+
});
50+
}

simple-git/test/unit/diff.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,12 @@ describe('diff', () => {
314314

315315
it('diffSummary with --name-status', async () => {
316316
const task = git.diffSummary(['--name-status']);
317-
await closeWithSuccess(`M ${file}`);
317+
await closeWithSuccess(`M\t${file}\nR100\tfrom\tto`);
318318

319319
assertExecutedCommands('diff', '--name-status');
320320
expect(await task).toEqual(
321321
like({
322-
changed: 1,
322+
changed: 2,
323323
deletions: 0,
324324
insertions: 0,
325325
files: [
@@ -328,6 +328,15 @@ describe('diff', () => {
328328
changes: 0,
329329
insertions: 0,
330330
deletions: 0,
331+
status: 'M',
332+
binary: false,
333+
},
334+
{
335+
file: 'to',
336+
changes: 0,
337+
insertions: 0,
338+
deletions: 0,
339+
status: 'R',
331340
binary: false,
332341
},
333342
],

simple-git/test/unit/utils.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,24 @@ import {
1111
including,
1212
last,
1313
NOOP,
14+
orVoid,
1415
toLinesWithContent,
1516
} from '../../src/lib/utils';
1617

1718
describe('utils', () => {
19+
describe('orVoid', () => {
20+
it.each([[null], [true], [''], ['non empty string'], [[]], [{}], [0], [1]])(
21+
'passes through %s',
22+
(item) => {
23+
expect(orVoid(item)).toBe(item);
24+
}
25+
);
26+
27+
it.each([[false], [undefined]])('removes %s', (item) => {
28+
expect(orVoid(item)).toBe(undefined);
29+
});
30+
});
31+
1832
describe('array edges', () => {
1933
it.each<[string, any, string | number | undefined, string | undefined]>([
2034
['string array', ['abc', 'def'], 'abc', 'def'],

simple-git/typings/response.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { DefaultLogFields } from '../src/lib/tasks/log';
1+
import type { DiffNameStatus } from '../src/lib/tasks/diff-name-status';
2+
import type { DefaultLogFields } from '../src/lib/tasks/log';
23

34
export interface BranchSummaryBranch {
45
current: boolean;
@@ -141,13 +142,19 @@ export interface DiffResultTextFile {
141142
insertions: number;
142143
deletions: number;
143144
binary: false;
145+
146+
/** `--name-status` argument needed */
147+
status?: DiffNameStatus;
144148
}
145149

146150
export interface DiffResultBinaryFile {
147151
file: string;
148152
before: number;
149153
after: number;
150154
binary: true;
155+
156+
/** `--name-status` argument needed */
157+
status?: string;
151158
}
152159

153160
export interface DiffResult {

simple-git/typings/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export { CheckRepoActions } from '../src/lib/tasks/check-is-repo';
1616
export { CleanOptions, CleanMode } from '../src/lib/tasks/clean';
1717
export type { CloneOptions } from '../src/lib/tasks/clone';
1818
export { GitConfigScope } from '../src/lib/tasks/config';
19+
export { DiffNameStatus } from '../src/lib/tasks/diff-name-status';
1920
export { GitGrepQuery, grepQueryBuilder } from '../src/lib/tasks/grep';
2021
export { ResetOptions, ResetMode } from '../src/lib/tasks/reset';
2122
export type { VersionResult } from '../src/lib/tasks/version';

0 commit comments

Comments
 (0)
0