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(repo_path: &RepoPath, commit: CommitId) -> Result<()> {
13    scope_time!("revert");
14
15    let repo = repo(repo_path)?;
16
17    let commit = repo.find_commit(commit.into())?;
18
19    repo.revert(&commit, None)?;
20
21    Ok(())
22}
23
24///
25pub fn revert_head(repo_path: &RepoPath) -> Result<CommitId> {
26    scope_time!("revert_head");
27
28    let path = repo(repo_path)?.path().join(GIT_REVERT_HEAD_FILE);
29
30    let file_content = read_file(&path)?;
31
32    let id = git2::Oid::from_str(file_content.trim())?;
33
34    Ok(id.into())
35}
36
37///
38pub fn commit_revert(repo_path: &RepoPath, msg: &str) -> Result<CommitId> {
39    scope_time!("commit_revert");
40
41    let id = crate::sync::commit(repo_path, msg)?;
42
43    repo(repo_path)?.cleanup_state()?;
44
45    Ok(id)
46}