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