Skip to content

Commit

Permalink
cli: when initializing repo backed by Git repo, check out Git's HEAD
Browse files Browse the repository at this point in the history
Closes #102.
  • Loading branch information
martinvonz committed Mar 9, 2022
1 parent f0ee74d commit 003c489
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
43 changes: 40 additions & 3 deletions tests/test_init_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"[email protected]",
&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(
Expand All @@ -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");
Expand All @@ -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 [email protected] 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git
~ My commit message
"###);
}

#[test]
Expand Down

0 comments on commit 003c489

Please sign in to comment.