Skip to content

Commit 99ff1a2

Browse files
committed
feat(commit): add --retry option
This adds an option to git cz --retry the latest commit. This is useful if you go through the prompts but your tests fail. Closes #132
1 parent b810db7 commit 99ff1a2

File tree

5 files changed

+92
-9
lines changed

5 files changed

+92
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"glob": "7.0.3",
7373
"gulp": "3.9.1",
7474
"gulp-git": "1.7.0",
75+
"home-or-tmp": "2.0.0",
7576
"inquirer": "0.12.0",
7677
"lodash": "4.6.1",
7778
"minimist": "1.2.0",

src/cli/strategies/git-cz.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ function gitCz(rawGitArgs, environment, adapterConfig) {
3838

3939
// Now, if we've made it past overrides, proceed with the git-cz strategy
4040
let parsedGitCzArgs = parse(rawGitArgs);
41+
42+
// Determine if we need to process this commit as a retry instead of a
43+
// normal commit.
44+
let retryLastCommit = rawGitArgs && rawGitArgs[0] === '--retry';
45+
4146
let resolvedAdapterConfigPath = resolveAdapterPath(adapterConfig.path);
4247
let resolvedAdapterRootPath = findRoot(resolvedAdapterConfigPath);
4348
let prompter = getPrompter(adapterConfig.path);
@@ -50,7 +55,7 @@ function gitCz(rawGitArgs, environment, adapterConfig) {
5055
let adapterPackageJson = getParsedPackageJsonFromPath(resolvedAdapterRootPath);
5156
let cliPackageJson = getParsedPackageJsonFromPath(environment.cliPath);
5257
console.log(`cz-cli@${cliPackageJson.version}, ${adapterPackageJson.name}@${adapterPackageJson.version}\n`);
53-
commit(sh, inquirer, process.cwd(), prompter, {args: parsedGitCzArgs, disableAppendPaths:true, emitData:true, quiet:false}, function() {
58+
commit(sh, inquirer, process.cwd(), prompter, {args: parsedGitCzArgs, disableAppendPaths:true, emitData:true, quiet:false, retryLastCommit}, function() {
5459
// console.log('commit happened');
5560
});
5661

src/commitizen.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as adapter from './commitizen/adapter';
2+
import * as cache from './commitizen/cache';
23
import commit from './commitizen/commit';
34
import * as configLoader from './commitizen/configLoader';
45
import init from './commitizen/init';
56
import * as staging from './commitizen/staging';
67

78
export {
89
adapter,
10+
cache,
911
commit,
1012
configLoader,
1113
init,

src/commitizen/cache.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import fs from 'fs';
2+
import _ from 'lodash';
3+
4+
export {
5+
getCacheValueSync,
6+
readCacheSync,
7+
setCacheValueSync,
8+
};
9+
10+
/**
11+
* Reads the entire cache
12+
*/
13+
function readCacheSync(cachePath) {
14+
return JSON.parse(fs.readFileSync(cachePath, 'utf8'));
15+
}
16+
17+
/**
18+
* Sets a cache value and writes the file to disk
19+
*/
20+
function setCacheValueSync(cachePath, key, value) {
21+
var originalCache;
22+
try {
23+
originalCache = readCacheSync(cachePath);
24+
} catch (e) {
25+
originalCache = {};
26+
}
27+
var newCache = Object.assign(originalCache, {
28+
[key]: value
29+
});
30+
fs.writeFileSync(cachePath, JSON.stringify(newCache, null, ' '));
31+
return newCache;
32+
}
33+
34+
/**
35+
* Gets a single value from the cache given a key
36+
*/
37+
function getCacheValueSync(cachePath, repoPath) {
38+
try {
39+
let cache = readCacheSync(cachePath);
40+
return cache[repoPath];
41+
} catch(e) {
42+
return;
43+
}
44+
}

src/commitizen/commit.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,51 @@
1+
import path from 'path';
2+
import homeOrTmp from 'home-or-tmp';
13
import dedent from 'dedent';
24
import {commit as gitCommit, log} from '../git';
5+
import * as cache from './cache';
36

47
export default commit;
5-
8+
9+
/**
10+
* Takes all of the final inputs needed in order to make dispatch a git commit
11+
*/
12+
function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done) {
13+
14+
// Commit the user input -- side effect that we'll test
15+
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function() {
16+
done(template);
17+
});
18+
19+
}
20+
621
/**
722
* Asynchronously commits files using commitizen
823
*/
924
function commit(sh, inquirer, repoPath, prompter, options, done) {
1025

11-
// Get user input -- side effect that is hard to test
12-
prompter(inquirer, function(template, overrideOptions) {
26+
var cachePath = path.join(homeOrTmp, 'commitizen.json');
27+
28+
if(options.retryLastCommit) {
1329

14-
// Commit the user input -- side effect that we'll test
15-
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function() {
16-
done(template);
17-
});
18-
});
30+
console.log('Retrying last commit attempt.');
31+
32+
// We want to use the last commit instead of the current commit,
33+
// so lets override some options using the values from cache.
34+
let {
35+
options: retryOptions,
36+
overrideOptions: retryOverrideOptions,
37+
template: retryTemplate
38+
} = cache.getCacheValueSync(cachePath, repoPath);
39+
dispatchGitCommit(sh, repoPath, retryTemplate, retryOptions, retryOverrideOptions, done);
40+
41+
} else {
42+
// Get user input -- side effect that is hard to test
43+
prompter(inquirer, function(template, overrideOptions) {
44+
45+
// We don't want to add retries to the cache, only actual commands
46+
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
47+
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
48+
});
49+
}
1950

2051
}

0 commit comments

Comments
 (0)