Skip to content

Commit

Permalink
Merge pull request #35 from JarvusInnovations/fix/issue-20
Browse files Browse the repository at this point in the history
fix: only trim trailing whitespace in most commands
  • Loading branch information
themightychris authored Dec 9, 2024
2 parents 23cd1eb + 388fb68 commit ebda262
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/Git.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,14 @@ class Git {
const process = child_process.spawn(this.command, commandArgs, execOptions);

if (execOptions.passthrough) {
process.stdout.on('data', data => data.toString().trim().split(/\n/).forEach(line => logger.info(line)));
process.stderr.on('data', data => data.toString().trim().split(/\n/).forEach(line => logger.error(line)));
process.stdout.on('data', data => data.toString().split(/\n/).forEach(line => logger.info(line.trimEnd())));
process.stderr.on('data', data => data.toString().split(/\n/).forEach(line => logger.error(line.trimEnd())));
} else {
if (execOptions.onStdout) {
process.stdout.on('data', data => data.toString().trim().split(/\n/).forEach(line => execOptions.onStdout(line)));
process.stdout.on('data', data => data.toString().split(/\n/).forEach(line => execOptions.onStdout(line.trimEnd())));
}
if (execOptions.onStderr) {
process.stderr.on('data', data => data.toString().trim().split(/\n/).forEach(line => execOptions.onStderr(line)));
process.stderr.on('data', data => data.toString().split(/\n/).forEach(line => execOptions.onStderr(line.trimEnd())));
}
}

Expand Down Expand Up @@ -528,7 +528,7 @@ class Git {
}
}

resolve(stdout.trim());
resolve(stdout.trimEnd());
});
});
} else {
Expand All @@ -543,7 +543,7 @@ class Git {
}
}

resolve(stdout.trim());
resolve(stdout.trimEnd());
});
});
}
Expand Down
44 changes: 44 additions & 0 deletions test/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,47 @@ test('stdout and stderr callbacks work together', async t => {
t.true(output[0].startsWith('out: commit'));
t.is(errors.length, 0);
});

test('preserves leading spaces in porcelain status', async t => {
// Create a temporary directory
const tmpDir = await tmp.dir();
const gitDir = path.join(tmpDir.path, '.git');

try {
// Initialize a new git repo
await git.init({ $gitDir: gitDir });

// Create a git instance
const testGit = new git.Git({ gitDir });

// Create and commit a file first
const testFile = path.join(tmpDir.path, 'test.txt');
await fs.writeFile(testFile, 'initial content');
await testGit.add({ $workTree: tmpDir.path }, testFile);
await testGit.commit({ $workTree: tmpDir.path }, '-m', 'Initial commit');

// Modify the file to get a modified status with leading space
await fs.writeFile(testFile, 'modified content');

// Get status with porcelain format using spawn mode (preserves spaces)
const spawnStatus = await testGit.status({
$workTree: tmpDir.path,
porcelain: true,
$spawn: true
});
const rawSpawnOutput = await spawnStatus.captureOutput();

// Get status with porcelain format using regular mode
const regularStatus = await testGit.status({
$workTree: tmpDir.path,
porcelain: true
});

// Both modes should preserve the leading space in ' M test.txt'
t.true(rawSpawnOutput.includes(' M test.txt'));
t.true(regularStatus.includes(' M test.txt'));

} finally {
await rmfr(tmpDir.path);
}
});

0 comments on commit ebda262

Please sign in to comment.