2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2022 The Jujutsu Authors
|
2022-11-25 04:17:39 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2022-12-13 01:04:57 +00:00
|
|
|
use std::path::Path;
|
2022-11-25 04:17:39 +00:00
|
|
|
|
2023-04-25 01:26:04 +00:00
|
|
|
use itertools::Itertools as _;
|
2024-01-04 07:18:04 +00:00
|
|
|
use jj_lib::backend::CommitId;
|
2023-04-25 01:26:04 +00:00
|
|
|
|
2023-07-03 10:04:31 +00:00
|
|
|
use crate::common::TestEnvironment;
|
2022-11-25 04:17:39 +00:00
|
|
|
|
2023-06-11 05:54:10 +00:00
|
|
|
#[test]
|
|
|
|
fn test_resolution_of_git_tracking_branches() {
|
|
|
|
let test_env = TestEnvironment::default();
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
|
2023-06-11 05:54:10 +00:00
|
|
|
let repo_path = test_env.env_root().join("repo");
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
|
|
|
|
test_env.jj_cmd_ok(&repo_path, &["describe", "-r", "main", "-m", "old_message"]);
|
2023-06-11 05:54:10 +00:00
|
|
|
|
|
|
|
// Create local-git tracking branch
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "export"]);
|
2023-06-11 05:54:10 +00:00
|
|
|
insta::assert_snapshot!(stdout, @"");
|
2023-10-10 11:59:18 +00:00
|
|
|
insta::assert_snapshot!(stderr, @"");
|
2023-06-11 05:54:10 +00:00
|
|
|
// Move the local branch somewhere else
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(&repo_path, &["describe", "-r", "main", "-m", "new_message"]);
|
2023-06-11 05:54:10 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
main: qpvuntsm 3af37026 (empty) new_message
|
2023-10-24 01:43:12 +00:00
|
|
|
@git (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 16d541ca (empty) old_message
|
2023-06-11 05:54:10 +00:00
|
|
|
"###);
|
|
|
|
|
|
|
|
// Test that we can address both revisions
|
2023-09-25 11:05:24 +00:00
|
|
|
let query = |expr| {
|
|
|
|
let template = r#"commit_id ++ " " ++ description"#;
|
|
|
|
test_env.jj_cmd_success(
|
|
|
|
&repo_path,
|
|
|
|
&["log", "-r", expr, "-T", template, "--no-graph"],
|
|
|
|
)
|
|
|
|
};
|
|
|
|
insta::assert_snapshot!(query("main"), @r###"
|
2023-06-11 05:54:10 +00:00
|
|
|
3af370264cdcbba791762f8ef6bc79b456dcbf3b new_message
|
|
|
|
"###);
|
2023-09-25 11:05:24 +00:00
|
|
|
insta::assert_snapshot!(query("main@git"), @r###"
|
|
|
|
16d541ca40f42baf2dea41aa61a0b5f1cbf1f91b old_message
|
|
|
|
"###);
|
2023-11-07 08:47:13 +00:00
|
|
|
// Can't be selected by remote_branches()
|
|
|
|
insta::assert_snapshot!(query(r#"remote_branches(exact:"main", exact:"git")"#), @"");
|
2023-06-11 05:54:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 04:17:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_git_export_conflicting_git_refs() {
|
|
|
|
let test_env = TestEnvironment::default();
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
|
2022-11-25 04:17:39 +00:00
|
|
|
let repo_path = test_env.env_root().join("repo");
|
|
|
|
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
|
|
|
|
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main/sub"]);
|
2023-07-03 10:04:31 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "export"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
2024-02-03 08:17:05 +00:00
|
|
|
insta::with_settings!({filters => vec![(": The lock for resource.*", ": ...")]}, {
|
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
2024-03-24 06:47:47 +00:00
|
|
|
Warning: Failed to export some branches:
|
2024-02-03 08:17:05 +00:00
|
|
|
main/sub: Failed to set: A lock could not be obtained for reference "refs/heads/main/sub": ...
|
|
|
|
Hint: Git doesn't allow a branch name that looks like a parent directory of
|
|
|
|
another (e.g. `foo` and `foo/bar`). Try to rename the branches that failed to
|
|
|
|
export or their "parent" branches.
|
|
|
|
"###);
|
|
|
|
});
|
2022-11-25 04:17:39 +00:00
|
|
|
}
|
2022-12-13 01:04:57 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-04-25 01:26:04 +00:00
|
|
|
fn test_git_export_undo() {
|
|
|
|
let test_env = TestEnvironment::default();
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
|
2023-04-25 01:26:04 +00:00
|
|
|
let repo_path = test_env.env_root().join("repo");
|
|
|
|
let git_repo = git2::Repository::open(repo_path.join(".jj/repo/store/git")).unwrap();
|
|
|
|
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "a"]);
|
2023-04-25 01:26:04 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
a: qpvuntsm 230dd059 (empty) (no description set)
|
2023-04-25 01:26:04 +00:00
|
|
|
"###);
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "export"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
|
|
|
insta::assert_snapshot!(stderr, @"");
|
2023-09-25 13:47:01 +00:00
|
|
|
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["log", "-ra@git"]), @r###"
|
2024-02-12 07:26:19 +00:00
|
|
|
@ qpvuntsm test.user@example.com 2001-02-03 08:05:07 a 230dd059
|
2023-09-25 13:47:01 +00:00
|
|
|
│ (empty) (no description set)
|
|
|
|
~
|
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
|
2023-09-25 13:47:01 +00:00
|
|
|
// Exported refs won't be removed by undoing the export, but the git-tracking
|
|
|
|
// branch is. This is the same as remote-tracking branches.
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["op", "undo"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
|
|
|
insta::assert_snapshot!(stderr, @"");
|
2023-06-07 03:30:11 +00:00
|
|
|
insta::assert_debug_snapshot!(get_git_repo_refs(&git_repo), @r###"
|
2023-04-25 01:26:04 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
"refs/heads/a",
|
|
|
|
CommitId(
|
|
|
|
"230dd059e1b059aefc0da06a2e5a7dbf22362f22",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
"###);
|
2023-09-25 13:47:01 +00:00
|
|
|
insta::assert_snapshot!(test_env.jj_cmd_failure(&repo_path, &["log", "-ra@git"]), @r###"
|
|
|
|
Error: Revision "a@git" doesn't exist
|
|
|
|
Hint: Did you mean "a"?
|
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
|
2023-09-25 13:47:01 +00:00
|
|
|
// This would re-export branch "a" and create git-tracking branch.
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "export"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
|
|
|
insta::assert_snapshot!(stderr, @"");
|
2023-09-25 13:47:01 +00:00
|
|
|
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["log", "-ra@git"]), @r###"
|
2024-02-12 07:26:19 +00:00
|
|
|
@ qpvuntsm test.user@example.com 2001-02-03 08:05:07 a 230dd059
|
2023-09-25 13:47:01 +00:00
|
|
|
│ (empty) (no description set)
|
|
|
|
~
|
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_git_import_undo() {
|
|
|
|
let test_env = TestEnvironment::default();
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
|
2023-04-25 01:26:04 +00:00
|
|
|
let repo_path = test_env.env_root().join("repo");
|
|
|
|
let git_repo = git2::Repository::open(repo_path.join(".jj/repo/store/git")).unwrap();
|
|
|
|
|
|
|
|
// Create branch "a" in git repo
|
|
|
|
let commit_id =
|
|
|
|
test_env.jj_cmd_success(&repo_path, &["log", "-Tcommit_id", "--no-graph", "-r@"]);
|
|
|
|
let commit = git_repo
|
|
|
|
.find_commit(git2::Oid::from_str(&commit_id).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
git_repo.branch("a", &commit, true).unwrap();
|
|
|
|
|
2023-06-07 03:30:11 +00:00
|
|
|
// Initial state we will return to after `undo`. There are no branches.
|
2023-06-01 04:51:28 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @"");
|
2023-08-21 00:34:49 +00:00
|
|
|
let base_operation_id = test_env.current_operation_id(&repo_path);
|
2023-06-01 04:51:28 +00:00
|
|
|
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "import"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
2024-02-13 22:17:05 +00:00
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
|
|
|
branch: a [new] tracked
|
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
a: qpvuntsm 230dd059 (empty) (no description set)
|
2023-10-18 06:12:17 +00:00
|
|
|
@git: qpvuntsm 230dd059 (empty) (no description set)
|
2023-04-25 01:26:04 +00:00
|
|
|
"###);
|
|
|
|
|
2023-09-25 13:47:01 +00:00
|
|
|
// "git import" can be undone by default.
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["op", "restore", &base_operation_id]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
|
|
|
insta::assert_snapshot!(stderr, @"");
|
2023-04-25 01:26:04 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @"");
|
|
|
|
// Try "git import" again, which should re-import the branch "a".
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "import"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
2024-02-13 22:17:05 +00:00
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
|
|
|
branch: a [new] tracked
|
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
a: qpvuntsm 230dd059 (empty) (no description set)
|
2023-10-18 06:12:17 +00:00
|
|
|
@git: qpvuntsm 230dd059 (empty) (no description set)
|
2023-04-25 01:26:04 +00:00
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-06-01 03:55:46 +00:00
|
|
|
fn test_git_import_move_export_with_default_undo() {
|
2023-04-25 01:26:04 +00:00
|
|
|
let test_env = TestEnvironment::default();
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
|
2023-04-25 01:26:04 +00:00
|
|
|
let repo_path = test_env.env_root().join("repo");
|
|
|
|
let git_repo = git2::Repository::open(repo_path.join(".jj/repo/store/git")).unwrap();
|
|
|
|
|
|
|
|
// Create branch "a" in git repo
|
|
|
|
let commit_id =
|
|
|
|
test_env.jj_cmd_success(&repo_path, &["log", "-Tcommit_id", "--no-graph", "-r@"]);
|
|
|
|
let commit = git_repo
|
|
|
|
.find_commit(git2::Oid::from_str(&commit_id).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
git_repo.branch("a", &commit, true).unwrap();
|
|
|
|
|
2023-06-07 03:30:11 +00:00
|
|
|
// Initial state we will try to return to after `op restore`. There are no
|
|
|
|
// branches.
|
2023-06-01 04:51:28 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @"");
|
2023-08-21 00:34:49 +00:00
|
|
|
let base_operation_id = test_env.current_operation_id(&repo_path);
|
2023-06-01 04:51:28 +00:00
|
|
|
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "import"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
2024-02-13 22:17:05 +00:00
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
|
|
|
branch: a [new] tracked
|
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
a: qpvuntsm 230dd059 (empty) (no description set)
|
2023-10-18 06:12:17 +00:00
|
|
|
@git: qpvuntsm 230dd059 (empty) (no description set)
|
2023-04-25 01:26:04 +00:00
|
|
|
"###);
|
|
|
|
|
|
|
|
// Move branch "a" and export to git repo
|
2023-10-10 11:59:18 +00:00
|
|
|
test_env.jj_cmd_ok(&repo_path, &["new"]);
|
|
|
|
test_env.jj_cmd_ok(&repo_path, &["branch", "set", "a"]);
|
2023-04-25 01:26:04 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
a: yqosqzyt 096dc80d (empty) (no description set)
|
|
|
|
@git (behind by 1 commits): qpvuntsm 230dd059 (empty) (no description set)
|
2023-04-25 01:26:04 +00:00
|
|
|
"###);
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "export"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
|
|
|
insta::assert_snapshot!(stderr, @"");
|
2023-06-07 03:30:11 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
a: yqosqzyt 096dc80d (empty) (no description set)
|
2023-10-18 06:12:17 +00:00
|
|
|
@git: yqosqzyt 096dc80d (empty) (no description set)
|
2023-07-30 05:24:34 +00:00
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
|
2023-06-07 03:30:11 +00:00
|
|
|
// "git import" can be undone with the default `restore` behavior, as shown in
|
|
|
|
// the previous test. However, "git export" can't: the branches in the git
|
|
|
|
// repo stay where they were.
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["op", "restore", &base_operation_id]);
|
2023-10-10 11:07:06 +00:00
|
|
|
insta::assert_snapshot!(stdout, @"");
|
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
Working copy now at: qpvuntsm 230dd059 (empty) (no description set)
|
|
|
|
Parent commit : zzzzzzzz 00000000 (empty) (no description set)
|
2023-04-25 01:26:04 +00:00
|
|
|
"###);
|
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @"");
|
2023-06-07 03:30:11 +00:00
|
|
|
insta::assert_debug_snapshot!(get_git_repo_refs(&git_repo), @r###"
|
2023-04-25 01:26:04 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
"refs/heads/a",
|
|
|
|
CommitId(
|
2023-06-01 04:51:28 +00:00
|
|
|
"096dc80da67094fbaa6683e2a205dddffa31f9a8",
|
2023-04-25 01:26:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
|
|
|
|
// The last branch "a" state is imported from git. No idea what's the most
|
|
|
|
// intuitive result here.
|
2023-10-10 11:59:18 +00:00
|
|
|
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "import"]);
|
|
|
|
insta::assert_snapshot!(stdout, @"");
|
2024-02-13 22:17:05 +00:00
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
|
|
|
branch: a [new] tracked
|
|
|
|
"###);
|
2023-04-25 01:26:04 +00:00
|
|
|
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
|
2023-07-11 01:46:38 +00:00
|
|
|
a: yqosqzyt 096dc80d (empty) (no description set)
|
2023-10-18 06:12:17 +00:00
|
|
|
@git: yqosqzyt 096dc80d (empty) (no description set)
|
2023-04-25 01:26:04 +00:00
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:04:57 +00:00
|
|
|
fn get_branch_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
|
2024-03-31 09:54:11 +00:00
|
|
|
test_env.jj_cmd_success(repo_path, &["branch", "list", "--all-remotes"])
|
2022-12-13 01:04:57 +00:00
|
|
|
}
|
2023-04-25 01:26:04 +00:00
|
|
|
|
2023-06-07 03:30:11 +00:00
|
|
|
fn get_git_repo_refs(git_repo: &git2::Repository) -> Vec<(String, CommitId)> {
|
2023-04-25 01:26:04 +00:00
|
|
|
let mut refs: Vec<_> = git_repo
|
|
|
|
.references()
|
|
|
|
.unwrap()
|
|
|
|
.filter_ok(|git_ref| git_ref.is_tag() || git_ref.is_branch() || git_ref.is_remote())
|
|
|
|
.filter_map_ok(|git_ref| {
|
|
|
|
let full_name = git_ref.name()?.to_owned();
|
|
|
|
let git_commit = git_ref.peel_to_commit().ok()?;
|
|
|
|
let commit_id = CommitId::from_bytes(git_commit.id().as_bytes());
|
|
|
|
Some((full_name, commit_id))
|
|
|
|
})
|
|
|
|
.try_collect()
|
|
|
|
.unwrap();
|
|
|
|
refs.sort();
|
|
|
|
refs
|
|
|
|
}
|