Skip to content

Commit

Permalink
Merge pull request #23 from brigand/diff-untracked-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
brigand authored Jun 2, 2021
2 parents c10625f + 2e5c5b7 commit b16196b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::env::current_dir;
use std::ffi::OsStr;
use std::fmt;
use std::io::Read;
use std::io::{self, BufRead, BufReader};
use std::os::unix::prelude::OsStrExt;
use std::path::Path;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::thread::spawn;

mod parse_log;

Expand Down Expand Up @@ -145,6 +149,51 @@ impl Git {
Ok(())
}

/// Prints the
pub fn directory_untracked_less(&self, dir: &Path) -> io::Result<()> {
let ls = Command::new("git")
.current_dir(&self.repo_root)
.arg("ls-files")
.arg("--others")
.arg("--exclude-standard")
.arg("--")
.arg(dir)
.stdout(Stdio::piped())
.spawn()?;

let ls_stdout = ls.stdout.ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "failed to get stdout of git diff")
})?;

let message = format!(
"= Contents of {} =",
String::from_utf8_lossy(dir.as_os_str().as_bytes())
);
let prefix = format!(
"{bar}\n{message}\n{bar}\n\n",
message = message,
bar = "=".repeat(message.len())
);

let mut less = Command::new("less")
.arg("-R")
.current_dir(&self.repo_root)
.stdin(Stdio::piped())
.spawn()?;

let mut stdin = less.stdin.take().expect("Failed to open stdin");
spawn(move || {
let suffix = "\n= End =\n";
let prefix = prefix.into_bytes();
let mut input = prefix.chain(ls_stdout).chain(suffix.as_bytes());
let _r = io::copy(&mut input, &mut stdin);
});

less.wait()?;

Ok(())
}

pub fn diff_less<I>(&self, files: impl IntoIterator<Item = I>) -> io::Result<()>
where
I: AsRef<OsStr>,
Expand Down
10 changes: 8 additions & 2 deletions src/prompt/files_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ impl<'a> FilesPrompt<'a> {
.nth(index - 1)
.expect("diff should match a file");

if option.is_new() && !option.is_dir() {
let _r = self.git.less(option.file_name());
if option.is_new() {
if option.is_dir() {
let _r = self
.git
.directory_untracked_less(option.file_name().as_ref());
} else {
let _r = self.git.less(option.file_name());
}
} else {
let files = vec![option.file_name().to_string()];
let _r = self.git.diff_less(files);
Expand Down

0 comments on commit b16196b

Please sign in to comment.