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

fix: Stash and pop any unstaged changes before rebasing #22

Merged
merged 1 commit into from
Mar 4, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Remove last dependency on external git binary, using libgit2 for all git interactions
* Show backtraces on error if RUST_BACKTRACE=1 is in the environment
* Correctly stash and unstash changes before the rebase

# Version 0.2.0

Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,19 @@ pub fn instafix(
let commit_to_amend = select_commit_to_amend(&repo, upstream, max_commits, &message_pattern)?;
eprintln!("Selected {}", disp(&commit_to_amend));
do_fixup_commit(&repo, &head_branch, &commit_to_amend, squash)?;
let needs_stash = worktree_is_dirty(&repo)?;
if needs_stash {
// TODO: is it reasonable to create a new repo to work around lifetime issues?
let mut repo = Repository::open(".")?;
let sig = repo.signature()?.clone();
repo.stash_save(&sig, "git-instafix stashing changes", None)?;
}
let current_branch = Branch::wrap(repo.head()?);
do_rebase(&repo, &current_branch, &commit_to_amend, &diff)?;
if needs_stash {
let mut repo = Repository::open(".")?;
repo.stash_pop(0, None)?;
}

Ok(())
}
Expand Down Expand Up @@ -253,6 +264,16 @@ fn create_diff(repo: &Repository, require_newline: bool) -> Result<Diff, anyhow:
Ok(diff)
}

fn worktree_is_dirty(repo: &Repository) -> Result<bool, anyhow::Error> {
let head = repo.head()?;
let head_tree = head.peel_to_tree()?;
let staged_diff = repo.diff_tree_to_index(Some(&head_tree), None, None)?;
let dirty_diff = repo.diff_index_to_workdir(None, None)?;
let diffstat = staged_diff.stats()?;
let dirty_workdir_stats = dirty_diff.stats()?;
Ok(diffstat.files_changed() > 0 || dirty_workdir_stats.files_changed() > 0)
}

/// Commit the current index as a fixup or squash commit
fn do_fixup_commit<'a>(
repo: &'a Repository,
Expand Down
55 changes: 55 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,57 @@ new
);
}

#[test]
fn stashes_before_rebase() {
let td = assert_fs::TempDir::new().unwrap();
git_init(&td);

git_commits(&["a", "b"], &td);
git(&["checkout", "-b", "changes"], &td);
git(&["branch", "-u", "main"], &td);
git_commits(&["target", "d"], &td);

let log = git_log(&td);
assert_eq!(
log,
"\
* d HEAD -> changes
* target
* b main
* a
",
"log:\n{}",
log
);

td.child("new").touch().unwrap();

let edited_file = "file_d";
td.child(edited_file).write_str("somthing").unwrap();

git(&["add", "new"], &td);
let tracked_changed_files = git_worktree_changed_files(&td);
assert_eq!(tracked_changed_files.trim(), edited_file);

fixup(&td).args(&["-P", "target"]).assert().success();

let (files, err) = git_changed_files("target", &td);

assert_eq!(
files,
"\
file_target
new
",
"out: {} err: {}",
files,
err
);

let popped_stashed_files = git_worktree_changed_files(&td);
assert_eq!(popped_stashed_files.trim(), edited_file);
}

#[test]
fn test_no_commit_in_range() {
let td = assert_fs::TempDir::new().unwrap();
Expand Down Expand Up @@ -254,6 +305,10 @@ fn git_changed_files(name: &str, tempdir: &assert_fs::TempDir) -> (String, Strin
(string(out.stdout), string(out.stderr))
}

fn git_worktree_changed_files(td: &assert_fs::TempDir) -> String {
string(git_out(&["diff", "--name-only"], td).stdout)
}

/// Run git in tempdir with args and panic if theres an error
fn git(args: &[&str], tempdir: &assert_fs::TempDir) {
git_inner(args, tempdir).ok().unwrap();
Expand Down