]>
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 | |
765bb138 MF |
6 | case $1 in |
7 | ""|-*) ;; | |
17ff99a0 MF |
8 | l) |
9 | cmd=list | |
10 | shift | |
11 | ;; | |
3c32f9b8 MF |
12 | s) |
13 | cmd=sync | |
14 | shift | |
15 | ;; | |
765bb138 MF |
16 | *) |
17 | cmd=$1 | |
18 | shift | |
11233988 | 19 | acmd=$(git config --get "alias.${cmd}" | sed 's: -.*::') |
765bb138 MF |
20 | ;; |
21 | esac | |
35c220c5 | 22 | |
1400d2cd MF |
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 | ||
003d4f70 MF |
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 | ||
955eb9e8 | 81 | case ${acmd:-${cmd}} in |
35c220c5 MF |
82 | rebase) |
83 | if [[ $1 == "all" ]] ; then | |
e947e1df | 84 | shift |
e051dd74 | 85 | if [[ $# -eq 0 ]] ; then |
71b529c6 | 86 | eval $(bash-colors --env | sed 's:^:export :') |
1400d2cd MF |
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 | ( | |
63717a62 | 94 | out=$(env _proj=${proj} r rb all . 2>&1) |
71b529c6 MF |
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 | |
1400d2cd MF |
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 | |
e051dd74 MF |
113 | fi |
114 | ||
e947e1df MF |
115 | branches=$(g b | awk ' |
116 | { | |
117 | if ($0 ~ "^[*] *[(]no branch[)]") { | |
118 | next | |
35da3f7e MF |
119 | } else if ($0 ~ "^[*] *[(]detached from ") { |
120 | next | |
e947e1df | 121 | } else if ($1 == "*") { |
04aedbbd | 122 | b = $2 |
e947e1df | 123 | } else { |
04aedbbd | 124 | list = list $1 " " |
e947e1df MF |
125 | } |
126 | } | |
127 | END { print list b }') | |
e051dd74 MF |
128 | [[ -z ${branches} ]] && exit 0 |
129 | ||
71b529c6 | 130 | [[ -z ${GOOD} ]] && eval $(bash-colors --env) |
b5092d38 | 131 | #echo "${GOOD}### ${PWD}${NORMAL}" |
63717a62 MF |
132 | # Skip if rebase is in progress. |
133 | if [[ -e .git/rebase-merge/interactive ]] ; then | |
71b529c6 | 134 | echo -e "# ${_proj}: ${WARN}skipping due to active rebase${NORMAL}" |
63717a62 MF |
135 | exit 1 |
136 | fi | |
e947e1df | 137 | for b in ${branches} ; do |
1400d2cd | 138 | #echo " ${HILITE}### $b${NORMAL}" |
17ff99a0 | 139 | g co -q $b || exit 1 |
1400d2cd | 140 | if ! r rb -q "$@" ; then |
e051dd74 MF |
141 | g rb-a |
142 | fi | |
35c220c5 MF |
143 | done |
144 | exit 0 | |
145 | fi | |
146 | ;; | |
7ba91d1a MF |
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 | ;; | |
bf81de9c MF |
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 | ||
955eb9e8 MF |
177 | sync_branch="v" |
178 | ||
1400d2cd | 179 | root=$(repo_root) |
955eb9e8 MF |
180 | cd "${root}" || exit 1 |
181 | ||
182 | if [[ ! -e .repo/sandbox-url ]] ; then | |
8f20a215 | 183 | err "Please configure remote url base in ${root}/.repo/sandbox-url" |
955eb9e8 MF |
184 | fi |
185 | remote=$(<.repo/sandbox-url) || exit 1 | |
186 | ||
187 | echo "pushing projects from ${root}" | |
188 | ||
1400d2cd MF |
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 | |
b5092d38 | 193 | |
7ba91d1a | 194 | rlist=$(r l) |
955eb9e8 MF |
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}" | |
bf81de9c MF |
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 | |
7ba91d1a | 216 | done < <(echo "${rlist}") |
1400d2cd | 217 | mj_finish |
955eb9e8 MF |
218 | |
219 | exit 0 | |
220 | ;; | |
17ff99a0 MF |
221 | g-push) |
222 | # For the times when repo is being stupid, push directly to gerrit myself. | |
e3be8889 MF |
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 | |
8e41af94 MF |
228 | echo "could not figure out remote branch; using ${branch}" |
229 | remote_branch=${branch} | |
e3be8889 | 230 | fi |
8a21037a MF |
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 | |
d3bdab7a | 236 | review=$(g cfg --get "remote.${remote}.review") |
e3be8889 MF |
237 | remote_branch=${remote_branch#refs/heads/} |
238 | ||
eacd89fe | 239 | git_args=() |
36fb4856 MF |
240 | reviewers="" |
241 | while [[ $# -gt 0 ]] ; do | |
242 | case $1 in | |
243 | --re) | |
003d4f70 | 244 | process_reviewers "$2" |
36fb4856 MF |
245 | shift |
246 | ;; | |
d8f836b5 | 247 | -n|--dry-run|--draft) |
eacd89fe MF |
248 | git_args+=( $1 ) |
249 | ;; | |
36fb4856 MF |
250 | *) |
251 | err "unknown option: $1" | |
252 | ;; | |
253 | esac | |
254 | shift | |
255 | done | |
256 | ||
d3bdab7a | 257 | ref_spec="${branch}:refs/for/${remote_branch}" |
36fb4856 MF |
258 | if [[ -n ${reviewers} ]] ; then |
259 | reviewers=( ${reviewers//,/ } ) | |
d3bdab7a MF |
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 | |
36fb4856 MF |
266 | fi |
267 | ||
d3bdab7a | 268 | vr git push "${git_args[@]}" ${remote} ${ref_spec} && exit |
e3be8889 | 269 | err "could not figure out remote to push to" |
17ff99a0 | 270 | ;; |
3c32f9b8 | 271 | sync) |
35da3f7e | 272 | set -- -j16 -c "$@" |
7dae893b | 273 | ;; |
8f20a215 MF |
274 | upload) |
275 | args=() | |
276 | while [[ $# -gt 0 ]] ; do | |
277 | case $1 in | |
278 | --re) | |
003d4f70 MF |
279 | process_reviewers "$2" |
280 | args+=( --re "${reviewers}" ) | |
281 | shift 2 | |
282 | continue | |
8f20a215 MF |
283 | ;; |
284 | esac | |
285 | args+=( "$1" ) | |
286 | shift | |
287 | done | |
288 | set -- "${args[@]}" | |
289 | ;; | |
35da3f7e MF |
290 | email) |
291 | email=${1:-${USER}@chromium.org} | |
292 | cmd='forall' | |
293 | set -- -c "git cfg user.email ${email}" | |
294 | ;; | |
35c220c5 MF |
295 | esac |
296 | ||
297 | exec repo ${acmd:-${cmd}} "$@" |