diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 8fffdbf20058e9970af4b5e4a14349ecb4ff455c..76c72065de73ea3f0da4665c0a47a64610e2ead2 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -406,50 +406,20 @@ int send_pack(struct send_pack_args *args, */ new_refs = 0; for (ref = remote_refs; ref; ref = ref->next) { - - if (ref->peer_ref) - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); - else if (!args->send_mirror) + if (!ref->peer_ref && !args->send_mirror) continue; - ref->deletion = is_null_sha1(ref->new_sha1); - if (ref->deletion && !allow_deleting_refs) { - ref->status = REF_STATUS_REJECT_NODELETE; - continue; - } - if (!ref->deletion && - !hashcmp(ref->old_sha1, ref->new_sha1)) { - ref->status = REF_STATUS_UPTODATE; + /* Check for statuses set by set_ref_status_for_push() */ + switch (ref->status) { + case REF_STATUS_REJECT_NONFASTFORWARD: + case REF_STATUS_UPTODATE: continue; + default: + ; /* do nothing */ } - /* This part determines what can overwrite what. - * The rules are: - * - * (0) you can always use --force or +A:B notation to - * selectively force individual ref pairs. - * - * (1) if the old thing does not exist, it is OK. - * - * (2) if you do not have the old thing, you are not allowed - * to overwrite it; you would not know what you are losing - * otherwise. - * - * (3) if both new and old are commit-ish, and new is a - * descendant of old, it is OK. - * - * (4) regardless of all of the above, removing :B is - * always allowed. - */ - - ref->nonfastforward = - !ref->deletion && - !is_null_sha1(ref->old_sha1) && - (!has_sha1_file(ref->old_sha1) - || !ref_newer(ref->new_sha1, ref->old_sha1)); - - if (ref->nonfastforward && !ref->force && !args->force_update) { - ref->status = REF_STATUS_REJECT_NONFASTFORWARD; + if (ref->deletion && !allow_deleting_refs) { + ref->status = REF_STATUS_REJECT_NODELETE; continue; } @@ -673,6 +643,9 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix) if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags)) return -1; + set_ref_status_for_push(remote_refs, args.send_mirror, + args.force_update); + ret = send_pack(&args, fd, conn, remote_refs, &extra_have); if (helper_status) diff --git a/remote.c b/remote.c index e3afecdb1069a244493e906b5e16a96dc47535d3..c70181cdc621b27ed02aba17b3e4f7ab64518e9f 100644 --- a/remote.c +++ b/remote.c @@ -1247,6 +1247,56 @@ int match_refs(struct ref *src, struct ref **dst, return 0; } +void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, + int force_update) +{ + struct ref *ref; + + for (ref = remote_refs; ref; ref = ref->next) { + if (ref->peer_ref) + hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); + else if (!send_mirror) + continue; + + ref->deletion = is_null_sha1(ref->new_sha1); + if (!ref->deletion && + !hashcmp(ref->old_sha1, ref->new_sha1)) { + ref->status = REF_STATUS_UPTODATE; + continue; + } + + /* This part determines what can overwrite what. + * The rules are: + * + * (0) you can always use --force or +A:B notation to + * selectively force individual ref pairs. + * + * (1) if the old thing does not exist, it is OK. + * + * (2) if you do not have the old thing, you are not allowed + * to overwrite it; you would not know what you are losing + * otherwise. + * + * (3) if both new and old are commit-ish, and new is a + * descendant of old, it is OK. + * + * (4) regardless of all of the above, removing :B is + * always allowed. + */ + + ref->nonfastforward = + !ref->deletion && + !is_null_sha1(ref->old_sha1) && + (!has_sha1_file(ref->old_sha1) + || !ref_newer(ref->new_sha1, ref->old_sha1)); + + if (ref->nonfastforward && !ref->force && !force_update) { + ref->status = REF_STATUS_REJECT_NONFASTFORWARD; + continue; + } + } +} + struct branch *branch_get(const char *name) { struct branch *ret; diff --git a/remote.h b/remote.h index 8b7ecf9197f2a9210299c9700cc88a92a796f714..6e13643cabb6fa9a5b619f53dd148345d9161ad4 100644 --- a/remote.h +++ b/remote.h @@ -98,6 +98,8 @@ char *apply_refspecs(struct refspec *refspecs, int nr_refspec, int match_refs(struct ref *src, struct ref **dst, int nr_refspec, const char **refspec, int all); +void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, + int force_update); /* * Given a list of the remote refs and the specification of things to diff --git a/t/t5541-http-push.sh b/t/t5541-http-push.sh index 2a58d0cc9cbfeec1085c97bf288fcd1c50c03aef..83a8e14c6c385d6d6fbdd179d30018128e8a200f 100755 --- a/t/t5541-http-push.sh +++ b/t/t5541-http-push.sh @@ -88,5 +88,49 @@ test_expect_success 'used receive-pack service' ' test_cmp exp act ' +test_expect_success 'non-fast-forward push fails' ' + cd "$ROOT_PATH"/test_repo_clone && + git checkout master && + echo "changed" > path2 && + git commit -a -m path2 --amend && + + HEAD=$(git rev-parse --verify HEAD) && + !(git push -v origin >output 2>&1) && + (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git && + test $HEAD != $(git rev-parse --verify HEAD)) +' + +test_expect_success 'non-fast-forward push show ref status' ' + grep "^ ! \[rejected\][ ]*master -> master (non-fast-forward)$" output +' + +test_expect_success 'non-fast-forward push shows help message' ' + grep \ +"To prevent you from losing history, non-fast-forward updates were rejected +Merge the remote changes before pushing again. See the '"'non-fast-forward'"' +section of '"'git push --help'"' for details." output +' + +test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' ' + # create a dissimilarly-named remote ref so that git is unable to match the + # two refs (viz. local, remote) unless an explicit refspec is provided. + git push origin master:retsam + + echo "change changed" > path2 && + git commit -a -m path2 --amend && + + # push master too; this ensures there is at least one '"'push'"' command to + # the remote helper and triggers interaction with the helper. + !(git push -v origin +master master:retsam >output 2>&1) && + + grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *master -> master (forced update)$" output && + grep "^ ! \[rejected\] *master -> retsam (non-fast-forward)$" output && + + grep \ +"To prevent you from losing history, non-fast-forward updates were rejected +Merge the remote changes before pushing again. See the '"'non-fast-forward'"' +section of '"'git push --help'"' for details." output +' + stop_httpd test_done diff --git a/transport-helper.c b/transport-helper.c index ca8fa92e638e2589e2b6b8ffbe21ac824b5274a5..fdf22562201d12d1321baae9157a0ca70053a7cb 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -528,24 +528,27 @@ static int push_refs(struct transport *transport, return transport->push_refs(transport, remote_refs, flags); } - if (!remote_refs) + if (!remote_refs) { + fprintf(stderr, "No refs in common and none specified; doing nothing.\n" + "Perhaps you should specify a branch such as 'master'.\n"); return 0; + } helper = get_helper(transport); if (!data->push) return 1; for (ref = remote_refs; ref; ref = ref->next) { - if (ref->peer_ref) - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); - else if (!mirror) + if (!ref->peer_ref && !mirror) continue; - ref->deletion = is_null_sha1(ref->new_sha1); - if (!ref->deletion && - !hashcmp(ref->old_sha1, ref->new_sha1)) { - ref->status = REF_STATUS_UPTODATE; + /* Check for statuses set by set_ref_status_for_push() */ + switch (ref->status) { + case REF_STATUS_REJECT_NONFASTFORWARD: + case REF_STATUS_UPTODATE: continue; + default: + ; /* do nothing */ } if (force_all) @@ -634,6 +637,15 @@ static int push_refs(struct transport *transport, continue; } + if (ref->status != REF_STATUS_NONE) { + /* + * Earlier, the ref was marked not to be pushed, so ignore the ref + * status reported by the remote helper if the latter is 'no match'. + */ + if (status == REF_STATUS_NONE) + continue; + } + ref->status = status; ref->remote_status = msg; } diff --git a/transport.c b/transport.c index c3f156ea04636a722a49aca225175a6f9e9259c9..d7db2941fb135c8d956756ced762dc0a3dd75a4b 100644 --- a/transport.c +++ b/transport.c @@ -983,7 +983,7 @@ int transport_push(struct transport *transport, int verbose = flags & TRANSPORT_PUSH_VERBOSE; int quiet = flags & TRANSPORT_PUSH_QUIET; int porcelain = flags & TRANSPORT_PUSH_PORCELAIN; - int ret; + int ret, err; if (flags & TRANSPORT_PUSH_ALL) match_flags |= MATCH_REFS_ALL; @@ -995,9 +995,16 @@ int transport_push(struct transport *transport, return -1; } + set_ref_status_for_push(remote_refs, + flags & TRANSPORT_PUSH_MIRROR, + flags & TRANSPORT_PUSH_FORCE); + ret = transport->push_refs(transport, remote_refs, flags); + err = push_had_errors(remote_refs); + + ret |= err; - if (!quiet || push_had_errors(remote_refs)) + if (!quiet || err) print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain, nonfastforward);