Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
check-coverage: false
check-coverage: false
files:
- 'index.test.js'
- 'lib/**/*.test.js'
2 changes: 1 addition & 1 deletion test.js → index.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
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('./lib/get-folder-in-path')
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, console)
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
33 changes: 33 additions & 0 deletions lib/get-folder-in-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'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, logger) {
const result = resolve(path, folder)

if (!exists(result)) {
logger.log('pre-commit:')
logger.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, logger)
}

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

module.exports = getFolderInPath
46 changes: 46 additions & 0 deletions lib/get-folder-in-path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

const t = require('tap')
const resolve = require('path').resolve
const normalize = require('path').normalize
const getFolderInPath = require('./get-folder-in-path')

const dummyLogger = {
log: () => {}
}

t.test('target folder is in root', function (t) {
t.plan(1)
const path = getFolderInPath('target_git', resolve(__dirname, '../testfolders/root'), dummyLogger)
t.ok(path.endsWith(normalize('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'), dummyLogger)
t.ok(path.endsWith(normalize('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'), dummyLogger)
t.ok(path.endsWith(normalize('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', '/', dummyLogger)
t.same(path, null)
})

t.test('folder is empty', function (t) {
t.plan(1)
const path = getFolderInPath('target_git', resolve(__dirname, '../testfolders/empty'), dummyLogger)
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'), dummyLogger)
t.same(path, null)
})
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",
"lint": "standard",
"test": "npm run unit",
"uninstall": "node uninstall.js"
Expand Down
Empty file added testfolders/empty/.gitkeep
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.