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
//! Git bundle create/verify/list/unbundle scoped to a [`Repository`](crate::repo::Repository).
use std::path::Path;
use crate::bundle::bundle_remote::BundleCreateOptions;
use crate::bundle::{
plan_bundle_create, plan_bundle_unbundle, Bundle, BundleCreatePlan, BundleError, BundleSpec,
BundleUnbundlePlan, BundleVerifyReport, UnbundleOutcome, UnbundledRef,
};
use crate::list_limit::Limited;
use crate::objects::ObjectId;
use crate::progress::{NullProgress, ProgressSink};
use crate::repo::Repository;
/// One ref tip from a bundle header (`git bundle list-heads`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BundleHead {
/// Full ref name from the bundle header.
pub name: String,
/// Tip object id.
pub oid: ObjectId,
}
/// Bundle file operations for one open repository.
#[derive(Debug, Copy, Clone)]
pub struct RepositoryBundles<'repo> {
repo: &'repo Repository,
}
impl<'repo> RepositoryBundles<'repo> {
pub(crate) fn new(repo: &'repo Repository) -> Self {
Self { repo }
}
/// Borrow the underlying repository.
#[must_use]
pub fn repository(&self) -> &'repo Repository {
self.repo
}
/// Plan bundle creation for `spec` without writing a file.
///
/// # Errors
///
/// Returns [`BundleError::EmptyRefs`] when `spec.include` is empty, or pack generation fails.
pub fn plan_create(
&self,
spec: &BundleSpec,
) -> std::result::Result<BundleCreatePlan, BundleError> {
plan_bundle_create(self.repo, spec)
}
/// Plan bundle creation from CLI-style revision options.
///
/// # Errors
///
/// Propagates rev-list, ref resolution, and pack failures.
pub fn plan_create_with_options(
&self,
options: &BundleCreateOptions,
) -> std::result::Result<BundleCreatePlan, BundleError> {
let spec = crate::bundle_remote::resolve_bundle_spec(self.repo, options)
.map_err(lib_error_to_bundle_error)?;
plan_bundle_create(self.repo, &spec)
}
/// Plan unbundle verification and ref updates without ingesting the pack.
///
/// # Errors
///
/// Returns [`BundleError`] when the bundle file is invalid.
pub fn plan_unbundle(
&self,
path: &Path,
) -> std::result::Result<BundleUnbundlePlan, BundleError> {
let bundle = Bundle::open(path)?;
plan_bundle_unbundle(&bundle, self.repo)
}
/// Write a bundle to `path` using `spec`.
///
/// Replaces an existing file at `path` only after the pack stream completes successfully
/// (writes to a sibling `*.bundle.part` first). On failure the previous file is unchanged.
///
/// # Errors
///
/// Returns [`BundleError`] when `spec.include` is empty or pack generation fails.
///
/// # Examples
///
/// ```
/// # use grit_lib::bundle::BundleSpec;
/// # use grit_lib::objects::ObjectId;
/// # use grit_lib::repo::Repository;
/// # use std::path::Path;
/// # fn demo() -> Result<(), Box<dyn std::error::Error>> {
/// let repo = Repository::discover(Some(Path::new(".")), &grit_lib::config::environment::RepositoryOptions::empty())?;
/// let spec = BundleSpec {
/// include: vec![(ObjectId::zero(), "refs/heads/main".into())],
/// ..Default::default()
/// };
/// let _ = repo.bundles().create(&spec, Path::new("/tmp/x.bundle"));
/// # Ok(())
/// # }
/// ```
pub fn create(&self, spec: &BundleSpec, path: &Path) -> std::result::Result<(), BundleError> {
self.create_with_progress(spec, path, &mut NullProgress, &crate::progress::NeverCancel)?;
Ok(())
}
/// Like [`Self::create`], reporting pack enumeration and copy progress through `progress`.
///
/// Returns the number of ref tips written to the bundle header.
///
/// # Errors
///
/// Same as [`Self::create`], plus [`BundleError::Cancelled`].
pub fn create_with_progress(
&self,
spec: &BundleSpec,
path: &Path,
progress: &mut dyn ProgressSink,
cancel: &dyn crate::progress::Cancel,
) -> std::result::Result<usize, BundleError> {
crate::bundle_remote::write_bundle_spec_to_path_with_cancel(
self.repo, spec, path, progress, cancel,
)
}
/// Write a bundle from CLI-style [`BundleCreateOptions`].
///
/// # Errors
///
/// Propagates rev-list, ref resolution, and pack failures.
pub fn create_with_options(
&self,
options: &BundleCreateOptions,
path: &Path,
progress: &mut dyn ProgressSink,
cancel: &dyn crate::progress::Cancel,
) -> std::result::Result<usize, BundleError> {
let spec = crate::bundle_remote::resolve_bundle_spec(self.repo, options)
.map_err(lib_error_to_bundle_error)?;
self.create_with_progress(&spec, path, progress, cancel)
}
/// Open and verify a bundle against this repository's object format and prerequisites.
///
/// # Errors
///
/// Returns [`BundleError`] when the file is invalid or hash algorithms disagree.
///
/// # Examples
///
/// ```
/// # use grit_lib::repo::Repository;
/// # use std::path::Path;
/// # fn demo() -> Result<(), Box<dyn std::error::Error>> {
/// let repo = Repository::discover(Some(Path::new(".")), &grit_lib::config::environment::RepositoryOptions::empty())?;
/// let report = repo.bundles().verify(Path::new("changes.bundle"))?;
/// assert!(report.is_ok() || !report.missing_prerequisites.is_empty());
/// # Ok(())
/// # }
/// ```
pub fn verify(&self, path: &Path) -> std::result::Result<BundleVerifyReport, BundleError> {
Bundle::open(path)?.verify(self.repo)
}
/// Parse bundle header ref lines without ingesting the pack.
///
/// # Errors
///
/// Returns [`BundleError`] when the signature or header is malformed.
pub fn list_heads(
&self,
path: &Path,
limit: Option<usize>,
) -> std::result::Result<Limited<BundleHead>, BundleError> {
let bundle = Bundle::open(path)?;
let heads: Vec<BundleHead> = bundle
.header()
.refs
.iter()
.map(|(name, oid)| BundleHead {
name: name.clone(),
oid: *oid,
})
.collect();
Ok(Limited::from_vec(heads, limit))
}
/// Ingest the pack stream from `path` into this repository (refs are not updated).
///
/// # Errors
///
/// Returns [`BundleError`] or ingest failures.
pub fn unbundle(&self, path: &Path) -> std::result::Result<UnbundleOutcome, BundleError> {
Bundle::open(path)?.unbundle_with_progress(
self.repo,
&mut NullProgress,
&crate::progress::NeverCancel,
)
}
/// Like [`Self::unbundle`], reporting progress through `progress`.
pub fn unbundle_with_progress(
&self,
path: &Path,
progress: &mut dyn ProgressSink,
cancel: &dyn crate::progress::Cancel,
) -> std::result::Result<UnbundleOutcome, BundleError> {
Bundle::open(path)?.unbundle_with_progress(self.repo, progress, cancel)
}
/// Open a bundle file handle for repeated operations.
pub fn open(&self, path: impl AsRef<Path>) -> std::result::Result<Bundle, BundleError> {
Bundle::open(path)
}
}
fn lib_error_to_bundle_error(err: crate::error::Error) -> BundleError {
match err {
crate::error::Error::BundleMissingPrerequisites => BundleError::PrerequisitesNotConnected,
crate::error::Error::ObjectNotFound(id) => BundleError::ObjectNotFound(id),
other => BundleError::Io(other.to_string()),
}
}
impl From<UnbundledRef> for BundleHead {
fn from(value: UnbundledRef) -> Self {
Self {
name: value.name,
oid: value.oid,
}
}
}