use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;
mod blame;
mod branches;
mod changes;
mod command;
mod commits;
mod contents;
mod diffs;
mod history;
mod messages;
mod operations;
mod patches;
mod remotes;
mod staging;
mod stash;
mod state_watch;
mod status;
mod tags;
mod worker;
mod worktrees;
pub use blame::Blame;
pub use blame::BlameCommit;
pub use blame::blame;
#[cfg(test)]
use blame::*;
pub use branches::BranchInfo;
pub use branches::checkout_branch;
pub use branches::create_branch;
pub use branches::create_branch_from;
pub use branches::current_branch;
pub use branches::default_branch;
pub use branches::delete_branch;
pub use branches::list_branches;
pub use branches::merge_branch;
pub use branches::rebase_branch;
pub use branches::rename_branch;
pub use branches::track_remote_branch;
pub use changes::ChangeEntry;
pub use changes::ChangeKind;
pub use changes::ChangeSection;
pub use changes::GitDecoration;
pub use changes::query_changes;
pub use changes::unquote_porcelain_path;
use changes::*;
pub(crate) use command::Git;
pub use commits::ResetMode;
pub use commits::checkout_detached;
pub use commits::cherry_pick;
pub use commits::commit;
pub use commits::commit_amend;
pub use commits::commit_amend_no_edit;
pub use commits::commit_staged;
pub use commits::create_tag_at;
pub use commits::head_is_on_upstream;
pub use commits::refuse_unmerged;
pub use commits::reset_to;
pub use commits::revert_commit;
pub use commits::undo_last_commit;
#[cfg(test)]
pub use commits::unmerged_paths;
pub use commits::worktree_fingerprint;
use commits::*;
pub use contents::read_bytes_at_head;
pub use contents::read_file_at_head;
pub use contents::read_file_at_rev;
pub use contents::read_file_at_stage;
pub use contents::remerge_diff3;
use contents::*;
pub use diffs::diff_against_branch;
pub use diffs::diff_previous_commit;
pub use diffs::diff_staged;
pub use diffs::is_submodule;
pub use diffs::merge_base;
pub use diffs::submodule_diff;
use diffs::*;
pub use history::CommitTree;
pub use history::FileHistoryEntry;
pub use history::GraphCommit;
pub use history::GraphRow;
pub use history::branch_history;
pub use history::branch_history_range;
pub use history::commit_card;
pub use history::commit_graph;
pub use history::commit_message;
pub use history::file_history;
pub use history::follow_names;
pub use history::humanize_age;
pub use history::layout_graph;
pub use history::show_commit;
pub use history::show_commit_file_diff;
pub use history::tree_paths;
use history::*;
pub use messages::error_line;
pub use messages::failure_line;
pub use messages::headline;
pub use messages::local_changes_block_switch;
pub use messages::result_line;
pub use messages::summary_line;
#[cfg(test)]
use messages::*;
pub use operations::RepoOp;
pub use operations::abort_operation;
pub use operations::bisect_mark;
pub use operations::continue_operation;
pub use operations::repo_operation;
pub use operations::skip_operation;
use operations::*;
pub use patches::apply_patch;
pub use remotes::LinkRev;
pub use remotes::add_remote;
pub use remotes::clone_into;
pub use remotes::fetch_all;
pub use remotes::list_remotes;
pub use remotes::publish_branch;
pub use remotes::pull_current_branch;
pub use remotes::pull_rebase;
pub use remotes::push_force;
pub use remotes::push_or_publish;
pub use remotes::push_to_remote;
pub use remotes::remove_remote;
pub use remotes::share_link;
pub use remotes::unpublished_branch;
use remotes::*;
pub use staging::discard_all_tracked;
pub use staging::discard_path;
pub use staging::remove_path;
pub use staging::stage_all;
pub use staging::stage_path;
pub use staging::stage_paths;
pub use staging::unstage_all;
pub use staging::unstage_path;
#[cfg(test)]
use staging::*;
pub use stash::StashKind;
pub use stash::list_stashes;
pub use stash::show_stash;
pub use stash::stash_apply;
pub use stash::stash_drop_oid;
pub use stash::stash_oid;
pub use stash::stash_pop;
pub use stash::stash_pop_at;
pub use stash::stash_pop_oid;
pub use stash::stash_push;
pub use stash::stash_push_staged;
pub use stash::stash_push_untracked;
#[cfg(test)]
use stash::*;
pub use state_watch::GitStateWatch;
#[cfg(test)]
use state_watch::*;
pub use status::GitStatus;
pub use status::child_repos;
pub use status::is_ignored;
pub use status::query;
use status::*;
pub use tags::create_tag;
pub use tags::delete_remote_tag;
pub use tags::delete_tag;
pub use tags::list_remote_tags;
pub use tags::list_tags;
pub use tags::push_tags;
pub use tags::tag_remote;
#[cfg(test)]
use tags::*;
pub use worker::GitRequest;
pub use worker::GitResponse;
pub use worker::git_worker_loop;
pub use worktrees::PrWorktree;
pub use worktrees::WorktreeLane;
pub use worktrees::add_worktree_lane;
pub use worktrees::checkout_pr_worktree;
pub use worktrees::lane_removal_block;
pub use worktrees::pr_worktree_keep_reason;
pub use worktrees::pr_worktree_path;
pub use worktrees::remote_for_slug;
pub use worktrees::remove_worktree_lane;
#[cfg(test)]
use worktrees::*;
fn join_raw(root: &Path, raw: &[u8]) -> PathBuf {
let trimmed = raw.strip_suffix(b"/").unwrap_or(raw);
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
root.join(std::ffi::OsStr::from_bytes(trimmed))
}
#[cfg(not(unix))]
{
root.join(String::from_utf8_lossy(trimmed).as_ref())
}
}
fn git_raw(root: &Path, args: &[&str], stdin_data: Option<&[u8]>) -> Option<Vec<u8>> {
use std::io::Write;
let mut cmd = Git::new()
.at(root)
.without_optional_locks()
.args(args)
.into_command();
cmd.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null())
.stdin(if stdin_data.is_some() {
std::process::Stdio::piped()
} else {
std::process::Stdio::null()
});
let mut child = cmd.spawn().ok()?;
let writer = match stdin_data {
Some(data) => {
let mut pipe = child.stdin.take()?;
let data = data.to_vec();
Some(std::thread::spawn(move || {
let _ = pipe.write_all(&data);
}))
}
None => None,
};
let out = child.wait_with_output().ok();
if let Some(w) = writer {
let _ = w.join();
}
Some(out?.stdout)
}
pub fn repo_toplevel(dir: &Path) -> Option<PathBuf> {
run_git(dir, &["rev-parse", "--show-toplevel"])
.ok()
.filter(|s| !s.is_empty())
.map(PathBuf::from)
}
fn run_git(path: &Path, args: &[&str]) -> std::io::Result<String> {
let path_str = path
.to_str()
.ok_or_else(|| std::io::Error::other("non-utf8 workspace path"))?;
let output = Git::new()
.at(path_str)
.without_optional_locks()
.args(args)
.into_command()
.output()?;
if !output.status.success() {
return Err(std::io::Error::other(format!(
"git {:?} failed with code {:?}",
args,
output.status.code()
)));
}
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
fn run_mutation(root: &Path, args: &[&str]) -> Result<String, String> {
run_mutation_with(root, args, false)
}
fn run_mutation_keeping_conflicts(root: &Path, args: &[&str]) -> Result<String, String> {
run_mutation_with(root, args, true)
}
fn run_mutation_with(root: &Path, args: &[&str], both_streams: bool) -> Result<String, String> {
run_mutation_inner(root, args, &[], both_streams)
}
fn run_mutation_with_env(
root: &Path,
args: &[&str],
envs: &[(&str, &str)],
) -> Result<String, String> {
run_mutation_inner(root, args, envs, false)
}
fn run_mutation_inner(
root: &Path,
args: &[&str],
envs: &[(&str, &str)],
both_streams: bool,
) -> Result<String, String> {
let path_str = root
.to_str()
.ok_or_else(|| "non-utf8 workspace path".to_string())?;
crate::output::push(
crate::output::CHANNEL_GIT,
crate::output::OutputLevel::Info,
&format!("> git {}", args.join(" ")),
);
let output = Git::new()
.at(path_str)
.args(args)
.envs(envs.iter().copied())
.never_prompting()
.into_command()
.output()
.map_err(|e| format!("failed to spawn git: {e}"))?;
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
if output.status.success() {
let msg = if !stdout.is_empty() { stdout } else { stderr };
if !msg.is_empty() {
crate::output::push(
crate::output::CHANNEL_GIT,
crate::output::OutputLevel::Info,
&msg,
);
}
Ok(msg)
} else {
let msg = if both_streams && !stdout.is_empty() && !stderr.is_empty() {
let mut both = stdout;
both.reserve(1 + stderr.len());
both.push('\n');
both.push_str(&stderr);
both
} else if !stderr.is_empty() {
stderr
} else {
stdout
};
let err = if msg.is_empty() {
let verb = args.first().copied().unwrap_or("git");
format!("git {verb} failed with code {:?}", output.status.code())
} else {
msg
};
crate::output::push(
crate::output::CHANNEL_GIT,
crate::output::OutputLevel::Error,
&err,
);
Err(err)
}
}
fn run_git_mut(dir: &Path, args: &[&str]) -> Result<(), String> {
let Some(dir) = dir.to_str() else {
return Err(String::from("path is not valid UTF-8"));
};
let output = Git::new()
.at(dir)
.args(args)
.into_command()
.output()
.map_err(|e| e.to_string())?;
if output.status.success() {
return Ok(());
}
let err = String::from_utf8_lossy(&output.stderr);
Err(err
.trim()
.lines()
.next()
.unwrap_or("git failed")
.to_string())
}
#[cfg(test)]
mod recorded_calls;
#[cfg(test)]
mod tests;