Last active
January 27, 2022 22:35
-
-
Save kkharji/c7ea4ed68eac119ce1df0d3c6430bd60 to your computer and use it in GitHub Desktop.
Using nix to generate emoji based commit messages.
This file contains hidden or 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
{ lib, ... }: | |
let | |
inherit (builtins) fromJSON readFile; | |
inherit (lib) mapAttrsToList strings concatStringsSep replaceStrings; | |
inherit (strings) stringToCharacters head; | |
# Read definitions to a list of definitions | |
defs = let | |
# Path to definitions | |
source = ../../config/nvim/snippets/gitcommit.json; | |
# Make name part of the definition to ease manipulation. | |
transform = n: v: v // { | |
name = n; | |
body = replaceStrings ["$2" "\n"] ["" ""] v.body; | |
}; | |
in mapAttrsToList transform (fromJSON (readFile source)); | |
# Get first char from name TODO: move to personal lib. | |
fc = s: head (stringToCharacters s); | |
# Format definition help message. | |
help = {name, description, ...}: ''-${fc name} | ${name}: \"${description}\"''; | |
# Switch case format | |
case = {name, ...}: ''-${fc name}|${name}) ${name} $ARGS ;;''; | |
# Format definition function definition. TODO: fix double new lines in commit | |
# details | |
function = { body, description, name, ... }: | |
'' | |
function ${name} { | |
[[ "$1" = "help" ]] && echo "$(bold "${description}")" && exit 0; | |
msg="${replaceStrings ["$1" "\n"] ["$@" ""] body}" | |
# split on new line, needs \\n to be passed, support up to 3 new line chars | |
array=(''${(ps:\n:)"$(echo $msg)"}) | |
git commit -m "''${array[1]}" -m "''${array[2]}" -m "''${array[3]}" -m "''${array[4]}" | |
} | |
''; | |
in | |
'' | |
#!/bin/zsh | |
# DON'T EDIT: GENERATED FROM ${./commit.nix} | |
function bold { | |
echo "$(tput bold)$1$(tput sgr0)" | |
} | |
${concatStringsSep "\n" (map function defs)} | |
ARGS="''${@:2}" | |
ALL="$@" | |
case ''${@:1:1} in | |
${concatStringsSep "\n " (map case defs)} | |
-h|help) | |
echo "$(bold 'Supported args:')\n\n${concatStringsSep "\n" (map help defs)}\n" | |
;; | |
*) git commit -m "$ALL" ;; | |
esac | |
'' |
This file contains hidden or 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
#!/bin/zsh | |
# DON'T EDIT: GENERATED FROM /nix/store/ygv8500sysjdc530r2ai7syksjmvq8f5-commit.nix | |
function bold { | |
echo "$(tput bold)$1$(tput sgr0)" | |
} | |
function Release { | |
[[ "$1" = "help" ]] && echo "$(bold "Release new version.")" && exit 0; | |
msg="π RELEASE: version $@" | |
array=(${(ps:\n:)"$(echo $msg)"}) # split on new line, needs \\n to be passed | |
git commit -m "${array[1]}" -m "${array[2]}" -m "${array[3]}" | |
} | |
function doc { | |
[[ "$1" = "help" ]] && echo "$(bold "Add/edit/upgrade documentation regarding an aspect or feature.")" && exit 0; | |
msg="π DOC: $@" | |
array=(${(ps:\n:)"$(echo $msg)"}) # split on new line, needs \\n to be passed | |
git commit -m "${array[1]}" -m "${array[2]}" -m "${array[3]}" | |
} | |
function fix { | |
[[ "$1" = "help" ]] && echo "$(bold "Fixed bug or issue.")" && exit 0; | |
msg="π FIX: $@" | |
array=(${(ps:\n:)"$(echo $msg)"}) # split on new line, needs \\n to be passed | |
git commit -m "${array[1]}" -m "${array[2]}" -m "${array[3]}" | |
} | |
function improve { | |
[[ "$1" = "help" ]] && echo "$(bold "Improved or enhanced feature or use case.")" && exit 0; | |
msg="π IMPROVE: $@" | |
array=(${(ps:\n:)"$(echo $msg)"}) # split on new line, needs \\n to be passed | |
git commit -m "${array[1]}" -m "${array[2]}" -m "${array[3]}" | |
} | |
function new { | |
[[ "$1" = "help" ]] && echo "$(bold "Add new feature or use case.")" && exit 0; | |
msg="π¦ NEW: $@" | |
array=(${(ps:\n:)"$(echo $msg)"}) # split on new line, needs \\n to be passed | |
git commit -m "${array[1]}" -m "${array[2]}" -m "${array[3]}" | |
} | |
function refactor { | |
[[ "$1" = "help" ]] && echo "$(bold "General refactoring without effecting usage or performance.")" && exit 0; | |
msg="π REFACTOR: $@" | |
array=(${(ps:\n:)"$(echo $msg)"}) # split on new line, needs \\n to be passed | |
git commit -m "${array[1]}" -m "${array[2]}" -m "${array[3]}" | |
} | |
function test { | |
[[ "$1" = "help" ]] && echo "$(bold "Add/modified test suit or case.")" && exit 0; | |
msg="𧬠TEST: $@" | |
array=(${(ps:\n:)"$(echo $msg)"}) # split on new line, needs \\n to be passed | |
git commit -m "${array[1]}" -m "${array[2]}" -m "${array[3]}" | |
} | |
ARGS="${@:2}" | |
ALL="$@" | |
case ${@:1:1} in | |
-R|Release) Release $ARGS ;; | |
-d|doc) doc $ARGS ;; | |
-f|fix) fix $ARGS ;; | |
-i|improve) improve $ARGS ;; | |
-n|new) new $ARGS ;; | |
-r|refactor) refactor $ARGS ;; | |
-t|test) test $ARGS ;; | |
-h|help) | |
echo "$(bold 'Supported args:')\n\n-R | Release: \"Release new version.\" | |
-d | doc: \"Add/edit/upgrade documentation regarding an aspect or feature.\" | |
-f | fix: \"Fixed bug or issue.\" | |
-i | improve: \"Improved or enhanced feature or use case.\" | |
-n | new: \"Add new feature or use case.\" | |
-r | refactor: \"General refactoring without effecting usage or performance.\" | |
-t | test: \"Add/modified test suit or case.\"\n" | |
;; | |
*) git commit -m "$ALL" ;; | |
esac |
This file contains hidden or 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
{ | |
"Release": { | |
"prefix": ["release", "rels", "r"], | |
"body": "π RELEASE: version $1", | |
"description": "Release new version." | |
}, | |
"new": { | |
"prefix": ["new", "n"], | |
"body": "π¦ NEW: $1", | |
"description": "Add new feature or use case." | |
}, | |
"doc": { | |
"prefix": ["doc", "d"], | |
"body": "π DOC: $1", | |
"description": "Add/edit/upgrade documentation regarding an aspect or feature." | |
}, | |
"improve": { | |
"prefix": ["imp", "improve", "i"], | |
"body": "π IMPROVE: $1", | |
"description": "Improved or enhanced feature or use case." | |
}, | |
"refactor": { | |
"prefix": ["ref", "refactor", "r"], | |
"body": "π REFACTOR: $1", | |
"description": "General refactoring without effecting usage or performance." | |
}, | |
"test": { | |
"prefix": ["test", "t"], | |
"body": "𧬠TEST: $1", | |
"description": "Add/modified test suit or case." | |
}, | |
"fix": { | |
"prefix": ["fix", "f"], | |
"body": "π FIX: $1", | |
"description": "Fixed bug or issue." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment