#!/usr/bin/env python3
"""
Shipit script will shipt all commits from a given feature branch
as a single non fast forward merge, while adding the proper agreed upon
commit message formatting.
You must sit on a local clone of the target branch.
If you are happy with how shipit merged, you just "git push" the result
to the remote branch.
"""
import argparse
import os
import pwd
import subprocess
def ask_if_missing(label, value):
"""
Asks for user input for label on no value.
Otherwise it returns the value itself.
"""
if value:
return value
# pylint: disable=bad-builtin
return input("{}: ".format(label))
def build_commit_msg(author, reviewers, from_branch, to_branch, msg, mp):
"""Builds the agreed convention merge commit message"""
return "Merge {} into {}\n\n{}\n\n[a={}] [r={}]\nMP: {}".format(
from_branch, to_branch, msg, author, reviewers, mp)
def build_merge_cmd(from_branch, commit_msg, summary):
"""Builds the merge command."""
return ["git", "merge", "--no-ff", from_branch, "-m",
"{}".format(commit_msg), "-e" if not summary else ""]
def main():
"""Invokes the commit building with proper user inputs."""
current_user = pwd.getpwuid(os.getuid()).pw_name
parser = argparse.ArgumentParser(
description='Ship approved MP commits to LP as a --no-ff merge')
parser.add_argument('--author', '-a', default=current_user,
help='Author LP login')
parser.add_argument('--reviewers', '-r',
help=('Comma-separated list of LP reviewer logins'
'(at least 2)'))
parser.add_argument('--feature_branch', '-f',
help='Feature branch name to merge')
parser.add_argument('--target', '-t', default='devel',
help='Target branch to merge to')
parser.add_argument('--message', '-m', default=None,
help='Merge MP branch commit message.')
parser.add_argument('--MP',
help='Link to the LP Merge Proposal')
args = parser.parse_args()
reviewers = ask_if_missing('Reviewers', args.reviewers)
feature_branch = ask_if_missing('Feature branch', args.feature_branch)
mp = ask_if_missing('MP', args.MP)
commit_msg = build_commit_msg(args.author,
reviewers,
feature_branch,
args.target,
args.message if args.message else '',
mp)
cmd = build_merge_cmd(feature_branch, commit_msg, args.message)
print(" ".join(cmd))
p = subprocess.Popen(cmd)
p.communicate()
if __name__ == "__main__":
main()