summaryrefslogtreecommitdiff
path: root/tcwg-upstream2gerrit.sh
blob: 88899321ad6469b7b5af677167e2a7f38760023f (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
#!/bin/bash

set -ex

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

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

. jenkins-helpers.sh

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")

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

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)
trap "rm -f $patch_file" 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
	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 reset $patch_file
		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

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