forked from cms-sw/cms-git-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-cms-merge-topic
executable file
·164 lines (152 loc) · 4.62 KB
/
git-cms-merge-topic
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash -e
case `uname` in
Linux) ECHO="echo -e" ;;
*) ECHO="echo" ;;
esac
usage() {
$ECHO "git cms-merge-topic [options] <github-user>:<branch>"
$ECHO "git cms-merge-topic [options] <official-cmssw-branch>"
$ECHO "git cms-merge-topic [options] <pull-request-id>"
$ECHO
$ECHO "Options:"
$ECHO "-d, --debug \tenable debug output"
$ECHO " --https \taccess GitHub over https (default)"
$ECHO " --ssh \taccess GitHub over ssh"
$ECHO "-q, --quiet \tbe completely quiet"
$ECHO "--no-commit \tdo not do the final commit when merging"
$ECHO "-s, --strategy \tspecify strategy when merging"
$ECHO "-X, --strategy-option \tspecify strategy when merging"
$ECHO "-u, --unsafe \tdo not perform checkdeps at the end"
$ECHO "-v, --verbose \tshow verbose output"
exit 1
}
# colors and formatting
RED='\033[31m'
NORMAL='\033[0m'
NORMAL_STREAM=/dev/stdout
VERBOSE_STREAM=/dev/stdout
DEBUG_STREAM=/dev/null
PROTOCOL=https
while [ $# -gt 0 ]; do
case $1 in
-u|--unsafe)
UNSAFE=true
shift
;;
-v|--verbose)
NORMAL_STREAM=/dev/stdout
VERBOSE_STREAM=/dev/stdout
DEBUG_STREAM=/dev/null
shift
;;
-q|--quiet)
NORMAL_STREAM=/dev/null
VERBOSE_STREAM=/dev/null
DEBUG_STREAM=/dev/null
shift
;;
-d|--debug)
NORMAL_STREAM=/dev/stdout
VERBOSE_STREAM=/dev/stdout
DEBUG_STREAM=/dev/stdout
DEBUG_OPT=-d
set -x
shift
;;
--https )
PROTOCOL=https
shift
;;
--ssh )
PROTOCOL=ssh
shift
;;
--no-commit )
NO_COMMIT=--no-commit
shift
;;
-s | --strategy )
MERGE_STRATEGY="-s $2"
shift; shift
;;
-X | --strategy-option )
STRATEGY_OPTION="-X $2"
shift; shift
;;
-*)
echo "Unknown option $1" ; exit 1 ;;
*)
if [ ! X$BRANCH = X ]; then
echo "Unexpected extra argument $1" ; exit 1
fi
# Handle branch options:
#
# - Generic personal branch: `<user-name>:<branch-name>`
# - Generic cms-sw branch: `<branch-name>`
# - Pull request: `<pull-request-id>`
GITHUB_USER=cms-sw ;
BRANCH=$1 ;
case $1 in
*:*)
BRANCH=`echo $1 | cut -f2 -d:`
GITHUB_USER=`echo $1 | cut -f1 -d:` ;;
[0-9]*)
BRANCH=refs/pull/$1/head ;;
esac ;
shift
;;
esac
done
if [ X$BRANCH = X ]; then
usage
fi
PULL_ID=$1
# initialize the local repository
if [ -z "$CMSSW_BASE" ]; then
echo "CMSSW environment not setup, please run 'cmsenv' before 'git cms-merge-topic'."
exit 1
fi
if ! [ -d $CMSSW_BASE/src/.git ]; then
git cms-init $INITOPTIONS
fi
cd $CMSSW_BASE/src
git fetch . +HEAD:merge-attempt || { echo "You are on a failed merge branch. Do \"git branch\" and checkout the one you were on."; exit 1; }
if [ "$(git status --porcelain --untracked=no | grep '^[ACDMRU]')" ]; then
$ECHO "${RED}Error:${NORMAL} there are staged but not committed changes on your working tree, please commit or stash them."
exit 1
fi
if [ "$PROTOCOL" = "ssh" ]; then
[email protected]:$GITHUB_USER/cmssw.git
else
REPOSITORY=https://github.com/$GITHUB_USER/cmssw.git
fi
# check if the "branch" is actually an annotated tag, and dereference it
COMMIT=`git ls-remote -t $REPOSITORY $BRANCH^{} | cut -c -40`
if [ -z "$COMMIT" ]; then
COMMIT=$BRANCH
fi
# Fetch the branch specified from github and replace merge-attempt with it.
# The + is used to force the merge-attampt branch to be updated.
git fetch -n $REPOSITORY +$COMMIT:$GITHUB_USER/$BRANCH
# Save the name of the current branch.
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
# Attempt a merge in a separate branch
git checkout merge-attempt >$DEBUG_STREAM
MERGE_BASE=`git merge-base $GITHUB_USER/$BRANCH $CURRENT_BRANCH`
git cms-sparse-checkout $DEBUG_OPT $MERGE_BASE $GITHUB_USER/$BRANCH
git read-tree -mu HEAD
git merge $NO_COMMIT $MERGE_STRATEGY $STRATEGY_OPTION --no-ff -m "Merged $BRANCH from repository $GITHUB_USER" $GITHUB_USER/$BRANCH || { echo "Unable to merge branch $BRANCH from repository $GITHUB_USER." ; exit 1; }
if [ ! X$NO_COMMIT = X ]; then
echo \"--no-commit\" specified: not committing and leaving you on the merge-attempt branch.\n Use git-status to check changes. ; exit 0
fi
git checkout $CURRENT_BRANCH
# Add the missing files.
git read-tree -mu HEAD
# This should always be a FF commit.
git merge --ff merge-attempt >$DEBUG_STREAM
# Delete the branch used for merge
git branch -D merge-attempt >$DEBUG_STREAM || true
# Do checkdeps unless not specified.
if [ ! "X$UNSAFE" = Xtrue ]; then
git cms-checkdeps -a
fi