[PATCH] advice: improve hint for diverging branches.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Konstantin Pereiaslov <perk11@xxxxxxxxxxx>

Added a description of what the offered options will do and for pull
also offered a 3rd option during a pull - a hard reset.
This option should be helpful for the new users that accidentally
committed into the wrong branch which is a scenario I saw very
often.

The resulting tooltip looks like this for pull:

hint: Diverging branches can't be fast-forwarded.
Consider the following options:
hint:
hint: To merge remote changes into your branch:
hint:   git merge --no-ff
hint:
hint: To apply your changes on top of remote changes:
hint:   git rebase
hint:
hint: To discard your local changes and apply the remote changes:
hint:   git reset --hard refs/remotes/upstream/branch-name
hint:
hint: Disable this message with "git config advice.diverging false"

There is some danger because it's semi-destructive, but so are
other options offered if user doesn't know the commands to
revert back. Additionally, I think "To discard your local changes"
wording describes the danger clearly enough.

And for merge I improved the wording and added a description of what
the commands do:

hint: Diverging branches can't be fast-forwarded.
hint: Consider the following options:
hint:
hint: To merge changes into your branch:
hint:   git merge --no-ff
hint:
hint: To apply your changes on top:
hint:   git rebase
hint:
hint: Disable this message with "git config advice.diverging false"

Signed-off-by: Konstantin Pereiaslov <perk11@xxxxxxxxxxx>
---
    Improve hint for diverging branches.
    
    I have seen a lot of developers not know what to do when they try to do
    a git pull on a master branch with the intention of updating that branch
    to the latest version, but see an error about branches diverging because
    they accidentally committed their changes to that branch. They then
    spend their time resolving conflicts and still not getting the intended
    result. The suggestion to do a hard reset should be something that helps
    in this situation.
    
    I'm not sure if a new config option needs to be created as technically
    these are two different advice now. I'm also not sure if "refs/remotes"
    part of the refspec is necessary, that is what I found the functions in
    pull.c are returning. I think "upstream/branch-name" should be the same
    thing, but kept it as is ( git reset --hard
    refs/remotes/upstream/branch-name) for now. Please feel free to chime
    in.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1570%2Fperk11%2Fdiverging-advice-improvements-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1570/perk11/diverging-advice-improvements-v1
Pull-Request: https://github.com/git/git/pull/1570

 Documentation/config/advice.txt |  3 ++-
 advice.c                        | 27 +++++++++++++++++++++++----
 advice.h                        |  3 ++-
 builtin/merge.c                 |  2 +-
 builtin/pull.c                  | 20 ++++++++++++++++++--
 5 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/Documentation/config/advice.txt b/Documentation/config/advice.txt
index c548a91e676..f3daa232ace 100644
--- a/Documentation/config/advice.txt
+++ b/Documentation/config/advice.txt
@@ -137,7 +137,8 @@ advice.*::
 		is asked to update index entries outside the current sparse
 		checkout.
 	diverging::
-		Advice shown when a fast-forward is not possible.
+		Advice shown when a fast-forward is not possible during merge
+		or pull operation.
 	worktreeAddOrphan::
 		Advice shown when a user tries to create a worktree from an
 		invalid reference, to instruct how to create a new orphan
diff --git a/advice.c b/advice.c
index 50c79443ba7..8fc4fb19932 100644
--- a/advice.c
+++ b/advice.c
@@ -220,19 +220,38 @@ void NORETURN die_conclude_merge(void)
 	die(_("Exiting because of unfinished merge."));
 }
 
-void NORETURN die_ff_impossible(void)
+void NORETURN die_ff_impossible_during_merge(void)
 {
 	advise_if_enabled(ADVICE_DIVERGING,
-		_("Diverging branches can't be fast-forwarded, you need to either:\n"
+		_("Diverging branches can't be fast-forwarded.\n"
+		"Consider the following options:\n"
 		"\n"
+		"To merge changes into your branch:\n"
 		"\tgit merge --no-ff\n"
 		"\n"
-		"or:\n"
-		"\n"
+		"To apply your changes on top:\n"
 		"\tgit rebase\n"));
 	die(_("Not possible to fast-forward, aborting."));
 }
 
+void NORETURN die_ff_impossible_during_pull(const char *upstream_branch_spec)
+{
+	advise_if_enabled(ADVICE_DIVERGING,
+			  _("Diverging branches can't be fast-forwarded. "
+			    "Consider the following options:\n"
+			    "\n"
+			    "To merge remote changes into your branch:\n"
+			    "\tgit merge --no-ff\n"
+			    "\n"
+			    "To apply your changes on top of remote changes:\n"
+			    "\tgit rebase\n"
+			    "\n"
+			    "To discard your local changes and apply the remote changes:\n"
+			    "\tgit reset --hard %s\n"), upstream_branch_spec);
+	die(_("Not possible to fast-forward, aborting."));
+}
+
+
 void advise_on_updating_sparse_paths(struct string_list *pathspec_list)
 {
 	struct string_list_item *item;
diff --git a/advice.h b/advice.h
index 2affbe14261..f87369a5471 100644
--- a/advice.h
+++ b/advice.h
@@ -71,7 +71,8 @@ void advise_if_enabled(enum advice_type type, const char *advice, ...);
 int error_resolve_conflict(const char *me);
 void NORETURN die_resolve_conflict(const char *me);
 void NORETURN die_conclude_merge(void);
-void NORETURN die_ff_impossible(void);
+void NORETURN die_ff_impossible_during_merge(void);
+void NORETURN die_ff_impossible_during_pull(const char *upstream_branch_spec);
 void advise_on_updating_sparse_paths(struct string_list *pathspec_list);
 void detach_advice(const char *new_name);
 void advise_on_moving_dirty_path(struct string_list *pathspec_list);
diff --git a/builtin/merge.c b/builtin/merge.c
index de68910177f..8358f137f1d 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1675,7 +1675,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	}
 
 	if (fast_forward == FF_ONLY)
-		die_ff_impossible();
+		die_ff_impossible_during_merge();
 
 	if (autostash)
 		create_autostash(the_repository,
diff --git a/builtin/pull.c b/builtin/pull.c
index be2b2c9ebc9..51d30e6f918 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -765,6 +765,20 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	refspec_item_clear(&spec);
 	return merge_branch;
 }
+/**
+ * Returns the branch the pull is performed from.
+ * If remote is NULL or refspec is NULL, configured upstream remote of the
+ * current branch is used.
+ * If refspec is NULL, the current upstream branch is used.
+ */
+static const char *get_pull_branch(const char *remote, const char *refspec)
+{
+	if (refspec == NULL || remote == NULL) {
+		return get_upstream_branch(remote);
+	}
+
+	return get_tracking_branch(remote, refspec);
+}
 
 /**
  * Given the repo and refspecs, sets fork_point to the point at which the
@@ -1112,8 +1126,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
 
 	/* ff-only takes precedence over rebase */
 	if (opt_ff && !strcmp(opt_ff, "--ff-only")) {
-		if (divergent)
-			die_ff_impossible();
+		if (divergent) {
+			const char* pull_branch_spec = get_pull_branch(repo, *refspecs);
+			die_ff_impossible_during_pull(pull_branch_spec);
+		}
 		opt_rebase = REBASE_FALSE;
 	}
 	/* If no action specified and we can't fast forward, then warn. */

base-commit: d814540bb75bbd2257f9a6bf59661a84fe8cf3cf
-- 
gitgitgadget



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux