-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgit-update-parent-branch
More file actions
executable file
·89 lines (78 loc) · 2.16 KB
/
git-update-parent-branch
File metadata and controls
executable file
·89 lines (78 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
# Updates the base or parent branch
CURRENT_BRANCH="$(git current-branch)"
PARENT_BRANCH=""
COMMIT_MODE="multi-commit"
UNTIL_COMMIT=""
COMMITS=""
while getopts ":-:" opt; do
case ${opt} in
-)
case ${OPTARG} in
"single-commit"*) COMMIT_MODE="single-commit";;
esac
esac
done
if [ "$COMMIT_MODE" == "single-commit" ]; then
if [ "$2" == "" ]; then
PARENT_BRANCH="$(git parent-branch)"
else
PARENT_BRANCH="$2"
fi
COMMITS="$(git rev-parse "$CURRENT_BRANCH")"
else
if [ "$1" == "" ]; then
PARENT_BRANCH="$(git parent-branch)"
COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$PARENT_BRANCH")"
else
PARENT_BRANCH="$1"
COMMON_ANCESTOR="$(git merge-base "$CURRENT_BRANCH" "$PARENT_BRANCH")"
COMMITS="$(git rev-list "$CURRENT_BRANCH"..."$COMMON_ANCESTOR")"
fi
if [ "$2" != "" ]; then
UNTIL_COMMIT="$2"
fi
fi
# TODO - What if we don't have commits?
echo "CURRENT BRANCH -> $CURRENT_BRANCH"
echo "PARENT BRANCH -> $PARENT_BRANCH"
echo "COMMIT LIST -> $COMMITS"
echo "----"
# TODO - What if a checkout fails?
git checkout -b "$CURRENT_BRANCH-temp"
git fetch
git checkout "$PARENT_BRANCH" > /dev/null 2>&1
git branch -D "$CURRENT_BRANCH" > /dev/null 2>&1
git checkout -b "$CURRENT_BRANCH" > /dev/null 2>&1
# TODO - What if a cherry-pick fails?
if [ "$COMMIT_MODE" == "single-commit" ]; then
echo "----"
echo "Cherry Picking -> $COMMITS"
git cherry-pick "$COMMITS"
else
echo 'Multi Commit Mode Enabled!'
COMMIT_ARRAY=($COMMITS)
COMMITS_TO_CHERRY=()
if [ "$UNTIL_COMMIT" != "" ]; then
for commit_id in "${COMMIT_ARRAY[@]}"
do
if [[ "$commit_id" =~ "$UNTIL_COMMIT" ]]; then
break
fi
COMMITS_TO_CHERRY+=("$commit_id")
done
fi
size=${#COMMITS_TO_CHERRY[@]}
for (( i=size-1; i>=0; i-- )); do
echo "----"
if [[ "${COMMITS_TO_CHERRY[i]}" =~ "$UNTIL_COMMIT" ]]; then
echo "NOT Cherry Picking! -> ${COMMITS_TO_CHERRY[i]}"
else
echo "Cherry Picking -> ${COMMITS_TO_CHERRY[i]}"
git cherry-pick "${COMMITS_TO_CHERRY[i]}"
fi
done
fi
echo "----"
git push --set-upstream origin -f "$CURRENT_BRANCH"
git log --oneline --color --decorate --graph --all