]> git.wh0rd.org - home.git/blob - .bin/r
fix quoting with gerrit push
[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 case ${acmd:-${cmd}} in
24 rebase)
25 if [[ $1 == "all" ]] ; then
26 shift
27 if [[ $# -eq 0 ]] ; then
28 exec r forall -p -c 'r rb all .' </dev/null
29 fi
30
31 branches=$(g b | awk '
32 {
33 if ($0 ~ "^[*] *[(]no branch[)]") {
34 next
35 } else if ($1 == "*") {
36 b = $2
37 } else {
38 list = list $1 " "
39 }
40 }
41 END { print list b }')
42 [[ -z ${branches} ]] && exit 0
43
44 eval $(bash-colors --env)
45 #echo "${GOOD}### ${PWD}${NORMAL}"
46 for b in ${branches} ; do
47 echo " ${HILITE}### $b${NORMAL}"
48 g co -q $b || exit 1
49 if ! r rb "$@" ; then
50 g rb-a
51 fi
52 done
53 exit 0
54 fi
55 ;;
56 sb-push)
57 sync_branch="v"
58
59 root=${PWD}
60 while [[ ! -d ${root}/.repo && ${root} != "/" ]] ; do
61 root=${root%/*}
62 done
63 cd "${root}" || exit 1
64
65 if [[ ! -e .repo/sandbox-url ]] ; then
66 err "Please configure remote url base in ${root}/.repo/sandbox-url"
67 fi
68 remote=$(<.repo/sandbox-url) || exit 1
69
70 echo "pushing projects from ${root}"
71
72 pipe=$(mktemp)
73 rm -f "${pipe}"
74 mkfifo "${pipe}"
75 exec {ctlfd}<>"${pipe}"
76 rm -f "${pipe}"
77 jobs=0
78
79 rlist=$(r list)
80 tcnt=$(echo "${rlist}" | wc -l)
81 cnt=1
82 while read line ; do
83 line=( ${line} )
84 path=${line[0]}
85 export GIT_DIR=${path}/.git
86 proj=${line[2]}
87 printf '### (%*i/%i %3i%%) %s\n' \
88 ${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj}
89 src="${sync_branch}"
90 g l -1 ${src} >& /dev/null || src=
91 (
92 g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null
93 echo ${BASHPID} $? >&${ctlfd}
94 ) &
95
96 # ssh servers do not like it when you hammer them :)
97 # Received disconnect from 74.125.248.80: 7: Too many concurrent connections
98 # fatal: The remote end hung up unexpectedly
99 : $(( ++jobs ))
100 if [[ ${jobs} -eq 16 ]] ; then
101 read -r -u ${ctlfd} pid ret
102 : $(( --jobs ))
103 fi
104 done < <(r list)
105 wait
106
107 exit 0
108 ;;
109 g-push)
110 # For the times when repo is being stupid, push directly to gerrit myself.
111 if ! branch=$(g symbolic-ref -q HEAD) ; then
112 err "could not figure out active branch"
113 fi
114 branch=${branch#refs/heads/}
115 if ! remote_branch=$(g cfg --get "branch.${branch}.merge") ; then
116 err "could not figure out remote branch"
117 fi
118 remote_branch=${remote_branch#refs/heads/}
119
120 reviewers=""
121 while [[ $# -gt 0 ]] ; do
122 case $1 in
123 --re)
124 reviewers=$2
125 shift
126 ;;
127 *)
128 err "unknown option: $1"
129 ;;
130 esac
131 shift
132 done
133
134 git_args=()
135 if [[ -n ${reviewers} ]] ; then
136 reviewers=( ${reviewers//,/ } )
137 git_args+=( "--receive-pack=git receive-pack ${reviewers[*]/#/--reviewer=}" )
138 fi
139
140 for remote in cros-internal cros origin ; do
141 if g cfg --get "remote.${remote}.url" >/dev/null ; then
142 vr git push "${git_args[@]}" ${remote} ${branch}:refs/for/${remote_branch}
143 exit $?
144 fi
145 done
146 err "could not figure out remote to push to"
147 ;;
148 sync)
149 [[ $# -eq 0 ]] && set -- -j16
150 ;;
151 upload)
152 args=()
153 while [[ $# -gt 0 ]] ; do
154 case $1 in
155 --re)
156 if [[ $2 == *"OWNERS" ]] ; then
157 owners=$(awk -F'@' '{list = list "," $1} END {print substr(list, 2)}' "$2")
158 if [[ -z ${owners} ]] ; then
159 err "cannot find OWNERS list"
160 else
161 echo "Auto setting reviewers to: ${owners}"
162 fi
163 args+=( --re "${owners}" )
164 shift 2
165 continue
166 fi
167 ;;
168 esac
169 args+=( "$1" )
170 shift
171 done
172 set -- "${args[@]}"
173 ;;
174 esac
175
176 exec repo ${acmd:-${cmd}} "$@"