Skip to content

Commit

Permalink
Feat/name status similarity (#1025)
Browse files Browse the repository at this point in the history
* Add `similarity` to the `DiffResultNameStatusFile` interface, used when getting a log/diff with the `--name-status` option.

Closes #993

* Changeset
  • Loading branch information
steveukx authored Sep 19, 2024
1 parent 03e1c64 commit 52f767b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-hornets-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'simple-git': minor
---

Add `similarity` to the `DiffResultNameStatusFile` interface used when fetching log/diff with 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 @@ -89,7 +89,7 @@ const nameOnlyParser = [
const nameStatusParser = [
new LineParser<DiffResult>(
/([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,
Expand All @@ -99,6 +99,7 @@ const nameStatusParser = [
binary: false,
status: orVoid(isDiffNameStatus(status) && status),
from: orVoid(!!to && from !== to && from),
similarity: asNumber(similarity),
});
}
),
Expand Down
32 changes: 19 additions & 13 deletions simple-git/test/integration/log-name-status.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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,
};
});
function mockListLogLine(
message: string,
changes: Record<string, [DiffNameStatus, number?, string?]>
) {
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 }),
Expand Down
2 changes: 2 additions & 0 deletions simple-git/test/unit/diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ describe('diff', () => {
insertions: 0,
deletions: 0,
binary: false,
similarity: 0,
status: 'M',
from: undefined,
},
Expand All @@ -338,6 +339,7 @@ describe('diff', () => {
insertions: 0,
deletions: 0,
binary: false,
similarity: 100,
status: 'R',
from: 'from',
},
Expand Down
1 change: 1 addition & 0 deletions simple-git/typings/response.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export interface DiffResultBinaryFile {
export interface DiffResultNameStatusFile extends DiffResultTextFile {
status?: DiffNameStatus;
from?: string;
similarity: number;
}

export interface DiffResult {
Expand Down

0 comments on commit 52f767b

Please sign in to comment.