Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
45 changes: 44 additions & 1 deletion test.js → index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict'
const Hook = require('.')
const t = require('tap')
const Hook = require('./')
const tty = require('tty')
const ttySupportColor = tty.isatty(process.stdout.fd)
const resolve = require('path').resolve
const normalize = require('path').normalize
const getFolderInPath = require('./lib/get-folder-in-path')

const proxyquire = require('proxyquire')

Expand Down Expand Up @@ -330,3 +333,43 @@ t.test('pre-commit', function (t) {
})
})
})

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(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'))
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'))
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', '/')
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)
})
})
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)
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
34 changes: 34 additions & 0 deletions lib/get-folder-in-path.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
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 index.test.js",
"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.