diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
| commit | 0caac63bacefb9a1f62848636d47fca07f592bba (patch) | |
| tree | 8d2ef66e597a8d8c228d3ba2490a59f48b26db4d /crates/shirabe/src/util/git.rs | |
| parent | 34c74255d781ad0a0bf7cc5ad4ec1761bef61e04 (diff) | |
| download | php-shirabe-0caac63bacefb9a1f62848636d47fca07f592bba.tar.gz php-shirabe-0caac63bacefb9a1f62848636d47fca07f592bba.tar.zst php-shirabe-0caac63bacefb9a1f62848636d47fca07f592bba.zip | |
refactor(preg): split PregMatches reads into get() and name()
PregMatches keyed both forms of a capture group through CaptureKey, so
every read built one: a usize wrapped in an enum, or worse, a String
allocated to name a group that regex::Captures can look up from a &str.
It now mirrors regex::Captures instead -- get() takes the group number,
name() the group name -- and the enum drops out of the type entirely.
That is 285 call sites across 59 files, and the named ones carry most of
the win: `matches.get(&CaptureKey::ByName("host".to_string()))` reads as
`matches.name("host")`. ProcessExecutor loses a `user_key` binding that
existed only to build the key once.
CaptureKey stays as the key type of PregMatchesAll and
PregMatchesAllWithOffsets, where numbered and named entries share one
IndexMap and a key type is the point. Five files still name it.
Also retargets the two preg_match_all comments that described the
occurrence count through `matches[&CaptureKey::ByIndex(0)].len()`, an
Index impl these types no longer carry.
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 | 87 |
1 files changed, 19 insertions, 68 deletions
diff --git a/crates/shirabe/src/util/git.rs b/crates/shirabe/src/util/git.rs index e7239987..e14ff05f 100644 --- a/crates/shirabe/src/util/git.rs +++ b/crates/shirabe/src/util/git.rs @@ -14,7 +14,7 @@ use crate::util::ProcessExecutor; use crate::util::Url; use crate::util::{AuthHelper, StoreAuth}; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatches}; +use shirabe_pcre::{Preg, PregMatches}; use shirabe_php_shim::{ AnyThrowable, CmpOp, InvalidArgumentException, PHP_EOL, PhpMixed, RuntimeException, array_map, clearstatcache, explode, implode, in_array_loose, in_array_strict, is_dir, php_regex, @@ -230,17 +230,12 @@ impl Git { php_regex!(r"{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im"), &output, ) { - let m3 = m - .get(&CaptureKey::ByIndex(3)) - .unwrap_or_default() - .to_string(); + let m3 = m.get(3).unwrap_or_default().to_string(); if !self.io.has_authentication(&m3) { self.io.borrow_mut().set_authentication( m3, - rawurldecode(m.get(&CaptureKey::ByIndex(1)).unwrap_or_default()), - Some(rawurldecode( - m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), - )), + rawurldecode(m.get(1).unwrap_or_default()), + Some(rawurldecode(m.get(2).unwrap_or_default())), ); } } @@ -265,14 +260,8 @@ impl Git { _ => vec![], }; for protocol in &protocols_list { - let m1 = m - .get(&CaptureKey::ByIndex(1)) - .unwrap_or_default() - .to_string(); - let m2 = m - .get(&CaptureKey::ByIndex(2)) - .unwrap_or_default() - .to_string(); + let m1 = m.get(1).unwrap_or_default().to_string(); + let m2 = m.get(2).unwrap_or_default().to_string(); let proto_url = if protocol == "ssh" { format!("git@{}:{}", m1, m2) } else { @@ -300,10 +289,7 @@ impl Git { } // failed to checkout, first check git accessibility - let m1 = m - .get(&CaptureKey::ByIndex(1)) - .unwrap_or_default() - .to_string(); + let m1 = m.get(1).unwrap_or_default().to_string(); if !self.io.has_authentication(&m1) && !self.io.is_interactive() { self.throw_exception( &format!( @@ -369,14 +355,8 @@ impl Git { ) }); if let Some(m) = github_matched { - let m1 = m - .get(&CaptureKey::ByIndex(1)) - .unwrap_or_default() - .to_string(); - let m2 = m - .get(&CaptureKey::ByIndex(2)) - .unwrap_or_default() - .to_string(); + let m1 = m.get(1).unwrap_or_default().to_string(); + let m2 = m.get(2).unwrap_or_default().to_string(); if !self.io.has_authentication(&m1) { let mut git_hub_util = GitHub::new( self.io.clone(), @@ -439,14 +419,8 @@ impl Git { None, )?; - let domain = m - .get(&CaptureKey::ByIndex(2)) - .unwrap_or_default() - .to_string(); - let mut repo_with_git_part = m - .get(&CaptureKey::ByIndex(3)) - .unwrap_or_default() - .to_string(); + let domain = m.get(2).unwrap_or_default().to_string(); + let mut repo_with_git_part = m.get(3).unwrap_or_default().to_string(); if !repo_with_git_part.ends_with(".git") { repo_with_git_part.push_str(".git"); } @@ -588,18 +562,9 @@ impl Git { url, ) }) { - let mut m1 = m - .get(&CaptureKey::ByIndex(1)) - .unwrap_or_default() - .to_string(); - let m2 = m - .get(&CaptureKey::ByIndex(2)) - .unwrap_or_default() - .to_string(); - let m3 = m - .get(&CaptureKey::ByIndex(3)) - .unwrap_or_default() - .to_string(); + let mut m1 = m.get(1).unwrap_or_default().to_string(); + let m2 = m.get(2).unwrap_or_default().to_string(); + let m3 = m.get(3).unwrap_or_default().to_string(); if m1 == "git" { m1 = "https".to_string(); } @@ -673,18 +638,9 @@ impl Git { } } else if let Some(m) = self.get_authentication_failure(url) { // private non-github/gitlab/bitbucket repo that failed to authenticate - let m1 = m - .get(&CaptureKey::ByIndex(1)) - .unwrap_or_default() - .to_string(); - let mut m2 = m - .get(&CaptureKey::ByIndex(2)) - .unwrap_or_default() - .to_string(); - let m3 = m - .get(&CaptureKey::ByIndex(3)) - .unwrap_or_default() - .to_string(); + let m1 = m.get(1).unwrap_or_default().to_string(); + let mut m2 = m.get(2).unwrap_or_default().to_string(); + let m3 = m.get(3).unwrap_or_default().to_string(); let mut auth_parts: Option<String> = None; if m2.contains("@") { let parts = explode("@", &m2); @@ -1210,12 +1166,7 @@ impl Git { 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)) - .unwrap_or_default() - .to_string(), - )); + return Ok(Some(matches.get(1).unwrap_or_default().to_string())); } } @@ -1336,7 +1287,7 @@ impl Git { && let Some(matches) = Preg::is_match3(php_regex!(r"/^git version (\d+(?:\.\d+)+)/m"), &output) { - *version = Some(matches.get(&CaptureKey::ByIndex(1)).map(str::to_string)); + *version = Some(matches.get(1).map(str::to_string)); } } version.clone().unwrap_or(None) |
