From be9a3f77366560c1a8fc90c6125ac6ee1f8fa19e Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Sun, 14 Oct 2018 20:43:45 +0300 Subject: [PATCH 1/2] feat: add support for git submodules No test included in this commit as this would require an update to @commitlint/test to support submodules. --- @commitlint/read/src/index.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/@commitlint/read/src/index.js b/@commitlint/read/src/index.js index f6405889cc..6a1596801f 100644 --- a/@commitlint/read/src/index.js +++ b/@commitlint/read/src/index.js @@ -41,11 +41,29 @@ async function getEditCommit(cwd, edit) { throw new TypeError(`Could not find git root from ${cwd}`); } - const editFilePath = - typeof edit === 'string' - ? path.resolve(top, edit) - : path.join(top, '.git/COMMIT_EDITMSG'); + const editFilePath = await getEditFilePath(top, edit); const editFile = await sander.readFile(editFilePath); return [`${editFile.toString('utf-8')}\n`]; } + +// Get path to recently edited commit message file +// (top: string, edit: any) => Promise +async function getEditFilePath(top, edit) { + let editFilePath; + if (typeof edit === 'string') { + editFilePath = path.resolve(top, edit); + } else { + const dotgitPath = path.join(top, '.git'); + const dotgitStats = sander.lstatSync(dotgitPath); + if (dotgitStats.isDirectory()) { + editFilePath = path.join(top, '.git/COMMIT_EDITMSG'); + } else { + const gitFile = await sander.readFile(dotgitPath, 'utf8'); + const relativeGitPath = gitFile.replace('gitdir: ', '').replace('\n', ''); + editFilePath = path.join(top, relativeGitPath, 'COMMIT_EDITMSG'); + } + } + + return editFilePath; +} From ae9a37c6ff15a5ed565193b1776fa2632eac67e2 Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Wed, 24 Oct 2018 16:44:49 +0300 Subject: [PATCH 2/2] fix: resolve path to commit message for git submodules because `path.resolve` should always be used for relative paths. --- @commitlint/read/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/@commitlint/read/src/index.js b/@commitlint/read/src/index.js index 6a1596801f..9323a34a48 100644 --- a/@commitlint/read/src/index.js +++ b/@commitlint/read/src/index.js @@ -61,7 +61,7 @@ async function getEditFilePath(top, edit) { } else { const gitFile = await sander.readFile(dotgitPath, 'utf8'); const relativeGitPath = gitFile.replace('gitdir: ', '').replace('\n', ''); - editFilePath = path.join(top, relativeGitPath, 'COMMIT_EDITMSG'); + editFilePath = path.resolve(top, relativeGitPath, 'COMMIT_EDITMSG'); } }