advice: improve hint for diverging branches.
PATCH 1/1 advice: improve hint for diverging branches.
From: Konstantin Pereiaslov <[email protected]>
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"
---
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(-)
Documentation/config/advice.txt +2 -1
@@ -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
advice.c +23 -4
@@ -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;
advice.h +2 -1
@@ -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);
builtin/merge.c +1 -1
@@ -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,
builtin/pull.c +18 -2
@@ -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
Signature · 1 line
gitgitgadget
"Konstantin Pereiaslov via GitGitGadget" <[email protected]>
Quoted 7 lines · writes:
From: Konstantin Pereiaslov <[email protected]>
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.
cf. Documentation/SubmittingPatches:[[describe-changes]]
Quoted 4 lines
The resulting tooltip looks like this for pull:
hint: Diverging branches can't be fast-forwarded.
Consider the following options:
We do not give "hint:" prefix to this line???
Quoted 6 lines
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
Hmph, "apply" -> "replay" perhaps?
Quoted 1 line
hint: To discard your local changes and apply the remote changes:
Here "apply" is definitely a misnomer. Nothing is applied; you just discard your work and adopt (or "accept") the state of the remote as a whole.
Quoted 3 lines
hint: git reset --hard refs/remotes/upstream/branch-name
hint:
hint: Disable this message with "git config advice.diverging false"
OK.
Overall, "... looks like this" should be shown a bit indented so that the example stands out from the text that explains the example.
Quoted 3 lines
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.
Sorry, but I do not quite understand what you want to say here.
builtin/pull.c int cmd_pull(int argc, const char **argv, const char *prefix) · line 1130
Quoted 8 lines
builtin/pull.c +2
-2@@ -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);
In this codebase, asterisk sticks to the variable/function identifier, not types.
But more importantly, what guarantees your recomputation using '*refspecs' here will match the result of the logic that computed 'divergent', which certainly would have already known what commit we tried to fast-forward our branch to, and where that commit came from? We shouldn't be computing the same thing twice, and in different ways; that is a sure way to introduce inconsistent results.
Quoted 7 lines
builtin/pull.c +2
-0+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
Thanks.
Thank you, all good points. I will work on the wording improvements based on your suggestions.
"Consider the following options:" should be on a new line and get a "hint:" prefix, I will fix that.
As far as the danger to the user, I was talking about the fact that the user doing "git reset --hard" is going to lose any uncommitted work as well as any commits currently in the branch.
Quoted 7 lines
"But more importantly, what guarantees your recomputation using '*refspecs' here will match the result of the logic that computed 'divergent', which certainly would have already known what commit we tried to fast-forward our branch to, and where that commit came from? We shouldn't be computing the same thing twice, and in different ways; that is a sure way to introduce inconsistent results.
This makes sens, I did it this way because I wanted to get a remote and branch name, not just hash id and it was difficult to get this information. I will try to rework it so that it shares the code with the logic that determines the divergence status.
Any tips on what's the best way to do that would be highly appreciated.
Quoted 71 lines · Junio C Hamano
"Konstantin Pereiaslov via GitGitGadget" <[email protected]>
Quoted 7 lines · writes:
From: Konstantin Pereiaslov <[email protected]>
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.
cf. Documentation/SubmittingPatches:[[describe-changes]]
Quoted 4 lines
The resulting tooltip looks like this for pull:
hint: Diverging branches can't be fast-forwarded.
Consider the following options:We do not give "hint:" prefix to this line???
Quoted 6 lines
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 rebaseHmph, "apply" -> "replay" perhaps?
Quoted 1 line
hint: To discard your local changes and apply the remote changes:
Here "apply" is definitely a misnomer. Nothing is applied; you just discard your work and adopt (or "accept") the state of the remote as a whole.
Quoted 3 lines
hint: git reset --hard refs/remotes/upstream/branch-name
hint:
hint: Disable this message with "git config advice.diverging false"OK.
Overall, "... looks like this" should be shown a bit indented so that the example stands out from the text that explains the example.
Quoted 3 lines
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.
Sorry, but I do not quite understand what you want to say here.
builtin/pull.c int cmd_pull(int argc, const char **argv, const char *prefix) · line 1130
Quoted 8 lines
builtin/pull.c +2
-2@@ -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);In this codebase, asterisk sticks to the variable/function identifier, not types.
But more importantly, what guarantees your recomputation using '*refspecs' here will match the result of the logic that computed 'divergent', which certainly would have already known what commit we tried to fast-forward our branch to, and where that commit came from? We shouldn't be computing the same thing twice, and in different ways; that is a sure way to introduce inconsistent results.
Quoted 7 lines
builtin/pull.c +2
-0+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:
d814540bb75bbd2257f9a6bf59661a84fe8cf3cfThanks.
Thank you, all good points. I will work on the wording improvements based on your suggestions.
"Consider the following options:" should be on a new line and get a "hint:" prefix, I will fix that.
As far as the danger to the user, I was talking about the fact that the user doing "git reset --hard" is going to lose any uncommitted work as well as any commits currently in the branch.
Quoted 7 lines
"But more importantly, what guarantees your recomputation using '*refspecs' here will match the result of the logic that computed 'divergent', which certainly would have already known what commit we tried to fast-forward our branch to, and where that commit came from? We shouldn't be computing the same thing twice, and in different ways; that is a sure way to introduce inconsistent results.
This makes sens, I did it this way because I wanted to get a remote and branch name, not just hash id and it was difficult to get this information. I will try to rework it so that it shares the code with the logic that determines the divergence status.
Any tips on what's the best way to do that would be highly appreciated.
Quoted 72 lines · Junio C Hamano
"Konstantin Pereiaslov via GitGitGadget" <[email protected]>
Quoted 7 lines · writes:
From: Konstantin Pereiaslov <[email protected]>
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.
cf. Documentation/SubmittingPatches:[[describe-changes]]
Quoted 4 lines
The resulting tooltip looks like this for pull:
hint: Diverging branches can't be fast-forwarded.
Consider the following options:We do not give "hint:" prefix to this line???
Quoted 6 lines
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 rebaseHmph, "apply" -> "replay" perhaps?
Quoted 1 line
hint: To discard your local changes and apply the remote changes:
Here "apply" is definitely a misnomer. Nothing is applied; you just discard your work and adopt (or "accept") the state of the remote as a whole.
Quoted 3 lines
hint: git reset --hard refs/remotes/upstream/branch-name
hint:
hint: Disable this message with "git config advice.diverging false"OK.
Overall, "... looks like this" should be shown a bit indented so that the example stands out from the text that explains the example.
Quoted 3 lines
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.
Sorry, but I do not quite understand what you want to say here.
builtin/pull.c int cmd_pull(int argc, const char **argv, const char *prefix) · line 1130
Quoted 8 lines
builtin/pull.c +2
-2@@ -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);In this codebase, asterisk sticks to the variable/function identifier, not types.
But more importantly, what guarantees your recomputation using '*refspecs' here will match the result of the logic that computed 'divergent', which certainly would have already known what commit we tried to fast-forward our branch to, and where that commit came from? We shouldn't be computing the same thing twice, and in different ways; that is a sure way to introduce inconsistent results.
Quoted 7 lines
builtin/pull.c +2
-0+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:
d814540bb75bbd2257f9a6bf59661a84fe8cf3cfThanks.