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 <gavinmak@google.com>
Commit-Queue: Josef Malmstrom <Josef.Malmstrom@arm.com>
Tested-by: Josef Malmstrom <Josef.Malmstrom@arm.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
diff --git a/git_config.py b/git_config.py
index d2c8851..9b3188f 100644
--- a/git_config.py
+++ b/git_config.py
@@ -724,7 +724,10 @@
     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 2ece4c6..3bc1405 100644
--- a/tests/test_git_config.py
+++ b/tests/test_git_config.py
@@ -226,3 +226,21 @@
     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"
+    )