git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christopher Tiwald <christiwald@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>,
	Junio C Hamano <gitster@pobox.com>,
	git@vger.kernel.org
Subject: [RFC/PATCH] Give better 'pull' advice when pushing non-ff updates to current branch
Date: Tue, 10 Apr 2012 22:08:16 -0400	[thread overview]
Message-ID: <20120411020816.GU376@gmail.com> (raw)
In-Reply-To: <20120406071520.GD25301@sigill.intra.peff.net>

On Fri, Apr 06, 2012 at 03:15:20AM -0400, Jeff King wrote:
> So shouldn't the advice for a non-fast-forward push be:
> 
>    if $source_ref is currently checked out
>            advise "git checkout $source_ref, and then..."
>    fi
>    if $dest_remote == branch.$source_ref.remote &&
>       $dest_ref == branch.$source_ref.merge
>            advise "git pull"
>    else
>            advise "git pull $dest_remote $dest_ref"
>    fi
> 
> That handles only one ref, of course. If you get multiple non-ff
> failures, I'm not sure what we should advise.

Hmmm. Maybe something like this? Note to reviewers: This is necessarily
based on ct/advise-push-default.

Assuming this logic is sound and the patch is a reasonable change, I'm not
wedded to "pushNonFFCurrentUntracked" and "pushNonFFCurrentTracked". I'm
concerned both config options are a bit too long. Is there a better, more
concise way to specify those config options?

---- >8 ----
Suppose a user configured a local branch to track an upstream branch by
a different name or didn't set an upstream branch at all. In these
cases, issuing 'git pull' without specifying a remote repository or
refspec can be dangerous. In the first case, 'git pull --rebase' could
rewrite published history. In the second, 'git pull' without argument
will fail.

Modify 'git push's non-fast-forward advice to account for these cases.
Instruct users who push a non-fast-forward update to their current
branch to 'git pull <repository> <refspec>' when the branch is untracked
or tracks to a different repo or refspec then the one they specified.
Otherwise, instruct users to 'git pull'. Make both types of advice
configurable, so that users who disable one won't disable the other on
accident. Finally, offer users who configure a branch for octopus
merges, i.e. where 'branch->merge_nr > 1', the simple 'git pull' advice.

Signed-off-by: Christopher Tiwald <christiwald@gmail.com>
---
 Documentation/config.txt |    9 +++++++--
 advice.c                 |    6 ++++--
 advice.h                 |    3 ++-
 builtin/push.c           |   48 +++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index fb386ab..fd72120 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -141,9 +141,14 @@ advice.*::
 		Set this variable to 'false' if you want to disable
 		'pushNonFFCurrent', 'pushNonFFDefault', and
 		'pushNonFFMatching' simultaneously.
-	pushNonFFCurrent::
+	pushNonFFCurrentUntracked::
 		Advice shown when linkgit:git-push[1] fails due to a
-		non-fast-forward update to the current branch.
+		non-fast-forward update to the current branch and that
+		branch doesn't match the tracked remote and refspec.
+	pushNonFFCurrentTracked::
+		Advice shown when linkgit:git-push[1] fails due to a
+		non-fast-forward update to the current branch and that
+		branch matches the tracked remote and refspec.
 	pushNonFFDefault::
 		Advice to set 'push.default' to 'upstream' or 'current'
 		when you ran linkgit:git-push[1] and pushed 'matching
diff --git a/advice.c b/advice.c
index a492eea..828a41b 100644
--- a/advice.c
+++ b/advice.c
@@ -1,7 +1,8 @@
 #include "cache.h"
 
 int advice_push_nonfastforward = 1;
-int advice_push_non_ff_current = 1;
+int advice_push_non_ff_current_untracked = 1;
+int advice_push_non_ff_current_tracked = 1;
 int advice_push_non_ff_default = 1;
 int advice_push_non_ff_matching = 1;
 int advice_status_hints = 1;
