forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmsdist-tool-update.sh
executable file
·146 lines (134 loc) · 4.81 KB
/
cmsdist-tool-update.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash -ex
TOOL=$1
TOOL_BRANCH=$(echo $2| sed 's|:.*||')
CMSDIST_BRANCH=$3
TOOL_CHECKOUT_CMD="git checkout $TOOL_BRANCH"
if [ $(echo $2 | grep ':' | wc -l) -gt 0 ] ; then
TOOL_CHECKOUT_CMD="${TOOL_CHECKOUT_CMD} ; git checkout $(echo $2 | sed 's|.*:||')"
fi
if [ "X$TOOL" = "X" ] ; then echo "Missing tool name"; exit 1; fi
if [ "X$TOOL_BRANCH" = "X" ] ; then echo "Missing tool branch name"; exit 1; fi
if [ "X$CMSDIST_BRANCH" = "X" ] ; then echo "Missing cmsdist branch name"; exit 1; fi
TOOL_TAG_CMD="git log --pretty=format:'%h' -n 1"
function get_rpm_marco_value (){
#Needs spec file path and rpm variable
grep "^ *%define *$2 " $1 | tail -1 | sed "s|.*$2 *||;s| *$||"
}
function get_package_info (){
#Needs the spec file path in $1
#Returns cmsrepo:branch:commit:fork_repo
for line in $(grep '^Source' ${1} | grep 'github.com' | sed 's|.*github.com/||;s| .*$||') ; do
local cmsrep=$(echo $line | sed 's|\?.*$||;s|\.git$||;s|\%||;s|{||;s|}||;s|github_user|cms-sw|')
local objinfo=$(echo $line | sed 's|.*obj=||;s|&.*$||')
local branch=$(echo ${objinfo} | sed 's|/.*$||;s|\%||;s|{||;s|}||')
local commit=$(echo ${objinfo} | sed 's|^.*/||;s|\%||;s|{||;s|}||')
local fork_repo=$(curl -s https://api.github.com/repos/${cmsrep} | grep forks_url | tail -1 | sed 's|.*/api.github.com/repos/||;s|/forks.*||')
echo "${cmsrep}:${branch}:${commit}:${fork_repo}"
done
}
function commit_cmsdist(){
local TOOL=$1
local TOOL_BRANCH=$2
local PUSH_CMDS_FILE=$3
pushd cmsdist
git commit -a -m "Updated $TOOL to tip of branch ${TOOL_BRANCH}"
NEW_BRANCH=$TOOL-update-${TOOL_BRANCH}-$(date +%Y%m%d)
git checkout -b $NEW_BRANCH
echo "cd $(/bin/pwd)" >> $PUSH_CMDS_FILE
echo "git push my $NEW_BRANCH" >> $PUSH_CMDS_FILE
popd
}
case $(whoami) in
cmsbuild|cmsbld) GITHUB_USER="cms-sw" ;;
*) GITHUB_USER=$(cat ~/.gitconfig | grep github | sed 's|.*= *||;s| *$||') ;;
esac
if [ "X$GITHUB_USER" = "X" ] ; then
echo "Error: Unable to find the github user name"
exit 1
fi
if [ ! -d cmsdist ] ; then
git clone [email protected]:cms-sw/cmsdist
pushd cmsdist
git remote add cms [email protected]:cms-sw/cmsdist
git remote add my [email protected]:$GITHUB_USER/cmsdist
git checkout $CMSDIST_BRANCH
popd
fi
PUSH_CMDS_FILE=`/bin/pwd`/push-branches.sh
COMMIT_CMSDIST=NO
if [ ! -f $PUSH_CMDS_FILE ] ; then
COMMIT_CMSDIST=YES
echo "#!/bin/bash -ex" > $PUSH_CMDS_FILE
chmod +x $PUSH_CMDS_FILE
fi
if [ "X$4" = "X" ] ; then
for line in $(get_package_info cmsdist/${TOOL}.spec) ; do
$0 "$1" "$2" "$3" $(echo $line | tr ':' ' ')
done
commit_cmsdist $TOOL ${TOOL_BRANCH} $PUSH_CMDS_FILE
exit 0
fi
TOOL_CMS_REPO=$4
TOOL_REG_BRANCH=$5
TOOL_REG_TAG=$6
TOOL_FORK_REPO=$7
TOOL_REPO_NAME=$(echo $TOOL_FORK_REPO | sed 's|.*/||')
TOOL_DOWNOAD_CMD="git clone [email protected]:${TOOL_FORK_REPO} ${TOOL_REPO_NAME}; cd ${TOOL_REPO_NAME}; ${TOOL_CHECKOUT_CMD}"
OLD_TOOL_HASH=$(get_rpm_marco_value cmsdist/${TOOL}.spec ${TOOL_REG_BRANCH} | sed 's|cms/||')
if [ "X$OLD_TOOL_HASH" = "X" ] ; then
echo "Error: Unable to get OLD_TOOL_HASH using cmsdist/$CMSDIST_BRANCH/$TOOL.spec"
exit 1
fi
mkdir -p tool
pushd tool
eval $TOOL_DOWNOAD_CMD
NEW_TOOL_HASH=`eval $TOOL_TAG_CMD`
echo "[$NEW_TOOL_HASH]"
echo "[$OLD_TOOL_HASH]"
if [ "$NEW_TOOL_HASH" = "$OLD_TOOL_HASH" ] ; then
echo "New and old branches are same: $OLD_TOOL_HASH"
exit 0
fi
if [ ! -d .git ] ; then
git init
git add .
fi
git remote add cms [email protected]:${TOOL_CMS_REPO}
git fetch cms cms/$NEW_TOOL_HASH:cms/$NEW_TOOL_HASH || true
git branch
if [ "X`git branch | grep \"^ *cms/$NEW_TOOL_HASH\$\"`" != "X" ] ; then
echo "cms/$NEW_TOOL_HASH already exists in cms-sw/$TOOL.git"
exit 1
fi
git checkout -b cms/$NEW_TOOL_HASH
git fetch cms cms/$OLD_TOOL_HASH:cms/$OLD_TOOL_HASH
echo CMS Changes
git log --oneline $OLD_TOOL_HASH..cms/$OLD_TOOL_HASH | awk '{print $1}' > ../commits
for commit in $(tac ../commits); do
git show --oneline $commit | head -1
while [ true ] ; do
echo "Cherry pick commit $commit [Y/N/Q]:"
if [ "X$FORCE_APPLY_CMS_COMMITS" != "Xtrue" ] ; then
read -n 1 RES
else
RES=Y
fi
if [ "X$RES" = "XY" ] ; then
git cherry-pick $commit
break
elif [ "X$RES" = "XN" ] ; then
break
elif [ "X$RES" = "XQ" ] ; then
exit 0
fi
done
done
echo "cd $(/bin/pwd)" >> $PUSH_CMDS_FILE
echo "git push cms cms/$NEW_TOOL_HASH" >> $PUSH_CMDS_FILE
TOOL_TAG=`git log | head -1 | awk '{print $2}'`
popd
sed -i -e "s|^%define *${TOOL_REG_BRANCH} *cms/.*$|%define ${TOOL_REG_BRANCH} cms/$NEW_TOOL_HASH|" cmsdist/$TOOL.spec
sed -i -e "s|^%define *${TOOL_REG_TAG} .*$|%define ${TOOL_REG_TAG} $TOOL_TAG|" cmsdist/$TOOL.spec
if [ "$COMMIT_CMSDIST" = "YES" ] ; then
commit_cmsdist $TOOL ${TOOL_BRANCH} $PUSH_CMDS_FILE
fi