Commit 55ec3797 authored by Adrian DC's avatar Adrian DC
Browse files

feat(tools): create 'git rbl' Python tool and 'rbl' alias

Issue: #3
---

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent b9c879a1
Loading
Loading
Loading
Loading
Loading
+2
Changes for profiles/gitconfig/alias/rebase/rbl.alias: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Git rebase local commits with interactive selection
dct-tools-git-rbl
+1
Changes for setup.py: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ PROJECT_SCRIPTS = [
    'git-dct = git_dct.cli.main:main',
    'git-dct-tools-git-pu = git_dct.tools.git_pu:main',
    'git-dct-tools-git-rb = git_dct.tools.git_rb:main',
    'git-dct-tools-git-rbl = git_dct.tools.git_rbl:main',
    'git-dct-tools-git-stat = git_dct.tools.git_stat:main',
]
PROJECT_URL = f'https://gitlab.com/{PROJECT_NAMESPACE}/{PROJECT_NAME}'
+2 −2
Changes for src/tools/git_pu.py: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -109,12 +109,12 @@ def git_pu(

    # Show git statistics
    else:
        result = git_stat(
        _, commits_remote = git_stat(
            remote=remote,
            branch=branch,
            stats_only=True,
        )
        if result != 0:
        if commits_remote != 0:
            risky = True

    # Prepare push commands
+182
Changes for src/tools/git_rbl.py: 182 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env python3

# Standard libraries
from argparse import (
    _ArgumentGroup,
    ArgumentParser,
    Namespace,
    RawTextHelpFormatter,
)
from shutil import get_terminal_size
from sys import exit as sys_exit
from typing import List

# Components
from ..package.bundle import Bundle
from ..package.settings import Settings
from ..package.updates import Updates
from ..prints.colors import Colors
from ..system.platform import Platform
from .git_stat import git_stat
from .tools import Tools

# Constants
HELP_POSITION: int = 23

# Git rebase local commits, pylint: disable=duplicate-code
def git_rbl(
    remote: str,
    branch: str,
) -> int:

    # Variables
    commands: List[str]
    result: int

    # Select remote
    if not remote:
        remote = Tools.select_remote()

        # Validate remote
        if not remote:
            print(' ')
            print(f'{Colors.YELLOW} {Tools.NAME}:'
                  f'{Colors.RED} Remote selection missing...'
                  f'{Colors.RESET}')
            print(' ')
            Platform.flush()
            return -1

    # Select branch
    if not branch:
        branch = Tools.select_branch(remote)

        # Validate branch
        if not branch:
            print(' ')
            print(f'{Colors.YELLOW} {Tools.NAME}:'
                  f'{Colors.RED} Remote branch selection missing...'
                  f'{Colors.RESET}')
            print(' ')
            Platform.flush()
            return -3

    # Rebase header
    print(' ')
    print(f'{Colors.GREEN} === {Tools.NAME} - Rebasing'
          f' local commits'
          f' against branch \'{branch}\''
          f' from remote \'{remote}\' ==='
          f'{Colors.RESET}')
    Platform.flush()

    # Show git statistics
    commits_local, _ = git_stat(
        remote=remote,
        branch=branch,
        stats_only=False,
    )

    # Prepare rebase commands
    commands = [
        'git',
        'rebase',
        '-i',
        f'HEAD~{commits_local}',
    ]

    # Rebase over remote branch
    result = Tools.run_command(commands)
    print(' ')
    Platform.flush()

    # Result
    return result

# Main, pylint: disable=duplicate-code
def main() -> None:

    # Variables
    group: _ArgumentGroup
    result: int

    # Configure tool
    Tools.NAME = 'git rbl'
    Tools.DESCRIPTION = 'Git rebase local commits with interactive selection'

    # Arguments creation
    parser: ArgumentParser = ArgumentParser(
        prog=Tools.NAME,
        description=f'{Tools.NAME}: {Tools.DESCRIPTION} ({Bundle.NAME})',
        add_help=False,
        formatter_class=lambda prog: RawTextHelpFormatter(
            prog,
            max_help_position=HELP_POSITION,
            width=min(
                120,
                get_terminal_size().columns - 2,
            ),
        ),
    )

    # Arguments tool definitions
    group = parser.add_argument_group('tool arguments')
    group.add_argument(
        '-h',
        '--help',
        dest='help',
        action='store_true',
        help='Show this help message',
    )
    group.add_argument(
        'remote',
        nargs='?',
        default=None,
        help='Remote repository name (default: auto)',
    )
    group.add_argument(
        'branch',
        nargs='?',
        default=None,
        help='Branch name (default: auto)',
    )

    # Arguments parser
    options: Namespace = parser.parse_args()

    # Help informations
    if options.help:
        print(' ')
        parser.print_help()
        print(' ')
        Platform.flush()
        sys_exit(0)

    # Instantiate settings
    settings: Settings = Settings(name=Bundle.NAME)

    # Prepare colors
    Colors.prepare()

    # Instantiate updates
    updates: Updates = Updates(
        name=Bundle.PACKAGE,
        settings=settings,
    )

    # Run tool
    result = git_rbl(
        remote=options.remote,
        branch=options.branch,
    )

    # Check for daily updates
    if updates.enabled and updates.daily:
        updates.check()

    # Result
    sys_exit(0 if result >= 0 else result)

# Entrypoint
if __name__ == '__main__': # pragma: no cover
    main()
+6 −6
Changes for src/tools/git_stat.py: 6 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ def git_stat(
    remote: str,
    branch: str,
    stats_only: bool,
) -> int:
) -> tuple[int, int]:

    # Select remote
    if not remote:
@@ -40,7 +40,7 @@ def git_stat(
                  f'{Colors.RESET}')
            print(' ')
            Platform.flush()
            return -1
            return -1, -1

    # Select branch
    if not branch:
@@ -54,7 +54,7 @@ def git_stat(
                  f'{Colors.RESET}')
            print(' ')
            Platform.flush()
            return -2
            return -2, -2

    # Fetch remote branch
    print(' ')
@@ -71,7 +71,7 @@ def git_stat(
              f'{Colors.RESET}')
        print(' ')
        Platform.flush()
        return -3
        return -3, -3

    # Show differences
    print(' ')
@@ -115,7 +115,7 @@ def git_stat(
    Platform.flush()

    # Result
    return commits_remote
    return commits_local, commits_remote

# Main, pylint: disable=duplicate-code
def main() -> None:
@@ -195,7 +195,7 @@ def main() -> None:
    )

    # Run tool
    commits_remote = git_stat(
    _, commits_remote = git_stat(
        remote=options.remote,
        branch=options.branch,
        stats_only=options.stats_only,