diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-17 08:05:42 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-17 08:05:42 +0900 |
| commit | 79b504e55cd4c4d1da102c3a076dc2a2e1edcd65 (patch) | |
| tree | 2c12198742f289de49b9b77a2c2ad66ef417405c /crates/shirabe/src/util/git.rs | |
| parent | 9fd6aecad27240ccedab4487f6c157914142ca47 (diff) | |
| download | php-shirabe-79b504e55cd4c4d1da102c3a076dc2a2e1edcd65.tar.gz php-shirabe-79b504e55cd4c4d1da102c3a076dc2a2e1edcd65.tar.zst php-shirabe-79b504e55cd4c4d1da102c3a076dc2a2e1edcd65.zip | |
refactor(pcre): return the Preg $matches instead of filling an out-param
`Composer\Pcre\Preg` fills `$matches` through a by-ref parameter, and the
port mirrored that with a `&mut` (or `Option<&mut>`) out-param plus a
bool or count return. Callers had to declare an empty map one line ahead
of the call, and the type never said the map is only meaningful when the
call matched. Return the matches instead:
- match3/match4/is_match3/is_match4 -> Option<PregMatchedGroups>
- is_match_named -> Option<PregNamedGroups>
- match_all2/is_match_all -> PregMatchesAll
- is_match_all_with_offsets3 -> PregMatchesAllWithOffsets
Nothing is lost: the bool is `Option::is_some()`, and the occurrence
count is the length of any one column of a PREG_PATTERN_ORDER map, now
spelled `PregMatchesAll::occurrence_count()`. is_match() still answers
the bool question directly for callers that want no groups.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/util/git.rs')
| -rw-r--r-- | crates/shirabe/src/util/git.rs | 94 |
1 files changed, 37 insertions, 57 deletions
diff --git a/crates/shirabe/src/util/git.rs b/crates/shirabe/src/util/git.rs index 0f463aea..2b012d4c 100644 --- a/crates/shirabe/src/util/git.rs +++ b/crates/shirabe/src/util/git.rs @@ -226,11 +226,9 @@ impl Git { &mut output, cwd, )?; - let mut m = PregMatchedGroups::new(); - if Preg::is_match3( + if let Some(m) = Preg::is_match3( php_regex!(r"{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im"), &output, - Some(&mut m), ) { let m3 = m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(); if !self.io.has_authentication(&m3) { @@ -248,14 +246,12 @@ impl Git { let protocols = self.config.borrow_mut().get("github-protocols"); // public github, autoswitch protocols // @phpstan-ignore composerPcre.maybeUnsafeStrictGroups - let mut m = PregMatchedGroups::new(); - if Preg::is_match3( + if let Some(m) = Preg::is_match3( format!( "{{^(?:https?|git)://{}/(.*)}}", Self::get_github_domains_regex(&self.config.borrow()) ), url, - Some(&mut m), ) { let mut messages: Vec<String> = vec![]; let protocols_list: Vec<String> = match &protocols { @@ -344,23 +340,23 @@ impl Git { let mut error_msg = self.process.borrow().get_error_output().to_string(); // private github repository without ssh key access, try https with auth // @phpstan-ignore composerPcre.maybeUnsafeStrictGroups - let mut m = PregMatchedGroups::new(); let github_matched = Preg::is_match3( format!( "{{^git@{}:(.+?)\\.git$}}i", Self::get_github_domains_regex(&self.config.borrow()) ), url, - Some(&mut m), - ) || Preg::is_match3( - format!( - "{{^https?://{}/(.*?)(?:\\.git)?$}}i", - Self::get_github_domains_regex(&self.config.borrow()) - ), - url, - Some(&mut m), - ); - if github_matched { + ) + .or_else(|| { + Preg::is_match3( + format!( + "{{^https?://{}/(.*?)(?:\\.git)?$}}i", + Self::get_github_domains_regex(&self.config.borrow()) + ), + url, + ) + }); + if let Some(m) = github_matched { let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); let m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); if !self.io.has_authentication(&m1) { @@ -410,15 +406,12 @@ impl Git { credentials = vec![rawurlencode(&username), rawurlencode(&password)]; error_msg = self.process.borrow().get_error_output().to_string(); } - } else if Preg::is_match3( + } else if let Some(m) = Preg::is_match3( php_regex!(r"{^(https?)://(bitbucket\.org)/(.*?)(?:\.git)?$}i"), url, - Some(&mut m), - ) || Preg::is_match3( - php_regex!(r"{^(git)@(bitbucket\.org):(.+?\.git)$}i"), - url, - Some(&mut m), - ) { + ) + .or_else(|| Preg::is_match3(php_regex!(r"{^(git)@(bitbucket\.org):(.+?\.git)$}i"), url)) + { // bitbucket either through oauth or app password, with fallback to ssh. let mut bitbucket_util = Bitbucket::new( self.io.clone(), @@ -556,21 +549,22 @@ impl Git { } error_msg = self.process.borrow().get_error_output().to_string(); - } else if Preg::is_match3( + } else if let Some(m) = Preg::is_match3( format!( "{{^(git)@{}:(.+?\\.git)$}}i", Self::get_gitlab_domains_regex(&self.config.borrow()) ), url, - Some(&mut m), - ) || Preg::is_match3( - format!( - "{{^(https?)://{}/(.*)}}i", - Self::get_gitlab_domains_regex(&self.config.borrow()) - ), - url, - Some(&mut m), - ) { + ) + .or_else(|| { + Preg::is_match3( + format!( + "{{^(https?)://{}/(.*)}}i", + Self::get_gitlab_domains_regex(&self.config.borrow()) + ), + url, + ) + }) { let mut m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); let m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); let m3 = m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(); @@ -1090,14 +1084,7 @@ impl Git { } fn get_authentication_failure(&self, url: &str) -> Option<PregMatchedGroups> { - let mut m = PregMatchedGroups::new(); - if !Preg::is_match3( - php_regex!(r"{^(https?://)([^/]+)(.*)$}i"), - url, - Some(&mut m), - ) { - return None; - } + let m = Preg::is_match3(php_regex!(r"{^(https?://)([^/]+)(.*)$}i"), url)?; let auth_failures = [ "fatal: Authentication failed", @@ -1179,12 +1166,9 @@ impl Git { .borrow() .split_lines(output_mixed.as_string().unwrap_or("")); for line in lines { - let mut matches = PregMatchedGroups::new(); - if Preg::is_match3( - php_regex!(r"{^\s*HEAD branch:\s(.+)\s*$}m"), - &line, - Some(&mut matches), - ) { + if let Some(matches) = + Preg::is_match3(php_regex!(r"{^\s*HEAD branch:\s(.+)\s*$}m"), &line) + { return Ok(Some( matches .get(&CaptureKey::ByIndex(1)) @@ -1307,15 +1291,11 @@ impl Git { &mut output, Option::<&str>::None, ); - if exit_code == 0 { - let mut matches = PregMatchedGroups::new(); - if Preg::is_match3( - php_regex!(r"/^git version (\d+(?:\.\d+)+)/m"), - &output, - Some(&mut matches), - ) { - *version = Some(matches.get(&CaptureKey::ByIndex(1)).cloned()); - } + if exit_code == 0 + && let Some(matches) = + Preg::is_match3(php_regex!(r"/^git version (\d+(?:\.\d+)+)/m"), &output) + { + *version = Some(matches.get(&CaptureKey::ByIndex(1)).cloned()); } } version.clone().unwrap_or(None) |
