feature: agrego comando release, elimino codigo no utilizado
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from argparse import Namespace, ArgumentParser
|
||||
from abc import ABC, abstractmethod
|
||||
import os
|
||||
from os.path import isfile
|
||||
|
||||
from git_flow import (
|
||||
@@ -45,6 +44,11 @@ class Command(ABC):
|
||||
def run(self, args: Namespace = Namespace()):
|
||||
pass
|
||||
|
||||
def init(self):
|
||||
self.flowconfig = (
|
||||
Git.get_config(FLOWCONFIG_FILE) if isfile(FLOWCONFIG_FILE) else {}
|
||||
)
|
||||
|
||||
def success(self, msg: str):
|
||||
self._print(TYPE_SUCCESS, msg)
|
||||
|
||||
@@ -145,7 +149,7 @@ class Command(ABC):
|
||||
raise GitFlowError("Ejecución cancelada.")
|
||||
|
||||
def ensure_repository_token(self):
|
||||
if not os.path.isfile(REPOSITORY_TOKEN_FILENAME):
|
||||
if not isfile(REPOSITORY_TOKEN_FILENAME):
|
||||
raise GitFlowError(
|
||||
"No existe un token para el repositorio en " + REPOSITORY_TOKEN_FILENAME
|
||||
)
|
||||
@@ -153,6 +157,23 @@ class Command(ABC):
|
||||
with open(REPOSITORY_TOKEN_FILENAME) as f:
|
||||
return f.readline().strip()
|
||||
|
||||
def ensure_clean_worktree(self, has_remote: bool):
|
||||
status = Git.status()
|
||||
|
||||
if not status:
|
||||
return
|
||||
|
||||
not_empty = any(map(lambda s: s.index != " " or s.worktree != " ", status))
|
||||
|
||||
if not_empty:
|
||||
if not has_remote:
|
||||
self.warning("Existen cambios en tu entorno de trabajo sin commitear.")
|
||||
else:
|
||||
raise GitFlowError("No se puede continuar con cambios pendientes.")
|
||||
|
||||
if not self.confirm("¿Desea continuar?", False):
|
||||
raise GitFlowError("Ejecución abortada")
|
||||
|
||||
def get_branch_env_and_type(self, branch: str) -> tuple[str, str]:
|
||||
components = branch.split("/")
|
||||
target_branches = self.flowconfig["flow.branches"].split(" ")
|
||||
|
||||
@@ -27,7 +27,7 @@ class MergeCommand(Command):
|
||||
self.run_local(base, branch, target)
|
||||
|
||||
def run_remote(self, remote: str, token: str, base: str, branch: str, target: str):
|
||||
self.check_pending_changes(True)
|
||||
self.ensure_clean_worktree(True)
|
||||
|
||||
Git("switch", target).exec(print="Cambiando a rama destino")
|
||||
Git("pull").exec(print="Obteniendo ultimos cambios del remoto")
|
||||
@@ -44,7 +44,7 @@ class MergeCommand(Command):
|
||||
self.create_pull_request(token, base, branch, target)
|
||||
|
||||
def run_local(self, base: str, branch: str, target: str):
|
||||
self.check_pending_changes(False)
|
||||
self.ensure_clean_worktree(False)
|
||||
self.check_merge_conflicts(target)
|
||||
self.show_commits_to_merge(base)
|
||||
|
||||
@@ -52,23 +52,6 @@ class MergeCommand(Command):
|
||||
Git("switch", target).exec(print="Cambiando a rama destino")
|
||||
Git("merge", branch, ff=False).exec(print="Mergeando")
|
||||
|
||||
def check_pending_changes(self, has_remote: bool):
|
||||
status = Git.status()
|
||||
|
||||
if not status:
|
||||
return
|
||||
|
||||
not_empty = any(map(lambda s: s.index != " " or s.worktree != " ", status))
|
||||
|
||||
if not_empty:
|
||||
if not has_remote:
|
||||
self.warning("Existen cambios en tu entorno de trabajo sin commitear.")
|
||||
else:
|
||||
raise GitFlowError("No se puede continuar con cambios pendientes.")
|
||||
|
||||
if not self.confirm("¿Desea continuar?", False):
|
||||
raise GitFlowError("Ejecución abortada")
|
||||
|
||||
def show_commits_to_merge(self, base):
|
||||
commits = Git("log", base + "..", format="%s").lines()
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
from argparse import Namespace
|
||||
from git_flow import COMMIT_TYPES, GitFlowError
|
||||
from git_flow.command.base import Command
|
||||
from git_flow.git import Git
|
||||
|
||||
|
||||
class ReleaseCommand(Command):
|
||||
def name(self) -> str:
|
||||
return "release"
|
||||
|
||||
def description(self) -> str:
|
||||
return """Realiza el merge de la rama, o crea el PR para hacerlo si tiene un remoto configurado"""
|
||||
|
||||
def run(self, args: Namespace = Namespace()):
|
||||
self.ensure_initialized()
|
||||
|
||||
branch = self.ensure_right_branch()
|
||||
envs = self.flowconfig["flow.branches"].split(",")
|
||||
(env, _) = self.get_branch_env_and_type(branch)
|
||||
|
||||
if env == envs[-1]:
|
||||
raise GitFlowError(
|
||||
"No se puede hacer release de una rama en el ultimo entorno."
|
||||
)
|
||||
|
||||
has_remote = "flow.remote" in self.flowconfig
|
||||
|
||||
self.ensure_clean_worktree(has_remote)
|
||||
|
||||
next_env = envs[envs.index(env) + 1]
|
||||
next_branch = (
|
||||
branch.replace(f"/{env}/", f"/{next_env}/")
|
||||
if branch.startswith("release/")
|
||||
else f"release/{next_env}/{branch}"
|
||||
)
|
||||
|
||||
if has_remote:
|
||||
Git("pull").exec(print="Sincronizando cambios de la rama")
|
||||
Git("switch", next_env).exec(print="Cambiando al siguiente entorno")
|
||||
Git("pull").exec(print="Sincronizando cambios del entorno")
|
||||
Git("switch", "-").exec(print="Volviendo a la rama original")
|
||||
|
||||
base = Git.get_first_fork_point(branch, env)
|
||||
commits = int(Git("rev-list", base + "..", count=True).firstline())
|
||||
|
||||
if commits > 1 and self.confirm(
|
||||
"Desea reemplazar los {commits} commits de la rama por uno solo?"
|
||||
):
|
||||
Git("switch", next_branch, base, create=True).exec(
|
||||
print="Creando rama release en base"
|
||||
)
|
||||
Git("merge", branch, squash=True).exec(
|
||||
print="Squasheando commits en uno solo"
|
||||
)
|
||||
|
||||
commit_type = self.choice("Tipo de commit", COMMIT_TYPES[:-1])
|
||||
commit_message = self.prompt(
|
||||
"Mensaje (máximo recomendado: 100 caracteres)", persistent=True
|
||||
)
|
||||
message = commit_type + ": " + commit_message
|
||||
Git("commit", m=message).exec(print="Creando commit único")
|
||||
else:
|
||||
Git("switch", next_branch, create=True).exec(print="Creando rama release")
|
||||
|
||||
Git("rebase", base, next_branch, onto=next_env).exec(
|
||||
print="Moviendo cambios hacia el siguiente ambiente"
|
||||
)
|
||||
Reference in New Issue
Block a user