| 3 | use super::status; |
| 4 | |
| 5 | pub fn commit(message: String, allow_empty: bool) { |
| 6 | let index = Index::get_instance(); |
| 7 | if !allow_empty && status::changes_to_be_committed().is_empty() { |
| 8 | panic!("工作区没有任何改动,不需要提交"); |
| 9 | } |
| 10 | |
| 11 | let current_head = head::current_head(); |
| 12 | let current_commit_hash = head::current_head_commit(); |
| 13 | |
| 14 | let mut commit = { |
| 15 | if current_commit_hash.is_empty() { |
| 16 | Commit::new(index, vec![], message.clone()) |
| 17 | } else { |
| 18 | Commit::new(index, vec![current_commit_hash.clone()], message.clone()) |
| 19 | } |
| 20 | }; |
| 21 | let commit_hash = commit.save(); |
| 22 | head::update_head_commit(&commit_hash); |
| 23 | |
| 24 | match current_head { |
| 25 | head::Head::Branch(branch_name) => { |
| 26 | println!("commit to [{:?}] message{:?}", branch_name, message) |
| 27 | } |
| 28 | head::Head::Detached(commit_hash) => { |
| 29 | println!("Detached HEAD commit {:?} message{:?}", commit_hash[0..7].to_string(), message) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | println!("commit hash: {:?}", commit_hash); |
| 34 | index.save(); |
| 35 | } |
| 36 | |
| 37 | #[cfg(test)] |
| 38 | mod test { |