Skip to content

Commit

Permalink
Fix path to .gitmessage file in commit.template config
Browse files Browse the repository at this point in the history
The `commit.template` config value is interpreted as a path relative to
the repository's root directory. Before, this config value stored the
path `.git/.gitmessage` in all cases. This is the correct path to the
`.gitmessage` file for normal repositories. However, if the repository
is a submodule of another repository, the `.gitmessage` file will be
stored in the `modules` subdirectory of the parent repository's `.git`
directory.

The solution here is to correctly compute a path that, when resolved
from the proejct's root directory, will resolve to the `.gitmessage`
file in all cases.
  • Loading branch information
cjlarose authored and rkotze committed Jan 16, 2019
1 parent 5b9e6c1 commit fb777d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/git-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ function insideWorkTree() {
return silentRun('git rev-parse --is-inside-work-tree').status === 0;
}

/**
* Computes the path to the top-level directory of the git repository.
* @returns {string} Path to the top-level directory of the git repository.
*/
function topLevelDirectory() {
return silentRun('git rev-parse --show-toplevel').stdout.trim();
}

module.exports = {
version: gitVersion,
config: {
Expand All @@ -133,5 +141,6 @@ module.exports = {
revParse: {
gitPath,
insideWorkTree,
topLevelDirectory,
},
};
3 changes: 2 additions & 1 deletion src/git-message/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const os = require('os');
const path = require('path');

const { config, revParse } = require('../git-commands');

Expand Down Expand Up @@ -75,7 +76,7 @@ function commitTemplatePath() {
return (
process.env.GITMOB_MESSAGE_PATH ||
config.get('commit.template') ||
'.git/.gitmessage'
path.relative(revParse.topLevelDirectory(), gitMessagePath())
);
}

Expand Down

0 comments on commit fb777d9

Please sign in to comment.