From af145e8ea516c35c82db4bc6f478532bc7112b4a Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 13 Aug 2023 15:41:53 -0700 Subject: [PATCH] cli: include hint when push is not fast-forward --- cli/src/commands/git.rs | 5 +++++ cli/tests/test_git_push.rs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index c336ce4d5..7d443aaaa 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -975,6 +975,11 @@ fn cmd_git_push( }) .map_err(|err| match err { GitPushError::InternalGitError(err) => map_git_error(err), + GitPushError::NotFastForward => user_error_with_hint( + "The push conflicts with changes made on the remote (it is not fast-forwardable).", + "Try fetching from the remote, then make the branch point to where you want it to be, \ + and push again.", + ), _ => user_error(err.to_string()), })?; git::import_refs(tx.mut_repo(), &git_repo, &command.settings().git_settings())?; diff --git a/cli/tests/test_git_push.rs b/cli/tests/test_git_push.rs index 57dc5e00a..c3ab6ccbf 100644 --- a/cli/tests/test_git_push.rs +++ b/cli/tests/test_git_push.rs @@ -14,7 +14,7 @@ use std::path::PathBuf; -use crate::common::TestEnvironment; +use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment}; pub mod common; @@ -169,6 +169,37 @@ fn test_git_push_current_branch_unchanged() { "###); } +#[test] +fn test_git_push_not_fast_forward() { + let (test_env, workspace_root) = set_up(); + + // Move branch1 forward on the remote + let origin_path = test_env.env_root().join("origin"); + test_env.jj_cmd_success(&origin_path, &["new", "branch1", "-m=remote"]); + std::fs::write(origin_path.join("remote"), "remote").unwrap(); + test_env.jj_cmd_success(&origin_path, &["branch", "set", "branch1"]); + test_env.jj_cmd_success(&origin_path, &["git", "export"]); + + // Move branch1 forward to another commit locally + test_env.jj_cmd_success(&workspace_root, &["new", "branch1", "-m=local"]); + std::fs::write(workspace_root.join("local"), "local").unwrap(); + test_env.jj_cmd_success(&workspace_root, &["branch", "set", "branch1"]); + + // Pushing should fail + let assert = test_env + .jj_cmd(&workspace_root, &["git", "push"]) + .assert() + .code(1); + insta::assert_snapshot!(get_stdout_string(&assert), @r###" + Branch changes to push to origin: + Move branch branch1 from 45a3aa29e907 to c35839cb8e8c + "###); + insta::assert_snapshot!(get_stderr_string(&assert), @r###" + Error: The push conflicts with changes made on the remote (it is not fast-forwardable). + Hint: Try fetching from the remote, then make the branch point to where you want it to be, and push again. + "###); +} + #[test] fn test_git_push_multiple() { let (test_env, workspace_root) = set_up();