Skip to content

Commit 3034966

Browse files
committed
refactor: move progress tracking setup into utils function
1 parent cf27542 commit 3034966

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

rust/patchable/src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{fs::File, io::Write, path::PathBuf};
1010
use git2::{Oid, Repository};
1111
use serde::{Deserialize, Serialize};
1212
use snafu::{ensure, OptionExt, ResultExt as _, Snafu};
13-
use tracing_indicatif::{span_ext::IndicatifSpanExt, IndicatifLayer};
13+
use tracing_indicatif::IndicatifLayer;
1414
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};
1515

1616
#[derive(clap::Parser)]
@@ -466,13 +466,14 @@ fn main() -> Result<()> {
466466
});
467467

468468
// Add progress tracking for push operation
469-
let span_push = tracing::info_span!("pushing");
470-
span_push.pb_set_style(&utils::progress_bar_style());
471-
let _ = span_push.enter();
472-
let mut quant_push = utils::Quantizer::percent();
469+
let (span_push, mut quant_push) = utils::setup_progress_tracking(tracing::info_span!("pushing"));
473470
callbacks.push_transfer_progress(move |current, total, _| {
474471
if total > 0 {
475-
quant_push.update_span_progress(current, total, &span_push);
472+
quant_push.update_span_progress(
473+
current,
474+
total,
475+
&span_push,
476+
);
476477
}
477478
});
478479

rust/patchable/src/repo.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ use git2::{
55
WorktreeAddOptions,
66
};
77
use snafu::{ResultExt, Snafu};
8-
use tracing_indicatif::span_ext::IndicatifSpanExt;
98

109
use crate::{
1110
error::{self, CommitRef},
12-
utils::{progress_bar_style, Quantizer},
11+
utils::setup_progress_tracking,
1312
};
1413

1514
#[derive(Debug, Snafu)]
@@ -149,15 +148,11 @@ pub fn resolve_and_fetch_commitish(
149148
error = &err as &dyn std::error::Error,
150149
"base commit not found locally, fetching from upstream"
151150
);
152-
let span_recv = tracing::info_span!("receiving");
153-
let span_index = tracing::info_span!("indexing");
154-
span_recv.pb_set_style(&progress_bar_style());
155-
span_index.pb_set_style(&progress_bar_style());
156-
let _ = span_recv.enter();
157-
let _ = span_index.enter();
151+
152+
let (span_recv, mut quant_recv) = setup_progress_tracking(tracing::info_span!("receiving"));
153+
let (span_index, mut quant_index) = setup_progress_tracking(tracing::info_span!("indexing"));
154+
158155
let mut callbacks = RemoteCallbacks::new();
159-
let mut quant_recv = Quantizer::percent();
160-
let mut quant_index = Quantizer::percent();
161156
callbacks.transfer_progress(move |progress| {
162157
quant_recv.update_span_progress(
163158
progress.received_objects(),
@@ -171,6 +166,7 @@ pub fn resolve_and_fetch_commitish(
171166
);
172167
true
173168
});
169+
174170
repo.remote_anonymous(upstream_url)
175171
.context(CreateRemoteSnafu {
176172
repo,

rust/patchable/src/utils.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ use git2::Repository;
44
use tracing::Span;
55
use tracing_indicatif::{span_ext::IndicatifSpanExt, style::ProgressStyle};
66

7-
pub fn progress_bar_style() -> ProgressStyle {
8-
ProgressStyle::with_template(
9-
"{span_child_prefix}{spinner} {span_name}{{{span_fields}}} {wide_msg} {bar:40} {percent:>3}%",
10-
)
11-
.expect("hard-coded template should be valid")
12-
}
13-
147
/// Runs a function whenever a `value` changes "enough".
158
///
169
/// See [`Self::update`], and especially [`Self::update_span_progress`].
@@ -94,3 +87,14 @@ pub mod oid_serde {
9487
.and_then(|oid| Oid::from_str(&oid).map_err(<D::Error as serde::de::Error>::custom))
9588
}
9689
}
90+
91+
/// Sets up progress tracking for Git operations with a progress bar.
92+
pub fn setup_progress_tracking(span: tracing::Span) -> (tracing::Span, Quantizer) {
93+
span.pb_set_style(&ProgressStyle::with_template(
94+
"{span_child_prefix}{spinner} {span_name}{{{span_fields}}} {wide_msg} {bar:40} {percent:>3}%",
95+
)
96+
.expect("hard-coded template should be valid"));
97+
let _ = span.enter();
98+
let quantizer = Quantizer::percent();
99+
(span, quantizer)
100+
}

0 commit comments

Comments
 (0)