1514 lines · 51.9 KB · Rust
RawDownload
// Copyright 2022 The Jujutsu Authors//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// https://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License. use std::path; use indoc::formatdoc;use indoc::indoc;use testutils::TestResult;use testutils::git; use crate::common::CommandOutput;use crate::common::TestEnvironment;use crate::common::TestWorkDir;use crate::common::to_toml_value; fn set_up_non_empty_git_repo(git_repo: &gix::Repository) {    set_up_git_repo_with_file(git_repo, "file");} fn set_up_git_repo_with_file(git_repo: &gix::Repository, filename: &str) {    git::add_commit(        git_repo,        "refs/heads/main",        filename,        b"content",        "message",        &[],    );    git::set_symbolic_reference(git_repo, "HEAD", "refs/heads/main");} #[test]fn test_git_clone() {    let test_env = TestEnvironment::default();    let root_dir = test_env.work_dir("");    test_env.add_config("remotes.origin.auto-track-bookmarks = '*'");    let git_repo_path = test_env.env_root().join("source");    let git_repo = git::init(git_repo_path);     // Clone an empty repo    let output = root_dir.run_jj(["git", "clone", "source", "empty"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/empty"    Nothing changed.    [EOF]    "#);     set_up_non_empty_git_repo(&git_repo);     // Clone with relative source path    let output = root_dir.run_jj(["git", "clone", "source", "clone"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/clone"    bookmark: main@origin [new] tracked    Setting the revset alias `trunk()` to `main@origin`    Working copy  (@) now at: uuqppmxq 3711b3b5 (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    let clone_dir = test_env.work_dir("clone");    assert!(clone_dir.root().join("file").exists());     // Subsequent fetch should just work even if the source path was relative    let output = clone_dir.run_jj(["git", "fetch"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Nothing changed.    [EOF]    ");     // Evolution history should be omitted for the "clone" operation    let output = clone_dir.run_jj(["evolog", "-r..remote_bookmarks()"]);    insta::assert_snapshot!(output, @"    ◆  qomsplrm someone@example.org 1970-01-01 11:00:00 main ebeb70d8       message    [EOF]    ");     // Failed clone should clean up the destination directory    root_dir.create_dir("bad");    let output = root_dir.run_jj(["git", "clone", "bad", "failed"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/failed"    Error: Could not find repository at '$TEST_ENV/bad'    [EOF]    [exit status: 1]    "#);    assert!(!test_env.env_root().join("failed").exists());     // Failed initialization of the workspace should clean up the destination    // directory    let output = root_dir.run_jj([        "git",        "clone",        "--config=fsmonitor.backend=invalid",        "source",        "failed-init",    ]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Internal error: Failed to access the repository    Caused by:    1: Failed to read the tree state settings    2: Invalid type or value for fsmonitor.backend    3: Unknown fsmonitor kind: invalid    [EOF]    [exit status: 255]    ");    assert!(!test_env.env_root().join("failed-init").exists());     // Failed clone shouldn't remove the existing destination directory    let failed_dir = root_dir.create_dir("failed");    let output = root_dir.run_jj(["git", "clone", "bad", "failed"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/failed"    Error: Could not find repository at '$TEST_ENV/bad'    [EOF]    [exit status: 1]    "#);    assert!(failed_dir.root().exists());    assert!(!failed_dir.root().join(".jj").exists());     // Failed clone (if attempted) shouldn't remove the existing workspace    let output = root_dir.run_jj(["git", "clone", "bad", "clone"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");    assert!(clone_dir.root().join(".jj").exists());     // Try cloning into an existing workspace    let output = root_dir.run_jj(["git", "clone", "source", "clone"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");     // Try cloning into an existing file    root_dir.write_file("file", "contents");    let output = root_dir.run_jj(["git", "clone", "source", "file"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");     // Try cloning into non-empty, non-workspace directory    clone_dir.remove_dir_all(".jj");    let output = root_dir.run_jj(["git", "clone", "source", "clone"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");     // Clone into a nested path    let output = root_dir.run_jj(["git", "clone", "source", "nested/path/to/repo"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/nested/path/to/repo"    bookmark: main@origin [new] tracked    Setting the revset alias `trunk()` to `main@origin`    Working copy  (@) now at: msksykpx 5ed2b734 (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);} #[test]fn test_git_clone_bad_source() {    let test_env = TestEnvironment::default();    let root_dir = test_env.work_dir("");     let output = root_dir.run_jj(["git", "clone", "", "dest"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Error: local path "" does not specify a path to a repository    [EOF]    [exit status: 2]    "#);     // Invalid URL unparsable by gitoxide    let output = root_dir.run_jj(["git", "clone", "https://", "dest"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Error: URL "https://" can not be parsed as valid URL    Caused by: Scheme requires host    [EOF]    [exit status: 2]    "#);} #[test]fn test_git_clone_choose_dest_path() {    let test_env = TestEnvironment::default();    let root_dir = test_env.work_dir("");    let git_repo_path = test_env.env_root().join("source.git");    git::init(git_repo_path);     // Windows drive letters prevent this case from triggering    if cfg!(unix) {        let output = root_dir.run_jj(["git", "clone", "/"]);        insta::assert_snapshot!(output, @"        ------- stderr -------        Error: No destination specified and wasn't able to guess it        [EOF]        [exit status: 1]        ");         let output = root_dir.run_jj(["git", "clone", "/.git"]);        insta::assert_snapshot!(output, @"        ------- stderr -------        Error: No destination specified and wasn't able to guess it        [EOF]        [exit status: 1]        ");    }     let output = root_dir.run_jj(["git", "clone", "source.git"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/source"    Nothing changed.    [EOF]    "#);} #[test]fn test_git_clone_colocate() -> TestResult {    let test_env = TestEnvironment::default();    let root_dir = test_env.work_dir("");    test_env.add_config("remotes.origin.auto-track-bookmarks = '*'");    let git_repo_path = test_env.env_root().join("source");    let git_repo = git::init(git_repo_path);     // Clone an empty repo    let output = root_dir.run_jj(["git", "clone", "source", "empty", "--colocate"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/empty"    Nothing changed.    [EOF]    "#);     // git_target path should be relative to the store    let empty_dir = test_env.work_dir("empty");    let git_target_file_contents =        String::from_utf8(empty_dir.read_file(".jj/repo/store/git_target").into())?;    insta::assert_snapshot!(        git_target_file_contents.replace(path::MAIN_SEPARATOR, "/"),        @"../../../.git");     set_up_non_empty_git_repo(&git_repo);     // Clone with relative source path    let output = root_dir.run_jj(["git", "clone", "source", "clone", "--colocate"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/clone"    bookmark: main@origin [new] tracked    Setting the revset alias `trunk()` to `main@origin`    Working copy  (@) now at: uuqppmxq 3711b3b5 (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    let clone_dir = test_env.work_dir("clone");    assert!(clone_dir.root().join("file").exists());    assert!(clone_dir.root().join(".git").exists());     eprintln!(        "{:?}",        git_repo.head().expect("Repo head should be set").name()    );     let jj_git_repo = git::open(clone_dir.root());    assert_eq!(        jj_git_repo            .head_id()            .expect("Clone Repo HEAD should be set.")            .detach(),        git_repo            .head_id()            .expect("Repo HEAD should be set.")            .detach(),    );    // ".jj" directory should be ignored at Git side.    let git_statuses = git::status(&jj_git_repo);    insta::assert_debug_snapshot!(git_statuses, @r#"    [        GitStatus {            path: ".jj/.gitignore",            status: Worktree(                Ignored,            ),        },        GitStatus {            path: ".jj/repo",            status: Worktree(                Ignored,            ),        },        GitStatus {            path: ".jj/working_copy",            status: Worktree(                Ignored,            ),        },    ]    "#);     // The old default bookmark "master" shouldn't exist.    insta::assert_snapshot!(get_bookmark_output(&clone_dir), @"    main: qomsplrm ebeb70d8 message      @git: qomsplrm ebeb70d8 message      @origin: qomsplrm ebeb70d8 message    [EOF]    ");     // Subsequent fetch should just work even if the source path was relative    let output = clone_dir.run_jj(["git", "fetch"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Nothing changed.    [EOF]    ");     // Failed clone should clean up the destination directory    root_dir.create_dir("bad");    let output = root_dir.run_jj(["git", "clone", "--colocate", "bad", "failed"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/failed"    Error: Could not find repository at '$TEST_ENV/bad'    [EOF]    [exit status: 1]    "#);    assert!(!test_env.env_root().join("failed").exists());     // Failed clone shouldn't remove the existing destination directory    let failed_dir = root_dir.create_dir("failed");    let output = root_dir.run_jj(["git", "clone", "--colocate", "bad", "failed"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/failed"    Error: Could not find repository at '$TEST_ENV/bad'    [EOF]    [exit status: 1]    "#);    assert!(failed_dir.root().exists());    assert!(!failed_dir.root().join(".git").exists());    assert!(!failed_dir.root().join(".jj").exists());     // Failed clone (if attempted) shouldn't remove the existing workspace    let output = root_dir.run_jj(["git", "clone", "--colocate", "bad", "clone"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");    assert!(clone_dir.root().join(".git").exists());    assert!(clone_dir.root().join(".jj").exists());     // Try cloning into an existing workspace    let output = root_dir.run_jj(["git", "clone", "source", "clone", "--colocate"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");     // Try cloning into an existing file    root_dir.write_file("file", "contents");    let output = root_dir.run_jj(["git", "clone", "source", "file", "--colocate"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");     // Try cloning into non-empty, non-workspace directory    clone_dir.remove_dir_all(".jj");    let output = root_dir.run_jj(["git", "clone", "source", "clone", "--colocate"]);    insta::assert_snapshot!(output, @"    ------- stderr -------    Error: Destination path exists and is not an empty directory    [EOF]    [exit status: 1]    ");     // Clone into a nested path    let output = root_dir.run_jj([        "git",        "clone",        "source",        "nested/path/to/repo",        "--colocate",    ]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/nested/path/to/repo"    bookmark: main@origin [new] tracked    Setting the revset alias `trunk()` to `main@origin`    Working copy  (@) now at: vzqnnsmr fea36bca (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    Ok(())} #[test]fn test_git_clone_colocate_via_config() {    let test_env = TestEnvironment::default();    let root_dir = test_env.work_dir("");    let git_repo_path = test_env.env_root().join("source");    let git_repo = git::init(git_repo_path);     test_env.add_config("git.colocate = true");     set_up_non_empty_git_repo(&git_repo);     let output = root_dir.run_jj(["git", "clone", "source", "clone"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/clone"    bookmark: main@origin [new] tracked    Setting the revset alias `trunk()` to `main@origin`    Working copy  (@) now at: sqpuoqvx 1ca44815 (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    let clone_dir = test_env.work_dir("clone");    assert!(clone_dir.root().join("file").exists());    assert!(clone_dir.root().join(".git").exists());} #[test]fn test_git_clone_no_colocate() {    let test_env = TestEnvironment::default();    let root_dir = test_env.work_dir("");    let git_repo_path = test_env.env_root().join("source");    let git_repo = git::init(git_repo_path);     test_env.add_config("git.colocate = true");     set_up_non_empty_git_repo(&git_repo);     let output = root_dir.run_jj(["git", "clone", "--no-colocate", "source", "clone"]);    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/clone"    bookmark: main@origin [new] tracked    Setting the revset alias `trunk()` to `main@origin`    Working copy  (@) now at: sqpuoqvx 1ca44815 (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    let clone_dir = test_env.work_dir("clone");    assert!(clone_dir.root().join("file").exists());    assert!(!clone_dir.root().join(".git").exists());} #[test]fn test_git_clone_default_bookmarks_and_tags() {    let test_env = TestEnvironment::default();    let root_dir = test_env.work_dir("");     let source_repo = git::init(root_dir.root().join("source"));    git::add_commit(&source_repo, "refs/tags/tag1", "file", b"", "1a", &[]);    git::add_commit(&source_repo, "refs/heads/branch1", "file", b"", "1b", &[]);    git::add_commit(&source_repo, "refs/tags/tag2", "file", b"", "1c", &[]);

Lines 1–500 of 1514.