diff --git a/cli/src/command_error.rs b/cli/src/command_error.rs index 8993eb2f14..1e8847630c 100644 --- a/cli/src/command_error.rs +++ b/cli/src/command_error.rs @@ -438,20 +438,11 @@ impl From for CommandError { impl From for CommandError { fn from(err: GitImportError) -> Self { let hint = match &err { - GitImportError::MissingHeadTarget { .. } - | GitImportError::MissingRefAncestor { .. } => Some( - "\ -Is this Git repository a shallow or partial clone (cloned with the --depth or --filter \ - argument)? -jj currently does not support shallow/partial clones. To use jj with this \ - repository, try -unshallowing the repository (https://stackoverflow.com/q/6802145) or re-cloning with the full -repository contents." - .to_string(), - ), GitImportError::RemoteReservedForLocalGitRepo => { Some("Run `jj git remote rename` to give different name.".to_string()) } + GitImportError::MissingHeadTarget { .. } => None, + GitImportError::MissingRefAncestor { .. } => None, GitImportError::InternalBackend(_) => None, GitImportError::InternalGitError(_) => None, GitImportError::UnexpectedBackend => None, diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 7d2b267e99..16ec8cba13 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -519,6 +519,7 @@ fn commit_from_git_without_root_parent( id: &CommitId, git_object: &gix::Object, uses_tree_conflict_format: bool, + is_shallow: bool, ) -> BackendResult { let commit = git_object .try_to_commit_ref() @@ -537,10 +538,15 @@ fn commit_from_git_without_root_parent( .map(|b| b.reverse_bits()) .collect(), ); - let parents = commit - .parents() - .map(|oid| CommitId::from_bytes(oid.as_bytes())) - .collect_vec(); + // shallow commits don't have parents their parents actually fetched, so we discard them here + let parents = if is_shallow { + vec![] + } else { + commit + .parents() + .map(|oid| CommitId::from_bytes(oid.as_bytes())) + .collect_vec() + }; let tree_id = TreeId::from_bytes(commit.tree().as_bytes()); // If this commit is a conflict, we'll update the root tree later, when we read // the extra metadata. @@ -859,6 +865,8 @@ fn import_extra_metadata_entries_from_heads( head_ids: &HashSet<&CommitId>, uses_tree_conflict_format: bool, ) -> BackendResult<()> { + let shallow_commits = git_repo.shallow_commits().ok().flatten(); + let mut work_ids = head_ids .iter() .filter(|&id| mut_table.get_value(id.as_bytes()).is_none()) @@ -868,11 +876,18 @@ fn import_extra_metadata_entries_from_heads( let git_object = git_repo .find_object(validate_git_object_id(&id)?) .map_err(|err| map_not_found_err(err, &id))?; + let is_shallow = shallow_commits + .as_ref() + .is_some_and(|shallow| shallow.contains(&git_object.id)); // TODO(#1624): Should we read the root tree here and check if it has a // `.jjconflict-...` entries? That could happen if the user used `git` to e.g. // change the description of a commit with tree-level conflicts. - let commit = - commit_from_git_without_root_parent(&id, &git_object, uses_tree_conflict_format)?; + let commit = commit_from_git_without_root_parent( + &id, + &git_object, + uses_tree_conflict_format, + is_shallow, + )?; mut_table.add_entry(id.to_bytes(), serialize_extras(&commit)); work_ids.extend( commit @@ -1142,7 +1157,12 @@ impl Backend for GitBackend { let git_object = locked_repo .find_object(git_commit_id) .map_err(|err| map_not_found_err(err, id))?; - commit_from_git_without_root_parent(id, &git_object, false)? + let is_shallow = locked_repo + .shallow_commits() + .ok() + .flatten() + .is_some_and(|shallow| shallow.contains(&git_object.id)); + commit_from_git_without_root_parent(id, &git_object, false, is_shallow)? }; if commit.parents.is_empty() { commit.parents.push(self.root_commit_id.clone()); @@ -2151,8 +2171,8 @@ mod tests { author Someone 0 +0000 committer Someone 0 +0000 gpgsig test sig - - + + hash=9ad9526c3b2103c41a229f2f3c82d107a0ecd902f476a855f0e1dd5f7bef1430663de12749b73e293a877113895a8a2a0f29da4bbc5a5f9a19c3523fb0e53518 initial