1

I'm trying to write a git post-commit file in node.js and I'm having trouble.

The file seems to have to be named post-commit in the .git/hooks directory. Since that file can't have an extension, my vim editor won't persistently give it javascript syntax highlighting, and thus none of my snippets work, and a variety of other challenges present themselves.

What can I do? Tell git to run post-commit.js or tell vim to consider this file javascript, but that doesn't persist.

Thoughts?

2 Answers 2

2

You can have the post-commit file run a separate JavaScript file.

post-commit:

#!/bin/bash/
node yourJavascriptFile.js
5
  • A bit much for a single command, but useful once there's more than one build step. Nov 19, 2016 at 19:22
  • That doesn't work. It can't seem to find that file. I think it executes the post-commit bash file from the top level directory, rather than from within .git/hooks Nov 19, 2016 at 19:26
  • 1
    Adding the absolute path to both node and the script is best practice.
    – ppovoski
    Nov 19, 2016 at 19:28
  • @ppovoski, absolute paths is the worst practice. It's an antipattern almost always and everywhere. Especially for user script files. In this particular case, it is better to use node $(dirname ${BASH_SOURCE[0]})/yourJavascriptFile.js to look for the script in the hooks directory. The node binary must be in the PATH. Jul 10, 2017 at 12:23
  • It is also worth noting that the shebang string has a typo. There should be no trailing slash after #!/bin/bash Jul 10, 2017 at 12:26
1

Use :set filetype=javascript command in vim to set file type. You may also want to enable modeline in vim and include the aforementioned command in a comment at the top of your file like this:

/* vim: set filetype=javascript : */
your_Javascript_code_goes_here()

To enable modeline in vim, add this to your ~/.vimrc file:

set modelines=2
set modeline

That will enable searching for vim modelines in the first two lines of the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.