From 52f767b6a66a2fab6ed2e5085615a6b0b2f9d4a3 Mon Sep 17 00:00:00 2001 From: Steve King Date: Thu, 19 Sep 2024 13:13:33 +0100 Subject: [PATCH] Feat/name status similarity (#1025) * Add `similarity` to the `DiffResultNameStatusFile` interface, used when getting a log/diff with the `--name-status` option. Closes #993 * Changeset --- .changeset/hot-hornets-clean.md | 5 +++ .../src/lib/parsers/parse-diff-summary.ts | 3 +- .../test/integration/log-name-status.spec.ts | 32 +++++++++++-------- simple-git/test/unit/diff.spec.ts | 2 ++ simple-git/typings/response.d.ts | 1 + 5 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 .changeset/hot-hornets-clean.md diff --git a/.changeset/hot-hornets-clean.md b/.changeset/hot-hornets-clean.md new file mode 100644 index 00000000..3347e208 --- /dev/null +++ b/.changeset/hot-hornets-clean.md @@ -0,0 +1,5 @@ +--- +'simple-git': minor +--- + +Add `similarity` to the `DiffResultNameStatusFile` interface used when fetching log/diff with the `--name-status` option. diff --git a/simple-git/src/lib/parsers/parse-diff-summary.ts b/simple-git/src/lib/parsers/parse-diff-summary.ts index 6510d2d5..53d10678 100644 --- a/simple-git/src/lib/parsers/parse-diff-summary.ts +++ b/simple-git/src/lib/parsers/parse-diff-summary.ts @@ -89,7 +89,7 @@ const nameOnlyParser = [ const nameStatusParser = [ new LineParser( /([ACDMRTUXB])([0-9]{0,3})\t(.[^\t]*)(\t(.[^\t]*))?$/, - (result, [status, _similarity, from, _to, to]) => { + (result, [status, similarity, from, _to, to]) => { result.changed++; result.files.push({ file: to ?? from, @@ -99,6 +99,7 @@ const nameStatusParser = [ binary: false, status: orVoid(isDiffNameStatus(status) && status), from: orVoid(!!to && from !== to && from), + similarity: asNumber(similarity), }); } ), diff --git a/simple-git/test/integration/log-name-status.spec.ts b/simple-git/test/integration/log-name-status.spec.ts index 8065e2dd..0e9be3d5 100644 --- a/simple-git/test/integration/log-name-status.spec.ts +++ b/simple-git/test/integration/log-name-status.spec.ts @@ -26,24 +26,30 @@ describe('log-name-status', function () { const actual = await newSimpleGit(context.root).log(['--name-status']); expect(actual.all).toEqual([ - mockListLogLine('two', { b: [DiffNameStatus.RENAMED, 'a'] }), + mockListLogLine('two', { b: [DiffNameStatus.RENAMED, 100, 'a'] }), mockListLogLine('one', { a: [DiffNameStatus.ADDED] }), ]); }); }); -function mockListLogLine(message: string, changes: Record) { - const files: DiffResultTextFile[] = Object.entries(changes).map(([file, [status, from]]) => { - return { - binary: false, - changes: 0, - deletions: 0, - file, - insertions: 0, - status, - from, - }; - }); +function mockListLogLine( + message: string, + changes: Record +) { + const files: DiffResultTextFile[] = Object.entries(changes).map( + ([file, [status, similarity = 0, from]]) => { + return { + binary: false, + changes: 0, + deletions: 0, + file, + insertions: 0, + similarity, + status, + from, + }; + } + ); return like({ message, diff: like({ changed: files.length, deletions: 0, insertions: 0, files }), diff --git a/simple-git/test/unit/diff.spec.ts b/simple-git/test/unit/diff.spec.ts index ab122a65..eaab3687 100644 --- a/simple-git/test/unit/diff.spec.ts +++ b/simple-git/test/unit/diff.spec.ts @@ -329,6 +329,7 @@ describe('diff', () => { insertions: 0, deletions: 0, binary: false, + similarity: 0, status: 'M', from: undefined, }, @@ -338,6 +339,7 @@ describe('diff', () => { insertions: 0, deletions: 0, binary: false, + similarity: 100, status: 'R', from: 'from', }, diff --git a/simple-git/typings/response.d.ts b/simple-git/typings/response.d.ts index ad8bbc89..fe3d1058 100644 --- a/simple-git/typings/response.d.ts +++ b/simple-git/typings/response.d.ts @@ -155,6 +155,7 @@ export interface DiffResultBinaryFile { export interface DiffResultNameStatusFile extends DiffResultTextFile { status?: DiffNameStatus; from?: string; + similarity: number; } export interface DiffResult {