Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify codebase #7

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/commands/autosquash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@ pub fn run<T: Exec>(
];
let arg: String;

if let Some(number) = number {
arg = format!("HEAD~{number}");

args.push(&arg);
} else {
args.push(base);
match number {
Some(number) => {
arg = format!("HEAD~{number}");
args.push(&arg);
}
None => args.push(base),
}

let result = cmd.exec(&args, verbose);

match result {
Ok(_) => Ok(()),
Err(()) => Err("Failed to auto squash commits"),
}
cmd.exec(&args, verbose)
.map(|_| ())
.map_err(|()| "Failed to auto squash commits")
}

#[cfg(test)]
Expand Down
42 changes: 32 additions & 10 deletions src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,41 @@ pub fn run<T: Exec>(
base: &str,
verbose: bool,
) -> Result<(), &'static str> {
let result = refresh_base(command, base, verbose);
refresh_base(command, base, verbose).map_err(|()| "Failed to refresh base branch")?;

if let Err(()) = result {
return Err("Failed to refresh base branch");
}

let result = command.exec(&["checkout", "-b", &name], verbose);

if let Err(()) = result {
return Err("Failed to create branch");
}
command
.exec(&["checkout", "-b", &name], verbose)
.map_err(|()| "Failed to create branch")?;

println!("Created branch {name}");

Ok(())
}

#[cfg(test)]
mod tests {
use crate::commands::branch::run;
use crate::commands::MockCmd;

#[test]
fn test_run_with_master_branch() {
let mut command = MockCmd::new();
command
.expect_exec()
.times(1)
.withf(|args, verbose| args == ["checkout", "main"] && !(*verbose))
.returning(|_, _| Ok(String::new()));
command
.expect_exec()
.times(1)
.withf(|args, verbose| args == ["pull"] && !(*verbose))
.returning(|_, _| Ok(String::new()));
command
.expect_exec()
.times(1)
.withf(|args, verbose| args == ["checkout", "-b", "test"] && !(*verbose))
.returning(|_, _| Ok(String::new()));

assert_eq!(run(&command, "test", "main", false), Ok(()));
}
}
72 changes: 30 additions & 42 deletions src/commands/delete_branches.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::commands::Exec;

