Skip to content

Commit

Permalink
refactor: remove boilerplate in git/mid.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed Feb 10, 2024
1 parent 55c5bed commit 4a50576
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 64 deletions.
101 changes: 43 additions & 58 deletions src/git/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::Res;
use std::{
error::Error,
path::Path,
process::Command,
str::{self},
str::{self, FromStr},
};

use self::diff::Diff;
Expand All @@ -15,87 +16,47 @@ pub(crate) mod status;
// TODO Use only plumbing commands

pub(crate) fn diff(dir: &Path, args: &[&str]) -> Res<Diff> {
let out = Command::new("git")
.args(&[&["diff"], args].concat())
.current_dir(dir)
.output()?
.stdout;

Ok(str::from_utf8(&out)?.parse()?)
run_git(dir, &["diff"], args)
}

pub(crate) fn diff_unstaged(dir: &Path) -> Res<Diff> {
let out = Command::new("git")
.arg("diff")
.current_dir(dir)
.output()?
.stdout;

Ok(str::from_utf8(&out)?.parse()?)
run_git(dir, &["diff"], &[])
}

pub(crate) fn diff_staged(dir: &Path) -> Res<Diff> {
let out = Command::new("git")
.args(["diff", "--staged"])
.current_dir(dir)
.output()?
.stdout;

Ok(str::from_utf8(&out)?.parse()?)
run_git(dir, &["diff", "--staged"], &[])
}

pub(crate) fn status(dir: &Path) -> Res<status::Status> {
let out = Command::new("git")
.args(["status", "--porcelain", "--branch"])
.current_dir(dir)
.output()?
.stdout;
Ok(str::from_utf8(&out)?.parse()?)
run_git(dir, &["status", "--porcelain", "--branch"], &[])
}

pub(crate) fn status_simple(dir: &Path) -> Res<String> {
let out = Command::new("git")
.args(["-c", "color.status=always", "status"])
.current_dir(dir)
.output()?
.stdout;
Ok(str::from_utf8(&out)?.replace("", ""))
run_git_no_parse(dir, &["-c", "color.status=always", "status"], &[])
}

pub(crate) fn show(dir: &Path, args: &[&str]) -> Res<Diff> {
let out = Command::new("git")
.args(&[&["show"], args].concat())
.current_dir(dir)
.output()?
.stdout;
Ok(str::from_utf8(&out)?.parse()?)
run_git(dir, &["show"], args)
}

pub(crate) fn show_summary(dir: &Path, args: &[&str]) -> Res<String> {
let out = Command::new("git")
.args(&[&["show", "--summary", "--decorate", "--color"], args].concat())
.current_dir(dir)
.output()?
.stdout;
Ok(str::from_utf8(&out)?.replace("", ""))
run_git_no_parse(dir, &["show", "--summary", "--decorate", "--color"], args)
}

// TODO Make this return a more useful type. Vec<Log>?
pub(crate) fn log_recent(dir: &Path) -> Res<String> {
let out = Command::new("git")
.args(["log", "-n", "5", "--oneline", "--decorate", "--color"])
.current_dir(dir)
.output()?
.stdout;
Ok(String::from_utf8(out)?.replace("", ""))
run_git_no_parse(
dir,
&["log", "-n", "5", "--oneline", "--decorate", "--color"],
&[],
)
}
// TODO Make this return a more useful type. Vec<Log>?
pub(crate) fn log(dir: &Path, args: &[&str]) -> Res<String> {
let out = Command::new("git")
.args(&[&["log", "--oneline", "--decorate", "--color"], args].concat())
.current_dir(dir)
.output()?
.stdout;
Ok(str::from_utf8(&out)?.replace("", ""))
run_git_no_parse(dir, &["log", "--oneline", "--decorate", "--color"], args)
}

// TODO Clean this up
pub(crate) fn show_refs(dir: &Path) -> Res<Vec<(String, String, String)>> {
let out = Command::new("git")
.args([
Expand Down Expand Up @@ -193,6 +154,30 @@ pub(crate) fn checkout_ref_cmd(reference: &str) -> Command {
git(&["checkout", reference])
}

fn run_git<T: FromStr<Err = Box<dyn Error>>>(
dir: &Path,
args: &[&str],
meta_args: &[&str],
) -> Res<T> {
let out = Command::new("git")
.args(&[args, meta_args].concat())
.current_dir(dir)
.output()?
.stdout;

Ok(str::from_utf8(&out)?.parse()?)
}

fn run_git_no_parse(dir: &Path, args: &[&str], meta_args: &[&str]) -> Res<String> {
let out = Command::new("git")
.args(&[args, meta_args].concat())
.current_dir(dir)
.output()?
.stdout;

Ok(String::from_utf8(out)?)
}

fn git(args: &[&str]) -> Command {
let mut cmd = Command::new("git");
cmd.args(args);
Expand Down
7 changes: 5 additions & 2 deletions src/git/parse/diff/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use pest::Parser;
use pest_derive::Parser;
use std::str::{self, FromStr};
use std::{
error::Error,
str::{self, FromStr},
};

use crate::git::diff::{Delta, Diff, Hunk};

Expand All @@ -9,7 +12,7 @@ use crate::git::diff::{Delta, Diff, Hunk};
struct DiffParser;

impl FromStr for Diff {
type Err = pest::error::Error<Rule>;
type Err = Box<dyn Error>;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut deltas = vec![];
Expand Down
4 changes: 2 additions & 2 deletions src/git/parse/status/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{error::Error, str::FromStr};

use pest::Parser;
use pest_derive::Parser;
Expand All @@ -10,7 +10,7 @@ use crate::git::status::{BranchStatus, Status, StatusFile};
struct StatusParser;

impl FromStr for Status {
type Err = pest::error::Error<Rule>;
type Err = Box<dyn Error>;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut local = None;
Expand Down
2 changes: 1 addition & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub(crate) fn create_log_items(log: &str) -> impl Iterator<Item = Item> + '_ {
Item {
id: log_line.to_string().into(),
display: log_line
.to_string()
.replace("[m", "[0m")
.into_text()
.expect("Error creating log text"),
depth: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/screen/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn create(config: &Config, size: Rect, args: Vec<String>) -> Res<Scre
let show = git::show(&config.dir.clone(), &str_args)?;

Ok(iter::once(Item {
display: summary.into_text()?,
display: summary.replace("[m", "[0m").into_text()?,
..Default::default()
})
.chain(items::create_diff_items(&show, &0))
Expand Down
1 change: 1 addition & 0 deletions src/screen/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub(crate) fn create(config: &Config, size: Rect, status: bool) -> Res<Screen> {
.then_some(Item {
id: "status".into(),
display: git::status_simple(&config.dir)?
.replace("", "")
.into_text()
.expect("Error parsing status ansi"),
unselectable: true,
Expand Down

0 comments on commit 4a50576

Please sign in to comment.