Skip to content
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
4 changes: 2 additions & 2 deletions src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import { spawn } from 'child_process';
import { log } from './Logger';

export const command = async (command: string): Promise<string> => {
export const command = async (command: string, location: string): Promise<string> => {
return await new Promise<string>((resolve, reject) => {
let res = '';

log.trace('Command ', command, 'started');
const cmd = spawn(command, { shell: true });
const cmd = spawn(command, { cwd: location, shell: true });

cmd.stdout.on('data', (chunk) => {
res += String(chunk);
Expand Down
4 changes: 2 additions & 2 deletions src/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getCommitMessage = async (document: vscode.TextDocument, commit: st
const location = getLocation(document.fileName);

try {
const body = await command(`cd ${location} && git show ${commit}`);
const body = await command(`git show ${commit}`, location);
const message = body?.substring(body.indexOf('\n\n'), body.search('diff --git'));
return message?.replace(/^ {2,}/gm, '');
} catch (e) {
Expand All @@ -23,7 +23,7 @@ export const blameFile = async (fileName: string): Promise<string> => {
const location = fileName.replace(name, '');

try {
return await command(`cd "${location}" && git blame --porcelain "${name}"`) ?? '';
return await command(`git blame --porcelain "${name}"`, location) ?? '';
} catch (e) {
if ((e as Error).message.match(/no such path .* in HEAD/)) {
vscode.window.showWarningMessage(`File: ${name} is not in HEAD`);
Expand Down
14 changes: 7 additions & 7 deletions src/test/suite/Git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ diff --git

const message = await getCommitMessage(document, blame.hash);

sinon.assert.calledWithExactly(commandStub, `cd ${getLocation(document.fileName)} && git show a`);
sinon.assert.calledWithExactly(commandStub, `git show a`, getLocation(document.fileName));

assert.strictEqual(message, `

Expand Down Expand Up @@ -100,36 +100,36 @@ Maybe this commit message is long and descriptive enough to prove a point.
test('test blameFile slash succeeds', async () => {
await blameFile('path/to/file/test.ts');

sinon.assert.calledWithExactly(commandStub, `cd \"path/to/file/\" && git blame --porcelain \"test.ts\"`);
sinon.assert.calledWithExactly(commandStub, `git blame --porcelain \"test.ts\"`, `path/to/file/`);
});

test('test blameFile backslash succeeds', async () => {
await blameFile('path\\to\\file\\test.ts');

sinon.assert.calledWithExactly(commandStub, 'cd \"path\\to\\file\\\" && git blame --porcelain \"test.ts\"');
sinon.assert.calledWithExactly(commandStub, `git blame --porcelain \"test.ts\"`, 'path\\to\\file\\');
});

test('test blameFile filename with dash succeeds', async () => {
await blameFile('path/to/file/test-this.txt');

sinon.assert.calledWithExactly(commandStub, `cd \"path/to/file/\" && git blame --porcelain \"test-this.txt\"`);
sinon.assert.calledWithExactly(commandStub, `git blame --porcelain \"test-this.txt\"`, `path/to/file/`);
});

test('test blameFile filename with space succeeds', async () => {
await blameFile('path/to/file/test this.txt');

sinon.assert.calledWithExactly(commandStub, `cd \"path/to/file/\" && git blame --porcelain \"test this.txt\"`);
sinon.assert.calledWithExactly(commandStub, `git blame --porcelain \"test this.txt\"`, `path/to/file/`);
});

test('test blameFile path with space succeeds', async () => {
await blameFile('path/to my files/test.txt');

sinon.assert.calledWithExactly(commandStub, `cd \"path/to my files/\" && git blame --porcelain \"test.txt\"`);
sinon.assert.calledWithExactly(commandStub, `git blame --porcelain \"test.txt\"`, `path/to my files/`);
});

test('test blameFile path and filename with space succeeds', async () => {
await blameFile('path/to my files/test this.txt');

sinon.assert.calledWithExactly(commandStub, `cd \"path/to my files/\" && git blame --porcelain \"test this.txt\"`);
sinon.assert.calledWithExactly(commandStub, `git blame --porcelain \"test this.txt\"`, `path/to my files/`);
});
});