Skip to content

Commit ba01547

Browse files
authored
Merge pull request libgit2#6061 from libgit2/ethomson/email
Introduce `git_email_create`; deprecate `git_diff_format_email`
2 parents 0a87286 + ba3595a commit ba01547

File tree

12 files changed

+1026
-267
lines changed

12 files changed

+1026
-267
lines changed

include/git2.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "git2/deprecated.h"
2727
#include "git2/describe.h"
2828
#include "git2/diff.h"
29+
#include "git2/email.h"
2930
#include "git2/errors.h"
3031
#include "git2/filter.h"
3132
#include "git2/global.h"

include/git2/deprecated.h

+96
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,102 @@ typedef git_configmap git_cvar_map;
294294

295295
/**@}*/
296296

297+
/** @name Deprecated Diff Functions and Constants
298+
*
299+
* These functions and enumeration values are retained for backward
300+
* compatibility. The newer versions of these functions and values
301+
* should be preferred in all new code.
302+
*
303+
* There is no plan to remove these backward compatibility values at
304+
* this time.
305+
*/
306+
/**@{*/
307+
308+
/**
309+
* Formatting options for diff e-mail generation
310+
*/
311+
typedef enum {
312+
/** Normal patch, the default */
313+
GIT_DIFF_FORMAT_EMAIL_NONE = 0,
314+
315+
/** Don't insert "[PATCH]" in the subject header*/
316+
GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER = (1 << 0),
317+
318+
} git_diff_format_email_flags_t;
319+
320+
/**
321+
* Options for controlling the formatting of the generated e-mail.
322+
*/
323+
typedef struct {
324+
unsigned int version;
325+
326+
/** see `git_diff_format_email_flags_t` above */
327+
uint32_t flags;
328+
329+
/** This patch number */
330+
size_t patch_no;
331+
332+
/** Total number of patches in this series */
333+
size_t total_patches;
334+
335+
/** id to use for the commit */
336+
const git_oid *id;
337+
338+
/** Summary of the change */
339+
const char *summary;
340+
341+
/** Commit message's body */
342+
const char *body;
343+
344+
/** Author of the change */
345+
const git_signature *author;
346+
} git_diff_format_email_options;
347+
348+
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1
349+
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL}
350+
351+
/**
352+
* Create an e-mail ready patch from a diff.
353+
*
354+
* @deprecated git_email_create_from_diff
355+
* @see git_email_create_from_diff
356+
*/
357+
GIT_EXTERN(int) git_diff_format_email(
358+
git_buf *out,
359+
git_diff *diff,
360+
const git_diff_format_email_options *opts);
361+
362+
/**
363+
* Create an e-mail ready patch for a commit.
364+
*
365+
* @deprecated git_email_create_from_commit
366+
* @see git_email_create_from_commit
367+
*/
368+
GIT_EXTERN(int) git_diff_commit_as_email(
369+
git_buf *out,
370+
git_repository *repo,
371+
git_commit *commit,
372+
size_t patch_no,
373+
size_t total_patches,
374+
uint32_t flags,
375+
const git_diff_options *diff_opts);
376+
377+
/**
378+
* Initialize git_diff_format_email_options structure
379+
*
380+
* Initializes a `git_diff_format_email_options` with default values. Equivalent
381+
* to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT.
382+
*
383+
* @param opts The `git_blame_options` struct to initialize.
384+
* @param version The struct version; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`.
385+
* @return Zero on success; -1 on failure.
386+
*/
387+
GIT_EXTERN(int) git_diff_format_email_options_init(
388+
git_diff_format_email_options *opts,
389+
unsigned int version);
390+
391+
/**@}*/
392+
297393
/** @name Deprecated Error Functions and Constants
298394
*
299395
* These functions and enumeration values are retained for backward

include/git2/diff.h

-93
Original file line numberDiff line numberDiff line change
@@ -1376,99 +1376,6 @@ GIT_EXTERN(int) git_diff_stats_to_buf(
13761376
*/
13771377
GIT_EXTERN(void) git_diff_stats_free(git_diff_stats *stats);
13781378

