#!/bin/bash g() { git "$@"; } err() { printf '%b\n' "$*" 1>&2; exit 1; } vr() { echo "$@"; "$@"; } case $1 in ""|-*) ;; l) cmd=list shift ;; s) cmd=sync shift ;; *) cmd=$1 shift acmd=$(git config --get "alias.${cmd}" | sed 's: -.*::') ;; 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}" } case ${acmd:-${cmd}} in rebase) if [[ $1 == "all" ]] ; then shift if [[ $# -eq 0 ]] ; then eval $(bash-colors --env | sed 's:^:export :') root=$(repo_root) mj_init while read -a line ; do dir=${line[0]} proj=${line[2]} cd "${root}/${dir}" ( out=$(env _proj=${proj} r rb all . 2>&1) if [[ -n ${out} ]] ; then while read line ; do if [[ ${line} == "# "* ]] ; then line="${line#[#] }" printf '%s### %s%-40s%s: %s\n' \ "${BRACKET}" "${GOOD}" "${line%%:*}" "${NORMAL}" "${line#*:}" else echo "${line}" fi done < <(echo "${out}") fi _mj_child ) & mj_post_child done < <(r l) mj_finish exit 0 #exec r forall -p -c 'r rb all .' & /dev/null || src= mj_child g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null done < <(r list) mj_finish exit 0 ;; g-push) # For the times when repo is being stupid, push directly to gerrit myself. if ! branch=$(g symbolic-ref -q HEAD) ; then err "could not figure out active branch" fi branch=${branch#refs/heads/} if ! remote_branch=$(g cfg --get "branch.${branch}.merge") ; then err "could not figure out remote branch" fi remote_branch=${remote_branch#refs/heads/} git_args=() reviewers="" while [[ $# -gt 0 ]] ; do case $1 in --re) reviewers=$2 shift ;; -n|--dry-run) git_args+=( $1 ) ;; *) err "unknown option: $1" ;; esac shift done if [[ -n ${reviewers} ]] ; then reviewers=( ${reviewers//,/ } ) git_args+=( "--receive-pack=git receive-pack ${reviewers[*]/#/--reviewer=}" ) fi for remote in cros-internal cros origin ; do if g cfg --get "remote.${remote}.url" >/dev/null ; then vr git push "${git_args[@]}" ${remote} ${branch}:refs/for/${remote_branch} exit $? fi done err "could not figure out remote to push to" ;; sync) set -- -j16 "$@" ;; upload) args=() while [[ $# -gt 0 ]] ; do case $1 in --re) if [[ $2 == *"OWNERS" ]] ; then owners=$(awk -F'@' '{list = list "," $1} END {print substr(list, 2)}' "$2") if [[ -z ${owners} ]] ; then err "cannot find OWNERS list" else echo "Auto setting reviewers to: ${owners}" fi args+=( --re "${owners}" ) shift 2 continue fi ;; esac args+=( "$1" ) shift done set -- "${args[@]}" ;; esac exec repo ${acmd:-${cmd}} "$@"