Skip to content

Commit 12e1597

Browse files
committed
Update rebasing helper
1 parent 5178f07 commit 12e1597

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

rebase_helper.sh

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/bin/bash
22

3-
# This script is used to rebase the current branch onto the target branch. The base
4-
# commit is the commit prior to the earliest commits you want to rebase. The target
5-
# branch is the branch you want to rebase onto.
3+
# This script is used to rebase the current branch onto the target branch. The target
4+
# branch is the branch you want to rebase onto. The script will search for the latest
5+
# commit that is the same as the target branch and use it as the base commit.
66
#
77
# Branch A Branch B <target-branch>
8-
# | 2bce381 | a7da927
8+
# | 2bce381 | a7da927
99
# | 895eb6d | e069629
1010
# | 1e9f10f | ...
1111
# | f3db518 | ...
@@ -17,21 +17,49 @@
1717
# For example, if you want to rebase commits from f50f78b to 2bce381 from branch A to branch B,
1818
# you can run the following command:
1919
#
20-
# ./rebase_helper.sh <branch-B> 95f677a
20+
# ./rebase_helper.sh <branch-B>
2121
#
22+
# Commit 95f677a would be identified as the base commit.
2223

2324
TARGET=$1
24-
BASE=$2
2525

2626
# Check if the target branch and base commit are provided
27-
if [ -z "$TARGET" ] || [ -z "$BASE" ]; then
28-
echo "Usage: $0 <target-branch> <base-commit>"
27+
if [ -z "$TARGET" ]; then
28+
echo "Usage: $0 <target-branch>"
2929
exit 1
3030
fi
3131

32+
# Ignore certain files and directories due to git-lfs limitations
33+
ignore=':(exclude)assets/*.gif :(exclude).gitattributes'
34+
35+
# Iterate over commit history backward till the initial commit
36+
commit=$(git rev-parse HEAD)
37+
38+
while [ -n "$commit" ]; do
39+
echo "Processing commit: $commit"
40+
41+
# Check if the diff between the commit and the target branch is empty
42+
if [ "$(git diff $commit $TARGET -- $ignore)" == "" ]; then
43+
# Print the commit message in one line
44+
echo $(git log -1 --oneline $commit)
45+
break
46+
fi
47+
48+
# Get the parent commit, if it exists
49+
parent_commit=$(git rev-parse "$commit^" 2>/dev/null)
50+
51+
# If there is no parent commit (i.e., we're at the initial commit), break the loop
52+
if [ $? -ne 0 ]; then
53+
break
54+
fi
55+
56+
# Move to the parent commit
57+
commit=$parent_commit
58+
done
59+
3260
echo "Commits to be Rebased:"
33-
git log --oneline ${BASE}..HEAD
61+
git log --oneline ${commit}..HEAD
3462

35-
echo
63+
echo
3664
echo "Start Rebasing"
37-
git rebase --onto ${TARGET} ${BASE}
65+
git rebase --onto ${TARGET} ${commit}

0 commit comments

Comments
 (0)