| 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 |
|
| |
| 8 |
|
| #include "email.h" |
| 9 |
|
| |
| 10 |
|
| #include "common.h" |
| 11 |
|
| #include "buf.h" |
| 12 |
|
| #include "diff_generate.h" |
| 13 |
|
| #include "diff_stats.h" |
| 14 |
|
| #include "patch.h" |
| 15 |
|
| #include "date.h" |
| 16 |
|
| |
| 17 |
|
| #include "git2/email.h" |
| 18 |
|
| #include "git2/patch.h" |
| 19 |
|
| #include "git2/version.h" |
| 20 |
|
| |
| 21 |
|
| /* |
| 22 |
|
| * Git uses a "magic" timestamp to indicate that an email message |
| 23 |
|
| * is from `git format-patch` (or our equivalent). |
| 24 |
|
| */ |
| 25 |
|
| #define EMAIL_TIMESTAMP "Mon Sep 17 00:00:00 2001" |
| 26 |
|
| |
| 27 |
3 |
| GIT_INLINE(int) include_prefix( |
| 28 |
|
| size_t patch_count, |
| 29 |
|
| git_email_create_options *opts) |
| 30 |
|
| { |
| 31 |
1 |
▶ | return ((!opts->subject_prefix || *opts->subject_prefix) || |
Branch: 1 branch
| Branch | True | False | Total |
| return ((!opts->subject_prefix || *opts->subject_prefix) || |
1 |
0 |
1 |
|
| 32 |
1 |
▶ | (opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| (opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 || |
1 |
0 |
1 |
|
| 33 |
5 |
▶ | opts->reroll_number || |
Branch: 3 branches
| Branch | True | False | Total |
| opts->reroll_number || |
1 |
2 |
3 |
| opts->reroll_number || |
1 |
0 |
1 |
| opts->reroll_number || |
1 |
0 |
1 |
|
| 34 |
1 |
▶ | (patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS))); |
Branch: 1 branch
| Branch | True | False | Total |
| (patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS))); |
1 |
0 |
1 |
|
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
3 |
| static int append_prefix( |
| 38 |
|
| git_str *out, |
| 39 |
|
| size_t patch_idx, |
| 40 |
|
| size_t patch_count, |
| 41 |
|
| git_email_create_options *opts) |
| 42 |
|
| { |
| 43 |
6 |
| const char *subject_prefix = opts->subject_prefix ? |
| 44 |
3 |
▶ | opts->subject_prefix : "PATCH"; |
Branch: 1 branch
| Branch | True | False | Total |
| opts->subject_prefix : "PATCH"; |
1 |
2 |
3 |
|
| 45 |
|
| |
| 46 |
3 |
| git_str_putc(out, '['); |
| 47 |
|
| |
| 48 |
3 |
▶ | if (*subject_prefix) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*subject_prefix) |
2 |
1 |
3 |
|
| 49 |
2 |
| git_str_puts(out, subject_prefix); |
| 50 |
|
| |
| 51 |
3 |
▶ | if (opts->reroll_number) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->reroll_number) { |
0 |
3 |
3 |
|
| 52 |
0 |
▶ | if (*subject_prefix) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*subject_prefix) |
0 |
0 |
0 |
|
| 53 |
0 |
| git_str_putc(out, ' '); |
| 54 |
|
| |
| 55 |
0 |
| git_str_printf(out, "v%" PRIuZ, opts->reroll_number); |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
3 |
▶ | if ((opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 || |
3 |
0 |
3 |
| if ((opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 || |
2 |
1 |
3 |
|
| 59 |
2 |
▶ | (patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS))) { |
Branch: 1 branch
| Branch | True | False | Total |
| (patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS))) { |
2 |
0 |
2 |
|
| 60 |
4 |
| size_t start_number = opts->start_number ? |
| 61 |
2 |
▶ | opts->start_number : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| opts->start_number : 1; |
0 |
2 |
2 |
|
| 62 |
|
| |
| 63 |
2 |
▶ | if (*subject_prefix || opts->reroll_number) |
Branch: 2 branches
| Branch | True | False | Total |
| if (*subject_prefix || opts->reroll_number) |
1 |
1 |
2 |
| if (*subject_prefix || opts->reroll_number) |
0 |
1 |
1 |
|
| 64 |
1 |
| git_str_putc(out, ' '); |
| 65 |
|
| |
| 66 |
2 |
| git_str_printf(out, "%" PRIuZ "/%" PRIuZ, |
| 67 |
2 |
| patch_idx + (start_number - 1), |
| 68 |
2 |
| patch_count + (start_number - 1)); |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
3 |
| git_str_puts(out, "]"); |
| 72 |
|
| |
| 73 |
3 |
▶ | return git_str_oom(out) ? -1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return git_str_oom(out) ? -1 : 0; |
0 |
3 |
3 |
|
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
3 |
| static int append_date( |
| 77 |
|
| git_str *out, |
| 78 |
|
| const git_time *date) |
| 79 |
|
| { |
| 80 |
|
| int error; |
| 81 |
|
| |
| 82 |
6 |
| if ((error = git_str_printf(out, "Date: ")) == 0 && |
| 83 |
3 |
| (error = git_date_rfc2822_fmt(out, date->time, date->offset)) == 0) |
| 84 |
3 |
| error = git_str_putc(out, '\n'); |
| 85 |
|
| |
| 86 |
3 |
| return error; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
3 |
| static int append_subject( |
| 90 |
|
| git_str *out, |
| 91 |
|
| size_t patch_idx, |
| 92 |
|
| size_t patch_count, |
| 93 |
|
| const char *summary, |
| 94 |
|
| git_email_create_options *opts) |
| 95 |
|
| { |
| 96 |
3 |
| bool prefix = include_prefix(patch_count, opts); |
| 97 |
3 |
▶ | size_t summary_len = summary ? strlen(summary) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t summary_len = summary ? strlen(summary) : 0; |
3 |
0 |
3 |
|
| 98 |
|
| int error; |
| 99 |
|
| |
| 100 |
3 |
▶ | if (summary_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (summary_len) { |
3 |
0 |
3 |
|
| 101 |
3 |
| const char *nl = strchr(summary, '\n'); |
| 102 |
|
| |
| 103 |
3 |
▶ | if (nl) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nl) |
0 |
3 |
3 |
|
| 104 |
0 |
| summary_len = (nl - summary); |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
3 |
▶ | if ((error = git_str_puts(out, "Subject: ")) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_str_puts(out, "Subject: ")) < 0) |
0 |
3 |
3 |
|
| 108 |
0 |
| return error; |
| 109 |
|
| |
| 110 |
6 |
| if (prefix && |
| 111 |
3 |
| (error = append_prefix(out, patch_idx, patch_count, opts)) < 0) |
| 112 |
0 |
| return error; |
| 113 |
|
| |
| 114 |
3 |
▶ | if (prefix && summary_len && (error = git_str_putc(out, ' ')) < 0) |
Branch: 3 branches
| Branch | True | False | Total |
| if (prefix && summary_len && (error = git_str_putc(out, ' ')) < 0) |
3 |
0 |
3 |
| if (prefix && summary_len && (error = git_str_putc(out, ' ')) < 0) |
3 |
0 |
3 |
| if (prefix && summary_len && (error = git_str_putc(out, ' ')) < 0) |
0 |
3 |
3 |
|
| 115 |
0 |
| return error; |
| 116 |
|
| |
| 117 |
6 |
| if (summary_len && |
| 118 |
3 |
| (error = git_str_put(out, summary, summary_len)) < 0) |
| 119 |
0 |
| return error; |
| 120 |
|
| |
| 121 |
3 |
| return git_str_putc(out, '\n'); |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
3 |
| static int append_header( |
| 125 |
|
| git_str *out, |
| 126 |
|
| size_t patch_idx, |
| 127 |
|
| size_t patch_count, |
| 128 |
|
| const git_oid *commit_id, |
| 129 |
|
| const char *summary, |
| 130 |
|
| const git_signature *author, |
| 131 |
|
| git_email_create_options *opts) |
| 132 |
|
| { |
| 133 |
|
| char id[GIT_OID_HEXSZ]; |
| 134 |
|
| int error; |
| 135 |
|
| |
| 136 |
6 |
| if ((error = git_oid_fmt(id, commit_id)) < 0 || |
| 137 |
6 |
▶ | (error = git_str_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| (error = git_str_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 || |
3 |
0 |
3 |
|
| 138 |
6 |
▶ | (error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| (error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 || |
3 |
0 |
3 |
|
| 139 |
6 |
▶ | (error = append_date(out, &author->when)) < 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| (error = append_date(out, &author->when)) < 0 || |
0 |
3 |
3 |
|
| 140 |
3 |
| (error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0) |
| 141 |
0 |
| return error; |
| 142 |
|
| |
| 143 |
3 |
▶ | if ((error = git_str_putc(out, '\n')) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_str_putc(out, '\n')) < 0) |
0 |
3 |
3 |
|
| 144 |
0 |
| return error; |
| 145 |
|
| |
| 146 |
3 |
| return 0; |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
3 |
| static int append_body(git_str *out, const char *body) |
| 150 |
|
| { |
| 151 |
|
| size_t body_len; |
| 152 |
|
| int error; |
| 153 |
|
| |
| 154 |
3 |
▶ | if (!body) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!body) |
3 |
0 |
3 |
|
| 155 |
3 |
| return 0; |
| 156 |
|
| |
| 157 |
0 |
| body_len = strlen(body); |
| 158 |
|
| |
| 159 |
0 |
▶ | if ((error = git_str_puts(out, body)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_str_puts(out, body)) < 0) |
0 |
0 |
0 |
|
| 160 |
0 |
| return error; |
| 161 |
|
| |
| 162 |
0 |
▶ | if (body_len && body[body_len - 1] != '\n') |
Branch: 2 branches
| Branch | True | False | Total |
| if (body_len && body[body_len - 1] != '\n') |
0 |
0 |
0 |
| if (body_len && body[body_len - 1] != '\n') |
0 |
0 |
0 |
|
| 163 |
0 |
| error = git_str_putc(out, '\n'); |
| 164 |
|
| |
| 165 |
0 |
| return error; |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
3 |
| static int append_diffstat(git_str *out, git_diff *diff) |
| 169 |
|
| { |
| 170 |
3 |
| git_diff_stats *stats = NULL; |
| 171 |
|
| unsigned int format_flags; |
| 172 |
|
| int error; |
| 173 |
|
| |
| 174 |
3 |
| format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY; |
| 175 |
|
| |
| 176 |
6 |
| if ((error = git_diff_get_stats(&stats, diff)) == 0 && |
| 177 |
3 |
| (error = git_diff__stats_to_buf(out, stats, format_flags, 0)) == 0) |
| 178 |
3 |
| error = git_str_putc(out, '\n'); |
| 179 |
|
| |
| 180 |
3 |
| git_diff_stats_free(stats); |
| 181 |
3 |
| return error; |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
3 |
| static int append_patches(git_str *out, git_diff *diff) |
| 185 |
|
| { |
| 186 |
|
| size_t i, deltas; |
| 187 |
3 |
| int error = 0; |
| 188 |
|
| |
| 189 |
3 |
| deltas = git_diff_num_deltas(diff); |
| 190 |
|
| |
| 191 |
6 |
▶ | for (i = 0; i < deltas; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < deltas; ++i) { |
3 |
3 |
6 |
|
| 192 |
3 |
| git_patch *patch = NULL; |
| 193 |
|
| |
| 194 |
3 |
▶ | if ((error = git_patch_from_diff(&patch, diff, i)) >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_patch_from_diff(&patch, diff, i)) >= 0) |
3 |
0 |
3 |
|
| 195 |
3 |
| error = git_patch__to_buf(out, patch); |
| 196 |
|
| |
| 197 |
3 |
| git_patch_free(patch); |
| 198 |
|
| |
| 199 |
3 |
▶ | if (error < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (error < 0) |
0 |
3 |
3 |
|
| 200 |
0 |
| break; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
3 |
| return error; |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
3 |
| int git_email__append_from_diff( |
| 207 |
|
| git_str *out, |
| 208 |
|
| git_diff *diff, |
| 209 |
|
| size_t patch_idx, |
| 210 |
|
| size_t patch_count, |
| 211 |
|
| const git_oid *commit_id, |
| 212 |
|
| const char *summary, |
| 213 |
|
| const char *body, |
| 214 |
|
| const git_signature *author, |
| 215 |
|
| const git_email_create_options *given_opts) |
| 216 |
|
| { |
| 217 |
3 |
| git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; |
| 218 |
|
| int error; |
| 219 |
|
| |
| 220 |
3 |
▶ | GIT_ASSERT_ARG(out); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(out); |
0 |
3 |
3 |
|
| 221 |
3 |
▶ | GIT_ASSERT_ARG(diff); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(diff); |
0 |
3 |
3 |
|
| 222 |
3 |
▶ | GIT_ASSERT_ARG(!patch_idx || patch_idx <= patch_count); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_ASSERT_ARG(!patch_idx || patch_idx <= patch_count); |
3 |
0 |
3 |
| GIT_ASSERT_ARG(!patch_idx || patch_idx <= patch_count); |
0 |
3 |
3 |
|
| 223 |
3 |
▶ | GIT_ASSERT_ARG(commit_id); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(commit_id); |
0 |
3 |
3 |
|
| 224 |
3 |
▶ | GIT_ASSERT_ARG(author); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(author); |
0 |
3 |
3 |
|
| 225 |
|
| |
| 226 |
3 |
▶ | GIT_ERROR_CHECK_VERSION(given_opts, |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_VERSION(given_opts, |
0 |
3 |
3 |
|
| 227 |
|
| GIT_EMAIL_CREATE_OPTIONS_VERSION, |
| 228 |
|
| "git_email_create_options"); |
| 229 |
|
| |
| 230 |
3 |
▶ | if (given_opts) |
Branch: 1 branch
| Branch | True | False | Total |
| if (given_opts) |
3 |
0 |
3 |
|
| 231 |
3 |
| memcpy(&opts, given_opts, sizeof(git_email_create_options)); |
| 232 |
|
| |
| 233 |
6 |
| if ((error = append_header(out, patch_idx, patch_count, commit_id, summary, author, &opts)) == 0 && |
| 234 |
6 |
▶ | (error = append_body(out, body)) == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| (error = append_body(out, body)) == 0 && |
3 |
0 |
3 |
|
| 235 |
6 |
▶ | (error = git_str_puts(out, "---\n")) == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| (error = git_str_puts(out, "---\n")) == 0 && |
3 |
0 |
3 |
|
| 236 |
6 |
▶ | (error = append_diffstat(out, diff)) == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| (error = append_diffstat(out, diff)) == 0 && |
3 |
0 |
3 |
|
| 237 |
3 |
| (error = append_patches(out, diff)) == 0) |
| 238 |
3 |
| error = git_str_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n"); |
| 239 |
|
| |
| 240 |
3 |
| return error; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
3 |
| int git_email_create_from_diff( |
| 244 |
|
| git_buf *out, |
| 245 |
|
| git_diff *diff, |
| 246 |
|
| size_t patch_idx, |
| 247 |
|
| size_t patch_count, |
| 248 |
|
| const git_oid *commit_id, |
| 249 |
|
| const char *summary, |
| 250 |
|
| const char *body, |
| 251 |
|
| const git_signature *author, |
| 252 |
|
| const git_email_create_options *given_opts) |
| 253 |
|
| { |
| 254 |
3 |
| git_str email = GIT_STR_INIT; |
| 255 |
|
| int error; |
| 256 |
|
| |
| 257 |
3 |
| git_buf_tostr(&email, out); |
| 258 |
|
| |
| 259 |
3 |
| error = git_email__append_from_diff(&email, diff, patch_idx, |
| 260 |
|
| patch_count, commit_id, summary, body, author, |
| 261 |
|
| given_opts); |
| 262 |
|
| |
| 263 |
3 |
▶ | if (error == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (error == 0) |
3 |
0 |
3 |
|
| 264 |
3 |
| error = git_buf_fromstr(out, &email); |
| 265 |
|
| |
| 266 |
3 |
| git_str_dispose(&email); |
| 267 |
3 |
| return error; |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
0 |
| int git_email_create_from_commit( |
| 271 |
|
| git_buf *out, |
| 272 |
|
| git_commit *commit, |
| 273 |
|
| const git_email_create_options *given_opts) |
| 274 |
|
| { |
| 275 |
0 |
| git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; |
| 276 |
0 |
| git_diff *diff = NULL; |
| 277 |
|
| git_repository *repo; |
| 278 |
|
| git_diff_options *diff_opts; |
| 279 |
|
| git_diff_find_options *find_opts; |
| 280 |
|
| const git_signature *author; |
| 281 |
|
| const char *summary, *body; |
| 282 |
|
| const git_oid *commit_id; |
| 283 |
0 |
| int error = -1; |
| 284 |
|
| |
| 285 |
0 |
▶ | GIT_ASSERT_ARG(out); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(out); |
0 |
0 |
0 |
|
| 286 |
0 |
▶ | GIT_ASSERT_ARG(commit); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(commit); |
0 |
0 |
0 |
|
| 287 |
|
| |
| 288 |
0 |
▶ | GIT_ERROR_CHECK_VERSION(given_opts, |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_VERSION(given_opts, |
0 |
0 |
0 |
|
| 289 |
|
| GIT_EMAIL_CREATE_OPTIONS_VERSION, |
| 290 |
|
| "git_email_create_options"); |
| 291 |
|
| |
| 292 |
0 |
▶ | if (given_opts) |
Branch: 1 branch
| Branch | True | False | Total |
| if (given_opts) |
0 |
0 |
0 |
|
| 293 |
0 |
| memcpy(&opts, given_opts, sizeof(git_email_create_options)); |
| 294 |
|
| |
| 295 |
0 |
| repo = git_commit_owner(commit); |
| 296 |
0 |
| author = git_commit_author(commit); |
| 297 |
0 |
| summary = git_commit_summary(commit); |
| 298 |
0 |
| body = git_commit_body(commit); |
| 299 |
0 |
| commit_id = git_commit_id(commit); |
| 300 |
0 |
| diff_opts = &opts.diff_opts; |
| 301 |
0 |
| find_opts = &opts.diff_find_opts; |
| 302 |
|
| |
| 303 |
0 |
▶ | if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0) |
0 |
0 |
0 |
|
| 304 |
0 |
| goto done; |
| 305 |
|
| |
| 306 |
0 |
| if ((opts.flags & GIT_EMAIL_CREATE_NO_RENAMES) == 0 && |
| 307 |
0 |
| (error = git_diff_find_similar(diff, find_opts)) < 0) |
| 308 |
0 |
| goto done; |
| 309 |
|
| |
| 310 |
0 |
| error = git_email_create_from_diff(out, diff, 1, 1, commit_id, summary, body, author, &opts); |
| 311 |
|
| |
| 312 |
0 |
| done: |
| 313 |
0 |
| git_diff_free(diff); |
| 314 |
0 |
| return error; |
| 315 |
|
| } |