]> git.wh0rd.org - home.git/blob - .bin/r
r: call rb-all directly to avoid double expansion
[home.git] / .bin / r
1 #!/bin/bash
2 g() { git "$@"; }
3 err() { printf '%b\n' "$*" 1>&2; exit 1; }
4 vr() { echo "$@"; "$@"; }
5
6 case $1 in
7 -x) set -x; shift;;
8 esac
9
10 case $1 in
11 rb-all)
12 cmd=rebase
13 shift
14 set -- all "$@"
15 ;;
16 esac
17
18 mj_init() {
19 pipe=$(mktemp)
20 rm -f "${pipe}"
21 mkfifo "${pipe}"
22 exec {ctlfd}<>"${pipe}"
23 rm -f "${pipe}"
24 jobs=0
25 jobs_max=${1:-$(getconf _NPROCESSORS_ONLN)}
26 }
27 _mj_child() { echo ${BASHPID} $? >&${ctlfd} ; }
28 mj_child() {
29 (
30 "$@"
31 _mj_child
32 ) &
33 mj_post_child
34 }
35 mj_post_child() {
36 : $(( ++jobs ))
37 if [[ ${jobs} -eq ${jobs_max} ]] ; then
38 read -r -u ${ctlfd} pid ret
39 : $(( --jobs ))
40 fi
41 }
42 mj_finish() {
43 wait
44 }
45
46 repo_root() {
47 local root=${PWD}
48 while [[ ! -d ${root}/.repo && ${root:-/} != "/" ]] ; do
49 root=${root%/*}
50 done
51 echo "${root}"
52 }
53
54 process_reviewers() {
55 local r arr=()
56 for r in ${*//,/ } ; do
57 case ${r} in
58 OWNERS*|*/OWNERS*)
59 local owners=$(
60 awk -F'@' '
61 ($2 == "chromium.org" || $2 == "google.com") {list = list "," $1}
62 END {print substr(list, 2)}
63 ' "${r}"
64 )
65 if [[ -z ${owners} ]] ; then
66 err "cannot find OWNERS list"
67 else
68 echo "Auto setting reviewers to: ${owners}"
69 fi
70 arr+=( ${owners} )
71 ;;
72 *)
73 arr+=( "${r}" )
74 ;;
75 esac
76 done
77 reviewers=$(printf '%s,' "${arr[@]}")
78 reviewers=${reviewers%,}
79 }
80
81 find_repo() {
82 # Diff projects have diff versions of repo. Find a compatible one.
83 local root=$(repo_root)
84
85 # Use the manifest repo URL.
86 local manifest_dir="${root}/.repo/manifests.git"
87 local d
88
89 # Default to the local repo if it's there.
90 local search=(
91 "${root}/.repo/repo"
92 )
93
94 case $(g --git-dir="${manifest_dir}" config remote.origin.url) in
95 *android*)
96 search+=(
97 /usr/local/src/repo
98 ~/src/repo
99 )
100 ;;
101 *chromium*|*chrome*)
102 search+=(
103 ~/depot_tools
104 ~/chromiumos/depot_tools
105 /usr/local/src/depot_tools
106 ~/src/depot_tools
107 )
108 ;;
109 esac
110 for d in "${search[@]}" ; do
111 if [[ -x ${d}/repo ]] ; then
112 echo "${d}"/repo
113 return
114 fi
115 done
116
117 # Fallback: use $PATH.
118 type -P repo
119 }
120
121 case ${acmd:-${cmd}} in
122 rebase)
123 if [[ $1 == "all" ]] ; then
124 shift
125 if [[ $# -eq 0 ]] ; then
126 eval "$(bash-colors --env | sed 's:^:export :')"
127 root=$(repo_root)
128 mj_init
129 while read -a line ; do
130 dir=${line[0]}
131 proj=${line[2]}
132 if ! cd "${root}/${dir}" ; then
133 echo "bad ${proj}"
134 continue
135 fi
136 (
137 out=$(r rb-all . 2>&1)
138 if [[ -n ${out} ]] ; then
139 head=$(printf "%-40s" "${proj}")
140 echo "${out}" | sed "s:^:${head} :"
141 fi
142 _mj_child
143 ) &
144 mj_post_child
145 done < <(r l)
146 mj_finish
147 exit 0
148 #exec r forall -p -c 'r rb all .' </dev/null
149 fi
150
151 exec git rb-all -q
152 fi
153 ;;
154 clean)
155 root=$(repo_root)
156 cd "${root}" || exit 1
157 mj_init
158 while read -a line ; do
159 dir=${line[0]}
160 proj=${line[2]}
161 cd "${root}/${dir}"
162 (
163 out=$(g clean "$@" 2>&1)
164 if [[ -n ${out} ]] ; then
165 echo "### ${proj}"
166 echo "${out}"
167 fi
168 _mj_child
169 ) &
170 mj_post_child
171 done < <(r l)
172 mj_finish
173 exit
174 ;;
175 sb)
176 sb_cmd=$1
177 case ${sb_cmd} in
178 pull) ;;
179 push) ;;
180 f|fetch) sb_cmd="fetch" ;;
181 *) err "unknown sandbox command: $1"
182 esac
183
184 sync_branch="v"
185
186 root=$(repo_root)
187 cd "${root}" || exit 1
188
189 if [[ ! -e .repo/sandbox-url ]] ; then
190 err "Please configure remote url base in ${root}/.repo/sandbox-url"
191 fi
192 remote=$(<.repo/sandbox-url) || exit 1
193
194 echo "pushing projects from ${root}"
195
196 # ssh servers do not like it when you hammer them :)
197 # Received disconnect from 74.125.248.80: 7: Too many concurrent connections
198 # fatal: The remote end hung up unexpectedly
199 mj_init 16
200
201 rlist=$(r l)
202 tcnt=$(echo "${rlist}" | wc -l)
203 cnt=1
204 while read line ; do
205 line=( ${line} )
206 path=${line[0]}
207 export GIT_DIR=${path}/.git
208 proj=${line[2]}
209 printf '### (%*i/%i %3i%%) %s\n' \
210 ${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj}
211 src="${sync_branch}"
212 case ${sb_cmd} in
213 push)
214 g l -1 ${src} >& /dev/null || src=
215 mj_child g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null
216 ;;
217 pull)
218 ;;
219 fetch)
220 mj_child g fetch ${remote}/${proj} refs/sandbox/${USER}/${sync_branch}:refs/remotes/sb/${sync_branch} >/dev/null
221 ;;
222 esac
223 done < <(echo "${rlist}")
224 mj_finish
225
226 exit 0
227 ;;
228 upload)
229 args=()
230 while [[ $# -gt 0 ]] ; do
231 case $1 in
232 --re)
233 process_reviewers "$2"
234 args+=( --re "${reviewers}" )
235 shift 2
236 continue
237 ;;
238 esac
239 args+=( "$1" )
240 shift
241 done
242 set -- "${args[@]}"
243 ;;
244 email)
245 email=${1:-${USER}@chromium.org}
246
247 root=$(repo_root)
248 git --git-dir="${root}/.repo/manifests.git" cfg user.email "${email}"
249 git --git-dir="${root}/.repo/repo/.git" cfg user.email "${email}"
250
251 cmd='forall'
252 set -- -c "git cfg user.email '${email}'"
253 ;;
254 esac
255
256 exec $(find_repo) ${acmd:-${cmd}} "$@"