]>
Commit | Line | Data |
---|---|---|
35c220c5 MF |
1 | #!/bin/bash |
2 | g() { git "$@"; } | |
8f20a215 | 3 | err() { printf '%b\n' "$*" 1>&2; exit 1; } |
e3be8889 | 4 | vr() { echo "$@"; "$@"; } |
955eb9e8 | 5 | |
24988ec0 MF |
6 | case $1 in |
7 | -x) set -x; shift;; | |
8 | esac | |
9 | ||
765bb138 MF |
10 | case $1 in |
11 | ""|-*) ;; | |
17ff99a0 MF |
12 | l) |
13 | cmd=list | |
14 | shift | |
15 | ;; | |
3c32f9b8 MF |
16 | s) |
17 | cmd=sync | |
18 | shift | |
19 | ;; | |
dfe9b9cf MF |
20 | rb-all) |
21 | cmd=rebase | |
22 | shift | |
23 | set -- all "$@" | |
24 | ;; | |
765bb138 MF |
25 | *) |
26 | cmd=$1 | |
27 | shift | |
11233988 | 28 | acmd=$(git config --get "alias.${cmd}" | sed 's: -.*::') |
765bb138 MF |
29 | ;; |
30 | esac | |
35c220c5 | 31 | |
1400d2cd MF |
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} | |
eee212f8 | 62 | while [[ ! -d ${root}/.repo && ${root:-/} != "/" ]] ; do |
1400d2cd MF |
63 | root=${root%/*} |
64 | done | |
65 | echo "${root}" | |
66 | } | |
67 | ||
003d4f70 MF |
68 | process_reviewers() { |
69 | local r arr=() | |
70 | for r in ${*//,/ } ; do | |
71 | case ${r} in | |
72 | *OWNERS) | |
f8260dbd MF |
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 | ) | |
003d4f70 MF |
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 | ||
b4533091 MF |
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 | ||
955eb9e8 | 135 | case ${acmd:-${cmd}} in |
35c220c5 MF |
136 | rebase) |
137 | if [[ $1 == "all" ]] ; then | |
e947e1df | 138 | shift |
e051dd74 | 139 | if [[ $# -eq 0 ]] ; then |
71b529c6 | 140 | eval $(bash-colors --env | sed 's:^:export :') |
1400d2cd MF |
141 | root=$(repo_root) |
142 | mj_init | |
143 | while read -a line ; do | |
144 | dir=${line[0]} | |
145 | proj=${line[2]} | |
24988ec0 MF |
146 | if ! cd "${root}/${dir}" ; then |
147 | echo "bad ${proj}" | |
148 | continue | |
149 | fi | |
1400d2cd | 150 | ( |
63717a62 | 151 | out=$(env _proj=${proj} r rb all . 2>&1) |
71b529c6 MF |
152 | if [[ -n ${out} ]] ; then |
153 | while read line ; do | |
154 | if [[ ${line} == "# "* ]] ; then | |
155 | line="${line#[#] }" | |
156 | printf '%s### %s%-40s%s: %s\n' \ | |
157 | "${BRACKET}" "${GOOD}" "${line%%:*}" "${NORMAL}" "${line#*:}" | |
158 | else | |
24988ec0 MF |
159 | printf '%s### %s%-40s%s: ERROR: %s\n' \ |
160 | "${BRACKET}" "${BAD}" "${dir}" "${NORMAL}" "${line}" | |
71b529c6 MF |
161 | fi |
162 | done < <(echo "${out}") | |
163 | fi | |
1400d2cd MF |
164 | _mj_child |
165 | ) & | |
166 | mj_post_child | |
167 | done < <(r l) | |
168 | mj_finish | |
169 | exit 0 | |
170 | #exec r forall -p -c 'r rb all .' </dev/null | |
e051dd74 MF |
171 | fi |
172 | ||
e947e1df MF |
173 | branches=$(g b | awk ' |
174 | { | |
175 | if ($0 ~ "^[*] *[(]no branch[)]") { | |
176 | next | |
24988ec0 | 177 | } else if ($0 ~ "^[*] *[(](HEAD )?detached (from|at) ") { |
35da3f7e | 178 | next |
e947e1df | 179 | } else if ($1 == "*") { |
04aedbbd | 180 | b = $2 |
e947e1df | 181 | } else { |
04aedbbd | 182 | list = list $1 " " |
e947e1df MF |
183 | } |
184 | } | |
185 | END { print list b }') | |
e051dd74 MF |
186 | [[ -z ${branches} ]] && exit 0 |
187 | ||
71b529c6 | 188 | [[ -z ${GOOD} ]] && eval $(bash-colors --env) |
b5092d38 | 189 | #echo "${GOOD}### ${PWD}${NORMAL}" |
63717a62 MF |
190 | # Skip if rebase is in progress. |
191 | if [[ -e .git/rebase-merge/interactive ]] ; then | |
71b529c6 | 192 | echo -e "# ${_proj}: ${WARN}skipping due to active rebase${NORMAL}" |
63717a62 MF |
193 | exit 1 |
194 | fi | |
e947e1df | 195 | for b in ${branches} ; do |
1400d2cd | 196 | #echo " ${HILITE}### $b${NORMAL}" |
17ff99a0 | 197 | g co -q $b || exit 1 |
1400d2cd | 198 | if ! r rb -q "$@" ; then |
e051dd74 MF |
199 | g rb-a |
200 | fi | |
35c220c5 MF |
201 | done |
202 | exit 0 | |
203 | fi | |
204 | ;; | |
7ba91d1a MF |
205 | clean) |
206 | root=$(repo_root) | |
207 | cd "${root}" || exit 1 | |
208 | mj_init | |
209 | while read -a line ; do | |
210 | dir=${line[0]} | |
211 | proj=${line[2]} | |
212 | cd "${root}/${dir}" | |
213 | ( | |
214 | out=$(g clean "$@" 2>&1) | |
215 | if [[ -n ${out} ]] ; then | |
216 | echo "### ${proj}" | |
217 | echo "${out}" | |
218 | fi | |
219 | _mj_child | |
220 | ) & | |
221 | mj_post_child | |
222 | done < <(r l) | |
223 | mj_finish | |
224 | exit | |
225 | ;; | |
bf81de9c MF |
226 | sb) |
227 | sb_cmd=$1 | |
228 | case ${sb_cmd} in | |
229 | pull) ;; | |
230 | push) ;; | |
231 | f|fetch) sb_cmd="fetch" ;; | |
232 | *) err "unknown sandbox command: $1" | |
233 | esac | |
234 | ||
955eb9e8 MF |
235 | sync_branch="v" |
236 | ||
1400d2cd | 237 | root=$(repo_root) |
955eb9e8 MF |
238 | cd "${root}" || exit 1 |
239 | ||
240 | if [[ ! -e .repo/sandbox-url ]] ; then | |
8f20a215 | 241 | err "Please configure remote url base in ${root}/.repo/sandbox-url" |
955eb9e8 MF |
242 | fi |
243 | remote=$(<.repo/sandbox-url) || exit 1 | |
244 | ||
245 | echo "pushing projects from ${root}" | |
246 | ||
1400d2cd MF |
247 | # ssh servers do not like it when you hammer them :) |
248 | # Received disconnect from 74.125.248.80: 7: Too many concurrent connections | |
249 | # fatal: The remote end hung up unexpectedly | |
250 | mj_init 16 | |
b5092d38 | 251 | |
7ba91d1a | 252 | rlist=$(r l) |
955eb9e8 MF |
253 | tcnt=$(echo "${rlist}" | wc -l) |
254 | cnt=1 | |
255 | while read line ; do | |
256 | line=( ${line} ) | |
257 | path=${line[0]} | |
258 | export GIT_DIR=${path}/.git | |
259 | proj=${line[2]} | |
260 | printf '### (%*i/%i %3i%%) %s\n' \ | |
261 | ${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj} | |
262 | src="${sync_branch}" | |
bf81de9c MF |
263 | case ${sb_cmd} in |
264 | push) | |
265 | g l -1 ${src} >& /dev/null || src= | |
266 | mj_child g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null | |
267 | ;; | |
268 | pull) | |
269 | ;; | |
270 | fetch) | |
271 | mj_child g fetch ${remote}/${proj} refs/sandbox/${USER}/${sync_branch}:refs/remotes/sb/${sync_branch} >/dev/null | |
272 | ;; | |
273 | esac | |
7ba91d1a | 274 | done < <(echo "${rlist}") |
1400d2cd | 275 | mj_finish |
955eb9e8 MF |
276 | |
277 | exit 0 | |
278 | ;; | |
17ff99a0 MF |
279 | g-push) |
280 | # For the times when repo is being stupid, push directly to gerrit myself. | |
e3be8889 MF |
281 | if ! branch=$(g symbolic-ref -q HEAD) ; then |
282 | err "could not figure out active branch" | |
283 | fi | |
284 | branch=${branch#refs/heads/} | |
285 | if ! remote_branch=$(g cfg --get "branch.${branch}.merge") ; then | |
8e41af94 MF |
286 | echo "could not figure out remote branch; using ${branch}" |
287 | remote_branch=${branch} | |
e3be8889 | 288 | fi |
8a21037a MF |
289 | if ! remote=$(g cfg --get "branch.${branch}.remote") ; then |
290 | for remote in cros-internal cros origin ; do | |
291 | g cfg --get "remote.${remote}.url" >/dev/null && break | |
292 | done | |
293 | fi | |
d3bdab7a | 294 | review=$(g cfg --get "remote.${remote}.review") |
e3be8889 MF |
295 | remote_branch=${remote_branch#refs/heads/} |
296 | ||
eacd89fe | 297 | git_args=() |
36fb4856 MF |
298 | reviewers="" |
299 | while [[ $# -gt 0 ]] ; do | |
300 | case $1 in | |
301 | --re) | |
003d4f70 | 302 | process_reviewers "$2" |
36fb4856 MF |
303 | shift |
304 | ;; | |
d8f836b5 | 305 | -n|--dry-run|--draft) |
eacd89fe MF |
306 | git_args+=( $1 ) |
307 | ;; | |
36fb4856 MF |
308 | *) |
309 | err "unknown option: $1" | |
310 | ;; | |
311 | esac | |
312 | shift | |
313 | done | |
314 | ||
d3bdab7a | 315 | ref_spec="${branch}:refs/for/${remote_branch}" |
36fb4856 MF |
316 | if [[ -n ${reviewers} ]] ; then |
317 | reviewers=( ${reviewers//,/ } ) | |
d3bdab7a MF |
318 | if [[ ${review} != ssh://* ]] ; then |
319 | gob_args=$(printf 'r=%s,' "${reviewers[@]}") | |
320 | ref_spec+="%${gob_args%,}" | |
321 | else | |
322 | git_args+=( "--receive-pack=git receive-pack ${reviewers[*]/#/--reviewer=}" ) | |
323 | fi | |
36fb4856 MF |
324 | fi |
325 | ||
d3bdab7a | 326 | vr git push "${git_args[@]}" ${remote} ${ref_spec} && exit |
e3be8889 | 327 | err "could not figure out remote to push to" |
17ff99a0 | 328 | ;; |
3c32f9b8 | 329 | sync) |
35da3f7e | 330 | set -- -j16 -c "$@" |
7dae893b | 331 | ;; |
8f20a215 MF |
332 | upload) |
333 | args=() | |
334 | while [[ $# -gt 0 ]] ; do | |
335 | case $1 in | |
336 | --re) | |
003d4f70 MF |
337 | process_reviewers "$2" |
338 | args+=( --re "${reviewers}" ) | |
339 | shift 2 | |
340 | continue | |
8f20a215 MF |
341 | ;; |
342 | esac | |
343 | args+=( "$1" ) | |
344 | shift | |
345 | done | |
346 | set -- "${args[@]}" | |
347 | ;; | |
35da3f7e MF |
348 | email) |
349 | email=${1:-${USER}@chromium.org} | |
b4533091 MF |
350 | |
351 | root=$(repo_root) | |
352 | git --git-dir="${root}/.repo/manifests.git" cfg user.email "${email}" | |
353 | git --git-dir="${root}/.repo/repo/.git" cfg user.email "${email}" | |
354 | ||
35da3f7e | 355 | cmd='forall' |
b4533091 | 356 | set -- -c "git cfg user.email '${email}'" |
35da3f7e | 357 | ;; |
35c220c5 MF |
358 | esac |
359 | ||
b4533091 | 360 | exec python2 $(find_repo) ${acmd:-${cmd}} "$@" |