diff --git a/src/git_flow/command/commit.py b/src/git_flow/command/commit.py index 07c3559..9ef004f 100644 --- a/src/git_flow/command/commit.py +++ b/src/git_flow/command/commit.py @@ -1,10 +1,12 @@ from datetime import datetime import typer +from rich.table import Table -import git_flow.command.base as base from git_flow import TRASH_BRANCH_PREFIX, WIP_BRANCH_PREFIX, GitFlowError +from git_flow.command import base from git_flow.git import Git +from git_flow.io import console app = typer.Typer() @@ -31,6 +33,11 @@ def commit(): else: raise GitFlowError("Debe agregar algún cambio al indice para continuar.") + _show_index() + + if not base.io_confirm("¿Desea commitear estos cambios?"): + raise GitFlowError("Ejecución cancelada.") + message = base.get_commit_message() commit_type = message[: message.index(":")] @@ -63,10 +70,41 @@ def _has_files_staged(): if not status: raise GitFlowError("No hay cambios para commitear.") - files_in_index = [] + return any(_is_staged(s) for s in status) - for s in status: - if s.worktree != "?" and s.worktree != " ": - files_in_index.append(s.file) - return len(files_in_index) > 0 +def _is_staged(s: Git.Status) -> bool: + return s.index != "?" and s.index != " " + + +INDEX_STATES = { + "M": "modificado", + "T": "cambio de tipo", + "A": "agregado", + "D": "eliminado", + "R": "renombrado", + "C": "copiado", + "U": "en conflicto", +} + + +def _show_index(): + """Muestra en una tabla los archivos del indice (los que entran en el commit), e informa + cuántos archivos quedan con cambios fuera del indice.""" + status = Git.status() + staged = [s for s in status if _is_staged(s)] + not_staged = [s for s in status if s.worktree != " "] + + table = Table(title="Cambios a commitear", title_justify="left", title_style="bold") + table.add_column("Estado", style="cyan", no_wrap=True) + table.add_column("Archivo") + + for s in staged: + table.add_row(INDEX_STATES.get(s.index, s.index), s.file) + + console.print(table) + + if not_staged: + base.io_info( + f"{len(not_staged)} archivo(s) con cambios fuera del indice no se incluirán en el commit." + ) diff --git a/src/git_flow/git.py b/src/git_flow/git.py index 95e4f52..9f754e8 100644 --- a/src/git_flow/git.py +++ b/src/git_flow/git.py @@ -26,8 +26,9 @@ class Git: file: str def __init__(self, line: str) -> None: - self.worktree = line[0] - self.index = line[1] + # Formato porcelain "XY ": X es el estado en el indice, Y en el worktree + self.index = line[0] + self.worktree = line[1] self.file = line[3:].strip("\n") command: list[str] @@ -170,7 +171,7 @@ class Git: process = self._get(**kwargs) lines = process.stdout.splitlines() - return lines if not strip else list(map(lambda l: l.strip(), lines)) + return lines if not strip else [l.strip() for l in lines] def firstline(self, **kwargs): lines = self.lines(**kwargs)