title: How can I bump the package.json version when I commit based on the commit message?
body: <p>I am trying to bump the version of my package.json file when I commit, but only if the commit message is prefixed correctly: <code>MJ</code> prefix triggers a patch bump, <code>^</code> prefix triggers a minor bump. I wrote a gulp task to do this and it works, the only problem is that I can't get the message of the current commit, execute the task, then add package.json to the commit and continue. </p>
  
  <p>I tried running my task on these two git hooks:</p>
  
  <h1><code>pre-commit</code></h1>
  
  <p>Problem:</p>
  
  <ul>
  <li>I only have access the previous commit message in this hook, not the one we're doing right now.</li>
  </ul>
  
  <p>What works</p>
  
  <ul>
  <li>I can bump the version, but only based on the previous commit message</li>
  <li>I can stage package.json</li>
  <li>package.json can then be added to the commit along with the rest</li>
  </ul>
  
  <h1><code>prepare-commit-msg</code></h1>
  
  <p>Problem:</p>
  
  <ul>
  <li>staging package.json does not add it to the commit</li>
  </ul>
  
  <p>What works</p>
  
  <ul>
  <li>I can bump the version based on the current commit message</li>
  <li>I can stage package.json
  
  <hr></li>
  </ul>
  
  <p>This is my gulp task that I've tried with the two hooks. I removed some noise to try to keep it minimal. </p>
  
  <pre><code>import * as fs from "fs";
  import gulp from "gulp";
  import * as shell from "shelljs";
  import pkg from "./package.json";
  
  const getCommitMsg = () =&gt; fs.readFileSync(".git/COMMIT_EDITMSG", "utf8");
  gulp.task(
      BUMP_VERSION.task,
          (done) =&gt; {
  
              const message = getCommitMsg();
              const isMinor = message.startsWith(MINOR_PREFIX);
              const isPatch = message.startsWith(PATCH_PREFIX);
  
              if (!isMinor &amp;&amp; !isPatch) {
                  done();
                  return exit(EC.NOT_VERSION);
              }
  
              const newPatch = isPatch ? parseInt(patch) + 1 : 0;
              const newMinor = isMinor ? parseInt(minor) + 1 : minor;
  
              const newVersion = `${major}.${newMinor}.${newPatch}`;
              const newPkg = Object.assign({}, pkg, { version: newVersion }); // update version
              fs.writeFileSync("./package.json", JSON.stringify(newPkg, null, 4));
  
              shell.exec("git add ./package.json");
  
              done();
          },
  );
  </code></pre>
  
  <p>Pretty much everything relies on the commit message, which is fetched using the <code>getCommitMsg</code> function. Maybe <code>fs.readFileSync(".git/COMMIT_EDITMSG", "utf8");</code> isn't the way to go? Perhaps there is another command I could run (with <code>shelljs</code>) to get the current commit message in the <code>pre-commit</code> hook? Otherwise if I use the <code>prepare-commit-msg</code> hook, then I can get the correct message, but then how would I add it to the current commit along with the other staged files?</p>