Skip to content

Commit

Permalink
Merge pull request #790 from steveukx/fix/789-renamed-files-git-status
Browse files Browse the repository at this point in the history
Fix/789 renamed files git status
  • Loading branch information
steveukx authored Apr 23, 2022
2 parents 98ea7ca + 5939bd7 commit 9e3252f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/growing-off-carpet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"simple-git": patch
---

Resolves issue whereby renamed files no longer appear correctly in the response to `git.status`.
2 changes: 1 addition & 1 deletion simple-git/src/lib/responses/FileStatusSummary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileStatusResult } from '../../../typings/response';
import { FileStatusResult } from '../../../typings';

export const fromPathRegex = /^(.+) -> (.+)$/;

Expand Down
30 changes: 17 additions & 13 deletions simple-git/src/lib/responses/StatusSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,11 @@ enum PorcelainFileStatus {
}

function renamedFile(line: string) {
const detail = /^(.+) -> (.+)$/.exec(line);

if (!detail) {
return {
from: line, to: line
};
}
const [to, from] = line.split(NULL);

return {
from: String(detail[1]),
to: String(detail[2]),
from: from || to,
to,
};
}

Expand Down Expand Up @@ -120,11 +114,21 @@ const parsers: Map<string, StatusLineParser> = new Map([
]);

export const parseStatusSummary = function (text: string): StatusResult {
const lines = text.trim().split(NULL);
const lines = text.split(NULL);
const status = new StatusSummary();

for (let i = 0, l = lines.length; i < l; i++) {
splitLine(status, lines[i]);
for (let i = 0, l = lines.length; i < l;) {
let line = lines[i++].trim();

if (!line) {
continue;
}

if (line.charAt(0) === PorcelainFileStatus.RENAMED) {
line += NULL + (lines[i++] || '');
}

splitLine(status, line);
}

return status;
Expand All @@ -150,7 +154,7 @@ function splitLine(result: StatusResult, lineStr: string) {
}

if (raw !== '##' && raw !== '!!') {
result.files.push(new FileStatusSummary(path, index, workingDir));
result.files.push(new FileStatusSummary(path.replace(/\0.+$/, ''), index, workingDir));
}
}
}
15 changes: 15 additions & 0 deletions simple-git/test/integration/status.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ describe('status', () => {
await setUpFilesAdded(context, ['alpha', 'beta'], ['alpha', 'beta', './clean-dir']);
});

it('detects renamed files', async () => {
await context.git.raw('mv', 'alpha', 'gamma');
const status = await context.git.status();

expect(status).toEqual(like({
files: [
like({path: 'gamma'}),
like({path: 'dirty-dir/dirty'}),
],
renamed: [
{from: 'alpha', to: 'gamma'},
]
}));
});

it('whole repo status', async () => {
const status = await newSimpleGit(context.root).status();
expect(status).toHaveProperty('not_added', ['dirty-dir/dirty']);
Expand Down
2 changes: 1 addition & 1 deletion simple-git/test/unit/__fixtures__/responses/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createFixture } from '../create-fixture';
import { NULL } from '../../../../src/lib/utils';

export function stagedRenamed(from = 'from.ext', to = 'to.ext', workingDir = ' ') {
return `R${workingDir} ${from} -> ${to}`;
return `R${workingDir} ${to}${NULL}${from}`;
}

export function stagedRenamedWithModifications(from = 'from.ext', to = 'to.ext') {
Expand Down
17 changes: 9 additions & 8 deletions simple-git/test/unit/status.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ describe('status', () => {
const statusSummary = parseStatusSummary(statusResponse('master',
' M other.txt',
'A src/b.txt',
'R src/a.txt -> src/c.txt').stdOut);
stagedRenamed('src/a.txt', 'src/c.txt'),
).stdOut);

expect(statusSummary).toEqual(like({
created: ['src/b.txt'],
Expand All @@ -239,7 +240,7 @@ describe('status', () => {
});

it('Handles renamed', () => {
expect(parseStatusSummary(' R src/file.js -> src/another-file.js')).toEqual(like({
expect(parseStatusSummary(` R src/another-file.js${NULL}src/file.js`)).toEqual(like({
renamed: [{from: 'src/file.js', to: 'src/another-file.js'}],
}));
});
Expand Down Expand Up @@ -283,12 +284,12 @@ describe('status', () => {
});

it.each<[string, any]>([
['?? Not tracked File', { not_added: ['Not tracked File'] }],
['UU Conflicted', { conflicted: ['Conflicted'] }],
[' D Removed', { deleted: ['Removed'] }],
[' M Modified', { modified: ['Modified'] }],
[' A Added', { created: ['Added'] }],
['AM Changed', { created: ['Changed'], modified: ['Changed'] }],
['?? Not tracked File', {not_added: ['Not tracked File']}],
['UU Conflicted', {conflicted: ['Conflicted']}],
[' D Removed', {deleted: ['Removed']}],
[' M Modified', {modified: ['Modified']}],
[' A Added', {created: ['Added']}],
['AM Changed', {created: ['Changed'], modified: ['Changed']}],
])('parses file status - %s', (file, result) => {
expect(parseStatusSummary(statusResponse('branch', file).stdOut)).toEqual(like({
modified: [],
Expand Down

0 comments on commit 9e3252f

Please sign in to comment.