diff options
Diffstat (limited to 'src/bin/git_remote_nostr')
| -rw-r--r-- | src/bin/git_remote_nostr/fetch.rs | 68 | ||||
| -rw-r--r-- | src/bin/git_remote_nostr/push.rs | 62 | ||||
| -rw-r--r-- | src/bin/git_remote_nostr/utils.rs | 63 |
3 files changed, 100 insertions, 93 deletions
diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs index 1e38e0e..59067fa 100644 --- a/src/bin/git_remote_nostr/fetch.rs +++ b/src/bin/git_remote_nostr/fetch.rs | |||
| @@ -18,7 +18,7 @@ use ngit::{ | |||
| 18 | use crate::utils::{ | 18 | use crate::utils::{ |
| 19 | fetch_or_list_error_is_not_authentication_failure, find_proposal_and_patches_by_branch_name, | 19 | fetch_or_list_error_is_not_authentication_failure, find_proposal_and_patches_by_branch_name, |
| 20 | get_oids_from_fetch_batch, get_open_proposals, get_read_protocols_to_try, join_with_and, | 20 | get_oids_from_fetch_batch, get_open_proposals, get_read_protocols_to_try, join_with_and, |
| 21 | report_on_sideband_progress, report_on_transfer_progress, ProgressStatus, TransferDirection, | 21 | report_on_sideband_progress, |
| 22 | }; | 22 | }; |
| 23 | 23 | ||
| 24 | pub async fn run_fetch( | 24 | pub async fn run_fetch( |
| @@ -172,6 +172,58 @@ fn fetch_from_git_server( | |||
| 172 | } | 172 | } |
| 173 | } | 173 | } |
| 174 | 174 | ||
| 175 | enum ProgressStatus { | ||
| 176 | InProgress, | ||
| 177 | Complete, | ||
| 178 | } | ||
| 179 | |||
| 180 | #[allow(clippy::cast_precision_loss)] | ||
| 181 | #[allow(clippy::float_cmp)] | ||
| 182 | #[allow(clippy::needless_pass_by_value)] | ||
| 183 | fn report_on_transfer_progress( | ||
| 184 | progress_stats: &git2::Progress<'_>, | ||
| 185 | term: &console::Term, | ||
| 186 | status: ProgressStatus, | ||
| 187 | ) { | ||
| 188 | let total = progress_stats.total_objects() as f64; | ||
| 189 | if total == 0.0 { | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | let received = progress_stats.received_objects() as f64; | ||
| 193 | let percentage = (received / total) * 100.0; | ||
| 194 | |||
| 195 | // Get the total received bytes | ||
| 196 | let received_bytes = progress_stats.received_bytes() as f64; | ||
| 197 | |||
| 198 | // Determine whether to use KiB or MiB | ||
| 199 | let (size, unit) = if received_bytes >= (1024.0 * 1024.0) { | ||
| 200 | // Convert to MiB | ||
| 201 | (received_bytes / (1024.0 * 1024.0), "MiB") | ||
| 202 | } else { | ||
| 203 | // Convert to KiB | ||
| 204 | (received_bytes / 1024.0, "KiB") | ||
| 205 | }; | ||
| 206 | |||
| 207 | // Format the output for receiving objects | ||
| 208 | if received < total || matches!(status, ProgressStatus::Complete) { | ||
| 209 | let _ = term.write_line( | ||
| 210 | format!( | ||
| 211 | "Receiving objects: {percentage:.0}% ({received}/{total}) {size:.2} {unit}, done.", | ||
| 212 | ) | ||
| 213 | .as_str(), | ||
| 214 | ); | ||
| 215 | } | ||
| 216 | if received == total || matches!(status, ProgressStatus::Complete) { | ||
| 217 | let indexed_deltas = progress_stats.indexed_deltas() as f64; | ||
| 218 | let total_deltas = progress_stats.total_deltas() as f64; | ||
| 219 | let percentage = (indexed_deltas / total_deltas) * 100.0; | ||
| 220 | let _ = term.write_line( | ||
| 221 | format!("Resolving deltas: {percentage:.0}% ({indexed_deltas}/{total_deltas}) done.") | ||
| 222 | .as_str(), | ||
| 223 | ); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 175 | fn fetch_from_git_server_url( | 227 | fn fetch_from_git_server_url( |
| 176 | git_repo: &Repository, | 228 | git_repo: &Repository, |
| 177 | oids: &[String], | 229 | oids: &[String], |
| @@ -193,12 +245,7 @@ fn fetch_from_git_server_url( | |||
| 193 | }); | 245 | }); |
| 194 | remote_callbacks.transfer_progress(|stats| { | 246 | remote_callbacks.transfer_progress(|stats| { |
| 195 | let _ = term.clear_last_lines(1); | 247 | let _ = term.clear_last_lines(1); |
| 196 | report_on_transfer_progress( | 248 | report_on_transfer_progress(&stats, term, ProgressStatus::InProgress); |
| 197 | &stats, | ||
| 198 | term, | ||
| 199 | TransferDirection::Fetch, | ||
| 200 | ProgressStatus::InProgress, | ||
| 201 | ); | ||
| 202 | true | 249 | true |
| 203 | }); | 250 | }); |
| 204 | 251 | ||
| @@ -209,12 +256,7 @@ fn fetch_from_git_server_url( | |||
| 209 | term.write_line("")?; | 256 | term.write_line("")?; |
| 210 | git_server_remote.download(oids, Some(&mut fetch_options))?; | 257 | git_server_remote.download(oids, Some(&mut fetch_options))?; |
| 211 | 258 | ||
| 212 | report_on_transfer_progress( | 259 | report_on_transfer_progress(&git_server_remote.stats(), term, ProgressStatus::Complete); |
| 213 | &git_server_remote.stats(), | ||
| 214 | term, | ||
| 215 | TransferDirection::Fetch, | ||
| 216 | ProgressStatus::Complete, | ||
| 217 | ); | ||
| 218 | 260 | ||
| 219 | git_server_remote.disconnect()?; | 261 | git_server_remote.disconnect()?; |
| 220 | Ok(()) | 262 | Ok(()) |
diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 5a19bb0..0a7f1a6 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs | |||
| @@ -18,6 +18,7 @@ use ngit::{ | |||
| 18 | git::{ | 18 | git::{ |
| 19 | self, | 19 | self, |
| 20 | nostr_url::{CloneUrl, NostrUrlDecoded}, | 20 | nostr_url::{CloneUrl, NostrUrlDecoded}, |
| 21 | oid_to_shorthand_string, | ||
| 21 | }, | 22 | }, |
| 22 | git_events::{self, get_event_root}, | 23 | git_events::{self, get_event_root}, |
| 23 | login::{self, get_curent_user}, | 24 | login::{self, get_curent_user}, |
| @@ -39,7 +40,6 @@ use crate::{ | |||
| 39 | find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url, | 40 | find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url, |
| 40 | get_short_git_server_name, get_write_protocols_to_try, join_with_and, | 41 | get_short_git_server_name, get_write_protocols_to_try, join_with_and, |
| 41 | push_error_is_not_authentication_failure, read_line, report_on_sideband_progress, | 42 | push_error_is_not_authentication_failure, read_line, report_on_sideband_progress, |
| 42 | report_on_transfer_progress, ProgressStatus, TransferDirection, | ||
| 43 | }, | 43 | }, |
| 44 | }; | 44 | }; |
| 45 | 45 | ||
| @@ -355,13 +355,7 @@ fn push_to_remote( | |||
| 355 | let mut success = false; | 355 | let mut success = false; |
| 356 | 356 | ||
| 357 | for protocol in &protocols_to_attempt { | 357 | for protocol in &protocols_to_attempt { |
| 358 | term.write_line( | 358 | term.write_line(format!("push: {} over {protocol}...", server_url.short_name(),).as_str())?; |
| 359 | format!( | ||
| 360 | "fetching {} ref list over {protocol}...", | ||
| 361 | server_url.short_name(), | ||
| 362 | ) | ||
| 363 | .as_str(), | ||
| 364 | )?; | ||
| 365 | 359 | ||
| 366 | let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; | 360 | let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; |
| 367 | 361 | ||
| @@ -424,16 +418,50 @@ fn push_to_remote_url( | |||
| 424 | } | 418 | } |
| 425 | Ok(()) | 419 | Ok(()) |
| 426 | }); | 420 | }); |
| 427 | remote_callbacks.transfer_progress(|stats| { | 421 | remote_callbacks.push_negotiation(|updates| { |
| 428 | let _ = term.clear_last_lines(1); | 422 | for update in updates { |
| 429 | report_on_transfer_progress( | 423 | let _ = term.write_line( |
| 430 | &stats, | 424 | format!( |
| 431 | term, | 425 | " {}..{} {} -> {}", |
| 432 | TransferDirection::Push, | 426 | oid_to_shorthand_string(update.dst()).unwrap(), |
| 433 | ProgressStatus::InProgress, | 427 | oid_to_shorthand_string(update.src()).unwrap(), |
| 434 | ); | 428 | update |
| 435 | true | 429 | .src_refname() |
| 430 | .unwrap_or("") | ||
| 431 | .replace("refs/heads/", "") | ||
| 432 | .replace("refs/tags/", "tags/"), | ||
| 433 | update | ||
| 434 | .dst_refname() | ||
| 435 | .unwrap_or("") | ||
| 436 | .replace("refs/heads/", "") | ||
| 437 | .replace("refs/tags/", "tags/"), | ||
| 438 | ) | ||
| 439 | .as_str(), | ||
| 440 | ); | ||
| 441 | // now a blank line for transfer progress to delete | ||
| 442 | let _ = term.write_line(""); | ||
| 443 | } | ||
| 444 | Ok(()) | ||
| 436 | }); | 445 | }); |
| 446 | remote_callbacks.push_transfer_progress( | ||
| 447 | #[allow(clippy::cast_precision_loss)] | ||
| 448 | |current, total, bytes| { | ||
| 449 | let _ = term.clear_last_lines(1); | ||
| 450 | let percentage = (current as f64 / total as f64) * 100.0; | ||
| 451 | let (size, unit) = if bytes >= (1024 * 1024) { | ||
| 452 | (bytes as f64 / (1024.0 * 1024.0), "MiB") | ||
| 453 | } else { | ||
| 454 | (bytes as f64 / 1024.0, "KiB") | ||
| 455 | }; | ||
| 456 | let _ = term.write_line( | ||
| 457 | format!( | ||
| 458 | "Writing objects: {percentage:.0}% ({current}/{total}) {size:.2} {unit}, done." | ||
| 459 | ) | ||
| 460 | .as_str(), | ||
| 461 | ); | ||
| 462 | }, | ||
| 463 | ); | ||
| 464 | |||
| 437 | remote_callbacks.sideband_progress(|data| { | 465 | remote_callbacks.sideband_progress(|data| { |
| 438 | report_on_sideband_progress(data, term); | 466 | report_on_sideband_progress(data, term); |
| 439 | true | 467 | true |
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index 6e09851..15a0d76 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs | |||
| @@ -310,69 +310,6 @@ pub fn error_might_be_authentication_related(error: &anyhow::Error) -> bool { | |||
| 310 | false | 310 | false |
| 311 | } | 311 | } |
| 312 | 312 | ||
| 313 | pub enum TransferDirection { | ||
| 314 | Fetch, | ||
| 315 | Push, | ||
| 316 | } | ||
| 317 | |||
| 318 | pub enum ProgressStatus { | ||
| 319 | InProgress, | ||
| 320 | Complete, | ||
| 321 | } | ||
| 322 | |||
| 323 | #[allow(clippy::cast_precision_loss)] | ||
| 324 | #[allow(clippy::float_cmp)] | ||
| 325 | #[allow(clippy::needless_pass_by_value)] | ||
| 326 | pub fn report_on_transfer_progress( | ||
| 327 | progress_stats: &git2::Progress<'_>, | ||
| 328 | term: &console::Term, | ||
| 329 | direction: TransferDirection, | ||
| 330 | status: ProgressStatus, | ||
| 331 | ) { | ||
| 332 | let total = progress_stats.total_objects() as f64; | ||
| 333 | if total == 0.0 { | ||
| 334 | return; | ||
| 335 | } | ||
| 336 | let received = progress_stats.received_objects() as f64; | ||
| 337 | let percentage = (received / total) * 100.0; | ||
| 338 | |||
| 339 | // Get the total received bytes | ||
| 340 | let received_bytes = progress_stats.received_bytes() as f64; | ||
| 341 | |||
| 342 | // Determine whether to use KiB or MiB | ||
| 343 | let (size, unit) = if received_bytes >= (1024.0 * 1024.0) { | ||
| 344 | // Convert to MiB | ||
| 345 | (received_bytes / (1024.0 * 1024.0), "MiB") | ||
| 346 | } else { | ||
| 347 | // Convert to KiB | ||
| 348 | (received_bytes / 1024.0, "KiB") | ||
| 349 | }; | ||
| 350 | |||
| 351 | // Format the output for receiving objects | ||
| 352 | if received < total || matches!(status, ProgressStatus::Complete) { | ||
| 353 | let _ = term.write_line( | ||
| 354 | format!( | ||
| 355 | "{} objects: {percentage:.0}% ({received}/{total}) {size:.2} {unit}, done.\r", | ||
| 356 | if matches!(direction, TransferDirection::Fetch) { | ||
| 357 | "Receiving" | ||
| 358 | } else { | ||
| 359 | "Writing" | ||
| 360 | }, | ||
| 361 | ) | ||
| 362 | .as_str(), | ||
| 363 | ); | ||
| 364 | } | ||
| 365 | if received == total || matches!(status, ProgressStatus::Complete) { | ||
| 366 | let indexed_deltas = progress_stats.indexed_deltas() as f64; | ||
| 367 | let total_deltas = progress_stats.total_deltas() as f64; | ||
| 368 | let percentage = (indexed_deltas / total_deltas) * 100.0; | ||
| 369 | let _ = term.write_line( | ||
| 370 | format!("Resolving deltas: {percentage:.0}% ({indexed_deltas}/{total_deltas}) done.\r") | ||
| 371 | .as_str(), | ||
| 372 | ); | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | pub fn report_on_sideband_progress(data: &[u8], term: &console::Term) { | 313 | pub fn report_on_sideband_progress(data: &[u8], term: &console::Term) { |
| 377 | if let Ok(data) = str::from_utf8(data) { | 314 | if let Ok(data) = str::from_utf8(data) { |
| 378 | let data = data | 315 | let data = data |