1379-
/**
1380-
* Formatting options for diff e-mail generation
1381-
*/
1382-
typedef enum {
1383-
/** Normal patch, the default */
1384-
GIT_DIFF_FORMAT_EMAIL_NONE = 0,
1385-
1386-
/** Don't insert "[PATCH]" in the subject header*/
1387-
GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER = (1 << 0),
1388-
1389-
} git_diff_format_email_flags_t;
1390-
1391-
/**
1392-
* Options for controlling the formatting of the generated e-mail.
1393-
*/
1394-
typedef struct {
1395-
unsigned int version;
1396-
1397-
/** see `git_diff_format_email_flags_t` above */
1398-
uint32_t flags;
1399-
1400-
/** This patch number */
1401-
size_t patch_no;
1402-
1403-
/** Total number of patches in this series */
1404-
size_t total_patches;
1405-
1406-
/** id to use for the commit */
1407-
const git_oid *id;
1408-
1409-
/** Summary of the change */
1410-
const char *summary;
1411-
1412-
/** Commit message's body */
1413-
const char *body;
1414-
1415-
/** Author of the change */
1416-
const git_signature *author;
1417-
} git_diff_format_email_options;
1418-
1419-
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1
1420-
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL}
1421-
1422-
/**
1423-
* Create an e-mail ready patch from a diff.
1424-
*
1425-
* @param out buffer to store the e-mail patch in
1426-
* @param diff containing the commit
1427-
* @param opts structure with options to influence content and formatting.
1428-
* @return 0 or an error code
1429-
*/
1430-
GIT_EXTERN(int) git_diff_format_email(
1431-
git_buf *out,
1432-
git_diff *diff,
1433-
const git_diff_format_email_options *opts);
1434-
1435-
/**
1436-
* Create an e-mail ready patch for a commit.
1437-
*
1438-
* Does not support creating patches for merge commits (yet).
1439-
*
1440-
* @param out buffer to store the e-mail patch in
1441-
* @param repo containing the commit
1442-
* @param commit pointer to up commit
1443-
* @param patch_no patch number of the commit
1444-
* @param total_patches total number of patches in the patch set
1445-
* @param flags determines the formatting of the e-mail
1446-
* @param diff_opts structure with options to influence diff or NULL for defaults.
1447-
* @return 0 or an error code
1448-
*/
1449-
GIT_EXTERN(int) git_diff_commit_as_email(
1450-
git_buf *out,
1451-
git_repository *repo,
1452-
git_commit *commit,
1453-
size_t patch_no,
1454-
size_t total_patches,
1455-
uint32_t flags,
1456-
const git_diff_options *diff_opts);
1457-
1458-
/**
1459-
* Initialize git_diff_format_email_options structure
1460-
*
1461-
* Initializes a `git_diff_format_email_options` with default values. Equivalent
1462-
* to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT.
1463-
*
1464-
* @param opts The `git_blame_options` struct to initialize.
1465-
* @param version The struct version; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`.
1466-
* @return Zero on success; -1 on failure.
1467-
*/
1468-
GIT_EXTERN(int) git_diff_format_email_options_init(
1469-
git_diff_format_email_options *opts,
1470-
unsigned int version);
1471-
14721379
/**
14731380
* Patch ID options structure
14741381
*

include/git2/email.h

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_git_email_h__
8+
#define INCLUDE_git_email_h__
9+
10+
#include "common.h"
11+
12+
/**
13+
* @file git2/email.h
14+
* @brief Git email formatting and application routines.
15+
* @ingroup Git
16+
* @{
17+
*/
18+
GIT_BEGIN_DECL
19+
20+
/**
21+
* Formatting options for diff e-mail generation
22+
*/
23+
typedef enum {
24+
/** Normal patch, the default */
25+
GIT_EMAIL_CREATE_DEFAULT = 0,
26+
27+
/** Do not include patch numbers in the subject prefix. */
28+
GIT_EMAIL_CREATE_OMIT_NUMBERS = (1u << 0),
29+
30+
/**
31+
* Include numbers in the subject prefix even when the
32+
* patch is for a single commit (1/1).
33+
*/
34+
GIT_EMAIL_CREATE_ALWAYS_NUMBER = (1u << 1),
35+
36+
/** Do not perform rename or similarity detection. */
37+
GIT_EMAIL_CREATE_NO_RENAMES = (1u << 2),
38+
} git_email_create_flags_t;
39+
40+
/**
41+
* Options for controlling the formatting of the generated e-mail.
42+
*/
43+
typedef struct {
44+
unsigned int version;
45+
46+
/** see `git_email_create_flags_t` above */
47+
uint32_t flags;
48+
49+
/** Options to use when creating diffs */
50+
git_diff_options diff_opts;
51+
52+
/** Options for finding similarities within diffs */
53+
git_diff_find_options diff_find_opts;
54+
55+
/**
56+
* The subject prefix, by default "PATCH". If set to an empty
57+
* string ("") then only the patch numbers will be shown in the
58+
* prefix. If the subject_prefix is empty and patch numbers
59+
* are not being shown, the prefix will be omitted entirely.
60+
*/
61+
const char *subject_prefix;
62+
63+
/**
64+
* The starting patch number; this cannot be 0. By default,
65+
* this is 1.
66+
*/
67+
size_t start_number;
68+
69+
/** The "re-roll" number. By default, there is no re-roll. */
70+
size_t reroll_number;
71+
} git_email_create_options;
72+
73+
/*
74+
* By default, our options include rename detection and binary
75+
* diffs to match `git format-patch`.
76+
*/
77+
#define GIT_EMAIL_CREATE_OPTIONS_VERSION 1
78+
#define GIT_EMAIL_CREATE_OPTIONS_INIT \
79+
{ \
80+
GIT_EMAIL_CREATE_OPTIONS_VERSION, \
81+
GIT_EMAIL_CREATE_DEFAULT, \
82+
{ GIT_DIFF_OPTIONS_VERSION, GIT_DIFF_SHOW_BINARY, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3 }, \
83+
GIT_DIFF_FIND_OPTIONS_INIT \
84+
}
85+
86+
/**
87+
* Create a diff for a commit in mbox format for sending via email.
88+
*
89+
* @param out buffer to store the e-mail patch in
90+
* @param diff the changes to include in the email
91+
* @param patch_idx the patch index
92+
* @param patch_count the total number of patches that will be included
93+
* @param commit_id the commit id for this change
94+
* @param summary the commit message for this change
95+
* @param body optional text to include above the diffstat
96+
* @param author the person who authored this commit
97+
* @param opts email creation options
98+
*/
99+
GIT_EXTERN(int) git_email_create_from_diff(
100+
git_buf *out,
101+
git_diff *diff,
102+
size_t patch_idx,
103+
size_t patch_count,
104+
const git_oid *commit_id,
105+
const char *summary,
106+
const char *body,
107+
const git_signature *author,
108+
const git_email_create_options *opts);
109+
110+
/**
111+
* Create a diff for a commit in mbox format for sending via email.
112+
* The commit must not be a merge commit.
113+
*
114+
* @param out buffer to store the e-mail patch in
115+
* @param commit commit to create a patch for
116+
* @param opts email creation options
117+
*/
118+
GIT_EXTERN(int) git_email_create_from_commit(
119+
git_buf *out,
120+
git_commit *commit,
121+
const git_email_create_options *opts);
122+
123+
GIT_END_DECL
124+
125+
/** @} */
126+
127+
#endif

