Skip to content

Instantly share code, notes, and snippets.

@alpacaaa
Last active December 16, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpacaaa/5387322 to your computer and use it in GitHub Desktop.
Save alpacaaa/5387322 to your computer and use it in GitHub Desktop.
Bash script to switch directory to the nearest .git folder
// .bash_aliases
// alias cdg='cd `node /path/to/script/nearestGitFolder.js`'
var fs = require('fs');
var hasGitDir = function(path) {
return fs.existsSync(path + '/.git');
}
var cycleFolders = function(current) {
if (!fs.existsSync(current)) return false;
if (hasGitDir(current)) return current;
return cycleFolders(current + '/..');
}
var folder = cycleFolders(process.cwd()) || '.';
process.stdout.write(folder);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment