]> git.wh0rd.org - home.git/blob - .bin/r
r: handle more sandbox options
[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 ""|-*) ;;
8 l)
9 cmd=list
10 shift
11 ;;
12 s)
13 cmd=sync
14 shift
15 ;;
16 *)
17 cmd=$1
18 shift
19 acmd=$(git config --get "alias.${cmd}" | sed 's: -.*::')
20 ;;
21 esac
22
23 mj_init() {
24 pipe=$(mktemp)
25 rm -f "${pipe}"
26 mkfifo "${pipe}"
27 exec {ctlfd}<>"${pipe}"
28 rm -f "${pipe}"
29 jobs=0
30 jobs_max=${1:-$(getconf _NPROCESSORS_ONLN)}
31 }
32 _mj_child() { echo ${BASHPID} $? >&${ctlfd} ; }
33 mj_child() {
34 (
35 "$@"
36 _mj_child
37 ) &
38 mj_post_child
39 }
40 mj_post_child() {
41 : $(( ++jobs ))
42 if [[ ${jobs} -eq ${jobs_max} ]] ; then
43 read -r -u ${ctlfd} pid ret
44 : $(( --jobs ))
45 fi
46 }
47 mj_finish() {
48 wait
49 }
50
51 repo_root() {
52 local root=${PWD}
53 while [[ ! -d ${root}/.repo && ${root} != "/" ]] ; do
54 root=${root%/*}
55 done
56 echo "${root}"
57 }
58
59 process_reviewers() {
60 local r arr=()
61 for r in ${*//,/ } ; do
62 case ${r} in
63 *OWNERS)
64 local owners=$(awk -F'@' '{list = list "," $1} END {print substr(list, 2)}' "${r}")
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 case ${acmd:-${cmd}} in
82 rebase)
83 if [[ $1 == "all" ]] ; then
84 shift
85 if [[ $# -eq 0 ]] ; then
86 eval $(bash-colors --env | sed 's:^:export :')
87 root=$(repo_root)
88 mj_init
89 while read -a line ; do
90 dir=${line[0]}
91 proj=${line[2]}
92 cd "${root}/${dir}"
93 (
94 out=$(env _proj=${proj} r rb all . 2>&1)
95 if [[ -n ${out} ]] ; then
96 while read line ; do
97 if [[ ${line} == "# "* ]] ; then
98 line="${line#[#] }"
99 printf '%s### %s%-40s%s: %s\n' \
100 "${BRACKET}" "${GOOD}" "${line%%:*}" "${NORMAL}" "${line#*:}"
101 else
102 echo "${line}"
103 fi
104 done < <(echo "${out}")
105 fi
106 _mj_child
107 ) &
108 mj_post_child
109 done < <(r l)
110 mj_finish
111 exit 0
112 #exec r forall -p -c 'r rb all .' </dev/null
113 fi
114
115 branches=$(g b | awk '
116 {
117 if ($0 ~ "^[*] *[(]no branch[)]") {
118 next
119 } else if ($0 ~ "^[*] *[(]detached from ") {
120 next
121 } else if ($1 == "*") {
122 b = $2
123 } else {
124 list = list $1 " "
125 }
126 }
127 END { print list b }')
128 [[ -z ${branches} ]] && exit 0
129
130 [[ -z ${GOOD} ]] && eval $(bash-colors --env)
131 #echo "${GOOD}### ${PWD}${NORMAL}"
132 # Skip if rebase is in progress.
133 if [[ -e .git/rebase-merge/interactive ]] ; then
134 echo -e "# ${_proj}: ${WARN}skipping due to active rebase${NORMAL}"
135 exit 1
136 fi
137 for b in ${branches} ; do
138 #echo " ${HILITE}### $b${NORMAL}"
139 g co -q $b || exit 1
140 if ! r rb -q "$@" ; then
141 g rb-a
142 fi
143 done
144 exit 0
145 fi
146 ;;
147 clean)
148 root=$(repo_root)
149 cd "${root}" || exit 1
150 mj_init
151 while read -a line ; do
152 dir=${line[0]}
153 proj=${line[2]}
154 cd "${root}/${dir}"
155 (
156 out=$(g clean "$@" 2>&1)
157 if [[ -n ${out} ]] ; then
158 echo "### ${proj}"
159 echo "${out}"
160 fi
161 _mj_child
162 ) &
163 mj_post_child
164 done < <(r l)
165 mj_finish
166 exit
167 ;;
168 sb)
169 sb_cmd=$1
170 case ${sb_cmd} in
171 pull) ;;
172 push) ;;
173 f|fetch) sb_cmd="fetch" ;;
174 *) err "unknown sandbox command: $1"
175 esac
176
177 sync_branch="v"
178
179 root=$(repo_root)
180 cd "${root}" || exit 1
181
182 if [[ ! -e .repo/sandbox-url ]] ; then
183 err "Please configure remote url base in ${root}/.repo/sandbox-url"
184 fi
185 remote=$(<.repo/sandbox-url) || exit 1
186
187 echo "pushing projects from ${root}"
188
189 # ssh servers do not like it when you hammer them :)
190 # Received disconnect from 74.125.248.80: 7: Too many concurrent connections
191 # fatal: The remote end hung up unexpectedly
192 mj_init 16
193
194 rlist=$(r l)
195 tcnt=$(echo "${rlist}" | wc -l)
196 cnt=1
197 while read line ; do
198 line=( ${line} )
199 path=${line[0]}
200 export GIT_DIR=${path}/.git
201 proj=${line[2]}
202 printf '### (%*i/%i %3i%%) %s\n' \
203 ${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj}
204 src="${sync_branch}"
205 case ${sb_cmd} in
206 push)
207 g l -1 ${src} >& /dev/null || src=
208 mj_child g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null
209 ;;
210 pull)
211 ;;
212 fetch)
213 mj_child g fetch ${remote}/${proj} refs/sandbox/${USER}/${sync_branch}:refs/remotes/sb/${sync_branch} >/dev/null
214 ;;
215 esac
216 done < <(echo "${rlist}")
217 mj_finish
218
219 exit 0
220 ;;
221 g-push)
222 # For the times when repo is being stupid, push directly to gerrit myself.
223 if ! branch=$(g symbolic-ref -q HEAD) ; then
224 err "could not figure out active branch"
225 fi
226 branch=${branch#refs/heads/}
227 if ! remote_branch=$(g cfg --get "branch.${branch}.merge") ; then
228 echo "could not figure out remote branch; using ${branch}"
229 remote_branch=${branch}
230 fi
231 if ! remote=$(g cfg --get "branch.${branch}.remote") ; then
232 for remote in cros-internal cros origin ; do
233 g cfg --get "remote.${remote}.url" >/dev/null && break
234 done
235 fi
236 review=$(g cfg --get "remote.${remote}.review")
237 remote_branch=${remote_branch#refs/heads/}
238
239 git_args=()
240 reviewers=""
241 while [[ $# -gt 0 ]] ; do
242 case $1 in
243 --re)
244 process_reviewers "$2"
245 shift
246 ;;
247 -n|--dry-run|--draft)
248 git_args+=( $1 )
249 ;;
250 *)
251 err "unknown option: $1"
252 ;;
253 esac
254 shift
255 done
256
257 ref_spec="${branch}:refs/for/${remote_branch}"
258 if [[ -n ${reviewers} ]] ; then
259 reviewers=( ${reviewers//,/ } )
260 if [[ ${review} != ssh://* ]] ; then
261 gob_args=$(printf 'r=%s,' "${reviewers[@]}")
262 ref_spec+="%${gob_args%,}"
263 else
264 git_args+=( "--receive-pack=git receive-pack ${reviewers[*]/#/--reviewer=}" )
265 fi
266 fi
267
268 vr git push "${git_args[@]}" ${remote} ${ref_spec} && exit
269 err "could not figure out remote to push to"
270 ;;
271 sync)
272 set -- -j16 -c "$@"
273 ;;
274 upload)
275 args=()
276 while [[ $# -gt 0 ]] ; do
277 case $1 in
278 --re)
279 process_reviewers "$2"
280 args+=( --re "${reviewers}" )
281 shift 2
282 continue
283 ;;
284 esac
285 args+=( "$1" )
286 shift
287 done
288 set -- "${args[@]}"
289 ;;
290 email)
291 email=${1:-${USER}@chromium.org}
292 cmd='forall'
293 set -- -c "git cfg user.email ${email}"
294 ;;
295 esac
296
297 exec repo ${acmd:-${cmd}} "$@"