"""
Archive branches that are merged into master.
This is done by tagging them as archive/<branchname> and removing them both locally and
remotely. Before each operation, the user is asked for confirmation.
"""
# This dependency can be found on github: https://github.com/Chiel92/python-shellout
from pyshellout import get, out, confirm


# Tag merged branches
merged_branches = [line.strip() for line in
                   get(r'git branch --merged master').n]
merged_branches = [branch for branch in merged_branches
                   if branch != '' and not '*' in branch and not branch == 'master']
archived_branches = []
for branch in merged_branches:
    if confirm('Archive branch {}?', branch):
        out('git tag archive/{} {}', branch, branch)
        archived_branches.append(branch)

if not archived_branches:
    exit('No branches archived. Bye.')

# Push archive tags to remote
print()
print('Created archive tags:')
for branch in archived_branches:
    print('    ' + branch)
if confirm('Push archive tags to remote?'):
    for branch in archived_branches:
        out('git push origin archive/{}', branch)
> git archive-branches
# archive branches merged into epic-branch
> git archive-branches epic-branch

"""
# This dependency can be found on github: https://github.com/Chiel92/python-shellout
import sys
from pyshellout import get, out, confirm

MASTER = 'master'
ARGS_COUNT = len(sys.argv)
REFERENCE_BRANCH = MASTER if ARGS_COUNT <= 1 else sys.argv[1]

# Tag merged branches
merged_branches = [line.strip() for line in
                   get(r'git branch --merged ' + REFERENCE_BRANCH).n]
merged_branches = [branch for branch in merged_branches
                   if branch != '' and not '*' in branch and not branch == MASTER and not branch == REFERENCE_BRANCH]
archived_branches = []
archived_branches_all = []
for branch in merged_branches:
    if confirm('Archive branch {}?', branch):
        out('git tag archive/{} {}', branch, branch)
        archived_branches.append(branch)

if not archived_branches:
    exit('No branches archived. Bye.')

print('Next tasks are: push tags to remote, delete remote and local branches')
do_not_ask = not confirm('Request confirmation for every step?')
"""
Merge a set of files, explicitly generating the files 'common', 'ours' and 'theirs'.
Allow the user to edit these files right before doing a 3 way merge.
Merge results are stored in another file, named '{filename}.result'.
"""
import re

# This dependency can be found on github: https://github.com/Chiel92/python-shellout
from pyshellout import get, out, confirm

kdiff3 = r'"c:/Program Files/KDiff3/kdiff3.exe"'


regex = re.compile(r'\d+ (\w+) (\d)\s*([\w/.-]+)')
lines = get('git ls-files -u').n
filenames = []
for line in lines:
    match = regex.match(line)
    sha = match.group(1)
    role = match.group(2)
    filename = match.group(3)

    # Only add the filename once
    if role == '1':
        filenames.append(filename)

    target = {
        '1': 'ours',
        '2': 'common',
        '3': 'theirs',
    }[role]
import argparse

# This dependency can be found on github: https://github.com/Chiel92/python-shellout
from pyshellout import get, out, confirm


parser = argparse.ArgumentParser()
parser.add_argument('master',
                    help='Name of the master branch. Defaults to \'master\'.',
                    nargs='?', default='master')
args = parser.parse_args()
master = args.master


# Get merged branches
merged_branches = [line.strip() for line in
                   get(r'git branch --merged {}', master, critical=True).n]
merged_branches = [branch for branch in merged_branches
                   if branch != '' and not '*' in branch and not branch == master]

print('Merged local branches:')
for branch in merged_branches:
    print('    ' + branch)


print()
for branch in merged_branches:
    if confirm('Delete local branch {}?', branch):
        out('git branch -d {}', branch)