Skip to content

Commit 75e5c0d

Browse files
committed
push: introduce REJECT_FETCH_FIRST and REJECT_NEEDS_FORCE
When we push to update an existing ref, if: * the object at the tip of the remote is not a commit; or * the object we are pushing is not a commit, it won't be correct to suggest to fetch, integrate and push again, as the old and new objects will not "merge". We should explain that the push must be forced when there is a non-committish object is involved in such a case. If we do not have the current object at the tip of the remote, we do not even know that object, when fetched, is something that can be merged. In such a case, suggesting to pull first just like non-fast-forward case may not be technically correct, but in practice, most such failures are seen when you try to push your work to a branch without knowing that somebody else already pushed to update the same branch since you forked, so "pull first" would work as a suggestion most of the time. And if the object at the tip is not a commit, "pull first" will fail, without making any permanent damage. As a side effect, it also makes the error message the user will get during the next "push" attempt easier to understand, now the user is aware that a non-commit object is involved. In these cases, the current code already rejects such a push on the client end, but we used the same error and advice messages as the ones used when rejecting a non-fast-forward push, i.e. pull from there and integrate before pushing again. Introduce new rejection reasons and reword the messages appropriately. [jc: with help by Peff on message details] Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0f4d498 commit 75e5c0d

File tree

11 files changed

+93
-4
lines changed

11 files changed

+93
-4
lines changed

Documentation/config.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ advice.*::
143143
pushUpdateRejected::
144144
Set this variable to 'false' if you want to disable
145145
'pushNonFFCurrent', 'pushNonFFDefault',
146-
'pushNonFFMatching', and 'pushAlreadyExists'
146+
'pushNonFFMatching', 'pushAlreadyExists',
147+
'pushFetchFirst', and 'pushNeedsForce'
147148
simultaneously.
148149
pushNonFFCurrent::
149150
Advice shown when linkgit:git-push[1] fails due to a
@@ -162,6 +163,15 @@ advice.*::
162163
pushAlreadyExists::
163164
Shown when linkgit:git-push[1] rejects an update that
164165
does not qualify for fast-forwarding (e.g., a tag.)
166+
pushFetchFirst::
167+
Shown when linkgit:git-push[1] rejects an update that
168+
tries to overwrite a remote ref that points at an
169+
object we do not have.
170+
pushNeedsForce::
171+
Shown when linkgit:git-push[1] rejects an update that
172+
tries to overwrite a remote ref that points at an
173+
object that is not a committish, or make the remote
174+
ref point at an object that is not a committish.
165175
statusHints::
166176
Show directions on how to proceed from the current
167177
state in the output of linkgit:git-status[1] and in

advice.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ int advice_push_non_ff_current = 1;
55
int advice_push_non_ff_default = 1;
66
int advice_push_non_ff_matching = 1;
77
int advice_push_already_exists = 1;
8+
int advice_push_fetch_first = 1;
9+
int advice_push_needs_force = 1;
810
int advice_status_hints = 1;
911
int advice_commit_before_merge = 1;
1012
int advice_resolve_conflict = 1;
@@ -20,6 +22,8 @@ static struct {
2022
{ "pushnonffdefault", &advice_push_non_ff_default },
2123
{ "pushnonffmatching", &advice_push_non_ff_matching },
2224
{ "pushalreadyexists", &advice_push_already_exists },
25+
{ "pushfetchfirst", &advice_push_fetch_first },
26+
{ "pushneedsforce", &advice_push_needs_force },
2327
{ "statushints", &advice_status_hints },
2428
{ "commitbeforemerge", &advice_commit_before_merge },
2529
{ "resolveconflict", &advice_resolve_conflict },

advice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern int advice_push_non_ff_current;
88
extern int advice_push_non_ff_default;
99
extern int advice_push_non_ff_matching;
1010
extern int advice_push_already_exists;
11+
extern int advice_push_fetch_first;
12+
extern int advice_push_needs_force;
1113
extern int advice_status_hints;
1214
extern int advice_commit_before_merge;
1315
extern int advice_resolve_conflict;

builtin/push.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,22 @@ static const char message_advice_checkout_pull_push[] =
220220
"(e.g. 'git pull') before pushing again.\n"
221221
"See the 'Note about fast-forwards' in 'git push --help' for details.");
222222

223+
static const char message_advice_ref_fetch_first[] =
224+
N_("Updates were rejected because the remote contains work that you do\n"
225+
"not have locally. This is usually caused by another repository pushing\n"
226+
"to the same ref. You may want to first merge the remote changes (e.g.,\n"
227+
"'git pull') before pushing again.\n"
228+
"See the 'Note about fast-forwards' in 'git push --help' for details.");
229+
223230
static const char message_advice_ref_already_exists[] =
224231
N_("Updates were rejected because the destination reference already exists\n"
225232
"in the remote.");
226233

234+
static const char message_advice_ref_needs_force[] =
235+
N_("You cannot update a remote ref that points at a non-commit object,\n"
236+
"or update a remote ref to make it point at a non-commit object,\n"
237+
"without using the '--force' option.\n");
238+
227239
static void advise_pull_before_push(void)
228240
{
229241
if (!advice_push_non_ff_current || !advice_push_update_rejected)
@@ -252,6 +264,20 @@ static void advise_ref_already_exists(void)
252264
advise(_(message_advice_ref_already_exists));
253265
}
254266

