Skip to content

Commit

Permalink
refactor: inline cmd() & cmd_arg()
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed May 21, 2024
1 parent b517a84 commit adfb60c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 72 deletions.
81 changes: 47 additions & 34 deletions src/ops/discard.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
use super::{cmd, cmd_arg, Action, OpTrait};
use crate::items::TargetData;
use super::{Action, OpTrait};
use crate::{git::diff::Hunk, items::TargetData};
use derive_more::Display;
use std::{ffi::OsStr, process::Command};
use std::{path::PathBuf, process::Command, rc::Rc};

#[derive(Display)]
#[display(fmt = "Discard")]
pub(crate) struct Discard;
impl OpTrait for Discard {
fn get_action(&self, target: Option<&TargetData>) -> Option<Action> {
let action = match target.cloned() {
Some(TargetData::Branch(r)) => cmd_arg(discard_branch, r.into()),
Some(TargetData::File(f)) => cmd_arg(clean_file_cmd, f.into()),
Some(TargetData::Branch(branch)) => discard_branch(branch),
Some(TargetData::File(file)) => clean_file(file),
Some(TargetData::Delta(d)) => match d.status {
git2::Delta::Added => cmd_arg(remove_file_cmd, d.new_file.into()),
_ => cmd_arg(checkout_file_cmd, d.old_file.into()),
git2::Delta::Added => remove_file(d.new_file),
_ => checkout_file(d.old_file),
},
Some(TargetData::Hunk(h)) => {
cmd(h.format_patch().into_bytes(), discard_unstaged_patch_cmd)
}
Some(TargetData::Hunk(h)) => discard_unstaged_patch(h),
_ => return None,
};

Expand All @@ -29,36 +27,51 @@ impl OpTrait for Discard {
}
}

fn discard_unstaged_patch_cmd() -> Command {
let mut cmd = Command::new("git");
cmd.args(["apply", "--reverse"]);
cmd
fn discard_branch(branch: String) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["branch", "-d"]);
cmd.arg(&branch);

state.run_cmd(term, &[], cmd)
})
}

fn clean_file_cmd(file: &OsStr) -> Command {
let mut cmd = Command::new("git");
cmd.args(["clean", "--force"]);
cmd.arg(file);
cmd
fn clean_file(file: PathBuf) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["clean", "--force"]);
cmd.arg(&file);

state.run_cmd(term, &[], cmd)
})
}

fn remove_file_cmd(file: &OsStr) -> Command {
let mut cmd = Command::new("git");
cmd.args(["rm", "--force"]);
cmd.arg(file);
cmd
fn remove_file(file: PathBuf) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["rm", "--force"]);
cmd.arg(&file);

state.run_cmd(term, &[], cmd)
})
}

fn checkout_file_cmd(file: &OsStr) -> Command {
let mut cmd = Command::new("git");
cmd.args(["checkout", "HEAD", "--"]);
cmd.arg(file);
cmd
fn checkout_file(file: PathBuf) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["checkout", "HEAD", "--"]);
cmd.arg(&file);

state.run_cmd(term, &[], cmd)
})
}

fn discard_branch(branch: &OsStr) -> Command {
let mut cmd = Command::new("git");
cmd.args(["branch", "-d"]);
cmd.arg(branch);
cmd
fn discard_unstaged_patch(h: Rc<Hunk>) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["apply", "--reverse"]);

state.run_cmd(term, &h.format_patch().into_bytes(), cmd)
})
}
15 changes: 1 addition & 14 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ use crate::{
cmd_log::CmdLogEntry, items::TargetData, menu::Menu, prompt::PromptData, state::State,
term::Term, Res,
};
use std::{
ffi::{OsStr, OsString},
fmt::Display,
process::Command,
rc::Rc,
};
use std::{ffi::OsString, fmt::Display, rc::Rc};

pub(crate) mod checkout;
pub(crate) mod commit;
Expand Down Expand Up @@ -185,14 +180,6 @@ impl Display for Menu {
}
}

