gnostr_asyncgit/sync/
commit_revert.rs

1use scopetime::scope_time;
2
3use super::{CommitId, RepoPath};
4use crate::{
5	error::Result,
6	sync::{repository::repo, utils::read_file},
7};
8
9const GIT_REVERT_HEAD_FILE: &str = "REVERT_HEAD";
10
11///
12pub fn revert_commit(
13	repo_path: &RepoPath,
14	commit: CommitId,
15) -> Result<()> {
16	scope_time!("revert");
17
18	let repo = repo(repo_path)?;
19
20	let commit = repo.find_commit(commit.into())?;
21
22	repo.revert(&commit, None)?;
23
24	Ok(())
25}
26
27///
28pub fn revert_head(repo_path: &RepoPath) -> Result<CommitId> {
29	scope_time!("revert_head");
30
31	let path = repo(repo_path)?.path().join(GIT_REVERT_HEAD_FILE);
32
33	let file_content = read_file(&path)?;
34
35	let id = git2::Oid::from_str(file_content.trim())?;
36
37	Ok(id.into())
38}
39
40///
41pub fn commit_revert(
42	repo_path: &RepoPath,
43	msg: &str,
44) -> Result<CommitId> {
45	scope_time!("commit_revert");
46
47	let id = crate::sync::commit(repo_path, msg)?;
48
49	repo(repo_path)?.cleanup_state()?;
50
51	Ok(id)
52}