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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Git commit operation with comprehensive options.
//!
//! This module provides the `CommitOpts` builder pattern and commit operation
//! implementation for the `GitGix` service.
use chrono::{DateTime, Utc};
use crate::{CommitId, GitError, GitResult, RepoHandle};
/// Git signature (author/committer) information.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature {
pub name: String,
pub email: String,
pub time: DateTime<Utc>,
}
impl Signature {
/// Create a new signature with current time.
pub fn new<N: Into<String>, E: Into<String>>(name: N, email: E) -> Self {
Self {
name: name.into(),
email: email.into(),
time: Utc::now(),
}
}
/// Create a signature with a specific time.
pub fn with_time<N: Into<String>, E: Into<String>>(
name: N,
email: E,
time: DateTime<Utc>,
) -> Self {
Self {
name: name.into(),
email: email.into(),
time,
}
}
}
impl From<gix::actor::Signature> for Signature {
fn from(sig: gix::actor::Signature) -> Self {
use chrono::TimeZone;
// Convert gix::date::Time to DateTime<Utc>
let timestamp = sig.time.seconds;
let time = Utc
.timestamp_opt(timestamp, 0)
.single()
.unwrap_or_else(Utc::now);
Self {
name: sig.name.to_string(),
email: sig.email.to_string(),
time,
}
}
}
/// Options for `commit` operation with builder pattern.
#[derive(Debug, Clone)]
pub struct CommitOpts {
pub message: String,
pub amend: bool,
pub all: bool,
pub author: Option<Signature>,
pub committer: Option<Signature>,
}
impl CommitOpts {
/// Create new commit options with the given message.
#[inline]
pub fn message<S: Into<String>>(message: S) -> Self {
Self {
message: message.into(),
amend: false,
all: false,
author: None,
committer: None,
}
}
/// Enable amend mode (modify the last commit).
#[must_use]
pub fn amend(mut self, yes: bool) -> Self {
self.amend = yes;
self
}
/// Enable all mode (commit all tracked files automatically).
#[must_use]
pub fn all(mut self, yes: bool) -> Self {
self.all = yes;
self
}
/// Set the author signature.
#[must_use]
pub fn author(mut self, sig: Signature) -> Self {
self.author = Some(sig);
self
}
/// Set the committer signature.
#[must_use]
pub fn committer(mut self, sig: Signature) -> Self {
self.committer = Some(sig);
self
}
}
/// Execute commit operation with the given options.
pub async fn commit(repo: RepoHandle, opts: CommitOpts) -> GitResult<CommitId> {
let repo_clone = repo.clone_inner();
tokio::task::spawn_blocking(move || {
let CommitOpts {
message,
amend,
all,
author,
committer,
} = opts;
if message.trim().is_empty() {
return Err(GitError::InvalidInput(
"Commit message cannot be empty".to_string(),
));
}
// Get current index
let index = repo_clone
.open_index()
.map_err(|e| GitError::Gix(e.into()))?;
// Handle --all option: stage all modified tracked files
let index = if all {
// Get working directory (fail for bare repos)
let workdir = repo_clone.workdir().ok_or_else(|| {
GitError::InvalidInput("Cannot use --all in bare repository".to_string())
})?;
// Shadow index as mutable
let mut index = index;
let mut changed = false;
// Collect entries to process to avoid borrow issues
let entries_to_process: Vec<_> = (0..index.entries().len())
.map(|idx| {
let entry = &index.entries()[idx];
let entry_path = entry.path(&index).to_owned();
let entry_id = entry.id;
(entry_path, entry_id)
})
.collect();
// Process each tracked file
for (entry_path, old_id) in entries_to_process {
// Build full path
use gix::bstr::ByteSlice;
use std::path::Path;
let path_str = std::str::from_utf8(entry_path.as_bytes())
.map_err(|_| GitError::InvalidInput("Invalid UTF-8 in path".to_string()))?;
let full_path = workdir.join(Path::new(path_str));
// Skip non-existent or non-file entries
if !full_path.exists() || !full_path.is_file() {
continue;
}
// Read file contents
let contents = std::fs::read(&full_path)?;
// Write blob and get ID
let blob_id = repo_clone
.write_blob(&contents)
.map_err(|e| GitError::Gix(e.into()))?
.detach();
// Only update if content changed
if blob_id != old_id {
// Get file metadata
let metadata = gix::index::fs::Metadata::from_path_no_follow(&full_path)?;
// Determine mode
use gix::index::entry::Mode;
let mode = if metadata.is_executable() {
Mode::FILE_EXECUTABLE
} else {
Mode::FILE
};
// Create stat
use gix::index::entry::Stat;
let stat = Stat::from_fs(&metadata).map_err(|e| {
GitError::InvalidInput(format!("Failed to create stat: {e}"))
})?;
// Add to index
use gix::index::entry::Flags;
index.dangerously_push_entry(
stat,
blob_id,
Flags::empty(),
mode,
entry_path.as_ref(),
);
changed = true;
}
}
// If we modified the index, write it to disk
if changed {
// CRITICAL: Sort entries to maintain index invariants
index.sort_entries();
// Write index with proper locking and checksum
use gix::index::write::Options;
index
.write(Options::default())
.map_err(|e| GitError::Gix(e.into()))?;
}
// Re-open index for tree building
repo_clone
.open_index()
.map_err(|e| GitError::Gix(e.into()))?
} else {
index
};
// Create tree editor to build hierarchical tree structure
let mut editor = gix::objs::tree::Editor::new(
gix::objs::Tree::empty(),
&repo_clone.objects,
repo_clone.object_hash(),
);
// Add each index entry with path components to build hierarchy
for entry in index.entries() {
if let Some(tree_mode) = entry.mode.to_tree_entry_mode() {
let path = entry.path(&index);
// Split path into components for hierarchical tree building
let components: Vec<&gix::bstr::BStr> = path
.split(|&b| b == b'/')
.map(std::convert::AsRef::as_ref)
.collect();
// Convert tree::EntryMode to EntryKind
let kind = tree_mode.kind();
editor
.upsert(components, kind, entry.id)
.map_err(|e| GitError::Gix(Box::new(e)))?;
}
}
// Write all tree objects and get root tree ID
let tree_id = editor
.write(|tree| {
repo_clone
.write_object(tree)
.map(gix::Id::detach)
.map_err(|e| GitError::Gix(Box::new(e)))
})
.map_err(|e| match e {
GitError::Gix(inner) => GitError::Gix(inner),
_ => GitError::Gix(Box::new(e)),
})?;
// Get current HEAD commit ID
let head_commit_id = repo_clone.head_id().ok();
// Get or create author signature
let author_sig = if let Some(author) = author {
gix::actor::Signature {
name: author.name.as_str().into(),
email: author.email.as_str().into(),
time: gix::date::Time::new(author.time.timestamp(), 0),
}
} else {
// Use config default - convert SignatureRef to owned Signature
let sig_ref = repo_clone
.author()
.ok_or_else(|| GitError::InvalidInput("No author configured".to_string()))?
.map_err(|e| GitError::Gix(Box::new(e)))?;
sig_ref.to_owned().map_err(|e| GitError::Gix(Box::new(e)))?
};
// Get or create committer signature
let committer_sig = if let Some(committer) = committer {
gix::actor::Signature {
name: committer.name.as_str().into(),
email: committer.email.as_str().into(),
time: gix::date::Time::new(committer.time.timestamp(), 0),
}
} else {
// Use config default or author
match repo_clone.committer() {
Some(Ok(sig_ref)) => sig_ref.to_owned().map_err(|e| GitError::Gix(Box::new(e)))?,
Some(Err(e)) => return Err(GitError::Gix(Box::new(e))),
None => author_sig.clone(),
}
};
// Determine parents based on amend flag
let parents = if amend {
// For amend: use HEAD's parents (replace HEAD)
if let Some(head_id) = head_commit_id {
// Get HEAD commit object using pattern from checkout.rs
let head_commit = repo_clone
.find_object(head_id)
.map_err(|e| GitError::Gix(e.into()))?
.try_into_commit()
.map_err(|_| {
GitError::InvalidInput("HEAD does not point to a commit".to_string())
})?;
// Get HEAD's parent IDs
head_commit
.parent_ids()
.map(gix::Id::detach)
.collect::<Vec<_>>()
} else {
// No HEAD exists - cannot amend
return Err(GitError::InvalidInput(
"Cannot amend: no commits exist yet".to_string(),
));
}
} else {
// Normal commit: use HEAD as parent
head_commit_id
.into_iter()
.map(gix::Id::detach)
.collect::<Vec<_>>()
};
// Create time buffers for signature conversion
use gix::date::parse::TimeBuf;
let mut committer_time_buf = TimeBuf::default();
let mut author_time_buf = TimeBuf::default();
let commit_id = repo_clone
.commit_as(
committer_sig.to_ref(&mut committer_time_buf),
author_sig.to_ref(&mut author_time_buf),
"HEAD",
&message,
tree_id,
parents,
)
.map_err(|e| GitError::Gix(e.into()))?;
Ok(commit_id.detach())
})
.await
.map_err(|e| GitError::InvalidInput(format!("Task join error: {e}")))?
}