Skip to content

Instantly share code, notes, and snippets.

@disouzam
Last active February 10, 2026 17:45
Show Gist options
  • Select an option

  • Save disouzam/66c0a7c1de97c9e660f38d682e2c1e82 to your computer and use it in GitHub Desktop.

Select an option

Save disouzam/66c0a7c1de97c9e660f38d682e2c1e82 to your computer and use it in GitHub Desktop.
Check correctness of rebase operation
#!/bin/bash
function get_short_log() {
echo "function get_short_log - Number of arguments: $#"
for i in $(seq 1 $#); do
echo "Argument $i: ${!i}"
done
suppress_whitespace_flag=""
if [[ " $* " == *" -w "* ]]; then
echo "Supress whitespace flag (-w) detected"
suppress_whitespace_flag="-w"
fi
unified_context=""
if [[ " $* " =~ " -U"[0-9]+ ]]; then
echo "Context flag (-U) detected"
for arg in "$@"; do
if [[ "$arg" =~ ^-U[0-9]+$ ]]; then
unified_context="$arg"
break
fi
done
fi
word_diff=""
if [[ " $* " == *" --word-diff "* ]]; then
echo "Word diff flag (--word-diff) detected"
word_diff="--word-diff"
fi
first_parent=""
if [[ " $* " == *" --first-parent "* ]]; then
echo "First parent flag (--first-parent) detected"
first_parent="--first-parent"
fi
sanitized_commits=$(sed 's/\.\././g' <<< "$1")
commit1=$(echo "$sanitized_commits" | cut -d '.' -f 1)
commit2=$(echo "$sanitized_commits" | cut -d '.' -f 2)
echo -e "=============================="
echo -e "Short log between $commit1 and $commit2:\n"
git shortlog --group=author $commit1..$commit2
echo -e "\n=============================="
echo -e "Diff stats between $commit1 and $commit2:\n"
git diff --stat $commit1..$commit2
echo -e "\n=============================="
echo -e "Diff patches between $commit1 and $commit2:\n"
git diff $commit1..$commit2 $suppress_whitespace_flag $unified_context $word_diff $first_parent
}
function rebase_check() {
echo "function rebase_check - Number of arguments: $#"
for i in $(seq 1 $#); do
echo "Argument $i: ${!i}"
done
suppress_whitespace_flag=""
if [[ " $* " == *" -w "* ]]; then
echo "Supress whitespace flag (-w) detected"
suppress_whitespace_flag="-w"
fi
unified_context=""
if [[ " $* " =~ " -U"[0-9]+ ]]; then
echo "Context flag (-U) detected"
for arg in "$@"; do
if [[ "$arg" =~ ^-U[0-9]+$ ]]; then
unified_context="$arg"
break
fi
done
fi
word_diff=""
if [[ " $* " == *" --word-diff "* ]]; then
echo "Word diff flag (--word-diff) detected"
word_diff="--word-diff"
fi
first_parent=""
if [[ " $* " == *" --first-parent "* ]]; then
echo "First parent flag (--first-parent) detected"
first_parent="--first-parent"
fi
# https://www.funwithlinux.net/blog/restoring-stdout-and-stderr-to-default-value/
exec 4>&2 # Save original stderr (FD 2) to FD 4
git restore --staged rebase-check.diff 2>/dev/null
git cluntf rebase-check.diff 2>/dev/null
rm -rf rebase-check.diff 2>/dev/null
exec 2>&4 # Restore stderr by copying FD 4 to FD 2
exec 4>&- # Close the temporary FD 4
echo -e "Checking if rebase was correct..." > rebase-check.diff
echo -e "\n==============================" >> rebase-check.diff
echo -e "References before rebase: $1..$2\n" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $1 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $2 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
echo -e "\n==============================" >> rebase-check.diff
echo -e "References after rebase: $3..$4\n" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $3 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $4 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
echo "" >> rebase-check.diff
get_short_log "$1..$2" $suppress_whitespace_flag $unified_context $word_diff $first_parent >> rebase-check.diff
git add rebase-check.diff
echo -e "Checking if rebase was correct..." > rebase-check.diff
echo -e "\n==============================" >> rebase-check.diff
echo -e "References before rebase: $1..$2\n" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $1 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $2 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
echo -e "\n==============================" >> rebase-check.diff
echo -e "References after rebase: $3..$4\n" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $3 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
git log --format=format:'1. Commit **%h**: %<(80,trunc)%s' $4 -n1 >> rebase-check.diff
echo "" >> rebase-check.diff
echo "" >> rebase-check.diff
get_short_log "$3..$4" $suppress_whitespace_flag $unified_context $word_diff $first_parent >> rebase-check.diff
}
@disouzam
Copy link
Author

How to use

rebase_check {commit_hash_1} {commit_hash_2} {commit_hash_3} {commit_hash_4}

where
{commit_hash_1}: base of changes before rebase
{commit_hash_2}: tip of rebased branch before rebase
{commit_hash_3}: base of changes after rebase
{commit_hash_4}: tip of rebased branch after rebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment