delete-github-action-head-commits.sh
· 1.9 KiB · Bash
Raw
#!/bin/bash
# Usage: action-bot automated repository can be filled with GA commits,
# the script would delete GA commits in HEAD until the earliest commit not authored by GA.
# Author: Vinfall
# License: CC0 (Public Domain)
# Define a function to delete commits authored by a specific email address
del_ga_commits() {
# Define the email address of the author to be deleted
author_email="[email protected]"
# Get the HEAD of the current branch
current_commit=$(git rev-parse HEAD)
# Loop to find the first commit that is not authored by the specified email address
while true; do
# Get the author of the current commit
commit_author=$(git log -1 --pretty=format:"%ae" "$current_commit")
# If the author is not the specified email address, stop the loop
if [ "$commit_author" != "$author_email" ]; then
break
fi
# Get the parent commit of the current commit
current_commit=$(git log -1 --pretty=%P "$current_commit")
# If it has reached the earliest commit, stop the loop
if [ -z "$current_commit" ]; then
echo "No commits authored by GitHub Action in HEAD found, no need to delete."
return
fi
done
# Get the short version of the commit hash
short_commit=$(git rev-parse --short=8 "$current_commit")
# Prompt the user to confirm the operation
echo "You are about to delete all commits from HEAD to commit $short_commit. Continue? [y/n] \c"
read -r REPLY
if [[ $REPLY =~ ^[Yy]$ ]]
then
# Reset the main branch to the specified commit
git reset --hard "$current_commit"
# Push the changes to the remote repository
git push origin main --force
else
echo "Operation canceled."
fi
}
# Call the function to delete commits authored by a specific email address
del_ga_commits
1 | #!/bin/bash |
2 | |
3 | # Usage: action-bot automated repository can be filled with GA commits, |
4 | # the script would delete GA commits in HEAD until the earliest commit not authored by GA. |
5 | # Author: Vinfall |
6 | # License: CC0 (Public Domain) |
7 | |
8 | # Define a function to delete commits authored by a specific email address |
9 | del_ga_commits() { |
10 | # Define the email address of the author to be deleted |
11 | author_email="[email protected]" |
12 | |
13 | # Get the HEAD of the current branch |
14 | current_commit=$(git rev-parse HEAD) |
15 | |
16 | # Loop to find the first commit that is not authored by the specified email address |
17 | while true; do |
18 | # Get the author of the current commit |
19 | commit_author=$(git log -1 --pretty=format:"%ae" "$current_commit") |
20 | |
21 | # If the author is not the specified email address, stop the loop |
22 | if [ "$commit_author" != "$author_email" ]; then |
23 | break |
24 | fi |
25 | |
26 | # Get the parent commit of the current commit |
27 | current_commit=$(git log -1 --pretty=%P "$current_commit") |
28 | |
29 | # If it has reached the earliest commit, stop the loop |
30 | if [ -z "$current_commit" ]; then |
31 | echo "No commits authored by GitHub Action in HEAD found, no need to delete." |
32 | return |
33 | fi |
34 | done |
35 | |
36 | # Get the short version of the commit hash |
37 | short_commit=$(git rev-parse --short=8 "$current_commit") |
38 | |
39 | # Prompt the user to confirm the operation |
40 | echo "You are about to delete all commits from HEAD to commit $short_commit. Continue? [y/n] \c" |
41 | read -r REPLY |
42 | if [[ $REPLY =~ ^[Yy]$ ]] |
43 | then |
44 | # Reset the main branch to the specified commit |
45 | git reset --hard "$current_commit" |
46 | |
47 | # Push the changes to the remote repository |
48 | git push origin main --force |
49 | else |
50 | echo "Operation canceled." |
51 | fi |
52 | } |
53 | |
54 | # Call the function to delete commits authored by a specific email address |
55 | del_ga_commits |