#!/bin/bash
g() { git "$@"; }
+err() { printf '%b\n' "$*" 1>&2; exit 1; }
+vr() { echo "$@"; "$@"; }
case $1 in
-""|-*) ;;
-*)
- cmd=$1
+-x) set -x; shift;;
+esac
+
+case $1 in
+rb-all)
+ cmd=rebase
shift
- acmd=$(git config --get "alias.${cmd}")
+ set -- all "$@"
;;
esac
+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
+}
+
case ${acmd:-${cmd}} in
rebase)
if [[ $1 == "all" ]] ; then
shift
if [[ $# -eq 0 ]] ; then
- exec r forall -c 'r rb all .'
+ eval "$(bash-colors --env | sed 's:^:export :')"
+ root=$(repo_root)
+ mj_init
+ while read -a line ; do
+ dir=${line[0]}
+ proj=${line[2]}
+ if ! cd "${root}/${dir}" ; then
+ echo "bad ${proj}"
+ continue
+ fi
+ (
+ out=$(r rb-all . 2>&1)
+ if [[ -n ${out} ]] ; then
+ head=$(printf "%-40s" "${proj}")
+ echo "${out}" | sed "s:^:${head} :"
+ fi
+ _mj_child
+ ) &
+ mj_post_child
+ done < <(r l)
+ mj_finish
+ exit 0
+ #exec r forall -p -c 'r rb all .' </dev/null
fi
- branches=$(g b | awk '
- {
- if ($0 ~ "^[*] *[(]no branch[)]") {
- next
- } else if ($1 == "*") {
- b = $NF
- } else {
- list = list $NF " "
- }
- }
- END { print list b }')
- [[ -z ${branches} ]] && exit 0
-
- eval $(bash-colors --env)
- echo "${GOOD}### ${PWD}${NORMAL}"
- for b in ${branches} ; do
- echo " ${HILITE}### $b${NORMAL}"
- g co $b || exit 1
- if ! r rb "$@" ; then
- g rb-a
- fi
- done
- exit 0
+ exec git rb-all -q
fi
;;
-sb-push)
+clean)
+ root=$(repo_root)
+ cd "${root}" || exit 1
+ mj_init
+ while read -a line ; do
+ dir=${line[0]}
+ proj=${line[2]}
+ cd "${root}/${dir}"
+ (
+ out=$(g clean "$@" 2>&1)
+ if [[ -n ${out} ]] ; then
+ echo "### ${proj}"
+ echo "${out}"
+ fi
+ _mj_child
+ ) &
+ mj_post_child
+ done < <(r l)
+ mj_finish
+ exit
+ ;;
+sb)
+ sb_cmd=$1
+ case ${sb_cmd} in
+ pull) ;;
+ push) ;;
+ f|fetch) sb_cmd="fetch" ;;
+ *) err "unknown sandbox command: $1"
+ esac
+
sync_branch="v"
- root=${PWD}
- while [[ ! -d ${root}/.repo && ${root} != "/" ]] ; do
- root=${root%/*}
- done
+ root=$(repo_root)
cd "${root}" || exit 1
if [[ ! -e .repo/sandbox-url ]] ; then
- echo "Please configure remote url base in ${root}/.repo/sandbox-url"
- exit 1
+ err "Please configure remote url base in ${root}/.repo/sandbox-url"
fi
remote=$(<.repo/sandbox-url) || exit 1
echo "pushing projects from ${root}"
- pids=()
- rlist=$(r list)
+ # ssh servers do not like it when you hammer them :)
+ # Received disconnect from 74.125.248.80: 7: Too many concurrent connections
+ # fatal: The remote end hung up unexpectedly
+ mj_init 16
+
+ rlist=$(r l)
tcnt=$(echo "${rlist}" | wc -l)
cnt=1
while read line ; do
printf '### (%*i/%i %3i%%) %s\n' \
${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj}
src="${sync_branch}"
- g l -1 ${src} >& /dev/null || src=
- ( g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null ) &
-
- # ssh servers do not like it when you hammer them :)
- # Received disconnect from 74.125.248.80: 7: Too many concurrent connections
- # fatal: The remote end hung up unexpectedly
- pids+=( $! )
- if [[ ${#pids[@]} -eq 20 ]] ; then
- wait ${pids[@]:0:3}
- pids=( ${pids[@]:3} )
- fi
- done <<<"$(r list)"
- wait
+ case ${sb_cmd} in
+ push)
+ g l -1 ${src} >& /dev/null || src=
+ mj_child g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null
+ ;;
+ pull)
+ ;;
+ fetch)
+ mj_child g fetch ${remote}/${proj} refs/sandbox/${USER}/${sync_branch}:refs/remotes/sb/${sync_branch} >/dev/null
+ ;;
+ esac
+ done < <(echo "${rlist}")
+ mj_finish
exit 0
;;
+upload)
+ args=()
+ while [[ $# -gt 0 ]] ; do
+ case $1 in
+ --re)
+ process_reviewers "$2"
+ args+=( --re "${reviewers}" )
+ shift 2
+ continue
+ ;;
+ esac
+ args+=( "$1" )
+ shift
+ done
+ set -- "${args[@]}"
+ ;;
+email)
+ email=${1:-${USER}@chromium.org}
+
+ root=$(repo_root)
+ git --git-dir="${root}/.repo/manifests.git" cfg user.email "${email}"
+ git --git-dir="${root}/.repo/repo/.git" cfg user.email "${email}"
+
+ cmd='forall'
+ set -- -c "git cfg user.email '${email}'"
+ ;;
esac
-exec repo ${acmd:-${cmd}} "$@"
+exec $(find_repo) ${acmd:-${cmd}} "$@"