]> git.wh0rd.org Git - home.git/blob - .bin/r
6771348c705bcf90d1fb944f63b467d7282dbe02
[home.git] / .bin / r
1 #!/bin/bash
2 g() { git "$@"; }
3 err() { printf '%b\n' "$*" 1>&2; exit 1; }
4
5 case $1 in
6 ""|-*) ;;
7 l)
8         cmd=list
9         shift
10         ;;
11 *)
12         cmd=$1
13         shift
14         acmd=$(git config --get "alias.${cmd}")
15         ;;
16 esac
17
18 case ${acmd:-${cmd}} in
19 rebase)
20         if [[ $1 == "all" ]] ; then
21                 shift
22                 if [[ $# -eq 0 ]] ; then
23                         exec r forall -c 'r rb all .'
24                 fi
25
26                 branches=$(g b | awk '
27                         {
28                                 if ($0 ~ "^[*] *[(]no branch[)]") {
29                                         next
30                                 } else if ($1 == "*") {
31                                         b = $NF
32                                 } else {
33                                         list = list $NF " "
34                                 }
35                         }
36                         END { print list b }')
37                 [[ -z ${branches} ]] && exit 0
38
39                 eval $(bash-colors --env)
40                 echo "${GOOD}### ${PWD}${NORMAL}"
41                 for b in ${branches} ; do
42                         echo " ${HILITE}### $b${NORMAL}"
43                         g co -q $b || exit 1
44                         if ! r rb "$@" ; then
45                                 g rb-a
46                         fi
47                 done
48                 exit 0
49         fi
50         ;;
51 sb-push)
52         sync_branch="v"
53
54         root=${PWD}
55         while [[ ! -d ${root}/.repo && ${root} != "/" ]] ; do
56                 root=${root%/*}
57         done
58         cd "${root}" || exit 1
59
60         if [[ ! -e .repo/sandbox-url ]] ; then
61                 err "Please configure remote url base in ${root}/.repo/sandbox-url"
62         fi
63         remote=$(<.repo/sandbox-url) || exit 1
64
65         echo "pushing projects from ${root}"
66
67         pids=()
68         rlist=$(r list)
69         tcnt=$(echo "${rlist}" | wc -l)
70         cnt=1
71         while read line ; do
72                 line=( ${line} )
73                 path=${line[0]}
74                 export GIT_DIR=${path}/.git
75                 proj=${line[2]}
76                 printf '### (%*i/%i %3i%%) %s\n' \
77                         ${#tcnt} $((cnt++)) ${tcnt} $(( cnt * 100 / tcnt )) ${proj}
78                 src="${sync_branch}"
79                 g l -1 ${src} >& /dev/null || src=
80                 ( g push --force ${remote}/${proj} ${src}:refs/sandbox/${USER}/${sync_branch} >/dev/null ) &
81
82                 # ssh servers do not like it when you hammer them :)
83                 #       Received disconnect from 74.125.248.80: 7: Too many concurrent connections
84                 #       fatal: The remote end hung up unexpectedly
85                 pids+=( $! )
86                 if [[ ${#pids[@]} -eq 20 ]] ; then
87                         wait ${pids[@]:0:3}
88                         pids=( ${pids[@]:3} )
89                 fi
90         done <<<"$(r list)"
91         wait
92
93         exit 0
94         ;;
95 g-push)
96         # For the times when repo is being stupid, push directly to gerrit myself.
97         shift
98         if [[ $# -ne 1 ]] ; then
99                 echo "Usage: r g-push <branch>"
100                 exit 1
101         fi
102         exec g push cros-internal HEAD:refs/for/master
103         ;;
104 upload)
105         args=()
106         while [[ $# -gt 0 ]] ; do
107                 case $1 in
108                 --re)
109                         if [[ $2 == *"OWNERS" ]] ; then
110                                 owners=$(awk -F'@' '{list = list "," $1} END {print substr(list, 2)}' "$2")
111                                 if [[ -z ${owners} ]] ; then
112                                         err "cannot find OWNERS list"
113                                 else
114                                         echo "Auto setting reviewers to: ${owners}"
115                                 fi
116                                 args+=( --re "${owners}" )
117                                 shift 2
118                                 continue
119                         fi
120                         ;;
121                 esac
122                 args+=( "$1" )
123                 shift
124         done
125         set -- "${args[@]}"
126         ;;
127 esac
128
129 exec repo ${acmd:-${cmd}} "$@"