feat: Add git-changes script
This commit is contained in:
47
home/shells/zsh/scripts/default.nix
Normal file
47
home/shells/zsh/scripts/default.nix
Normal file
@@ -0,0 +1,47 @@
|
||||
{
|
||||
pkgs,
|
||||
dotsDir,
|
||||
...
|
||||
}: {
|
||||
home.packages = with pkgs; [
|
||||
(
|
||||
writeScriptBin "rebuild"
|
||||
# sh
|
||||
''
|
||||
cd ${dotsDir}
|
||||
git add -A 2>/dev/null
|
||||
sudo nixos-rebuild $@ switch --flake .
|
||||
cd - >/dev/null
|
||||
''
|
||||
)
|
||||
|
||||
(
|
||||
writeScriptBin "update"
|
||||
# sh
|
||||
''
|
||||
cd ${dotsDir}
|
||||
git add -A 2>/dev/null
|
||||
sudo nix flake $@ update
|
||||
cd - >/dev/null
|
||||
''
|
||||
)
|
||||
|
||||
(
|
||||
writeScriptBin "nix"
|
||||
# sh
|
||||
''
|
||||
NIX=${pkgs.lib.getExe pkgs.nix}
|
||||
|
||||
if [[ $1 == "develop" || $1 == "shell" ]]; then
|
||||
cmd=$1
|
||||
shift
|
||||
exec "$NIX" "$cmd" -c "$SHELL" "$@"
|
||||
else
|
||||
exec "$NIX" "$@"
|
||||
fi
|
||||
''
|
||||
)
|
||||
|
||||
(writeScriptBin "git-changes" (builtins.readFile ./git-changes.sh))
|
||||
];
|
||||
}
|
||||
97
home/shells/zsh/scripts/git-changes.sh
Normal file
97
home/shells/zsh/scripts/git-changes.sh
Normal file
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env bash
|
||||
if [ -n "$1" ]; then
|
||||
BASE_BRANCH="$1"
|
||||
else
|
||||
if git show-ref --verify --quiet refs/heads/dev; then
|
||||
BASE_BRANCH="dev"
|
||||
elif git show-ref --verify --quiet refs/heads/main; then
|
||||
BASE_BRANCH="main"
|
||||
elif git show-ref --verify --quiet refs/heads/master; then
|
||||
BASE_BRANCH="master"
|
||||
else
|
||||
echo "Warning: Neither 'dev', 'main' nor 'master' found. Defaulting to 'main'." >&2
|
||||
BASE_BRANCH="main"
|
||||
fi
|
||||
fi
|
||||
|
||||
TARGET_BRANCH=${2:-$(git rev-parse --abbrev-ref HEAD)}
|
||||
|
||||
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
echo "Error: Not a git repository." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! git show-ref --verify --quiet refs/heads/"$BASE_BRANCH"; then
|
||||
echo "Error: Base branch '$BASE_BRANCH' does not exist." >&2
|
||||
fi
|
||||
|
||||
REMOTE_URL=$(git config --get remote.origin.url)
|
||||
REPO_URL=""
|
||||
|
||||
if [[ "$REMOTE_URL" == *"github.com"* || $REMOTE_URL == *"gitlab.com"* || $REMOTE_URL == *"gitlab.uni-ulm.de"* ]]; then
|
||||
REPO_URL=$(echo "$REMOTE_URL" | sed -E 's/git@([^:]+):/https:\/\/\1\//')
|
||||
REPO_URL=${REPO_URL%.git}
|
||||
fi
|
||||
|
||||
get_commit_link() {
|
||||
local hash=$1
|
||||
if [ -n "$REPO_URL" ]; then
|
||||
echo "($REPO_URL/commit/$hash)"
|
||||
else
|
||||
echo "($hash)"
|
||||
fi
|
||||
}
|
||||
|
||||
feats=""
|
||||
fixes=""
|
||||
docs=""
|
||||
refactors=""
|
||||
tests=""
|
||||
others=""
|
||||
|
||||
echo "Generating changelog for '$TARGET_BRANCH' (comparing against '$BASE_BRANCH')..."
|
||||
echo "-------------------------------------------------------------------------------"
|
||||
|
||||
while IFS="|" read -r hash subject; do
|
||||
link=$(get_commit_link "$hash")
|
||||
|
||||
if [[ "$subject" =~ ^([a-z]+)(\([a-z0-9\ -]+\))?!?:[[:space:]]*(.*)$ ]]; then
|
||||
type="${BASH_REMATCH[1]}"
|
||||
clean_msg="${BASH_REMATCH[3]}"
|
||||
|
||||
line=" - [\`${hash:0:7}\`]${link}: ${clean_msg}"
|
||||
|
||||
case "$type" in
|
||||
feat) feats="${feats}\n${line}" ;;
|
||||
fix) fixes="${fixes}\n${line}" ;;
|
||||
docs) docs="${docs}\n${line}" ;;
|
||||
refactor) refactors="${refactors}\n${line}" ;;
|
||||
test) tests="${tests}\n${line}" ;;
|
||||
*) others="${others}\n${line} (Type: $type)" ;;
|
||||
esac
|
||||
else
|
||||
line=" - [\`${hash:0:7}\`]${link}: ${subject}"
|
||||
others="${others}\n${line}"
|
||||
fi
|
||||
|
||||
done < <(git log --no-merges --pretty=format:"%H|%s" "$BASE_BRANCH..$TARGET_BRANCH")
|
||||
|
||||
if [ -n "$feats" ]; then
|
||||
echo -e "\n- **New Features:**$feats"
|
||||
fi
|
||||
if [ -n "$fixes" ]; then
|
||||
echo -e "\n- **Bug Fixes:**$fixes"
|
||||
fi
|
||||
if [ -n "$docs" ]; then
|
||||
echo -e "\n- **Documentation:**$docs"
|
||||
fi
|
||||
if [ -n "$refactors" ]; then
|
||||
echo -e "\n- **Refactoring:**$refactors"
|
||||
fi
|
||||
if [ -n "$tests" ]; then
|
||||
echo -e "\n- **Tests:**$tests"
|
||||
fi
|
||||
if [ -n "$others" ]; then
|
||||
echo -e "\n- **Other Changes:**$others"
|
||||
fi
|
||||
echo -e "\n> Automatically generated by [git-changes](https://github.com/demenik/dots/tree/main/home/shells/zsh/scripts/git-changes.sh)"
|
||||
Reference in New Issue
Block a user