Skip to content

Commit

Permalink
Use git2::DiffLineType enum instead of char values (#668)
Browse files Browse the repository at this point in the history
closes #655
  • Loading branch information
wandernauta authored and Stephan Dilly committed May 27, 2021
1 parent 5d62149 commit 2742cdd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
22 changes: 14 additions & 8 deletions asyncgit/src/sync/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ pub enum DiffLineType {
Delete,
}

impl From<git2::DiffLineType> for DiffLineType {
fn from(line_type: git2::DiffLineType) -> Self {
match line_type {
git2::DiffLineType::HunkHeader => Self::Header,
git2::DiffLineType::DeleteEOFNL
| git2::DiffLineType::Deletion => Self::Delete,
git2::DiffLineType::AddEOFNL
| git2::DiffLineType::Addition => Self::Add,
_ => Self::None,
}
}
}

impl Default for DiffLineType {
fn default() -> Self {
Self::None
Expand Down Expand Up @@ -237,18 +250,11 @@ fn raw_diff_to_file_diff<'a>(
}
}

let line_type = match line.origin() {
'H' => DiffLineType::Header,
'<' | '-' => DiffLineType::Delete,
'>' | '+' => DiffLineType::Add,
_ => DiffLineType::None,
};

let diff_line = DiffLine {
position: DiffLinePosition::from(&line),
content: String::from_utf8_lossy(line.content())
.to_string(),
line_type,
line_type: line.origin_value().into(),
};

current_lines.push(diff_line);
Expand Down
30 changes: 20 additions & 10 deletions asyncgit/src/sync/staging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
diff::DiffLinePosition, patches::HunkLines, utils::work_dir,
};
use crate::error::Result;
use git2::{DiffLine, Repository};
use git2::{DiffLine, DiffLineType, Repository};
use std::{
collections::HashSet, convert::TryFrom, fs::File, io::Read,
};
Expand Down Expand Up @@ -82,8 +82,16 @@ pub(crate) fn apply_selection(
let mut new_content = NewFromOldContent::default();
let lines = lines.iter().collect::<HashSet<_>>();

let char_added = if reverse { '-' } else { '+' };
let char_deleted = if reverse { '+' } else { '-' };
let added = if reverse {
DiffLineType::Deletion
} else {
DiffLineType::Addition
};
let deleted = if reverse {
DiffLineType::Addition
} else {
DiffLineType::Deletion
};

let mut first_hunk_encountered = false;
for hunk in hunks {
Expand Down Expand Up @@ -122,36 +130,38 @@ pub(crate) fn apply_selection(
.trim()
);

if hunk_line.origin() == '<'
|| hunk_line.origin() == '>'
if hunk_line.origin_value()
== DiffLineType::DeleteEOFNL
|| hunk_line.origin_value()
== DiffLineType::AddEOFNL
{
break;
}

if (is_staged && !selected_line)
|| (!is_staged && selected_line)
{
if hunk_line.origin() == char_added {
if hunk_line.origin_value() == added {
new_content.add_from_hunk(hunk_line)?;
if is_staged {
new_content.skip_old_line();
}
} else if hunk_line.origin() == char_deleted {
} else if hunk_line.origin_value() == deleted {
if !is_staged {
new_content.skip_old_line();
}
} else {
new_content.add_old_line(old_lines);
}
} else {
if hunk_line.origin() != char_added {
if hunk_line.origin_value() != added {
new_content.add_from_hunk(hunk_line)?;
}

if (is_staged
&& hunk_line.origin() != char_deleted)
&& hunk_line.origin_value() != deleted)
|| (!is_staged
&& hunk_line.origin() != char_added)
&& hunk_line.origin_value() != added)
{
new_content.skip_old_line();
}
Expand Down

0 comments on commit 2742cdd

Please sign in to comment.