Created
August 6, 2021 17:35
-
-
Save V1ncNet/d492c7d3db3b54f101a9dfb9f837ad8d to your computer and use it in GitHub Desktop.
Git Extension for Gerrit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Queries from https://gerrit-review.googlesource.com/Documentation/user-upload.html | |
import sys | |
import getopt | |
import subprocess | |
query_entries = [] | |
def get_active_branch_name(): | |
branch_name_blob = subprocess.check_output(["git", "branch", "--show-current"]) | |
branch_name = branch_name_blob.decode('utf-8') | |
return branch_name.strip() | |
def appendMultiple(queryKey, joined): | |
values = joined.split(",") | |
values = map(lambda value: "{0}={1}".format(queryKey, value), values) | |
query_entries.extend(list(values)) | |
def appendPair(key, value): | |
query_entries.append("{}={}".format(key, value)) | |
def append(query): | |
query_entries.append(query) | |
def encode(str): | |
return str.replace(" ", "_") | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "pswr:c:t:m:", ["private", "silent", "wip", "reviewers=", "cc=", "topic=", "tags=", "message="]) | |
for opt, arg in opts: | |
if opt in ("-r", "--reviewers"): | |
appendMultiple("r", arg) | |
elif opt in ("-t", "--tags"): | |
appendMultiple("t", arg) | |
elif opt in ("-c", "--cc"): | |
appendMultiple("cc", arg) | |
elif opt in ("--topic"): | |
appendPair("topic", arg) | |
elif opt in ("-m", "--message"): | |
value = encode(arg) | |
appendPair("m", value) | |
elif opt in ("-w", "--wip"): | |
append("wip") | |
elif opt in ("-p", "--p"): | |
append("private") | |
elif opt in ("-s", "--silent"): | |
append("notify=NONE") | |
except: | |
pass | |
query_string = ",".join(query_entries) | |
branch_name = get_active_branch_name() | |
git_push = ["git", "push", "origin", "HEAD:refs/for/{}%{}".format(branch_name, query_string)] | |
child = subprocess.Popen(git_push, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
stdout, stderr = child.communicate() | |
stdout, stderr | |
exit(child.returncode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment