commit:     3b8442d6683cad9debe9fce554e2bd958188ab7c
Author:     Florian Schmaus <flow <AT> gentoo <DOT> org>
AuthorDate: Thu Oct 19 08:48:31 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Oct 22 18:58:57 2023 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=3b8442d6

sync/git: update git remote url with sync-uri when necessary

If the repo is not volatile, update the URL of the git remote if it is
not equal to the configured sync-uri.

Closes: https://bugs.gentoo.org/905869
Signed-off-by: Florian Schmaus <flow <AT> gentoo.org>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 NEWS                                |  3 +++
 lib/portage/sync/modules/git/git.py | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index ee33b0eb57..63b072f7ba 100644
--- a/NEWS
+++ b/NEWS
@@ -36,6 +36,9 @@ Bug fixes:
 
 * Eliminate unnecessary package reinstalls (bug #915494).
 
+* Update the git remote URL of an overlay with its configured sync-uri
+  when necessary (bug #905869).
+
 portage-3.0.52 (2023-10-03)
 --------------
 

diff --git a/lib/portage/sync/modules/git/git.py 
b/lib/portage/sync/modules/git/git.py
index 4b11e3fa85..a2c96e8098 100644
--- a/lib/portage/sync/modules/git/git.py
+++ b/lib/portage/sync/modules/git/git.py
@@ -294,9 +294,40 @@ class GitSync(NewBase):
                 writemsg_level(msg + "\n", level=logging.ERROR, noiselevel=-1)
                 return (exitcode, False)
 
+        git_remote = remote_branch.partition("/")[0]
+
+        if not self.repo.volatile:
+            git_get_remote_url_cmd = ["git", "ls-remote", "--get-url", 
git_remote]
+            git_remote_url = portage._unicode_decode(
+                subprocess.check_output(
+                    git_get_remote_url_cmd,
+                    cwd=portage._unicode_encode(self.repo.location),
+                )
+            ).strip()
+            if git_remote_url != self.repo.sync_uri:
+                git_set_remote_url_cmd = [
+                    "git",
+                    "remote",
+                    "set-url",
+                    git_remote,
+                    self.repo.sync_uri,
+                ]
+                exitcode = portage.process.spawn(
+                    git_set_remote_url_cmd,
+                    cwd=portage._unicode_encode(self.repo.location),
+                    **self.spawn_kwargs,
+                )
+                if exitcode != os.EX_OK:
+                    msg = f"!!! could not update git remote {git_remote}'s url 
to {self.repo.sync_uri}"
+                    self.logger(self.xterm_titles, msg)
+                    writemsg_level(msg + "\n", level=logging.ERROR, 
noiselevel=-1)
+                    return (exitcode, False)
+                elif not quiet:
+                    writemsg_level(" ".join(git_set_remote_url_cmd) + "\n")
+
         git_cmd = "{} fetch {}{}".format(
             self.bin_command,
-            remote_branch.partition("/")[0],
+            git_remote,
             git_cmd_opts,
         )
 

Reply via email to