diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-11 17:13:53 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-12 07:21:28 +0100 |
| commit | f5bd964718fc063a07af442e93f65fb8239f8228 (patch) | |
| tree | 1633548707e46f5be61e51acb8ec5665d925a0db /src/bin/git_remote_nostr | |
| parent | f0921887dc19c6a60a454fb9f64574f62da73af1 (diff) | |
feat(remote): add fetch status updates
based off of:
https://github.com/rust-lang/git2-rs/blob/master/examples/fetch.rs
Diffstat (limited to 'src/bin/git_remote_nostr')
| -rw-r--r-- | src/bin/git_remote_nostr/fetch.rs | 76 |
1 files changed, 69 insertions, 7 deletions
diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs index 312690a..b74e432 100644 --- a/src/bin/git_remote_nostr/fetch.rs +++ b/src/bin/git_remote_nostr/fetch.rs | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | use core::str; | ||
| 1 | use std::io::Stdin; | 2 | use std::io::Stdin; |
| 2 | 3 | ||
| 3 | use anyhow::{anyhow, bail, Result}; | 4 | use anyhow::{anyhow, bail, Result}; |
| @@ -125,11 +126,13 @@ fn fetch_from_git_server( | |||
| 125 | )?; | 126 | )?; |
| 126 | 127 | ||
| 127 | let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; | 128 | let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; |
| 128 | let res = if [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol) { | 129 | let res = fetch_from_git_server_url( |
| 129 | fetch_from_git_server_url_unauthenticated(git_repo, oids, &formatted_url) | 130 | git_repo, |
| 130 | } else { | 131 | oids, |
| 131 | fetch_from_git_server_url(git_repo, oids, &formatted_url) | 132 | &formatted_url, |
| 132 | }; | 133 | [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol), |
| 134 | term, | ||
| 135 | ); | ||
| 133 | term.clear_last_lines(1)?; | 136 | term.clear_last_lines(1)?; |
| 134 | if let Err(error) = res { | 137 | if let Err(error) = res { |
| 135 | term.write_line( | 138 | term.write_line( |
| @@ -172,6 +175,8 @@ fn fetch_from_git_server_url( | |||
| 172 | git_repo: &Repository, | 175 | git_repo: &Repository, |
| 173 | oids: &[String], | 176 | oids: &[String], |
| 174 | git_server_url: &str, | 177 | git_server_url: &str, |
| 178 | dont_authenticate: bool, | ||
| 179 | term: &console::Term, | ||
| 175 | ) -> Result<()> { | 180 | ) -> Result<()> { |
| 176 | if git_server_url.parse::<CloneUrl>()?.protocol() == ServerProtocol::Ssh && !check_ssh_keys() { | 181 | if git_server_url.parse::<CloneUrl>()?.protocol() == ServerProtocol::Ssh && !check_ssh_keys() { |
| 177 | bail!("no ssh keys found"); | 182 | bail!("no ssh keys found"); |
| @@ -181,10 +186,67 @@ fn fetch_from_git_server_url( | |||
| 181 | let auth = GitAuthenticator::default(); | 186 | let auth = GitAuthenticator::default(); |
| 182 | let mut fetch_options = git2::FetchOptions::new(); | 187 | let mut fetch_options = git2::FetchOptions::new(); |
| 183 | let mut remote_callbacks = git2::RemoteCallbacks::new(); | 188 | let mut remote_callbacks = git2::RemoteCallbacks::new(); |
| 184 | // TODO status update callback | 189 | remote_callbacks.sideband_progress(|data| { |
| 185 | remote_callbacks.credentials(auth.credentials(&git_config)); | 190 | let _ = term.clear_last_lines(1); |
| 191 | let _ = term.write_line(format!("remote: {}", str::from_utf8(data).unwrap()).as_str()); | ||
| 192 | true | ||
| 193 | }); | ||
| 194 | remote_callbacks.transfer_progress(|stats| { | ||
| 195 | let _ = term.clear_last_lines(1); | ||
| 196 | if stats.received_objects() == stats.total_objects() { | ||
| 197 | let _ = term.write_line( | ||
| 198 | format!( | ||
| 199 | "Resolving deltas {}/{}\r", | ||
| 200 | stats.indexed_deltas(), | ||
| 201 | stats.total_deltas() | ||
| 202 | ) | ||
| 203 | .as_str(), | ||
| 204 | ); | ||
| 205 | } else if stats.total_objects() > 0 { | ||
| 206 | let _ = term.write_line( | ||
| 207 | format!( | ||
| 208 | "Received {}/{} objects ({}) in {} bytes\r", | ||
| 209 | stats.received_objects(), | ||
| 210 | stats.total_objects(), | ||
| 211 | stats.indexed_objects(), | ||
| 212 | stats.received_bytes() | ||
| 213 | ) | ||
| 214 | .as_str(), | ||
| 215 | ); | ||
| 216 | } | ||
| 217 | true | ||
| 218 | }); | ||
| 219 | if !dont_authenticate { | ||
| 220 | remote_callbacks.credentials(auth.credentials(&git_config)); | ||
| 221 | } | ||
| 186 | fetch_options.remote_callbacks(remote_callbacks); | 222 | fetch_options.remote_callbacks(remote_callbacks); |
| 187 | git_server_remote.download(oids, Some(&mut fetch_options))?; | 223 | git_server_remote.download(oids, Some(&mut fetch_options))?; |
| 224 | { | ||
| 225 | let stats = git_server_remote.stats(); | ||
| 226 | if stats.local_objects() > 0 { | ||
| 227 | term.write_line( | ||
| 228 | format!( | ||
| 229 | "\rReceived {}/{} objects in {} bytes (used {} local \ | ||
| 230 | objects)", | ||
| 231 | stats.indexed_objects(), | ||
| 232 | stats.total_objects(), | ||
| 233 | stats.received_bytes(), | ||
| 234 | stats.local_objects() | ||
| 235 | ) | ||
| 236 | .as_str(), | ||
| 237 | )?; | ||
| 238 | } else { | ||
| 239 | term.write_line( | ||
| 240 | format!( | ||
| 241 | "\rReceived {}/{} objects in {} bytes", | ||
| 242 | stats.indexed_objects(), | ||
| 243 | stats.total_objects(), | ||
| 244 | stats.received_bytes() | ||
| 245 | ) | ||
| 246 | .as_str(), | ||
| 247 | )?; | ||
| 248 | } | ||
| 249 | } | ||
| 188 | git_server_remote.disconnect()?; | 250 | git_server_remote.disconnect()?; |
| 189 | Ok(()) | 251 | Ok(()) |
| 190 | } | 252 | } |