104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
from argparse import Namespace
|
|
from git_flow import GitFlowError
|
|
from git_flow.changelog import Changelog
|
|
from git_flow.command.base import Command
|
|
from git_flow.git import Git
|
|
|
|
|
|
class MergeCommand(Command):
|
|
def name(self) -> str:
|
|
return "merge"
|
|
|
|
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()
|
|
(target, _) = self.get_branch_env_and_type(branch)
|
|
base = Git.get_first_fork_point(branch, target)
|
|
|
|
if "flow.remote" in self.flowconfig:
|
|
remote = self.flowconfig["flow.remote"]
|
|
token = self.ensure_repository_token()
|
|
self.run_remote(remote, token, base, branch, target)
|
|
else:
|
|
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)
|
|
|
|
Git("switch", target).exec()
|
|
Git("pull").exec()
|
|
Git("switch", "-").exec()
|
|
|
|
self.check_merge_conflicts(target)
|
|
self.show_commits_to_merge(base)
|
|
|
|
Git("push", remote, branch, set_upstream=True).exec()
|
|
|
|
self.create_pull_request(token, base, branch, target)
|
|
|
|
def run_local(self, base: str, branch: str, target: str):
|
|
self.check_pending_changes(False)
|
|
self.check_merge_conflicts(target)
|
|
self.show_commits_to_merge(base)
|
|
|
|
if self.confirm(f"Mergear rama '{target}' <= '{branch}'?"):
|
|
Git("switch", target).exec()
|
|
Git("merge", branch, ff=False).exec()
|
|
|
|
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()
|
|
|
|
if not commits:
|
|
raise GitFlowError("No hay cambios a mergear.")
|
|
|
|
print("Cambios a mergear:")
|
|
|
|
for commit in commits:
|
|
print("- " + commit)
|
|
|
|
def check_merge_conflicts(self, target: str):
|
|
merge_conflicts = Git("merge", target, ff=False, commit=False).code() != 0
|
|
|
|
if merge_conflicts:
|
|
Git("merge", abort=True).exec()
|
|
self.error(f"La rama actual tiene conflictos con {target}")
|
|
self.info(
|
|
f"""Se recomienda mergear {target} a la rama actual, resolver los conflictos localmente, y ejecutar nuevamente este comando"""
|
|
)
|
|
raise GitFlowError("Ejecución abortada.")
|
|
else:
|
|
self.success("No se detectaron merge conflicts.")
|
|
|
|
def create_pull_request(self, token: str, base: str, branch: str, target: str):
|
|
changelog = Changelog()
|
|
|
|
message = self.get_remote_api(token).create_pull_request(
|
|
branch,
|
|
target,
|
|
branch.replace("/", ": ").replace("-", " "),
|
|
changelog.generate_content(base, branch),
|
|
)
|
|
|
|
self.success(message)
|