Created
May 29, 2019 18:57
-
-
Save jnewbery/1d45a6b5e14b3f3fefe5942d0cc2608d to your computer and use it in GitHub Desktop.
git tools
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 | |
import subprocess | |
import os | |
if os.name == 'posix': | |
RED = "\033[1;31m" | |
BLUE = "\033[0;34m" | |
CYAN = "\033[0;36m" | |
GREEN = "\033[0;32m" | |
RESET = "\033[0;0m" | |
branches = [br.split() for br in subprocess.run(["git for-each-ref --format='%(refname:strip=2) %(objectname:short) %(upstream:strip=2)' refs/heads/"], stdout=subprocess.PIPE, shell=True, universal_newlines=True).stdout.split('\n')][:-1] | |
max_br_len = len(max([br[0] for br in branches], key=len)) | |
max_upstream_len = len(max([br[2] for br in branches if len(br) > 2], key=len)) | |
ret = [] | |
for branch in branches: | |
name = branch[0].ljust(max_br_len) | |
sha = branch[1] | |
upstream = branch[2].ljust(max_upstream_len) if len(branch) > 2 else " " * max_upstream_len | |
if len(branch) > 1: | |
upstream += " " * max_upstream_len | |
desc = subprocess.run(["git config branch.%s.description" % branch[0]], stdout=subprocess.PIPE, shell=True, universal_newlines=True).stdout.rstrip() | |
if subprocess.run(["git rev-parse --abbrev-ref HEAD"], stdout=subprocess.PIPE, shell=True, universal_newlines=True).stdout.rstrip() == branch[0]: | |
ret += GREEN + "*%s %s %s -- %s" % (name, sha, upstream, desc) + RESET + "\n" | |
else: | |
ret += " " + name + " " + RED + sha + RESET + " " + BLUE + upstream + RESET + " " + CYAN + "-- " + desc + RESET + "\n" | |
# print("Pass: %s%s%s, Duration: %s s\n" % (BOLD[1], passed, BOLD[0], duration)) | |
print("".join(ret), end='') |
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 | |
"""checks out a pull request from github and updates the description of that PR to be the PR's title""" | |
import configparser | |
import os | |
import re | |
import requests | |
import shlex | |
import sys | |
import subprocess | |
# Get github credentials | |
config = configparser.ConfigParser() | |
configfile = os.path.abspath(os.path.dirname(__file__)) + "/github.ini" | |
try: | |
config.read_file(open(configfile, encoding="utf8")) | |
auth = "?client_id={}&client_password={}".format(config["github"]["CLIENT_ID"], config["github"]["CLIENT_SECRET"]) | |
except FileNotFoundError: | |
print("No github credentials found. Requesting without token.") | |
auth = "" | |
pr = int(sys.argv[1]) | |
remote = "upstream" | |
if len(sys.argv) > 2: | |
remote = sys.argv[2] | |
remotes = subprocess.run(["git remote -v | grep %s | grep fetch" % remote], stdout=subprocess.PIPE, shell=True, universal_newlines=True).stdout | |
repo = re.search(":(\S*)\.git ", remotes).group(1) | |
req = 'https://api.github.com/repos/{}/pulls/{}{}'.format(repo, pr, auth) | |
response = requests.get(req) | |
if response.status_code != 200: | |
print("Github returned error {}: {}.".format(response.status_code, response.reason), file=sys.stderr) | |
sys.exit(1) | |
pull = response.json() | |
title = pull['title'] | |
author = pull['user']['login'] | |
url = "https://github.com/%s/pull/%d" % (repo, pr) | |
subprocess.run("git fetch -fu %s refs/pull/%d/head:pr%d" % (remote, pr, pr), shell=True) | |
subprocess.run("git checkout pr%d" % pr, shell=True) | |
# sanitize the title before updating the description | |
desc = shlex.quote('[{}] {} - {}'.format(author, title, url)) | |
subprocess.run("git config branch.pr{}.description {}".format(pr, desc), shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment