From aacf077c256ed79a08d92a0481cd41f1d1f33574 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Sat, 16 Dec 2023 11:41:06 +0200 Subject: [PATCH] Issue #58: Handle repository with zero commits, branches, ...etc --- crates/gitql-engine/src/engine_function.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/gitql-engine/src/engine_function.rs b/crates/gitql-engine/src/engine_function.rs index 087ada48..87d60a90 100644 --- a/crates/gitql-engine/src/engine_function.rs +++ b/crates/gitql-engine/src/engine_function.rs @@ -122,7 +122,12 @@ fn select_commits( let repo_path = repo.path().to_str().unwrap().to_string(); let mut commits: Vec = Vec::new(); - let revwalk = repo.head_id().unwrap().ancestors().all().unwrap(); + let head_id = repo.head_id(); + if head_id.is_err() { + return Ok(commits); + } + + let revwalk = head_id.unwrap().ancestors().all().unwrap(); let names_len = fields_names.len() as i64; let values_len = fields_values.len() as i64; @@ -343,7 +348,17 @@ fn select_branches( let local_branches = platform.local_branches().unwrap(); let remote_branches = platform.remote_branches().unwrap(); let local_and_remote_branches = local_branches.chain(remote_branches); - let head_ref = repo.head_ref().unwrap().unwrap(); + let head_ref_result = repo.head_ref(); + if head_ref_result.is_err() { + return Ok(branches); + } + + let head_ref_option = head_ref_result.unwrap(); + if head_ref_option.is_none() { + return Ok(branches); + } + + let head_ref = head_ref_option.unwrap(); let names_len = fields_names.len() as i64; let values_len = fields_values.len() as i64;