Skip to content

Commit

Permalink
Merge pull request #7 from ErichDonGubler/files-args
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Jul 11, 2023
2 parents 0c472d8 + 8b76dc2 commit ab3b46a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
ffi::OsStr,
io::{self, Cursor},
process::{exit, Command, ExitStatus, Output},
};
Expand Down Expand Up @@ -76,9 +77,10 @@ impl From<ExecuteError<RunErrorKind>> for Error {

pub type Result<T> = std::result::Result<T, Error>;

pub fn show_graph<'a, I>(format: Option<String>, object_names: I) -> Result<()>
pub fn show_graph<'a, Os, Fs>(format: Option<String>, object_names: Os, files: Fs) -> Result<()>
where
I: IntoIterator<Item = &'a str> + Clone,
Os: IntoIterator<Item = &'a str> + Clone,
Fs: IntoIterator<Item = &'a OsStr> + Clone,
{
let merge_base = {
let mut output = stdout_lines(EasyCommand::new_with("git", |cmd| {
Expand Down Expand Up @@ -123,6 +125,7 @@ where
.arg(format!("^{merge_base}^@"))
.args(object_names.clone().into_iter())
.arg("--") // Make it unambiguous that we're specifying branches first
.args(files)
})
.spawn_and_wait()
.map_err(Into::into)
Expand Down
45 changes: 37 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: move to `src/bin.rs`
use std::process::Command;
use std::{ffi::OsString, process::Command};

use clap::Parser;
use ezcmd::EasyCommand;
Expand All @@ -24,15 +24,21 @@ enum Subcommand {
base: Option<String>,
#[clap(flatten)]
config: PresetConfig,
#[clap(flatten)]
files: FileSelection,
},
/// Display local branches and, optionally, their upstreams.
Locals {
#[clap(flatten)]
config: PresetConfig,
#[clap(flatten)]
files: FileSelection,
},
Select {
/// Additional branches to include.
branches: Vec<String>,
#[clap(flatten)]
files: FileSelection,
},
}

Expand All @@ -49,6 +55,13 @@ struct PresetConfig {
select_last_tag: bool,
}

#[derive(Debug, Parser)]
struct FileSelection {
/// Files by which to filter history.
#[clap(raw(true))]
files: Vec<OsString>,
}

fn main() {
run(|| {
let Args { format, subcommand } = Args::parse();
Expand All @@ -59,6 +72,7 @@ fn main() {
select_pushes: false,
select_last_tag: false,
},
files: FileSelection { files: vec![] },
});
let current_branch = || {
stdout_lines(EasyCommand::new_with("git", |cmd| {
Expand Down Expand Up @@ -115,8 +129,12 @@ fn main() {

Ok(branches)
};
let branches = match subcommand {
Subcommand::Stack { base, config } => {
let (branches, files) = match subcommand {
Subcommand::Stack {
base,
config,
files: FileSelection { files },
} => {
let specified_base = base
.map(Ok)
.or_else(|| git_config("glimpse.base").transpose())
Expand All @@ -127,7 +145,7 @@ fn main() {
default
});

if let Some(current_branch) = current_branch()? {
let branches = if let Some(current_branch) = current_branch()? {
let mut config = config;
if current_branch == base {
config.select_upstreams = true;
Expand All @@ -142,12 +160,23 @@ fn main() {
let mut branches = branches(&config, &|cmd| cmd.arg(base))?;
branches.push("HEAD".to_owned());
branches
}
};
(branches, files)
}
Subcommand::Locals { config } => branches(&config, &|cmd| cmd)?,
Subcommand::Select { branches } => branches,
Subcommand::Locals {
config,
files: FileSelection { files },
} => (branches(&config, &|cmd| cmd)?, files),
Subcommand::Select {
branches,
files: FileSelection { files },
} => (branches, files),
};
log::debug!("showing graph for branches {branches:?}");
show_graph(format, branches.iter().map(|s| s.as_str()))
show_graph(
format,
branches.iter().map(|s| s.as_str()),
files.iter().map(|f| f.as_os_str()),
)
})
}

0 comments on commit ab3b46a

Please sign in to comment.