grit_lib/commits/handle.rs
1//! [`Commits`] — record and preview commits.
2
3use crate::error::Result;
4use crate::objects::ObjectId;
5use crate::porcelain::commit::{
6 commit_with_scope, default_commit_scope, pending_commit_message, plan_commit,
7 prepare_commit_bytes, write_commit_object, CommitOutcome, CommitPlan, CommitRequest,
8 CommitScope,
9};
10use crate::progress::{CancelToken, ProgressSink};
11use crate::repo::Repository;
12
13/// Request plus scope for [`Commits::create`] and [`Commits::plan`].
14///
15/// Build with [`Self::new`] after staging (for [`crate::porcelain::commit::CommitScope::Staged`])
16/// or to preview auto-staging behavior via [`CommitScope::AutoStage`](crate::porcelain::commit::CommitScope::AutoStage).
17#[derive(Debug, Clone)]
18#[non_exhaustive]
19pub struct CommitOptions {
20 /// Author, message, hooks, amend flags, and related commit inputs.
21 pub request: CommitRequest,
22 /// Which paths and staging mode apply (`AutoStage`, `Staged`, pathspecs, or patch).
23 pub scope: CommitScope,
24}
25
26impl CommitOptions {
27 /// Start options from `request` and `scope`.
28 #[must_use]
29 pub fn new(request: CommitRequest, scope: CommitScope) -> Self {
30 Self { request, scope }
31 }
32
33 /// Sets [`request`](Self::request).
34 #[must_use]
35 pub fn with_request(mut self, request: CommitRequest) -> Self {
36 self.request = request;
37 self
38 }
39
40 /// Sets [`scope`](Self::scope).
41 #[must_use]
42 pub fn with_scope(mut self, scope: CommitScope) -> Self {
43 self.scope = scope;
44 self
45 }
46}
47
48/// Record and preview commits for one open repository.
49#[derive(Debug, Copy, Clone)]
50pub struct Commits<'repo> {
51 repo: &'repo Repository,
52}
53
54impl<'repo> Commits<'repo> {
55 /// Bind commit operations to `repo`.
56 #[inline]
57 #[must_use]
58 pub fn new(repo: &'repo Repository) -> Self {
59 Self { repo }
60 }
61
62 /// Underlying repository.
63 #[inline]
64 #[must_use]
65 pub fn repository(&self) -> &'repo Repository {
66 self.repo
67 }
68
69 /// Default [`CommitScope`] from `commit.autoStage` (default auto-stage).
70 ///
71 /// # Errors
72 ///
73 /// Propagates config read failures.
74 pub fn default_scope(&self) -> Result<CommitScope> {
75 default_commit_scope(self.repo)
76 }
77
78 /// Default message for the next commit when merge or squash state supplies one.
79 ///
80 /// # Errors
81 ///
82 /// Propagates I/O and merge-state read failures.
83 pub fn pending_message(&self) -> Result<Option<String>> {
84 pending_commit_message(self.repo)
85 }
86
87 /// Record a commit using `options.scope` to decide how the index is prepared.
88 ///
89 /// # Errors
90 ///
91 /// See [`crate::porcelain::commit::create_commit`] and [`crate::porcelain::CommitError`].
92 pub fn create(
93 &self,
94 options: &CommitOptions,
95 progress: &mut dyn ProgressSink,
96 cancel: &CancelToken,
97 ) -> Result<CommitOutcome> {
98 commit_with_scope(
99 self.repo,
100 &options.request,
101 options.scope.clone(),
102 progress,
103 cancel,
104 )
105 }
106
107 /// Preview a commit for `options.scope` without mutating the repository.
108 ///
109 /// # Errors
110 ///
111 /// Same as [`Self::create`].
112 pub fn plan(
113 &self,
114 options: &CommitOptions,
115 progress: &mut dyn ProgressSink,
116 cancel: &CancelToken,
117 ) -> Result<CommitPlan> {
118 plan_commit(
119 self.repo,
120 &options.request,
121 options.scope.clone(),
122 progress,
123 cancel,
124 )
125 }
126
127 /// Record a commit from the current index without auto-staging the worktree.
128 ///
129 /// Equivalent to [`Self::create`] with [`CommitScope::Staged`].
130 ///
131 /// # Errors
132 ///
133 /// Same as [`Self::create`].
134 pub fn create_staged(
135 &self,
136 request: &CommitRequest,
137 progress: &mut dyn ProgressSink,
138 cancel: &CancelToken,
139 ) -> Result<CommitOutcome> {
140 self.create(
141 &CommitOptions::new(request.clone(), CommitScope::Staged),
142 progress,
143 cancel,
144 )
145 }
146
147 /// Serialize commit bytes from assembled [`CommitData`](crate::objects::CommitData), honoring signing config.
148 ///
149 /// # Errors
150 ///
151 /// Propagates signing and config failures.
152 pub fn prepare_bytes(
153 &self,
154 data: &crate::objects::CommitData,
155 sign_override: Option<bool>,
156 ) -> Result<Vec<u8>> {
157 prepare_commit_bytes(self.repo, data, sign_override)
158 }
159
160 /// Hash and store a commit object from [`CommitData`](crate::objects::CommitData).
161 ///
162 /// # Errors
163 ///
164 /// Same as [`Self::prepare_bytes`], plus object-database write failures.
165 pub fn write_object(
166 &self,
167 data: &crate::objects::CommitData,
168 sign_override: Option<bool>,
169 ) -> Result<ObjectId> {
170 write_commit_object(self.repo, data, sign_override)
171 }
172}