lineage-push: Convert to python

Change-Id: Ib025430257ae46cd237520108f29939edde172e7
This commit is contained in:
Luca Stefani
2018-04-14 22:14:05 +02:00
parent 0f56b376cb
commit 45c215cba1
3 changed files with 106 additions and 83 deletions

View File

@@ -1,22 +1,30 @@
# LineageOS Push Script
```
usage: lineage-push.py [-h] [-d] [-e] [-f] [-l LABEL] [-m] [-r REF] [-s]
[-t TOPIC]
branch
Pushes a local git repository's changes to Gerrit for code review
positional arguments:
branch upload change to branch
optional arguments:
-h, --help show this help message and exit
-d, --draft upload change as draft
-e, --edit upload change as edit
-f, --force force push
-l LABEL, --label LABEL
assign label
-m, --merge bypass review and merge
-r REF, --ref REF push to specified ref
-s, --submit submit change
-t TOPIC, --topic TOPIC
append topic to change
```
Usage:
./lineage-push.sh [options] branch
Options:
-d Upload change as draft.
-e Update change as edit.
-f Force push.
-l <label> Assign label.
-m Bypass review and merge.
-r <ref> Push to specified ref ( will override draft ).
-s Submit.
-t <topic> Append topic to change.
Example:
```
Examples:
lineage-push -d -t test cm-14.1
lineage-push -s -l "Code-Review+2,Verified+1" cm-14.1
```

84
lineage-push/lineage-push.py Executable file
View File

@@ -0,0 +1,84 @@
#!/usr/bin/env python
from __future__ import print_function
import re
import subprocess
import sys
from argparse import ArgumentParser
def push(args):
command = 'git push'
if args.force:
command += ' -f'
username = subprocess.check_output(
["git", "config", "review.review.lineageos.org.username"]).decode("utf-8").strip()
remotes = subprocess.check_output(
["git", "remote", "-v"]).decode("utf-8").strip()
repo = re.search(r'LineageOS\S+', remotes).group(0)
command += ' ssh://{}@review.lineageos.org:29418/{}'.format(
username, repo)
command += ' HEAD:'
if args.ref != 'for':
command += 'refs/{}/'.format(args.ref)
elif args.merge:
command += ''
elif args.draft:
command += 'refs/drafts/'
else:
command += 'refs/{}/'.format(args.ref)
command += args.branch
if args.label:
labels = args.label.split(',')
command += '%'
for count, label in enumerate(labels):
command += 'l={}'.format(label)
if count != len(labels) - 1:
command += ','
if args.edit:
command += '%edit'
if args.topic:
command += '%topic={}'.format(args.topic)
if args.submit:
command += '%submit'
sys.exit(subprocess.call(command, shell=True))
def parse_cmdline():
parser = ArgumentParser(
description='Pushes a local git repository\'s changes to Gerrit for code review')
parser.add_argument('branch', help='upload change to branch')
parser.add_argument('-d', '--draft', action='store_true',
help='upload change as draft')
parser.add_argument('-e', '--edit', action='store_true',
help='upload change as edit')
parser.add_argument(
'-f', '--force', action='store_true', help='force push')
parser.add_argument('-l', '--label', help='assign label')
parser.add_argument('-m', '--merge', action='store_true',
help='bypass review and merge')
parser.add_argument(
'-r', '--ref', help='push to specified ref', default="for")
parser.add_argument(
'-s', '--submit', action='store_true', help='submit change')
parser.add_argument('-t', '--topic', help='append topic to change')
return parser.parse_args()
def main():
args = parse_cmdline()
push(args)
if __name__ == '__main__':
main()

View File

@@ -1,69 +0,0 @@
#!/bin/bash
usage() {
echo "Usage:"
echo " lineage-push [options] branch"
echo
echo " Options:"
echo " -d Upload change as draft."
echo " -e Update change as edit."
echo " -f Force push."
echo " -l <label> Assign label."
echo " -m Bypass review and merge."
echo " -r <ref> Push to specified ref ( will override draft )."
echo " -s Submit."
echo " -t <topic> Append topic to change."
echo
echo " Example:"
echo " lineage-push -d -t test cm-14.1"
echo " lineage-push -s -l \"Code-Review+2,Verified+1\" cm-14.1"
echo
exit 1
}
while getopts ":del:fmr:st:" opt; do
case $opt in
d) [ -z "$ref" ] && ref="refs/drafts/" ;;
e) edit="%edit" ;;
f) push_args="-f" ;;
l) i=0
labels="%"
IFS=',' read -ra LABELS <<< "$OPTARG"
for label in "${LABELS[@]}"; do
labels+="l=$label"
i=$(($i + 1))
if [ $i -ne ${#LABELS[@]} ]; then
labels+=","
fi
done
;;
m) [ -z "$ref" ] && ref="" ;;
r) ref="refs/$OPTARG/" ;;
s) submit="%submit" ;;
t) topic="%topic=$OPTARG" ;;
:)
echo "Option -$OPTARG requires an argument"
echo
usage
;;
\?)
echo "Invalid option: -$OPTARG"
echo
usage
;;
esac
done
shift $((OPTIND-1))
if [ "$#" -ne 1 ]; then
usage
fi
if [ -z "$ref" ]; then
ref="refs/for/"
fi
repo_name=$(git remote -v | grep LineageOS | head -n1 | awk '{print $2}' | sed 's/.*\///' | sed 's/\.git//')
username=$(git config review.review.lineageos.org.username)
git push ${push_args} ssh://${username}@review.lineageos.org:29418/LineageOS/${repo_name} HEAD:${ref}$1${topic}${labels}${submit}${edit}