From 003f0407fc5a301105a457f5ddc9773f1224cc97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Malmstr=C3=B6m?= Date: Mon, 4 May 2026 14:11:46 +0200 Subject: [PATCH] Fix missing `None` check in Remote.Save This code was causing an exception in cases where `pushUrl` was set but `projectname` was not. This can happen when `pushUrl` is set for the manifest repo which does not have a `projectname`. For example: repo init --manifest-url ssh://url.to/my/manifest cd .repo/manifests git config remote.origin.pushurl ssh://url.to/my/manifest repo init --manifest-url ssh://url.to/my/manifest --repo-rev main The last `repo init` invocation causes an error. Change-Id: Ibb68c8446880cfbac22feee595d1fd1b678c7ade Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/579162 Reviewed-by: Gavin Mak Commit-Queue: Josef Malmstrom Tested-by: Josef Malmstrom Reviewed-by: Mike Frysinger --- git_config.py | 5 ++++- tests/test_git_config.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/git_config.py b/git_config.py index d2c88514b..9b3188f1e 100644 --- a/git_config.py +++ b/git_config.py @@ -724,7 +724,10 @@ class Remote: def Save(self): """Save this remote to the configuration.""" self._Set("url", self.url) - if self.pushUrl is not None: + # projectname is initialized for projects listed in the manifest, but + # not for others (e.g. the manifest project). This class is used for + # all of them. + if self.pushUrl is not None and self.projectname is not None: self._Set("pushurl", self.pushUrl + "/" + self.projectname) else: self._Set("pushurl", self.pushUrl) diff --git a/tests/test_git_config.py b/tests/test_git_config.py index 2ece4c6e3..3bc140522 100644 --- a/tests/test_git_config.py +++ b/tests/test_git_config.py @@ -226,3 +226,21 @@ def test_get_sync_analysis_state_data(rw_config_file: Path) -> None: for key, value in TESTS: assert sync_data[f"{git_config.SYNC_STATE_PREFIX}{key}"] == value assert sync_data[f"{git_config.SYNC_STATE_PREFIX}main.synctime"] + + +def test_remote_save_with_push_url_without_projectname( + rw_config_file: Path, +) -> None: + """Test saving a remote pushUrl when projectname is unset.""" + config = git_config.GitConfig(str(rw_config_file)) + remote = config.GetRemote("origin") + remote.url = "https://example.com/repo" + remote.pushUrl = "ssh://example.com" + remote.projectname = None + + remote.Save() + + written_config = git_config.GitConfig(str(rw_config_file)) + assert ( + written_config.GetString("remote.origin.pushurl") == "ssh://example.com" + )