Skip to content

Commit ae256db

Browse files
author
Stephan Dilly
authored
expose diffline origin value so i do not need to hardcode char's but can use actual typed enum values (#679)
1 parent 8d22454 commit ae256db

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

Diff for: src/diff.rs

+86-1
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,61 @@ impl<'diff> DoubleEndedIterator for Deltas<'diff> {
951951
}
952952
impl<'diff> ExactSizeIterator for Deltas<'diff> {}
953953

954+
/// Line origin constants.
955+
#[derive(Copy, Clone, Debug, PartialEq)]
956+
pub enum DiffLineType {
957+
/// These values will be sent to `git_diff_line_cb` along with the line
958+
Context,
959+
///
960+
Addition,
961+
///
962+
Deletion,
963+
/// Both files have no LF at end
964+
ContextEOFNL,
965+
/// Old has no LF at end, new does
966+
AddEOFNL,
967+
/// Old has LF at end, new does not
968+
DeleteEOFNL,
969+
/// The following values will only be sent to a `git_diff_line_cb` when
970+
/// the content of a diff is being formatted through `git_diff_print`.
971+
FileHeader,
972+
///
973+
HunkHeader,
974+
/// For "Binary files x and y differ"
975+
Binary,
976+
}
977+
978+
impl Binding for DiffLineType {
979+
type Raw = raw::git_diff_line_t;
980+
unsafe fn from_raw(raw: raw::git_diff_line_t) -> Self {
981+
match raw {
982+
raw::GIT_DIFF_LINE_CONTEXT => DiffLineType::Context,
983+
raw::GIT_DIFF_LINE_ADDITION => DiffLineType::Addition,
984+
raw::GIT_DIFF_LINE_DELETION => DiffLineType::Deletion,
985+
raw::GIT_DIFF_LINE_CONTEXT_EOFNL => DiffLineType::ContextEOFNL,
986+
raw::GIT_DIFF_LINE_ADD_EOFNL => DiffLineType::AddEOFNL,
987+
raw::GIT_DIFF_LINE_DEL_EOFNL => DiffLineType::DeleteEOFNL,
988+
raw::GIT_DIFF_LINE_FILE_HDR => DiffLineType::FileHeader,
989+
raw::GIT_DIFF_LINE_HUNK_HDR => DiffLineType::HunkHeader,
990+
raw::GIT_DIFF_LINE_BINARY => DiffLineType::Binary,
991+
_ => panic!("Unknown git diff line type"),
992+
}
993+
}
994+
fn raw(&self) -> raw::git_diff_line_t {
995+
match *self {
996+
DiffLineType::Context => raw::GIT_DIFF_LINE_CONTEXT,
997+
DiffLineType::Addition => raw::GIT_DIFF_LINE_ADDITION,
998+
DiffLineType::Deletion => raw::GIT_DIFF_LINE_DELETION,
999+
DiffLineType::ContextEOFNL => raw::GIT_DIFF_LINE_CONTEXT_EOFNL,
1000+
DiffLineType::AddEOFNL => raw::GIT_DIFF_LINE_ADD_EOFNL,
1001+
DiffLineType::DeleteEOFNL => raw::GIT_DIFF_LINE_DEL_EOFNL,
1002+
DiffLineType::FileHeader => raw::GIT_DIFF_LINE_FILE_HDR,
1003+
DiffLineType::HunkHeader => raw::GIT_DIFF_LINE_HUNK_HDR,
1004+
DiffLineType::Binary => raw::GIT_DIFF_LINE_BINARY,
1005+
}
1006+
}
1007+
}
1008+
9541009
impl<'a> DiffLine<'a> {
9551010
/// Line number in old file or `None` for added line
9561011
pub fn old_lineno(&self) -> Option<u32> {
@@ -988,6 +1043,12 @@ impl<'a> DiffLine<'a> {
9881043
}
9891044
}
9901045

1046+
/// origin of this `DiffLine`.
1047+
///
1048+
pub fn origin_value(&self) -> DiffLineType {
1049+
unsafe { Binding::from_raw((*self.raw).origin as raw::git_diff_line_t) }
1050+
}
1051+
9911052
/// Sigil showing the origin of this `DiffLine`.
9921053
///
9931054
/// * ` ` - Line context
@@ -1475,7 +1536,7 @@ impl DiffPatchidOptions {
14751536

14761537
#[cfg(test)]
14771538
mod tests {
1478-
use crate::{DiffOptions, Oid, Signature, Time};
1539+
use crate::{DiffLineType, DiffOptions, Oid, Signature, Time};
14791540
use std::borrow::Borrow;
14801541
use std::fs::File;
14811542
use std::io::Write;
@@ -1726,4 +1787,28 @@ mod tests {
17261787
assert_eq!(line.trim(), "")
17271788
}
17281789
}
1790+
1791+
#[test]
1792+
fn foreach_diff_line_origin_value() {
1793+
let foo_path = Path::new("foo");
1794+
let (td, repo) = crate::test::repo_init();
1795+
t!(t!(File::create(&td.path().join(foo_path))).write_all(b"bar\n"));
1796+
let mut index = t!(repo.index());
1797+
t!(index.add_path(foo_path));
1798+
let mut opts = DiffOptions::new();
1799+
opts.include_untracked(true);
1800+
let diff = t!(repo.diff_tree_to_index(None, Some(&index), Some(&mut opts)));
1801+
let mut origin_values: Vec<DiffLineType> = Vec::new();
1802+
t!(diff.foreach(
1803+
&mut |_file, _progress| { true },
1804+
None,
1805+
None,
1806+
Some(&mut |_file, _hunk, line| {
1807+
origin_values.push(line.origin_value());
1808+
true
1809+
})
1810+
));
1811+
assert_eq!(origin_values.len(), 1);
1812+
assert_eq!(origin_values[0], DiffLineType::Addition);
1813+
}
17291814
}

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub use crate::cred::{Cred, CredentialHelper};
9292
pub use crate::describe::{Describe, DescribeFormatOptions, DescribeOptions};
9393
pub use crate::diff::{Deltas, Diff, DiffDelta, DiffFile, DiffOptions};
9494
pub use crate::diff::{DiffBinary, DiffBinaryFile, DiffBinaryKind};
95-
pub use crate::diff::{DiffFindOptions, DiffHunk, DiffLine, DiffStats};
95+
pub use crate::diff::{DiffFindOptions, DiffHunk, DiffLine, DiffLineType, DiffStats};
9696
pub use crate::error::Error;
9797
pub use crate::index::{
9898
Index, IndexConflict, IndexConflicts, IndexEntries, IndexEntry, IndexMatchedPath,

0 commit comments

Comments
 (0)