Skip to content

Commit

Permalink
Merge pull request #13 from Luladjiev/fix_stashing_of_files
Browse files Browse the repository at this point in the history
Update the way files are stashed and update rebase function to stash them too
  • Loading branch information
Luladjiev authored Feb 17, 2024
2 parents 5ee67cd + 7f5a31a commit 413e1b5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lgit"
version = "0.7.2"
version = "0.7.3"
edition = "2021"
authors = ["Peter Luladjiev<lgit-rs@luladjiev.com>"]
description = "CLI tool for managing git repositories"
Expand Down
13 changes: 5 additions & 8 deletions src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ pub fn run<T: Exec>(
base: &str,
verbose: bool,
) -> Result<(), &'static str> {
stash(command, verbose)?;
let unsaved_changes = stash(command, verbose)?;

refresh_base(command, base, verbose).map_err(|()| "Failed to refresh base branch")?;

command
.exec(&["checkout", "-b", &name], verbose)
.map_err(|()| "Failed to create branch")?;

unstash(command, verbose)?;
if unsaved_changes {
unstash(command, verbose)?;
}

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

Expand All @@ -33,7 +35,7 @@ mod tests {
command
.expect_exec()
.times(1)
.withf(|args, verbose| args == ["stash", "-u"] && !(*verbose))
.withf(|args, verbose| args == ["status", "--porcelain"] && !(*verbose))
.returning(|_, _| Ok(String::new()));
command
.expect_exec()
Expand All @@ -50,11 +52,6 @@ mod tests {
.times(1)
.withf(|args, verbose| args == ["checkout", "-b", "test"] && !(*verbose))
.returning(|_, _| Ok(String::new()));
command
.expect_exec()
.times(1)
.withf(|args, verbose| args == ["stash", "pop"] && !(*verbose))
.returning(|_, _| Ok(String::new()));
assert_eq!(run(&command, "test", "main", false), Ok(()));
}
}
8 changes: 7 additions & 1 deletion src/commands/rebase.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::commands::Exec;
use crate::utils::refresh_base;
use crate::utils::{refresh_base, stash, unstash};

pub fn run<T: Exec>(command: &T, base: &str, verbose: bool) -> Result<(), &'static str> {
let unsaved_changes = stash(command, verbose)?;

refresh_base(command, base, verbose).map_err(|()| "Failed to refresh base branch")?;

command
Expand All @@ -12,6 +14,10 @@ pub fn run<T: Exec>(command: &T, base: &str, verbose: bool) -> Result<(), &'stat
.exec(&["rebase", base], verbose)
.map_err(|()| "Failed to rebase")?;

if unsaved_changes {
unstash(command, verbose)?;
}

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

Ok(())
Expand Down
12 changes: 10 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ fn search_branch<T: Exec>(command: &T, branch: &str, verbose: bool) -> Result<()
}
}

pub fn stash<T: Exec>(command: &T, verbose: bool) -> Result<(), &'static str> {
pub fn stash<T: Exec>(command: &T, verbose: bool) -> Result<bool, &'static str> {
let result = command
.exec(&["status", "--porcelain"], verbose)
.map_err(|()| "Failed to retrieve branch status")?;

if result.is_empty() {
return Ok(false);
}

command
.exec(&["stash", "-u"], verbose)
.map_err(|()| "Failed to stash changes")?;

Ok(())
Ok(true)
}

pub fn unstash<T: Exec>(command: &T, verbose: bool) -> Result<(), &'static str> {
Expand Down

0 comments on commit 413e1b5

Please sign in to comment.