]> git.wh0rd.org - home.git/blob - .bin/r
parallelize rebasing of entire repo
[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 case ${acmd:-${cmd}} in
60 rebase)
61 if [[ $1 == "all" ]] ; then
62 shift
63 if [[ $# -eq 0 ]] ; then
64 root=$(repo_root)
65 mj_init
66 while read -a line ; do
67 dir=${line[0]}
68 proj=${line[2]}
69 cd "${root}/${dir}"
70 (
71 out=$(r rb all . 2>&1)
72 [[ -n ${out} ]] && echo "${out}"
73 _mj_child
74 ) &
75 mj_post_child
76 done < <(r l)
77 mj_finish
78 exit 0
79 #exec r forall -p -c 'r rb all .' </dev/null
80 fi
81
82 branches=$(g b | awk '
83 {
84 if ($0 ~ "^[*] *[(]no branch[)]") {
85 next
86 } else if ($1 == "*") {
87 b = $2
88 } else {
89 list = list $1 " "
90 }
91 }
92 END { print list b }')
93 [[ -z ${branches} ]] && exit 0
94
95 eval $(bash-colors --env)
96 #echo "${GOOD}### ${PWD}${NORMAL}"
97 for b in ${branches} ; do
98 #echo " ${HILITE}### $b${NORMAL}"
99 g co -q $b || exit 1
100 if ! r rb -q "$@" ; then
101 g rb-a
102 fi
103 done
104 exit 0
105 fi
106 ;;
107 sb-push)
108 sync_branch="v"
109
110 root=$(repo_root)
111 cd "${root}" || exit 1
112
113 if [[ ! -e .repo/sandbox-url ]] ; then
114 err "Please configure remote url base in ${root}/.repo/sandbox-url"
115 fi
116 remote=$(<.repo/sandbox-url) || exit 1
117
118 echo "pushing projects from ${root}"
119
120 # ssh servers do not like it when you hammer them :)
121 # Received disconnect from 74.125.248.80: 7: Too many concurrent connections
122 # fatal: The remote end hung up unexpectedly
123 mj_init 16
124
125 rlist=$(r list)
126 tcnt=$(echo "${rlist}" | wc -l)
127 cnt=1
128 while read line ; do
129 line=( ${line} )
130 path=${line[0]}
131 export GIT_DIR=${path}/.git
132 proj=${line[2]}
133 printf '### (%*i/%i %3i%%) %s\n' \
134 ${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj}
135 src="${sync_branch}"
136 g l -1 ${src} >& /dev/null || src=
137 mj_child g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null
138 done < <(r list)
139 mj_finish
140
141 exit 0
142 ;;
143 g-push)
144 # For the times when repo is being stupid, push directly to gerrit myself.
145 if ! branch=$(g symbolic-ref -q HEAD) ; then
146 err "could not figure out active branch"
147 fi
148 branch=${branch#refs/heads/}
149 if ! remote_branch=$(g cfg --get "branch.${branch}.merge") ; then
150 err "could not figure out remote branch"
151 fi
152 remote_branch=${remote_branch#refs/heads/}
153
154 reviewers=""
155 while [[ $# -gt 0 ]] ; do
156 case $1 in
157 --re)
158 reviewers=$2
159 shift
160 ;;
161 *)
162 err "unknown option: $1"
163 ;;
164 esac
165 shift
166 done
167
168 git_args=()
169 if [[ -n ${reviewers} ]] ; then
170 reviewers=( ${reviewers//,/ } )
171 git_args+=( "--receive-pack=git receive-pack ${reviewers[*]/#/--reviewer=}" )
172 fi
173
174 for remote in cros-internal cros origin ; do
175 if g cfg --get "remote.${remote}.url" >/dev/null ; then
176 vr git push "${git_args[@]}" ${remote} ${branch}:refs/for/${remote_branch}
177 exit $?
178 fi
179 done
180 err "could not figure out remote to push to"
181 ;;
182 sync)
183 set -- -j16 "$@"
184 ;;
185 upload)
186 args=()
187 while [[ $# -gt 0 ]] ; do
188 case $1 in
189 --re)
190 if [[ $2 == *"OWNERS" ]] ; then
191 owners=$(awk -F'@' '{list = list "," $1} END {print substr(list, 2)}' "$2")
192 if [[ -z ${owners} ]] ; then
193 err "cannot find OWNERS list"
194 else
195 echo "Auto setting reviewers to: ${owners}"
196 fi
197 args+=( --re "${owners}" )
198 shift 2
199 continue
200 fi
201 ;;
202 esac
203 args+=( "$1" )
204 shift
205 done
206 set -- "${args[@]}"
207 ;;
208 esac
209
210 exec repo ${acmd:-${cmd}} "$@"