]> git.wh0rd.org - home.git/blob - .bin/r
r: add OWNERS parsing to all upload commands
[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-push)
169 sync_branch="v"
170
171 root=$(repo_root)
172 cd "${root}" || exit 1
173
174 if [[ ! -e .repo/sandbox-url ]] ; then
175 err "Please configure remote url base in ${root}/.repo/sandbox-url"
176 fi
177 remote=$(<.repo/sandbox-url) || exit 1
178
179 echo "pushing projects from ${root}"
180
181 # ssh servers do not like it when you hammer them :)
182 # Received disconnect from 74.125.248.80: 7: Too many concurrent connections
183 # fatal: The remote end hung up unexpectedly
184 mj_init 16
185
186 rlist=$(r l)
187 tcnt=$(echo "${rlist}" | wc -l)
188 cnt=1
189 while read line ; do
190 line=( ${line} )
191 path=${line[0]}
192 export GIT_DIR=${path}/.git
193 proj=${line[2]}
194 printf '### (%*i/%i %3i%%) %s\n' \
195 ${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj}
196 src="${sync_branch}"
197 g l -1 ${src} >& /dev/null || src=
198 mj_child g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null
199 done < <(echo "${rlist}")
200 mj_finish
201
202 exit 0
203 ;;
204 g-push)
205 # For the times when repo is being stupid, push directly to gerrit myself.
206 if ! branch=$(g symbolic-ref -q HEAD) ; then
207 err "could not figure out active branch"
208 fi
209 branch=${branch#refs/heads/}
210 if ! remote_branch=$(g cfg --get "branch.${branch}.merge") ; then
211 echo "could not figure out remote branch; using ${branch}"
212 remote_branch=${branch}
213 fi
214 if ! remote=$(g cfg --get "branch.${branch}.remote") ; then
215 for remote in cros-internal cros origin ; do
216 g cfg --get "remote.${remote}.url" >/dev/null && break
217 done
218 fi
219 review=$(g cfg --get "remote.${remote}.review")
220 remote_branch=${remote_branch#refs/heads/}
221
222 git_args=()
223 reviewers=""
224 while [[ $# -gt 0 ]] ; do
225 case $1 in
226 --re)
227 process_reviewers "$2"
228 shift
229 ;;
230 -n|--dry-run|--draft)
231 git_args+=( $1 )
232 ;;
233 *)
234 err "unknown option: $1"
235 ;;
236 esac
237 shift
238 done
239
240 ref_spec="${branch}:refs/for/${remote_branch}"
241 if [[ -n ${reviewers} ]] ; then
242 reviewers=( ${reviewers//,/ } )
243 if [[ ${review} != ssh://* ]] ; then
244 gob_args=$(printf 'r=%s,' "${reviewers[@]}")
245 ref_spec+="%${gob_args%,}"
246 else
247 git_args+=( "--receive-pack=git receive-pack ${reviewers[*]/#/--reviewer=}" )
248 fi
249 fi
250
251 vr git push "${git_args[@]}" ${remote} ${ref_spec} && exit
252 err "could not figure out remote to push to"
253 ;;
254 sync)
255 set -- -j16 -c "$@"
256 ;;
257 upload)
258 args=()
259 while [[ $# -gt 0 ]] ; do
260 case $1 in
261 --re)
262 process_reviewers "$2"
263 args+=( --re "${reviewers}" )
264 shift 2
265 continue
266 ;;
267 esac
268 args+=( "$1" )
269 shift
270 done
271 set -- "${args[@]}"
272 ;;
273 email)
274 email=${1:-${USER}@chromium.org}
275 cmd='forall'
276 set -- -c "git cfg user.email ${email}"
277 ;;
278 esac
279
280 exec repo ${acmd:-${cmd}} "$@"