forked from gitui-org/gitui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatches.rs
82 lines (68 loc) · 1.71 KB
/
patches.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use super::diff::{get_diff_raw, DiffOptions, HunkHeader};
use crate::error::{Error, Result};
use git2::{Diff, DiffLine, Patch, Repository};
#[allow(clippy::redundant_pub_crate)]
pub(crate) struct HunkLines<'a> {
pub hunk: HunkHeader,
pub lines: Vec<DiffLine<'a>>,
}
#[allow(clippy::redundant_pub_crate)]
pub(crate) fn get_file_diff_patch_and_hunklines<'a>(
repo: &'a Repository,
file: &str,
is_staged: bool,
reverse: bool,
) -> Result<(Patch<'a>, Vec<HunkLines<'a>>)> {
let diff = get_diff_raw(
repo,
file,
is_staged,
reverse,
Some(DiffOptions {
context: 1,
..DiffOptions::default()
}),
)?;
let patches = get_patches(&diff)?;
if patches.len() > 1 {
return Err(Error::Generic(String::from("patch error")));
}
let patch = patches.into_iter().next().ok_or_else(|| {
Error::Generic(String::from("no patch found"))
})?;
let lines = patch_get_hunklines(&patch)?;
Ok((patch, lines))
}
//
fn patch_get_hunklines<'a>(
patch: &Patch<'a>,
) -> Result<Vec<HunkLines<'a>>> {
let count_hunks = patch.num_hunks();
let mut res = Vec::with_capacity(count_hunks);
for hunk_idx in 0..count_hunks {
let (hunk, _) = patch.hunk(hunk_idx)?;
let count_lines = patch.num_lines_in_hunk(hunk_idx)?;
let mut hunk = HunkLines {
hunk: HunkHeader::from(hunk),
lines: Vec::with_capacity(count_lines),
};
for line_idx in 0..count_lines {
let line = patch.line_in_hunk(hunk_idx, line_idx)?;
hunk.lines.push(line);
}
res.push(hunk);
}
Ok(res)
}
//
fn get_patches<'a>(diff: &Diff<'a>) -> Result<Vec<Patch<'a>>> {
let count = diff.deltas().len();
let mut res = Vec::with_capacity(count);
for idx in 0..count {
let p = Patch::from_diff(diff, idx)?;
if let Some(p) = p {
res.push(p);
}
}
Ok(res)
}