Skip to content

Commit db76e5a

Browse files
Add bindings for git email create
Add bindings for `git_email_create_from_diff()` and `git_email_create_from_commit()`. Deprecate `git_diff_format_email()` to reflect upstream changes.
1 parent fe71885 commit db76e5a

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

Diff for: libgit2-sys/lib.rs

+41
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,28 @@ pub struct git_message_trailer_array {
19921992
pub _trailer_block: *mut c_char,
19931993
}
19941994

1995+
#[repr(C)]
1996+
pub struct git_email_create_options {
1997+
pub version: c_uint,
1998+
pub flags: u32,
1999+
pub diff_opts: git_diff_options,
2000+
pub diff_find_opts: git_diff_find_options,
2001+
pub subject_prefix: *const c_char,
2002+
pub start_number: usize,
2003+
pub reroll_number: usize,
2004+
}
2005+
2006+
pub const GIT_EMAIL_CREATE_OPTIONS_VERSION: c_uint = 1;
2007+
2008+
git_enum! {
2009+
pub enum git_email_create_flags_t {
2010+
GIT_EMAIL_CREATE_DEFAULT = 0,
2011+
GIT_EMAIL_CREATE_OMIT_NUMBERS = 1 << 0,
2012+
GIT_EMAIL_CREATE_ALWAYS_NUMBER = 1 << 1,
2013+
GIT_EMAIL_CREATE_NO_RENAMES = 1 << 2,
2014+
}
2015+
}
2016+
19952017
extern "C" {
19962018
// threads
19972019
pub fn git_libgit2_init() -> c_int;
@@ -4106,6 +4128,25 @@ extern "C" {
41064128
replace_email: *const c_char,
41074129
) -> c_int;
41084130

4131+
// email
4132+
pub fn git_email_create_from_diff(
4133+
out: *mut git_buf,
4134+
diff: *mut git_diff,
4135+
patch_idx: usize,
4136+
patch_count: usize,
4137+
commit_id: *const git_oid,
4138+
summary: *const c_char,
4139+
body: *const c_char,
4140+
author: *const git_signature,
4141+
given_opts: *const git_email_create_options,
4142+
) -> c_int;
4143+
4144+
pub fn git_email_create_from_commit(
4145+
out: *mut git_buf,
4146+
commit: *mut git_commit,
4147+
given_opts: *const git_email_create_options,
4148+
) -> c_int;
4149+
41094150
pub fn git_trace_set(level: git_trace_level_t, cb: git_trace_cb) -> c_int;
41104151
}
41114152

Diff for: src/diff.rs

