1171 lines · 41.8 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 test_case::test_case;use testutils::git; use crate::common::to_toml_value;use crate::common::CommandOutput;use crate::common::TestEnvironment;use crate::common::TestWorkDir; 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");} #[cfg_attr(feature = "git2", test_case(false; "use git2 for remote calls"))]#[test_case(true; "spawn a git subprocess for remote calls")]fn test_git_clone(subprocess: bool) {    let test_env = TestEnvironment::default().with_git_subprocess(subprocess);    let root_dir = test_env.work_dir("");    test_env.add_config("git.auto-local-bookmark = true");    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::allow_duplicates! {    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::allow_duplicates! {    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 f78d2645 (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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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", "bad", "failed"]);    // git2's internal error is slightly different    if subprocess {        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]        "#);    } else {        insta::assert_snapshot!(output, @r#"        ------- stderr -------        Fetching into new repo in "$TEST_ENV/failed"        Error: could not find repository at '$TEST_ENV/bad'; class=Repository (6)        [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", "bad", "failed"]);    // git2's internal error is slightly different    if subprocess {        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]        "#);    } else {        insta::assert_snapshot!(output, @r#"        ------- stderr -------        Fetching into new repo in "$TEST_ENV/failed"        Error: could not find repository at '$TEST_ENV/bad'; class=Repository (6)        [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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    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: uuzqqzqu cf5d593e (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    }} #[cfg_attr(feature = "git2", test_case(false; "use git2 for remote calls"))]#[test_case(true; "spawn a git subprocess for remote calls")]fn test_git_clone_bad_source(subprocess: bool) {    let test_env = TestEnvironment::default().with_git_subprocess(subprocess);    let root_dir = test_env.work_dir("");     let output = root_dir.run_jj(["git", "clone", "", "dest"]);    insta::allow_duplicates! {    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Error: local path "" does not specify a path to a repository    [EOF]    [exit status: 2]    "#);    }     // Invalid port number    let output = root_dir.run_jj(["git", "clone", "https://example.net:bad-port/bar", "dest"]);    insta::allow_duplicates! {    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Error: URL "https://example.net:bad-port/bar" can not be parsed as valid URL    Caused by: invalid port number    [EOF]    [exit status: 2]    "#);    }} #[cfg_attr(feature = "git2", test_case(false; "use git2 for remote calls"))]#[test_case(true; "spawn a git subprocess for remote calls")]fn test_git_clone_colocate(subprocess: bool) {    let test_env = TestEnvironment::default().with_git_subprocess(subprocess);    let root_dir = test_env.work_dir("");    test_env.add_config("git.auto-local-bookmark = true");    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::allow_duplicates! {    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()).unwrap();    insta::allow_duplicates! {    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::allow_duplicates! {    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 f78d2645 (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::allow_duplicates! {    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::allow_duplicates! {    insta::assert_snapshot!(get_bookmark_output(&clone_dir), @r"    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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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"]);    // git2's internal error is slightly different    if subprocess {        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]        "#);    } else {        insta::assert_snapshot!(output, @r#"        ------- stderr -------        Fetching into new repo in "$TEST_ENV/failed"        Error: could not find repository at '$TEST_ENV/bad'; class=Repository (6)        [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"]);    // git2's internal error is slightly different    if subprocess {        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]        "#);    } else {        insta::assert_snapshot!(output, @r#"        ------- stderr -------        Fetching into new repo in "$TEST_ENV/failed"        Error: could not find repository at '$TEST_ENV/bad'; class=Repository (6)        [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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    insta::assert_snapshot!(output, @r"    ------- 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::allow_duplicates! {    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 589d0921 (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    }} #[cfg_attr(feature = "git2", test_case(false; "use git2 for remote calls"))]#[test_case(true; "spawn a git subprocess for remote calls")]fn test_git_clone_remote_default_bookmark(subprocess: bool) {    let test_env = TestEnvironment::default().with_git_subprocess(subprocess);    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.clone());     set_up_non_empty_git_repo(&git_repo);     // Create non-default bookmark in remote    let head_id = git_repo.head_id().unwrap().detach();    git_repo        .reference(            "refs/heads/feature1",            head_id,            gix::refs::transaction::PreviousValue::MustNotExist,            "",        )        .unwrap();     // All fetched bookmarks will be imported if auto-local-bookmark is on    test_env.add_config("git.auto-local-bookmark = true");    let output = root_dir.run_jj(["git", "clone", "source", "clone1"]);    insta::allow_duplicates! {    insta::assert_snapshot!(output, @r#"    ------- stderr -------    Fetching into new repo in "$TEST_ENV/clone1"    bookmark: feature1@origin [new] tracked    bookmark: main@origin     [new] tracked    Setting the revset alias `trunk()` to `main@origin`    Working copy  (@) now at: sqpuoqvx 2ca1c979 (empty) (no description set)    Parent commit (@-)      : qomsplrm ebeb70d8 feature1 main | message    Added 1 files, modified 0 files, removed 0 files    [EOF]    "#);    }    let clone_dir1 = test_env.work_dir("clone1");    insta::allow_duplicates! {

Lines 1–500 of 1171.