pub fn run<T: Exec>(command: &T, dry_run: bool, verbose: bool) -> Result<(), &str> {
match try_delete_branches(command, dry_run, verbose) {
match delete_branches(command, dry_run, verbose) {
Ok(output) => {
println!("{output}");
Ok(())
Expand All @@ -10,64 +10,52 @@ pub fn run<T: Exec>(command: &T, dry_run: bool, verbose: bool) -> Result<(), &st
}
}

fn try_delete_branches<T: Exec>(
fn delete_branches<T: Exec>(
command: &T,
dry_run: bool,
verbose: bool,
) -> Result<String, &'static str> {
let result = command.exec(&["fetch", "--prune"], verbose);
command
.exec(&["fetch", "--prune"], verbose)
.map_err(|()| "Failed to fetch")?;

if let Err(()) = result {
return Err("Failed to fetch");
}

let branches = command.exec(&["branch", "-vv"], verbose);

let Ok(branches) = branches else {
return Err("Failed to get branches");
};
let branches = command
.exec(&["branch", "-vv"], verbose)
.map_err(|()| "Failed to get branches")?;

let mut result = Vec::new();

for line in branches.lines() {
if line.starts_with('*') {
if line.starts_with('*') || !line.contains(": gone]") {
continue;
}

if line.contains(": gone]") {
let branch_split = line.split_whitespace().next();

let Some(branch_name) = branch_split else {
return Err("Failed to parse branch name");
};

if !dry_run {
let output = command.exec(&["branch", "-D", branch_name], verbose);
let branch_name = line
.split_whitespace()
.next()
.ok_or("Failed to parse branch name")?;

if let Err(()) = output {
return Err("Failed to delete branch");
}
}

result.push(format!("Deleted branch {branch_name}"));
if !dry_run {
command
.exec(&["branch", "-D", branch_name], verbose)
.map_err(|()| "Failed to delete branch")?;
}

result.push(format!("Deleted branch {branch_name}"));
}

let message = if result.is_empty() {
Ok(if result.is_empty() {
"No branches to delete".to_string()
} else {
result.join("\n")
};

Ok(message)
})
}

#[cfg(test)]
mod tests {
use crate::commands::delete_branches::delete_branches;
use crate::commands::MockCmd;

use super::*;

fn cmd_fetch_prune() -> MockCmd {
let mut command = MockCmd::new();
command
Expand All @@ -91,10 +79,10 @@ mod tests {
}

#[test]
fn try_delete_branches_does_not_delete_when_dry_run() {
fn delete_branches_does_not_delete_when_dry_run() {
let command = cmd_fetch_prune_branch();

let result = try_delete_branches(&command, true, false);
let result = delete_branches(&command, true, false);

assert!(result.is_ok());
assert_eq!(
Expand All @@ -104,7 +92,7 @@ mod tests {
}

#[test]
fn try_delete_branches_does_not_delete_current_branch() {
fn delete_branches_does_not_delete_current_branch() {
let mut command = cmd_fetch_prune_branch();
command
.expect_exec()
Expand All @@ -117,7 +105,7 @@ mod tests {
.times(1)
.returning(|_, _| Ok(String::new()));

let result = try_delete_branches(&command, false, false);
let result = delete_branches(&command, false, false);

assert!(result.is_ok());
assert_eq!(
Expand All @@ -127,29 +115,29 @@ mod tests {
}

#[test]
fn try_delete_branches_returns_error_when_delete_fails() {
fn delete_branches_returns_error_when_delete_fails() {
let mut command = cmd_fetch_prune_branch();
command
.expect_exec()
.withf(|args, verbose| args == ["branch", "-D", "branch1"] && !(*verbose))
.times(1)
.returning(|_, _| Err(()));

let result = try_delete_branches(&command, false, false);
let result = delete_branches(&command, false, false);

assert!(result.is_err());
}

#[test]
fn try_delete_branches_no_branches_to_delete() {
fn delete_branches_no_branches_to_delete() {
let mut command = cmd_fetch_prune();
command
.expect_exec()
.withf(|args, verbose| args == ["branch", "-vv"] && !(*verbose))
.times(1)
.returning(|_, _| Ok("* branch3 [origin/branch3]".to_string()));

let result = try_delete_branches(&command, false, false);
let result = delete_branches(&command, false, false);

assert!(result.is_ok());
assert_eq!(result.unwrap(), "No branches to delete");
Expand Down
35 changes: 14 additions & 21 deletions src/commands/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use dialoguer::FuzzySelect;
use crate::commands::Exec;

pub fn run<T: Exec>(command: &T, number: u32, verbose: bool) -> Result<(), &'static str> {
let commit = get_sha(command, number, verbose);
let commit = commit?;

let commit = get_sha(command, number, verbose)?;
let result = command.exec(&["commit", "--fixup", commit.as_str()], verbose);

match result {
Expand All @@ -23,17 +21,15 @@ fn get_sha<T: Exec>(command: &T, number: u32, verbose: bool) -> Result<String, &
.with_prompt("Which commit you want to fix?")
.default(0)
.items(&options)
.interact();

if let Err(err) = option {
if verbose {
println!("{err}");
}
.interact()
.map_err(|err| {
if verbose {
println!("{err}");
}

return Err("There was an error determining the commit");
}
"There was an error determining the commit"
})?;

let option = option.unwrap();
let option = options.get(option);

if option.is_none() {
Expand All @@ -48,16 +44,13 @@ fn get_sha<T: Exec>(command: &T, number: u32, verbose: bool) -> Result<String, &
}

fn get_log<T: Exec>(command: &T, number: u32, verbose: bool) -> Result<Vec<String>, &'static str> {
let log = command.exec(
&["log", "--format=%h %s", "-n", &number.to_string()],
verbose,
);

if let Err(()) = log {
return Err("Failed to fetch git log");
}
let log = command
.exec(
&["log", "--format=%h %s", "-n", &number.to_string()],
verbose,
)
.map_err(|()| "Failed to fetch git log")?;

let log = log.unwrap();
let log = log.lines().map(String::from);
let log = log.collect();

Expand Down
22 changes: 7 additions & 15 deletions src/commands/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ use crate::commands::Exec;
use crate::utils::refresh_base;

pub fn run<T: Exec>(command: &T, base: &str, verbose: bool) -> Result<(), &'static str> {
let result = refresh_base(command, base, verbose);
refresh_base(command, base, verbose).map_err(|()| "Failed to refresh base branch")?;

let Ok(base) = result else {
return Err("Failed to refresh base branch");
};
command
.exec(&["checkout", "-"], verbose)
.map_err(|()| "Failed to checkout back to initial branch")?;

let result = command.exec(&["checkout", "-"], verbose);

if let Err(()) = result {
return Err("Failed to checkout back to initial branch");
}

let result = command.exec(&["rebase", base], verbose);

if let Err(()) = result {
return Err("Failed to rebase");
}
command
.exec(&["rebase", base], verbose)
.map_err(|()| "Failed to rebase")?;

println!("Rebased onto {base}");

Expand Down
33 changes: 10 additions & 23 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,19 @@ pub fn get_base<T: Exec>(command: &T, base: Option<String>, verbose: bool) -> St
}

pub fn refresh_base<'a, T: Exec>(command: &T, base: &'a str, verbose: bool) -> Result<&'a str, ()> {
let result = command.exec(&["checkout", base], verbose);

if let Err(()) = result {
return Err(());
}

let result = command.exec(&["pull"], verbose);

match result {
Ok(_) => Ok(base),
Err(()) => Err(()),
}
command.exec(&["checkout", base], verbose)?;
command.exec(&["pull"], verbose).map(|_| base)
}

fn search_branch<T: Exec>(command: &T, branch: &str, verbose: bool) -> Result<(), &'static str> {
let result = command.exec(&["branch", "-l", branch], verbose);

match result {
Ok(output) => {
if output.is_empty() {
Err("Branch not found")
} else {
Ok(())
}
}
Err(()) => Err("Failed to list branch"),
let result = command
.exec(&["branch", "-l", branch], verbose)
.map_err(|()| "Failed to list branch")?;

if result.is_empty() {
Err("Branch not found")
} else {
Ok(())
}
}

Expand Down