+mj_init() {
+ pipe=$(mktemp)
+ rm -f "${pipe}"
+ mkfifo "${pipe}"
+ exec {ctlfd}<>"${pipe}"
+ rm -f "${pipe}"
+ jobs=0
+ jobs_max=${1:-$(getconf _NPROCESSORS_ONLN)}
+}
+_mj_child() { echo ${BASHPID} $? >&${ctlfd} ; }
+mj_child() {
+ (
+ "$@"
+ _mj_child
+ ) &
+ mj_post_child
+}
+mj_post_child() {
+ : $(( ++jobs ))
+ if [[ ${jobs} -eq ${jobs_max} ]] ; then
+ read -r -u ${ctlfd} pid ret
+ : $(( --jobs ))
+ fi
+}
+mj_finish() {
+ wait
+}
+
+repo_root() {
+ local root=${PWD}
+ while [[ ! -d ${root}/.repo && ${root:-/} != "/" ]] ; do
+ root=${root%/*}
+ done
+ echo "${root}"
+}
+
+process_reviewers() {
+ local r arr=()
+ for r in ${*//,/ } ; do
+ case ${r} in
+ OWNERS*|*/OWNERS*)
+ local owners=$(
+ awk -F'@' '
+ ($2 == "chromium.org" || $2 == "google.com") {list = list "," $1}
+ END {print substr(list, 2)}
+ ' "${r}"
+ )
+ if [[ -z ${owners} ]] ; then
+ err "cannot find OWNERS list"
+ else
+ echo "Auto setting reviewers to: ${owners}"
+ fi
+ arr+=( ${owners} )
+ ;;
+ *)
+ arr+=( "${r}" )
+ ;;
+ esac
+ done
+ reviewers=$(printf '%s,' "${arr[@]}")
+ reviewers=${reviewers%,}
+}
+
+find_repo() {
+ # Diff projects have diff versions of repo. Find a compatible one.
+ local root=$(repo_root)
+
+ # Use the manifest repo URL.
+ local manifest_dir="${root}/.repo/manifests.git"
+ local d
+
+ # Default to the local repo if it's there.
+ local search=(
+ "${root}/.repo/repo"
+ )
+
+ case $(g --git-dir="${manifest_dir}" config remote.origin.url) in
+ *android*)
+ search+=(
+ /usr/local/src/repo
+ ~/src/repo
+ )
+ ;;
+ *chromium*|*chrome*)
+ search+=(
+ ~/depot_tools
+ ~/chromiumos/depot_tools
+ /usr/local/src/depot_tools
+ ~/src/depot_tools
+ )
+ ;;
+ esac
+ for d in "${search[@]}" ; do
+ if [[ -x ${d}/repo ]] ; then
+ echo "${d}"/repo
+ return
+ fi
+ done
+
+ # Fallback: use $PATH.
+ type -P repo
+}
+