@@ -15,7 +16,8 @@ static struct {
 	int *preference;
 } advice_config[] = {
 	{ "pushnonfastforward", &advice_push_nonfastforward },
-	{ "pushnonffcurrent", &advice_push_non_ff_current },
+	{ "pushnonffcurrentuntracked", &advice_push_non_ff_current_untracked },
+	{ "pushnonffcurrenttracked", &advice_push_non_ff_current_tracked },
 	{ "pushnonffdefault", &advice_push_non_ff_default },
 	{ "pushnonffmatching", &advice_push_non_ff_matching },
 	{ "statushints", &advice_status_hints },
diff --git a/advice.h b/advice.h
index f3cdbbf..c18809f 100644
--- a/advice.h
+++ b/advice.h
@@ -4,7 +4,8 @@
 #include "git-compat-util.h"
 
 extern int advice_push_nonfastforward;
-extern int advice_push_non_ff_current;
+extern int advice_push_non_ff_current_untracked;
+extern int advice_push_non_ff_current_tracked;
 extern int advice_push_non_ff_default;
 extern int advice_push_non_ff_matching;
 extern int advice_status_hints;
diff --git a/builtin/push.c b/builtin/push.c
index 8a14e4b..0671d27 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -118,12 +118,18 @@ static void setup_default_push_refspecs(struct remote *remote)
 	}
 }
 
-static const char message_advice_pull_before_push[] =
+static const char message_advice_tracked_pull_before_push[] =
 	N_("Updates were rejected because the tip of your current branch is behind\n"
 	   "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
 	   "before pushing again.\n"
 	   "See the 'Note about fast-forwards' in 'git push --help' for details.");
 
+static const char message_advice_untracked_pull_before_push[] =
+	N_("Updates were rejected because the tip of your current branch is behind\n"
+	   "its remote counterpart. Merge the remote changes to your local branch\n"
+	   "(e.g. 'git pull <repository> <refspec>') before pushing again.\n"
+	   "See the 'Note about fast-forwards' in 'git push --help' for details.");
+
 static const char message_advice_use_upstream[] =
 	N_("Updates were rejected because a pushed branch tip is behind its remote\n"
 	   "counterpart. If you did not intend to push that branch, you may want to\n"
@@ -136,11 +142,20 @@ static const char message_advice_checkout_pull_push[] =
 	   "(e.g. 'git pull') before pushing again.\n"
 	   "See the 'Note about fast-forwards' in 'git push --help' for details.");
 
-static void advise_pull_before_push(void)
+static void advise_tracked_pull_before_push(void)
+{
+	if (!advice_push_non_ff_current_tracked ||
+	    !advice_push_nonfastforward)
+		return;
+	advise(_(message_advice_tracked_pull_before_push));
+}
+
+static void advise_untracked_pull_before_push(void)
 {
-	if (!advice_push_non_ff_current || !advice_push_nonfastforward)
+	if (!advice_push_non_ff_current_untracked ||
+	    !advice_push_nonfastforward)
 		return;
-	advise(_(message_advice_pull_before_push));
+	advise(_(message_advice_untracked_pull_before_push));
 }
 
 static void advise_use_upstream(void)
@@ -161,6 +176,16 @@ static int push_with_options(struct transport *transport, int flags)
 {
 	int err;
 	int nonfastforward;
+	struct branch *branch;
+	struct strbuf buf = STRBUF_INIT;
+
+	branch = branch_get(NULL);
+
+	if (branch) {
+		strbuf_addstr(&buf, transport->remote->name);
+		strbuf_addstr(&buf, "/");
+		strbuf_addstr(&buf, branch->name);
+	}
 
 	transport_set_verbosity(transport, verbosity, progress);
 
@@ -185,7 +210,18 @@ static int push_with_options(struct transport *transport, int flags)
 	default:
 		break;
 	case NON_FF_HEAD:
-		advise_pull_before_push();
+		/* Branches configured for octopus merges should advise
+		 * just 'git pull' */
+		if (branch->remote_name &&
+		    branch->merge &&
+		    branch->merge_nr == 1 &&
+		    !strcmp(transport->remote->name, branch->remote_name) &&
+		    !strcmp(strbuf_detach(&buf, NULL),
+			    prettify_refname(branch->merge[0]->dst))) {
+			advise_tracked_pull_before_push();
+		}
+		else
+			advise_untracked_pull_before_push();
 		break;
 	case NON_FF_OTHER:
 		if (default_matching_used)
@@ -195,6 +231,8 @@ static int push_with_options(struct transport *transport, int flags)
 		break;
 	}
 
