Last active
August 19, 2019 18:07
-
-
Save tcodes0/aa0e8cd42305b633b77c9860601270cf to your computer and use it in GitHub Desktop.
lazy git function with commilint style messages
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
#! /usr/bin/env bash | |
# LAZY GIT | |
# with commitlint automation. Does git add --all and builds commit message: | |
# lg foo bar -> chore(misc): foo bar | |
# lg chore bar -> chore(misc): bar | |
# lg chore app: fix stuff -> chore(app): fix stuff | |
lg() { | |
local args="$*" | |
local commitTypes=(build ci chore docs feat fix perf refactor revert style test) | |
local defaultType="chore" | |
local defaultScope="misc" | |
local msg="" | |
local scope="" | |
local type="" | |
# git add --all, if fail exit | |
if ! git add --all; then | |
return 1 | |
fi | |
if [ "$args" ]; then | |
# check if first arg is a commit type | |
if [[ "${commitTypes[*]}" =~ $1 ]]; then | |
# add : to it (replace) | |
type="$1" | |
args=${args/"$1" /} | |
else | |
# use default type | |
type="$defaultType" | |
fi | |
#check if arg 1 or 2 is a scope (has :) | |
for arg in "$1" "$2"; do | |
# regex, does arg match ":" ? | |
# if yes, asign scope with no : (replace with nothing) | |
if [[ "$arg" =~ : ]]; then | |
scope="${arg/:/}" | |
args=${args/"$arg" /} | |
fi | |
done | |
# use default scope | |
if [ ! "$scope" ]; then | |
scope="$defaultScope" | |
fi | |
#build msg string | |
msg="${type}(${scope}): $args" | |
echo commit msg \> "$msg" | |
git commit -q -m "$msg" | |
else | |
git commit -q -v | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment