summaryrefslogtreecommitdiff
path: root/functions
blob: 5250f8303e060470f6d458b6bc1c12e9d0872657 (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
# vim:syn=sh

init() {
	# AOSP's gerrit doesn't like anonymous checkouts
	git config --global user.email &>/dev/null || git config --global user.email buildbot@linaro.org
	git config --global user.name &>/dev/null || git config --global user.name "Linaro Build Bot"
}

apply() {
	local REPO
	local DIR
	local SERVER
	local LOCAL_DIR
	if [ "$1" = "--linaro" ]; then
		SERVER="git://android-review.linaro.org/"
		shift
	elif [ "$1" = "--server" ]; then
		shift
		SERVER="$1"
		shift
	else
		SERVER="https://android.googlesource.com/"
	fi

	# Make specifying local path possible
	if [ "$1" = "--local" ]; then
		shift
		LOCAL_DIR="$1"
		shift
	else
		LOCAL_DIR="$1"
	fi

	if [ "$1" = "--remote" ]; then
		## when --remote specified, use the next argument as remote project path directly
		REMOTE_OPT=true
		shift
		REPO="$1"
	elif echo "$1" |grep -q -e '^device/' -e '^kernel/' -e '^android/' -e '^landing-teams/'; then
		# device and kernel have different path on repo to projects under platform
		REPO="$1"
	elif [ "$1" = "build/make" ]; then
		# Special case -- repository location and checkout location
		# don't match in upstream manifests
		REPO="platform/build"
	else
		REPO="platform/$1"
	fi
	DIR=$(echo "$2" |cut -d/ -f1 |rev |cut -b1-2 |rev)

	cd "$AOSP"/"${LOCAL_DIR}"
	echo "=== Applying $2 ==="
	if ! git fetch "$SERVER$REPO" "refs/changes/$DIR/$2"; then
		echo "Failed to fetch $2, typo in script?"
		exit 1
	fi
	if ! git cherry-pick FETCH_HEAD; then
		echo "$2 failed to apply, please fix"
		git reset --hard
		exit 1
	fi
	PATCHES=$((PATCHES+1))
	cd "$AOSP"
}

cherrypick() {
	local URL=""
	local BRANCH=""
	if [ "$1" = "--url" ]; then
		shift
		URL="$1"
		shift
	fi
	if [ "$1" = "--branch" ]; then
		shift
		BRANCH="$1"
		shift
	fi
	cd "$AOSP"/"$1"
	if [ -n "${URL}" ]; then
		local remote_name=$(mktemp -u cherrypick-server-XXXX)
		if ! git remote add "${remote_name}" "${URL}"; then
			echo "Failed to add remote ${remote_name} for ${URL}"
			exit 1
		fi
		if ! git fetch "${remote_name}" "${BRANCH}"; then
		echo "Failed to fetch from ${remote_name} for branch ${BRANCH}"
		exit 1
		fi
	fi
	echo "=== Cherry-picking $2 ==="
	if ! git cherry-pick "$2"; then
		echo "$2 failed to apply, please fix"
		git reset --hard
		exit 1
	fi
	PATCHES=$((PATCHES+1))
	cd "$AOSP"
}

revert() {
	local PATCH
	PATCH=false
	if [ "$1" = "--patch" ] || [ "$1" = "-p" ]; then
		PATCH=true
		shift
	fi
	cd "$AOSP"/"$1"
	echo "=== Reverting $2 ==="
	if $PATCH; then
		if ! git show "$2" |patch -p1 -R; then
			echo "$2 failed to revert by patching, please fix"
			exit 1
		fi
		git commit -am "Revert $2"
	else
		if ! git revert --no-edit "$2"; then
			echo "$2 failed to revert, please fix"
			git reset --hard
			exit 1
		fi
	fi
	PATCHES=$((PATCHES+1))
	cd "$AOSP"
}

curl_am(){
    local PATCH_URL=$1 && shift
    local PATCH_PROJECT=$1 && shift

    cd "$AOSP"/"${PATCH_PROJECT}"
    echo "=== AM Applying patch ${PATCH_URL} ==="

    curl "${PATCH_URL}"|git am
    if [ $? -ne 0 ]; then
        echo "Failed to apply patch ${PATCH_URL} for project $2"
        git reset --hard
        cd "$AOSP"
        exit 1
    fi
    cd "$AOSP"
}