#!/usr/bin/env python3

import argparse
import sys

from jnscommons import jnsgit


def main():
    exit_code = 0

    try:
        opts = _parse_args()
        _validate(opts)
        _rebase(opts)
    except ExitCodeError as e:
        exit_code = e.exit_code
        print(str(e), file=sys.stderr, flush=True)

    sys.exit(exit_code)


def _parse_args():
    parser = argparse.ArgumentParser(description='Rebase on master.')

    parser.add_argument('--dry-run', action='store_true', default=False, dest='dry_run',
                        help='Output what actions will be performed without taking them (default: %(default)s)')

    parser.add_argument('branch', nargs='?', metavar='branchname', default='master',
                        help='The branch to rebase on (default: %(default)s)')

    opts = parser.parse_args()
    opts.branch = opts.branch.strip()

    return opts


def _validate(opts):
    current_branch = jnsgit.branch_name()

    if current_branch == 'master':
        raise ExitCodeError('Do not rebase master!')

    if current_branch == opts.branch:
        raise ExitCodeError('Cannot rebase {} onto itself'.format(current_branch))


def _rebase(opts):
    current_branch = jnsgit.branch_name()

    jnsgit.checkout(opts.branch, dry_run=opts.dry_run, print_cmd=True)
    print()

    jnsgit.pull(dry_run=opts.dry_run, print_cmd=True)
    print()

    jnsgit.checkout(current_branch, dry_run=opts.dry_run, print_cmd=True)
    print()

    jnsgit.rebase(opts.branch, interactive=True, dry_run=opts.dry_run, print_cmd=True)


class ExitCodeError(Exception):
    def __init__(self, message, exit_code=1):
        super().__init__(message)
        self.exit_code = exit_code


if __name__ == '__main__':
    main()

@@ -77,16 +77,92 @@
 nsgit%0A%0A%0A
+_PROTECTED_BRANCHES = %5B'master', 'main', 'dev', 'develop', 'development'%5D%0A%0A%0A
 def main
@@ -1045,19 +1045,30 @@
 nch 
-== 'master'
+in _PROTECTED_BRANCHES
 :%0A  
@@ -1093,16 +1093,17 @@
 deError(
+f
 'Do not 
@@ -1109,22 +1109,32 @@
  rebase 
-master
+%7Bcurrent_branch%7D
 !')%0A%0A   

Add protected branches to gitrom