diff options
author | Luca Stefani <luca.stefani.ge1@gmail.com> | 2018-04-14 22:14:05 +0200 |
---|---|---|
committer | Luca Stefani <luca.stefani.ge1@gmail.com> | 2018-04-14 23:21:11 +0200 |
commit | 45c215cba1b2e34d4b5973b98f9866ec9ab8be53 (patch) | |
tree | 4a7cb1d545847df26c6aa7f7da40f97df1bc5908 | |
parent | 0f56b376cb285cfac9b05ef89a513a48e0cad708 (diff) | |
download | scripts-45c215cba1b2e34d4b5973b98f9866ec9ab8be53.tar.gz scripts-45c215cba1b2e34d4b5973b98f9866ec9ab8be53.tar.bz2 scripts-45c215cba1b2e34d4b5973b98f9866ec9ab8be53.zip |
lineage-push: Convert to python
Change-Id: Ib025430257ae46cd237520108f29939edde172e7
-rw-r--r-- | lineage-push/README.md | 36 | ||||
-rwxr-xr-x | lineage-push/lineage-push.py | 84 | ||||
-rwxr-xr-x | lineage-push/lineage-push.sh | 69 |
3 files changed, 106 insertions, 83 deletions
diff --git a/lineage-push/README.md b/lineage-push/README.md index 966716d..14e5f70 100644 --- a/lineage-push/README.md +++ b/lineage-push/README.md @@ -1,22 +1,30 @@ # LineageOS Push Script -Pushes a local git repository's changes to Gerrit for code review - ``` -Usage: - ./lineage-push.sh [options] branch +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 - 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. +positional arguments: + branch upload change to branch - Example: +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 +``` +``` + Examples: lineage-push -d -t test cm-14.1 lineage-push -s -l "Code-Review+2,Verified+1" cm-14.1 ``` diff --git a/lineage-push/lineage-push.py b/lineage-push/lineage-push.py new file mode 100755 index 0000000..5a596a7 --- /dev/null +++ b/lineage-push/lineage-push.py @@ -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() diff --git a/lineage-push/lineage-push.sh b/lineage-push/lineage-push.sh deleted file mode 100755 index a39407e..0000000 --- a/lineage-push/lineage-push.sh +++ /dev/null @@ -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} |