diff --git a/icechunk/src/session.rs b/icechunk/src/session.rs index cf4955a8d..cff6d884e 100644 --- a/icechunk/src/session.rs +++ b/icechunk/src/session.rs @@ -1140,6 +1140,13 @@ impl Session { // Icechunk 1 doesn't support amend self.asset_manager.fail_unless_spec_at_least(SpecVersionBin::V2dot0)?; + // Cannot amend the initial commit (which has no parent and no transaction log) + let (repo_info, _) = self.asset_manager.fetch_repo_info().await?; + let current_snapshot_info = repo_info.find_snapshot(self.snapshot_id())?; + if current_snapshot_info.is_initial() { + return Err(SessionErrorKind::NoAmendForInitialCommit.into()); + } + self._commit(message, properties, false, CommitMethod::Amend).await } @@ -2606,7 +2613,7 @@ async fn do_commit_v2( (CommitMethod::NewCommit, _) => parent_snapshot_id.clone(), (CommitMethod::Amend, Some(parent_id)) => parent_id, (CommitMethod::Amend, None) => { - return Err(RepositoryErrorKind::NoAmendForInitialCommit.into()); + unreachable!("amend() checks for initial commit before calling _commit") } }; @@ -4184,6 +4191,15 @@ mod tests { #[tokio_test] async fn test_amend() -> Result<(), Box> { let repo = create_memory_store_repository().await; + + // Test that amending the initial commit fails + let mut session = repo.writable_session("main").await?; + session.add_group(Path::root(), Bytes::copy_from_slice(b"")).await?; + let amend_result = session.amend("cannot amend initial commit", None).await; + assert!(amend_result.is_err()); + assert!(amend_result.unwrap_err().to_string().contains("first commit")); + + // Now make a proper first commit let mut session = repo.writable_session("main").await?; session.add_group(Path::root(), Bytes::copy_from_slice(b"")).await?; session.commit("make root", None).await?;