]> git.wh0rd.org Git - home.git/blob - .bin/r
filter out options to expanded commands
[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 = $NF
37                                 } else {
38                                         list = list $NF " "
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         for remote in cros-internal cros origin ; do
121                 if g cfg --get "remote.${remote}.url" >/dev/null ; then
122                         vr git push ${remote} ${branch}:refs/for/${remote_branch}
123                         exit $?
124                 fi
125         done
126         err "could not figure out remote to push to"
127         ;;
128 sync)
129         [[ $# -eq 0 ]] && set -- -j16
130         ;;
131 upload)
132         args=()
133         while [[ $# -gt 0 ]] ; do
134                 case $1 in
135                 --re)
136                         if [[ $2 == *"OWNERS" ]] ; then
137                                 owners=$(awk -F'@' '{list = list "," $1} END {print substr(list, 2)}' "$2")
138                                 if [[ -z ${owners} ]] ; then
139                                         err "cannot find OWNERS list"
140                                 else
141                                         echo "Auto setting reviewers to: ${owners}"
142                                 fi
143                                 args+=( --re "${owners}" )
144                                 shift 2
145                                 continue
146                         fi
147                         ;;
148                 esac
149                 args+=( "$1" )
150                 shift
151         done
152         set -- "${args[@]}"
153         ;;
154 esac
155
156 exec repo ${acmd:-${cmd}} "$@"