From b6f3da51322345a19c259b9359065aa54cda74ff Mon Sep 17 00:00:00 2001 From: Alex Henrie Date: Wed, 12 Jul 2023 22:41:13 -0600 Subject: [PATCH 1/3] wt-status: don't show divergence advice when committing When the user is in the middle of making a commit, they are not yet at the point where they are ready to think about integrating their local branch with the corresponding remote branch or force-pushing over the remote branch. Don't include advice on how to deal with divergent branches in the commit template, to avoid giving the impression that the divergence needs to be dealt with immediately. Similar advice will be printed when it is most relevant, that is, if the user does try to push without first reconciling the two branches. Signed-off-by: Alex Henrie Signed-off-by: Junio C Hamano --- builtin/checkout.c | 2 +- remote.c | 6 ++++-- remote.h | 3 ++- t/t7508-status.sh | 10 ++++------ wt-status.c | 3 ++- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index fd6ee8c272..c278c2169d 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -916,7 +916,7 @@ static void report_tracking(struct branch_info *new_branch_info) struct strbuf sb = STRBUF_INIT; struct branch *branch = branch_get(new_branch_info->name); - if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL)) + if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL, 1)) return; fputs(sb.buf, stdout); strbuf_release(&sb); diff --git a/remote.c b/remote.c index a81f2e2f17..d79aae0d76 100644 --- a/remote.c +++ b/remote.c @@ -2258,7 +2258,8 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, * Return true when there is anything to report, otherwise false. */ int format_tracking_info(struct branch *branch, struct strbuf *sb, - enum ahead_behind_flags abf) + enum ahead_behind_flags abf, + int show_divergence_advice) { int ours, theirs, sti; const char *full_base; @@ -2321,7 +2322,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb, "respectively.\n", ours + theirs), base, ours, theirs); - if (advice_enabled(ADVICE_STATUS_HINTS)) + if (show_divergence_advice && + advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addstr(sb, _(" (use \"git pull\" to merge the remote branch into yours)\n")); } diff --git a/remote.h b/remote.h index 929c7c676d..cdc8b1db42 100644 --- a/remote.h +++ b/remote.h @@ -380,7 +380,8 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, const char **upstream_name, int for_push, enum ahead_behind_flags abf); int format_tracking_info(struct branch *branch, struct strbuf *sb, - enum ahead_behind_flags abf); + enum ahead_behind_flags abf, + int show_divergence_advice); struct ref *get_local_heads(void); /* diff --git a/t/t7508-status.sh b/t/t7508-status.sh index 36567708f5..845af287d7 100755 --- a/t/t7508-status.sh +++ b/t/t7508-status.sh @@ -847,7 +847,6 @@ test_expect_success 'dry-run of partial commit excluding new file in index' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -1013,7 +1012,7 @@ test_expect_success 'status -s submodule summary' ' ' test_expect_success 'status submodule summary (clean submodule): commit' ' - cat >expect <expect-status < expect-commit && git commit -m "commit submodule" && git config status.submodulesummary 10 && test_must_fail git commit --dry-run >output && - test_cmp expect output && + test_cmp expect-commit output && git status >output && - test_cmp expect output + test_cmp expect-status output ' cat >expect <..." to unstage) @@ -1558,7 +1557,6 @@ test_expect_success 'git commit --dry-run will show a staged but ignored submodu On branch main Your branch and '\''upstream'\'' have diverged, and have 2 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) Changes to be committed: (use "git restore --staged ..." to unstage) diff --git a/wt-status.c b/wt-status.c index bcd0ef8044..e3e3732ea2 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1186,7 +1186,8 @@ static void wt_longstatus_print_tracking(struct wt_status *s) t_begin = getnanotime(); - if (!format_tracking_info(branch, &sb, s->ahead_behind_flags)) + if (!format_tracking_info(branch, &sb, s->ahead_behind_flags, + !s->commit_template)) return; if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) && From d92304ff5cfdca463e9ecd1345807d0b46d6af33 Mon Sep 17 00:00:00 2001 From: Alex Henrie Date: Wed, 12 Jul 2023 22:41:14 -0600 Subject: [PATCH 2/3] remote: don't imply that integration is always required before pushing In a narrow but common case, the user is the only author of a branch and doesn't mind overwriting the corresponding branch on the remote. This workflow is especially common on GitHub, GitLab, and Gerrit, which keep a permanent record of every version of a branch that is pushed while a pull request is open for that branch. On those platforms, force-pushing is encouraged and is analogous to emailing a new version of a patchset. When giving advice about divergent branches, tell the user about `git pull`, but don't unconditionally instruct the user to do it. A less prescriptive message will help prevent users from thinking that they are required to create an integrated history instead of simply replacing the previous history. Likewise, don't imply that `git pull` is only for merging. Signed-off-by: Alex Henrie Signed-off-by: Junio C Hamano --- remote.c | 2 +- t/t7508-status.sh | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/remote.c b/remote.c index d79aae0d76..71019564d5 100644 --- a/remote.c +++ b/remote.c @@ -2325,7 +2325,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb, if (show_divergence_advice && advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addstr(sb, - _(" (use \"git pull\" to merge the remote branch into yours)\n")); + _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n")); } free(base); return 1; diff --git a/t/t7508-status.sh b/t/t7508-status.sh index 845af287d7..6928fd89f5 100755 --- a/t/t7508-status.sh +++ b/t/t7508-status.sh @@ -92,7 +92,7 @@ test_expect_success 'status --column' ' # On branch main # Your branch and '\''upstream'\'' have diverged, # and have 1 and 2 different commits each, respectively. -# (use "git pull" to merge the remote branch into yours) +# (use "git pull" if you want to integrate the remote branch with yours) # # Changes to be committed: # (use "git restore --staged ..." to unstage) @@ -123,7 +123,7 @@ cat >expect <<\EOF # On branch main # Your branch and 'upstream' have diverged, # and have 1 and 2 different commits each, respectively. -# (use "git pull" to merge the remote branch into yours) +# (use "git pull" if you want to integrate the remote branch with yours) # # Changes to be committed: # (use "git restore --staged ..." to unstage) @@ -270,7 +270,7 @@ test_expect_success 'status with gitignore' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -335,7 +335,7 @@ test_expect_success 'status with gitignore (nothing untracked)' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -405,7 +405,7 @@ test_expect_success 'status -uno' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -467,7 +467,7 @@ test_expect_success 'status -unormal' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -522,7 +522,7 @@ test_expect_success 'status -uall' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -582,7 +582,7 @@ test_expect_success 'status with relative paths' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -650,7 +650,7 @@ test_expect_success TTY 'status with color.ui' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -773,7 +773,7 @@ test_expect_success 'status without relative paths' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -900,7 +900,7 @@ test_expect_success 'status submodule summary is disabled by default' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -957,7 +957,7 @@ test_expect_success 'status submodule summary' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 1 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -1016,7 +1016,7 @@ test_expect_success 'status submodule summary (clean submodule): commit' ' On branch main Your branch and '\''upstream'\'' have diverged, and have 2 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes not staged for commit: (use "git add ..." to update what will be committed) @@ -1116,7 +1116,7 @@ test_expect_success '--ignore-submodules=untracked suppresses submodules with un On branch main Your branch and '\''upstream'\'' have diverged, and have 2 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -1225,7 +1225,7 @@ test_expect_success "--ignore-submodules=untracked doesn't suppress submodules w On branch main Your branch and '\''upstream'\'' have diverged, and have 2 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -1282,7 +1282,7 @@ test_expect_success "--ignore-submodules=untracked doesn't suppress submodule su On branch main Your branch and '\''upstream'\'' have diverged, and have 2 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) @@ -1363,7 +1363,7 @@ cat > expect << EOF ; On branch main ; Your branch and 'upstream' have diverged, ; and have 2 and 2 different commits each, respectively. -; (use "git pull" to merge the remote branch into yours) +; (use "git pull" if you want to integrate the remote branch with yours) ; ; Changes to be committed: ; (use "git restore --staged ..." to unstage) @@ -1411,7 +1411,7 @@ test_expect_success "--ignore-submodules=all suppresses submodule summary" ' On branch main Your branch and '\''upstream'\'' have diverged, and have 2 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes not staged for commit: (use "git add ..." to update what will be committed) @@ -1437,7 +1437,7 @@ test_expect_success '.gitmodules ignore=all suppresses unstaged submodule summar On branch main Your branch and '\''upstream'\'' have diverged, and have 2 and 2 different commits each, respectively. - (use "git pull" to merge the remote branch into yours) + (use "git pull" if you want to integrate the remote branch with yours) Changes to be committed: (use "git restore --staged ..." to unstage) From c577d65158aa78817a59a58649bb381f2daaa88d Mon Sep 17 00:00:00 2001 From: Alex Henrie Date: Wed, 12 Jul 2023 22:41:15 -0600 Subject: [PATCH 3/3] push: don't imply that integration is always required before pushing In a narrow but common case, the user is the only author of a branch and doesn't mind overwriting the corresponding branch on the remote. This workflow is especially common on GitHub, GitLab, and Gerrit, which keep a permanent record of every version of a branch that is pushed while a pull request is open for that branch. On those platforms, force-pushing is encouraged and is analogous to emailing a new version of a patchset. When giving advice about divergent branches, tell the user about `git pull`, but don't unconditionally instruct the user to do it. A less prescriptive message will help prevent users from thinking that they are required to create an integrated history instead of simply replacing the previous history. Also, don't put `git pull` in an awkward parenthetical, because `git pull` can always be used to reconcile branches and is the normal way to do so. Due to the difficulty of knowing which command for force-pushing is best suited to the user's situation, no specific advice is given about force-pushing. Instead, the user is directed to the Git documentation to read about possible ways forward that do not involve integration. Signed-off-by: Alex Henrie Signed-off-by: Junio C Hamano --- builtin/push.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/builtin/push.c b/builtin/push.c index 6f8a8dc711..61a251c50a 100644 --- a/builtin/push.c +++ b/builtin/push.c @@ -301,21 +301,21 @@ static void setup_default_push_refspecs(int *flags, struct remote *remote) static const char message_advice_pull_before_push[] = N_("Updates were rejected because the tip of your current branch is behind\n" - "its remote counterpart. Integrate the remote changes (e.g.\n" - "'git pull ...') before pushing again.\n" + "its remote counterpart. If you want to integrate the remote changes,\n" + "use 'git pull' before pushing again.\n" "See the 'Note about fast-forwards' in 'git push --help' for details."); static const char message_advice_checkout_pull_push[] = N_("Updates were rejected because a pushed branch tip is behind its remote\n" - "counterpart. Check out this branch and integrate the remote changes\n" - "(e.g. 'git pull ...') before pushing again.\n" + "counterpart. If you want to integrate the remote changes, use 'git pull'\n" + "before pushing again.\n" "See the 'Note about fast-forwards' in 'git push --help' for details."); static const char message_advice_ref_fetch_first[] = - N_("Updates were rejected because the remote contains work that you do\n" - "not have locally. This is usually caused by another repository pushing\n" - "to the same ref. You may want to first integrate the remote changes\n" - "(e.g., 'git pull ...') before pushing again.\n" + N_("Updates were rejected because the remote contains work that you do not\n" + "have locally. This is usually caused by another repository pushing to\n" + "the same ref. If you want to integrate the remote changes, use\n" + "'git pull' before pushing again.\n" "See the 'Note about fast-forwards' in 'git push --help' for details."); static const char message_advice_ref_already_exists[] = @@ -327,10 +327,10 @@ static const char message_advice_ref_needs_force[] = "without using the '--force' option.\n"); static const char message_advice_ref_needs_update[] = - N_("Updates were rejected because the tip of the remote-tracking\n" - "branch has been updated since the last checkout. You may want\n" - "to integrate those changes locally (e.g., 'git pull ...')\n" - "before forcing an update.\n"); + N_("Updates were rejected because the tip of the remote-tracking branch has\n" + "been updated since the last checkout. If you want to integrate the\n" + "remote changes, use '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) {