pub(crate) fn cmd(input: Vec<u8>, command: fn() -> Command) -> Action {
Rc::new(move |state, term| state.run_cmd(term, &input, command()))
}

pub(crate) fn cmd_arg(command: fn(&OsStr) -> Command, arg: OsString) -> Action {
Rc::new(move |state, term| state.run_cmd(term, &[], command(&arg)))
}

pub(crate) fn create_y_n_prompt(mut action: Action, prompt: &'static str) -> Action {
let update_fn = Rc::new(move |state: &mut State, term: &mut Term| {
if state.prompt.state.status().is_pending() {
Expand Down
64 changes: 40 additions & 24 deletions src/ops/stage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use super::{cmd, cmd_arg, OpTrait};
use crate::{git::diff::PatchMode, items::TargetData, state::State, term::Term, Action};
use super::OpTrait;
use crate::{
git::diff::{Hunk, PatchMode},
items::TargetData,
state::State,
term::Term,
Action,
};
use derive_more::Display;
use std::{ffi::OsStr, process::Command, rc::Rc};
use std::{ffi::OsString, process::Command, rc::Rc};

#[derive(Display)]
#[display(fmt = "Stage")]
Expand All @@ -11,14 +17,10 @@ impl OpTrait for Stage {
let action = match target.cloned() {
Some(TargetData::AllUnstaged) => stage_unstaged(),
Some(TargetData::AllUntracked(untracked)) => stage_untracked(untracked),
Some(TargetData::File(u)) => cmd_arg(stage_file_cmd, u.into()),
Some(TargetData::Delta(d)) => cmd_arg(stage_file_cmd, d.new_file.into()),
Some(TargetData::Hunk(h)) => cmd(h.format_patch().into_bytes(), stage_patch_cmd),
Some(TargetData::HunkLine(h, i)) => cmd(
h.format_line_patch(i..(i + 1), PatchMode::Normal)
.into_bytes(),
stage_line_cmd,
),
Some(TargetData::File(u)) => stage_file(u.into()),
Some(TargetData::Delta(d)) => stage_file(d.new_file.into()),
Some(TargetData::Hunk(h)) => stage_patch(h),
Some(TargetData::HunkLine(h, i)) => stage_line(h, i),
_ => return None,
};

Expand All @@ -33,6 +35,7 @@ fn stage_unstaged() -> Action {
Rc::new(move |state: &mut State, term: &mut Term| {
let mut cmd = Command::new("git");
cmd.args(["add", "-u", "."]);

state.run_cmd(term, &[], cmd)
})
}
Expand All @@ -42,25 +45,38 @@ fn stage_untracked(untracked: Vec<std::path::PathBuf>) -> Action {
let mut cmd = Command::new("git");
cmd.arg("add");
cmd.args(untracked.clone());

state.run_cmd(term, &[], cmd)
})
}

fn stage_file_cmd(file: &OsStr) -> Command {
let mut cmd = Command::new("git");
cmd.args(["add"]);
cmd.arg(file);
cmd
fn stage_file(file: OsString) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["add"]);
cmd.arg(&file);

state.run_cmd(term, &[], cmd)
})
}

fn stage_patch_cmd() -> Command {
let mut cmd = Command::new("git");
cmd.args(["apply", "--cached"]);
cmd
fn stage_patch(h: Rc<Hunk>) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["apply", "--cached"]);

state.run_cmd(term, &h.format_patch().into_bytes(), cmd)
})
}

fn stage_line_cmd() -> Command {
let mut cmd = Command::new("git");
cmd.args(["apply", "--cached", "--recount"]);
cmd
fn stage_line(h: Rc<Hunk>, i: usize) -> Action {
Rc::new(move |state, term| {
let mut cmd = Command::new("git");
cmd.args(["apply", "--cached", "--recount"]);

let input = h
.format_line_patch(i..(i + 1), PatchMode::Normal)
.into_bytes();
state.run_cmd(term, &input, cmd)
})
}

0 comments on commit adfb60c

Please sign in to comment.