From eef6fe1d3182ec5c0853058e932898224bbc762c Mon Sep 17 00:00:00 2001 From: "Thomas G." <1809566+tomgrv@users.noreply.github.com> Date: Sun, 18 Aug 2024 13:12:41 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20fix=20author=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/githooks/_prepare-commit-msg.sh | 1 + src/githooks/devcontainer-feature.json | 2 +- src/gitutils/_fixauthor.sh | 39 ++++++++++++++++++++++++++ src/gitutils/_fixup.sh | 12 ++++---- src/gitutils/devcontainer-feature.json | 4 +-- 5 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/gitutils/_fixauthor.sh diff --git a/src/githooks/_prepare-commit-msg.sh b/src/githooks/_prepare-commit-msg.sh index b83064c..a768a78 100644 --- a/src/githooks/_prepare-commit-msg.sh +++ b/src/githooks/_prepare-commit-msg.sh @@ -10,6 +10,7 @@ fi PLUGINS=$(cat package.json | npx jqn '.config.commitizen.path' | tr -d "'[]:") npm list $PLUGINS 2>/dev/null 1>&2 || npm install --no-save $PLUGINS 2>/dev/null 1>&2 npm list commitizen 2>/dev/null 1>&2 || npm install --no-save commitizen 2>/dev/null 1>&2 +npm list @commitlint/cli 2>/dev/null 1>&2 || npm install --no-save @commitlint/cli 2>/dev/null 1>&2 # Edit commit message if [ $(grep -cv -e '^#' -e '^$' .git/COMMIT_EDITMSG) -eq 0 ]; then diff --git a/src/githooks/devcontainer-feature.json b/src/githooks/devcontainer-feature.json index 2080219..27d2ef7 100644 --- a/src/githooks/devcontainer-feature.json +++ b/src/githooks/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "Git Hooks", "id": "githooks", - "version": "1.2.6", + "version": "1.2.7", "description": "A feature to add useful Git hooks to your project", "installsAfter": [ "ghcr.io/devcontainers/features/common-utils" diff --git a/src/gitutils/_fixauthor.sh b/src/gitutils/_fixauthor.sh new file mode 100644 index 0000000..b7363f5 --- /dev/null +++ b/src/gitutils/_fixauthor.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# Check if at least one old email is provided +if [ "$#" -lt 1 ]; then + echo "Usage: $0 [ ... ]" + exit 1 +fi + +OLD_EMAILS=$@ +CORRECT_NAME=$(git config user.name) +CORRECT_EMAIL=$(git config user.email) + +# Check if the correct name and email are available +if [ -z "$CORRECT_NAME" ] || [ -z "$CORRECT_EMAIL" ]; then + echo "Error: Could not retrieve the current Git user name or email." + exit 1 +fi + +#### Goto repository root +cd "$(git rev-parse --show-toplevel)" >/dev/null + +#### Rewrite history to fix author +envfilter="for OLD_EMAIL in $OLD_EMAILS; do + if test \"\$GIT_COMMITTER_EMAIL\" = \"\$OLD_EMAIL\"; then + export GIT_COMMITTER_NAME=\"$CORRECT_NAME\" + export GIT_COMMITTER_EMAIL=\"$CORRECT_EMAIL\" + fi + if test \"\$GIT_AUTHOR_EMAIL\" = \"\$OLD_EMAIL\"; then + export GIT_AUTHOR_NAME=\"$CORRECT_NAME\" + export GIT_AUTHOR_EMAIL=\"$CORRECT_EMAIL\" + fi +done" + +#### Rewrite history to fix author +git filter-branch --env-filter "$envfilter" --tag-name-filter cat -- --all + +# Clean up the original refs +rm -rf .git/refs/original/ +git reflog expire --expire=now --all +git gc --prune=now diff --git a/src/gitutils/_fixup.sh b/src/gitutils/_fixup.sh index b7454fb..5a79b98 100644 --- a/src/gitutils/_fixup.sh +++ b/src/gitutils/_fixup.sh @@ -6,12 +6,6 @@ if [ -n "$(git diff --cached --name-only | grep -E 'composer.lock|package-lock.j exit 1 fi -## Do not fixup if no files are staged -if [ -z "$(git diff --cached --name-only)" ]; then - echo 'No files are staged, fixup is not allowed.' - exit 1 -fi - #### Goto repository root cd "$(git rev-parse --show-toplevel)" >/dev/null @@ -22,6 +16,12 @@ git fetch --progress --prune --recurse-submodules=no origin >/dev/null echo 'Check if fixup commit exists...' if ! git isFixup; then + ## Do not fixup if no files are staged + if [ -z "$(git diff --cached --name-only)" ]; then + echo 'No files are staged, fixup is not allowed.' + exit 1 + fi + #### LOOK FOR COMMIT TO FIXUP IF NOT GIVEN AS PARAMETER if [ -z "$1" ]; then #### GET COMMIT TO FIXUP diff --git a/src/gitutils/devcontainer-feature.json b/src/gitutils/devcontainer-feature.json index 2a58d22..a164908 100644 --- a/src/gitutils/devcontainer-feature.json +++ b/src/gitutils/devcontainer-feature.json @@ -1,7 +1,7 @@ { "name": "Git Aliases", "id": "gitutils", - "version": "1.4.1", + "version": "1.5.2", "description": "A feature to add useful Git aliases to your shell.", "installsAfter": [ "ghcr.io/devcontainers/features/common-utils", @@ -12,4 +12,4 @@ "ghcr.io/tomgrv/devcontainer-features/gitversion": "5.*" }, "postStartCommand": "/usr/local/share/gitutils/configure.sh" -} +} \ No newline at end of file