Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions getFolderInPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'
const fs = require('fs')
const path = require('path')
const resolve = path.resolve
const exists = path.existsSync || fs.existsSync

// Function to recursively finding a folder
function getFolderInPath (folder, path) {
const result = resolve(path, folder)

if (!exists(result)) {
console.log('pre-commit:')
console.log('pre-commit: Not found ' + folder + ' folder in', result)

const newPath = resolve(path, '..')

// Stop if we on top folder
if (path === newPath) {
return null
}

return getFolderInPath(folder, newPath)
}

if (fs.lstatSync(result).isDirectory()) {
console.log('pre-commit:')
console.log('pre-commit: Found ' + folder + ' folder in', result)
return result
}
return null
}

module.exports = getFolderInPath
module.exports.getFolderInPath = getFolderInPath
26 changes: 2 additions & 24 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const path = require('path')
const os = require('os')
const hook = path.join(__dirname, 'hook')
const root = path.resolve(__dirname, '..', '..', '..')
const getFolderInPath = require('./getFolderInPath')
const exists = fs.existsSync || path.existsSync

//
Expand All @@ -17,33 +18,10 @@ const exists = fs.existsSync || path.existsSync
// to work correctly.
//

let git = getGitFolderPath(root)
let git = getFolderInPath('.git', root)
let hooks
let precommit

// Function to recursively finding .git folder
function getGitFolderPath (currentPath) {
const git = path.resolve(currentPath, '.git')

if (!exists(git) || !fs.lstatSync(git).isDirectory()) {
console.log('pre-commit:')
console.log('pre-commit: Not found .git folder in', git)

const newPath = path.resolve(currentPath, '..')

// Stop if we on top folder
if (currentPath === newPath) {
return null
}

return getGitFolderPath(newPath)
}

console.log('pre-commit:')
console.log('pre-commit: Found .git folder in', git)
return git
}

//
// Resolve git directory for submodules
//
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"example-fail": "echo \"This is the example hook, I exit with 1\" && exit 1",
"example-pass": "echo \"This is the example hook, I exit with 0\" && exit 0",
"install": "node install.js",
"unit": "tap test.js",
"unit": "tap test/*.test.js",
"lint": "standard",
"test": "npm run unit",
"uninstall": "node uninstall.js"
Expand Down
43 changes: 43 additions & 0 deletions test/getFolderInPath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const t = require('tap')
const resolve = require('path').resolve
const getFolderInPath = require('../getFolderInPath')

t.test('getFolderInPath', function (t) {
t.plan(6)

t.test('target folder is in root', function (t) {
t.plan(1)
const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/root'))
t.ok(path.endsWith('testfolders/root/target_git'))
})

t.test('test folder is in submodule', function (t) {
t.plan(1)
const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/submodule/moduleA'))
t.ok(path.endsWith('testfolders/submodule/moduleA/target_git'))
})

t.test('test folder is in submodule', function (t) {
t.plan(1)
const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/recursive/root/sub'))
t.ok(path.endsWith('testfolders/recursive/root/target_git'))
})

t.test('folder is root', function (t) {
t.plan(1)
const path = getFolderInPath('super-special-folder-which-should-never-be-found', '/')
t.same(path, null)
})

t.test('folder is empty', function (t) {
t.plan(1)
const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/empty'))
t.same(path, null)
})

t.test('folder is empty', function (t) {
t.plan(1)
const path = getFolderInPath('target_git', resolve(__dirname, 'testfolders/file/module/sub'))
t.same(path, null)
})
})
8 changes: 4 additions & 4 deletions test.js → test/hook.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const Hook = require('..')
const t = require('tap')
const Hook = require('./')
const tty = require('tty')
const ttySupportColor = tty.isatty(process.stdout.fd)

Expand Down Expand Up @@ -146,7 +146,7 @@ t.test('pre-commit', function (t) {
t.test('overrides the `pre-commit` config property in package.json with the config inside `.pre-commit.json` if it exists', function (t) {
t.plan(1)

const Hook = proxyquire('.', {
const Hook = proxyquire('..', {
fs: {
existsSync () {
return true
Expand All @@ -167,7 +167,7 @@ t.test('pre-commit', function (t) {
t.test('should properly handle errors while trying to read and parse the contents of `.pre-commit.json`', function (t) {
t.plan(4)

let Hook = proxyquire('.', {
let Hook = proxyquire('..', {
fs: {
existsSync () {
return true
Expand All @@ -180,7 +180,7 @@ t.test('pre-commit', function (t) {

hook = new Hook(exit)

Hook = proxyquire('.', {
Hook = proxyquire('..', {
fs: {
existsSync () { return true },
readFileSync () {
Expand Down
Empty file added test/testfolders/empty/.gitkeep
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.