UNPKG

huxley

Version:
41 lines (35 loc) 1.29 kB
var Promise = require('bluebird'); var execP = require('../promisified/execP'); // git stash: save all modifications of already tracked files // git add .: now there are only untracked files. Track them // git stash: stash these too // invariant: the repo looks the same before and after this operation function safeStashAll() { return execP('git stash && git add . && git stash') .catch(function(e) { return safeUnstashAll() .then(function() { return Promise.reject(e); }); }); } // git stash pop: pop newly added files // git reset HEAD .: un-add them // git stash pop --index: revert first stash. // --index reverts exactly to previous state (partial add, stage, etc.) function safeUnstashAll() { // TODO: will a re-stash ever be needed? This is fine for now because this // basically assumes that this folows a `safeStashAll` return execP('git stash pop && git reset HEAD . && git stash pop --index') .catch(function(err) { if (err && err.message && err.message.indexOf('No stash found') > -1) { // this is ok. Caused by second `stash pop` returning and error return; } return Promise.reject(err); }); } module.exports = { safeStashAll: safeStashAll, safeUnstashAll: safeUnstashAll, };