Skip to content

Commit

Permalink
Renamed files in git diff --name-status (#1023)
Browse files Browse the repository at this point in the history
* Creates the `DiffResultNameStatusFile` type to represent file detail returned by the `git diff --name-status` parser.

Adds a `from?: string` property, to be populated with the file's previous name if it has been renamed.

* Update tests throughout

* Changeset
  • Loading branch information
steveukx committed Sep 15, 2024
1 parent bc90e7e commit 739b0d9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-socks-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'simple-git': minor
---

Diff summary includes original name of renamed files when run wiht the `--name-status` option.
3 changes: 2 additions & 1 deletion simple-git/src/lib/parsers/parse-diff-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ const nameStatusParser = [
result.files.push({
file: to ?? from,
changes: 0,
status: orVoid(isDiffNameStatus(status) && status),
insertions: 0,
deletions: 0,
binary: false,
status: orVoid(isDiffNameStatus(status) && status),
from: orVoid(!!to && from !== to && from),
});
}
),
Expand Down
23 changes: 23 additions & 0 deletions simple-git/test/integration/diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ describe('diff', function () {
await context.file(nameWithTrailingSpaces, nextContent);
});

it('detects moved files with --namestatus', async () => {
// save current repo state, move a file, commit that move, get the commit hash of the previous commit
const log = await newSimpleGit(context.root)
.add('.')
.commit('change content')
.mv(nameWithTrailingSpaces, 'next.file')
.add('.')
.commit('renaming')
.log();

const diffC = await newSimpleGit(context.root).diffSummary([
log.all[1].hash,
'--name-status',
]);

expect(diffC.files).toEqual([
like({
file: 'next.file',
from: nameWithTrailingSpaces,
}),
]);
});

it('detects diff with --numstat', async () => {
const diff = await newSimpleGit(context.root).diffSummary(['--numstat']);

Expand Down
9 changes: 5 additions & 4 deletions simple-git/test/integration/log-name-status.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ describe('log-name-status', function () {
const actual = await newSimpleGit(context.root).log(['--name-status']);

expect(actual.all).toEqual([
mockListLogLine('two', { b: DiffNameStatus.RENAMED }),
mockListLogLine('one', { a: DiffNameStatus.ADDED }),
mockListLogLine('two', { b: [DiffNameStatus.RENAMED, 'a'] }),
mockListLogLine('one', { a: [DiffNameStatus.ADDED] }),
]);
});
});

function mockListLogLine(message: string, changes: Record<string, DiffNameStatus>) {
const files: DiffResultTextFile[] = Object.entries(changes).map(([file, status]) => {
function mockListLogLine(message: string, changes: Record<string, [DiffNameStatus, string?]>) {
const files: DiffResultTextFile[] = Object.entries(changes).map(([file, [status, from]]) => {
return {
binary: false,
changes: 0,
deletions: 0,
file,
insertions: 0,
status,
from,
};
});
return like({
Expand Down
6 changes: 4 additions & 2 deletions simple-git/test/unit/diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,18 @@ describe('diff', () => {
changes: 0,
insertions: 0,
deletions: 0,
status: 'M',
binary: false,
status: 'M',
from: undefined,
},
{
file: 'to',
changes: 0,
insertions: 0,
deletions: 0,
status: 'R',
binary: false,
status: 'R',
from: 'from',
},
],
})
Expand Down
12 changes: 6 additions & 6 deletions simple-git/typings/response.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,27 @@ export interface DiffResultTextFile {
insertions: number;
deletions: number;
binary: false;

/** `--name-status` argument needed */
status?: DiffNameStatus;
}

export interface DiffResultBinaryFile {
file: string;
before: number;
after: number;
binary: true;
}

/** `--name-status` argument needed */
status?: string;
/** `--name-status` argument needed */
export interface DiffResultNameStatusFile extends DiffResultTextFile {
status?: DiffNameStatus;
from?: string;
}

export interface DiffResult {
/** The total number of files changed as reported in the summary line */
changed: number;

/** When present in the diff, lists the details of each file changed */
files: Array<DiffResultTextFile | DiffResultBinaryFile>;
files: Array<DiffResultTextFile | DiffResultBinaryFile | DiffResultNameStatusFile>;

/** The number of files changed with insertions */
insertions: number;
Expand Down

0 comments on commit 739b0d9

Please sign in to comment.