use std::path::Path;
use serde::Serialize;
use super::constants::GIT_REMOTE_UPSTREAM;
use super::discovery;
use crate::config;
mod history;
mod push;
mod remote;
mod workflow;
pub(crate) use history::get_first_commit;
pub(crate) use push::PushDisabledReason;
pub(crate) use push::PushState;
pub(crate) use remote::GitOrigin;
pub(crate) use remote::RemoteInfo;
pub(crate) use remote::RemoteKind;
use remote::RemoteResolveContext;
use remote::UpstreamRemote;
use remote::list_remote_names;
pub(crate) use workflow::WorkflowPresence;
use super::branches;
#[derive(Debug, Clone, Default, Serialize)]
pub(crate) struct RepoInfo {
pub remotes: Vec<RemoteInfo>,
pub workflows: WorkflowPresence,
pub first_commit: Option<String>,
pub last_fetched: Option<String>,
pub default_branch: Option<String>,
pub local_main_branch: Option<String>,
}
impl RepoInfo {
pub fn origin_kind(&self) -> GitOrigin {
if self.remotes.is_empty() {
GitOrigin::Local
} else if self.remotes.iter().any(|r| r.name == GIT_REMOTE_UPSTREAM) {
GitOrigin::Fork
} else {
GitOrigin::Clone
}
}
pub fn get(probe_path: &Path) -> Option<Self> {
let repo_root = discovery::git_repo_root(probe_path)?;
let active_config = config::active_config();
let branch = branches::get_current_branch(&repo_root);
let current_upstream = branches::get_upstream_branch(&repo_root);
let default_branch = branches::get_default_branch(&repo_root);
let local_main_branch = branches::resolve_local_main_branch(&repo_root);
let remote_names = list_remote_names(&repo_root);
let upstream_remote = UpstreamRemote::from_remote_names(&remote_names);
let pushurls = push::list_remote_pushurls(&repo_root);
let remote_context = RemoteResolveContext {
repo_root: &repo_root,
upstream_remote,
current_upstream: current_upstream.as_deref(),
default_branch: default_branch.as_deref(),
current_branch: branch.as_deref(),
config: &active_config,
};
let remotes: Vec<RemoteInfo> = remote_names
.iter()
.map(|name| {
remote::build_remote_info(
&remote_context,
name,
pushurls.get(name.as_str()).map(String::as_str),
)
})
.collect();
Some(Self {
remotes,
workflows: workflow::get_workflow_presence(&repo_root),
first_commit: None,
last_fetched: history::get_last_fetched(&repo_root),
default_branch,
local_main_branch,
})
}
}