Skip to content

Commit

Permalink
Make obvious that interrupt request was received
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 11, 2020
1 parent 5ac9538 commit 34b2373
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
25 changes: 21 additions & 4 deletions git-features/src/interruptible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@ use std::{

#[cfg(feature = "interrupt-handler")]
mod _impl {
pub fn init_interrupt_handler() {
ctrlc::set_handler(|| {
super::IS_INTERRUPTED.store(true, std::sync::atomic::Ordering::Relaxed);
use std::{
io,
sync::atomic::{AtomicUsize, Ordering},
};

pub fn init_interrupt_handler(mut message_channel: impl io::Write + Send + 'static) {
ctrlc::set_handler(move || {
const MESSAGES: &[&'static str] = &[
"interrupt requested",
"please wait…",
"the program will respond soon…",
"if the program doesn't respond quickly enough, please let us know here: https://github.com/Byron/gitoxide/issues"
];
static CURRENT_MESSAGE: AtomicUsize = AtomicUsize::new(0);
if !super::is_interrupted() {
CURRENT_MESSAGE.store(0, Ordering::Relaxed);
}
let msg_idx =CURRENT_MESSAGE.fetch_add(1, Ordering::Relaxed);
super::IS_INTERRUPTED.store(true, Ordering::Relaxed);
writeln!(message_channel, "{}", MESSAGES[msg_idx % MESSAGES.len()]).ok();
})
.expect("it is up to the application to ensure only one interrupt handler is installed, and this function is called only once.")
}
}
#[cfg(not(feature = "interrupt-handler"))]
mod _impl {
pub fn init_interrupt_handler() {}
pub fn init_interrupt_handler(mut message_channel: impl io::Write + Send + 'static) {}
}
pub use _impl::init_interrupt_handler;

Expand Down
13 changes: 11 additions & 2 deletions git-odb/src/pack/tree/from_offsets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{pack, pack::index::access::PackOffset, pack::tree::Tree};
use git_features::progress::{self, Progress};
use git_features::{
interruptible::is_interrupted,
progress::{self, Progress},
};
use quick_error::quick_error;
use std::{
fs, io,
Expand All @@ -26,6 +29,9 @@ quick_error! {
source(err)
from()
}
Interrupted {
display("Interrupted by user")
}
}
}

Expand Down Expand Up @@ -70,7 +76,7 @@ impl<T> Tree<T> {

let mut previous_cursor_position = None::<u64>;

for data in data_sorted_by_offsets {
for (idx, data) in data_sorted_by_offsets.enumerate() {
let pack_offset = get_pack_offset(&data);
if let Some(previous_offset) = previous_cursor_position {
Self::advance_cursor_to_pack_offset(&mut r, pack_offset, previous_offset)?;
Expand Down Expand Up @@ -99,6 +105,9 @@ impl<T> Tree<T> {
}
};
progress.inc();
if idx % 10_000 == 0 && is_interrupted() {
return Err(Error::Interrupted);
}
}

progress.show_throughput(then);
Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/lean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn prepare(
pub fn main() -> Result<()> {
pub use options::*;
let cli: Args = crate::shared::from_env();
git_features::interruptible::init_interrupt_handler();
git_features::interruptible::init_interrupt_handler(std::io::stderr());
let thread_limit = cli.threads;
let verbose = cli.verbose;
match cli.subcommand {
Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub fn main() -> Result<()> {
format,
cmd,
} = Args::parse();
git_features::interruptible::init_interrupt_handler();
git_features::interruptible::init_interrupt_handler(std::io::stderr());

match cmd {
Subcommands::IndexFromPack {
Expand Down
2 changes: 1 addition & 1 deletion src/porcelain/lean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use gitoxide_core as core;
pub fn main() -> Result<()> {
pub use options::*;
let cli: Args = crate::shared::from_env();
git_features::interruptible::init_interrupt_handler();
git_features::interruptible::init_interrupt_handler(std::io::stderr());

match cli.subcommand {
SubCommands::Init(_) => core::repository::init(),
Expand Down
2 changes: 1 addition & 1 deletion src/porcelain/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod options {
pub fn main() -> Result<()> {
use options::*;
let args = Args::parse();
git_features::interruptible::init_interrupt_handler();
git_features::interruptible::init_interrupt_handler(std::io::stderr());
match args.cmd {
Subcommands::Init => core::repository::init(),
}?;
Expand Down

0 comments on commit 34b2373

Please sign in to comment.