Skip to content

Commit 3b990aa

Browse files
clickyotomygitster
authored andcommitted
push: parse and set flag for "--force-if-includes"
The previous commit added the necessary machinery to implement the "--force-if-includes" protection, when "--force-with-lease" is used without giving exact object the remote still ought to have. Surface the feature by adding a command line option and a configuration variable to enable it. - Add a flag: "TRANSPORT_PUSH_FORCE_IF_INCLUDES" to indicate that the new option was passed from the command line of via configuration settings; update command line and configuration parsers to set the new flag accordingly. - Introduce a new configuration option "push.useForceIfIncludes", which is equivalent to setting "--force-if-includes" in the command line. - Update "remote-curl" to recognize and pass this option to "send-pack" when enabled. - Update "advise" to catch the reject reason "REJECT_REF_NEEDS_UPDATE", set when the ref status is "REF_STATUS_REJECT_REMOTE_UPDATED" and (optionally) print a help message when the push fails. - The new option is a "no-op" in the following scenarios: * When used without "--force-with-lease". * When used with "--force-with-lease", and if the expected commit on the remote side is specified as an argument. Signed-off-by: Srinidhi Kaushik <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 99a1f9a commit 3b990aa

File tree

8 files changed

+68
-6
lines changed

8 files changed

+68
-6
lines changed

