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" + )