summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@atos.net>2021-03-01 11:38:42 +0100
committerMatthias Beyer <mail@beyermatthias.de>2021-03-01 17:42:26 +0100
commit6387e8f333aa9f5b65a5ebb8ce3d8ebeaa0fa58c (patch)
tree20796512435300b6dd3b2c6ee0a3a41631f2cc60
parent18d06cc867fb3c7c10b7494b96d213aa62fa9927 (diff)
Refactor: Open git repository in command::build()
This commit refactors the git2::Repository::open() call to be in the command::build() implementation, so we do not open the repository multiple times. Signed-off-by: Matthias Beyer <matthias.beyer@atos.net>
-rw-r--r--src/commands/build.rs7
-rw-r--r--src/ui.rs8
-rw-r--r--src/util/git.rs13
3 files changed, 12 insertions, 16 deletions
diff --git a/src/commands/build.rs b/src/commands/build.rs
index 1044896..4474d4e 100644
--- a/src/commands/build.rs
+++ b/src/commands/build.rs
@@ -61,7 +61,10 @@ pub async fn build(
) -> Result<()> {
use crate::db::models::{EnvVar, GitHash, Image, Job, Package, Submit};
- let _ = crate::ui::package_repo_cleanness_check(&repo_root)?;
+ let git_repo = git2::Repository::open(repo_path)
+ .with_context(|| anyhow!("Opening repository at {}", repo_path.display()))?;
+
+ let _ = crate::ui::package_repo_cleanness_check(&git_repo)?;
let now = chrono::offset::Local::now().naive_local();
let shebang = Shebang::from({
@@ -92,7 +95,7 @@ pub async fn build(
}
debug!("Getting repository HEAD");
- let hash_str = crate::util::git::get_repo_head_commit_hash(repo_path)?;
+ let hash_str = crate::util::git::get_repo_head_commit_hash(&git_repo)?;
trace!("Repository HEAD = {}", hash_str);
let phases = config.available_phases();
diff --git a/src/ui.rs b/src/ui.rs
index d7c5489..fda8f5b 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -28,15 +28,15 @@ use crate::package::Script;
use crate::package::ScriptBuilder;
use crate::package::Shebang;
-pub fn package_repo_cleanness_check(repo_path: &Path) -> Result<()> {
- if !crate::util::git::repo_is_clean(&repo_path)? {
+pub fn package_repo_cleanness_check(repo: &git2::Repository) -> Result<()> {
+ if !crate::util::git::repo_is_clean(&repo)? {
error!(
"Repository not clean, refusing to go on: {}",
- repo_path.display()
+ repo.path().display()
);
Err(anyhow!(
"Repository not clean, refusing to go on: {}",
- repo_path.display()
+ repo.path().display()
))
} else {
Ok(())
diff --git a/src/util/git.rs b/src/util/git.rs
index 628e511..10ac7fd 100644
--- a/src/util/git.rs
+++ b/src/util/git.rs
@@ -8,8 +8,6 @@
// SPDX-License-Identifier: EPL-2.0
//
-use std::path::Path;
-
use anyhow::anyhow;
use anyhow::Context;
use anyhow::Error;
@@ -17,9 +15,7 @@ use anyhow::Result;
use git2::Repository;
use log::trace;
-pub fn repo_is_clean(p: &Path) -> Result<bool> {
- let r = Repository::open(p)?;
-
+pub fn repo_is_clean(r: &Repository) -> Result<bool> {
r.diff_index_to_workdir(None, None)
.and_then(|d| d.stats())
.map_err(Error::from)
@@ -30,13 +26,10 @@ pub fn repo_is_clean(p: &Path) -> Result<bool> {
})
}
-pub fn get_repo_head_commit_hash(p: &Path) -> Result<String> {
- let r =
- Repository::open(p).with_context(|| anyhow!("Opening repository at {}", p.display()))?;
-
+pub fn get_repo_head_commit_hash(r: &Repository) -> Result<String> {
let s = r
.head()
- .with_context(|| anyhow!("Getting HEAD from repository at {}", p.display()))?
+ .with_context(|| anyhow!("Getting HEAD from repository at {}", r.path().display()))?
.peel_to_commit()
.with_context(|| anyhow!("Failed to get commit hash: Not valid UTF8"))?
.id()