Skip to content

Commit

Permalink
feat: ✨ add fix author script
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgrv committed Aug 21, 2024
1 parent d95d543 commit eef6fe1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/githooks/_prepare-commit-msg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/githooks/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
39 changes: 39 additions & 0 deletions src/gitutils/_fixauthor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh
# Check if at least one old email is provided
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <old-email-1> [<old-email-2> ... <old-email-n>]"
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
12 changes: 6 additions & 6 deletions src/gitutils/_fixup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/gitutils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -12,4 +12,4 @@
"ghcr.io/tomgrv/devcontainer-features/gitversion": "5.*"
},
"postStartCommand": "/usr/local/share/gitutils/configure.sh"
}
}

0 comments on commit eef6fe1

Please sign in to comment.