advice.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int advice_push_already_exists = 1;
1111
int advice_push_fetch_first = 1;
1212
int advice_push_needs_force = 1;
1313
int advice_push_unqualified_ref_name = 1;
14+
int advice_push_ref_needs_update = 1;
1415
int advice_status_hints = 1;
1516
int advice_status_u_option = 1;
1617
int advice_status_ahead_behind_warning = 1;
@@ -72,6 +73,7 @@ static struct {
7273
{ "pushFetchFirst", &advice_push_fetch_first },
7374
{ "pushNeedsForce", &advice_push_needs_force },
7475
{ "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
76+
{ "pushRefNeedsUpdate", &advice_push_ref_needs_update },
7577
{ "statusHints", &advice_status_hints },
7678
{ "statusUoption", &advice_status_u_option },
7779
{ "statusAheadBehindWarning", &advice_status_ahead_behind_warning },
@@ -116,6 +118,7 @@ static struct {
116118
[ADVICE_PUSH_ALREADY_EXISTS] = { "pushAlreadyExists", 1 },
117119
[ADVICE_PUSH_FETCH_FIRST] = { "pushFetchFirst", 1 },
118120
[ADVICE_PUSH_NEEDS_FORCE] = { "pushNeedsForce", 1 },
121+
[ADVICE_PUSH_REF_NEEDS_UPDATE] = { "pushRefNeedsUpdate", 1 },
119122

120123
/* make this an alias for backward compatibility */
121124
[ADVICE_PUSH_UPDATE_REJECTED_ALIAS] = { "pushNonFastForward", 1 },

advice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern int advice_push_already_exists;
1111
extern int advice_push_fetch_first;
1212
extern int advice_push_needs_force;
1313
extern int advice_push_unqualified_ref_name;
14+
extern int advice_push_ref_needs_update;
1415
extern int advice_status_hints;
1516
extern int advice_status_u_option;
1617
extern int advice_status_ahead_behind_warning;
@@ -60,6 +61,7 @@ extern int advice_add_empty_pathspec;
6061
ADVICE_PUSH_UNQUALIFIED_REF_NAME,
6162
ADVICE_PUSH_UPDATE_REJECTED_ALIAS,
6263
ADVICE_PUSH_UPDATE_REJECTED,
64+
ADVICE_PUSH_REF_NEEDS_UPDATE,
6365
ADVICE_RESET_QUIET_WARNING,
6466
ADVICE_RESOLVE_CONFLICT,
6567
ADVICE_RM_HINTS,

builtin/push.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ static const char message_advice_ref_needs_force[] =
290290
"or update a remote ref to make it point at a non-commit object,\n"
291291
"without using the '--force' option.\n");
292292

293+
static const char message_advice_ref_needs_update[] =
294+
N_("Updates were rejected because the tip of the remote-tracking\n"
295+
"branch has been updated since the last checkout. You may want\n"
296+
"to integrate those changes locally (e.g., 'git pull ...')\n"
297+
"before forcing an update.\n");
298+
293299
static void advise_pull_before_push(void)
294300
{
295301
if (!advice_push_non_ff_current || !advice_push_update_rejected)
@@ -325,6 +331,13 @@ static void advise_ref_needs_force(void)
325331
advise(_(message_advice_ref_needs_force));
326332
}
327333

334+
static void advise_ref_needs_update(void)
335+
{
336+
if (!advice_push_ref_needs_update || !advice_push_update_rejected)
337+
return;
338+
advise(_(message_advice_ref_needs_update));
339+
}
340+
328341
static int push_with_options(struct transport *transport, struct refspec *rs,
329342
int flags)
330343
{
@@ -374,6 +387,8 @@ static int push_with_options(struct transport *transport, struct refspec *rs,
374387
advise_ref_fetch_first();
375388
} else if (reject_reasons & REJECT_NEEDS_FORCE) {
376389
advise_ref_needs_force();
390+
} else if (reject_reasons & REJECT_REF_NEEDS_UPDATE) {
391+
advise_ref_needs_update();
377392
}
378393

379394
return 1;
@@ -510,6 +525,12 @@ static int git_push_config(const char *k, const char *v, void *cb)
510525
if (!v)
511526
return config_error_nonbool(k);
512527
return color_parse(v, push_colors[slot]);
528+
} else if (!strcmp(k, "push.useforceifincludes")) {
529+
if (git_config_bool(k, v))
530+
*flags |= TRANSPORT_PUSH_FORCE_IF_INCLUDES;
531+
else
532+
*flags &= ~TRANSPORT_PUSH_FORCE_IF_INCLUDES;
533+
return 0;
513534
}
514535

515536
return git_default_config(k, v, NULL);
@@ -541,6 +562,9 @@ int cmd_push(int argc, const char **argv, const char *prefix)
541562
OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
542563
N_("require old value of ref to be at this value"),
543564
PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, parseopt_push_cas_option),
565+
OPT_BIT(0, TRANS_OPT_FORCE_IF_INCLUDES, &flags,
566+
N_("require remote updates to be integrated locally"),
567+
TRANSPORT_PUSH_FORCE_IF_INCLUDES),
544568
OPT_CALLBACK(0, "recurse-submodules", &recurse_submodules, "(check|on-demand|no)",
545569
N_("control recursive pushing of submodules"), option_parse_recurse_submodules),
546570
OPT_BOOL_F( 0 , "thin", &thin, N_("use thin pack"), PARSE_OPT_NOCOMPLETE),
@@ -625,6 +649,9 @@ int cmd_push(int argc, const char **argv, const char *prefix)
625649
if ((flags & TRANSPORT_PUSH_ALL) && (flags & TRANSPORT_PUSH_MIRROR))
626650
die(_("--all and --mirror are incompatible"));
627651

652+
if (!is_empty_cas(&cas) && (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES))
653+
cas.use_force_if_includes = 1;
654+
628655
for_each_string_list_item(item, push_options)
629656
if (strchr(item->string, '\n'))
630657
die(_("push options must not have new line characters"));

builtin/send-pack.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
178178
int progress = -1;
179179
int from_stdin = 0;
180180
struct push_cas_option cas = {0};
181+
int force_if_includes = 0;
181182
struct packet_reader reader;
182183

183184
struct option options[] = {
@@ -203,6 +204,8 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
203204
OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
204205
N_("require old value of ref to be at this value"),
205206
PARSE_OPT_OPTARG, parseopt_push_cas_option),
207+
OPT_BOOL(0, TRANS_OPT_FORCE_IF_INCLUDES, &force_if_includes,
208+
N_("require remote updates to be integrated locally")),
206209
OPT_END()
207210
};
208211

@@ -304,6 +307,9 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
304307
if (!is_empty_cas(&cas))
305308
apply_push_cas(&cas, remote, remote_refs);
306309

310+
if (!is_empty_cas(&cas) && force_if_includes)
311+
cas.use_force_if_includes = 1;
312+
307313
set_ref_status_for_push(remote_refs, args.send_mirror,
308314
args.force_update);
309315