+	strbuf_release(&buf);
+
 	return 1;
 }
 
-- 
1.7.10.4.g2c970.dirty

  parent reply	other threads:[~2012-04-11  2:08 UTC|newest]

Thread overview: 152+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-28 19:47 [ANNOUNCE] Git 1.7.10-rc3 Junio C Hamano
2012-03-29  9:52 ` Jeff King
2012-03-29 21:22   ` Junio C Hamano
2012-03-29 22:11     ` Jeff King
2012-03-30  1:54       ` Junio C Hamano
2012-03-30  7:13         ` push.default: current vs upstream Jeff King
2012-03-30 20:25           ` Junio C Hamano
2012-03-30 21:01             ` Jeff King
2012-03-30 21:28               ` Junio C Hamano
2012-03-30 21:53                 ` Jeff King
2012-03-30 22:15                   ` Junio C Hamano
2012-03-30 22:20                     ` Jeff King
2012-03-30 23:12                       ` Junio C Hamano
2012-03-31 22:49               ` Nathan Gray
2012-03-31 23:48                 ` Seth Robertson
2012-04-01  2:22                   ` Junio C Hamano
2012-04-01  5:58                   ` Nathan Gray
2012-04-02  7:40             ` Matthieu Moy
2012-04-02 16:48               ` Junio C Hamano
2012-04-02 17:20                 ` Matthieu Moy
2012-04-02 18:40                   ` Junio C Hamano
2012-04-02 18:58                     ` Matthieu Moy
2012-04-02 19:47                       ` Junio C Hamano
2012-04-02 20:40                         ` Matthieu Moy
2012-04-02 20:50                           ` Junio C Hamano
2012-04-02 21:02                           ` demerphq
2012-04-02 21:16                             ` Matthieu Moy
2012-04-04  7:57                               ` demerphq
2012-04-05 13:13               ` Jeff King
2012-04-05 16:46                 ` Matthieu Moy
2012-04-06  7:15                   ` Jeff King
2012-04-06  7:44                     ` Matthieu Moy
2012-04-06  8:00                       ` Jeff King
2012-04-06 17:41                         ` Junio C Hamano
2012-04-06 18:01                         ` Jehan Bing
2012-04-07  7:49                         ` Michael Haggerty
2012-04-07  7:51                           ` Jeff King
2012-04-07  8:40                             ` Andrew Sayers
2012-04-12  7:11                               ` Jeff King
2012-04-12 15:04                                 ` Junio C Hamano
2012-04-12 21:16                                 ` Andrew Sayers
2012-04-12 21:33                                   ` Junio C Hamano
2012-04-12 22:11                                     ` Jeff King
2012-04-12 22:59                                       ` Philip Oakley
2012-04-13 19:31                                         ` Junio C Hamano
2012-04-17 20:13                                     ` Andrew Sayers
2012-04-08  4:43                             ` Junio C Hamano
2012-04-11 16:17                               ` Matthieu Moy
2012-04-11 16:44                                 ` Junio C Hamano
2012-04-12  7:55                                 ` Jeff King
2012-04-12  8:09                                   ` Matthieu Moy
2012-04-12  8:14                                     ` Jeff King
2012-04-12  8:59                                       ` Matthieu Moy
2012-04-12 15:56                                         ` Junio C Hamano
2012-04-19 16:06                                           ` Junio C Hamano
2012-04-19 20:38                                             ` Matthieu Moy
2012-04-19 21:02                                               ` Junio C Hamano
2012-04-19 22:57                                               ` [RFC/PATCH 0/3] push.default upcomming change Matthieu Moy
2012-04-19 22:57                                                 ` [PATCH 1/3] push: introduce new push.default mode "simple" Matthieu Moy
2012-04-19 23:46                                                   ` Jeff King
2012-04-20 14:52                                                     ` Matthieu Moy
2012-04-20 14:59                                                       ` [PATCH 0/4 v2] push.default upcomming change Matthieu Moy
2012-04-20 14:59                                                         ` [PATCH 1/4] Documentation: explain push.default option a bit more Matthieu Moy
2012-04-20 20:13                                                           ` Jeff King
2012-04-20 21:28                                                             ` Junio C Hamano
2012-04-21  3:51                                                               ` Michael Haggerty
2012-04-21  4:08                                                                 ` Junio C Hamano
2012-04-21  5:01                                                                   ` Michael Haggerty
2012-04-21  5:42                                                                     ` Junio C Hamano
2012-04-20 14:59                                                         ` [PATCH 2/4] push: introduce new push.default mode "simple" Matthieu Moy
2012-04-20 20:33                                                           ` Jeff King
2012-04-22 16:24                                                             ` Zbigniew Jędrzejewski-Szmek
2012-04-20 21:42                                                           ` Junio C Hamano
2012-04-23  8:38                                                             ` Matthieu Moy
2012-04-20 14:59                                                         ` [PATCH 3/4] t5570: use explicit push refspec Matthieu Moy
2012-04-20 14:59                                                         ` [PATCH 4/4] push: start warning upcoming default change for push.default Matthieu Moy
2012-04-20 20:35                                                         ` [PATCH 0/4 v2] push.default upcomming change Jeff King
2012-04-22 11:05                                                           ` Matthieu Moy
2012-04-23  2:50                                                             ` Junio C Hamano
2012-04-23  8:37                                                         ` [PATCH 0/7 v3] " Matthieu Moy
2012-04-23  8:37                                                           ` [PATCH 1/7] Documentation: explain push.default option a bit more Matthieu Moy
2012-04-23 15:20                                                             ` Junio C Hamano
2012-04-23 19:00                                                             ` Philip Oakley
2012-04-23 19:11                                                               ` Junio C Hamano
2012-04-23 21:01                                                                 ` Philip Oakley
2012-04-23  8:37                                                           ` [PATCH 2/7] Undocument deprecated alias 'push.default=tracking' Matthieu Moy
2012-04-23 15:21                                                             ` Junio C Hamano
2013-01-31 17:10                                                             ` Ævar Arnfjörð Bjarmason
2013-01-31 17:35                                                               ` Junio C Hamano
2013-01-31 19:07                                                                 ` Jonathan Nieder
2013-01-31 19:11                                                                   ` Jonathan Nieder
2013-01-31 19:58                                                                     ` Junio C Hamano
2013-01-31 19:41                                                                   ` Junio C Hamano
2013-01-31 19:57                                                                     ` Jonathan Nieder
2013-01-31 20:01                                                                       ` Junio C Hamano
2013-01-31 20:11                                                                         ` Jonathan Nieder
2013-01-31 20:42                                                                           ` Junio C Hamano
2013-01-31 20:44                                                                             ` Junio C Hamano
2013-01-31 20:04                                                                     ` Jonathan Nieder
2013-01-31 20:08                                                                       ` Matthieu Moy
2013-01-31 20:21                                                                       ` Junio C Hamano
2013-01-31 20:50                                                                         ` Junio C Hamano
2013-01-31 21:00                                                                           ` Jonathan Nieder
2013-02-01  1:15                                                                             ` Junio C Hamano
2013-01-31 21:08                                                                         ` Jonathan Nieder
2013-01-31 20:41                                                                       ` Philip Oakley
2012-04-23  8:38                                                           ` [PATCH 3/7] t5528-push-default.sh: add helper functions Matthieu Moy
2012-04-23 15:36                                                             ` Junio C Hamano
2012-04-23 16:02                                                               ` Matthieu Moy
2012-04-23 16:16                                                                 ` Junio C Hamano
2012-04-23 16:20                                                                   ` Matthieu Moy
2012-04-23 16:57                                                                     ` Matthieu Moy
2012-04-23 17:09                                                                       ` Junio C Hamano
2012-04-23 16:42                                                                   ` Junio C Hamano
2012-04-23 16:45                                                                     ` [PATCH 2/3] fixup! " Junio C Hamano
2012-04-23 16:48                                                                     ` [PATCH 3/3] push: suggested updates to push configuration documentation Junio C Hamano
2012-04-23  8:38                                                           ` [PATCH 4/7] push: introduce new push.default mode "simple" Matthieu Moy
2012-04-23 10:32                                                             ` Michael Haggerty
2012-04-23 11:20                                                               ` Matthieu Moy
2012-04-23 15:52                                                             ` Junio C Hamano
2012-04-23 16:09                                                               ` Matthieu Moy
2012-04-23  8:38                                                           ` [PATCH 5/7] t5570: use explicit push refspec Matthieu Moy
2012-04-23  8:38                                                           ` [PATCH 6/7] push: document the future default change for push.default (matching -> simple) Matthieu Moy
2012-04-23  8:38                                                           ` [PATCH 7/7] push: start warning upcoming default change for push.default Matthieu Moy
2012-04-24  7:49                                                           ` [PATCH 0/7 v4] push.default upcomming change Matthieu Moy
2012-04-24  7:50                                                             ` [PATCH 1/7] Documentation: explain push.default option a bit more Matthieu Moy
2012-04-24  7:50                                                             ` [PATCH 2/7] Undocument deprecated alias 'push.default=tracking' Matthieu Moy
2012-04-24  7:50                                                             ` [PATCH 3/7] t5528-push-default.sh: add helper functions Matthieu Moy
2012-04-24  7:50                                                             ` [PATCH 4/7] push: introduce new push.default mode "simple" Matthieu Moy
2012-04-25  1:53                                                               ` Junio C Hamano
2012-04-25 11:01                                                                 ` Matthieu Moy
2012-04-24  7:50                                                             ` [PATCH 5/7] t5570: use explicit push refspec Matthieu Moy
2012-04-24  7:50                                                             ` [PATCH 6/7] push: document the future default change for push.default (matching -> simple) Matthieu Moy
2012-04-24  7:50                                                             ` [PATCH 7/7] push: start warning upcoming default change for push.default Matthieu Moy
2012-04-24 19:28                                                             ` [PATCH 0/7 v4] push.default upcomming change Junio C Hamano
2012-04-19 22:57                                                 ` [PATCH 2/3] t5570: use explicit push refspec Matthieu Moy
2012-04-19 22:57                                                 ` [PATCH 3/3] push: start warning upcoming default change for push.default Matthieu Moy
2012-04-26  5:44                                                   ` [PATCH] t5541: warning message is given even with --quiet Junio C Hamano
2012-04-26  6:31                                                     ` Matthieu Moy
2012-04-11  2:08                     ` Christopher Tiwald [this message]
2012-04-06 11:38                   ` push.default: current vs upstream Dmitry Potapov
2012-04-06 13:36                     ` demerphq
2012-04-06 18:03                       ` Dmitry Potapov
2012-04-06 18:48                         ` demerphq
2012-04-06 19:38                           ` Dmitry Potapov
2012-03-30 23:07           ` Junio C Hamano
2012-04-03 20:59             ` Jeff King
2012-04-03 21:04               ` Jeff King
2012-04-03 22:29               ` Junio C Hamano
2012-04-03 23:23                 ` Junio C Hamano
2012-04-05 12:45                 ` Jeff King
2012-04-08 12:52           ` Felipe Contreras

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120411020816.GU376@gmail.com \
    --to=christiwald@gmail.com \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).