Compare

05cf4ceb5bc28580ef30cb45c8bbd8b447431cf4..609d2a88349963d1b355062eec97506ec0e69f65

imap-send: add --draft to set IMAP \Draft flag

The documented purpose of imap-send is to upload draft emails for sending
later, but it did not have any way to mark the messages as \Draft, so some
email clients presented the result as an un-editable, un-sendable email
even if it happened to be in a "Drafts" folder.

Signed-off-by: Wolfgang Faust <contrib-git@wolfgangfaust.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Wolfgang Faust <contrib-git@wolfgangfaust.com> · 2026-09-02 00:13 UTC · committed by Junio C Hamano

Browse files
7 files changed, +64 −66
modified Documentation/git-imap-send.adoc
@@ -9,7 +9,7 @@ git-imap-send - Send a collection of patches from stdin to an IMAP folder
9 9 SYNOPSIS
10 10 --------
11 11 [synopsis]
12 - git imap-send [-v] [-q] [--[no-]curl] [(--folder|-f) <folder>]
12 + git imap-send [-v] [-q] [--[no-]curl] [--[no-]draft] [(--folder|-f) <folder>]
13 13 git imap-send --list
14 14
15 15
@@ -55,6 +55,13 @@ OPTIONS
55 55 using libcurl. Ignored if Git was built with the NO_OPENSSL option
56 56 set.
57 57
58 + `--draft`::
59 + `--no-draft`::
60 + Mark uploaded messages with the IMAP `\Draft` flag. The default is `--no-draft`.
61 + +
62 + With libcurl, `--draft` requires version 8.13.0 or later.
63 + Older libcurl still uploads the message but cannot set the flag.
64 +
58 65 `--list`::
59 66 Run the IMAP LIST command to output a list of all the folders present.
60 67
modified builtin/checkout.c
@@ -1340,51 +1340,13 @@ enum checkout_command {
1340 1340 CHECKOUT_RESTORE = 3,
1341 1341 };
1342 1342
1343 - static void advise_disambiguating_remotes(enum checkout_command which_command,
1344 - const char *branch,
1345 - const struct string_list *matched_remote_names)
1346 - {
1347 - const char *cmdname;
1348 - struct string_list_item *item;
1349 -
1350 - switch (which_command) {
1351 - case CHECKOUT_CHECKOUT:
1352 - cmdname = "checkout";
1353 - break;
1354 - case CHECKOUT_SWITCH:
1355 - cmdname = "switch";
1356 - break;
1357 - default:
1358 - BUG("command <%d> should not reach advise_disambiguating_remotes",
1359 - which_command);
1360 - break;
1361 - }
1362 -
1363 - advise(_("Branch name '%s' appears in multiple remotes:"), branch);
1364 - for_each_string_list_item(item, matched_remote_names) {
1365 - advise(_(" %s"), item->string);
1366 - }
1367 - advise(_("If you meant to check out a remote tracking branch on <remote>,\n"
1368 - "you can do so by fully qualifying the name with the --track option:\n"
1369 - "\n"
1370 - " git %s --track <remote>/%s\n"
1371 - "\n"
1372 - "If you'd like to always have checkouts of an ambiguous name prefer\n"
1373 - "one remote, e.g. the 'origin' remote, consider setting\n"
1374 - "checkout.defaultRemote=origin in your config."),
1375 - cmdname, branch);
1376 - }
1377 -
1378 1343 static char *parse_remote_branch(const char *arg,
1379 1344 struct object_id *rev,
1380 1345 int could_be_checkout_paths,
1381 1346 enum checkout_command which_command)
1382 1347 {
1383 1348 int num_matches = 0;
1384 - struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
1385 -
1386 - char *remote = unique_tracking_name(arg, rev, &num_matches,
1387 - &matched_remote_names);
1349 + char *remote = unique_tracking_name(arg, rev, &num_matches);
1388 1350
1389 1351 if (remote && could_be_checkout_paths) {
1390 1352 die(_("'%s' could be both a local file and a tracking branch.\n"
@@ -1393,15 +1355,37 @@ static char *parse_remote_branch(const char *arg,
1393 1355 }
1394 1356
1395 1357 if (!remote && num_matches > 1) {
1396 - if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
1397 - advise_disambiguating_remotes(which_command, arg,
1398 - &matched_remote_names);
1399 - die(_("'%s' matched multiple (%d) remote tracking branches"),
1400 - arg, num_matches);
1358 + if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) {
1359 + const char *cmdname;
1360 +
1361 + switch (which_command) {
1362 + case CHECKOUT_CHECKOUT:
1363 + cmdname = "checkout";
1364 + break;
1365 + case CHECKOUT_SWITCH:
1366 + cmdname = "switch";
1367 + break;
1368 + default:
1369 + BUG("command <%d> should not reach parse_remote_branch",
1370 + which_command);
1371 + break;
1372 + }
1373 +
1374 + advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1375 + "you can do so by fully qualifying the name with the --track option:\n"
1376 + "\n"
1377 + " git %s --track origin/<name>\n"
1378 + "\n"
1379 + "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1380 + "one remote, e.g. the 'origin' remote, consider setting\n"
1381 + "checkout.defaultRemote=origin in your config."),
1382 + cmdname);
1383 + }
1384 +
1385 + die(_("'%s' matched multiple (%d) remote tracking branches"),
1386 + arg, num_matches);
1401 1387 }
1402 1388
1403 - string_list_clear(&matched_remote_names, 0);
1404 -
1405 1389 return remote;
1406 1390 }
1407 1391
modified builtin/worktree.c
@@ -781,7 +781,7 @@ static char *dwim_branch(const char *path, char **new_branch)
781 781 *new_branch = branchname;
782 782 if (guess_remote) {
783 783 struct object_id oid;
784 - char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL);
784 + char *remote = unique_tracking_name(*new_branch, &oid, NULL);
785 785 return remote;
786 786 }
787 787 return NULL;
@@ -903,7 +903,7 @@ static int add(int ac, const char **av, const char *prefix,
903 903
904 904 commit = lookup_commit_reference_by_name(branch);
905 905 if (!commit) {
906 - remote = unique_tracking_name(branch, &oid, NULL, NULL);
906 + remote = unique_tracking_name(branch, &oid, NULL);
907 907 if (remote) {
908 908 new_branch = branch;
909 909 branch = new_branch_to_free = remote;
modified checkout.c
@@ -8,7 +8,6 @@
8 8 #include "checkout.h"
9 9 #include "config.h"
10 10 #include "strbuf.h"
11 - #include "string-list.h"
12 11
13 12 struct tracking_name_data {
14 13 /* const */ char *src_ref;
@@ -18,7 +17,6 @@ struct tracking_name_data {
18 17 const char *default_remote;
19 18 char *default_dst_ref;
20 19 struct object_id *default_dst_oid;
21 - struct string_list *remote_names;
22 20 };
23 21
24 22 #define TRACKING_NAME_DATA_INIT { 0 }
@@ -41,8 +39,6 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
41 39 oidcpy(dst, cb->dst_oid);
42 40 cb->default_dst_oid = dst;
43 41 }
44 - if (cb->remote_names)
45 - string_list_append(cb->remote_names, remote->name);
46 42 if (cb->dst_ref) {
47 43 free(query.dst);
48 44 return 0;
@@ -52,19 +48,14 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
52 48 }
53 49
54 50 char *unique_tracking_name(const char *name, struct object_id *oid,
55 - int *dwim_remotes_matched,
56 - struct string_list *dwim_remote_names)
51 + int *dwim_remotes_matched)
57 52 {
58 53 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
59 54 const char *default_remote = NULL;
60 -
61 - if (!repo_config_get_string_tmp(the_repository,
62 - "checkout.defaultremote",
63 - &default_remote))
55 + if (!repo_config_get_string_tmp(the_repository, "checkout.defaultremote", &default_remote))
64 56 cb_data.default_remote = default_remote;
65 57 cb_data.src_ref = xstrfmt("refs/heads/%s", name);
66 58 cb_data.dst_oid = oid;
67 - cb_data.remote_names = dwim_remote_names;
68 59 for_each_remote(check_tracking_name, &cb_data);
69 60 if (dwim_remotes_matched)
70 61 *dwim_remotes_matched = cb_data.num_matches;
modified checkout.h
@@ -3,8 +3,6 @@
3 3
4 4 #include "hash.h"
5 5
6 - struct string_list;
7 -
8 6 /*
9 7 * Check if the branch name uniquely matches a branch name on a remote
10 8 * tracking branch. Return the name of the remote if such a branch
@@ -12,7 +10,6 @@ struct string_list;
12 10 */
13 11 char *unique_tracking_name(const char *name,
14 12 struct object_id *oid,
15 - int *dwim_remotes_matched,
16 - struct string_list *dwim_remote_names);
13 + int *dwim_remotes_matched);
17 14
18 15 #endif /* CHECKOUT_H */
modified git-curl-compat.h
@@ -67,4 +67,12 @@
67 67 #define GIT_CURL_HAVE_CURLOPT_TCP_KEEPCNT
68 68 #endif
69 69
70 + /**
71 + * CURLOPT_UPLOAD_FLAGS and CURLULFLAG_* were added in 8.13.0,
72 + * released in April 2025.
73 + */
74 + #if LIBCURL_VERSION_NUM >= 0x080D00
75 + #define GIT_CURL_HAVE_CURLOPT_UPLOAD_FLAGS
76 + #endif
77 +
70 78 #endif
modified imap-send.c
@@ -35,6 +35,7 @@
35 35 #include "setup.h"
36 36 #include "strbuf.h"
37 37 #ifdef USE_CURL_FOR_IMAP_SEND
38 + #include "git-curl-compat.h"
38 39 #include "http.h"
39 40 #endif
40 41
@@ -49,10 +50,11 @@
49 50 static int verbosity;
50 51 static int list_folders;
51 52 static int use_curl = USE_CURL_DEFAULT;
53 + static int opt_draft;
52 54 static char *opt_folder;
53 55
54 56 static char const * const imap_send_usage[] = {
55 - N_("git imap-send [-v] [-q] [--[no-]curl] [(--folder|-f) <folder>] < <mbox>"),
57 + N_("git imap-send [-v] [-q] [--[no-]curl] [--[no-]draft] [(--folder|-f) <folder>] < <mbox>"),
56 58 "git imap-send --list",
57 59 NULL
58 60 };
@@ -60,6 +62,7 @@ static char const * const imap_send_usage[] = {
60 62 static struct option imap_send_options[] = {
61 63 OPT__VERBOSITY(&verbosity),
62 64 OPT_BOOL(0, "curl", &use_curl, "use libcurl to communicate with the IMAP server"),
65 + OPT_BOOL(0, "draft", &opt_draft, "mark uploaded messages with the IMAP \\Draft flag"),
63 66 OPT_STRING('f', "folder", &opt_folder, "folder", "specify the IMAP folder"),
64 67 OPT_BOOL(0, "list", &list_folders, "list all folders on the IMAP server"),
65 68 OPT_END()
@@ -1416,7 +1419,8 @@ static int imap_store_msg(struct imap_store *ctx, struct strbuf *msg)
1416 1419
1417 1420 box = ctx->name;
1418 1421 prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1419 - ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" ", prefix, box);
1422 + ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box,
1423 + opt_draft ? "(\\Draft) " : "");
1420 1424 imap->caps = imap->rcaps;
1421 1425 if (ret != DRV_OK)
1422 1426 return ret;
@@ -1718,6 +1722,13 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
1718 1722
1719 1723 curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf);
1720 1724
1725 + if (opt_draft) {
1726 + #ifdef GIT_CURL_HAVE_CURLOPT_UPLOAD_FLAGS
1727 + curl_easy_setopt(curl, CURLOPT_UPLOAD_FLAGS, CURLULFLAG_DRAFT);
1728 + #else
1729 + warning("--draft requires libcurl 8.13.0 or later");
1730 + #endif
1731 + }
1721 1732 fprintf(stderr, "Sending %d message%s to %s folder...\n",
1722 1733 total, (total != 1) ? "s" : "", server->folder);
1723 1734 while (1) {