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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use libc::{c_int, c_void};
use std::path::Path;
use std::ptr;
use crate::diff::{print_cb, LineCb};
use crate::util::{into_opt_c_string, Binding};
use crate::{raw, Blob, Buf, Diff, DiffDelta, DiffHunk, DiffLine, DiffOptions, Error};
pub struct Patch {
raw: *mut raw::git_patch,
}
unsafe impl Send for Patch {}
impl Binding for Patch {
type Raw = *mut raw::git_patch;
unsafe fn from_raw(raw: Self::Raw) -> Patch {
Patch { raw: raw }
}
fn raw(&self) -> Self::Raw {
self.raw
}
}
impl Drop for Patch {
fn drop(&mut self) {
unsafe { raw::git_patch_free(self.raw) }
}
}
impl Patch {
pub fn from_diff(diff: &Diff<'_>, idx: usize) -> Result<Option<Patch>, Error> {
let mut ret = ptr::null_mut();
unsafe {
try_call!(raw::git_patch_from_diff(&mut ret, diff.raw(), idx));
Ok(Binding::from_raw_opt(ret))
}
}
pub fn from_blobs(
old_blob: &Blob<'_>,
old_path: Option<&Path>,
new_blob: &Blob<'_>,
new_path: Option<&Path>,
opts: Option<&mut DiffOptions>,
) -> Result<Patch, Error> {
let mut ret = ptr::null_mut();
let old_path = into_opt_c_string(old_path)?;
let new_path = into_opt_c_string(new_path)?;
unsafe {
try_call!(raw::git_patch_from_blobs(
&mut ret,
old_blob.raw(),
old_path,
new_blob.raw(),
new_path,
opts.map(|s| s.raw())
));
Ok(Binding::from_raw(ret))
}
}
pub fn from_blob_and_buffer(
old_blob: &Blob<'_>,
old_path: Option<&Path>,
new_buffer: &[u8],
new_path: Option<&Path>,
opts: Option<&mut DiffOptions>,
) -> Result<Patch, Error> {
let mut ret = ptr::null_mut();
let old_path = into_opt_c_string(old_path)?;
let new_path = into_opt_c_string(new_path)?;
unsafe {
try_call!(raw::git_patch_from_blob_and_buffer(
&mut ret,
old_blob.raw(),
old_path,
new_buffer.as_ptr() as *const c_void,
new_buffer.len(),
new_path,
opts.map(|s| s.raw())
));
Ok(Binding::from_raw(ret))
}
}
pub fn from_buffers(
old_buffer: &[u8],
old_path: Option<&Path>,
new_buffer: &[u8],
new_path: Option<&Path>,
opts: Option<&mut DiffOptions>,
) -> Result<Patch, Error> {
crate::init();
let mut ret = ptr::null_mut();
let old_path = into_opt_c_string(old_path)?;
let new_path = into_opt_c_string(new_path)?;
unsafe {
try_call!(raw::git_patch_from_buffers(
&mut ret,
old_buffer.as_ptr() as *const c_void,
old_buffer.len(),
old_path,
new_buffer.as_ptr() as *const c_void,
new_buffer.len(),
new_path,
opts.map(|s| s.raw())
));
Ok(Binding::from_raw(ret))
}
}
pub fn delta(&self) -> DiffDelta<'_> {
unsafe { Binding::from_raw(raw::git_patch_get_delta(self.raw) as *mut _) }
}
pub fn num_hunks(&self) -> usize {
unsafe { raw::git_patch_num_hunks(self.raw) }
}
pub fn line_stats(&self) -> Result<(usize, usize, usize), Error> {
let mut context = 0;
let mut additions = 0;
let mut deletions = 0;
unsafe {
try_call!(raw::git_patch_line_stats(
&mut context,
&mut additions,
&mut deletions,
self.raw
));
}
Ok((context, additions, deletions))
}
pub fn hunk(&self, hunk_idx: usize) -> Result<(DiffHunk<'_>, usize), Error> {
let mut ret = ptr::null();
let mut lines = 0;
unsafe {
try_call!(raw::git_patch_get_hunk(
&mut ret, &mut lines, self.raw, hunk_idx
));
Ok((Binding::from_raw(ret), lines))
}
}
pub fn num_lines_in_hunk(&self, hunk_idx: usize) -> Result<usize, Error> {
unsafe { Ok(try_call!(raw::git_patch_num_lines_in_hunk(self.raw, hunk_idx)) as usize) }
}
pub fn line_in_hunk(
&self,
hunk_idx: usize,
line_of_hunk: usize,
) -> Result<DiffLine<'_>, Error> {
let mut ret = ptr::null();
unsafe {
try_call!(raw::git_patch_get_line_in_hunk(
&mut ret,
self.raw,
hunk_idx,
line_of_hunk
));
Ok(Binding::from_raw(ret))
}
}
pub fn size(
&self,
include_context: bool,
include_hunk_headers: bool,
include_file_headers: bool,
) -> usize {
unsafe {
raw::git_patch_size(
self.raw,
include_context as c_int,
include_hunk_headers as c_int,
include_file_headers as c_int,
)
}
}
pub fn print(&mut self, mut line_cb: &mut LineCb<'_>) -> Result<(), Error> {
let ptr = &mut line_cb as *mut _ as *mut c_void;
unsafe {
try_call!(raw::git_patch_print(self.raw, print_cb, ptr));
Ok(())
}
}
pub fn to_buf(&mut self) -> Result<Buf, Error> {
let buf = Buf::new();
unsafe {
try_call!(raw::git_patch_to_buf(buf.raw(), self.raw));
}
Ok(buf)
}
}
impl std::fmt::Debug for Patch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
let mut ds = f.debug_struct("Patch");
ds.field("delta", &self.delta())
.field("num_hunks", &self.num_hunks());
if let Ok(line_stats) = &self.line_stats() {
ds.field("line_stats", line_stats);
}
ds.finish()
}
}