-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-merge-pr
executable file
·145 lines (123 loc) · 3.7 KB
/
git-merge-pr
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
PRUNE_FLAG=off
BRANCH_TO_MERGE=""
DESTINATION_BRANCH=""
function usage() {
echo "usage: git merge-pr [--no-prune] <src> <dest>"
}
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
usage
exit
fi
while :
do
case "$1" in
-h | -help )
usage
exit 0
;;
--no-prune )
PRUNE_FLAG=on
shift
;;
-* )
usage
exit 0
;;
* )
if [ -z $BRANCH_TO_MERGE ]; then
BRANCH_TO_MERGE="$1"
shift
elif [ -z $DESTINATION_BRANCH ]; then
if [ -n "$1" ]; then
DESTINATION_BRANCH="$1"
shift
else
usage
exit 0
fi
else
break
fi
esac
done
CURRENT_BRANCH="$(git symbolic-ref --short HEAD)"
WORKING_DIRECTORY=$(pwd)
unstash_changes() {
if [ "$STASH_OUTPUT" != "No local changes to save" ]; then
echo "Attempting to unstash local changes"
POP_STASH="$(git stash pop)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$POP_STASH"
exit 9
fi
fi
}
restore_original_state() {
echo "Attempting to return to branch $CURRENT_BRANCH"
CLEAR_INDEX="$(git reset --hard HEAD)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$CLEAR_INDEX"
fi
CHECKOUT_ORIGINAL_OUTPUT="$(git checkout -q $CURRENT_BRANCH)"
if [ $? != 0 ]; then
echo "ERROR:"
echo "$CHECKOUT_ORIGINAL_OUTPUT"
exit 8
fi
unstash_changes
cd $WORKING_DIRECTORY
}
run_command()
{
output=$(eval $1 2>&1)
if [ $? != 0 ]; then
printf "ERROR: %s\n" "$output"
return 1
else
return 0
fi
}
echo "Attempting to securely merge branch 'origin/$BRANCH_TO_MERGE' into 'origin/$DESTINATION_BRANCH'"
# prevent running on branch to be merged without --no-prune
if [ $CURRENT_BRANCH = $BRANCH_TO_MERGE ] && [ $PRUNE_FLAG = off ]; then
echo "ERROR: You are checked out on the branch you are trying to merge."
echo " This will prevent the tool from auto-pruning after merging."
echo " If you want to keep a local copy of this branch, please consider the --no-prune flag."
echo " Otherwise, check out a different branch and try again."
exit 10
fi
echo "Stashing any local changes and checking out remote branch 'origin/$DESTINATION_BRANCH'"
STASH_OUTPUT="$(git -c commit.gpgSign=false stash save -a)" || exit 1;
cd $(git rev-parse --show-toplevel)
echo "Securely fetching remote branches and revisions:"
echo " Fetching $DESTINATION_BRANCH..."
echo " <---- GPG signature required to commit RSL fetch entry"
run_command "git secure-fetch origin $DESTINATION_BRANCH" || { restore_original_state && exit 2; }
echo " Fetching $BRANCH_TO_MERGE..."
echo " <---- GPG signature required to commit RSL fetch entry"
run_command "git secure-fetch origin $BRANCH_TO_MERGE" || { restore_original_state && exit 2; }
echo "Cleaning up local repository"
run_command "git gc"
echo "Checking out $DESTINATION_BRANCH"
run_command "git checkout --detach origin/$DESTINATION_BRANCH" || { unstash_changes && exit 3; }
echo "Merging $BRANCH_TO_MERGE into $DESTINATION_BRANCH"
MERGE_MESSAGE="\"Merge 'origin/$BRANCH_TO_MERGE' into $DESTINATION_BRANCH\""
run_command "git merge -S --no-ff -m $MERGE_MESSAGE origin/$BRANCH_TO_MERGE" || { restore_original_state && exit 4; }
echo "Merge Successful!"
echo "Pushing to remote repository 'origin'"
echo " clearsigning RSL push entry and signing RSL push commit"
echo " <---- 2 GPG signatures required"
run_command "git secure-push origin HEAD:$DESTINATION_BRANCH" || { restore_original_state && exit 5; }
echo "Push Successful!"
if [ "$PRUNE_FLAG" = off ]; then
run_command "git push origin :$BRANCH_TO_MERGE" || { restore_original_state && exit 6; }
BRANCH_EXISTS="$(git show-ref refs/heads/$BRANCH_TO_MERGE)"
if [ -n "$BRANCH_EXISTS" ]; then
run_command "git branch -d $BRANCH_TO_MERGE" || { restore_original_state && exit 7; }
fi
fi
restore_original_state
exit 0