upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/git_remote_nostr/fetch.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-09-12 15:13:50 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-09-12 15:13:50 +0100
commite882fde7d3fc7f2113196585b450f7065061abb9 (patch)
tree222ecde50be06a5873a3080762c072b28f50666c /src/bin/git_remote_nostr/fetch.rs
parent1a38f98807a3244b77d2525136f3d6976f61dcc4 (diff)
fix(remote): improve `push` status updates
to bring them more into line to the native git client
Diffstat (limited to 'src/bin/git_remote_nostr/fetch.rs')
-rw-r--r--src/bin/git_remote_nostr/fetch.rs84
1 files changed, 18 insertions, 66 deletions
diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs
index 1b19347..1e38e0e 100644
--- a/src/bin/git_remote_nostr/fetch.rs
+++ b/src/bin/git_remote_nostr/fetch.rs
@@ -18,6 +18,7 @@ use ngit::{
18use crate::utils::{ 18use 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}; 22};
22 23
23pub async fn run_fetch( 24pub async fn run_fetch(
@@ -187,31 +188,19 @@ fn fetch_from_git_server_url(
187 let mut fetch_options = git2::FetchOptions::new(); 188 let mut fetch_options = git2::FetchOptions::new();
188 let mut remote_callbacks = git2::RemoteCallbacks::new(); 189 let mut remote_callbacks = git2::RemoteCallbacks::new();
189 remote_callbacks.sideband_progress(|data| { 190 remote_callbacks.sideband_progress(|data| {
190 if let Ok(data) = str::from_utf8(data) { 191 report_on_sideband_progress(data, term);
191 let data = data 192 true
192 .split(['\n', '\r']) 193 });
193 .find(|line| !line.is_empty()) 194 remote_callbacks.transfer_progress(|stats| {
194 .unwrap_or(""); 195 let _ = term.clear_last_lines(1);
195 if !data.is_empty() { 196 report_on_transfer_progress(
196 let s = format!("remote: {data}"); 197 &stats,
197 let _ = term.clear_last_lines(1); 198 term,
198 let _ = term.write_line(s.as_str()); 199 TransferDirection::Fetch,
199 if !s.contains('%') || s.contains("100%") { 200 ProgressStatus::InProgress,
200 // print it twice so the next sideband_progress doesn't delete it 201 );
201 let _ = term.write_line(s.as_str());
202 }
203 }
204 }
205 true 202 true
206 }); 203 });
207 remote_callbacks.transfer_progress(
208 #[allow(clippy::cast_precision_loss)]
209 |stats| {
210 let _ = term.clear_last_lines(1);
211 report_on_transfer_progress(&stats, term, false);
212 true
213 },
214 );
215 204
216 if !dont_authenticate { 205 if !dont_authenticate {
217 remote_callbacks.credentials(auth.credentials(&git_config)); 206 remote_callbacks.credentials(auth.credentials(&git_config));
@@ -220,50 +209,13 @@ fn fetch_from_git_server_url(
220 term.write_line("")?; 209 term.write_line("")?;
221 git_server_remote.download(oids, Some(&mut fetch_options))?; 210 git_server_remote.download(oids, Some(&mut fetch_options))?;
222 211
223 report_on_transfer_progress(&git_server_remote.stats(), term, true); 212 report_on_transfer_progress(
213 &git_server_remote.stats(),
214 term,
215 TransferDirection::Fetch,
216 ProgressStatus::Complete,
217 );
224 218
225 git_server_remote.disconnect()?; 219 git_server_remote.disconnect()?;
226 Ok(()) 220 Ok(())
227} 221}
228
229#[allow(clippy::cast_precision_loss)]
230#[allow(clippy::float_cmp)]
231fn report_on_transfer_progress(stats: &git2::Progress<'_>, term: &console::Term, complete: bool) {
232 let total = stats.total_objects() as f64;
233 if total == 0.0 {
234 return;
235 }
236 let received = stats.received_objects() as f64;
237 let percentage = (received / total) * 100.0;
238
239 // Get the total received bytes
240 let received_bytes = stats.received_bytes() as f64;
241
242 // Determine whether to use KiB or MiB
243 let (size, unit) = if received_bytes >= (1024.0 * 1024.0) {
244 // Convert to MiB
245 (received_bytes / (1024.0 * 1024.0), "MiB")
246 } else {
247 // Convert to KiB
248 (received_bytes / 1024.0, "KiB")
249 };
250
251 // Format the output for receiving objects
252 if received < total || complete {
253 let _ = term.write_line(
254 format!(
255 "Receiving objects: {percentage:.0}% ({received}/{total}) {size:.2} {unit}, done.\r"
256 )
257 .as_str(),
258 );
259 }
260 if received == total || complete {
261 let indexed_deltas = stats.indexed_deltas() as f64;
262 let total_deltas = stats.total_deltas() as f64;
263 let percentage = (indexed_deltas / total_deltas) * 100.0;
264 let _ = term.write_line(
265 format!("Resolving deltas: {percentage:.0}% ({indexed_deltas}/{total_deltas}) done.\r")
266 .as_str(),
267 );
268 }
269}