|
1 | 1 | #!/bin/bash |
2 | 2 |
|
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. |
6 | 6 | # |
7 | 7 | # Branch A Branch B <target-branch> |
8 | | -# | 2bce381 | a7da927 |
| 8 | +# | 2bce381 | a7da927 |
9 | 9 | # | 895eb6d | e069629 |
10 | 10 | # | 1e9f10f | ... |
11 | 11 | # | f3db518 | ... |
|
17 | 17 | # For example, if you want to rebase commits from f50f78b to 2bce381 from branch A to branch B, |
18 | 18 | # you can run the following command: |
19 | 19 | # |
20 | | -# ./rebase_helper.sh <branch-B> 95f677a |
| 20 | +# ./rebase_helper.sh <branch-B> |
21 | 21 | # |
| 22 | +# Commit 95f677a would be identified as the base commit. |
22 | 23 |
|
23 | 24 | TARGET=$1 |
24 | | -BASE=$2 |
25 | 25 |
|
26 | 26 | # 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>" |
29 | 29 | exit 1 |
30 | 30 | fi |
31 | 31 |
|
| 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 | + |
32 | 60 | echo "Commits to be Rebased:" |
33 | | -git log --oneline ${BASE}..HEAD |
| 61 | +git log --oneline ${commit}..HEAD |
34 | 62 |
|
35 | | -echo |
| 63 | +echo |
36 | 64 | echo "Start Rebasing" |
37 | | -git rebase --onto ${TARGET} ${BASE} |
| 65 | +git rebase --onto ${TARGET} ${commit} |
0 commit comments