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