-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
92 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
|
||
} |