Skip to content

Commit

Permalink
feat(commit): add --retry option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jimthedev committed Mar 10, 2016
1 parent b810db7 commit 99ff1a2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"glob": "7.0.3",
"gulp": "3.9.1",
"gulp-git": "1.7.0",
"home-or-tmp": "2.0.0",
"inquirer": "0.12.0",
"lodash": "4.6.1",
"minimist": "1.2.0",
Expand Down
7 changes: 6 additions & 1 deletion src/cli/strategies/git-cz.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ function gitCz(rawGitArgs, environment, adapterConfig) {

// Now, if we've made it past overrides, proceed with the git-cz strategy
let parsedGitCzArgs = parse(rawGitArgs);

// Determine if we need to process this commit as a retry instead of a
// normal commit.
let retryLastCommit = rawGitArgs && rawGitArgs[0] === '--retry';

let resolvedAdapterConfigPath = resolveAdapterPath(adapterConfig.path);
let resolvedAdapterRootPath = findRoot(resolvedAdapterConfigPath);
let prompter = getPrompter(adapterConfig.path);
Expand All @@ -50,7 +55,7 @@ function gitCz(rawGitArgs, environment, adapterConfig) {
let adapterPackageJson = getParsedPackageJsonFromPath(resolvedAdapterRootPath);
let cliPackageJson = getParsedPackageJsonFromPath(environment.cliPath);
console.log(`cz-cli@${cliPackageJson.version}, ${adapterPackageJson.name}@${adapterPackageJson.version}\n`);
commit(sh, inquirer, process.cwd(), prompter, {args: parsedGitCzArgs, disableAppendPaths:true, emitData:true, quiet:false}, function() {
commit(sh, inquirer, process.cwd(), prompter, {args: parsedGitCzArgs, disableAppendPaths:true, emitData:true, quiet:false, retryLastCommit}, function() {
// console.log('commit happened');
});

Expand Down
2 changes: 2 additions & 0 deletions src/commitizen.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as adapter from './commitizen/adapter';
import * as cache from './commitizen/cache';
import commit from './commitizen/commit';
import * as configLoader from './commitizen/configLoader';
import init from './commitizen/init';
import * as staging from './commitizen/staging';

export {
adapter,
cache,
commit,
configLoader,
init,
Expand Down
44 changes: 44 additions & 0 deletions src/commitizen/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from 'fs';
import _ from 'lodash';

export {
getCacheValueSync,
readCacheSync,
setCacheValueSync,
};

/**
* Reads the entire cache
*/
function readCacheSync(cachePath) {
return JSON.parse(fs.readFileSync(cachePath, 'utf8'));
}

/**
* Sets a cache value and writes the file to disk
*/
function setCacheValueSync(cachePath, key, value) {
var originalCache;
try {
originalCache = readCacheSync(cachePath);
} catch (e) {
originalCache = {};
}
var newCache = Object.assign(originalCache, {
[key]: value
});
fs.writeFileSync(cachePath, JSON.stringify(newCache, null, ' '));
return newCache;
}

/**
* Gets a single value from the cache given a key
*/
function getCacheValueSync(cachePath, repoPath) {
try {
let cache = readCacheSync(cachePath);
return cache[repoPath];
} catch(e) {
return;
}
}
47 changes: 39 additions & 8 deletions src/commitizen/commit.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
import path from 'path';
import homeOrTmp from 'home-or-tmp';
import dedent from 'dedent';
import {commit as gitCommit, log} from '../git';
import * as cache from './cache';

export default commit;


/**
* Takes all of the final inputs needed in order to make dispatch a git commit
*/
function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done) {

// Commit the user input -- side effect that we'll test
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function() {
done(template);
});

}

/**
* Asynchronously commits files using commitizen
*/
function commit(sh, inquirer, repoPath, prompter, options, done) {

// Get user input -- side effect that is hard to test
prompter(inquirer, function(template, overrideOptions) {
var cachePath = path.join(homeOrTmp, 'commitizen.json');

if(options.retryLastCommit) {

// Commit the user input -- side effect that we'll test
gitCommit(sh, repoPath, template, { ...options, ...overrideOptions }, function() {
done(template);
});
});
console.log('Retrying last commit attempt.');

// We want to use the last commit instead of the current commit,
// so lets override some options using the values from cache.
let {
options: retryOptions,
overrideOptions: retryOverrideOptions,
template: retryTemplate
} = cache.getCacheValueSync(cachePath, repoPath);
dispatchGitCommit(sh, repoPath, retryTemplate, retryOptions, retryOverrideOptions, done);

} else {
// Get user input -- side effect that is hard to test
prompter(inquirer, function(template, overrideOptions) {

// We don't want to add retries to the cache, only actual commands
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
});
}

}

0 comments on commit 99ff1a2

Please sign in to comment.