forked from elastic/detection-rules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commit-and-push.sh
executable file
·46 lines (36 loc) · 1.14 KB
/
commit-and-push.sh
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
#!/bin/bash
set -x
set -e
echo "Switch to the target branch and keep the staged changes"
TARGET_BRANCH=$1
COMMIT_SHA=$2
echo "Backporting from commit ${COMMIT_SHA} on branch ${TARGET_BRANCH}"
echo "Checking out target branch"
git checkout ${TARGET_BRANCH}
NEEDS_BACKPORT=$(git diff HEAD --quiet --exit-code && echo n || echo y)
if [ "n" = "$NEEDS_BACKPORT" ]
then
echo "No changes to backport"
exit 0
fi
echo "Create the new commit with the same author"
git commit --reuse-message ${COMMIT_SHA}
echo "Save the commit message"
git log ${COMMIT_SHA} --format=%B -n1 > $COMMIT_MSG_FILE
echo "Append to the commit message"
if [ -s "$UNSTAGED_LIST_FILE" ]
then
echo "Track note for the removed files"
echo "" >> $COMMIT_MSG_FILE
echo "Removed changes from:" >> $COMMIT_MSG_FILE
awk '{print "- " $0}' $UNSTAGED_LIST_FILE >> $COMMIT_MSG_FILE
echo "" >> $COMMIT_MSG_FILE
echo '(selectively cherry picked from commit ${COMMIT_SHA})' >> $COMMIT_MSG_FILE
else
echo "No removed files"
echo "" >> $COMMIT_MSG_FILE
echo '(cherry picked from commit ${COMMIT_SHA})' >> $COMMIT_MSG_FILE
fi
echo "Amend the commit message and push"
git commit --amend -F $COMMIT_MSG_FILE
git push