include/git2/sys/email.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_sys_git_email_h__
8+
#define INCLUDE_sys_git_email_h__
9+
10+
/**
11+
* @file git2/sys/email.h
12+
* @brief Advanced git email creation routines
13+
* @defgroup git_email Advanced git email creation routines
14+
* @ingroup Git
15+
* @{
16+
*/
17+
GIT_BEGIN_DECL
18+
19+
/**
20+
* Create a diff for a commit in mbox format for sending via email.
21+
*
22+
* @param out buffer to store the e-mail patch in
23+
* @param diff the changes to include in the email
24+
* @param patch_idx the patch index
25+
* @param patch_count the total number of patches that will be included
26+
* @param commit_id the commit id for this change
27+
* @param summary the commit message for this change
28+
* @param body optional text to include above the diffstat
29+
* @param author the person who authored this commit
30+
* @param opts email creation options
31+
*/
32+
GIT_EXTERN(int) git_email_create_from_diff(
33+
git_buf *out,
34+
git_diff *diff,
35+
size_t patch_idx,
36+
size_t patch_count,
37+
const git_oid *commit_id,
38+
const char *summary,
39+
const char *body,
40+
const git_signature *author,
41+
const git_email_create_options *opts);
42+
43+
/** @} */
44+
GIT_END_DECL
45+
#endif

src/buffer.c

+7
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,13 @@ void git_buf_shorten(git_buf *buf, size_t amount)
600600
git_buf_clear(buf);
601601
}
602602

603+
void git_buf_truncate_at_char(git_buf *buf, char separator)
604+
{
605+
ssize_t idx = git_buf_find(buf, separator);
606+
if (idx >= 0)
607+
git_buf_truncate(buf, (size_t)idx);
608+
}
609+
603610
void git_buf_rtruncate_at_char(git_buf *buf, char separator)
604611
{
605612
ssize_t idx = git_buf_rfind_next(buf, separator);

0 commit comments

Comments
 (0)