summaryrefslogtreecommitdiff
path: root/tcwg-upstream2gerrit.sh
blob: 03c5b8937a38ff7953e3bfe75256e6107c11c91c (plain)
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
#!/bin/bash

set -ef -o pipefail

# shellcheck source=jenkins-helpers.sh
. "$(dirname $0)"/jenkins-helpers.sh
convert_args_to_variables "$@"

# Make shellcheck happy and workaround Jenkins not defining variables
# for empty arguments.
branch="${branch-master}"
patches="${patches-last}"
project="${project-gcc}"
squash="${squash-false}"
filter="${filter-false}"
verbose="${verbose-true}"

# Jenkins doesn't define variables when parameter value is empty,
# so enable "set -u" only after above binding of variables.
set -u

if $verbose; then set -x; fi

rm -f pwclient
wget http://people.linaro.org/~maxim.kuvyrkov/pwclient/pwclient

pwc="$(pwd)/pwclient"
chmod +x "$pwc"
sed -i -e "s#~/.pwclientrc#$(pwd)/pwclientrc#g" "$pwc"
cat > pwclientrc <<EOF
[options]
default=$project

[$project]
url= https://patches-$project.linaro.org/xmlrpc/
EOF

last_id=$($pwc list -N 1 -f "%{id}")
if [ "$last_id" = "" ]; then
    echo "ERROR: Cannot fetch last patch id."
    exit 1
fi

patches=$(echo "$patches" | sed -e "s/last/$last_id/g")

refdir=/home/tcwg-buildslave/snapshots-ref/$project.git
if ! [ -d $refdir/.git ]; then
    refdir=""
fi

clone_or_update_repo $project refs/heads/$branch https://git-us.linaro.org/toolchain/$project $refdir

cd $project
cat > .gitreview <<EOF
[gerrit]
host=review.linaro.org
port=29418
project=toolchain/$project
EOF
git review -s
rm .gitreview

case "$project" in
    "gcc")
	# Attempt to apply patches to nested gcc/ directory if top-level
	# fails.
	try_dirs=". gcc"
	;;
    *) try_dirs="." ;;
esac

patch_file=$(mktemp)
# shellcheck disable=SC2064
trap "rm -f $patch_file /tmp/mydiff.$$" EXIT

count="0"
for patch in $patches; do
    download_file=$($pwc get -p $project $patch | sed -e "s/Saved patch to //")
    mv $download_file $patch_file

    res=0
    git reset --hard
    git clean -df
    if grep -q "diff --git" $patch_file; then
	git am $patch_file || res=$?
	if [ "$res" = "0" ]; then
	    git commit --amend --reset-author -C HEAD
	else
	    git am --abort
	fi
    else
	for dir in $try_dirs; do
	    pushd $dir
	    git reset --hard
	    git clean -df
	    patch -f -p0 < $patch_file || res=$?
	    if [ "$res" = "0" ]; then
		git add .
		git commit -m "Patch #$patch"
	    fi
	    popd
	    if [ "$res" = "0" ]; then
		break
	    fi
	done
    fi
    if [ "$res" != "0" ]; then
	echo "ERROR: Cannot apply patch $patch"
	exit 1
    fi
    count=$(($count+1))
done

# Apply the filter once the patch series has been applied, to decide
# whether we actually want review and validation
keepit=true
if $filter; then
    keepit=false
    git diff HEAD~$count..HEAD > /tmp/mydiff.$$

    # Keep commits impacting ARM or AArch64
    wanted1=0
    # Search the exact words arm, thumb or aarch64
    egrep '^[-+]' /tmp/mydiff.$$ | egrep -w -i 'arm|thumb|aarch64' || wanted1=$?
    wanted2=0
    # Search the same strings with '_' prefix or suffix, excluding string with 'parm'
    egrep '^[-+]' /tmp/mydiff.$$ | egrep -i '_arm|arm_|_thumb|thumb_|_aarch64|aarch64_' | grep -v -i 'parm' || wanted2=$?
    if [ $wanted1 -eq 0 ] || [ $wanted2 -eq 0 ]
    then
	keepit=true
    else
	# We could keep commits from ARM, Linaro, or whitelist if we had a ChangeLog
	echo "Cannot filter authors without a ChangeLog"
	keepit=false
    fi
fi

if ! $keepit; then
    echo "The filter decided to skip this patch"
    exit 0
fi

if $squash; then
    git reset --soft HEAD~$count
    git commit -m "Patches: $patches"
fi
git review -y "$branch"