History

Prevent pack-index-from-data to block if stdin is a terminal

Sebastian Thiel committed 5 years ago

Commit 39dec0e1 parent 4c36f923 files · +29 −9Browse files

Unified · Split

3 files
  1. gitoxide-core/src/pack/index.rs +8 −6
  2. src/plumbing/lean/main.rs +11 −1
  3. src/plumbing/pretty/main.rs +10 −2
gitoxide-core/src/pack/index.rs+8−6
@@ -67,8 +67,13 @@ pub fn stream_len(mut s: impl io::Seek) -> io::Result<u64> { · 66 unmodified lines
67 67
68pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 2..=3;68pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 2..=3;
69 69
70pub enum PathOrRead {
71 Path(PathBuf),
72 Read(Box<dyn std::io::Read + Send + 'static>),
73}
74
70pub fn from_pack(75pub fn from_pack(
71 pack: Option<PathBuf>,76 pack: PathOrRead,
72 directory: Option<PathBuf>,77 directory: Option<PathBuf>,
73 progress: impl Progress,78 progress: impl Progress,
74 ctx: Context<impl io::Write>,79 ctx: Context<impl io::Write>,
@@ -82,15 +87,12 @@ pub fn from_pack( · 7 unmodified lines
82 let out = ctx.out;87 let out = ctx.out;
83 let format = ctx.format;88 let format = ctx.format;
84 let res = match pack {89 let res = match pack {
85 Some(pack) => {90 PathOrRead::Path(pack) => {
86 let pack_len = pack.metadata()?.len();91 let pack_len = pack.metadata()?.len();
87 let pack_file = fs::File::open(pack)?;92 let pack_file = fs::File::open(pack)?;
88 pack::Bundle::write_to_directory_eagerly(pack_file, Some(pack_len), directory, progress, options)93 pack::Bundle::write_to_directory_eagerly(pack_file, Some(pack_len), directory, progress, options)
89 }94 }
90 None => {95 PathOrRead::Read(input) => pack::Bundle::write_to_directory_eagerly(input, None, directory, progress, options),
91 let stdin = io::stdin();
92 pack::Bundle::write_to_directory_eagerly(stdin, None, directory, progress, options)
93 }
94 }96 }
95 .with_context(|| "Failed to write pack and index")?;97 .with_context(|| "Failed to write pack and index")?;
96 match format {98 match format {
src/plumbing/lean/main.rs+11−1
@@ -90,9 +90,19 @@ pub fn main() -> Result<()> { · 89 unmodified lines
90 pack_path,90 pack_path,
91 directory,91 directory,
92 }) => {92 }) => {
93 use gitoxide_core::pack::index::PathOrRead;
93 let (_handle, progress) = prepare(verbose, "pack-explode", core::pack::index::PROGRESS_RANGE);94 let (_handle, progress) = prepare(verbose, "pack-explode", core::pack::index::PROGRESS_RANGE);
95 let input = if let Some(path) = pack_path {
96 PathOrRead::Path(path)
97 } else {
98 #[cfg(feature = "atty")]
99 if atty::is(atty::Stream::Stdin) {
100 anyhow::bail!("Refusing to read from standard input as no path is given, but it's a terminal.")
101 }
102 PathOrRead::Read(Box::new(std::io::stdin()))
103 };
94 core::pack::index::from_pack(104 core::pack::index::from_pack(
95 pack_path,105 input,
96 directory,106 directory,
97 DoOrDiscard::from(progress),107 DoOrDiscard::from(progress),
98 core::pack::index::Context {108 core::pack::index::Context {
src/plumbing/pretty/main.rs+10−2
@@ -31,7 +31,6 @@ pub fn main() -> Result<()> { · 30 unmodified lines
31 } => {31 } => {
32 let has_tips = !tips.is_empty();32 let has_tips = !tips.is_empty();
33 let has_tui = progress;33 let has_tui = progress;
34 #[cfg(feature = "atty")]
35 if atty::is(if has_tui {34 if atty::is(if has_tui {
36 atty::Stream::Stderr35 atty::Stream::Stderr
37 } else {36 } else {
@@ -123,8 +122,17 @@ pub fn main() -> Result<()> { · 85 unmodified lines
123 progress_keep_open,122 progress_keep_open,
124 core::pack::index::PROGRESS_RANGE,123 core::pack::index::PROGRESS_RANGE,
125 move |progress, out, _err| {124 move |progress, out, _err| {
125 use gitoxide_core::pack::index::PathOrRead;
126 let input = if let Some(path) = pack_path {
127 PathOrRead::Path(path)
128 } else {
129 if atty::is(atty::Stream::Stdin) {
130 anyhow::bail!("Refusing to read from standard input as no path is given, but it's a terminal.")
131 }
132 PathOrRead::Read(Box::new(std::io::stdin()))
133 };
126 core::pack::index::from_pack(134 core::pack::index::from_pack(
127 pack_path,135 input,
128 directory,136 directory,
129 git_features::progress::DoOrDiscard::from(progress),137 git_features::progress::DoOrDiscard::from(progress),
130 core::pack::index::Context {138 core::pack::index::Context {