Move a bunch of git ops to use the git binary (#302)

This commit is contained in:
Gregory Schier
2025-11-17 15:22:39 -08:00
committed by GitHub
parent 84219571e8
commit 9c52652a5e
43 changed files with 1238 additions and 1176 deletions

View File

@@ -1,54 +1,100 @@
use crate::callbacks::default_callbacks;
use crate::error::Error::NoActiveBranch;
use crate::binary::new_binary_command;
use crate::error::Error::GenericError;
use crate::error::Result;
use crate::merge::do_merge;
use crate::repository::open_repo;
use crate::util::{bytes_to_string, get_current_branch};
use git2::{FetchOptions, ProxyOptions};
use log::debug;
use crate::util::{get_current_branch_name, get_default_remote_in_repo};
use log::info;
use serde::{Deserialize, Serialize};
use std::path::Path;
use ts_rs::TS;
#[derive(Debug, Clone, Serialize, Deserialize, TS, PartialEq)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[serde(rename_all = "snake_case", tag = "type")]
#[ts(export, export_to = "gen_git.ts")]
pub(crate) struct PullResult {
received_bytes: usize,
received_objects: usize,
pub(crate) enum PullResult {
Success { message: String },
UpToDate,
NeedsCredentials { url: String, error: Option<String> },
}
pub(crate) fn git_pull(dir: &Path) -> Result<PullResult> {
let repo = open_repo(dir)?;
let branch_name = get_current_branch_name(&repo)?;
let remote = get_default_remote_in_repo(&repo)?;
let remote_name = remote.name().ok_or(GenericError("Failed to get remote name".to_string()))?;
let remote_url = remote.url().ok_or(GenericError("Failed to get remote url".to_string()))?;
let branch = get_current_branch(&repo)?.ok_or(NoActiveBranch)?;
let branch_ref = branch.get();
let branch_ref = bytes_to_string(branch_ref.name_bytes())?;
let out = new_binary_command(dir)?
.args(["pull", &remote_name, &branch_name])
.env("GIT_TERMINAL_PROMPT", "0")
.output()
.map_err(|e| GenericError(format!("failed to run git pull: {e}")))?;
let remote_name = repo.branch_upstream_remote(&branch_ref)?;
let remote_name = bytes_to_string(&remote_name)?;
debug!("Pulling from {remote_name}");
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
let combined = stdout + stderr;
let mut remote = repo.find_remote(&remote_name)?;
info!("Pulled status={} {combined}", out.status);
let mut options = FetchOptions::new();
let callbacks = default_callbacks();
options.remote_callbacks(callbacks);
if combined.to_lowercase().contains("could not read") {
return Ok(PullResult::NeedsCredentials {
url: remote_url.to_string(),
error: None,
});
}
let mut proxy = ProxyOptions::new();
proxy.auto();
options.proxy_options(proxy);
if combined.to_lowercase().contains("unable to access") {
return Ok(PullResult::NeedsCredentials {
url: remote_url.to_string(),
error: Some(combined.to_string()),
});
}
remote.fetch(&[&branch_ref], Some(&mut options), None)?;
if !out.status.success() {
return Err(GenericError(format!("Failed to pull {combined}")));
}
let stats = remote.stats();
if combined.to_lowercase().contains("up to date") {
return Ok(PullResult::UpToDate);
}
let fetch_head = repo.find_reference("FETCH_HEAD")?;
let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?;
do_merge(&repo, &branch, &fetch_commit)?;
Ok(PullResult {
received_bytes: stats.received_bytes(),
received_objects: stats.received_objects(),
Ok(PullResult::Success {
message: format!("Pulled from {}/{}", remote_name, branch_name),
})
}
// pub(crate) fn git_pull_old(dir: &Path) -> Result<PullResult> {
// let repo = open_repo(dir)?;
//
// let branch = get_current_branch(&repo)?.ok_or(NoActiveBranch)?;
// let branch_ref = branch.get();
// let branch_ref = bytes_to_string(branch_ref.name_bytes())?;
//
// let remote_name = repo.branch_upstream_remote(&branch_ref)?;
// let remote_name = bytes_to_string(&remote_name)?;
// debug!("Pulling from {remote_name}");
//
// let mut remote = repo.find_remote(&remote_name)?;
//
// let mut options = FetchOptions::new();
// let callbacks = default_callbacks();
// options.remote_callbacks(callbacks);
//
// let mut proxy = ProxyOptions::new();
// proxy.auto();
// options.proxy_options(proxy);
//
// remote.fetch(&[&branch_ref], Some(&mut options), None)?;
//
// let stats = remote.stats();
//
// let fetch_head = repo.find_reference("FETCH_HEAD")?;
// let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?;
// do_merge(&repo, &branch, &fetch_commit)?;
//
// Ok(PullResult::Success {
// message: "Hello".to_string(),
// // received_bytes: stats.received_bytes(),
// // received_objects: stats.received_objects(),
// })
// }