From 8c63fbc4ed434f52b045a3a5f8c0e397b7ec2ffd Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 5 Feb 2023 21:14:01 -0800 Subject: [PATCH] git_backend: don't panic if told to write merge with root commit I think the CLI currently checks that the backend is not told to write a merge commit with the root as one parent, but we should not panic if those checks fail. --- lib/src/git_backend.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index bd71c38ef..a1a03b767 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -460,7 +460,13 @@ impl Backend for GitBackend { // add it to the list of parents to write in the Git commit. We also check that // there are no other parents since Git cannot represent a merge between a root // commit and another commit. - assert_eq!(contents.parents.len(), 1); + if contents.parents.len() > 1 { + return Err(BackendError::Other( + "The Git backend does not support creating merge commits with the root \ + commit as one of the parents." + .to_string(), + )); + } } else { let git_commit_id = validate_git_object_id(parent_id)?; let parent_git_commit = locked_repo @@ -595,6 +601,7 @@ fn bytes_vec_from_json(value: &serde_json::Value) -> Vec { #[cfg(test)] mod tests { + use assert_matches::assert_matches; use super::*; use crate::backend::{FileId, MillisSinceEpoch}; @@ -757,6 +764,13 @@ mod tests { merge_git_commit.parent_ids().collect_vec(), vec![git_id(&first_id), git_id(&second_id)] ); + + // Merge commit with root as one parent + commit.parents = vec![first_id, backend.root_commit_id().clone()]; + assert_matches!( + backend.write_commit(&commit), + Err(BackendError::Other(message)) if message.contains("root commit") + ); } #[test]