1use std::path::Path;
3
4use crate::error::Result;
5use crate::index::{Index, IndexParseContext};
6use crate::repo::Repository;
7use crate::split_index::WriteSplitIndexRequest;
8
9#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum SparseIndexLoad {
13 #[default]
15 Expand,
16 Preserve,
19}
20
21#[derive(Debug, Clone, Copy, Default)]
23#[non_exhaustive]
24pub struct IndexLoadOptions<'a> {
25 pub path: Option<&'a Path>,
27 pub sparse: SparseIndexLoad,
29 pub parse_context: Option<IndexParseContext>,
31}
32
33impl<'a> IndexLoadOptions<'a> {
34 #[must_use]
36 pub fn with_path(mut self, path: &'a Path) -> Self {
37 self.path = Some(path);
38 self
39 }
40
41 #[must_use]
43 pub fn with_sparse(mut self, sparse: SparseIndexLoad) -> Self {
44 self.sparse = sparse;
45 self
46 }
47
48 #[must_use]
50 pub fn with_parse_context(mut self, parse_context: IndexParseContext) -> Self {
51 self.parse_context = Some(parse_context);
52 self
53 }
54}
55
56#[derive(Debug, Clone, Copy, Default)]
58#[non_exhaustive]
59pub struct IndexWriteOptions<'a> {
60 pub path: Option<&'a Path>,
62 pub split: WriteSplitIndexRequest,
64}
65
66impl<'a> IndexWriteOptions<'a> {
67 #[must_use]
69 pub fn with_path(mut self, path: &'a Path) -> Self {
70 self.path = Some(path);
71 self
72 }
73
74 #[must_use]
76 pub fn with_split(mut self, split: WriteSplitIndexRequest) -> Self {
77 self.split = split;
78 self
79 }
80}
81
82#[derive(Debug, Copy, Clone)]
100pub struct IndexFile<'repo> {
101 repo: &'repo Repository,
102}
103
104impl<'repo> IndexFile<'repo> {
105 #[inline]
107 #[must_use]
108 pub fn new(repo: &'repo Repository) -> Self {
109 Self { repo }
110 }
111
112 #[inline]
114 #[must_use]
115 pub fn repository(&self) -> &'repo Repository {
116 self.repo
117 }
118
119 #[inline]
121 #[must_use]
122 pub fn path(&self) -> std::path::PathBuf {
123 self.repo.index_path()
124 }
125
126 #[inline]
132 pub fn path_for_env(&self) -> Result<std::path::PathBuf> {
133 self.repo.index_path_for_env()
134 }
135
136 pub fn load(&self, options: &IndexLoadOptions<'_>) -> crate::error::Result<Index> {
145 let path = match options.path {
146 Some(path) => path.to_path_buf(),
147 None => self.repo.index_path_for_env()?,
148 };
149 let parse_ctx = options
150 .parse_context
151 .unwrap_or_else(|| IndexParseContext::from_policy(self.repo.thread_policy()));
152 match options.sparse {
153 SparseIndexLoad::Expand => self.repo.load_index_at_with_context(&path, parse_ctx),
154 SparseIndexLoad::Preserve => {
155 self.repo.load_index_at_preserving_sparse(&path, parse_ctx)
156 }
157 }
158 }
159
160 pub fn write(
171 &self,
172 index: &mut Index,
173 options: &IndexWriteOptions<'_>,
174 ) -> crate::error::Result<()> {
175 let path = match options.path {
176 Some(path) => path.to_path_buf(),
177 None => self.repo.index_path_for_env()?,
178 };
179 self.repo.write_index_at_split(&path, index, options.split)
180 }
181
182 pub fn apply_sparse_checkout(&self, index: &mut Index) -> Result<Vec<String>> {
197 let config = self.repo.config().snapshot()?;
198 Ok(crate::sparse_checkout::apply_sparse_checkout_with_config(
199 self.repo.git_dir(),
200 self.repo.work_tree(),
201 index,
202 config.as_ref(),
203 ))
204 }
205
206 #[inline]
208 pub fn begin_update(&self) -> crate::error::Result<crate::index::IndexLock> {
209 self.repo.begin_index_update()
210 }
211
212 #[inline]
214 pub fn commit_update(
215 &self,
216 lock: &mut crate::index::IndexLock,
217 index: &mut Index,
218 updated_workdir: bool,
219 ) -> crate::error::Result<()> {
220 self.repo.commit_index_update(lock, index, updated_workdir)
221 }
222
223 #[inline]
226 pub fn try_write(&self, index: &mut Index) -> crate::error::Result<bool> {
227 self.repo.try_write_index(index)
228 }
229
230 pub fn stage(
237 &self,
238 options: &crate::porcelain::add::StageOptions,
239 progress: &mut dyn crate::progress::ProgressSink,
240 cancel: &crate::progress::CancelToken,
241 ) -> crate::error::Result<crate::porcelain::add::StageOutcome> {
242 crate::porcelain::add::stage(self.repo, options, progress, cancel)
243 }
244
245 pub fn stage_for_commit(
251 &self,
252 progress: &mut dyn crate::progress::ProgressSink,
253 cancel: &crate::progress::CancelToken,
254 ) -> crate::error::Result<crate::porcelain::add::StageOutcome> {
255 crate::porcelain::add::stage_for_commit(self.repo, progress, cancel)
256 }
257
258 pub fn stage_on(
264 &self,
265 index: &mut Index,
266 options: &crate::porcelain::add::StageOptions,
267 progress: &mut dyn crate::progress::ProgressSink,
268 cancel: &crate::progress::CancelToken,
269 ) -> crate::error::Result<crate::porcelain::add::StageOutcome> {
270 crate::porcelain::add::stage_on_index(self.repo, index, options, progress, cancel)
271 }
272
273 pub fn stage_worktree_paths(
279 &self,
280 pathspecs: &[String],
281 ) -> crate::error::Result<crate::porcelain::staging::StageWorktreeChangesOutcome> {
282 crate::porcelain::staging::stage_worktree_changes(self.repo, pathspecs)
283 }
284
285 pub fn stage_tracked_modifications(
291 &self,
292 work_tree: &Path,
293 ) -> crate::error::Result<crate::porcelain::stage_tracked::StageTrackedSummary> {
294 let thresholds =
295 self.staging_thresholds_from_options(&crate::porcelain::add::StageOptions::default());
296 crate::porcelain::stage_tracked::stage_tracked_modifications_with_thresholds(
297 self.repo, work_tree, thresholds,
298 )
299 }
300
301 pub fn stage_tracked_modifications_on(
307 &self,
308 work_tree: &Path,
309 index_path: &Path,
310 index: &mut Index,
311 options: &crate::porcelain::add::StageOptions,
312 ) -> crate::error::Result<crate::porcelain::stage_tracked::StageTrackedSummary> {
313 let thresholds = self.staging_thresholds_from_options(options);
314 crate::porcelain::stage_tracked::stage_tracked_modifications_in_index_with_thresholds(
315 self.repo, work_tree, index_path, index, thresholds,
316 )
317 }
318
319 fn staging_thresholds_from_options(
320 &self,
321 options: &crate::porcelain::add::StageOptions,
322 ) -> crate::porcelain::bulk_index::IndexStagingThresholds {
323 #[cfg(any(test, feature = "test-support"))]
324 {
325 options.staging_thresholds.unwrap_or_default()
326 }
327 #[cfg(not(any(test, feature = "test-support")))]
328 {
329 options.staging_thresholds.unwrap_or_default()
330 }
331 }
332
333 pub fn stage_hunks(
339 &self,
340 side: crate::diff::DiffSide,
341 selection: &crate::porcelain::hunk_select::HunkSelection,
342 pathspecs: &[String],
343 diff_options: crate::diff::DiffOptions,
344 dry_run: bool,
345 cancel: &crate::progress::CancelToken,
346 ) -> crate::error::Result<crate::porcelain::hunk_select::StageHunksOutcome> {
347 crate::porcelain::hunk_select::stage_hunks(
348 self.repo,
349 side,
350 selection,
351 pathspecs,
352 diff_options,
353 dry_run,
354 cancel,
355 )
356 }
357
358 pub fn restore_hunks(
364 &self,
365 selection: &crate::porcelain::hunk_select::HunkSelection,
366 pathspecs: &[String],
367 restore_staged: bool,
368 restore_worktree: bool,
369 dry_run: bool,
370 ) -> crate::error::Result<crate::porcelain::hunk_select::RestoreHunksOutcome> {
371 crate::porcelain::hunk_select::restore_hunks(
372 self.repo,
373 selection,
374 pathspecs,
375 restore_staged,
376 restore_worktree,
377 dry_run,
378 )
379 }
380
381 pub fn hunk_diff_options(&self) -> crate::error::Result<crate::diff::DiffOptions> {
387 crate::porcelain::hunk_select::diff_options_for_hunk_select(self.repo)
388 }
389
390 pub fn unstaged_file_diffs(
396 &self,
397 pathspecs: &[String],
398 ) -> crate::error::Result<
399 Vec<(
400 crate::diff::FileDiff,
401 Vec<crate::diff::structured::StructuredHunk>,
402 )>,
403 > {
404 crate::porcelain::hunk_select::unstaged_file_diffs(self.repo, pathspecs)
405 }
406
407 pub fn list_unstaged_hunk_ids(
413 &self,
414 pathspecs: &[String],
415 diff_options: crate::diff::DiffOptions,
416 ) -> crate::error::Result<std::collections::BTreeMap<String, Vec<crate::diff::HunkId>>> {
417 crate::porcelain::hunk_select::list_unstaged_hunk_ids(self.repo, pathspecs, diff_options)
418 }
419
420 pub fn resolve_hunk_selection(
426 &self,
427 side: crate::diff::DiffSide,
428 selection: &crate::porcelain::hunk_select::HunkSelection,
429 pathspecs: &[String],
430 diff_options: crate::diff::DiffOptions,
431 ) -> crate::error::Result<Vec<crate::apply::FilePatch>> {
432 crate::porcelain::hunk_select::resolve_selection(
433 self.repo,
434 side,
435 selection,
436 pathspecs,
437 diff_options,
438 )
439 }
440
441 pub fn pathspec_matches_known(&self, resolved: &str) -> crate::error::Result<bool> {
447 crate::porcelain::pathspec_match::pathspec_matches_known_in_repository(self.repo, resolved)
448 }
449
450 pub fn ensure_pathspec_matches_known(
456 &self,
457 display_spec: &str,
458 resolved: &str,
459 ) -> crate::error::Result<()> {
460 crate::porcelain::pathspec_match::ensure_pathspec_matches_known_in_repository(
461 self.repo,
462 display_spec,
463 resolved,
464 )
465 }
466}