@@ -528,6 +528,47 @@ def execute_for_stack(self, stack: Stack, aws_env: Session | None = None) -> int
528528 return 1
529529 return 0
530530
531+ def _check_repository_state (self ) -> int :
532+ """Validate the repository state before a deploy.
533+
534+ :return: 0 if deploy can proceed, 1 otherwise
535+ """
536+ repo = GitRepository ("." )
537+
538+ error = "Failed to get the current branch"
539+ try :
540+ branch = repo .git_cmd (["branch" , "--show-current" ], output = PIPE ).out .strip ()
541+
542+ # Check we are on the correct branch
543+ if self .deploy_branch is not None and self .deploy_branch != branch :
544+ print (f"Can only deploy from branch { self .deploy_branch } " )
545+ return 1
546+
547+ # Check there are no local changes
548+ error = "Failed to check local changes"
549+ changes = repo .git_cmd (["status" , "-s" ], output = PIPE ).out .strip ()
550+ if changes != "" :
551+ print (
552+ "Can only deploy from a clean repository, ensure you have "
553+ "no modified files"
554+ )
555+ return 1
556+
557+ # Check the branch is up to date
558+ error = f"Failed to fetch { branch } "
559+ fetch_out = repo .git_cmd (
560+ ["fetch" , "origin" , branch , "--dry-run" ], output = PIPE
561+ ).out
562+ # Check if there is a line indicating a commit
563+ if re .search (rf"{ branch } \s*\-\>\s*origin\/" , fetch_out ):
564+ print ("Can only deploy from up to date branch, please do a git pull" )
565+ return 1
566+ except Exception :
567+ logger .exception (error )
568+ return 1
569+
570+ return 0
571+
531572 def execute (
532573 self ,
533574 args : list [str ] | None = None ,
@@ -549,50 +590,9 @@ def execute(
549590 os .environ .get ("CI" ) != "true"
550591 and self .args .command in ("push" , "update" )
551592 and not self .args .dry_run
593+ and (repository_state_status := self ._check_repository_state ())
552594 ):
553- repo = GitRepository ("." )
554-
555- # Retrieve the current branch
556- try :
557- branch = repo .git_cmd (
558- ["branch" , "--show-current" ], output = PIPE
559- ).out .strip ()
560- except Exception :
561- logger .exception ("Failed to get the current branch" )
562- return 1
563-
564- # Check we are on the correct branch
565- if self .deploy_branch is not None and self .deploy_branch != branch :
566- print (f"Can only deploy from branch { self .deploy_branch } " )
567- return 1
568-
569- # Check there are no local changes
570- try :
571- changes = repo .git_cmd (["status" , "-s" ], output = PIPE ).out .strip ()
572- if changes != "" :
573- print (
574- "Can only deploy from a clean repository, ensure you have "
575- "no modified files"
576- )
577- return 1
578- except Exception :
579- logger .exception ("Failed to check local changes" )
580- return 1
581-
582- # Check the branch is up to date
583- try :
584- fetch_out = repo .git_cmd (
585- ["fetch" , "origin" , branch , "--dry-run" ], output = PIPE
586- ).out
587- # Check if there is a line indicating a commit
588- if re .search (rf"{ branch } \s*\-\>\s*origin\/" , fetch_out ):
589- print (
590- "Can only deploy from up to date branch, please do a git pull"
591- )
592- return 1
593- except Exception :
594- logger .exception (f"Failed to fetch { branch } " )
595- return 1
595+ return repository_state_status
596596
597597 return_val = 0
598598 stacks = self .create_stack ()
0 commit comments