Skip to content

Commit c5da24a

Browse files
committed
Merge branch 'ct/advise-push-default'
Break down the cases in which "git push" fails due to non-ff into three categories, and give separate advise messages for each case. By Christopher Tiwald (2) and Jeff King (1) * ct/advise-push-default: Fix httpd tests that broke when non-ff push advice changed clean up struct ref's nonfastforward field push: Provide situational hints for non-fast-forward errors
2 parents af78c31 + 0aff719 commit c5da24a

File tree

10 files changed

+99
-13
lines changed

10 files changed

+99
-13
lines changed

Documentation/config.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,23 @@ advice.*::
138138
+
139139
--
140140
pushNonFastForward::
141-
Advice shown when linkgit:git-push[1] refuses
142-
non-fast-forward refs.
141+
Set this variable to 'false' if you want to disable
142+
'pushNonFFCurrent', 'pushNonFFDefault', and
143+
'pushNonFFMatching' simultaneously.
144+
pushNonFFCurrent::
145+
Advice shown when linkgit:git-push[1] fails due to a
146+
non-fast-forward update to the current branch.
147+
pushNonFFDefault::
148+
Advice to set 'push.default' to 'upstream' or 'current'
149+
when you ran linkgit:git-push[1] and pushed 'matching
150+
refs' by default (i.e. you did not provide an explicit
151+
refspec, and no 'push.default' configuration was set)
152+
and it resulted in a non-fast-forward error.
153+
pushNonFFMatching::
154+
Advice shown when you ran linkgit:git-push[1] and pushed
155+
'matching refs' explicitly (i.e. you used ':', or
156+
specified a refspec that isn't your current branch) and
157+
it resulted in a non-fast-forward error.
143158
statusHints::
144159
Directions on how to stage/unstage/add shown in the
145160
output of linkgit:git-status[1] and the template shown

advice.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "cache.h"
22

33
int advice_push_nonfastforward = 1;
4+
int advice_push_non_ff_current = 1;
5+
int advice_push_non_ff_default = 1;
6+
int advice_push_non_ff_matching = 1;
47
int advice_status_hints = 1;
58
int advice_commit_before_merge = 1;
69
int advice_resolve_conflict = 1;
@@ -12,6 +15,9 @@ static struct {
1215
int *preference;
1316
} advice_config[] = {
1417
{ "pushnonfastforward", &advice_push_nonfastforward },
18+
{ "pushnonffcurrent", &advice_push_non_ff_current },
19+
{ "pushnonffdefault", &advice_push_non_ff_default },
20+
{ "pushnonffmatching", &advice_push_non_ff_matching },
1521
{ "statushints", &advice_status_hints },
1622
{ "commitbeforemerge", &advice_commit_before_merge },
1723
{ "resolveconflict", &advice_resolve_conflict },

advice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "git-compat-util.h"
55

66
extern int advice_push_nonfastforward;
7+
extern int advice_push_non_ff_current;
8+
extern int advice_push_non_ff_default;
9+
extern int advice_push_non_ff_matching;
710
extern int advice_status_hints;
811
extern int advice_commit_before_merge;
912
extern int advice_resolve_conflict;

builtin/push.c

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static int progress = -1;
2424
static const char **refspec;
2525
static int refspec_nr;
2626
static int refspec_alloc;
27+
static int default_matching_used;
2728

2829
static void add_refspec(const char *ref)
2930
{
@@ -111,6 +112,9 @@ static void setup_default_push_refspecs(struct remote *remote)
111112
{
112113
switch (push_default) {
113114
default:
115+
case PUSH_DEFAULT_UNSPECIFIED:
116+
default_matching_used = 1;
117+
/* fallthru */
114118
case PUSH_DEFAULT_MATCHING:
115119
add_refspec(":");
116120
break;
@@ -130,6 +134,45 @@ static void setup_default_push_refspecs(struct remote *remote)
130134
}
131135
}
132136

137+
static const char message_advice_pull_before_push[] =
138+
N_("Updates were rejected because the tip of your current branch is behind\n"
139+
"its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
140+
"before pushing again.\n"
141+
"See the 'Note about fast-forwards' in 'git push --help' for details.");
142+
143+
static const char message_advice_use_upstream[] =
144+
N_("Updates were rejected because a pushed branch tip is behind its remote\n"
145+
"counterpart. If you did not intend to push that branch, you may want to\n"
146+
"specify branches to push or set the 'push.default' configuration\n"
147+
"variable to 'current' or 'upstream' to push only the current branch.");
148+
149+
static const char message_advice_checkout_pull_push[] =
150+
N_("Updates were rejected because a pushed branch tip is behind its remote\n"
151+
"counterpart. Check out this branch and merge the remote changes\n"
152+
"(e.g. 'git pull') before pushing again.\n"
153+
"See the 'Note about fast-forwards' in 'git push --help' for details.");
154+
155+
static void advise_pull_before_push(void)
156+
{
157+
if (!advice_push_non_ff_current || !advice_push_nonfastforward)
158+
return;
159+
advise(_(message_advice_pull_before_push));
160+
}
161+
162+
static void advise_use_upstream(void)
163+
{
164+
if (!advice_push_non_ff_default || !advice_push_nonfastforward)
165+
return;
166+
advise(_(message_advice_use_upstream));
167+
}
168+
169+
static void advise_checkout_pull_push(void)
170+
{
171+
if (!advice_push_non_ff_matching || !advice_push_nonfastforward)
172+
return;
173+
advise(_(message_advice_checkout_pull_push));
174+
}
175+
133176
static int push_with_options(struct transport *transport, int flags)
134177
{
135178
int err;
@@ -151,14 +194,21 @@ static int push_with_options(struct transport *transport, int flags)
151194
error(_("failed to push some refs to '%s'"), transport->url);
152195

153196
err |= transport_disconnect(transport);
154-
155197
if (!err)
156198
return 0;
157199

158-
if (nonfastforward && advice_push_nonfastforward) {
159-
fprintf(stderr, _("To prevent you from losing history, non-fast-forward updates were rejected\n"
160-
"Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
161-
"'Note about fast-forwards' section of 'git push --help' for details.\n"));
200+
switch (nonfastforward) {
201+
default:
202+
break;
203+
case NON_FF_HEAD:
204+
advise_pull_before_push();
205+
break;
206+
case NON_FF_OTHER:
207+
if (default_matching_used)
208+
advise_use_upstream();
209+
else
210+
advise_checkout_pull_push();
211+
break;
162212
}
163213

164214
return 1;

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ enum push_default_type {
625625
PUSH_DEFAULT_NOTHING = 0,
626626
PUSH_DEFAULT_MATCHING,
627627
PUSH_DEFAULT_UPSTREAM,
628-
PUSH_DEFAULT_CURRENT
628+
PUSH_DEFAULT_CURRENT,
629+
PUSH_DEFAULT_UNSPECIFIED
629630
};
630631

631632
extern enum branch_track git_branch_track;

environment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
5252
unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
5353
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
5454
enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
55-
enum push_default_type push_default = PUSH_DEFAULT_MATCHING;
55+
enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
5656
#ifndef OBJECT_CREATION_MODE
5757
#define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
5858
#endif

t/lib-httpd.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,6 @@ test_http_push_nonff() {
160160
'
161161

162162
test_expect_success 'non-fast-forward push shows help message' '
163-
test_i18ngrep "To prevent you from losing history, non-fast-forward updates were rejected" output
163+
test_i18ngrep "Updates were rejected because" output
164164
'
165165
}

t/t5541-http-push.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ test_expect_success 'push fails for non-fast-forward refs unmatched by remote he
168168
'
169169

170170
test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
171-
test_i18ngrep "To prevent you from losing history, non-fast-forward updates were rejected" \
171+
test_i18ngrep "Updates were rejected because" \
172172
output
173173
'
174174

transport.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ void transport_print_push_status(const char *dest, struct ref *refs,
721721
{
722722
struct ref *ref;
723723
int n = 0;
724+
unsigned char head_sha1[20];
725+
char *head;
726+
727+
head = resolve_refdup("HEAD", head_sha1, 1, NULL);
724728

725729
if (verbose) {
726730
for (ref = refs; ref; ref = ref->next)
@@ -738,8 +742,13 @@ void transport_print_push_status(const char *dest, struct ref *refs,
738742
ref->status != REF_STATUS_UPTODATE &&
739743
ref->status != REF_STATUS_OK)
740744
n += print_one_push_status(ref, dest, n, porcelain);
741-
if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
742-
*nonfastforward = 1;
745+
if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD &&
746+
*nonfastforward != NON_FF_HEAD) {
747+
if (!strcmp(head, ref->name))
748+
*nonfastforward = NON_FF_HEAD;
749+
else
750+
*nonfastforward = NON_FF_OTHER;
751+
}
743752
}
744753
}
745754

transport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ int transport_set_option(struct transport *transport, const char *name,
138138
void transport_set_verbosity(struct transport *transport, int verbosity,
139139
int force_progress);
140140

141+
#define NON_FF_HEAD 1
142+
#define NON_FF_OTHER 2
141143
int transport_push(struct transport *connection,
142144
int refspec_nr, const char **refspec, int flags,
143145
int * nonfastforward);

0 commit comments

Comments
 (0)