cli: when initializing repo backed by Git repo, check out Git's HEAD

Closes #102.
This commit is contained in:
Martin von Zweigbergk 2022-03-09 13:20:59 -08:00 committed by Martin von Zweigbergk
parent d475710d29
commit f6ba34f3c3
2 changed files with 48 additions and 3 deletions

View File

@ -1757,6 +1757,14 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &ArgMatches) -> Result<(
}
let mut tx = workspace_command.start_transaction("import git refs");
git::import_refs(tx.mut_repo(), &git_repo)?;
if let Some(git_head_id) = tx.mut_repo().view().git_head() {
let git_head_commit = tx.mut_repo().store().get_commit(&git_head_id)?;
tx.mut_repo().check_out(
workspace_command.workspace_id(),
ui.settings(),
&git_head_commit,
);
}
// TODO: Check out a recent commit. Maybe one with the highest generation
// number.
if tx.mut_repo().has_changes() {

View File

@ -43,7 +43,31 @@ fn test_init_git_internal() {
fn test_init_git_external() {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init(&git_repo_path).unwrap();
let git_repo = git2::Repository::init(&git_repo_path).unwrap();
let git_blob_oid = git_repo.blob(b"some content").unwrap();
let mut git_tree_builder = git_repo.treebuilder(None).unwrap();
git_tree_builder
.insert("some-file", git_blob_oid, 0o100644)
.unwrap();
let git_tree_id = git_tree_builder.write().unwrap();
let git_tree = git_repo.find_tree(git_tree_id).unwrap();
let git_signature = git2::Signature::new(
"Git User",
"git.user@example.com",
&git2::Time::new(123, 60),
)
.unwrap();
git_repo
.commit(
Some("refs/heads/my-branch"),
&git_signature,
&git_signature,
"My commit message",
&git_tree,
&[],
)
.unwrap();
git_repo.set_head("refs/heads/my-branch").unwrap();
let assert = test_env
.jj_cmd(
@ -57,8 +81,11 @@ fn test_init_git_external() {
)
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"Initialized repo in "repo"
"###);
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
Working copy now at: f6950fc115ae
Added 1 files, modified 0 files, removed 0 files
Initialized repo in "repo"
"###);
let workspace_root = test_env.env_root().join("repo");
let jj_path = workspace_root.join(".jj");
@ -73,6 +100,16 @@ fn test_init_git_external() {
assert!(git_target_file_contents
.replace('\\', "/")
.ends_with("/git-repo/.git"));
// Check that the Git repo's HEAD got checked out
let assert = test_env
.jj_cmd(&repo_path, &["log", "-r", "@-"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
o 8d698d4a8ee1 d3866db7e30a git.user@example.com 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git
~ My commit message
"###);
}
#[test]