Skip to content

Instantly share code, notes, and snippets.

@kkharji
Last active January 27, 2022 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkharji/c7ea4ed68eac119ce1df0d3c6430bd60 to your computer and use it in GitHub Desktop.
Save kkharji/c7ea4ed68eac119ce1df0d3c6430bd60 to your computer and use it in GitHub Desktop.
Using nix to generate emoji based commit messages.
{ 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
''
#!/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
{
"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