Message ID | pull.1570.git.git.1693861162353.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | advice: improve hint for diverging branches. | expand |
"Konstantin Pereiaslov via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Konstantin Pereiaslov <perk11@perk11.info> > > 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]] > 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??? > 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? > 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. > 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. > 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. > @@ -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. > + 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. > "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. On 9/5/23 18:20, Junio C Hamano wrote: > "Konstantin Pereiaslov via GitGitGadget" <gitgitgadget@gmail.com> > writes: > >> From: Konstantin Pereiaslov <perk11@perk11.info> >> >> 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]] > >> 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??? > >> 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? > >> 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. > >> 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. > >> 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. > >> @@ -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. > >> + 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. > "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. On 9/5/23 18:20, Junio C Hamano wrote: > "Konstantin Pereiaslov via GitGitGadget" <gitgitgadget@gmail.com> > writes: > >> From: Konstantin Pereiaslov <perk11@perk11.info> >> >> 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]] > >> 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??? > >> 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? > >> 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. > >> 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. > >> 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. > >> @@ -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. > >> + 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. >
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. */