267+
static void advise_ref_fetch_first(void)
268+
{
269+
if (!advice_push_fetch_first || !advice_push_update_rejected)
270+
return;
271+
advise(_(message_advice_ref_fetch_first));
272+
}
273+
274+
static void advise_ref_needs_force(void)
275+
{
276+
if (!advice_push_needs_force || !advice_push_update_rejected)
277+
return;
278+
advise(_(message_advice_ref_needs_force));
279+
}
280+
255281
static int push_with_options(struct transport *transport, int flags)
256282
{
257283
int err;
@@ -285,6 +311,10 @@ static int push_with_options(struct transport *transport, int flags)
285311
advise_checkout_pull_push();
286312
} else if (reject_reasons & REJECT_ALREADY_EXISTS) {
287313
advise_ref_already_exists();
314+
} else if (reject_reasons & REJECT_FETCH_FIRST) {
315+
advise_ref_fetch_first();
316+
} else if (reject_reasons & REJECT_NEEDS_FORCE) {
317+
advise_ref_needs_force();
288318
}
289319

290320
return 1;

builtin/send-pack.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ static void print_helper_status(struct ref *ref)
4444
msg = "non-fast forward";
4545
break;
4646

47+
case REF_STATUS_REJECT_FETCH_FIRST:
48+
res = "error";
49+
msg = "fetch first";
50+
break;
51+
52+
case REF_STATUS_REJECT_NEEDS_FORCE:
53+
res = "error";
54+
msg = "needs force";
55+
break;
56+
4757
case REF_STATUS_REJECT_ALREADY_EXISTS:
4858
res = "error";
4959
msg = "already exists";

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ struct ref {
10101010
REF_STATUS_REJECT_NONFASTFORWARD,
10111011
REF_STATUS_REJECT_ALREADY_EXISTS,
10121012
REF_STATUS_REJECT_NODELETE,
1013+
REF_STATUS_REJECT_FETCH_FIRST,
1014+
REF_STATUS_REJECT_NEEDS_FORCE,
10131015
REF_STATUS_UPTODATE,
10141016
REF_STATUS_REMOTE_REJECT,
10151017
REF_STATUS_EXPECTING_REPORT

remote.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,8 +1322,12 @@ void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
13221322

13231323
if (!prefixcmp(ref->name, "refs/tags/"))
13241324
why = REF_STATUS_REJECT_ALREADY_EXISTS;
1325-
else if (!has_sha1_file(ref->old_sha1)
1326-
|| !ref_newer(ref->new_sha1, ref->old_sha1))
1325+
else if (!has_sha1_file(ref->old_sha1))
1326+
why = REF_STATUS_REJECT_FETCH_FIRST;
1327+
else if (!lookup_commit_reference_gently(ref->old_sha1, 1) ||
1328+
!lookup_commit_reference_gently(ref->new_sha1, 1))
1329+
why = REF_STATUS_REJECT_NEEDS_FORCE;
1330+
else if (!ref_newer(ref->new_sha1, ref->old_sha1))
13271331
why = REF_STATUS_REJECT_NONFASTFORWARD;
13281332

13291333
if (!force_ref_update)
@@ -1512,7 +1516,8 @@ int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
15121516
struct commit_list *list, *used;
15131517
int found = 0;
15141518

1515-
/* Both new and old must be commit-ish and new is descendant of
1519+
/*
1520+
* Both new and old must be commit-ish and new is descendant of
15161521
* old. Otherwise we require --force.
15171522
*/
15181523
o = deref_tag(parse_object(old_sha1), NULL, 0);

send-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ int send_pack(struct send_pack_args *args,
230230
switch (ref->status) {
231231
case REF_STATUS_REJECT_NONFASTFORWARD:
232232
case REF_STATUS_REJECT_ALREADY_EXISTS:
233+
case REF_STATUS_REJECT_FETCH_FIRST:
234+
case REF_STATUS_REJECT_NEEDS_FORCE:
233235
case REF_STATUS_UPTODATE:
234236
continue;
235237
default:

transport-helper.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,16 @@ static void push_update_ref_status(struct strbuf *buf,
666666
free(msg);
667667
msg = NULL;
668668
}
669+
else if (!strcmp(msg, "fetch first")) {
670+
status = REF_STATUS_REJECT_FETCH_FIRST;
671+
free(msg);
672+
msg = NULL;
673+
}
674+
else if (!strcmp(msg, "needs force")) {
675+
status = REF_STATUS_REJECT_NEEDS_FORCE;
676+
free(msg);
677+
msg = NULL;
678+
}
669679
}
670680

671681
if (*ref)

transport.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,14 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, i
699699
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
700700
"already exists", porcelain);
701701
break;
702+
case REF_STATUS_REJECT_FETCH_FIRST:
703+
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
704+
"fetch first", porcelain);
705+
break;
706+
case REF_STATUS_REJECT_NEEDS_FORCE:
707+
print_ref_status('!', "[rejected]", ref, ref->peer_ref,
708+
"needs force", porcelain);
709+
break;
702710
case REF_STATUS_REMOTE_REJECT:
703711
print_ref_status('!', "[remote rejected]", ref,
704712
ref->deletion ? NULL : ref->peer_ref,
@@ -750,6 +758,10 @@ void transport_print_push_status(const char *dest, struct ref *refs,
750758
*reject_reasons |= REJECT_NON_FF_OTHER;
751759
} else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
752760
*reject_reasons |= REJECT_ALREADY_EXISTS;
761+
} else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
762+
*reject_reasons |= REJECT_FETCH_FIRST;
763+
} else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
764+
*reject_reasons |= REJECT_NEEDS_FORCE;
753765
}
754766
}
755767
}

0 commit comments

Comments
 (0)