remote-curl.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ struct options {
4444
from_promisor : 1,
4545

4646
atomic : 1,
47-
object_format : 1;
47+
object_format : 1,
48+
force_if_includes : 1;
4849
const struct git_hash_algo *hash_algo;
4950
};
5051
static struct options options;
@@ -131,6 +132,14 @@ static int set_option(const char *name, const char *value)
131132
string_list_append(&cas_options, val.buf);
132133
strbuf_release(&val);
133134
return 0;
135+
} else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
136+
if (!strcmp(value, "true"))
137+
options.force_if_includes = 1;
138+
else if (!strcmp(value, "false"))
139+
options.force_if_includes = 0;
140+
else
141+
return -1;
142+
return 0;
134143
} else if (!strcmp(name, "cloning")) {
135144
if (!strcmp(value, "true"))
136145
options.cloning = 1;
@@ -1318,6 +1327,9 @@ static int push_git(struct discovery *heads, int nr_spec, const char **specs)
13181327
strvec_push(&args, cas_option->string);
13191328
strvec_push(&args, url.buf);
13201329

1330+
if (options.force_if_includes)
1331+
strvec_push(&args, "--force-if-includes");
1332+
13211333
strvec_push(&args, "--stdin");
13221334
for (i = 0; i < nr_spec; i++)
13231335
packet_buf_write(&preamble, "%s\n", specs[i]);

transport-helper.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,11 @@ static void set_common_push_options(struct transport *transport,
938938
if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
939939
die(_("helper %s does not support --atomic"), name);
940940

941+
if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)
942+
if (set_helper_option(transport, TRANS_OPT_FORCE_IF_INCLUDES, "true") != 0)
943+
die(_("helper %s does not support --%s"),
944+
name, TRANS_OPT_FORCE_IF_INCLUDES);
945+
941946
if (flags & TRANSPORT_PUSH_OPTIONS) {
942947
struct string_list_item *item;
943948
for_each_string_list_item(item, transport->push_options)

transport.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ void transport_print_push_status(const char *dest, struct ref *refs,
748748
*reject_reasons |= REJECT_FETCH_FIRST;
749749
} else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
750750
*reject_reasons |= REJECT_NEEDS_FORCE;
751+
} else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) {
752+
*reject_reasons |= REJECT_REF_NEEDS_UPDATE;
751753
}
752754
}
753755
free(head);

transport.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ struct transport {
136136
#define TRANSPORT_PUSH_ATOMIC (1<<13)
137137
#define TRANSPORT_PUSH_OPTIONS (1<<14)
138138
#define TRANSPORT_RECURSE_SUBMODULES_ONLY (1<<15)
139+
#define TRANSPORT_PUSH_FORCE_IF_INCLUDES (1<<16)
139140

140141
int transport_summary_width(const struct ref *refs);
141142

@@ -208,6 +209,9 @@ void transport_check_allowed(const char *type);
208209
/* Request atomic (all-or-nothing) updates when pushing */
209210
#define TRANS_OPT_ATOMIC "atomic"
210211

212+
/* Require remote changes to be integrated locally. */
213+
#define TRANS_OPT_FORCE_IF_INCLUDES "force-if-includes"
214+
211215
/**
212216
* Returns 0 if the option was used, non-zero otherwise. Prints a
213217
* message to stderr if the option is not used.
@@ -217,11 +221,12 @@ int transport_set_option(struct transport *transport, const char *name,
217221
void transport_set_verbosity(struct transport *transport, int verbosity,
218222
int force_progress);
219223

220-
#define REJECT_NON_FF_HEAD 0x01
221-
#define REJECT_NON_FF_OTHER 0x02
222-
#define REJECT_ALREADY_EXISTS 0x04
223-
#define REJECT_FETCH_FIRST 0x08
224-
#define REJECT_NEEDS_FORCE 0x10
224+
#define REJECT_NON_FF_HEAD 0x01
225+
#define REJECT_NON_FF_OTHER 0x02
226+
#define REJECT_ALREADY_EXISTS 0x04
227+
#define REJECT_FETCH_FIRST 0x08
228+
#define REJECT_NEEDS_FORCE 0x10
229+
#define REJECT_REF_NEEDS_UPDATE 0x20
225230

226231
int transport_push(struct repository *repo,
227232
struct transport *connection,

0 commit comments

Comments
 (0)