summaryrefslogtreecommitdiff
path: root/precommit-ssh-apply.sh
blob: e96033702db5f30fd4ab4f9efd35ff9a375cf36c (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
#!/bin/bash

set -euf -o pipefail +x

scripts=$(dirname $0)
# shellcheck source=jenkins-helpers.sh
. $scripts/jenkins-helpers.sh
# shellcheck source=pw-helpers.sh
. $scripts/pw-helpers.sh

convert_args_to_variables "$@"

obligatory_variables pw_url
declare              pw_url

fetch_only="${fetch_only-false}"
series_dir="${series_dir-}"
verbose="${verbose-true}"

if $verbose; then
    set -x
fi

case "$pw_url" in
    "ssh://"*)
	# ssh://ssh_host:path/to/maildir[#num_patch]
	# Note that CI will ssh to $ssh_host as tcwg-buildslave user.
	true
	;;
    *) assert_with_msg "pw_url does not match ssh://*" false ;;
esac

series_url=$(echo "$pw_url" | cut -d/ -f 3- | sed -e "s/#.*//")
case "$series_url" in
    *".patch") ;;
    *"/") ;;
    *) series_url="$series_url/" ;;
esac

num_patch=$(echo "$pw_url" | cut -s -d# -f 2)
if [ "$num_patch" = "" ]; then
    num_patch="0"
fi

if [ "$series_dir" = "" ]; then
    series_dir=$(mktemp -d)
    trap 'rm -rf "$series_dir"' EXIT
fi
rsync -a "$series_url" "$series_dir/new/"

if $fetch_only; then
    exit 0
fi

obligatory_variables project pw_dir build_url patch_submitter
declare              project pw_dir build_url patch_submitter

url=$(get_baseline_git "${project}_url")
rev=$(get_baseline_git "${project}_rev")
echo "Fetching baseline $url#$rev"
clone_or_update_repo "$project" "$rev" "$url" > /dev/null

# Create a state file for this patch.  This is sourced by pw-report.sh to avoid
# passing a bunch of parameters on the command line.
mkdir -p "$pw_dir"
cat > "$pw_dir/$project" <<EOF
pw[project]='$project'
pw[${project}]='$project'
pw[${project}_patch_id]='$num_patch'
pw[${project}_check_cmd]='echo REPORT'
pw[${project}_build_url]='$build_url'
pw[${project}_patch_url]='$pw_url'
pw[${project}_patch_message_id]='no_message_id'
pw[${project}_patch_submitter]='$patch_submitter'
EOF

prev_head=$(git -C "$project" rev-parse HEAD)

apply_series_with_retry "$project" $prev_head am "$series_dir/" "$series_url" &
res=0 && wait $! || res=$?
patches_applied=$(git -C "$project" rev-list --count HEAD "^$prev_head")

if [ "$patches_applied" -le "$num_patch" ]; then
    apply_result="fail"
    if [ "$res" = "0" ] && [ "$patches_applied" = "0" ]; then
	# "series apply" finished successfully, but no patch was applied;
	# this means that the patch is already merged.
	apply_result="merged"
    fi

    cat > "$pw_dir/mail-recipients.txt" <<EOF
$patch_submitter
EOF
    cat > "$pw_dir/mail-subject.txt" <<EOF
[Linaro-TCWG-CI] $pw_url failed to apply
EOF
    cat > "$pw_dir/mail-body.txt" <<EOF
${build_url}artifact/artifacts/jenkins/precommit-ssh-apply.log/*view*/

Patch series $pw_url applied fewer patches than expected: $patches_applied vs expected $(($num_patch + 1))"
EOF
    if [ "$apply_result" = "merged" ]; then
	cat >> "$pw_dir/mail-body.txt" <<EOF

Most likely the patch series is now merged into mainline"
EOF
    fi

    if [ $res != 0 ]; then
	exit $res
    fi


    exit 5
fi

git -C "$project" checkout --detach HEAD~$num_patch

# Some debug traces, to check which files were modified and their time stamps
(
    set +e
    cd "$project"
    git status
    git status -s | awk '{print $NF;}' | while IFS='' read -r line
    do
	ls -l "$line"
    done
)