Skip to content

Commit 9d1bc87

Browse files
committed
Repository: Use .context() to provide additional error context
1 parent a3749be commit 9d1bc87

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/git.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ impl Repository {
197197
/// - If reading the global git config fails.
198198
///
199199
pub fn open(repository_config: &RepositoryConfig) -> anyhow::Result<Self> {
200-
let checkout_path = tempfile::Builder::new().prefix("git").tempdir()?;
200+
let checkout_path = tempfile::Builder::new()
201+
.prefix("git")
202+
.tempdir()
203+
.context("Failed to create temporary directory")?;
201204

202205
let repository = git2::build::RepoBuilder::new()
203206
.fetch_options(Self::fetch_options(&repository_config.credentials))
@@ -212,13 +215,21 @@ impl Repository {
212215
.clone(
213216
repository_config.index_location.as_str(),
214217
checkout_path.path(),
215-
)?;
218+
)
219+
.context("Failed to clone index repository")?;
216220

217221
// All commits to the index registry made through crates.io will be made by bors, the Rust
218222
// community's friendly GitHub bot.
219-
let mut cfg = repository.config()?;
220-
cfg.set_str("user.name", "bors")?;
221-
cfg.set_str("user.email", "[email protected]")?;
223+
224+
let mut cfg = repository
225+
.config()
226+
.context("Failed to read git configuration")?;
227+
228+
cfg.set_str("user.name", "bors")
229+
.context("Failed to set user name")?;
230+
231+
cfg.set_str("user.email", "[email protected]")
232+
.context("Failed to set user email address")?;
222233

223234
Ok(Self {
224235
checkout_path,
@@ -260,7 +271,9 @@ impl Repository {
260271
/// - If the `HEAD` pointer can't be retrieved.
261272
///
262273
pub fn head_oid(&self) -> anyhow::Result<git2::Oid> {
263-
Ok(self.repository.head()?.target().unwrap())
274+
let repo = &self.repository;
275+
let head = repo.head().context("Failed to read HEAD reference")?;
276+
Ok(head.target().unwrap())
264277
}
265278

266279
/// Commits the specified file with the specified commit message and pushes

0 commit comments

Comments
 (0)