Skip to content

Commit 1a64fbd

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 1a64fbd

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

Diff for: libgit2-sys/lib.rs

+42
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;
@@ -3559,6 +3581,7 @@ extern "C" {
35593581
ancestor: *const git_oid,
35603582
) -> c_int;
35613583

3584+
#[deprecated(note = "refactored to `email_from_diff` to match upstream")]
35623585
pub fn git_diff_format_email(
35633586
out: *mut git_buf,
35643587
diff: *mut git_diff,
@@ -4106,6 +4129,25 @@ extern "C" {
41064129
replace_email: *const c_char,
41074130
) -> c_int;
41084131

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

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

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

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)