forked from kemitix/git-next
refactor: get commit from from local repo (part 2/3)
Remove the legacy implementation
This commit is contained in:
parent
e62f5e2319
commit
8a35911d00
3 changed files with 13 additions and 155 deletions
|
@ -4,24 +4,22 @@ use git::ForgeLike as _;
|
|||
use git_next_config as config;
|
||||
use git_next_git as git;
|
||||
|
||||
use kxio::network;
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
pub async fn validate_positions(
|
||||
#[allow(clippy::cognitive_complexity)] // TODO: (#83) reduce complexity
|
||||
pub fn validate_positions(
|
||||
forge: &forgejo::ForgeJo,
|
||||
repository: &git::OpenRepository,
|
||||
repo_config: config::RepoConfig,
|
||||
) -> git::validation::Result {
|
||||
let repo_details = &forge.repo_details;
|
||||
// Collect Commit Histories for `main`, `next` and `dev` branches
|
||||
repository.fetch()?;
|
||||
let commit_histories =
|
||||
get_commit_histories(repository, repo_details, &repo_config, &forge.net).await;
|
||||
let commit_histories = get_commit_histories(repository, &repo_config);
|
||||
let commit_histories = match commit_histories {
|
||||
Ok(commit_histories) => commit_histories,
|
||||
Err(err) => {
|
||||
error!(?err, "Failed to get commit histories");
|
||||
return Err(git::validation::Error::Network(Box::new(err)));
|
||||
return Err(git::validation::Error::CommitLog(err));
|
||||
}
|
||||
};
|
||||
// Validations
|
||||
|
@ -145,37 +143,14 @@ pub async fn validate_positions(
|
|||
})
|
||||
}
|
||||
|
||||
async fn get_commit_histories(
|
||||
fn get_commit_histories(
|
||||
repository: &git::repository::OpenRepository,
|
||||
repo_details: &git::RepoDetails,
|
||||
repo_config: &config::RepoConfig,
|
||||
net: &network::Network,
|
||||
) -> Result<git::commit::Histories, network::NetworkError> {
|
||||
let main = (get_commit_history(
|
||||
repository,
|
||||
repo_details,
|
||||
&repo_config.branches().main(),
|
||||
&[],
|
||||
net,
|
||||
)
|
||||
.await)?;
|
||||
let main_head = main[0].clone();
|
||||
let next = (get_commit_history(
|
||||
repository,
|
||||
repo_details,
|
||||
&repo_config.branches().next(),
|
||||
&[main_head.clone()],
|
||||
net,
|
||||
)
|
||||
.await)?;
|
||||
let dev = (get_commit_history(
|
||||
repository,
|
||||
repo_details,
|
||||
&repo_config.branches().dev(),
|
||||
&[main_head],
|
||||
net,
|
||||
)
|
||||
.await)?;
|
||||
) -> Result<git::commit::Histories, git::commit::log::Error> {
|
||||
let main = (repository.commit_log(&repo_config.branches().main(), &[]))?;
|
||||
let main_head = [main[0].clone()];
|
||||
let next = repository.commit_log(&repo_config.branches().next(), &main_head)?;
|
||||
let dev = repository.commit_log(&repo_config.branches().dev(), &main_head)?;
|
||||
debug!(
|
||||
main = main.len(),
|
||||
next = next.len(),
|
||||
|
@ -185,120 +160,3 @@ async fn get_commit_histories(
|
|||
let histories = git::commit::Histories { main, next, dev };
|
||||
Ok(histories)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all, fields(%branch_name, ?find_commits))]
|
||||
async fn get_commit_history(
|
||||
repository: &git::repository::OpenRepository,
|
||||
repo_details: &git::RepoDetails,
|
||||
branch_name: &config::BranchName,
|
||||
find_commits: &[git::Commit],
|
||||
net: &kxio::network::Network,
|
||||
) -> Result<Vec<git::Commit>, network::NetworkError> {
|
||||
let original = original_get_commit_history(repo_details, branch_name, find_commits, net).await;
|
||||
let updated = repository.commit_log(branch_name, find_commits);
|
||||
match (original, updated) {
|
||||
(Ok(original), Ok(updated)) => {
|
||||
if updated == original {
|
||||
info!("new version matches");
|
||||
Ok(updated)
|
||||
} else {
|
||||
error!("new version doesn't match original");
|
||||
Ok(original)
|
||||
}
|
||||
}
|
||||
(Ok(original), Err(err_updated)) => {
|
||||
warn!(?err_updated, "original ok, updated error");
|
||||
Ok(original)
|
||||
}
|
||||
(Err(err_original), Ok(updated)) => {
|
||||
warn!(?err_original, "original err, updated ok");
|
||||
Ok(updated)
|
||||
}
|
||||
(Err(err_orignal), Err(err_updated)) => {
|
||||
error!(?err_orignal, ?err_updated, "original err, updated err");
|
||||
Err(err_orignal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(fields(%branch_name),skip_all)]
|
||||
async fn original_get_commit_history(
|
||||
repo_details: &git::RepoDetails,
|
||||
branch_name: &config::BranchName,
|
||||
find_commits: &[git::Commit],
|
||||
net: &kxio::network::Network,
|
||||
) -> Result<Vec<git::Commit>, network::NetworkError> {
|
||||
let hostname = &repo_details.forge.hostname();
|
||||
let repo_path = &repo_details.repo_path;
|
||||
|
||||
let mut page = 1;
|
||||
let limit = match find_commits.is_empty() {
|
||||
true => 1,
|
||||
false => 50,
|
||||
};
|
||||
let options = "stat=false&verification=false&files=false";
|
||||
let mut all_commits = Vec::new();
|
||||
loop {
|
||||
let api_token = &repo_details.forge.token();
|
||||
use secrecy::ExposeSecret;
|
||||
let token = api_token.expose_secret();
|
||||
let url = network::NetUrl::new(format!(
|
||||
"https://{hostname}/api/v1/repos/{repo_path}/commits?sha={branch_name}&{options}&token={token}&page={page}&limit={limit}"
|
||||
));
|
||||
|
||||
let request = network::NetRequest::new(
|
||||
network::RequestMethod::Get,
|
||||
url,
|
||||
network::NetRequestHeaders::new(),
|
||||
network::RequestBody::None,
|
||||
network::ResponseType::Json,
|
||||
None,
|
||||
network::NetRequestLogging::None,
|
||||
);
|
||||
let response = net.get::<Vec<Commit>>(request).await?;
|
||||
let commits = response
|
||||
.response_body()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(git::Commit::from)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let found = find_commits.is_empty()
|
||||
|| find_commits
|
||||
.iter()
|
||||
.any(|find_commit| commits.iter().any(|commit| commit == find_commit));
|
||||
let at_end = commits.len() < limit; // i.e. less than the number of commits requested
|
||||
for commit in commits {
|
||||
all_commits.push(commit.clone());
|
||||
if find_commits.contains(&commit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if found || at_end {
|
||||
break;
|
||||
}
|
||||
page += 1;
|
||||
}
|
||||
|
||||
Ok(all_commits)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Default, serde::Deserialize)]
|
||||
struct Commit {
|
||||
sha: String,
|
||||
commit: RepoCommit,
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Default, serde::Deserialize)]
|
||||
struct RepoCommit {
|
||||
message: String,
|
||||
}
|
||||
impl From<Commit> for git::Commit {
|
||||
fn from(value: Commit) -> Self {
|
||||
Self::new(
|
||||
git::commit::Sha::new(value.sha),
|
||||
git::commit::Message::new(value.commit.message),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ impl git::ForgeLike for ForgeJo {
|
|||
repository: git::OpenRepository,
|
||||
repo_config: config::RepoConfig,
|
||||
) -> git::validation::Result {
|
||||
branch::validate_positions(self, &repository, repo_config).await
|
||||
branch::validate_positions(self, &repository, repo_config)
|
||||
}
|
||||
|
||||
fn branch_reset(
|
||||
|
|
|
@ -12,10 +12,10 @@ pub struct Positions {
|
|||
|
||||
#[derive(Debug, derive_more::Display)]
|
||||
pub enum Error {
|
||||
Network(Box<kxio::network::NetworkError>),
|
||||
|
||||
Fetch(git::fetch::Error),
|
||||
|
||||
CommitLog(git::commit::log::Error),
|
||||
|
||||
#[display("Failed to Reset Branch {branch} to {commit}")]
|
||||
FailedToResetBranch {
|
||||
branch: BranchName,
|
||||
|
|
Loading…
Reference in a new issue