소스 검색

ap_git: add method to bulk add remotes

Shiv Tyagi 1 년 전
부모
커밋
9d2f335a25
1개의 변경된 파일29개의 추가작업 그리고 0개의 파일을 삭제
  1. 29 0
      ap_git/core.py

+ 29 - 0
ap_git/core.py

@@ -515,6 +515,35 @@ class GitRepo:
         logger.debug(f"Running {' '.join(cmd)}")
         subprocess.run(cmd, cwd=self.__local_path, shell=False, check=True)
 
+    def remote_add_bulk(self, remotes: tuple, force: bool = False) -> None:
+        """
+        Add multiple remotes to the Git repository at once.
+
+        Parameters:
+            remotes (tuple): Tuple of tuples containing remote name
+            and url.
+            E.g. (
+                    ('remote1', 'https://remote1_url'),
+                    ('remote2', 'https://remote2_url'),
+                )
+            force (bool): Force update the url if remote already exists.
+
+        Raises:
+            DuplicateRemoteError: If remote already exists and
+            overwrite is not allowed.
+        """
+        logger.info(f"Remotes to add: {remotes}.")
+        for (remote, url) in remotes:
+            try:
+                self.remote_add(remote=remote, url=url)
+            except ex.DuplicateRemoteError:
+                if not force:
+                    raise
+
+                logger.info(f"Remote {remote} already exists. Updating url.")
+                self.remote_set_url(remote=remote, url=url)
+            logger.info(f"Remote {remote} added to repo with url {url}.")
+
     @staticmethod
     def clone(source: str,
               dest: str,