Jelajahi Sumber

ap_git: add remote containing commit to copied repo on shallow clone

Shiv Tyagi 1 tahun lalu
induk
melakukan
3cbf96a52b
1 mengubah file dengan 35 tambahan dan 0 penghapusan
  1. 35 0
      ap_git/core.py

+ 35 - 0
ap_git/core.py

@@ -204,6 +204,37 @@ class GitRepo:
         logger.debug(f"Running {' '.join(cmd)}")
         subprocess.run(cmd, cwd=self.__local_path, check=True)
 
+    def remote_get_url(self, remote: str) -> str:
+        """
+        Get the URL for a specific remote.
+
+        Parameters:
+            remote (str): Name of the remote
+
+        Returns:
+            str: The URL associated with the remote
+
+        Raises:
+            ValueError: If remote is None
+        """
+        if remote is None:
+            raise ValueError("remote is required, cannot be None.")
+
+        cmd = ['git', 'remote', 'get-url', remote]
+        logger.debug(f"Running {' '.join(cmd)}")
+
+        # Capture the output of the command
+        result = subprocess.run(
+            cmd,
+            cwd=self.__local_path,
+            check=True,
+            capture_output=True,
+            text=True
+        )
+
+        # Return the URL from the output
+        return result.stdout.strip()
+
     def fetch_remote(self, remote: str, force: bool = False,
                      tags: bool = False, recurse_submodules: bool = False,
                      refetch: bool = False) -> None:
@@ -579,6 +610,10 @@ class GitRepo:
             shallow_submodules=True
         )
 
+        # add the remote containing the commit in cloned repo for reference
+        url = source_repo.remote_get_url(remote=remote)
+        cloned_repo.remote_add(remote=remote, url=url)
+
         # delete the temporary branch in source repository
         # after the clone operation
         source_repo.__branch_delete(branch_name=temp_branch_name, force=True)