Last active
December 16, 2015 05:49
-
-
Save alpacaaa/5387322 to your computer and use it in GitHub Desktop.
Bash script to switch directory to the nearest .git folder
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
// .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