+9
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ impl<'repo> Diff<'repo> {
254254
/// Create an e-mail ready patch from a diff.
255255
///
256256
/// Matches the format created by `git format-patch`
257+
#[doc(hidden)]
258+
#[deprecated(note = "refactored to `Email::from_diff` to match upstream")]
257259
pub fn format_email(
258260
&mut self,
259261
patch_no: usize,
@@ -277,6 +279,7 @@ impl<'repo> Diff<'repo> {
277279
raw_opts.body = message.as_ptr() as *const _;
278280
raw_opts.author = commit.author().raw();
279281
let buf = Buf::new();
282+
#[allow(deprecated)]
280283
unsafe {
281284
try_call!(raw::git_diff_format_email(buf.raw(), self.raw, &*raw_opts));
282285
}
@@ -1480,6 +1483,11 @@ impl DiffFindOptions {
14801483
}
14811484

14821485
// TODO: expose git_diff_similarity_metric
1486+
1487+
/// Acquire a pointer to the underlying raw options.
1488+
pub unsafe fn raw(&mut self) -> *const raw::git_diff_find_options {
1489+
&self.raw
1490+
}
14831491
}
14841492

14851493
impl Default for DiffFormatEmailOptions {
@@ -1775,6 +1783,7 @@ mod tests {
17751783
None,
17761784
)
17771785
.unwrap();
1786+
#[allow(deprecated)]
17781787
let actual_email = diff.format_email(1, 1, &updated_commit, None).unwrap();
17791788
let actual_email = actual_email.as_str().unwrap();
17801789
assert!(

Diff for: src/email.rs

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
use std::ffi::CString;
2+
use std::{mem, ptr};
3+
4+
use crate::util::Binding;
5+
use crate::{raw, Buf, Commit, DiffFindOptions, DiffOptions, Error, IntoCString};
6+
use crate::{Diff, Oid, Signature};
7+
8+
/// A structure to represent patch in mbox format for sending via email
9+
pub struct Email {
10+
/// Buffer to store the e-mail patch in
11+
pub buf: Buf,
12+
}
13+
14+
/// Options for controlling the formatting of the generated e-mail.
15+
pub struct EmailCreateOptions {
16+
/// Options to use when creating diffs
17+
pub diff_options: DiffOptions,
18+
/// Options for finding similarities within diffs
19+
pub diff_find_options: DiffFindOptions,
20+
subject_prefix: Option<CString>,
21+
raw: raw::git_email_create_options,
22+
}
23+
24+
impl Default for EmailCreateOptions {
25+
fn default() -> Self {
26+
// Defaults options created in corresponding to `GIT_EMAIL_CREATE_OPTIONS_INIT`
27+
let default_options = raw::git_email_create_options {
28+
version: raw::GIT_EMAIL_CREATE_OPTIONS_VERSION,
29+
flags: raw::GIT_EMAIL_CREATE_DEFAULT as u32,
30+
diff_opts: unsafe { mem::zeroed() },
31+
diff_find_opts: unsafe { mem::zeroed() },
32+
subject_prefix: ptr::null(),
33+
start_number: 1,
34+
reroll_number: 0,
35+
};
36+
let mut diff_options = DiffOptions::new();
37+
diff_options.show_binary(true).context_lines(3);
38+
Self {
39+
diff_options,
40+
diff_find_options: DiffFindOptions::new(),
41+
subject_prefix: None,
42+
raw: default_options,
43+
}
44+
}
45+
}
46+
47+
impl EmailCreateOptions {
48+
/// Creates a new set of email create options
49+
///
50+
/// By default, options include rename detection and binary
51+
/// diffs to match `git format-patch`.
52+
pub fn new() -> Self {
53+
Self::default()
54+
}
55+
56+
fn flag(&mut self, opt: raw::git_email_create_flags_t, val: bool) -> &mut Self {
57+
let opt = opt as u32;
58+
if val {
59+
self.raw.flags |= opt;
60+
} else {
61+
self.raw.flags &= !opt;
62+
}
63+
self
64+
}
65+
66+
/// Flag indicating whether patch numbers are included in the subject prefix.
67+
pub fn omit_numbers(&mut self, omit: bool) -> &mut Self {
68+
self.flag(raw::GIT_EMAIL_CREATE_OMIT_NUMBERS, omit)
69+
}
70+
71+
/// Flag indicating whether numbers included in the subject prefix even when
72+
/// the patch is for a single commit (1/1).
73+
pub fn always_number(&mut self, always: bool) -> &mut Self {
74+
self.flag(raw::GIT_EMAIL_CREATE_ALWAYS_NUMBER, always)
75+
}
76+
77+
/// Flag indicating whether rename or similarity detection are ignored.
78+
pub fn ignore_renames(&mut self, ignore: bool) -> &mut Self {
79+
self.flag(raw::GIT_EMAIL_CREATE_NO_RENAMES, ignore)
80+
}
81+
82+
/// Set the subject prefix
83+
///
84+
/// The default value for this is "PATCH". If set to an empty string ("")
85+
/// then only the patch numbers will be shown in the prefix.
86+
/// If the subject_prefix is empty and patch numbers are not being shown,
87+
/// the prefix will be omitted entirely.
88+
pub fn subject_prefix<T: IntoCString>(&mut self, t: T) -> &mut Self {
89+
self.subject_prefix = Some(t.into_c_string().unwrap());
90+
self
91+
}
92+
93+
/// Set the starting patch number; this cannot be 0.
94+
///
95+
/// The default value for this is 1.
96+
pub fn start_number(&mut self, number: usize) -> &mut Self {
97+
self.raw.start_number = number;
98+
self
99+
}
100+
101+
/// Set the "re-roll" number.
102+
///
103+
/// The default value for this is 0 (no re-roll).
104+
pub fn reroll_number(&mut self, number: usize) -> &mut Self {
105+
self.raw.reroll_number = number;
106+
self
107+
}
108+
109+
/// Acquire a pointer to the underlying raw options.
110+
///
111+
/// This function is unsafe as the pointer is only valid so long as this
112+
/// structure is not moved, modified, or used elsewhere.
113+
unsafe fn raw(&mut self) -> *const raw::git_email_create_options {
114+
self.raw.subject_prefix = self
115+
.subject_prefix
116+
.as_ref()
117+
.map(|s| s.as_ptr())
118+
.unwrap_or(ptr::null());
119+
self.raw.diff_opts = ptr::read(self.diff_options.raw());
120+
self.raw.diff_find_opts = ptr::read(self.diff_find_options.raw());
121+
&self.raw as *const _
122+
}
123+
}
124+
125+
impl Email {
126+
/// Create a diff for a commit in mbox format for sending via email.
127+
pub fn from_diff<T: IntoCString>(
128+
diff: &Diff<'_>,
129+
patch_idx: usize,
130+
patch_count: usize,
131+
commit_id: &Oid,
132+
summary: T,
133+
body: T,
134+
author: &Signature<'_>,
135+
opts: &mut EmailCreateOptions,
136+
) -> Result<Self, Error> {
137+
let buf = Buf::new();
138+
let summary = summary.into_c_string()?;
139+
let body = body.into_c_string()?;
140+
unsafe {
141+
try_call!(raw::git_email_create_from_diff(
142+
buf.raw(),
143+
Binding::raw(diff),
144+
patch_idx,
145+
patch_count,
146+
Binding::raw(commit_id),
147+
summary.as_ptr(),
148+
body.as_ptr(),
149+
Binding::raw(author),
150+
opts.raw()
151+
));
152+
Ok(Self { buf })
153+
}
154+
}
155+
156+
/// Create a diff for a commit in mbox format for sending via email.
157+
/// The commit must not be a merge commit.
158+
pub fn from_commit(commit: &Commit<'_>, opts: &mut EmailCreateOptions) -> Result<Self, Error> {
159+
let buf = Buf::new();
160+
unsafe {
161+
try_call!(raw::git_email_create_from_commit(
162+
buf.raw(),
163+
commit.raw(),
164+
opts.raw()
165+
));
166+
Ok(Self { buf })
167+
}
168+
}
169+
}

Diff for: src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ 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};
9595
pub use crate::diff::{DiffFindOptions, DiffHunk, DiffLine, DiffLineType, DiffStats};
96+
pub use crate::email::{Email, EmailCreateOptions};
9697
pub use crate::error::Error;
9798
pub use crate::index::{
9899
Index, IndexConflict, IndexConflicts, IndexEntries, IndexEntry, IndexMatchedPath,
@@ -675,6 +676,7 @@ mod config;
675676
mod cred;
676677
mod describe;
677678
mod diff;
679+
mod email;
678680
mod error;
679681
mod index;
680682
mod indexer;

0 commit comments

Comments
 (0)