diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-28 12:07:56 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-28 12:07:56 +0100 |
| commit | 564941cdbc77154ae4d05bca33e2a6f60e7cca9d (patch) | |
| tree | 6422c7b68996190b9e8cd42d91d6aaa70ee34594 /src | |
| parent | f2e674b5b90f891bd56247d370b047c7c81d33e3 (diff) | |
feat(remote): batch `push` and `fetch`
requests so that the git_server is called once
rather than many time in serial
Diffstat (limited to 'src')
| -rw-r--r-- | src/git_remote_helper.rs | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs index 6e8f70f..ded417f 100644 --- a/src/git_remote_helper.rs +++ b/src/git_remote_helper.rs | |||
| @@ -8,7 +8,7 @@ use core::str; | |||
| 8 | use std::{ | 8 | use std::{ |
| 9 | collections::HashSet, | 9 | collections::HashSet, |
| 10 | env, | 10 | env, |
| 11 | io::{self}, | 11 | io::{self, Stdin}, |
| 12 | path::PathBuf, | 12 | path::PathBuf, |
| 13 | }; | 13 | }; |
| 14 | 14 | ||
| @@ -94,7 +94,7 @@ async fn main() -> Result<()> { | |||
| 94 | } | 94 | } |
| 95 | ["fetch", _oid, refstr] => { | 95 | ["fetch", _oid, refstr] => { |
| 96 | temp_remote.connect(git2::Direction::Fetch)?; | 96 | temp_remote.connect(git2::Direction::Fetch)?; |
| 97 | temp_remote.download(&[refstr], None)?; | 97 | temp_remote.download(&get_refstrs_from_fetch_batch(&stdin, refstr)?, None)?; |
| 98 | temp_remote.disconnect()?; | 98 | temp_remote.disconnect()?; |
| 99 | println!(); | 99 | println!(); |
| 100 | } | 100 | } |
| @@ -105,7 +105,10 @@ async fn main() -> Result<()> { | |||
| 105 | let mut remote_callbacks = git2::RemoteCallbacks::new(); | 105 | let mut remote_callbacks = git2::RemoteCallbacks::new(); |
| 106 | remote_callbacks.credentials(auth.credentials(&git_config)); | 106 | remote_callbacks.credentials(auth.credentials(&git_config)); |
| 107 | push_options.remote_callbacks(remote_callbacks); | 107 | push_options.remote_callbacks(remote_callbacks); |
| 108 | temp_remote.push(&[refspec], Some(&mut push_options))?; | 108 | temp_remote.push( |
| 109 | &get_refspecs_from_push_batch(&stdin, refspec)?, | ||
| 110 | Some(&mut push_options), | ||
| 111 | )?; | ||
| 109 | update_remote_refs_from_push_refspecs(&git_repo.git_repo, refspec, url)?; | 112 | update_remote_refs_from_push_refspecs(&git_repo.git_repo, refspec, url)?; |
| 110 | temp_remote.disconnect()?; | 113 | temp_remote.disconnect()?; |
| 111 | println!(); | 114 | println!(); |
| @@ -231,3 +234,39 @@ fn get_remote_by_url<'a>(git_repo: &'a Repository, url: &'a str) -> Result<Remot | |||
| 231 | .find_remote(remote_name) | 234 | .find_remote(remote_name) |
| 232 | .context("we should have just located this remote") | 235 | .context("we should have just located this remote") |
| 233 | } | 236 | } |
| 237 | |||
| 238 | fn get_refstrs_from_fetch_batch(stdin: &Stdin, initial_refstr: &str) -> Result<Vec<String>> { | ||
| 239 | let mut line = String::new(); | ||
| 240 | let mut refstrs = vec![initial_refstr.to_string()]; | ||
| 241 | loop { | ||
| 242 | let tokens = read_line(stdin, &mut line)?; | ||
| 243 | match tokens.as_slice() { | ||
| 244 | ["fetch", _oid, refstr] => { | ||
| 245 | refstrs.push((*refstr).to_string()); | ||
| 246 | } | ||
| 247 | [] => break, | ||
| 248 | _ => bail!( | ||
| 249 | "after a `fetch` command we are only expecting another fetch or an empty line" | ||
| 250 | ), | ||
| 251 | } | ||
| 252 | } | ||
| 253 | Ok(refstrs) | ||
| 254 | } | ||
| 255 | |||
| 256 | fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result<Vec<String>> { | ||
| 257 | let mut line = String::new(); | ||
| 258 | let mut refspecs = vec![initial_refspec.to_string()]; | ||
| 259 | loop { | ||
| 260 | let tokens = read_line(stdin, &mut line)?; | ||
| 261 | match tokens.as_slice() { | ||
| 262 | ["push", spec] => { | ||
| 263 | refspecs.push((*spec).to_string()); | ||
| 264 | } | ||
| 265 | [] => break, | ||
| 266 | _ => { | ||
| 267 | bail!("after a `push` command we are only expecting another push or an empty line") | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | Ok(refspecs) | ||
| 272 | } | ||