Repository: VirtusLab/git-machete
Pull request #1394 (2025-02-04 16:08:45)

Allow for any user (not just `git`) in SSH URLs for GitHub/GitLab


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index df2e1ff..cac3d09 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -2,6 +2,8 @@
 
 ## New in git-machete 3.32.1
 
+- fixed: `git machete github` and `gitlab` recognize SSH URLs with any user before `@`, not just `git`
+
 ## New in git-machete 3.32.0
 
 - added: flags `-H`/`--sync-github-prs` and `-L`/`--sync-gitlab-mrs` to `traverse` to suggest retargeting PRs/MRs when traversing (suggested by @chriscz)
diff --git a/docs/generate_py_docs.py b/docs/generate_py_docs.py
index de15be4..1ce32ce 100644
--- a/docs/generate_py_docs.py
+++ b/docs/generate_py_docs.py
@@ -28,7 +28,7 @@ def html2txt(html_: str):
         tag.decompose()
 
     # remove comments
-    for comment in html_elements.findAll(string=lambda string: isinstance(string, Comment)):
+    for comment in html_elements.find_all(string=lambda string: isinstance(string, Comment)):
         comment.extract()
 
     # remove non-breaking spaces
diff --git a/docs/man/git-machete.1 b/docs/man/git-machete.1
index 6d8f8c0..43900bf 100644
--- a/docs/man/git-machete.1
+++ b/docs/man/git-machete.1
@@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
 .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
 ..
-.TH "GIT-MACHETE" "1" "Jan 17, 2025" "" "git-machete"
+.TH "GIT-MACHETE" "1" "Feb 04, 2025" "" "git-machete"
 .SH NAME
 git-machete \- git-machete 3.32.1
 .sp
diff --git a/git_machete/code_hosting.py b/git_machete/code_hosting.py
index 120ad82..16815a0 100644
--- a/git_machete/code_hosting.py
+++ b/git_machete/code_hosting.py
@@ -128,11 +128,14 @@ def remote_url_patterns(domain: str) -> List[str]:
     # Note that these regexes work for both GitLab and GitHub.
     # The difference is only that the organization (or rather, "namespace") in GitLab might contain multiple `/`-separated segments.
     domain_regex = re.escape(domain)
+    org_repo_regex = "(.+)/([^/]+)"
     return [
-        f"^https://.*@{domain_regex}/(.*)/([^/]*)$",
-        f"^https://{domain_regex}/(.*)/([^/]*)$",
-        f"^git@{domain_regex}:(.*)/([^/]*)$",
-        f"^ssh://git@{domain_regex}/(.*)/([^/]*)$"
+        # (?:...) is a non-capturing group
+        f"^https://(?:.+@)?{domain_regex}/{org_repo_regex}$",
+        # A very rare way to express SSH URL
+        f"^ssh://.+@{domain_regex}/{org_repo_regex}$",
+        # The below is way more common for SSH; the user before `@` is typically called `git`, but doesn't need to be so
+        f"^[^:/]+@{domain_regex}:{org_repo_regex}$",
     ]
 
 


Test patch:
diff --git a/tests/test_github.py b/tests/test_github.py
index 0c8dfe4..4b99249 100644
--- a/tests/test_github.py
+++ b/tests/test_github.py
@@ -36,13 +36,13 @@ class TestGitHub(BaseTest):
         repository = 'repo_sandbox'
         urls = [f'https://tester@github.com/{organization}/{repository}',
                 f'https://github.com/{organization}/{repository}',
-                f'git@github.com:{organization}/{repository}',
+                f'foo-1@github.com:{organization}/{repository}',
                 f'ssh://git@github.com/{organization}/{repository}']
         urls = urls + [url + '.git' for url in urls]
 
         for url in urls:
             org_and_repo = OrganizationAndRepository.from_url(domain=GitHubClient.DEFAULT_GITHUB_DOMAIN, url=url)
-            assert org_and_repo is not None
+            assert org_and_repo is not None, f"for {url}"
             assert org_and_repo.organization == organization
             assert org_and_repo.repository == repository
 
diff --git a/tests/test_gitlab.py b/tests/test_gitlab.py
index 99c3422..9e51b87 100644
--- a/tests/test_gitlab.py
+++ b/tests/test_gitlab.py
@@ -36,7 +36,7 @@ class TestGitLab(BaseTest):
         urls = [f'https://tester@gitlab.com/{organization}/{repository}',
                 f'https://gitlab.com/{organization}/{repository}',
                 f'git@gitlab.com:{organization}/{repository}',
-                f'ssh://git@gitlab.com/{organization}/{repository}']
+                f'ssh://foo-3@gitlab.com/{organization}/{repository}']
         urls = urls + [url + '.git' for url in urls]
 
         for url in urls: