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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use core::ops::Range;
use std::ffi::CStr;
use std::ffi::CString;
use std::iter::FusedIterator;
use std::ptr;

use libc::{c_char, c_int};

use crate::util::Binding;
use crate::{raw, Buf, Error, IntoCString};

/// Clean up a message, removing extraneous whitespace, and ensure that the
/// message ends with a newline. If `comment_char` is `Some`, also remove comment
/// lines starting with that character.
pub fn message_prettify<T: IntoCString>(
    message: T,
    comment_char: Option<u8>,
) -> Result<String, Error> {
    _message_prettify(message.into_c_string()?, comment_char)
}

fn _message_prettify(message: CString, comment_char: Option<u8>) -> Result<String, Error> {
    let ret = Buf::new();
    unsafe {
        try_call!(raw::git_message_prettify(
            ret.raw(),
            message,
            comment_char.is_some() as c_int,
            comment_char.unwrap_or(0) as c_char
        ));
    }
    Ok(ret.as_str().unwrap().to_string())
}

/// The default comment character for `message_prettify` ('#')
pub const DEFAULT_COMMENT_CHAR: Option<u8> = Some(b'#');

/// Get the trailers for the given message.
///
/// Use this function when you are dealing with a UTF-8-encoded message.
pub fn message_trailers_strs(message: &str) -> Result<MessageTrailersStrs, Error> {
    _message_trailers(message.into_c_string()?).map(|res| MessageTrailersStrs(res))
}

/// Get the trailers for the given message.
///
/// Use this function when the message might not be UTF-8-encoded,
/// or if you want to handle the returned trailer key–value pairs
/// as bytes.
pub fn message_trailers_bytes<S: IntoCString>(message: S) -> Result<MessageTrailersBytes, Error> {
    _message_trailers(message.into_c_string()?).map(|res| MessageTrailersBytes(res))
}

fn _message_trailers(message: CString) -> Result<MessageTrailers, Error> {
    let ret = MessageTrailers::new();
    unsafe {
        try_call!(raw::git_message_trailers(ret.raw(), message));
    }
    Ok(ret)
}

/// Collection of UTF-8-encoded trailers.
///
/// Use `iter()` to get access to the values.
pub struct MessageTrailersStrs(MessageTrailers);

impl MessageTrailersStrs {
    /// Create a borrowed iterator.
    pub fn iter(&self) -> MessageTrailersStrsIterator<'_> {
        MessageTrailersStrsIterator(self.0.iter())
    }
    /// The number of trailer key–value pairs.
    pub fn len(&self) -> usize {
        self.0.len()
    }
    /// Convert to the “bytes” variant.
    pub fn to_bytes(self) -> MessageTrailersBytes {
        MessageTrailersBytes(self.0)
    }
}

/// Collection of unencoded (bytes) trailers.
///
/// Use `iter()` to get access to the values.
pub struct MessageTrailersBytes(MessageTrailers);

impl MessageTrailersBytes {
    /// Create a borrowed iterator.
    pub fn iter(&self) -> MessageTrailersBytesIterator<'_> {
        MessageTrailersBytesIterator(self.0.iter())
    }
    /// The number of trailer key–value pairs.
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

struct MessageTrailers {
    raw: raw::git_message_trailer_array,
}

impl MessageTrailers {
    fn new() -> MessageTrailers {
        crate::init();
        unsafe {
            Binding::from_raw(&mut raw::git_message_trailer_array {
                trailers: ptr::null_mut(),
                count: 0,
                _trailer_block: ptr::null_mut(),
            } as *mut _)
        }
    }
    fn iter(&self) -> MessageTrailersIterator<'_> {
        MessageTrailersIterator {
            trailers: self,
            range: Range {
                start: 0,
                end: self.raw.count,
            },
        }
    }
    fn len(&self) -> usize {
        self.raw.count
    }
}

impl Drop for MessageTrailers {
    fn drop(&mut self) {
        unsafe {
            raw::git_message_trailer_array_free(&mut self.raw);
        }
    }
}

impl Binding for MessageTrailers {
    type Raw = *mut raw::git_message_trailer_array;
    unsafe fn from_raw(raw: *mut raw::git_message_trailer_array) -> MessageTrailers {
        MessageTrailers { raw: *raw }
    }
    fn raw(&self) -> *mut raw::git_message_trailer_array {
        &self.raw as *const _ as *mut _
    }
}

struct MessageTrailersIterator<'a> {
    trailers: &'a MessageTrailers,
    range: Range<usize>,
}

fn to_raw_tuple(trailers: &MessageTrailers, index: usize) -> (*const c_char, *const c_char) {
    unsafe {
        let addr = trailers.raw.trailers.wrapping_add(index);
        ((*addr).key, (*addr).value)
    }
}

/// Borrowed iterator over the UTF-8-encoded trailers.
pub struct MessageTrailersStrsIterator<'a>(MessageTrailersIterator<'a>);

impl<'pair> Iterator for MessageTrailersStrsIterator<'pair> {
    type Item = (&'pair str, &'pair str);

    fn next(&mut self) -> Option<Self::Item> {
        self.0
            .range
            .next()
            .map(|index| to_str_tuple(&self.0.trailers, index))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.range.size_hint()
    }
}

impl FusedIterator for MessageTrailersStrsIterator<'_> {}

impl ExactSizeIterator for MessageTrailersStrsIterator<'_> {
    fn len(&self) -> usize {
        self.0.range.len()
    }
}

impl DoubleEndedIterator for MessageTrailersStrsIterator<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0
            .range
            .next_back()
            .map(|index| to_str_tuple(&self.0.trailers, index))
    }
}

fn to_str_tuple(trailers: &MessageTrailers, index: usize) -> (&str, &str) {
    unsafe {
        let (rkey, rvalue) = to_raw_tuple(&trailers, index);
        let key = CStr::from_ptr(rkey).to_str().unwrap();
        let value = CStr::from_ptr(rvalue).to_str().unwrap();
        (key, value)
    }
}

/// Borrowed iterator over the raw (bytes) trailers.
pub struct MessageTrailersBytesIterator<'a>(MessageTrailersIterator<'a>);

impl<'pair> Iterator for MessageTrailersBytesIterator<'pair> {
    type Item = (&'pair [u8], &'pair [u8]);

    fn next(&mut self) -> Option<Self::Item> {
        self.0
            .range
            .next()
            .map(|index| to_bytes_tuple(&self.0.trailers, index))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.range.size_hint()
    }
}

impl FusedIterator for MessageTrailersBytesIterator<'_> {}

impl ExactSizeIterator for MessageTrailersBytesIterator<'_> {
    fn len(&self) -> usize {
        self.0.range.len()
    }
}

impl DoubleEndedIterator for MessageTrailersBytesIterator<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0
            .range
            .next_back()
            .map(|index| to_bytes_tuple(&self.0.trailers, index))
    }
}

fn to_bytes_tuple(trailers: &MessageTrailers, index: usize) -> (&[u8], &[u8]) {
    unsafe {
        let (rkey, rvalue) = to_raw_tuple(&trailers, index);
        let key = CStr::from_ptr(rkey).to_bytes();
        let value = CStr::from_ptr(rvalue).to_bytes();
        (key, value)
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn prettify() {
        use crate::{message_prettify, DEFAULT_COMMENT_CHAR};

        // This does not attempt to duplicate the extensive tests for
        // git_message_prettify in libgit2, just a few representative values to
        // make sure the interface works as expected.
        assert_eq!(message_prettify("1\n\n\n2", None).unwrap(), "1\n\n2\n");
        assert_eq!(
            message_prettify("1\n\n\n2\n\n\n3", None).unwrap(),
            "1\n\n2\n\n3\n"
        );
        assert_eq!(
            message_prettify("1\n# comment\n# more", None).unwrap(),
            "1\n# comment\n# more\n"
        );
        assert_eq!(
            message_prettify("1\n# comment\n# more", DEFAULT_COMMENT_CHAR).unwrap(),
            "1\n"
        );
        assert_eq!(
            message_prettify("1\n; comment\n; more", Some(';' as u8)).unwrap(),
            "1\n"
        );
    }

    #[test]
    fn trailers() {
        use crate::{message_trailers_bytes, message_trailers_strs, MessageTrailersStrs};
        use std::collections::HashMap;

        // no trailers
        let message1 = "
WHAT ARE WE HERE FOR

What are we here for?

Just to be eaten?
";
        let expected: HashMap<&str, &str> = HashMap::new();
        assert_eq!(expected, to_map(&message_trailers_strs(message1).unwrap()));

        // standard PSA
        let message2 = "
Attention all

We are out of tomatoes.

Spoken-by: Major Turnips
Transcribed-by: Seargant Persimmons
Signed-off-by: Colonel Kale
";
        let expected: HashMap<&str, &str> = vec![
            ("Spoken-by", "Major Turnips"),
            ("Transcribed-by", "Seargant Persimmons"),
            ("Signed-off-by", "Colonel Kale"),
        ]
        .into_iter()
        .collect();
        assert_eq!(expected, to_map(&message_trailers_strs(message2).unwrap()));

        // ignore everything after `---`
        let message3 = "
The fate of Seargant Green-Peppers

Seargant Green-Peppers was killed by Caterpillar Battalion 44.

Signed-off-by: Colonel Kale
---
I never liked that guy, anyway.

Opined-by: Corporal Garlic
";
        let expected: HashMap<&str, &str> = vec![("Signed-off-by", "Colonel Kale")]
            .into_iter()
            .collect();
        assert_eq!(expected, to_map(&message_trailers_strs(message3).unwrap()));

        // Raw bytes message; not valid UTF-8
        // Source: https://stackoverflow.com/a/3886015/1725151
        let message4 = b"
Be honest guys

Am I a malformed brussels sprout?

Signed-off-by: Lieutenant \xe2\x28\xa1prout
";

        let trailer = message_trailers_bytes(&message4[..]).unwrap();
        let expected = (&b"Signed-off-by"[..], &b"Lieutenant \xe2\x28\xa1prout"[..]);
        let actual = trailer.iter().next().unwrap();
        assert_eq!(expected, actual);

        fn to_map(trailers: &MessageTrailersStrs) -> HashMap<&str, &str> {
            let mut map = HashMap::with_capacity(trailers.len());
            for (key, value) in trailers.iter() {
                map.insert(key, value);
            }
            map
        }
    }
}