Skip to content

Commit d10c94f

Browse files
pawamoyjimthedev
authored andcommitted
fix(cli): Respect git commit -a original behavior (#376) (#471)
* fix(cli): Respect git commit -a original behavior (#376) * test(cli): Add test for -a option behavior (#376)
1 parent 5f8902e commit d10c94f

File tree

5 files changed

+99
-10
lines changed

5 files changed

+99
-10
lines changed

src/cli/strategies/git-cz.js

-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ function gitCz (rawGitArgs, environment, adapterConfig) {
2525
// commit strategy than git-cz. For example, in the case of --amend
2626
let parsedCommitizenArgs = commitizenParser.parse(rawGitArgs);
2727

28-
if (parsedCommitizenArgs.a) {
29-
// console.log('override -a in place');
30-
addPath(sh, process.cwd());
31-
}
32-
3328
if (parsedCommitizenArgs.amend) {
3429
// console.log('override --amend in place');
3530
gitStrategy.default(rawGitArgs, environment);

src/git.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import {addPath} from './git/add';
1+
import {addPath, addFile} from './git/add';
22
import {commit} from './git/commit';
33
import {init} from './git/init';
44
import {log} from './git/log';
5+
import {whatChanged} from './git/whatChanged';
56

67
export {
78
addPath,
9+
addFile,
810
commit,
911
init,
10-
log
12+
log,
13+
whatChanged
1114
};

src/git/add.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
export { addPath };
1+
export {
2+
addPath,
3+
addFile
4+
}
25

36
/**
47
* Synchronously adds a path to git staging
@@ -7,3 +10,11 @@ function addPath (sh, repoPath) {
710
sh.cd(repoPath);
811
sh.exec('git add .');
912
}
13+
14+
/**
15+
* Synchronously adds a file to git staging
16+
*/
17+
function addFile (sh, repoPath, filename) {
18+
sh.cd(repoPath);
19+
sh.exec('git add ' + filename)
20+
}

src/git/whatChanged.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { exec } from 'child_process';
2+
3+
export { whatChanged };
4+
5+
/**
6+
* Asynchronously gets the git whatchanged output
7+
*/
8+
function whatChanged (repoPath, done) {
9+
exec('git whatchanged', {
10+
maxBuffer: Infinity,
11+
cwd: repoPath
12+
}, function (error, stdout, stderr) {
13+
if (error) {
14+
throw error;
15+
}
16+
done(stdout);
17+
});
18+
}

test/tests/commit.js

+64-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import inquirer from 'inquirer';
1212
import {bootstrap} from '../tester';
1313

1414
// Get our source files
15-
import {addPath as gitAdd, commit as gitCommit, init as gitInit, log} from '../../src/git';
15+
import {addPath as gitAdd, addFile as gitAddFile, commit as gitCommit, init as gitInit, log, whatChanged} from '../../src/git';
1616
import {commit as commitizenCommit, init as commitizenInit, adapter} from '../../src/commitizen';
1717

1818
// Destructure some things for cleaner tests
@@ -232,6 +232,63 @@ describe('commit', function () {
232232

233233
});
234234

235+
it('should respect original behavior of -a option', function (done) {
236+
237+
this.timeout(config.maxTimeout); // this could take a while
238+
239+
// SETUP
240+
241+
let dummyCommitMessage = `sip sip sippin on some sizzurp`;
242+
243+
// Describe a repo and some files to add and commit
244+
let repoConfig = {
245+
path: config.paths.endUserRepo,
246+
files: {
247+
dummyfile: {
248+
contents: `duck-duck-goose`,
249+
filename: `mydummyfile.txt`,
250+
},
251+
dummyfilecopy: {
252+
contents: `duck-duck-goose`,
253+
filename: `mydummyfilecopy.txt`,
254+
add: false,
255+
},
256+
gitignore: {
257+
contents: `node_modules/`,
258+
filename: `.gitignore`
259+
}
260+
}
261+
};
262+
263+
// Describe an adapter
264+
let adapterConfig = {
265+
path: path.join(repoConfig.path, '/node_modules/cz-jira-smart-commit'),
266+
npmName: 'cz-jira-smart-commit'
267+
};
268+
269+
let options = {
270+
args: ['-a']
271+
};
272+
273+
// Quick setup the repos, adapter, and grab a simple prompter
274+
let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage, options);
275+
// TEST
276+
277+
// Pass in inquirer but it never gets used since we've mocked out a different
278+
// version of prompter.
279+
commitizenCommit(sh, inquirer, repoConfig.path, prompter, {disableAppendPaths: true, quiet: true, emitData: true}, function () {
280+
log(repoConfig.path, function (logOutput) {
281+
expect(logOutput).to.have.string(dummyCommitMessage);
282+
});
283+
whatChanged(repoConfig.path, function (whatChangedOutput) {
284+
expect(whatChangedOutput).to.have.string('A\t' + repoConfig.files.dummyfile.filename);
285+
expect(whatChangedOutput).to.not.have.string('A\t' + repoConfig.files.dummyfilecopy.filename);
286+
done();
287+
});
288+
});
289+
290+
});
291+
235292
});
236293

237294
afterEach(function () {
@@ -267,7 +324,12 @@ function quickPrompterSetup (sh, repoConfig, adapterConfig, commitMessage, optio
267324

268325
writeFilesToPath(repoConfig.files, repoConfig.path);
269326

270-
gitAdd(sh, repoConfig.path);
327+
for (let key in repoConfig.files) {
328+
let file = repoConfig.files[key];
329+
if (file.add !== false) {
330+
gitAddFile(sh, repoConfig.path, file.filename);
331+
}
332+
}
271333

272334
// NOTE: In the real world we would not be returning
273335
// this we would instead be just making the commented

0 commit comments

Comments
 (0)