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

Feat/name status similarity #1025

Merged
merged 2 commits into from
Sep 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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