Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 201 additions & 2 deletions apps/staged/src-tauri/src/git/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,19 @@ pub struct GitOutput {
/// [`GitOutput`] for the caller to interpret. A git that cannot be spawned, a
/// path that is not a repository, and non-UTF-8 stdout are still errors.
pub fn run_lite_output(repo: &Path, args: &[&str]) -> Result<GitOutput, GitError> {
let output = spawn_git(repo, args, EnvSource::Lite)?;
output_with_env(repo, args, EnvSource::Lite)
}

/// [`run`] without the success check, as [`run_lite_output`] is to
/// [`run_lite`]: for a question whose answer is in the exit status and
/// depends on which git answers and with what config, so it is asked with
/// the env the commands it is about will run with.
pub fn run_output(repo: &Path, args: &[&str]) -> Result<GitOutput, GitError> {
output_with_env(repo, args, EnvSource::Captured)
}

fn output_with_env(repo: &Path, args: &[&str], source: EnvSource) -> Result<GitOutput, GitError> {
let output = spawn_git(repo, args, source)?;
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
if !output.status.success() && stderr.contains("not a git repository") {
return Err(GitError::NotARepo(repo.display().to_string()));
Expand Down Expand Up @@ -277,9 +289,195 @@ pub fn run_smart(repo: &Path, args: &[&str]) -> Result<String, GitError> {
/// Use this when a caller has already resolved the directory a future
/// captured-env operation will need, but has no git read to run there yet.
pub(crate) fn prewarm_shell_env(repo: &Path) {
#[cfg(test)]
shell_env_calls::record("prewarm", repo);
warm_shell_env(repo);
}

/// Wait for the captured shell env of `repo`, starting the capture if
/// nothing has: what the first captured-env op there would wait for, as a
/// step of its own so a caller can time it apart from that op. A capture that
/// fails is that op's to report.
///
/// A no-op under unit tests, like [`prewarm_shell_env`].
#[cfg(not(test))]
pub(crate) fn wait_for_shell_env(repo: &Path) {
let _ = crate::session_runner::shell_env_cache().get_blocking(repo);
}

#[cfg(test)]
pub(crate) fn wait_for_shell_env(repo: &Path) {
shell_env_calls::record("wait", repo);
}

/// Drop the captured shell env `repo` uses, or the capture in flight for
/// it, so the next captured-env op there captures afresh. A capture in
/// flight runs on, uncounted; a caller that changed what it reads, or is
/// about to delete `repo`, wants [`wait_out_and_invalidate_shell_env`].
///
/// A no-op under unit tests, like [`prewarm_shell_env`].
#[cfg(not(test))]
pub(crate) fn invalidate_shell_env(repo: &Path) {
crate::session_runner::shell_env_cache().invalidate(repo);
}

#[cfg(test)]
pub(crate) fn invalidate_shell_env(repo: &Path) {
shell_env_calls::record("invalidate", repo);
}

/// [`invalidate_shell_env`] once the capture in flight for `repo`, if there
/// is one, has finished: its shell is running in `repo`, so this is for a
/// caller about to delete the directory, or to capture it again. Never
/// starts a capture. A capture can't be stopped early, so this can take as
/// long as one does (see
/// [`ShellEnvCache::wait_out_and_invalidate`](crate::shell_env::ShellEnvCache::wait_out_and_invalidate)).
///
/// A no-op under unit tests, like [`prewarm_shell_env`].
#[cfg(not(test))]
pub(crate) fn wait_out_and_invalidate_shell_env(repo: &Path) {
crate::session_runner::shell_env_cache().wait_out_and_invalidate(repo);
}

#[cfg(test)]
pub(crate) fn wait_out_and_invalidate_shell_env(repo: &Path) {
shell_env_calls::record("wait out and invalidate", repo);
}

/// What the shell-env entry points above were asked for, since they do
/// nothing under unit tests: a test that cares in what order a caller used
/// them, and whether a directory was still there when it did, watches a
/// directory of its own.
#[cfg(test)]
pub(crate) mod shell_env_calls {
use std::path::{Path, PathBuf};
use std::sync::Mutex;

/// One call, for a path under a watched directory.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Call {
/// `prewarm`, `wait`, `invalidate` or `wait out and invalidate`.
pub(crate) what: &'static str,
pub(crate) path: PathBuf,
/// Whether `path` existed when the call was made.
pub(crate) existed: bool,
/// Whether the file the watch was told to note
/// ([`Watch::noting`]) existed then; `false` with none to note.
pub(crate) noted: bool,
}

struct Watched {
dir: PathBuf,
note: Option<PathBuf>,
calls: Vec<Call>,
}

static WATCHED: Mutex<Vec<Watched>> = Mutex::new(Vec::new());

/// While it lives, calls for paths under its directory are kept, from
/// whichever thread makes them.
pub(crate) struct Watch(PathBuf);

impl Watch {
pub(crate) fn under(dir: &Path) -> Self {
let mut watched = WATCHED.lock().unwrap_or_else(|e| e.into_inner());
watched.push(Watched {
dir: dir.to_path_buf(),
note: None,
calls: Vec::new(),
});
Self(dir.to_path_buf())
}

/// Also keep, per call, whether `file` existed when it was made:
/// a file something else writes when it runs places that run
/// among the calls.
pub(crate) fn noting(self, file: &Path) -> Self {
let mut watched = WATCHED.lock().unwrap_or_else(|e| e.into_inner());
if let Some(entry) = watched.iter_mut().find(|entry| entry.dir == self.0) {
entry.note = Some(file.to_path_buf());
}
drop(watched);
self
}

/// The calls so far, in order.
pub(crate) fn calls(&self) -> Vec<Call> {
let watched = WATCHED.lock().unwrap_or_else(|e| e.into_inner());
watched
.iter()
.find(|entry| entry.dir == self.0)
.map(|entry| entry.calls.clone())
.unwrap_or_default()
}
}

impl Drop for Watch {
fn drop(&mut self) {
let mut watched = WATCHED.lock().unwrap_or_else(|e| e.into_inner());
watched.retain(|entry| entry.dir != self.0);
}
}

pub(super) fn record(what: &'static str, path: &Path) {
let mut watched = WATCHED.lock().unwrap_or_else(|e| e.into_inner());
for entry in watched.iter_mut() {
if path.starts_with(entry.dir.as_path()) {
entry.calls.push(Call {
what,
path: path.to_path_buf(),
existed: path.exists(),
noted: entry.note.as_deref().is_some_and(Path::exists),
});
}
}
}
}

/// Keeps the machine's git config out of a test. The git this module runs
/// reads the user's and the system's config like any other, so a test of
/// something that config decides (which hooks fire, say, on a machine whose
/// hook manager owns `core.hooksPath`) would pass or fail by where it ran.
#[cfg(test)]
pub(crate) mod own_config_only {
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;

static DIRS: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());

/// While it lives, the git this module runs for a repo under its
/// directory, from whichever thread, reads no config but the repo's
/// own. Git a test runs itself is not touched.
pub(crate) struct OwnConfigOnly(PathBuf);

impl OwnConfigOnly {
pub(crate) fn under(dir: &Path) -> Self {
let mut dirs = DIRS.lock().unwrap_or_else(|e| e.into_inner());
dirs.push(dir.to_path_buf());
Self(dir.to_path_buf())
}
}

impl Drop for OwnConfigOnly {
fn drop(&mut self) {
let mut dirs = DIRS.lock().unwrap_or_else(|e| e.into_inner());
if let Some(at) = dirs.iter().position(|dir| *dir == self.0) {
dirs.remove(at);
}
}
}

pub(super) fn apply(command: &mut Command, repo: &Path) {
let dirs = DIRS.lock().unwrap_or_else(|e| e.into_inner());
if dirs.iter().any(|dir| repo.starts_with(dir)) {
command
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_NOSYSTEM", "1");
}
}
}

/// Which environment to pass to the spawned git process.
///
/// Also surfaces at the `state::compute_local_branch_git_state` boundary as a
Expand Down Expand Up @@ -554,8 +752,9 @@ fn apply_env(command: &mut Command, repo: &Path, source: EnvSource) -> AppliedEn
}

#[cfg(test)]
fn apply_env(command: &mut Command, _repo: &Path, _source: EnvSource) -> AppliedEnv {
fn apply_env(command: &mut Command, repo: &Path, _source: EnvSource) -> AppliedEnv {
strip_git_env(command);
own_config_only::apply(command, repo);
pin_c_locale(command);
AppliedEnv::Lite
}
Expand Down
20 changes: 18 additions & 2 deletions apps/staged/src-tauri/src/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod diff;
mod env;
mod files;
pub mod github;
mod post_checkout_hook;
mod rebase;
mod refs;
mod state;
Expand All @@ -18,10 +19,17 @@ mod worktree;

pub use branch_config::{read_branch_config_snapshot, BranchConfigSnapshot};
pub(crate) use cli::configure_captured;
pub(crate) use cli::invalidate_shell_env;
#[cfg(test)]
pub(crate) use cli::own_config_only::OwnConfigOnly;
pub(crate) use cli::prewarm_shell_env;
pub(crate) use cli::run as cli_run;
pub(crate) use cli::run_lite_or_captured as cli_run_lite_or_captured;
pub(crate) use cli::run_smart as cli_run_smart;
#[cfg(test)]
pub(crate) use cli::shell_env_calls;
pub(crate) use cli::wait_for_shell_env;
pub(crate) use cli::wait_out_and_invalidate_shell_env;
pub use cli::{EnvSource, GitError};
pub use commit::commit;
pub use diff::{get_file_diff, get_unified_diff, list_diff_files};
Expand All @@ -40,6 +48,12 @@ pub use github::{
CreatePrResult, FailedCheck, GitHubAuthStatus, GitHubCommentResult, GitHubRepo, HeadBranchPr,
Issue, PrListState, PrStatus, PullRequest,
};
#[cfg(test)]
pub(crate) use post_checkout_hook::STOCK_GIT_LFS_POST_CHECKOUT;
pub use post_checkout_hook::{
post_checkout_hooks_for_worktree, post_checkout_hooks_to_come, PostCheckoutHooks,
PostCheckoutHooksToCome,
};
pub use rebase::{rebase_preserving_merge_edits_command, HAND_EDIT_COMMIT_SUBJECT_PREFIX};
pub use refs::{
branch_name_without_origin, detect_default_branch, detect_default_branch_from_remote,
Expand All @@ -59,15 +73,17 @@ pub use types::*;
pub use upstream::{upstream_branch_name, upstream_branch_name_with};
pub(crate) use worktree::force_delete_branch;
pub use worktree::{
branch_exists, create_worktree, create_worktree_at_path, create_worktree_for_existing_branch,
branch_exists, can_run_hooks_on_request, create_detached_worktree_without_hooks,
create_worktree, create_worktree_at_path, create_worktree_for_existing_branch,
create_worktree_for_existing_branch_at_path,
create_worktree_for_existing_branch_without_lfs_smudge, create_worktree_from_pr,
create_worktree_from_pr_at_path, discard_worktree_changes, fetch_origin_branch,
fetch_pr_head_sha, get_commits_since_base, get_full_commit_log, get_head_sha,
get_parent_commit, has_unpushed_commits, has_upstream, list_worktree_change_paths,
list_worktrees, move_worktree, parse_branch_commit_line, parse_worktree_status_paths,
project_worktree_path_for, project_worktree_root_for, remote_branch_exists, remove_worktree,
reset_to_commit, set_upstream_to_origin, set_upstream_to_origin_or_unset, switch_branch,
reset_to_commit, run_post_checkout_as_for_a_new_worktree, set_upstream_to_origin,
set_upstream_to_origin_or_unset, switch_branch, switch_detached_worktree_to_branch,
update_branch_from_pr, worktree_path_for, BranchCommitFields, CommitInfo, UpdateFromPrResult,
WorktreeChangePaths, BRANCH_COMMIT_LOG_FORMAT,
};
Loading
Loading