This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathgit-publish.sh
executable file
·123 lines (98 loc) · 3.11 KB
/
git-publish.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
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
#!/bin/bash
ABORT="\033[31mAbort:\033[00m"
SUCCESS="\033[32mSuccess:\033[00m"
INFO="\033[33mInfo:\033[00m"
echo -e $INFO "Checking index"
#set branch to commit to
if [[ $# -gt 0 ]]
then
PUBLISH_TO_BRANCH=$1
else
PUBLISH_TO_BRANCH="master"
fi
echo -e $INFO "branch to commit: $PUBLISH_TO_BRANCH"
# check clean index
if [[ -n $(git diff HEAD) ]]
then
echo -e $ABORT "You have uncommitted changes."
exit 1
fi
BRANCH_NAME=$(git symbolic-ref -q --short HEAD)
PUBLISH_BRANCH_NAME=_publish_"$BRANCH_NAME"_
echo -e $INFO "Checking branch $BRANCH_NAME"
# check if on branch
if [ -z $BRANCH_NAME ]
then
echo -e $ABORT "You are not on any branch."
exit 1
fi
# check if on $PUBLISH_TO_BRANCH
if [ $BRANCH_NAME == $PUBLISH_TO_BRANCH ]
then
echo -e $ABORT "You are on $PUBLISH_TO_BRANCH. Use this script only from a feature branch."
exit 1
fi
git checkout -q $PUBLISH_TO_BRANCH
git remote update &>/dev/null
echo -e $INFO "Checking $PUBLISH_TO_BRANCH"
# check if $PUBLISH_TO_BRANCH is up to date
if [[ -n $(git diff origin/$PUBLISH_TO_BRANCH) ]]
then
echo -e $ABORT "Your $PUBLISH_TO_BRANCH is not up-to-date. Run 'git-sync.sh' to update $PUBLISH_TO_BRANCH and rebase $BRANCH_NAME."
git checkout -q $BRANCH_NAME
exit 1
fi
PUBLIC_BRANCH_HEAD_COMMIT=$(git rev-parse HEAD)
BASE_COMMIT=$(git merge-base $BRANCH_NAME $PUBLISH_TO_BRANCH)
git checkout -q $BRANCH_NAME
BRANCH_HEAD_COMMIT=$(git rev-parse HEAD)
# check if feature has commits
if [ $BRANCH_HEAD_COMMIT == $PUBLIC_BRANCH_HEAD_COMMIT ]
then
echo -e $ABORT "Your feature branch has no commits."
exit 1
fi
# check if feature is not behind $PUBLIC_BRANCH_HEAD_COMMIT
if [ $BASE_COMMIT != $PUBLIC_BRANCH_HEAD_COMMIT ]
then
echo -e $ABORT "Your feature branch is behind $PUBLISH_TO_BRANCH. Use 'git rebase $PUBLISH_TO_BRANCH' to update your feature branch."
exit 1
fi
# Prepare publish message with commit messages
GIT_DIR=$(git rev-parse --show-toplevel)/.git
cat $GIT_DIR/../setup/git/git_commit_template.txt > $GIT_DIR/PUBLISH_MSG
printf "\n# commits:\n" >> $GIT_DIR/PUBLISH_MSG
git log --graph --format=%B $BASE_COMMIT..HEAD | sed 's/^/# &/' >> $GIT_DIR/PUBLISH_MSG
# switch to temporary branch for squashing
git checkout -q -b $PUBLISH_BRANCH_NAME
echo -e $INFO "Squashing commits"
git reset --soft $BASE_COMMIT
git commit --template=$GIT_DIR/PUBLISH_MSG
if [ $? != 0 ]
then
echo -e $ABORT "Commit was aborted."
git checkout -q $BRANCH_NAME
git branch -q -D $PUBLISH_BRANCH_NAME
exit 1
fi
echo -e $INFO "Rebasing on $PUBLISH_TO_BRANCH"
git checkout -q $PUBLISH_TO_BRANCH
git rebase -q $PUBLISH_BRANCH_NAME
if [ $? != 0 ]
then
git checkout -q $BRANCH_NAME
echo -e $ABORT "Rebasing on $PUBLISH_TO_BRANCH failed because of unstaged changes."
exit 1
fi
git branch -q -d $PUBLISH_BRANCH_NAME
echo -e $INFO "Uploading to origin $PUBLISH_TO_BRANCH"
git push origin $PUBLISH_TO_BRANCH
if [ $? != 0 ]
then
git reset --hard -q HEAD^
git checkout -q $BRANCH_NAME
echo -e $ABORT "Pushing to origin $PUBLISH_TO_BRANCH has failed, there are new remote changes. Run 'git-sync.sh' to update $PUBLISH_TO_BRANCH and rebase $BRANCH_NAME."
exit 1
fi
echo -e $SUCCESS "Published $BRANCH_NAME successfully"
exit 0