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