diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-31 17:14:25 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-31 17:14:25 +0100 |
| commit | 1180f0b3c3157b6fd8eb7bf08a255a114e961e5f (patch) | |
| tree | 8bb6bd9a0382e1395b4ef3b124e98461cf9c8683 /src/git_remote_helper.rs | |
| parent | 3acdeabfc3ab55d3e92d76d92d8ab6ad0383dd09 (diff) | |
fix(remote): updating `push` state event
ensure refs are included in state event
use `HashMap` to improve `RepoState` struct
Diffstat (limited to 'src/git_remote_helper.rs')
| -rw-r--r-- | src/git_remote_helper.rs | 40 |
1 files changed, 12 insertions, 28 deletions
diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs index 68aa681..08a59c1 100644 --- a/src/git_remote_helper.rs +++ b/src/git_remote_helper.rs | |||
| @@ -6,7 +6,7 @@ | |||
| 6 | 6 | ||
| 7 | use core::str; | 7 | use core::str; |
| 8 | use std::{ | 8 | use std::{ |
| 9 | collections::HashSet, | 9 | collections::{HashMap, HashSet}, |
| 10 | env, | 10 | env, |
| 11 | io::{self, Stdin}, | 11 | io::{self, Stdin}, |
| 12 | path::PathBuf, | 12 | path::PathBuf, |
| @@ -261,41 +261,25 @@ async fn generate_updated_state( | |||
| 261 | git_repo: &Repo, | 261 | git_repo: &Repo, |
| 262 | repo_ref: &RepoRef, | 262 | repo_ref: &RepoRef, |
| 263 | refspecs: &Vec<String>, | 263 | refspecs: &Vec<String>, |
| 264 | ) -> Result<Vec<(String, String)>> { | 264 | ) -> Result<HashMap<String, String>> { |
| 265 | let new_state = { | 265 | let new_state = { |
| 266 | if let Ok(mut repo_state) = get_state_from_cache(git_repo.get_path()?, repo_ref).await { | 266 | if let Ok(mut repo_state) = get_state_from_cache(git_repo.get_path()?, repo_ref).await { |
| 267 | for refspec in refspecs { | 267 | for refspec in refspecs { |
| 268 | let (from, to) = refspec_to_from_to(refspec)?; | 268 | let (from, to) = refspec_to_from_to(refspec)?; |
| 269 | if to.is_empty() { | 269 | if from.is_empty() { |
| 270 | // delete | 270 | // delete |
| 271 | repo_state.state.retain(|(name, _)| !name.eq(to)); | 271 | repo_state.state.remove(to); |
| 272 | } else if repo_state.state.iter().any(|(name, _)| name.eq(from)) { | ||
| 273 | // update | ||
| 274 | repo_state.state = repo_state | ||
| 275 | .state | ||
| 276 | .iter() | ||
| 277 | .map(|(name, value)| { | ||
| 278 | ( | ||
| 279 | name.clone(), | ||
| 280 | if name.eq(to) { | ||
| 281 | reference_to_ref_value(&git_repo.git_repo, to).unwrap() | ||
| 282 | } else { | ||
| 283 | value.to_string() | ||
| 284 | }, | ||
| 285 | ) | ||
| 286 | }) | ||
| 287 | .collect(); | ||
| 288 | } else { | 272 | } else { |
| 289 | // add | 273 | // add or update |
| 290 | repo_state.state.push(( | 274 | repo_state.state.insert( |
| 291 | to.to_string(), | 275 | to.to_string(), |
| 292 | reference_to_ref_value(&git_repo.git_repo, to).unwrap(), | 276 | reference_to_ref_value(&git_repo.git_repo, to).unwrap(), |
| 293 | )); | 277 | ); |
| 294 | } | 278 | } |
| 295 | } | 279 | } |
| 296 | repo_state.state | 280 | repo_state.state |
| 297 | } else { | 281 | } else { |
| 298 | let mut state = vec![]; | 282 | let mut state = HashMap::new(); |
| 299 | let git_server_url = repo_ref | 283 | let git_server_url = repo_ref |
| 300 | .git_server | 284 | .git_server |
| 301 | .first() | 285 | .first() |
| @@ -303,14 +287,14 @@ async fn generate_updated_state( | |||
| 303 | let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?; | 287 | let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?; |
| 304 | git_server_remote.connect(git2::Direction::Fetch)?; | 288 | git_server_remote.connect(git2::Direction::Fetch)?; |
| 305 | for head in git_server_remote.list()? { | 289 | for head in git_server_remote.list()? { |
| 306 | state.push(( | 290 | state.insert( |
| 307 | head.name().to_string(), | 291 | head.name().to_string(), |
| 308 | if let Some(symbolic_ref) = head.symref_target() { | 292 | if let Some(symbolic_ref) = head.symref_target() { |
| 309 | format!("ref: {}", symbolic_ref) | 293 | format!("ref: {symbolic_ref}") |
| 310 | } else { | 294 | } else { |
| 311 | head.oid().to_string() | 295 | head.oid().to_string() |
| 312 | }, | 296 | }, |
| 313 | )); | 297 | ); |
| 314 | } | 298 | } |
| 315 | git_server_remote.disconnect()?; | 299 | git_server_remote.disconnect()?; |
| 316 | state | 300 | state |
| @@ -460,7 +444,7 @@ fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result< | |||
| 460 | impl RepoState { | 444 | impl RepoState { |
| 461 | pub async fn build( | 445 | pub async fn build( |
| 462 | identifier: String, | 446 | identifier: String, |
| 463 | state: Vec<(String, String)>, | 447 | state: HashMap<String, String>, |
| 464 | signer: &NostrSigner, | 448 | signer: &NostrSigner, |
| 465 | ) -> Result<RepoState> { | 449 | ) -> Result<RepoState> { |
| 466 | let mut tags = vec![Tag::identifier(identifier.clone())]; | 450 | let mut tags = vec![Tag::identifier(identifier.clone())]; |