• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#set -x
3
4# used for projects where some files are mainline, some are not
5# we get a list of the files/directories out of the project's root.
6#
7# invocation   $0  ${repo_root} ${preupload_files}
8#
9# Example PREUPLOAD.cfg:
10#
11# [Hook Scripts]
12# mainline_hook = ${REPO_ROOT}/frameworks/av/tools/mainline_hook_partial.sh ${REPO_ROOT} ${PREUPLOAD_FILES}
13#
14# MainlineFiles.cfg syntax:
15#
16# ignore comment (#) lines and blank lines
17# rest are path prefixes starting at root of the project
18# (so OWNERS, not frameworks/av/OWNERS)
19#
20# path
21# INCLUDE path
22# EXCLUDE path
23#
24# 'path' and 'INCLUDE path' are identical -- they both indicate that this path
25# is part of mainline
26# EXCLUDE indicates that this is not part of mainline,
27# so 'foo/' and 'EXCLUDE foo/nope'
28# means everything under foo/ is part of mainline EXCEPT foo/nope.
29# INCLUDE/EXCLUDE/INCLUDE nested structuring is not supported
30#
31# matching is purely prefix
32# so 'foo' will match 'foo', 'foo.c', 'foo/bar/baz'
33# if you want to exclude a directory, best to use a pattern like "foo/"
34#
35
36## tunables:
37##
38DEV_BRANCH=master
39MAINLINE_BRANCH=sc-mainline-prod
40filelist_file=MainlineFiles.cfg
41
42###
43
44REPO_ROOT=$1; shift
45# the rest of the command line is the file list
46PREUPLOAD_FILES="$*"
47
48RED=$(tput setaf 1)
49NORMAL=$(tput sgr0)
50
51## get the active branch:
52## * <localbranch> <shainfo> [goog/master] Fix to handle missing checks on error returned
53## strip this down to "master"
54## * b157501573_advisory 25521834a6 [goog/sc-dev] Merge "PlayerBase: add audio session ID" into sc-dev
55##
56current=`git branch -vv | grep -P "^\*[^\[]+\[goog/"|sed -e 's/^.*\[//' | sed -e 's/\].*$//'|sed -e 's/:.*$//'| sed -e 's/^goog\///'`
57if [ "${current}" = "" ] ; then
58        current=unknown
59fi
60
61## figure out whether which files are for mainline and which are not
62if [ "${PREUPLOAD_FILES}" = "" ] ; then
63    # empty files? what's up there, i suppose we'll let that go
64    exit 0
65fi
66
67## get the list of files out of the project's root
68## figure out which way I'm going ..
69## use list of files to scan PREUPLOAD_FILES
70## use PREUPLOAD_FILES to scan the list of good/bad from the project root
71##
72## remember to do an exclude, so I can say
73## include/these/files/
74## EXCLUDE include/these/files/nested/
75##
76## and it should all be prefix based stuff...
77
78if [ ! -f ${REPO_ROOT}/${REPO_PATH}/${filelist_file} ] ; then
79    echo "Poorly Configured project, missing ${filelist_file} in root of project"
80    exit 1
81fi
82
83# is 1st arg a prefix of 2nd arg
84beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
85
86exclusions=""
87inclusions=""
88while read p1 p2
89do
90    # ignore comment lines in the file
91    # ignore empty lines in the file
92    if beginswith "#" "${p1}" ; then
93        # ignore this line
94        true
95    elif [ -z "${p1}" ] ; then
96        # ignore blanks
97        true
98    elif [ ${p1} = "EXCLUDE" ] ; then
99        # add to the exclusion list
100        if [ ! -z ${p2} ] ; then
101            exlusions="${exclusions} ${p2}"
102        fi
103    elif [ ${p1} = "INCLUDE" ] ; then
104        # add to the inclusion list
105        if [ ! -z ${p2} ] ; then
106            inclusions="${inclusions} ${p2}"
107        fi
108    elif [ ! -z ${p1} ] ; then
109        inclusions="${inclusions} ${p1}"
110    fi
111done < ${REPO_ROOT}/${REPO_PATH}/${filelist_file}
112
113# so we can play with array syntax
114#INCLUSIONS=( ${inclusions} )
115#EXCLUSIONS=( ${exclusions} )
116
117mainline_yes=""
118mainline_no=""
119
120# is it part of the list of mainline files/directories?
121for path in ${PREUPLOAD_FILES} ; do
122    #echo is ${path} a mainline file...
123    for aprefix in ${inclusions} .. ; do
124        #echo compare against ${aprefix} ...
125        if [ "${aprefix}" = ".." ] ; then
126            mainline_no="${mainline_no} ${path}"
127        elif beginswith ${aprefix} ${path} ; then
128            mainline_yes="${mainline_yes} ${path}"
129            break       # on to next uploaded file
130        fi
131    done
132done
133
134# TODO: audit the yes list to see if some should be moved to the no list
135
136# 3 situations
137# -- everything is on mainline (mainline_yes non-empty, other empty)
138# -- some is mainline, some is not (files_* both non-empty)
139# -- none is mainline   (mainline_yes empty, other non_empty
140# -- both empty only happens if PREUPLOAD_FILES is empty, covered above
141
142if [ -z "${mainline_yes}" ] ; then
143    # no mainline files, everything else is non-mainline, let it go
144    exit 0
145fi
146
147#
148# exit 0 is "all good, no output passed along to user"
149# exit 77 is "a warning, pass along the output to the user"
150# exit 1 will be a failure.
151#
152result=0
153
154# simple reminder that it should also land in mainline branch
155#
156if [ "${current}" != "${MAINLINE_BRANCH}" ] ; then
157        # simple reminder to ensure it hits mainline
158        result=77
159        cat - <<EOF
160You are uploading repo  ${RED}${REPO_PATH}${NORMAL} to branch ${RED}${current}${NORMAL}.
161The mainline branch for ${RED}${REPO_PATH}${NORMAL} is branch ${RED}${MAINLINE_BRANCH}${NORMAL}.
162
163Ensure an appropriate cherry pick or equivalent lands in branch ${RED}${MAINLINE_BRANCH}${NORMAL}.
164Security bulletin timing or unreleased functionality may drive when this can be landed.
165EOF
166fi
167
168# watch for the mixed some mainline / some not CL
169# we usually want to reject such mixed CLs
170#
171
172if [ ! -z "${mainline_no}" ] ; then
173        # mixed bag, suggest (not insist) that developer split them.
174        result=1
175        cat - <<EOF
176This change contains both mainline and non-mainline files.  Please separate
177them into separate CLs. It may also be appropriate to update the list of mainline
178files in ${RED}${REPO_ROOT}/${filelist_file}${NORMAL}.
179
180EOF
181        echo "===== Mainline files ====="
182        echo -e ${RED}
183        echo ${mainline_yes} | sed -e 's/ /
184/g'
185        echo -e ${NORMAL}
186
187        echo "===== Non-Mainline files ====="
188        echo -e ${RED}
189        echo ${mainline_no} | sed -e 's/ /
190/g'
191        echo -e ${NORMAL}
192
193    cat - <<EOF
194
195If you are sure you want to proceed uploading to branch ${RED}${current}${NORMAL},
196re-run your repo upload command with the '--no-verify' option
197
198EOF
199fi
200
201# result will be:
202# 0: all good, no output passed to user
203# 77: warnings (but we pass), output passed along to user
204# else: failure, output passed along to the user
205
206exit ${result}
207
208