-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] feature: adds git hook binding for automatic version bumps
- adds a post-commit script that only runs fist-bump if env flag is not set - adds an install script that copies post-commit script to .git/hooks/post-commit - adds some logic that checks whether a hook flag is set to run the install script
- Loading branch information
1 parent
4fa2a3e
commit f76ae2c
Showing
7 changed files
with
255 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/bin/sh | ||
|
||
# post-commit.sh | ||
# | ||
# The purpose of this script is to add `fist-bump` to git hooks. This ensures that | ||
# the version number is bumped before every commit automatically instead of having | ||
# to (remember) to do it manually every time. | ||
# | ||
# how does it work? | ||
# | ||
# The script adds `fist-bump` to the `post-commit` git hook. However, in order to | ||
# avoid an infinite loop, the script sets checks for an env var called `FIST_BUMP` | ||
# (which is set after the first `post-commit` hook call) prior to running | ||
# `fist-bump`. If the env var is set, `fist-bump` will not run. | ||
|
||
# get root directory of project | ||
root_dir=$(git rev-parse --show-toplevel) | ||
|
||
# function to remove the flag environment variable | ||
cleanup() { | ||
unset FIST_BUMP | ||
} | ||
|
||
# set a trap to always cleanup on exit | ||
trap cleanup EXIT | ||
|
||
# Only run if the environment variable is not set | ||
if [ "$FIST_BUMP" != "1" ]; then | ||
|
||
# run fist-bump | ||
./lib/index.js | ||
|
||
# set environment variable | ||
export FIST_BUMP=1 | ||
|
||
# amend the commit with the changes made by your script | ||
git commit --amend --no-edit | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/sh | ||
|
||
# install.sh | ||
# | ||
# The purpose of this script is to add `post-commit.sh` to the end of the | ||
# post-commit git hook (.git/hooks/post-commit). This ensures that the | ||
# version it can run after every commit. This ensures that the version | ||
# number is bumped before every commit automatically instead of having | ||
# to (remember) to do it manually every time. | ||
|
||
DELIMITER_START="# BEGIN FISTBUMP" | ||
DELIMITER_END="# END FISTBUMP" | ||
|
||
# get root directory of project | ||
root_dir=$(git rev-parse --show-toplevel) | ||
|
||
# create .git/hooks/post-commit if it doesn't exist | ||
if [ ! -f "$root_dir/.git/hooks/post-commit" ]; then | ||
touch "$root_dir/.git/hooks/post-commit" | ||
echo "[fist-bump] .git/hooks/post-commit created" | ||
fi | ||
|
||
# get contents of post-commit.sh (minus the shebang) | ||
post_commit=$(tail -n +2 "$root_dir/scripts/hooks/post-commit.sh") | ||
|
||
# wrap contents in a delimiter comment (BEGIN/END FISTBUMP) | ||
content="$DELIMITER_START\n$post_commit\n$DELIMITER_END" | ||
|
||
# exit with success if hook has a delimiter comment | ||
if grep -q "$DELIMITER_START" "$root_dir/.git/hooks/post-commit"; then | ||
echo "[fist-bump] fist-bump already installed to .git/hooks/post-commit" | ||
exit 0 | ||
fi | ||
|
||
# add contents of post-commit.sh to the end of .git/hooks/post-commit | ||
# overwrite .git/hooks/post-commit with extracted content | ||
if echo "$content" >> "$root_dir/.git/hooks/post-commit"; then | ||
echo "[fist-bump] fist-bump added to .git/hooks/post-commit" | ||
else | ||
echo "[fist-bump] Error: Failed to write to .git/hooks/post-commit (use sudo)" | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/sh | ||
|
||
# uninstall.sh | ||
# | ||
# The purpose of this script is to remove the `post-commit.sh` | ||
# script from the `post-commit` git hook. | ||
|
||
DELIMITER_START="# BEGIN FISTBUMP" | ||
DELIMITER_END="# END FISTBUMP" | ||
|
||
# get root directory of project | ||
root_dir=$(git rev-parse --show-toplevel) | ||
|
||
# get contents of .git/hooks/post-commit | ||
hook=$(cat "$root_dir/.git/hooks/post-commit") | ||
|
||
# get post-commit.sh content | ||
post_commit=$(tail -n +2 "$root_dir/scripts/hooks/post-commit.sh") | ||
|
||
# exit with success if hook doesn't have a delimiter comment | ||
if ! grep -q "$DELIMITER_START" "$root_dir/.git/hooks/post-commit"; then | ||
echo "[fist-bump] fist-bump not installed to .git/hooks/post-commit" | ||
exit 0 | ||
fi | ||
|
||
# get contents of .git/hooks/post-commit excluding fist-bump content | ||
content=$(echo "$hook" | sed "/$DELIMITER_START/,/$DELIMITER_END/d") | ||
|
||
# overwrite .git/hooks/post-commit with extracted content | ||
if echo "$content" > "$root_dir/.git/hooks/post-commit"; then | ||
echo "[fist-bump] fist-bump removed from .git/hooks/post-commit" | ||
else | ||
echo "[fist-bump] Error: Failed to write to .git/hooks/post-commit (use sudo)" | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,111 @@ | ||
#!/usr/bin/env node | ||
|
||
import { execute, getProjectRoot, logMessage } from "./utils"; | ||
import { bump } from "./bump"; | ||
|
||
bump(); | ||
const FLAG = { | ||
INSTALL: [ "-I", "--install" ], | ||
UNINSTALL: [ "-U", "--uninstall" ] | ||
} | ||
|
||
const MAXIMUM_ARGS = 3; | ||
|
||
/** | ||
* Returns true if a string is in the args | ||
* | ||
* @param string - sample arg | ||
* @returns boolean | ||
*/ | ||
function _inArgs(string: string): boolean { | ||
return process.argv.includes(string) | ||
} | ||
|
||
/** | ||
* Returns true if the install flag has been passed | ||
* | ||
* Valid flags: | ||
* - `-I` | ||
* - `--install` | ||
*/ | ||
function _isInstall(): boolean { | ||
let set = false; | ||
|
||
for(const flag of FLAG.INSTALL){ | ||
if(_inArgs(flag)){ | ||
set = true; | ||
break; | ||
} | ||
} | ||
|
||
return set; | ||
} | ||
|
||
/** | ||
* Returns true if the uninstall flag has been passed. | ||
* | ||
* Valid flags: | ||
* - `-U` | ||
* - `--uninstall` | ||
*/ | ||
function _isUninstall(): boolean { | ||
let set = false; | ||
|
||
for(const flag of FLAG.UNINSTALL){ | ||
if(_inArgs(flag)){ | ||
set = true; | ||
break; | ||
} | ||
} | ||
|
||
return set; | ||
} | ||
|
||
/** | ||
* Returns true if invalid argument is passed. | ||
* | ||
* Valid arguments: | ||
* - `./lib/cli.js`, | ||
* - `./lib/cli.js -H` | ||
* - `./lib/cli.js --hook`, | ||
*/ | ||
function _isInvalidArg(): boolean { | ||
if (process.argv.length > MAXIMUM_ARGS) { | ||
return true; | ||
} else if (process.argv.length === MAXIMUM_ARGS && !(_isInstall() || _isUninstall())) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Runs a script to install/uninstall a command that executes the | ||
* fist-bump command when the post-commit git hook is triggered. | ||
* | ||
* @param type - `install` or `uninstall` | ||
* @param silent - if true, the script will not log any messages (default: false) | ||
*/ | ||
function _executeHookScript(type: "install" | "uninstall"): void { | ||
const rootDir = getProjectRoot() || process.cwd(); | ||
const scriptPath = `${rootDir}/scripts/${type}.sh`; | ||
try { | ||
execute(`chmod +x ${scriptPath}`); | ||
execute(`${scriptPath}`); | ||
} catch (error) { | ||
logMessage(`Failed to ${type} git hook. ${error}`, "error"); | ||
} | ||
} | ||
if(_isInvalidArg()) { | ||
logMessage("Invalid argument passed. Only `--hook` or `-H` is allowed.", "error") | ||
process.exit(1); | ||
} else if(_isInstall()) { | ||
_executeHookScript("install"); | ||
logMessage("Git hook installed successfully."); | ||
} else if(_isUninstall()) { | ||
_executeHookScript("uninstall"); | ||
logMessage("Git hook uninstalled successfully."); | ||
} else { | ||
bump(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters