From f74d41a445623004d5a32e0eabb758a00fca0e40 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 5 Apr 2020 19:50:11 +0200 Subject: [PATCH] fix committing in empty repo --- README.md | 1 - asyncgit/src/sync/utils.rs | 34 +++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4fcd6084..7e9bb6b5 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,6 @@ GITUI_LOGGING=true gitui # todo for 0.1 (first release) -* [ ] fix crashes on empty git repos (unstaging,committing?) * [ ] (un)staging selected hunks * [ ] publish as homebrew-tap diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index 66942839..918a5282 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -37,12 +37,19 @@ pub fn commit(repo_path: &str, msg: &str) { let repo = repo(repo_path); let signature = repo.signature().unwrap(); - let reference = repo.head().unwrap(); let mut index = repo.index().unwrap(); let tree_id = index.write_tree().unwrap(); let tree = repo.find_tree(tree_id).unwrap(); - let parent = - repo.find_commit(reference.target().unwrap()).unwrap(); + + let parents = if let Ok(reference) = repo.head() { + let parent = + repo.find_commit(reference.target().unwrap()).unwrap(); + vec![parent] + } else { + Vec::new() + }; + + let parents = parents.iter().collect::>(); repo.commit( Some("HEAD"), @@ -50,7 +57,7 @@ pub fn commit(repo_path: &str, msg: &str) { &signature, msg, &tree, - &[&parent], + parents.as_slice(), ) .unwrap(); } @@ -89,7 +96,7 @@ mod tests { use crate::sync::{ stage_add, status::{get_status, StatusType}, - tests::repo_init, + tests::{repo_init, repo_init_empty}, }; use std::{fs::File, io::Write, path::Path}; @@ -121,4 +128,21 @@ mod tests { assert_eq!(status_count(StatusType::Stage), 0); assert_eq!(status_count(StatusType::WorkingDir), 0); } + + #[test] + fn test_commit_in_empty_repo() { + let file_path = Path::new("foo"); + let (_td, repo) = repo_init_empty(); + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + File::create(&root.join(file_path)) + .unwrap() + .write_all(b"test\nfoo") + .unwrap(); + + assert_eq!(stage_add(repo_path, file_path), true); + + commit(repo_path, "commit msg"); + } }