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
//
use std::path::PathBuf;

use crate::config::{
    git_dir::StoragePathType, BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource,
    RepoPath,
};

/// Defines a Repo within a `ForgeConfig` to be monitored by the server
/// Maps from `git-next-server.toml` at `forge.{forge}.repos.{name}`
#[derive(
    Clone,
    Debug,
    derive_more::From,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    serde::Deserialize,
    derive_more::Display,
    derive_more::Constructor,
)]
#[display("{}@{}", repo, branch)]
pub struct ServerRepoConfig {
    repo: String,
    branch: String,
    gitdir: Option<PathBuf>,
    main: Option<String>,
    next: Option<String>,
    dev: Option<String>,
}
impl ServerRepoConfig {
    pub(crate) fn repo(&self) -> RepoPath {
        RepoPath::new(self.repo.clone())
    }

    pub(crate) fn branch(&self) -> BranchName {
        BranchName::new(&self.branch)
    }

    #[must_use]
    pub fn gitdir(&self) -> Option<GitDir> {
        self.gitdir
            .clone()
            // Provenance is external as the gitdir is only used to specify non-internal paths
            .map(|dir| GitDir::new(dir, StoragePathType::External))
    }

    /// Returns a `RepoConfig` from the server configuration if ALL THREE branches were provided
    pub(crate) fn repo_config(&self) -> Option<RepoConfig> {
        match (&self.main, &self.next, &self.dev) {
            (Some(main), Some(next), Some(dev)) => Some(RepoConfig::new(
                RepoBranches::new(main.to_string(), next.to_string(), dev.to_string()),
                RepoConfigSource::Server,
            )),
            _ => None,
        }
    }
}