Skip to content

Fix 576 stash apply conflicts #578

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

Merged
merged 4 commits into from
Mar 9, 2021
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
81 changes: 78 additions & 3 deletions asyncgit/src/sync/stash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::{utils::repo, CommitId};
use crate::error::{Error, Result};
use git2::{Oid, Repository, StashFlags};
use git2::{
build::CheckoutBuilder, Oid, Repository, StashApplyOptions,
StashFlags,
};
use scopetime::scope_time;

///
Expand Down Expand Up @@ -45,14 +48,20 @@ pub fn stash_drop(repo_path: &str, stash_id: CommitId) -> Result<()> {
pub fn stash_apply(
repo_path: &str,
stash_id: CommitId,
allow_conflicts: bool,
) -> Result<()> {
scope_time!("stash_apply");

let mut repo = repo(repo_path)?;

let index = get_stash_index(&mut repo, stash_id.get_oid())?;

repo.stash_apply(index, None)?;
let mut checkout = CheckoutBuilder::new();
checkout.allow_conflicts(allow_conflicts);

let mut opt = StashApplyOptions::default();
opt.checkout_options(checkout);
repo.stash_apply(index, Some(&mut opt))?;

Ok(())
}
Expand Down Expand Up @@ -109,7 +118,11 @@ mod tests {
use super::*;
use crate::sync::{
commit, get_commit_files, get_commits_info, stage_add_file,
tests::{debug_cmd_print, get_statuses, repo_init},
staging::repo_write_file,
tests::{
debug_cmd_print, get_statuses, repo_init,
write_commit_file,
},
};
use std::{fs::File, io::Write, path::Path};

Expand Down Expand Up @@ -211,4 +224,66 @@ mod tests {

Ok(())
}

#[test]
fn test_stash_apply_conflict() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();

repo_write_file(&repo, "test.txt", "test").unwrap();

let id =
stash_save(repo_path, Some("foo"), true, false).unwrap();

repo_write_file(&repo, "test.txt", "foo").unwrap();

let res = stash_apply(repo_path, id, false);

assert!(res.is_err());
}

#[test]
fn test_stash_apply_conflict2() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();

write_commit_file(&repo, "test.txt", "test", "c1");

repo_write_file(&repo, "test.txt", "test2").unwrap();

let id =
stash_save(repo_path, Some("foo"), true, false).unwrap();

repo_write_file(&repo, "test.txt", "test3").unwrap();

let res = stash_apply(repo_path, id, false);

assert!(res.is_err());
}

#[test]
fn test_stash_apply_creating_conflict() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();

write_commit_file(&repo, "test.txt", "test", "c1");

repo_write_file(&repo, "test.txt", "test2").unwrap();

let id =
stash_save(repo_path, Some("foo"), true, false).unwrap();

repo_write_file(&repo, "test.txt", "test3").unwrap();

let res = stash_apply(repo_path, id, false);

assert!(res.is_err());

let res = stash_apply(repo_path, id, true);

assert!(res.is_ok());
}
}
4 changes: 4 additions & 0 deletions asyncgit/src/sync/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum StatusItemType {
Renamed,
///
Typechange,
///
Conflicted,
}

impl From<Status> for StatusItemType {
Expand All @@ -30,6 +32,8 @@ impl From<Status> for StatusItemType {
Self::Renamed
} else if s.is_index_typechange() || s.is_wt_typechange() {
Self::Typechange
} else if s.is_conflicted() {
Self::Conflicted
} else {
Self::Modified
}
Expand Down
1 change: 1 addition & 0 deletions src/components/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl FileTreeComponent {
StatusItemType::Deleted => '-',
StatusItemType::Renamed => 'R',
StatusItemType::Typechange => ' ',
StatusItemType::Conflicted => '!',
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tabs/stashlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl StashList {

fn apply_stash(&mut self) {
if let Some(e) = self.list.selected_entry() {
match sync::stash_apply(CWD, e.id) {
match sync::stash_apply(CWD, e.id, false) {
Ok(_) => {
self.queue
.borrow_mut()
Expand Down
3 changes: 3 additions & 0 deletions src/ui/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ impl Theme {
StatusItemType::Renamed => {
Style::default().fg(self.diff_file_moved)
}
StatusItemType::Conflicted => Style::default()
.fg(self.diff_file_modified)
.add_modifier(Modifier::BOLD),
StatusItemType::Typechange => Style::default(),
};

Expand Down