Skip to content

Commit

Permalink
feat(untracked): commit does not proceed if there are untracked files,
Browse files Browse the repository at this point in the history
…closes #89
  • Loading branch information
bahmutov committed Jun 11, 2016
1 parent a5ebc61 commit a4b9c1e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ Thanks to [ybiquitous](https://github.com/ybiquitous) for
* Git Bash (Git for Windows): work fine!
* Command prompt: work if sh.exe in PATH (e.g. `set PATH=C:\Program Files\Git\bin;%PATH%`)

## Untracked files

Before the commit, we check if there are any untracked files. A commit does
not continue if there are any. Please `git ignore` or delete unnecessary
files before `git commit` to ensure clean tests.

## Details

You can always skip pre-commit hook (but not pre-push hook!) by using `-n` option
Expand Down
11 changes: 10 additions & 1 deletion bin/pre-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ function printNothingToDo() {
console.log('');
}

const hasUntrackedFiles = require('pre-git').hasUntrackedFiles;
const run = require('pre-git').run;
const runTask = run.bind(null, label);

console.log('running pre-commit script');
console.log('running bin/pre-commit.js script');
haveChangesToCommit()
.then(hasUntrackedFiles)
.then((has) => {
if (has) {
const message = 'Has untracked files in folder.\n' +
'Please delete or ignore them.';
return Promise.reject(new Error(message));
}
})
.then(runTask, (err) => {
if (err) {
console.error(errorMessage(err));
Expand Down
62 changes: 45 additions & 17 deletions src/pre-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const la = require('lazy-ass');
const check = require('check-more-types');
const ggit = require('ggit');

var child = require('child_process');
var path = require('path');
Expand Down Expand Up @@ -153,6 +154,13 @@ function getTasks(label) {
return run;
}

function hasUntrackedFiles() {
return ggit.untrackedFiles()
.then(function (names) {
return check.unempty(names);
});
}

function runTask(root, task) {
console.log('executing task "' + task + '"');

Expand Down Expand Up @@ -207,29 +215,48 @@ function runAtRoot(root, label) {
return Promise.resolve();
}

return new Promise(function (resolve, reject) {
function showError(message) {
console.error('');
console.error(label, message);
console.error('');
return Promise.reject(new Error(message));
}

return new Promise(function () {
if (!root) {
console.error('');
console.error(label, 'Failed to find git root. Cannot run the tests.');
console.error('');
return reject(new Error('Failed to find git root'));
return showError('Failed to find git root. Cannot run the tests.');
}

var tasks = getTasks(label);
log('tasks for %s', label, tasks);
function runTasksForLabel() {
var tasks = getTasks(label);
log('tasks for %s', label, tasks);

if (!tasks || !tasks.length) {
console.log('');
console.log(label, 'Nothing the hook needs to do. Bailing out.');
console.log('');
return Promise.resolve('Nothing to do for ' + label);
}

if (!tasks || !tasks.length) {
console.log('');
console.log(label, 'Nothing the hook needs to do. Bailing out.');
console.log('');
return resolve('Nothing to do for ' + label);
const runTaskAt = runTask.bind(null, root);

return Promise.resolve(
Promise.each(tasks, runTaskAt)
);
}

const runTaskAt = runTask.bind(null, root);
if (label === 'pre-commit') {
hasUntrackedFiles()
.then(function (has) {
if (has) {
return showError('Cannot commit with untracked files present.');
}
return runTasksForLabel();
});
} else {
return runTasksForLabel();
}

return resolve(
Promise.each(tasks, runTaskAt)
);
});
}

Expand Down Expand Up @@ -323,7 +350,8 @@ module.exports = {
getTasks: getTasks,
getProjRoot: getProjRoot,
printError: printError,
wizard: pickWizard
wizard: pickWizard,
hasUntrackedFiles: hasUntrackedFiles
};

if (!module.parent) {
Expand Down
19 changes: 16 additions & 3 deletions test/e2e.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
echo "Linking current pre-git"
npm link
set -e

preGitFolder=$PWD
echo Current folder $preGitFolder

echo "Creating test folder"
folder=/tmp/test-pre-git
Expand All @@ -16,12 +18,23 @@ npm init --yes
echo "started package"

echo "Installing git hooks"
npm install --save-dev pre-git
# we can install latest from NPM
# npm install --save-dev pre-git
# or we can install current dev source
npm install $preGitFolder
echo "Package.json after installing pre-git"
cat package.json

# let us commit the code
git add package.json

# echo "deleting node_modules folder just for testing"
# rm -rf node_modules

# see how the check handles untracked files
# touch something
git ignore node_modules/
git add .gitignore
git commit -m "chore(test): this is a test commit"

ls -la
Expand Down

0 comments on commit a4b9c1e

Please sign in to comment.