-
Notifications
You must be signed in to change notification settings - Fork 413
/
Copy pathcommit.rs
36 lines (29 loc) · 1019 Bytes
/
commit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use git2::{Error, ErrorCode, Repository, Signature};
fn main() -> Result<(), Error> {
let repository = Repository::open(".")?;
// We will commit the content of the index
let mut index = repository.index()?;
let tree_oid = index.write_tree()?;
let tree = repository.find_tree(tree_oid)?;
let parent_commit = match repository.revparse_single("HEAD") {
Ok(obj) => Some(obj.into_commit().unwrap()),
// First commit so no parent commit
Err(e) if e.code() == ErrorCode::NotFound => None,
Err(e) => return Err(e),
};
let mut parents = Vec::new();
if parent_commit.is_some() {
parents.push(parent_commit.as_ref().unwrap());
}
let signature = Signature::now("username", "[email protected]")?;
let commit_oid = repository.commit(
Some("HEAD"),
&signature,
&signature,
"Commit message",
&tree,
&parents[..],
)?;
let _commit = repository.find_commit(commit_oid)?;
Ok(())
}