use std::os::unix::fs::MetadataExt;
use std::path::Path;
use anyhow::Result;
use super::{UntrackedFiles, acquisition, lock_launch, ownership};
use crate::git::Git;
use crate::progress;
pub struct SkillWorktree {
launch: Git,
checkout: Option<ownership::Checkout>,
untracked: UntrackedFiles,
recovered: bool,
}
pub struct ReviewConflict {
pub fingerprint: String,
pub error: anyhow::Error,
}
impl SkillWorktree {
pub fn create(launch: &Git) -> Result<Self> {
let _lock = lock_launch(launch)?;
let commit = launch.run(&["rev-parse", "--verify", "HEAD"])?;
Self::create_at(launch, &commit)
}
pub fn at(launch: &Git, commit: &str) -> Result<Self> {
let _lock = lock_launch(launch)?;
Self::create_at(launch, commit)
}
pub fn on_pull_request(launch: &Git, branch: &str, commit: &str) -> Result<Self> {
let _lock = lock_launch(launch)?;
let root = launch.run(&["rev-parse", "--show-toplevel"])?;
let retained = acquisition::recover_pull_request(launch, branch, commit)?;
let recovered = retained.is_some();
let checkout = match retained {
Some(checkout) => checkout,
None => acquire_in_temp("thirdshift-review-", |path| {
acquisition::add_review(launch, branch, path, commit)
})?,
};
Ok(Self {
launch: Git::new(root),
checkout: Some(checkout),
untracked: UntrackedFiles::DiscardNamedValidationArtifacts,
recovered,
})
}
fn create_at(launch: &Git, commit: &str) -> Result<Self> {
let root = launch.run(&["rev-parse", "--show-toplevel"])?;
let checkout = acquire_in_temp("thirdshift-skill-", |path| {
progress::step(format_args!(
"creating skill worktree {} at {commit}",
path.display()
));
acquisition::add(launch, None, path, commit)
})?;
Ok(Self {
launch: Git::new(root),
checkout: Some(checkout),
untracked: UntrackedFiles::Preserve,
recovered: false,
})
}
pub fn pull_request_conflict(
launch: &Git,
branch: &str,
commit: &str,
) -> Result<Option<ReviewConflict>> {
let _lock = lock_launch(launch)?;
let _refs_lock = launch.lock_worktree_refs()?;
ownership::pull_request_conflict(launch, branch, commit)
}
pub fn recovered(&self) -> bool {
self.recovered
}
pub fn restart_recommendation(&self, commit: &str) -> Result<()> {
if self.recovered {
self.checkout().ordinary(&self.launch, |_| {
ownership::restart_retained(&self.launch, self.checkout(), commit)
})?;
}
Ok(())
}
pub fn path(&self) -> &Path {
self.checkout().path()
}
fn checkout(&self) -> &ownership::Checkout {
self.checkout
.as_ref()
.expect("checkout not handed to Delivery")
}
pub(crate) fn take_for_delivery(&mut self, branch: &str) -> super::Worktree {
super::Worktree {
failed_work: super::FailedWork::Fix,
checkout: self.checkout.take().expect("checkout handed over once"),
launch: Git::new(self.launch.dir()),
branch: branch.into(),
synchronization_owner: std::sync::Arc::new(()),
cleanup: super::Cleanup::Remove,
}
}
pub fn discard_validation_artifacts(&mut self) {
self.untracked = UntrackedFiles::DiscardAllUntracked;
}
pub fn head(&self) -> Result<String> {
self.checkout()
.ordinary(&self.launch, |operation| operation.head())
}
pub fn merge_base_commit(&self, base_commit: &str) -> Result<String> {
self.checkout().ordinary(&self.launch, |operation| {
operation.run(&["merge-base", base_commit, "HEAD"])
})
}
}
fn acquire_in_temp(
prefix: &str,
acquire: impl FnOnce(&Path) -> Result<ownership::Checkout>,
) -> Result<ownership::Checkout> {
let directory = tempfile::Builder::new().prefix(prefix).tempdir()?;
let allocated = std::fs::symlink_metadata(directory.path())?;
let path = directory.keep();
acquire(&path).inspect_err(|_| {
if std::fs::symlink_metadata(&path).is_ok_and(|current| {
current.is_dir() && current.dev() == allocated.dev() && current.ino() == allocated.ino()
}) {
let _ = std::fs::remove_dir(&path);
}
})
}
impl Drop for SkillWorktree {
fn drop(&mut self) {
if let Some(checkout) = &self.checkout {
checkout.cleanup_preserving_work(&self.launch, self.untracked);
}
}
}