1#!/bin/bash
2
3function fn_aosp_ls {
4  CHANGE_ID=$1
5  curl -s "https://android-review.googlesource.com/changes/$CHANGE_ID?o=CURRENT_REVISION" | tail -n +2
6}
7
8function fn_aosp_checkout {
9  CHANGE_ID=$1
10  REPO_ID=`fn_aosp_ls $CHANGE_ID \
11    | jq "(._number|tostring) + \"/\" + (.revisions | to_entries[].value._number|tostring)"`
12
13  if [ -z $DEP_CHANGE_ID ]; then
14    echo "Failed to parse aosp command for Change-Id: $CHANGE_ID"
15    exit 1
16  fi
17
18  repo download "platform/frameworks/support" ${REPO_ID:1:-1}  \
19    && BRANCH=`git log -1 | grep Change-Id | awk '{print $2}'` \
20    && git checkout -B "aosp/$BRANCH"                          \
21    && git branch --set-upstream-to=aosp/androidx-main
22}
23
24function fn_aosp_merged {
25  CHANGE_ID=$1
26  echo $CHANGE_ID
27  fn_aosp_ls $CHANGE_ID
28}
29
30function fn_git_changeid {
31  BRANCH=$1
32  git log $BRANCH -1 | grep Change-Id | awk '{print $2}'
33}
34
35function fn_git_check_uncommitted_changes {
36  if [[ `git status --porcelain` ]]; then
37    echo "Uncommitted changes; exiting"
38    exit
39  fi
40}
41
42# Check that jq is installed.
43if [ -z `which jq` ]; then
44    echo "jq not installed; exiting"
45    exit
46fi
47
48if [[ -z "$1" ]] || [[ "$1" == "help" ]]; then
49  echo -n \
50"usage: aosp <command> [<args>]
51
52A CLI for Gerrit UI that works with the cli tool, repo, to make it easier to search, checkout, and rebase changes. This script currently only works within frameworks/support.
53
54Commands:
55  help               	Display this help text
56  ls [<gerrit-email>]	Query gerrit for open CLs; you must run this before aosp c. <gerrit-email> defaults to `whoami`@google.com
57  c <id>             	Force checkout a change in gerrit by the id returned from aosp ls command into a branch named `whoami`/<change-id>
58  prune              	Delete any branches whose HEAD has a Change-Id for a CL that has been MERGED or ABANDONED in Gerrit
59  r                  	Checkout the latest patchset of HEAD~1's Change-Id, and cherry-pick HEAD ontop of it
60"
61elif [[ "$1" == "ls" ]]; then
62  OWNER="${2:-`whoami`@google.com}"
63  curl -s "https://android-review.googlesource.com/changes/?q=owner:$OWNER+status:open&o=CURRENT_REVISION" \
64    | tail -n +2 \
65    | jq -r ".[] | [._number, .subject, (.revisions | to_entries[].value._number), (.revisions[.current_revision].fetch.http | .url, .ref)] | @csv" \
66    | awk -F, '{ printf "%s,%s/%s,%s,%s,%s\n", NR, $1, $3, $2, $4, $5}' \
67    | column -t -s, \
68    | tee ~/.dustinlam_aosp
69elif [[ "$1" == "prune" ]]; then
70  git branch           \
71    | grep -v "^\*"    \
72    | awk '{print $1}' \
73    | while read line
74    do
75      CHANGE_ID=`fn_git_changeid $line`
76      if [ ! -z "$CHANGE_ID" ]; then
77        STATUS=`fn_aosp_ls "$CHANGE_ID" | jq .status`
78        if [[ $STATUS == "\"MERGED\"" ]] || [[ $STATUS == "\"ABANDONED\"" ]]; then
79          git branch -D $line
80        fi
81      else
82        echo "Failed to prune $line due to missing Change-Id"
83      fi
84    done
85elif [[ "$1" == "c" ]]; then
86  fn_git_check_uncommitted_changes
87
88  PATCH=`cat ~/.dustinlam_aosp | sed -n "$2p" | awk '{print $2}'`      \
89  && repo download "platform/frameworks/support" "$PATCH"    \
90  && BRANCH=`git log -1 | grep Change-Id | awk '{print $2}'` \
91  && git checkout -B "`whoami`/$BRANCH"                     \
92  && git branch --set-upstream-to=aosp/androidx-main
93elif [[ "$1" == "r" ]]; then
94  fn_git_check_uncommitted_changes
95
96  HEAD_COMMIT_HASH=`git rev-parse HEAD`
97  CURRENT_BRANCH=`git branch | grep \* | awk '{print $2}'`
98  DEP_CHANGE_ID=`git log --skip 1 -1 | grep Change-Id | awk '{print $2}'`
99  if [ -z $DEP_CHANGE_ID ]; then
100    echo "Dependent change is missing Change-Id"
101    exit 1
102  fi
103
104  fn_aosp_checkout $DEP_CHANGE_ID   \
105    && git checkout -B $CURRENT_BRANCH \
106    && git cherry-pick $HEAD_COMMIT_HASH
107fi
108