Skip to content

Commit f5dbb0e

Browse files
CopilotswissspidyCopilot
authored
Auto-append .git suffix for GitHub/GitLab URLs missing it (#225)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent d662e02 commit f5dbb0e

2 files changed

Lines changed: 122 additions & 1 deletion

File tree

features/package-install.feature

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,102 @@ Feature: Install WP-CLI packages
298298
Success: Uninstalled package.
299299
"""
300300

301+
@github-api
302+
Scenario: Install a package from a Git URL without .git suffix
303+
Given an empty directory
304+
305+
When I run `wp package install https://github.com/wp-cli/google-sitemap-generator-cli`
306+
Then STDOUT should contain:
307+
"""
308+
Installing package wp-cli/google-sitemap-generator-cli
309+
"""
310+
And STDOUT should contain:
311+
"""
312+
Registering https://github.com/wp-cli/google-sitemap-generator-cli.git as a VCS repository...
313+
"""
314+
And STDOUT should contain:
315+
"""
316+
Success: Package installed.
317+
"""
318+
319+
When I run `wp package uninstall wp-cli/google-sitemap-generator-cli`
320+
Then STDOUT should contain:
321+
"""
322+
Success: Uninstalled package.
323+
"""
324+
325+
@github-api
326+
Scenario: Install a package from a Git URL without .git suffix but with a version suffix
327+
Given an empty directory
328+
329+
When I run `wp package install https://github.com/wp-cli/google-sitemap-generator-cli:dev-main`
330+
Then STDOUT should contain:
331+
"""
332+
Installing package wp-cli/google-sitemap-generator-cli (dev-main)
333+
"""
334+
And STDOUT should contain:
335+
"""
336+
Registering https://github.com/wp-cli/google-sitemap-generator-cli.git as a VCS repository...
337+
"""
338+
And STDOUT should contain:
339+
"""
340+
Success: Package installed.
341+
"""
342+
343+
When I run `wp package uninstall wp-cli/google-sitemap-generator-cli`
344+
Then STDOUT should contain:
345+
"""
346+
Success: Uninstalled package.
347+
"""
348+
349+
@github-api
350+
Scenario: Install a package from a GitHub SSH URL without .git suffix
351+
Given an empty directory
352+
353+
When I run `wp package install git@github.com:wp-cli/google-sitemap-generator-cli`
354+
Then STDOUT should contain:
355+
"""
356+
Installing package wp-cli/google-sitemap-generator-cli
357+
"""
358+
And STDOUT should contain:
359+
"""
360+
Registering git@github.com:wp-cli/google-sitemap-generator-cli.git as a VCS repository...
361+
"""
362+
And STDOUT should contain:
363+
"""
364+
Success: Package installed.
365+
"""
366+
367+
When I run `wp package uninstall wp-cli/google-sitemap-generator-cli`
368+
Then STDOUT should contain:
369+
"""
370+
Success: Uninstalled package.
371+
"""
372+
373+
@gitlab-api
374+
Scenario: Install a package from a GitLab URL without .git suffix and nested groups
375+
Given an empty directory
376+
377+
When I run `wp package install https://gitlab.com/wp-cli/wp-cli-test/test-command`
378+
Then STDOUT should contain:
379+
"""
380+
Installing package wp-cli-test/test-command
381+
"""
382+
And STDOUT should contain:
383+
"""
384+
Registering https://gitlab.com/wp-cli/wp-cli-test/test-command.git as a VCS repository...
385+
"""
386+
And STDOUT should contain:
387+
"""
388+
Success: Package installed.
389+
"""
390+
391+
When I run `wp package uninstall wp-cli-test/test-command`
392+
Then STDOUT should contain:
393+
"""
394+
Success: Uninstalled package.
395+
"""
396+
301397
@github-api
302398
Scenario: Install a package from a Git URL with mixed-case git name but lowercase composer.json name
303399
Given an empty directory

src/Package_Command.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,20 @@ public function install( $args, $assoc_args ) {
222222
$git_package = false;
223223
$dir_package = false;
224224
$version = '';
225+
// Append .git suffix for GitHub/GitLab URLs that are missing it.
226+
$package_name = $this->maybe_add_git_suffix( $package_name );
225227
// Parse version suffix from a git URL (e.g. https://github.com/vendor/package.git:dev-main).
226228
if ( preg_match( '#^(.+\.git):([^:]+)$#', $package_name, $url_version_matches ) ) {
227229
$package_name = $url_version_matches[1];
228230
$version = $url_version_matches[2];
229231
}
230232
if ( $this->is_git_repository( $package_name ) ) {
231233
if ( '' === $version ) {
232-
$version = "dev-{$this->get_github_default_branch( $package_name, $insecure )}";
234+
if ( preg_match( '#^(?:https?://github\.com/|git@github\.com:)#i', $package_name ) ) {
235+
$version = "dev-{$this->get_github_default_branch( $package_name, $insecure )}";
236+
} else {
237+
$version = 'dev-master';
238+
}
233239
}
234240
$git_package = $package_name;
235241
$matches = [];
@@ -1296,6 +1302,25 @@ private function is_git_repository( $package ) {
12961302
return '.git' === strtolower( substr( $package, -4, 4 ) );
12971303
}
12981304

1305+
/**
1306+
* Appends the .git suffix to GitHub or GitLab URLs that are missing it.
1307+
*
1308+
* @param string $package_name Package name or URL to normalize.
1309+
* @return string Normalized package name or URL.
1310+
*/
1311+
private function maybe_add_git_suffix( $package_name ) {
1312+
// Already has .git suffix (possibly followed by :version).
1313+
if ( preg_match( '#\.git(?::[^:]+)?$#i', $package_name ) ) {
1314+
return $package_name;
1315+
}
1316+
// Append .git for GitHub/GitLab HTTPS or SSH URLs, preserving any :version suffix.
1317+
// Pattern: (https?://github.com/<user>/<repo>[...subgroups...] or git@github.com:<user>/<repo>[...subgroups...])(:version)?
1318+
if ( preg_match( '#^((?:https?://(?:github|gitlab)\.com/|git@(?:github|gitlab)\.com:)[^/:]+(?:/[^/:]+)*)(:.*)?$#i', $package_name, $matches ) ) {
1319+
return $matches[1] . '.git' . ( $matches[2] ?? '' );
1320+
}
1321+
return $package_name;
1322+
}
1323+
12991324
/**
13001325
* Checks that `$package_name` matches the name in composer.json at Github.com, and return corrected value if not.
13011326
*

0 commit comments

Comments
 (0)