Skip to main content

testcontainers_build/sources/
git_source.rs

1use crate::{BuildContext, BuildContextProvider, GitCliProvider, GitProvider, GitRequest, Result};
2use async_trait::async_trait;
3
4/// Resolve a Git request with a Git provider.
5/// Keep the provider separate from image build execution.
6pub struct GitSource {
7    /// Set the source revision and location.
8    /// The provider receives this request.
9    pub request: GitRequest,
10    /// Set the Git provider.
11    /// It owns checkout behavior.
12    pub provider: Box<dyn GitProvider>,
13}
14
15impl GitSource {
16    /// Create a source with the Git CLI provider.
17    /// Use the request to select the revision.
18    pub fn new(request: GitRequest) -> Self {
19        Self {
20            request,
21            provider: Box::new(GitCliProvider::default()),
22        }
23    }
24
25    /// Create a source with a custom Git provider.
26    /// Use the request to select the revision.
27    pub fn with_provider(request: GitRequest, provider: impl GitProvider + 'static) -> Self {
28        Self {
29            request,
30            provider: Box::new(provider),
31        }
32    }
33}
34
35#[async_trait]
36impl BuildContextProvider for GitSource {
37    async fn resolve(&self) -> Result<BuildContext> {
38        self.provider.checkout(&self.request).await
39    }
40}