cli: print relative path to newly initialized repo

It's easier to test relative paths (no need to strip some prefix) and
it seems more user-friendly as well.
This commit is contained in:
Martin von Zweigbergk 2022-03-09 14:12:32 -08:00 committed by Martin von Zweigbergk
parent ea05f8f1e5
commit d475710d29
3 changed files with 18 additions and 22 deletions

View file

@ -68,6 +68,7 @@ use crate::formatter::Formatter;
use crate::graphlog::{AsciiGraphDrawer, Edge};
use crate::template_parser::TemplateParser;
use crate::templater::Template;
use crate::ui;
use crate::ui::{FilePathParseError, Ui};
enum CommandError {
@ -1766,7 +1767,9 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &ArgMatches) -> Result<(
} else {
Workspace::init_local(ui.settings(), wc_path.clone())?;
};
writeln!(ui, "Initialized repo in \"{}\"", wc_path.display())?;
let cwd = std::fs::canonicalize(&ui.cwd()).unwrap();
let relative_wc_path = ui::relative_path(&cwd, &wc_path);
writeln!(ui, "Initialized repo in \"{}\"", relative_wc_path.display())?;
Ok(())
}

View file

@ -178,7 +178,7 @@ pub enum FilePathParseError {
InputNotInRepo(String),
}
fn relative_path(mut from: &Path, to: &Path) -> PathBuf {
pub fn relative_path(mut from: &Path, to: &Path) -> PathBuf {
let mut result = PathBuf::from("");
loop {
if let Ok(suffix) = to.strip_prefix(from) {

View file

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use jujutsu::testutils::TestEnvironment;
use jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test]
fn test_init_git_internal() {
@ -21,12 +21,10 @@ fn test_init_git_internal() {
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"])
.assert()
.success();
let workspace_root = test_env.env_root().join("repo");
assert.stdout(format!(
"Initialized repo in \"{}\"\n",
workspace_root.to_str().unwrap()
));
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in "repo"
"###);
let workspace_root = test_env.env_root().join("repo");
let jj_path = workspace_root.join(".jj");
let repo_path = jj_path.join("repo");
let store_path = repo_path.join("store");
@ -59,12 +57,10 @@ fn test_init_git_external() {
)
.assert()
.success();
let workspace_root = test_env.env_root().join("repo");
assert.stdout(format!(
"Initialized repo in \"{}\"\n",
workspace_root.display()
));
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in "repo"
"###);
let workspace_root = test_env.env_root().join("repo");
let jj_path = workspace_root.join(".jj");
let repo_path = jj_path.join("repo");
let store_path = repo_path.join("store");
@ -88,10 +84,9 @@ fn test_init_git_colocated() {
.jj_cmd(&workspace_root, &["init", "--git-repo", "."])
.assert()
.success();
assert.stdout(format!(
"Initialized repo in \"{}\"\n",
workspace_root.display()
));
// TODO: We should say "." instead of "" here
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in ""
"###);
let jj_path = workspace_root.join(".jj");
let repo_path = jj_path.join("repo");
@ -114,12 +109,10 @@ fn test_init_local() {
.jj_cmd(test_env.env_root(), &["init", "repo"])
.assert()
.success();
let workspace_root = test_env.env_root().join("repo");
assert.stdout(format!(
"Initialized repo in \"{}\"\n",
workspace_root.display()
));
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in "repo"
"###);
let workspace_root = test_env.env_root().join("repo");
let jj_path = workspace_root.join(".jj");
let repo_path = jj_path.join("repo");
let store_path = repo_path.join("store");