Skip to main content

sr_core/
git.rs

1use semver::Version;
2
3use crate::commit::Commit;
4use crate::error::ReleaseError;
5
6/// Information about a git tag.
7#[derive(Debug, Clone)]
8pub struct TagInfo {
9    pub name: String,
10    pub version: Version,
11    pub sha: String,
12}
13
14/// Abstraction over git operations.
15pub trait GitRepository: Send + Sync {
16    /// Find the latest semver tag matching the configured prefix.
17    fn latest_tag(&self, prefix: &str) -> Result<Option<TagInfo>, ReleaseError>;
18
19    /// List commits between a starting point (exclusive) and HEAD (inclusive).
20    /// If `from` is `None`, returns all commits reachable from HEAD.
21    fn commits_since(&self, from: Option<&str>) -> Result<Vec<Commit>, ReleaseError>;
22
23    /// Create an annotated tag at HEAD. When `sign` is true, uses `-s` for GPG/SSH signing.
24    fn create_tag(&self, name: &str, message: &str, sign: bool) -> Result<(), ReleaseError>;
25
26    /// Push a tag to the remote.
27    fn push_tag(&self, name: &str) -> Result<(), ReleaseError>;
28
29    /// Stage files and commit. Returns Ok(false) if nothing to commit.
30    fn stage_and_commit(&self, paths: &[&str], message: &str) -> Result<bool, ReleaseError>;
31
32    /// Push current branch to origin.
33    fn push(&self) -> Result<(), ReleaseError>;
34
35    /// Check if a tag exists locally.
36    fn tag_exists(&self, name: &str) -> Result<bool, ReleaseError>;
37
38    /// Check if a tag exists on the remote.
39    fn remote_tag_exists(&self, name: &str) -> Result<bool, ReleaseError>;
40
41    /// List all semver tags matching prefix, sorted by version ascending.
42    fn all_tags(&self, prefix: &str) -> Result<Vec<TagInfo>, ReleaseError>;
43
44    /// List commits between two refs (exclusive `from`, inclusive `to`).
45    /// If `from` is None, returns all commits reachable from `to`.
46    fn commits_between(&self, from: Option<&str>, to: &str) -> Result<Vec<Commit>, ReleaseError>;
47
48    /// Get the date (YYYY-MM-DD) of the commit a tag points to.
49    fn tag_date(&self, tag_name: &str) -> Result<String, ReleaseError>;
50
51    /// Force-create an annotated tag at HEAD, overwriting if it already exists.
52    /// When `sign` is true, uses `-s` for GPG/SSH signing.
53    fn force_create_tag(&self, name: &str, message: &str, sign: bool) -> Result<(), ReleaseError>;
54
55    /// Force-push a tag to the remote, overwriting the remote tag if it exists.
56    fn force_push_tag(&self, name: &str) -> Result<(), ReleaseError>;
57
58    /// Return the full SHA of HEAD.
59    fn head_sha(&self) -> Result<String, ReleaseError>;
60
61    /// Like `commits_since`, but only includes commits that touched files under `path`.
62    fn commits_since_in_path(
63        &self,
64        from: Option<&str>,
65        path: &str,
66    ) -> Result<Vec<Commit>, ReleaseError> {
67        // Default: ignore path filter (for test fakes and backwards compat)
68        let _ = path;
69        self.commits_since(from)
70    }
71
72    /// Like `commits_between`, but only includes commits that touched files under `path`.
73    fn commits_between_in_path(
74        &self,
75        from: Option<&str>,
76        to: &str,
77        path: &str,
78    ) -> Result<Vec<Commit>, ReleaseError> {
79        let _ = path;
80        self.commits_between(from, to)
81    }
82}