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=rvc-dev
39filelist_file=MainlineFiles.cfg
40
41###
42
43REPO_ROOT=$1; shift
44# the rest of the command line is the file list
45PREUPLOAD_FILES="$*"
46
47RED=$(tput setaf 1)
48NORMAL=$(tput sgr0)
49
50## get the active branch:
51## * <localbranch> <shainfo> [goog/master] Fix to handle missing checks on error returned
52## strip this down to "master"
53##
54current=`git branch -vv | grep -P "^\*[^\[]+\[goog/"|sed -e 's/^.*\[//' | sed -e 's/:.*$//'| sed -e 's/^goog\///'`
55if [ "${current}" = "" ] ; then
56        current=unknown
57fi
58
59## figure out whether which files are for mainline and which are not
60if [ "${PREUPLOAD_FILES}" = "" ] ; then
61    # empty files? what's up there, i suppose we'll let that go
62    exit 0
63fi
64
65## get the list of files out of the project's root
66## figure out which way I'm going ..
67## use list of files to scan PREUPLOAD_FILES
68## use PREUPLOAD_FILES to scan the list of good/bad from the project root
69##
70## remember to do an exclude, so I can say
71## include/these/files/
72## EXCLUDE include/these/files/nested/
73##
74## and it should all be prefix based stuff...
75
76if [ ! -f ${REPO_ROOT}/${REPO_PATH}/${filelist_file} ] ; then
77    echo "Poorly Configured project, missing ${filelist_file} in root of project"
78    exit 1
79fi
80
81# is 1st arg a prefix of 2nd arg
82beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
83
84exclusions=""
85inclusions=""
86while read p1 p2
87do
88    # ignore comment lines in the file
89    # ignore empty lines in the file
90    if beginswith "#" "${p1}" ; then
91        # ignore this line
92        true
93    elif [ -z "${p1}" ] ; then
94        # ignore blanks
95        true
96    elif [ ${p1} = "EXCLUDE" ] ; then
97        # add to the exclusion list
98        if [ ! -z ${p2} ] ; then
99            exlusions="${exclusions} ${p2}"
100        fi
101    elif [ ${p1} = "INCLUDE" ] ; then
102        # add to the inclusion list
103        if [ ! -z ${p2} ] ; then
104            inclusions="${inclusions} ${p2}"
105        fi
106    elif [ ! -z ${p1} ] ; then
107        inclusions="${inclusions} ${p1}"
108    fi
109done < ${REPO_ROOT}/${REPO_PATH}/${filelist_file}
110
111# so we can play with array syntax
112#INCLUSIONS=( ${inclusions} )
113#EXCLUSIONS=( ${exclusions} )
114
115mainline_yes=""
116mainline_no=""
117
118# is it part of the list of mainline files/directories?
119for path in ${PREUPLOAD_FILES} ; do
120    #echo is ${path} a mainline file...
121    for aprefix in ${inclusions} .. ; do
122        #echo compare against ${aprefix} ...
123        if [ "${aprefix}" = ".." ] ; then
124            mainline_no="${mainline_no} ${path}"
125        elif beginswith ${aprefix} ${path} ; then
126            mainline_yes="${mainline_yes} ${path}"
127            break       # on to next uploaded file
128        fi
129    done
130done
131
132# TODO: audit the yes list to see if some should be moved to the no list
133
134# 3 situations
135# -- everything is on mainline (mainline_yes non-empty, other empty)
136# -- some is mainline, some is not (files_* both non-empty)
137# -- none is mainline   (mainline_yes empty, other non_empty
138# -- both empty only happens if PREUPLOAD_FILES is empty, covered above
139
140if [ -z "${mainline_yes}" ] ; then
141    # no mainline files, everything else is non-mainline, let it go
142    exit 0
143fi
144
145result=0
146if [ ! -z "${mainline_no}" ] ; then
147        # mixed bag, suggest (not insist) that developer split them.
148        result=1
149        cat - <<EOF
150This CL contains files contains both mainline and non-mainline files.  Consider separating
151them into separate CLs. It may also be appropriate to update the list of mainline
152files in ${RED}${REPO_ROOT}/${filelist_file}${NORMAL}.
153
154EOF
155        echo "===== Mainline files ====="
156        echo -e ${RED}
157        echo ${mainline_yes} | sed -e 's/ /
158/g'
159        echo -e ${NORMAL}
160
161        echo "===== Non-Mainline files ====="
162        echo -e ${RED}
163        echo ${mainline_no} | sed -e 's/ /
164/g'
165        echo -e ${NORMAL}
166
167fi
168
169if [ "${current}" != "${DEV_BRANCH}" ] ; then
170    # Change is not in the desired mainline dev branch
171    result=1
172
173    #echo -e "${RED}"
174    cat - <<EOF
175
176You are uploading repo  ${RED}${REPO_PATH}${NORMAL} to branch ${RED}${current}${NORMAL}. 
177The source of truth for ${RED}${REPO_PATH}${NORMAL} is branch ${RED}${DEV_BRANCH}${NORMAL}. 
178
179Please upload this change to branch ${RED}${DEV_BRANCH}${NORMAL} unless one or more of
180the following apply:
181- this is a security bug prohibited from disclosure before the next dessert release.
182  (moderate security bugs fall into this category).
183- this is new functionality prohibitied from disclosure before the next dessert release.
184EOF
185    #echo -e "${NORMAL}"
186
187fi
188
189## since stdout is buffered in a way that complicates the below, we're just going
190## to tell the user what they can do to get around this check instead of asking them
191## as part of this run of the command.
192
193if [ ${result} != 0 ] ; then
194    cat - <<EOF
195
196If you are sure you want to proceed uploading to branch ${RED}${current}${NORMAL},
197re-run your repo upload command with the '--no-verify' option
198
199EOF
200fi
201exit ${result}
202
203