From c00e43486c9cea62ea7d7f109433b1aeae7a97bb Mon Sep 17 00:00:00 2001 From: Christophe Triquet Date: Fri, 23 Feb 2024 11:20:06 +0100 Subject: [PATCH] Add remote update support --- nbgitpuller/pull.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/nbgitpuller/pull.py b/nbgitpuller/pull.py index f5a7dab1..415b1ffb 100644 --- a/nbgitpuller/pull.py +++ b/nbgitpuller/pull.py @@ -133,6 +133,30 @@ def resolve_default_branch(self): logging.exception(m) raise ValueError(m) + def check_and_update_remote(self): + """ + Checks the Git remote URL and update it if deprecated + + Only allows update of creds in URL e.g. access token. + Git repo must be unchanged. + """ + remote_url = subprocess.run( + ["git", "config", "remote.origin.url"], + cwd=self.repo_dir, + capture_output=True, + text=True, + check=True + ).stdout.strip() + + if ( + "@" in self.git_url and "@" in remote_url + and self.git_url.rsplit("@", 1)[0] != remote_url.rsplit("@", 1)[0] + ): + subprocess.run( + ["git", "remote", "set-url", "origin", self.git_url], + cwd=self.repo_dir + ) + def pull(self): """ Pull selected repo from a remote git repository, @@ -141,6 +165,7 @@ def pull(self): if not os.path.exists(self.repo_dir): yield from self.initialize_repo() else: + self.check_and_update_remote() yield from self.update() def initialize_repo(self):