Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 14 additions & 40 deletions bin/git-when-merged
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
#!/bin/sh
# -*- mode: python; coding: utf-8 -*-

# This file is used as both a shell script and as a Python script.

""":"
# This part is run by the shell. It looks for an appropriate Python
# interpreter then uses it to re-exec this script.

path=$(which python2 || which python)
if [ -x "$path" ]
then
PYTHON="$path"
else
echo 1>&2 "No usable Python interpreter was found!"
exit 1
fi

exec $PYTHON "$0" "$@"
" """
#!/usr/bin/env python

# Copyright (c) 2013 Michael Haggerty
#
Expand Down Expand Up @@ -104,13 +85,6 @@ import re
import subprocess
import optparse


# This upper end of this test doesn't actually work, because the
# syntax of this script is not valid Python 3.
if not (0x02050000 <= sys.hexversion < 0x03000000):
sys.exit('Python 2, version 2.5 or later is required')


# Backwards compatibility:
try:
from subprocess import CalledProcessError
Expand Down Expand Up @@ -153,15 +127,15 @@ class Failure(Exception):
def read_refpatterns(name):
key = 'whenmerged.%s.pattern' % (name,)
try:
out = check_output(['git', 'config', '--get-all', '--null', key])
out = check_output(['git', 'config', '--get-all', '--null', key]).decode('UTF-8')
except CalledProcessError:
raise Failure('There is no configuration setting for %r!' % (key,))
retval = []
for value in out.split('\0'):
if value:
try:
retval.append(re.compile(value))
except re.error, e:
except (re.error, e):
sys.stderr.write(
'Error compiling branch pattern %r; ignoring: %s\n'
% (value, e.message,)
Expand All @@ -183,7 +157,7 @@ def iter_commit_refs():
stdout=subprocess.PIPE,
)
for line in process.stdout:
words = line.strip().split()
words = line.strip().decode('UTF-8').split()
refname = words.pop(0)
if words == ['commit'] or words == ['tag', 'commit']:
yield refname
Expand All @@ -207,7 +181,7 @@ def rev_parse(arg, abbrev=None):
cmd = ['git', 'rev-parse', '--verify', '-q', arg]

try:
return check_output(cmd).strip()
return check_output(cmd).strip().decode('UTF-8')
except CalledProcessError:
raise Failure('%r is not a valid commit!' % (arg,))

Expand All @@ -218,7 +192,7 @@ def rev_list(*args):
stdout=subprocess.PIPE,
)
for line in process.stdout:
yield line.strip()
yield line.strip().decode('UTF-8')

retcode = process.wait()
if retcode:
Expand All @@ -235,7 +209,7 @@ def find_merge(commit, branch, abbrev):

try:
branch_sha1 = rev_parse(branch)
except Failure, e:
except (Failure, e):
sys.stdout.write(FORMAT % dict(refname=branch, msg='Is not a valid commit!'))
return None

Expand Down Expand Up @@ -286,7 +260,7 @@ def get_full_name(branch):
try:
full = check_output(
['git', 'rev-parse', '--verify', '-q', '--symbolic-full-name', branch]
).strip()
).strip().decode('UTF-8')
# The above call exits successfully, with no output, if branch
# is not a reference at all. So only use the value if it is
# not empty.
Expand All @@ -311,7 +285,7 @@ def main(args):

try:
default_abbrev = int(
check_output(['git', 'config', '--int', 'whenmerged.abbrev']).strip()
check_output(['git', 'config', '--int', 'whenmerged.abbrev']).strip().decode('UTF-8')
)
except CalledProcessError:
default_abbrev = None
Expand Down Expand Up @@ -374,15 +348,15 @@ def main(args):
# Convert commit into a SHA1:
try:
commit = rev_parse(commit)
except Failure, e:
except (Failure, e):
sys.exit(e.message)

refpatterns = []

for value in options.patterns:
try:
refpatterns.append(re.compile(value))
except re.error, e:
except (re.error, e):
sys.stderr.write(
'Error compiling pattern %r; ignoring: %s\n'
% (value, e.message,)
Expand All @@ -391,7 +365,7 @@ def main(args):
for value in options.names:
try:
refpatterns.extend(read_refpatterns(value))
except Failure, e:
except (Failure, e):
sys.exit(e.message)

branches = set()
Expand All @@ -406,7 +380,7 @@ def main(args):
for branch in args:
try:
branches.add(get_full_name(branch))
except Failure, e:
except (Failure, e):
sys.exit(e.message)

if not branches:
Expand All @@ -415,7 +389,7 @@ def main(args):
for branch in sorted(branches):
try:
merge = find_merge(commit, branch, options.abbrev)
except Failure, e:
except (Failure, e):
sys.stderr.write('%s\n' % (e.message,))
continue

Expand Down