From ee641bf7adb0a887cdc300316dedb68a68f66caa Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 30 Jan 2022 21:53:24 -0800 Subject: [PATCH] cli: when exporting Git HEAD, use right workspace's new checkout (#13) If the workspace is shared with a Git repo, we sometimes update Git's HEAD ref. We should get the new checkout from the right workspace ID when doing that (though I'm not sure we'll ever support sharing the working copy with Git in a non-default workspace). --- src/commands.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index f38d40dc4..31e8235a0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -319,16 +319,22 @@ impl WorkspaceCommandHelper { .ok() .map(|commit| commit.id()); git::export_refs(repo, &git_repo)?; - let checkout_id = repo.view().checkout(); - let first_parent_id = - repo.index().entry_by_id(checkout_id).unwrap().parents()[0].commit_id(); - if first_parent_id != *repo.store().root_commit_id() { - if let Some(current_git_commit_id) = current_git_commit_id { - git_repo.set_head_detached(current_git_commit_id)?; + if let Some(checkout_id) = repo.view().get_checkout(&self.workspace_id()) { + let first_parent_id = + repo.index().entry_by_id(checkout_id).unwrap().parents()[0].commit_id(); + if first_parent_id != *repo.store().root_commit_id() { + if let Some(current_git_commit_id) = current_git_commit_id { + git_repo.set_head_detached(current_git_commit_id)?; + } + let new_git_commit_id = Oid::from_bytes(first_parent_id.as_bytes()).unwrap(); + let new_git_commit = git_repo.find_commit(new_git_commit_id)?; + git_repo.reset(new_git_commit.as_object(), git2::ResetType::Mixed, None)?; } - let new_git_commit_id = Oid::from_bytes(first_parent_id.as_bytes()).unwrap(); - let new_git_commit = git_repo.find_commit(new_git_commit_id)?; - git_repo.reset(new_git_commit.as_object(), git2::ResetType::Mixed, None)?; + } else { + // The workspace was removed (maybe the user undid the + // initialization of the workspace?), which is weird, + // but we should probably just not do anything else here. + // Except maybe print a note about it? } Ok(()) }