Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

89.8 KB
Raw Blame
//! Git integration for the details view (per-file state column / colours) and the status bar
//! (branch, ahead/behind). Shells out to the user's `git` with `status --porcelain=v2 -z`
//! instead of linking libgit2: no extra binary size, and identical results to the command line
//! (ignore rules, submodules, sparse checkout, ...). Blocking: call off the UI thread.

use std::collections::HashMap;
use std::os::windows::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Output, Stdio};

use anyhow::{Result, bail};

use crate::git_program;

const CREATE_NO_WINDOW: u32 = 0x0800_0000;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FileState {
    Modified,
    Added,
    Deleted,
    Renamed,
    Untracked,
    Ignored,
    Conflicted,
}

impl FileState {
    /// Priority when folding several descendants into one folder state.
    fn rank(self) -> u8 {
        match self {
            FileState::Conflicted => 4,
            FileState::Modified | FileState::Added | FileState::Deleted | FileState::Renamed => 3,
            FileState::Untracked => 2,
            FileState::Ignored => 1,
        }
    }

    /// Short Turkish label for tooltips / a state column.
    pub fn label(self) -> &'static str {
        match self {
            FileState::Modified => "Değiştirildi",
            FileState::Added => "Eklendi",
            FileState::Deleted => "Silindi",
            FileState::Renamed => "Yeniden adlandırıldı",
            FileState::Untracked => "İzlenmiyor",
            FileState::Ignored => "Yoksayıldı",
            FileState::Conflicted => "Çakışma",
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct GitStatus {
    /// Repository root the paths below are relative to.
    pub root: PathBuf,
    /// Current branch, or `HEAD (abc1234)` when detached.
    pub branch: String,
    pub ahead: u32,
    pub behind: u32,
    /// Repo-relative path -> state. Untracked / ignored folders appear as one entry for the
    /// whole folder (git collapses them), not per file.
    pub files: HashMap<PathBuf, FileState>,
    /// Derived: repo-relative folder -> aggregated state of tracked-or-untracked descendants
    /// (ignored descendants do not propagate). Built by [`GitStatus::reindex`].
    pub dirs: HashMap<PathBuf, FileState>,
}

impl GitStatus {
    /// Rebuilds [`dirs`](Self::dirs) from [`files`](Self::files). Call after editing `files`.
    pub fn reindex(&mut self) {
        let mut dirs: HashMap<PathBuf, FileState> = HashMap::new();
        for (p, &st) in &self.files {
            if st == FileState::Ignored {
                continue;
            }
            // Any change below a folder shows the folder as Modified; Untracked / Conflicted stay.
            let folded = match st {
                FileState::Conflicted => FileState::Conflicted,
                FileState::Untracked => FileState::Untracked,
                _ => FileState::Modified,
            };
            for a in p.ancestors().skip(1) {
                if a.as_os_str().is_empty() {
                    break;
                }
                dirs.entry(a.to_path_buf())
                    .and_modify(|cur| {
                        if folded.rank() > cur.rank() || (folded.rank() == cur.rank() && folded == FileState::Modified) {
                            *cur = folded;
                        }
                    })
                    .or_insert(folded);
            }
        }
        self.dirs = dirs;
    }

    /// True when nothing differs from HEAD (ignored files do not count).
    pub fn is_clean(&self) -> bool {
        self.files.values().all(|&s| s == FileState::Ignored)
    }
}

/// Walks up from `dir` looking for a `.git` entry (directory, or file for worktrees and
/// submodules). Pure filesystem check, cheap enough for every navigation.
pub fn repo_root(dir: &Path) -> Option<PathBuf> {
    dir.ancestors().find(|a| a.join(".git").exists()).map(Path::to_path_buf)
}

/// What a repository's own settings need before git may look at its files.
#[derive(Debug, Default, PartialEq, Eq)]
struct RepoOverrides {
    /// `-c` arguments that switch the repository's filters off.
    args: Vec<String>,
    /// A partial clone: objects can be missing here, and git would fetch them on the way.
    partial: bool,
}

/// A clean / smudge / process filter is a program, and git runs it to compare files: a folder someone else made (a downloaded
/// project) could start anything that way just by being browsed. `listing` is git's own list of the settings in effect
/// (`config --list -z --name-only`, with each one's scope in front when `scoped`); it runs nothing, and it knows what a reading of
/// `.git/config` cannot (every spelling, includes, worktrees, submodules). The filters the repository defines are switched off; the
/// user's own (the system's and the global ones, such as Git LFS) stay. Without scopes all of them are taken for the repository's.
/// `Err` when a filter's name cannot be put on a command line safely: nothing that reads files may run then.
fn overrides_from(listing: &[u8], scoped: bool) -> Result<RepoOverrides> {
    let mut fields: Vec<&[u8]> = listing.split(|&b| b == 0).collect();
    fields.pop(); // after the last NUL
    // a Git inside WSL may be started by a shell that greets first (see `git_program`): the greeting stands before the first field
    if let Some(first) = fields.first_mut() {
        *first = first.rsplit(|&b| b == b'\n').next().unwrap_or_default();
        // one that does not end its line hides where the first name begins; a scope it hides is not the user's, which is safe
        if !scoped && first.windows(7).skip(1).any(|w| w.eq_ignore_ascii_case(b"filter.")) {
            bail!("git config çıktısı anlaşılamadı");
        }
    }
    if scoped && fields.len() % 2 != 0 {
        bail!("git config çıktısı anlaşılamadı");
    }
    let entries: Vec<(bool, &[u8])> = if scoped {
        fields.chunks_exact(2).map(|e| (!matches!(e[0], b"system" | b"global" | b"command"), e[1])).collect()
    } else {
        fields.into_iter().map(|k| (true, k)).collect()
    };
    let starts = |key: &[u8], prefix: &str| key.get(..prefix.len()).is_some_and(|k| k.eq_ignore_ascii_case(prefix.as_bytes()));
    let ends = |key: &[u8], suffix: &str| {
        key.len().checked_sub(suffix.len()).is_some_and(|at| key[at..].eq_ignore_ascii_case(suffix.as_bytes()))
    };
    // Git LFS runs the programs of its extensions, so a repository that names one makes the user's own filter run it
    let every = entries.iter().any(|&(repo, key)| repo && starts(key, "lfs.extension."));
    let mut names = std::collections::BTreeSet::new();
    let mut partial = false;
    for (repo, key) in entries {
        partial |= ends(key, ".promisor") || ends(key, ".partialclonefilter") || ends(key, "extensions.partialclone");
        if (!repo && !every) || !starts(key, "filter.") {
            continue;
        }
        // filter.<name>.<variable>; `filter.<variable>` alone belongs to no filter. The name may be empty: `* filter=` uses that one.
        let rest = &key["filter.".len()..];
        let Some(dot) = rest.iter().rposition(|&b| b == b'.') else { continue };
        let name = &rest[..dot];
        // `-c` ends the key at the first '=', and quotes, backslashes, `$`, other bytes… may not reach git as they are (Windows'
        // command line, a git run through a shell, Unicode forms). No real filter has such a name.
        if !name.iter().all(|b| b.is_ascii_alphanumeric() || b"-_. ".contains(b)) {
            bail!("Depo ayarlarındaki bir süzgeç güvenle kapatılamıyor: {}", String::from_utf8_lossy(name));
        }
        names.insert(String::from_utf8_lossy(name).into_owned());
    }
    let mut args = Vec::new();
    for name in names {
        for setting in ["clean=", "smudge=", "process=", "required=false"] {
            args.extend(["-c".to_string(), format!("filter.{name}.{setting}")]);
        }
    }
    Ok(RepoOverrides { args, partial })
}

fn repo_overrides(root: &Path) -> Result<RepoOverrides> {
    // (by the same program that will look at the files: a Git inside WSL has other settings than one of Windows)
    let list = |args: &[&str]| output(git_base(root).args(["config", "--list", "-z", "--name-only"]).args(args)).map_err(spawn_err);
    let out = list(&["--show-scope"])?;
    if out.status.success() {
        return overrides_from(whole(&out.stdout, true)?, true);
    }
    // git before 2.26 has no --show-scope
    let out = list(&[])?;
    if !out.status.success() {
        bail!("git config başarısız: {}", String::from_utf8_lossy(&out.stderr).trim());
    }
    overrides_from(whole(&out.stdout, false)?, false)
}

/// `listing` if git came to its end: the last settings it lists are the ones [`git_base`] gives it. A chosen program that says all
/// is well and passes on only a part, or nothing, must not read as "no filters".
fn whole(listing: &[u8], scoped: bool) -> Result<&[u8]> {
    let ours: &[u8] = if scoped { b"command\0core.untrackedcache\0" } else { b"core.untrackedcache\0" };
    if !listing.windows(ours.len()).any(|w| w == ours) {
        bail!("git config çıktısı eksik");
    }
    Ok(listing)
}

/// A Git command (the program is the one chosen in Settings) in `root`, without the file monitor the repository may name (a program
/// too). Does not switch its filters off: only for what never looks at a file's content. Run it with [`output`] or [`spawn`].
fn git_base(root: &Path) -> Command {
    let mut c = git_program::command();
    c.args(["-c", "core.fsmonitor=false", "-c", "core.untrackedCache=false", "--no-optional-locks"])
        // (a submodule's status is read by a Git run inside it, with ITS config)
        .args(["-c", "status.submoduleSummary=false", "-c", "submodule.recurse=false", "-c", "diff.ignoreSubmodules=all"])
        .current_dir(root)
        .env("GIT_OPTIONAL_LOCKS", "0")
        .env("GIT_TERMINAL_PROMPT", "0")
        // `git config` would list this file instead of the settings the other commands use
        .env_remove("GIT_CONFIG")
        .stdin(Stdio::null())
        .creation_flags(CREATE_NO_WINDOW);
    c
}

fn output(c: &mut Command) -> std::io::Result<Output> {
    git_program::check(c)?;
    c.output()
}

fn spawn(c: &mut Command) -> std::io::Result<Child> {
    git_program::check(c)?;
    c.spawn()
}

/// [`git_base`] for reading while browsing: nothing of the repository's settings that only a deeper look would need is run either.
/// In a partial clone git fetches a missing object by itself, with the program (`remote.<name>.uploadpack`, `core.sshCommand`…) the
/// repository names, and `log.showSignature` runs its `gpg.program`. The `-c` forms are for a git the environment does not reach.
fn git_read(root: &Path) -> Command {
    let mut c = git_base(root);
    c.args(["-c", "log.showSignature=false", "-c", "protocol.allow=never"]);
    for p in ["file", "ssh", "git", "http", "https", "ext", "fd"] {
        c.args(["-c", &format!("protocol.{p}.allow=never")]);
    }
    c.env("GIT_NO_LAZY_FETCH", "1").env("GIT_ALLOW_PROTOCOL", "none");
    c
}

/// [`git_base`] with the repository's filters switched off ([`overrides_from`]).
fn git_cmd(root: &Path) -> Result<Command> {
    let o = repo_overrides(root)?;
    let mut c = git_base(root);
    c.args(o.args);
    Ok(c)
}

const STATUS: [&str; 7] =
    ["status", "--porcelain=v2", "--branch", "-z", "--untracked-files=normal", "--ignored=traditional", "--ignore-submodules=dirty"];

/// Added to [`STATUS`] in a partial clone. 1.0 is git's way to write 100% without a `%`, which a chosen program is not given.
const EXACT_RENAMES: &str = "--find-renames=1.0";

/// Runs `git status` for the repository at `repo_root` (use [`repo_root`] to find it).
/// Errors when git is not installed or the command fails. A submodule counts as changed when another commit is checked out in it,
/// not when its files are: to tell, git would run status inside it, with filters of its own that cannot be named from here.
pub fn status(repo_root: &Path) -> Result<GitStatus> {
    let o = repo_overrides(repo_root)?;
    let mut c = git_read(repo_root);
    c.args(o.args).args(STATUS);
    if o.partial {
        // telling a renamed and changed file needs contents a partial clone may not have: identical ones only
        c.arg(EXACT_RENAMES);
    }
    let out = output(&mut c).map_err(spawn_err)?;
    if !out.status.success() {
        bail!("git status başarısız: {}", String::from_utf8_lossy(&out.stderr).trim());
    }
    Ok(parse_porcelain_v2(repo_root, &out.stdout))
}

fn map_xy(kind: char, x: char, y: char) -> FileState {
    if kind == '2' {
        FileState::Renamed
    } else if x == 'D' || y == 'D' {
        FileState::Deleted
    } else if x == 'A' || y == 'A' {
        FileState::Added
    } else {
        FileState::Modified
    }
}

/// Parses `git status --porcelain=v2 --branch -z` output. Tolerant: unknown or malformed
/// records are skipped.
pub fn parse_porcelain_v2(root: &Path, raw: &[u8]) -> GitStatus {
    let text = String::from_utf8_lossy(raw);
    let mut st = GitStatus { root: root.to_path_buf(), ..Default::default() };
    let mut oid = String::new();
    let mut recs = text.split('\0');
    while let Some(rec) = recs.next() {
        if rec.is_empty() {
            continue;
        }
        if let Some(h) = rec.strip_prefix("# ") {
            if let Some(v) = h.strip_prefix("branch.oid ") {
                oid = v.to_string();
            } else if let Some(v) = h.strip_prefix("branch.head ") {
                st.branch = v.to_string();
            } else if let Some(v) = h.strip_prefix("branch.ab ") {
                for tok in v.split_whitespace() {
                    if let Some(n) = tok.strip_prefix('+') {
                        st.ahead = n.parse().unwrap_or(0);
                    } else if let Some(n) = tok.strip_prefix('-') {
                        st.behind = n.parse().unwrap_or(0);
                    }
                }
            }
            continue;
        }
        let mut chars = rec.chars();
        let kind = chars.next().unwrap_or(' ');
        let (state, path) = match kind {
            '1' => {
                // 1 XY sub mH mI mW hH hI path  (path may contain spaces => splitn(9))
                let f: Vec<&str> = rec.splitn(9, ' ').collect();
                if f.len() < 9 {
                    continue;
                }
                let mut xy = f[1].chars();
                (map_xy('1', xy.next().unwrap_or('.'), xy.next().unwrap_or('.')), f[8])
            }
            '2' => {
                // 2 XY sub mH mI mW hH hI Xscore path \0 origPath
                let f: Vec<&str> = rec.splitn(10, ' ').collect();
                let _orig = recs.next(); // consumed: the old path is not a separate record
                if f.len() < 10 {
                    continue;
                }
                (FileState::Renamed, f[9])
            }
            'u' => {
                // u XY sub m1 m2 m3 mW h1 h2 h3 path
                let f: Vec<&str> = rec.splitn(11, ' ').collect();
                if f.len() < 11 {
                    continue;
                }
                (FileState::Conflicted, f[10])
            }
            '?' => match rec.get(2..) {
                Some(p) => (FileState::Untracked, p),
                None => continue,
            },
            '!' => match rec.get(2..) {
                Some(p) => (FileState::Ignored, p),
                None => continue,
            },
            _ => continue,
        };
        let p = path.trim_end_matches('/');
        if !p.is_empty() {
            st.files.insert(PathBuf::from(p), state);
        }
    }
    if st.branch == "(detached)" {
        let short: String = oid.chars().take(7).collect();
        st.branch = if short.is_empty() { "HEAD".into() } else { format!("HEAD ({short})") };
    }
    st.reindex();
    st
}

/// `path` relative to `root`, comparing components case-insensitively (Windows).
fn rel_to(root: &Path, path: &Path) -> Option<PathBuf> {
    let mut rc = root.components();
    let mut pc = path.components();
    loop {
        match (rc.next(), pc.next()) {
            (None, rest) => {
                let mut out = PathBuf::new();
                if let Some(c) = rest {
                    out.push(c.as_os_str());
                }
                out.extend(pc.map(|c| c.as_os_str()));
                return Some(out);
            }
            (Some(a), Some(b)) if a.as_os_str().eq_ignore_ascii_case(b.as_os_str()) => {}
            _ => return None,
        }
    }
}

/// State of the entry `name` inside the listed folder `dir` (absolute path inside the repo).
/// Files report their own state. Folders report the aggregate of their descendants (any
/// changed descendant -> `Modified`, conflict wins, then `Untracked`); a folder inside an
/// untracked or ignored folder inherits that state. `None` = clean / not in the repo.
pub fn state_for_child(status: &GitStatus, dir: &Path, name: &str) -> Option<FileState> {
    let child = rel_to(&status.root, dir)?.join(name);
    if let Some(&s) = status.files.get(&child) {
        return Some(s);
    }
    // Inside a collapsed untracked / ignored folder?
    for a in child.ancestors().skip(1) {
        if a.as_os_str().is_empty() {
            break;
        }
        if let Some(&s @ (FileState::Untracked | FileState::Ignored)) = status.files.get(a) {
            return Some(s);
        }
    }
    status.dirs.get(&child).copied()
}

/// The different states the direct children of `dir` show (what [`state_for_child`] answers for them), found from the status
/// alone and not child by child: for a list that wants to know what its status column will hold. Empty: every child is clean, or
/// `dir` is not in the repository.
pub fn states_in(status: &GitStatus, dir: &Path) -> Vec<FileState> {
    let Some(rel) = rel_to(&status.root, dir) else { return Vec::new() };
    let mut out: Vec<FileState> = Vec::new();
    let mut add = |s: FileState| {
        if !out.contains(&s) {
            out.push(s);
        }
    };
    // the folder is, or lies inside, an untracked or ignored folder: every child inherits that
    for a in rel.ancestors().take_while(|a| !a.as_os_str().is_empty()) {
        if let Some(&s @ (FileState::Untracked | FileState::Ignored)) = status.files.get(a) {
            add(s);
        }
    }
    for (p, s) in status.files.iter().chain(status.dirs.iter()) {
        if p.parent() == Some(rel.as_path()) {
            add(*s);
        }
    }
    out
}

#[cfg(test)]
mod states_in_tests {
    use std::path::{Path, PathBuf};

    use super::{FileState, GitStatus, state_for_child, states_in};

    fn status() -> GitStatus {
        let mut st = GitStatus { root: PathBuf::from(r"C:\repo"), ..Default::default() };
        for (p, s) in [
            (r"src\a.rs", FileState::Modified),
            (r"src\deep\b.rs", FileState::Added),
            ("notes", FileState::Untracked),
            ("top.txt", FileState::Deleted),
            (r"docs\x.md", FileState::Modified),
        ] {
            st.files.insert(PathBuf::from(p), s);
        }
        st.reindex();
        st
    }

    #[test]
    fn the_states_a_folders_children_show() {
        let st = status();
        let mut root = states_in(&st, Path::new(r"C:\repo"));
        root.sort_by_key(|s| format!("{s:?}"));
        // top.txt deleted, notes untracked, src and docs changed inside
        assert_eq!(root, [FileState::Deleted, FileState::Modified, FileState::Untracked]);
        let src = states_in(&st, Path::new(r"C:\repo\src"));
        assert!(src.contains(&FileState::Modified), "a.rs, and the folder deep by what is in it");
        assert_eq!(src.len(), src.iter().collect::<std::collections::HashSet<_>>().len(), "each state once");
        for name in ["a.rs", "deep", "clean.rs"] {
            if let Some(s) = state_for_child(&st, Path::new(r"C:\repo\src"), name) {
                assert!(src.contains(&s), "{name}: what a row shows is among the states measured");
            }
        }
    }

    #[test]
    fn inside_an_untracked_folder_every_child_is_untracked_and_a_clean_folder_has_none() {
        let st = status();
        assert_eq!(states_in(&st, Path::new(r"C:\repo\notes")), [FileState::Untracked]);
        assert_eq!(states_in(&st, Path::new(r"C:\repo\notes\sub")), [FileState::Untracked]);
        assert_eq!(state_for_child(&st, Path::new(r"C:\repo\notes\sub"), "f.txt"), Some(FileState::Untracked));
        assert!(states_in(&st, Path::new(r"C:\repo\src\deep\none")).is_empty());
        assert!(states_in(&st, Path::new(r"D:\elsewhere")).is_empty(), "not in the repository");
    }
}

// ---------------------------------------------------------------------------------------------
// Branches, checkout, network operations, last commit. All blocking: call off the UI thread.
// ---------------------------------------------------------------------------------------------

fn spawn_err(e: std::io::Error) -> anyhow::Error {
    if e.kind() == std::io::ErrorKind::NotFound && git_program::is_chosen() {
        anyhow::anyhow!("Ayarlarda seçilen Git programı bulunamadı")
    } else if e.kind() == std::io::ErrorKind::NotFound {
        anyhow::anyhow!("Git bulunamadı (kurulu değil ya da PATH'te yok)")
    } else {
        anyhow::Error::new(e).context("git çalıştırılamadı")
    }
}

/// Runs git and returns the raw output (even on a non-zero exit).
fn run_git(root: &Path, args: &[&str]) -> Result<Output> {
    // these never look at a file, so they need not ask for the repository's filters first
    let reads = args.first().is_some_and(|a| ["config", "for-each-ref", "remote", "show-ref", "symbolic-ref"].contains(a));
    let mut c = if reads { git_read(root) } else { git_cmd(root)? };
    output(c.args(args)).map_err(spawn_err)
}

/// Runs git and returns stdout; on failure bails with `what: <stderr>`.
fn git_text(root: &Path, args: &[&str], what: &str) -> Result<String> {
    text_of(run_git(root, args)?, what)
}

/// What git printed; on failure bails with `what: <stderr>`.
fn text_of(out: Output, what: &str) -> Result<String> {
    if !out.status.success() {
        let err = String::from_utf8_lossy(&out.stderr);
        let err = if err.trim().is_empty() { String::from_utf8_lossy(&out.stdout) } else { err };
        bail!("{what}: {}", err.trim());
    }
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

/// A local or remote-tracking branch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Branch {
    /// `main` for local branches, `origin/main` for remote-tracking ones.
    pub name: String,
    pub is_remote: bool,
    /// The currently checked-out branch.
    pub is_head: bool,
    /// Short name of the upstream (`origin/main`), local branches only.
    pub upstream: Option<String>,
    pub ahead: u32,
    pub behind: u32,
}

/// Lists local branches (HEAD first, then by name) followed by remote-tracking branches
/// (by name). Symbolic `origin/HEAD` refs are skipped.
pub fn branches(root: &Path) -> Result<Vec<Branch>> {
    let text = git_text(
        root,
        &[
            "for-each-ref",
            "--format=%(HEAD)%00%(refname)%00%(upstream:short)%00%(upstream:track,nobracket)",
            "refs/heads",
            "refs/remotes",
        ],
        "Dallar listelenemedi",
    )?;
    let mut local = Vec::new();
    let mut remote = Vec::new();
    for line in text.lines() {
        let f: Vec<&str> = line.split('\0').collect();
        if f.len() < 4 {
            continue;
        }
        let (is_head, refname, upstream, track) = (f[0].trim() == "*", f[1], f[2], f[3]);
        if let Some(n) = refname.strip_prefix("refs/heads/") {
            let (mut ahead, mut behind) = (0, 0);
            for part in track.split(',') {
                let part = part.trim();
                if let Some(v) = part.strip_prefix("ahead ") {
                    ahead = v.trim().parse().unwrap_or(0);
                } else if let Some(v) = part.strip_prefix("behind ") {
                    behind = v.trim().parse().unwrap_or(0);
                }
            }
            local.push(Branch {
                name: n.to_string(),
                is_remote: false,
                is_head,
                upstream: (!upstream.is_empty()).then(|| upstream.to_string()),
                ahead,
                behind,
            });
        } else if let Some(n) = refname.strip_prefix("refs/remotes/") {
            if n.ends_with("/HEAD") || !n.contains('/') {
                continue;
            }
            remote.push(Branch { name: n.to_string(), is_remote: true, is_head: false, upstream: None, ahead: 0, behind: 0 });
        }
    }
    local.sort_by(|a, b| b.is_head.cmp(&a.is_head).then_with(|| a.name.cmp(&b.name)));
    remote.sort_by(|a, b| a.name.cmp(&b.name));
    local.extend(remote);
    Ok(local)
}

/// What to do with uncommitted changes when switching branches.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CheckoutMode {
    /// Plain switch; fails when local changes would be overwritten.
    Normal,
    /// Stash (including untracked files), switch, then pop the stash.
    Stash,
    /// `--force`: throw away conflicting local changes.
    Discard,
}

enum Target {
    Local(String),
    /// Remote-tracking ref (`origin/foo`) without a local branch yet: create and track.
    Track(String),
}

fn ref_exists(root: &Path, full_ref: &str) -> bool {
    run_git(root, &["show-ref", "--verify", "--quiet", full_ref]).map(|o| o.status.success()).unwrap_or(false)
}

fn resolve_target(root: &Path, branch: &str) -> Result<Target> {
    if branch.starts_with('-') || branch.is_empty() {
        bail!("Geçersiz dal adı: {branch}");
    }
    if ref_exists(root, &format!("refs/heads/{branch}")) {
        return Ok(Target::Local(branch.to_string()));
    }
    if ref_exists(root, &format!("refs/remotes/{branch}")) {
        let local = branch.split_once('/').map(|(_, n)| n).unwrap_or(branch);
        if ref_exists(root, &format!("refs/heads/{local}")) {
            return Ok(Target::Local(local.to_string()));
        }
        return Ok(Target::Track(branch.to_string()));
    }
    bail!("Dal bulunamadı: {branch}")
}

fn switch_to(root: &Path, target: &Target, force: bool) -> Result<()> {
    let mut args: Vec<&str> = vec!["switch"];
    if force {
        args.push("--force");
    }
    match target {
        Target::Local(n) => args.push(n),
        Target::Track(r) => {
            args.push("--track");
            args.push(r);
        }
    }
    let out = run_git(root, &args)?;
    if out.status.success() {
        return Ok(());
    }
    let err = String::from_utf8_lossy(&out.stderr);
    let err = err.trim();
    if err.contains("overwritten") || err.contains("would be lost") {
        bail!("Dal değiştirilemedi: yerel değişiklikler çakışıyor. Değişiklikleri saklayın ya da atın.\n{err}");
    }
    bail!("Dal değiştirilemedi: {err}")
}

/// Switches to `branch`. A remote-tracking name (`origin/foo`) creates and tracks local `foo`
/// (or just switches to it when it already exists).
pub fn checkout(root: &Path, branch: &str, mode: CheckoutMode) -> Result<()> {
    let target = resolve_target(root, branch)?;
    match mode {
        CheckoutMode::Normal => switch_to(root, &target, false),
        CheckoutMode::Discard => switch_to(root, &target, true),
        CheckoutMode::Stash => {
            let dirty = !git_text(root, &["status", "--porcelain"], "git status başarısız")?.trim().is_empty();
            if dirty {
                git_text(
                    root,
                    &["stash", "push", "--include-untracked", "-m", "rustyfiles checkout"],
                    "Değişiklikler stash'e alınamadı",
                )?;
            }
            if let Err(e) = switch_to(root, &target, false) {
                if dirty {
                    let _ = run_git(root, &["stash", "pop"]);
                }
                return Err(e);
            }
            if dirty {
                git_text(
                    root,
                    &["stash", "pop"],
                    "Dal değiştirildi ama saklanan değişiklikler geri uygulanamadı (git stash listesinde duruyor)",
                )?;
            }
            Ok(())
        }
    }
}

/// Local check mirroring `git check-ref-format --branch` (without spawning git).
pub fn is_valid_branch_name(name: &str) -> bool {
    if name.is_empty() || name == "@" || name.starts_with('-') || name.starts_with('/') {
        return false;
    }
    if name.ends_with('/') || name.ends_with(".lock") || name.ends_with('.') {
        return false;
    }
    if name.contains("..") || name.contains("@{") || name.contains("//") {
        return false;
    }
    if name.chars().any(|c| c.is_whitespace() || c.is_control() || "~^:?*[\\".contains(c)) {
        return false;
    }
    name.split('/').all(|c| !c.starts_with('.') && !c.ends_with(".lock"))
}

/// Creates branch `name` (from `from`, default HEAD); optionally switches to it.
pub fn create_branch(root: &Path, name: &str, from: Option<&str>, checkout: bool) -> Result<()> {
    if !is_valid_branch_name(name) {
        bail!("Geçersiz dal adı: {name}");
    }
    if let Some(f) = from
        && (f.is_empty() || f.starts_with('-'))
    {
        bail!("Geçersiz başlangıç noktası: {f}");
    }
    let mut args: Vec<&str> = if checkout { vec!["switch", "-c", name] } else { vec!["branch", name] };
    if let Some(f) = from {
        args.push(f);
    }
    git_text(root, &args, "Dal oluşturulamadı").map(|_| ())
}

/// Deletes a local branch (`force` = `-D`, allows unmerged branches).
pub fn delete_branch(root: &Path, name: &str, force: bool) -> Result<()> {
    if name.is_empty() || name.starts_with('-') {
        bail!("Geçersiz dal adı: {name}");
    }
    let flag = if force { "-D" } else { "-d" };
    git_text(root, &["branch", flag, name], "Dal silinemedi").map(|_| ())
}

fn flush_line(cur: &mut Vec<u8>, kept: &mut Vec<String>, cb: &mut dyn FnMut(&str)) {
    if cur.is_empty() {
        return;
    }
    let line = String::from_utf8_lossy(cur).trim().to_string();
    cur.clear();
    if line.is_empty() {
        return;
    }
    cb(&line);
    if !is_progress_line(&line) {
        kept.push(line);
    }
}

fn is_progress_line(line: &str) -> bool {
    let l = line.strip_prefix("remote: ").unwrap_or(line);
    l.contains('%')
        && ["Receiving objects", "Resolving deltas", "Counting objects", "Compressing objects", "Writing objects", "Enumerating objects", "Updating files"]
            .iter()
            .any(|p| l.starts_with(p))
}

/// Runs `cmd`, feeding every stderr line (split on `\r` and `\n`, so progress updates arrive
/// live) to `on_progress`. On failure the error carries the last meaningful output lines.
fn run_streaming(mut cmd: Command, what: &str, on_progress: &mut dyn FnMut(&str)) -> Result<()> {
    use std::io::Read;
    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
    let mut child = spawn(&mut cmd).map_err(spawn_err)?;
    let mut stdout = child.stdout.take().expect("stdout piped");
    let out_thread = std::thread::spawn(move || {
        let mut v = Vec::new();
        let _ = stdout.read_to_end(&mut v);
        v
    });
    let mut stderr = child.stderr.take().expect("stderr piped");
    let mut kept: Vec<String> = Vec::new();
    let mut cur: Vec<u8> = Vec::new();
    let mut buf = [0u8; 4096];
    loop {
        match stderr.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => {
                for &b in &buf[..n] {
                    if b == b'\r' || b == b'\n' {
                        flush_line(&mut cur, &mut kept, on_progress);
                    } else {
                        cur.push(b);
                    }
                }
            }
            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
            Err(_) => break,
        }
    }
    flush_line(&mut cur, &mut kept, on_progress);
    let status = child.wait().map_err(spawn_err)?;
    let out_bytes = out_thread.join().unwrap_or_default();
    if status.success() {
        return Ok(());
    }
    let tail = |lines: Vec<String>| -> String {
        let skip = lines.len().saturating_sub(6);
        lines[skip..].join("\n")
    };
    let mut msg = tail(kept);
    if msg.is_empty() {
        let stdout_lines: Vec<String> =
            String::from_utf8_lossy(&out_bytes).lines().map(str::trim).filter(|l| !l.is_empty()).map(String::from).collect();
        msg = tail(stdout_lines);
    }
    if msg.is_empty() {
        msg = format!("çıkış kodu {}", status.code().unwrap_or(-1));
    }
    bail!("{what} başarısız: {msg}")
}

fn remotes(root: &Path) -> Result<Vec<String>> {
    const FAILED: &str = "Uzak depolar okunamadı";
    // (wslgit puts what `git remote` prints, which is the repository's own text, through a shell: see `git_program`)
    if git_program::may_use_shell() {
        let keys = git_text(root, &["config", "-z", "--name-only", "--list"], FAILED)?;
        return Ok(remotes_in_config(&keys));
    }
    let t = git_text(root, &["remote"], FAILED)?;
    Ok(t.lines().map(str::trim).filter(|l| !l.is_empty()).map(String::from).collect())
}

/// The remotes that have an address, by name, out of `git config -z --name-only --list`.
fn remotes_in_config(keys: &str) -> Vec<String> {
    let names = keys.split('\0').filter_map(|k| k.strip_prefix("remote.")?.strip_suffix(".url")).filter(|n| !n.is_empty());
    let names: std::collections::BTreeSet<String> = names.map(String::from).collect();
    names.into_iter().collect()
}

fn require_remote(root: &Path) -> Result<Vec<String>> {
    let r = remotes(root)?;
    if r.is_empty() {
        bail!("Uzak depo tanımlı değil");
    }
    Ok(r)
}

fn current_branch(root: &Path) -> Option<String> {
    let out = run_git(root, &["symbolic-ref", "--short", "-q", "HEAD"]).ok()?;
    let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
    (out.status.success() && !s.is_empty()).then_some(s)
}

fn upstream_of(root: &Path, branch: &str) -> Option<String> {
    let out = run_git(root, &["for-each-ref", "--format=%(upstream:short)", &format!("refs/heads/{branch}")]).ok()?;
    let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
    (!s.is_empty()).then_some(s)
}

/// `git fetch --all --prune` with live progress lines.
pub fn fetch(root: &Path, on_progress: &mut dyn FnMut(&str)) -> Result<()> {
    require_remote(root)?;
    let mut c = git_cmd(root)?;
    c.args(["fetch", "--all", "--prune", "--progress"]);
    run_streaming(c, "git fetch", on_progress)
}

/// `git pull --no-edit` (merge strategy unless the user configured `pull.rebase`).
pub fn pull(root: &Path, on_progress: &mut dyn FnMut(&str)) -> Result<()> {
    require_remote(root)?;
    let Some(branch) = current_branch(root) else {
        bail!("Ayrık HEAD durumunda çekme yapılamaz");
    };
    if upstream_of(root, &branch).is_none() {
        bail!("'{branch}' dalının izlediği bir uzak dal yok");
    }
    let mut c = git_cmd(root)?;
    let has_rebase_cfg = run_git(root, &["config", "--get", "pull.rebase"]).map(|o| o.status.success()).unwrap_or(false);
    if !has_rebase_cfg {
        c.args(["-c", "pull.rebase=false"]);
    }
    c.args(["pull", "--no-edit", "--progress"]);
    run_streaming(c, "git pull", on_progress)
}

/// `git push`; a branch without upstream is pushed with `-u <remote> <branch>`
/// (`origin` when present, otherwise the first remote).
pub fn push(root: &Path, on_progress: &mut dyn FnMut(&str)) -> Result<()> {
    let remotes = require_remote(root)?;
    let Some(branch) = current_branch(root) else {
        bail!("Ayrık HEAD durumunda gönderilemez");
    };
    let mut c = git_cmd(root)?;
    c.args(["push", "--progress"]);
    if upstream_of(root, &branch).is_none() {
        let remote = remotes.iter().find(|r| *r == "origin").unwrap_or(&remotes[0]);
        c.args(["-u", remote, &branch]);
    }
    run_streaming(c, "git push", on_progress)
}

/// Pull (when the branch has an upstream) then push.
pub fn sync(root: &Path, on_progress: &mut dyn FnMut(&str)) -> Result<()> {
    require_remote(root)?;
    if let Some(b) = current_branch(root)
        && upstream_of(root, &b).is_some()
    {
        pull(root, on_progress)?;
    }
    push(root, on_progress)
}

/// URL of `origin` (or the first remote); `None` when there is no remote.
pub fn remote_url(root: &Path) -> Option<String> {
    let r = remotes(root).ok()?;
    let name = r.iter().find(|n| *n == "origin").or_else(|| r.first())?;
    let out = if git_program::may_use_shell() {
        run_git(root, &["config", "--get", &format!("remote.{name}.url")])
    } else {
        run_git(root, &["remote", "get-url", name])
    };
    let out = out.ok()?;
    let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
    (out.status.success() && !s.is_empty()).then_some(s)
}

/// Points `origin` at `url` (adds it, or changes its address).
pub fn connect_remote(root: &Path, url: &str) -> Result<()> {
    let url = url.trim();
    if url.is_empty() {
        bail!("Depo adresi boş");
    }
    // (an address is never an option of `git remote`: a private security report)
    if url.starts_with('-') {
        bail!("Depo adresi geçersiz");
    }
    let verb = if remotes(root)?.iter().any(|r| r == "origin") { "set-url" } else { "add" };
    git_text(root, &["remote", verb, "--", "origin", url], "Uzak depo bağlanamadı").map(|_| ())
}

/// Whether there is anything to commit (changed, added, deleted or untracked files).
pub fn has_changes(root: &Path) -> bool {
    git_text(root, &["status", "--porcelain"], "").is_ok_and(|t| !t.trim().is_empty())
}

/// Stages everything and commits it with `message`.
pub fn commit_all(root: &Path, message: &str) -> Result<()> {
    let message = message.trim();
    if message.is_empty() {
        bail!("Kayıt açıklaması boş");
    }
    if !has_changes(root) {
        bail!("Kaydedilecek değişiklik yok");
    }
    git_text(root, &["add", "-A"], "Değişiklikler eklenemedi")?;
    const FAILED: &str = "Kaydedilemedi";
    if !git_program::is_chosen() {
        return git_text(root, &["commit", "-q", "-m", message], FAILED).map(|_| ());
    }
    // A chosen program is handed the message on its input: as an argument it may meet a shell (see `git_program`).
    let mut c = git_cmd(root)?;
    c.args(["commit", "-q", "-F", "-"]).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped());
    let mut child = spawn(&mut c).map_err(spawn_err)?;
    let mut stdin = child.stdin.take().expect("stdin piped");
    let text = message.to_string();
    // (on a thread: what git and its hooks print is read meanwhile)
    std::thread::spawn(move || {
        let _ = std::io::Write::write_all(&mut stdin, text.as_bytes());
    });
    text_of(child.wait_with_output().map_err(spawn_err)?, FAILED).map(|_| ())
}

/// `git init` in `dir` (created when missing).
pub fn init(dir: &Path) -> Result<()> {
    std::fs::create_dir_all(dir).map_err(|e| anyhow::Error::new(e).context("Klasör oluşturulamadı"))?;
    git_text(dir, &["init", "-q"], "Depo başlatılamadı").map(|_| ())
}

/// Clones `url` into a new folder under `dest_parent` (named after the repository) and returns
/// that folder.
pub fn clone(url: &str, dest_parent: &Path, on_progress: &mut dyn FnMut(&str)) -> Result<PathBuf> {
    let url = url.trim();
    if url.is_empty() {
        bail!("Depo adresi boş");
    }
    let last = url.trim_end_matches(['/', '\\']).rsplit(['/', '\\', ':']).next().unwrap_or("");
    let name = last.strip_suffix(".git").unwrap_or(last);
    let name = if name.is_empty() { "repo" } else { name };
    std::fs::create_dir_all(dest_parent).map_err(|e| anyhow::Error::new(e).context("Hedef klasör oluşturulamadı"))?;
    let mut c = git_cmd(dest_parent)?;
    // (a repository named "remote" is one wslgit would take for a command, see `git_program`; "./remote" is not)
    let dest = if git_program::may_use_shell() { format!("./{name}") } else { name.to_string() };
    c.args(["clone", "--progress", "--", url, &dest]);
    run_streaming(c, "git clone", on_progress)?;
    Ok(dest_parent.join(name))
}

/// Latest commit touching a path (or the whole repo).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Commit {
    /// Abbreviated hash.
    pub sha: String,
    /// Subject line.
    pub message: String,
    pub author: String,
    /// Commit time, milliseconds since the Unix epoch.
    pub time_ms: i64,
}

/// `git log -1` for `path` (or HEAD when `None`). `None` when there is no such commit.
pub fn last_commit(root: &Path, path: Option<&Path>) -> Option<Commit> {
    let mut c = git_read(root);
    c.args(["log", "-1", "--format=%h%x00%s%x00%an%x00%ct"]);
    if let Some(p) = path {
        c.arg("--").arg(p);
    }
    let out = output(&mut c).ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&out.stdout);
    let f: Vec<&str> = text.trim_end_matches(['\r', '\n']).split('\0').collect();
    if f.len() < 4 || f[0].is_empty() {
        return None;
    }
    Some(Commit { sha: f[0].into(), message: f[1].into(), author: f[2].into(), time_ms: f[3].trim().parse::<i64>().ok()?.saturating_mul(1000) })
}

/// The latest commit touching each of `names` (children of `dir`, a folder inside the
/// repository at `root`), like Files' commit columns. One `git log` streamed newest first and
/// stopped as soon as every name is found (or after `max_commits`): untracked names just stay
/// missing. Keys are the names as given.
pub fn last_commits_of_children(root: &Path, dir: &Path, names: &[String], max_commits: usize) -> HashMap<String, Commit> {
    use std::io::BufRead;
    let mut out = HashMap::new();
    let Some(rel) = rel_inside(root, dir) else { return out };
    let prefix = if rel.is_empty() { String::new() } else { format!("{rel}/") };
    // git reports paths in its own case; Windows names match case-insensitively
    let wanted: HashMap<String, &String> = names.iter().map(|n| (n.to_lowercase(), n)).collect();
    if wanted.is_empty() {
        return out;
    }
    let mut c = git_read(root);
    c.args(["-c", "core.quotepath=false", "log", "--no-renames", "--name-only", "--format=%x01%h%x1f%s%x1f%an%x1f%ct", "--"])
        .arg(if rel.is_empty() { ".".to_string() } else { rel.clone() })
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        // a long history is background work: below-normal priority, no console window
        .creation_flags(CREATE_NO_WINDOW | 0x0000_4000);
    let Ok(mut child) = spawn(&mut c) else { return out };
    let Some(stdout) = child.stdout.take() else { return out };
    let mut current: Option<Commit> = None;
    let mut commits = 0usize;
    for line in std::io::BufReader::new(stdout).split(b'\n') {
        let Ok(line) = line else { break };
        let line = String::from_utf8_lossy(&line);
        let line = line.trim_end_matches('\r');
        if let Some(h) = line.strip_prefix('\u{1}') {
            commits += 1;
            if commits > max_commits {
                break;
            }
            let f: Vec<&str> = h.split('\u{1f}').collect();
            current = (f.len() >= 4).then(|| Commit {
                sha: f[0].into(),
                message: f[1].into(),
                author: f[2].into(),
                time_ms: f[3].trim().parse::<i64>().unwrap_or(0).saturating_mul(1000),
            });
            continue;
        }
        let (Some(cm), Some(rest)) = (&current, line.strip_prefix(prefix.as_str())) else { continue };
        let first = rest.split('/').next().unwrap_or("");
        if first.is_empty() {
            continue;
        }
        if let Some(name) = wanted.get(&first.to_lowercase()) {
            out.entry((*name).clone()).or_insert_with(|| cm.clone());
            if out.len() == wanted.len() {
                break;
            }
        }
    }
    let _ = child.kill();
    let _ = child.wait();
    out
}

/// `dir` relative to `root` with `/` separators ("" for the root itself); `None` outside it.
/// The root prefix matches case-insensitively (drive letters, Explorer-typed paths).
fn rel_inside(root: &Path, dir: &Path) -> Option<String> {
    let r = root.to_string_lossy().replace('\\', "/");
    let d = dir.to_string_lossy().replace('\\', "/");
    let (r, d) = (r.trim_end_matches('/'), d.trim_end_matches('/'));
    if d.len() < r.len() || !d.is_char_boundary(r.len()) || !d[..r.len()].eq_ignore_ascii_case(r) {
        return None;
    }
    let rest = &d[r.len()..];
    if rest.is_empty() {
        return Some(String::new());
    }
    rest.strip_prefix('/').map(str::to_string)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A repository whose own config runs programs (a clean filter, a file monitor): reading its
    /// status must not start them. Skipped without git.
    #[test]
    fn status_does_not_run_the_repos_programs() {
        let git = |dir: &Path, args: &[&str]| Command::new("git").args(args).current_dir(dir).creation_flags(CREATE_NO_WINDOW).output();
        let dir = std::env::temp_dir().join(format!("rf-git-evil-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        if git(&dir, &["init", "-q", "."]).is_err() {
            return;
        }
        let _ = git(&dir, &["-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "--allow-empty", "-m", "x"]);
        std::fs::write(dir.join("f.txt"), "a").unwrap();
        let _ = git(&dir, &["add", "f.txt"]);
        let _ = git(&dir, &["-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "-m", "y"]);
        let (m1, m2) = (dir.join("marker1"), dir.join("marker2"));
        let _ = git(&dir, &["config", "filter.evil.clean", &format!("cmd /c echo x> \"{}\" & more", m1.display())]);
        let _ = git(&dir, &["config", "core.fsmonitor", &format!("cmd /c echo x> \"{}\"", m2.display())]);
        std::fs::write(dir.join(".gitattributes"), "* filter=evil").unwrap();
        std::fs::write(dir.join("f.txt"), "b").unwrap();
        let st = status(&dir);
        assert!(st.is_ok(), "{st:?}");
        assert!(!m1.exists() && !m2.exists(), "a program from the repository's config ran");
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// The ways past a reading of `.git/config` of our own (a private security report): every one must leave the program unstarted.
    #[test]
    fn status_runs_no_filter_however_it_is_written() {
        let git = |dir: &Path, args: &[&str]| Command::new("git").args(args).current_dir(dir).creation_flags(CREATE_NO_WINDOW).output();
        for (case, how) in ["capitals", "included", "gitfile", "equals"].iter().enumerate() {
            let dir = std::env::temp_dir().join(format!("rf-git-evil2-{}-{case}", std::process::id()));
            let _ = std::fs::remove_dir_all(&dir);
            std::fs::create_dir_all(&dir).unwrap();
            if git(&dir, &["init", "-q", "."]).is_err() {
                return;
            }
            let _ = git(&dir, &["-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "--allow-empty", "-m", "x"]);
            std::fs::write(dir.join("f.txt"), "a").unwrap();
            let _ = git(&dir, &["add", "f.txt"]);
            let _ = git(&dir, &["-c", "user.email=t@t", "-c", "user.name=t", "commit", "-q", "-m", "y"]);
            let marker = dir.join("marker");
            let run = format!("cmd /c echo x> \"{}\" & more", marker.display()).replace('\\', "/");
            let gitdir = dir.join(".git");
            let name = if *how == "equals" { "a=b" } else { "evil" };
            let section = format!("[{} \"{name}\"]\n\tclean = {run}\n", if *how == "capitals" { "FILTER" } else { "filter" });
            match *how {
                "included" => {
                    std::fs::write(dir.join("more.cfg"), &section).unwrap();
                    let mut cfg = std::fs::read_to_string(gitdir.join("config")).unwrap();
                    cfg.push_str(&format!("[include]\n\tpath = {}\n", dir.join("more.cfg").display().to_string().replace('\\', "/")));
                    std::fs::write(gitdir.join("config"), cfg).unwrap();
                }
                "gitfile" => {
                    // the repository's folder elsewhere, a `.git` file pointing at it
                    let real = dir.join("real-git");
                    std::fs::rename(&gitdir, &real).unwrap();
                    std::fs::write(&gitdir, format!("gitdir: {}\n", real.display().to_string().replace('\\', "/"))).unwrap();
                    let mut cfg = std::fs::read_to_string(real.join("config")).unwrap();
                    cfg.push_str(&section);
                    std::fs::write(real.join("config"), cfg).unwrap();
                }
                _ => {
                    let mut cfg = std::fs::read_to_string(gitdir.join("config")).unwrap();
                    cfg.push_str(&section);
                    std::fs::write(gitdir.join("config"), cfg).unwrap();
                }
            }
            std::fs::write(dir.join(".gitattributes"), format!("* filter={name}")).unwrap();
            std::fs::write(dir.join("f.txt"), "b").unwrap();
            let _ = status(&dir);
            let _ = last_commit(&dir, None);
            assert!(!marker.exists(), "{how}: a filter from the repository's config ran");
            let _ = std::fs::remove_dir_all(&dir);
        }
    }

    /// A repository made to run a program when git looks at it. The program is a shell command (git runs these with its own shell
    /// on Windows too) that leaves a marker behind.
    struct Evil {
        tmp: PathBuf,
        repo: PathBuf,
    }

    impl Drop for Evil {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.tmp);
        }
    }

    impl Evil {
        /// `None` without git. `f.txt` is committed and then changed to as many bytes, so that git has to compare the contents.
        fn new(tag: &str) -> Option<Evil> {
            let tmp = std::env::temp_dir().join(format!("rf-git-evil-{tag}-{}", std::process::id()));
            let _ = std::fs::remove_dir_all(&tmp);
            let repo = tmp.join("repo");
            std::fs::create_dir_all(&repo).unwrap();
            let e = Evil { tmp, repo };
            if !git(&e.tmp, &["--version"]) {
                return None;
            }
            Self::fill(&e.repo);
            Some(e)
        }

        fn fill(repo: &Path) {
            assert!(git(repo, &["init", "-q"]));
            std::fs::write(repo.join("f.txt"), "a\n").unwrap();
            assert!(git(repo, &["add", "f.txt"]));
            assert!(git(repo, &["commit", "-q", "-m", "x"]));
            std::fs::write(repo.join("f.txt"), "b\n").unwrap();
        }

        fn marker(&self) -> PathBuf {
            self.tmp.join("ran")
        }

        /// The program, for a config file. Works as a filter and as any other program git starts.
        fn program(&self) -> String {
            format!("echo x > '{}'; :", self.marker().display().to_string().replace('\\', "/"))
        }

        /// The `clean =` line; the value is quoted, as `;` would start a comment.
        fn clean(&self) -> String {
            format!("clean = \"{}\"\n", self.program())
        }

        fn append(&self, file: &str, text: &str) {
            let path = self.repo.join(file);
            let old = std::fs::read_to_string(&path).unwrap_or_default();
            std::fs::write(path, old + text).unwrap();
        }

        /// Whether the program ran since the last call.
        fn ran(&self) -> bool {
            std::fs::remove_file(self.marker()).is_ok()
        }

        /// Plain git does run the program in `dir` (else the test shows nothing); our status must not, whatever its answer is.
        #[track_caller]
        fn check(&self, dir: &Path) -> Result<GitStatus> {
            let _ = self.ran();
            git(dir, &["--no-optional-locks", "status", "--porcelain=v2"]);
            assert!(self.ran(), "plain git does not run the program: the test is wrong");
            let st = status(dir);
            assert!(!self.ran(), "a program from the repository's config ran");
            st
        }

        /// [`check`](Self::check) for a filter in `.git/config` whose section is spelled `section` and that `.gitattributes` calls
        /// `attr`.
        #[track_caller]
        fn check_filter(tag: &str, section: &str, attr: &str) -> Option<Result<GitStatus>> {
            let e = Evil::new(tag)?;
            e.append(".git/config", &format!("{section} {}", e.clean()));
            e.append(".gitattributes", &format!("* filter={attr}\n"));
            Some(e.check(&e.repo))
        }
    }

    #[test]
    fn status_runs_no_filter_however_its_section_is_spelled() {
        for (i, (section, attr)) in [
            ("[filter \"ev\"]\n", "ev"),
            ("[filter \"ev\"]", "ev"),
            ("[Filter \"ev\"]\n", "ev"),
            ("[FILTER   \"ev\"] # c\n", "ev"),
            ("[filter.EV]\n", "ev"),
            ("[core] x = 1\n[filter \"ev\"]\n", "ev"),
            ("[filter \"e\\v\"]\n", "ev"),
            ("[filter \"a.b-c_d\"]\n", "a.b-c_d"),
            // `* filter=` uses the filter without a name
            ("[filter \"\"]\n", ""),
        ]
        .into_iter()
        .enumerate()
        {
            let Some(st) = Evil::check_filter(&format!("spelling{i}"), section, attr) else { return };
            let st = st.unwrap_or_else(|e| panic!("{section}: {e:#}"));
            assert_eq!(st.files.get(Path::new("f.txt")), Some(&FileState::Modified), "{section}");
        }
    }

    #[test]
    fn status_refuses_a_filter_whose_name_cannot_be_overridden() {
        for (i, (section, attr)) in [
            ("[filter \"a=b\"]\n", "a=b"),
            ("[filter \"a\\\"b\"]\n", "a\"b"),
            ("[filter \"a\\\\b\"]\n", "a\\b"),
            ("[filter \"a$b\"]\n", "a$b"),
            ("[filter \"süzgeç\"]\n", "süzgeç"),
        ]
        .into_iter()
        .enumerate()
        {
            let Some(st) = Evil::check_filter(&format!("name{i}"), section, attr) else { return };
            assert!(st.is_err(), "{section}");
        }
    }

    #[test]
    fn status_runs_no_smudge_or_process_filter() {
        let Some(e) = Evil::new("process") else { return };
        e.append(".git/config", &format!("[Filter \"ev\"]\nrequired = true\nprocess = \"{}\"\n", e.program()));
        e.append(".gitattributes", "* filter=ev\n");
        assert!(e.check(&e.repo).is_ok());
    }

    #[test]
    fn status_runs_no_filter_of_an_included_file() {
        for (i, (include, more)) in [
            ("[include]", ""),
            ("[includeIf \"gitdir:**\"]", ""),
            ("[includeIf \"hasconfig:remote.*.url:https://e/**\"]", "[remote \"o\"]\nurl = https://e/x\n"),
        ]
        .into_iter()
        .enumerate()
        {
            let Some(e) = Evil::new(&format!("include{i}")) else { return };
            e.append(".git/extra", &format!("[filter \"ev\"]\n{}", e.clean()));
            e.append(".git/config", &format!("{include}\npath = extra\n{more}"));
            e.append(".gitattributes", "* filter=ev\n");
            assert!(e.check(&e.repo).is_ok(), "{include}");
        }
    }

    #[test]
    fn status_runs_no_filter_of_a_worktrees_config() {
        // .git is a file there, and the config is the main repository's
        let Some(e) = Evil::new("linked") else { return };
        let linked = e.tmp.join("linked");
        assert!(git(&e.repo, &["worktree", "add", "-q", "../linked", "HEAD"]));
        e.append(".git/config", &format!("[filter \"ev\"]\n{}", e.clean()));
        std::fs::write(linked.join(".gitattributes"), "* filter=ev\n").unwrap();
        std::fs::write(linked.join("f.txt"), "b\n").unwrap();
        assert!(linked.join(".git").is_file());
        assert!(e.check(&linked).is_ok());

        let Some(e) = Evil::new("wtconfig") else { return };
        e.append(".git/config", "[extensions]\nworktreeConfig = true\n");
        e.append(".git/config.worktree", &format!("[filter \"ev\"]\n{}", e.clean()));
        e.append(".gitattributes", "* filter=ev\n");
        assert!(e.check(&e.repo).is_ok());
    }

    #[test]
    fn status_runs_no_filter_of_a_submodule() {
        let Some(e) = Evil::new("submodule") else { return };
        let sub = e.repo.join("sub");
        std::fs::create_dir_all(&sub).unwrap();
        Evil::fill(&sub);
        // the repository asking for it changes nothing: in .gitmodules, which comes with the files, and in its config
        e.append(".gitmodules", "[submodule \"sub\"]\npath = sub\nurl = ./sub\nignore = none\n");
        assert!(git(&e.repo, &["add", "sub", ".gitmodules"]));
        assert!(git(&e.repo, &["commit", "-q", "-m", "sub"]));
        e.append("sub/.git/config", &format!("[filter \"ev\"]\n{}", e.clean()));
        e.append(".git/config", "[diff]\nignoreSubmodules = none\n");
        e.append("sub/.gitattributes", "* filter=ev\n");
        let st = e.check(&e.repo).unwrap();
        assert_eq!(st.files.get(Path::new("sub")), None);
        // inside the submodule, its own config
        assert!(e.check(&sub).is_ok());
        // another commit checked out in it still shows
        assert!(git(&sub, &["-c", "filter.ev.clean=", "commit", "-q", "-a", "-m", "y"]));
        let _ = e.ran();
        assert_eq!(status(&e.repo).unwrap().files.get(Path::new("sub")), Some(&FileState::Modified));
        assert!(!e.ran());
    }

    #[test]
    fn reading_runs_no_diff_program() {
        let Some(e) = Evil::new("diff") else { return };
        e.append(
            ".git/config",
            &format!("[diff]\nexternal = \"{0}\"\n[diff \"ev\"]\ntextconv = \"{0}\"\ncommand = \"{0}\"\n[status]\nsubmoduleSummary = true\n", e.program()),
        );
        e.append(".gitattributes", "* diff=ev\n");
        git(&e.repo, &["diff"]);
        assert!(e.ran(), "plain git diff does not run the program: the test is wrong");
        assert!(status(&e.repo).is_ok());
        assert_eq!(last_commits_of_children(&e.repo, &e.repo, &["f.txt".into()], 10).len(), 1);
        assert!(last_commit(&e.repo, None).is_some());
        assert!(!e.ran());
    }

    #[test]
    fn reading_a_signed_commit_does_not_run_the_repos_gpg() {
        let Some(e) = Evil::new("gpg") else { return };
        let out = Command::new("git").args(["cat-file", "commit", "HEAD"]).current_dir(&e.repo).output().unwrap();
        let text = String::from_utf8(out.stdout).unwrap();
        let signed = text.replacen("\n\n", "\ngpgsig -----BEGIN PGP SIGNATURE-----\n \n x\n -----END PGP SIGNATURE-----\n\n", 1);
        std::fs::write(e.tmp.join("commit"), signed).unwrap();
        let out =
            Command::new("git").args(["hash-object", "-t", "commit", "-w", "--no-filters", "../commit"]).current_dir(&e.repo).output().unwrap();
        let id = String::from_utf8(out.stdout).unwrap();
        assert!(git(&e.repo, &["update-ref", "HEAD", id.trim()]));
        // gpg.program is started without a shell, so it cannot be a command line here: a script. Git writes it: one this process had
        // open for writing can be "text file busy" while another test starts a program.
        let script = e.repo.join("gpg.sh");
        std::fs::write(&script, format!("#!/bin/sh\n{}\n", e.program())).unwrap();
        assert!(git(&e.repo, &["add", "--chmod=+x", "gpg.sh"]));
        std::fs::remove_file(&script).unwrap();
        assert!(git(&e.repo, &["checkout-index", "gpg.sh"]));
        e.append(".git/config", &format!("[log]\nshowSignature = true\n[gpg]\nprogram = {}\n", script.display().to_string().replace('\\', "/")));
        git(&e.repo, &["log", "-1"]);
        assert!(e.ran(), "plain git log does not run the program: the test is wrong");
        assert_eq!(last_commit(&e.repo, None).unwrap().message, "x");
        assert_eq!(last_commits_of_children(&e.repo, &e.repo, &["f.txt".into()], 10).len(), 1);
        assert!(!e.ran());
    }

    #[test]
    fn reading_a_partial_clone_fetches_nothing() {
        let Some(e) = Evil::new("partial") else { return };
        let other = e.tmp.join("other");
        std::fs::create_dir_all(&other).unwrap();
        Evil::fill(&other);
        e.append(
            ".git/config",
            &format!(
                "[core]\nrepositoryformatversion = 1\n[extensions]\npartialClone = o\n[protocol \"file\"]\nallow = always\n\
                 [remote \"o\"]\npromisor = true\nurl = {}\nuploadpack = \"{}\"\n",
                other.display().to_string().replace('\\', "/"),
                e.program()
            ),
        );
        // without its tree HEAD cannot be compared: git wants to fetch it
        let out = Command::new("git").args(["rev-parse", "HEAD^{tree}"]).current_dir(&e.repo).output().unwrap();
        let tree = String::from_utf8(out.stdout).unwrap();
        let object = e.repo.join(".git/objects").join(&tree[..2]).join(tree[2..].trim());
        std::fs::rename(object, e.tmp.join("tree")).unwrap();
        assert!(e.check(&e.repo).is_err());
        let _ = last_commits_of_children(&e.repo, &e.repo, &["f.txt".into()], 10);
        let _ = last_commit(&e.repo, Some(Path::new("f.txt")));
        assert!(!e.ran());
        // a Git that the environment does not reach (one in WSL): the arguments alone are enough
        let mut c = git_read(&e.repo);
        c.env_remove("GIT_NO_LAZY_FETCH").env_remove("GIT_ALLOW_PROTOCOL").args(["status", "--porcelain"]);
        assert!(!c.output().unwrap().status.success());
        assert!(!e.ran());
    }

    #[test]
    fn status_of_a_partial_clone_does_not_need_the_missing_contents() {
        let Some(e) = Evil::new("partial-rename") else { return };
        std::fs::write(e.repo.join("old"), "line\n".repeat(50)).unwrap();
        assert!(git(&e.repo, &["add", "old"]) && git(&e.repo, &["commit", "-q", "-m", "y"]));
        let out = Command::new("git").args(["rev-parse", "HEAD:old"]).current_dir(&e.repo).output().unwrap();
        let blob = String::from_utf8(out.stdout).unwrap();
        assert!(git(&e.repo, &["mv", "old", "new"]));
        std::fs::write(e.repo.join("new"), "line\n".repeat(51)).unwrap();
        assert!(git(&e.repo, &["add", "new"]));
        e.append(".git/config", "[remote \"o\"]\npromisor = true\npartialclonefilter = blob:none\nurl = https://e.invalid/x\n");
        let object = e.repo.join(".git/objects").join(&blob[..2]).join(blob[2..].trim());
        std::fs::rename(object, e.tmp.join("blob")).unwrap();
        let st = status(&e.repo).unwrap();
        assert_eq!(st.files.get(Path::new("new")), Some(&FileState::Added));
    }

    /// The `-c` values of the filters that are switched off, one of them per name.
    fn off(listing: &str, scoped: bool) -> Vec<String> {
        let o = overrides_from(listing.as_bytes(), scoped).unwrap();
        assert!(o.args.len().is_multiple_of(8));
        o.args.iter().filter_map(|a| Some(a.strip_prefix("filter.")?.strip_suffix(".clean=")?.to_string())).collect()
    }

    #[test]
    fn only_the_repositorys_own_filters_are_switched_off() {
        let users = "system\0filter.lfs.clean\0global\0filter.crypt.process\0command\0filter.c.clean\0";
        assert!(off(users, true).is_empty());
        assert_eq!(off(&format!("{users}local\0filter.ev.smudge\0worktree\0filter.wt.required\0"), true), ["ev", "wt"]);
        assert_eq!(off(&format!("{users}local\0filter.lfs.clean\0"), true), ["lfs"]);
        // a scope git may add one day is not the user's
        assert_eq!(off("new\0filter.ev.clean\0", true), ["ev"]);
        // an odd name is only in the way where the repository made it
        assert!(off("global\0filter.a=b.clean\0", true).is_empty());
        assert!(overrides_from(b"local\0filter.a=b.clean\0", true).is_err());
        assert!(overrides_from(b"local\0filter.\xff.clean\0", true).is_err());
        assert!(overrides_from(b"local\0filter.ev.clean\0local\0", true).is_err(), "cut short");
        // no filter
        assert!(off("local\0filter.clean\0local\0core.bare\0local\0filterx.a.clean\0", true).is_empty());
        assert!(off("", true).is_empty());
    }

    #[test]
    fn every_filter_is_switched_off_when_scopes_are_unknown_or_the_repository_extends_lfs() {
        assert_eq!(off("filter.lfs.clean\0core.bare\0filter.ev.clean\0", false), ["ev", "lfs"]);
        let users = "system\0filter.lfs.clean\0global\0filter.media.clean\0";
        assert_eq!(off(&format!("{users}local\0lfs.Extension.x.clean\0"), true), ["lfs", "media"]);
        assert!(off(&format!("{users}global\0lfs.extension.x.clean\0local\0lfs.url\0"), true).is_empty());
    }

    #[test]
    fn a_shells_greeting_before_the_listing_hides_no_filter() {
        // the user's own filter stays the user's, the repository's stays the repository's
        assert!(off("Welcome to Ubuntu\n\nsystem\0filter.lfs.clean\0", true).is_empty());
        assert_eq!(off("Welcome\nlocal\0filter.ev.clean\0", true), ["ev"]);
        assert_eq!(off("Welcome> local\0filter.ev.clean\0", true), ["ev"]);
        assert_eq!(off("Welcome> system\0filter.ev.clean\0", true), ["ev"], "not known to be the user's");
        assert_eq!(off("Welcome\nfilter.ev.clean\0core.bare\0", false), ["ev"]);
        assert!(overrides_from(b"Welcome> filter.ev.clean\0core.bare\0", false).is_err());
        assert_eq!(off("Welcome> core.bare\0filter.ev.clean\0", false), ["ev"]);
    }

    #[test]
    fn a_listing_that_is_cut_short_is_no_listing() {
        let end = "command\0core.fsmonitor\0command\0core.untrackedcache\0";
        assert!(whole(format!("local\0filter.ev.clean\0{end}").as_bytes(), true).is_ok());
        assert!(whole(b"", true).is_err());
        assert!(whole(b"system\0filter.lfs.clean\0", true).is_err());
        assert!(whole(b"local\0core.untrackedcache\0", true).is_err(), "the repository's own setting of that name");
        assert!(whole(b"core.bare\0core.fsmonitor\0core.untrackedcache\0", false).is_ok());
        assert!(whole(b"core.bare\0", false).is_err());
    }

    /// What is run while browsing must get past the guard for a chosen program that may use a shell, or there would be no status.
    #[test]
    fn a_chosen_program_is_given_our_commands() {
        let root = Path::new(".");
        let o = overrides_from(b"local\0filter.lfs.clean\0local\0filter.a.b-c_d.process\0local\0filter..clean\0", true).unwrap();
        let refused = |c: &Command| git_program::refused(c.get_args());
        let mut c = git_base(root);
        assert_eq!(refused(c.args(["config", "--list", "-z", "--name-only", "--show-scope"])), None);
        let mut c = git_read(root);
        assert_eq!(refused(c.args(&o.args).args(STATUS).arg(EXACT_RENAMES)), None);
        let mut c = git_read(root);
        assert_eq!(refused(c.args(["log", "-1", "--format=%h%x00%s%x00%an%x00%ct", "--", "src/a b.txt"])), None);
        // a name with a space is one it might change on the way: refused, so nothing runs
        let o = overrides_from(b"local\0filter.a b.clean\0", true).unwrap();
        let mut c = git_read(root);
        assert!(refused(c.args(&o.args).args(STATUS)).is_some());
    }

    #[test]
    fn a_partial_clone_is_recognised() {
        for key in ["extensions.partialclone", "remote.o.promisor", "remote.o.partialclonefilter"] {
            assert!(overrides_from(format!("local\0{key}\0").as_bytes(), true).unwrap().partial, "{key}");
        }
        assert!(!overrides_from(b"local\0remote.o.url\0", true).unwrap().partial);
    }

    #[test]
    fn parses_synthetic_output() {
        let raw = b"# branch.oid 0123456789abcdef\0# branch.head main\0# branch.upstream origin/main\0# branch.ab +2 -1\0\
1 .M N... 100644 100644 100644 aaaa bbbb src/a file.rs\0\
1 A. N... 000000 100644 100644 0000 cccc new.txt\0\
1 .D N... 100644 100644 000000 aaaa aaaa gone.txt\0\
2 R. N... 100644 100644 100644 aaaa bbbb R100 dir/new name.txt\0dir/old name.txt\0\
u UU N... 100644 100644 100644 100644 a b c conflict.txt\0\
? untracked dir/\0\
! target/\0";
        let st = parse_porcelain_v2(Path::new(r"C:\repo"), raw);
        assert_eq!((st.branch.as_str(), st.ahead, st.behind), ("main", 2, 1));
        assert_eq!(st.files[Path::new("src/a file.rs")], FileState::Modified);
        assert_eq!(st.files[Path::new("new.txt")], FileState::Added);
        assert_eq!(st.files[Path::new("gone.txt")], FileState::Deleted);
        assert_eq!(st.files[Path::new("dir/new name.txt")], FileState::Renamed);
        assert!(!st.files.contains_key(Path::new("dir/old name.txt")));
        assert_eq!(st.files[Path::new("conflict.txt")], FileState::Conflicted);
        assert_eq!(st.files[Path::new("untracked dir")], FileState::Untracked);
        assert_eq!(st.files[Path::new("target")], FileState::Ignored);
        assert!(!st.is_clean());

        let root = Path::new(r"C:\repo");
        assert_eq!(state_for_child(&st, root, "src"), Some(FileState::Modified));
        // Only the repo-root prefix is matched case-insensitively; names inside use git's case.
        assert_eq!(state_for_child(&st, &Path::new(r"c:\REPO").join("src"), "a file.rs"), Some(FileState::Modified));
        assert_eq!(state_for_child(&st, &root.join("untracked dir"), "deep.txt"), Some(FileState::Untracked));
        assert_eq!(state_for_child(&st, root, "target"), Some(FileState::Ignored));
        assert_eq!(state_for_child(&st, root, "README.md"), None);
        assert_eq!(state_for_child(&st, Path::new(r"D:\other"), "src"), None);
    }

    #[test]
    fn remotes_out_of_the_configuration() {
        let keys = "core.bare\0remote.upstream.url\0remote.origin.url\0remote.origin.fetch\0remote.origin.url\0remote.my.fork.url\0remote.pushdefault\0";
        assert_eq!(remotes_in_config(keys), ["my.fork", "origin", "upstream"]);
        assert!(remotes_in_config("core.bare\0branch.main.remote\0").is_empty());
    }

    #[test]
    fn detached_and_empty() {
        let st = parse_porcelain_v2(Path::new("."), b"# branch.oid abcdef0123\0# branch.head (detached)\0");
        assert_eq!(st.branch, "HEAD (abcdef0)");
        assert!(st.is_clean());
    }

    #[test]
    fn finds_root() {
        let base = std::env::temp_dir().join(format!("rf-git-root-{}", std::process::id()));
        std::fs::create_dir_all(base.join("r/.git")).unwrap();
        std::fs::create_dir_all(base.join("r/x/y")).unwrap();
        assert_eq!(repo_root(&base.join("r/x/y")), Some(base.join("r")));
        assert_eq!(repo_root(&base.join("r")), Some(base.join("r")));
        std::fs::remove_dir_all(&base).unwrap();
    }

    fn git(dir: &Path, args: &[&str]) -> bool {
        Command::new("git")
            .args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false", "-c", "core.autocrlf=false"])
            .args(args)
            .current_dir(dir)
            .stdin(Stdio::null())
            .creation_flags(CREATE_NO_WINDOW)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    #[test]
    fn real_repo() {
        let tmp = std::env::temp_dir().join(format!("rf-git-real-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();
        let remote = tmp.join("remote.git");
        let repo = tmp.join("repo");
        std::fs::create_dir_all(&repo).unwrap();
        if !git(&tmp, &["--version"]) {
            eprintln!("git yok: test atlandı");
            let _ = std::fs::remove_dir_all(&tmp);
            return;
        }
        assert!(git(&tmp, &["init", "-q", "--bare", "remote.git"]));
        assert!(git(&repo, &["init", "-q"]));
        assert!(git(&repo, &["symbolic-ref", "HEAD", "refs/heads/main"]));
        std::fs::create_dir_all(repo.join("sub")).unwrap();
        std::fs::create_dir_all(repo.join("deep/er")).unwrap();
        for (f, c) in [
            ("a.txt", "a\n"),
            ("b.txt", "b\n"),
            ("sub/c.txt", "c\n"),
            ("deep/er/d.txt", "d\n"),
            (".gitignore", "*.log\n"),
        ] {
            std::fs::write(repo.join(f), c).unwrap();
        }
        assert!(git(&repo, &["add", "-A"]));
        assert!(git(&repo, &["commit", "-q", "-m", "ilk"]));
        assert!(git(&repo, &["remote", "add", "origin", remote.to_str().unwrap()]));
        assert!(git(&repo, &["push", "-q", "-u", "origin", "main"]));
        assert!(status(&repo).unwrap().is_clean());

        // ahead by one
        std::fs::write(repo.join("a.txt"), "a2\n").unwrap();
        assert!(git(&repo, &["commit", "-q", "-am", "ikinci"]));

        // working tree states
        std::fs::write(repo.join("a.txt"), "a3\n").unwrap(); // Modified
        std::fs::write(repo.join("added.txt"), "x\n").unwrap();
        assert!(git(&repo, &["add", "added.txt"])); // Added
        std::fs::remove_file(repo.join("b.txt")).unwrap(); // Deleted
        assert!(git(&repo, &["mv", "sub/c.txt", "sub/c2.txt"])); // Renamed
        std::fs::write(repo.join("untracked.txt"), "u\n").unwrap(); // Untracked
        std::fs::write(repo.join("noise.log"), "l\n").unwrap(); // Ignored
        std::fs::write(repo.join("deep/er/d.txt"), "d2\n").unwrap(); // nested modification

        let st = status(&repo).unwrap();
        assert_eq!(st.branch, "main");
        assert_eq!((st.ahead, st.behind), (1, 0));
        let f = |p: &str| st.files.get(Path::new(p)).copied();
        assert_eq!(f("a.txt"), Some(FileState::Modified));
        assert_eq!(f("added.txt"), Some(FileState::Added));
        assert_eq!(f("b.txt"), Some(FileState::Deleted));
        assert_eq!(f("sub/c2.txt"), Some(FileState::Renamed));
        assert_eq!(f("untracked.txt"), Some(FileState::Untracked));
        assert_eq!(f("noise.log"), Some(FileState::Ignored));
        assert_eq!(state_for_child(&st, &repo, "sub"), Some(FileState::Modified));
        assert_eq!(state_for_child(&st, &repo, "deep"), Some(FileState::Modified));
        assert_eq!(state_for_child(&st, &repo.join("deep"), "er"), Some(FileState::Modified));
        assert_eq!(state_for_child(&st, &repo, ".gitignore"), None);

        // merge conflict
        assert!(git(&repo, &["add", "-A"]));
        assert!(git(&repo, &["commit", "-q", "-m", "ucuncu"]));
        assert!(git(&repo, &["checkout", "-q", "-b", "yan", "HEAD~1"]));
        std::fs::write(repo.join("a.txt"), "yan\n").unwrap();
        assert!(git(&repo, &["commit", "-q", "-am", "yan"]));
        assert!(!git(&repo, &["merge", "main"])); // conflicts => non-zero
        let st = status(&repo).unwrap();
        assert_eq!(st.files.get(Path::new("a.txt")).copied(), Some(FileState::Conflicted));
        assert_eq!(st.branch, "yan");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    // ---- branches / checkout / network ----

    fn git_out(dir: &Path, args: &[&str]) -> String {
        let o = Command::new("git").args(args).current_dir(dir).stdin(Stdio::null()).creation_flags(CREATE_NO_WINDOW).output().unwrap();
        String::from_utf8_lossy(&o.stdout).trim().to_string()
    }

    /// Local repo identity so stash / merge commits work without a global git config.
    fn configure(dir: &Path) {
        assert!(git(dir, &["config", "user.name", "t"]));
        assert!(git(dir, &["config", "user.email", "t@t"]));
        assert!(git(dir, &["config", "commit.gpgsign", "false"]));
        assert!(git(dir, &["config", "core.autocrlf", "false"]));
    }

    struct Fixture {
        tmp: PathBuf,
        remote: PathBuf,
        repo: PathBuf,
    }

    impl Drop for Fixture {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.tmp);
        }
    }

    /// bare `remote.git` (origin) + `repo` with two files committed on `main` and pushed.
    fn fixture(name: &str) -> Option<Fixture> {
        let tmp = std::env::temp_dir().join(format!("rf-git-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();
        if !git(&tmp, &["--version"]) {
            eprintln!("git yok: test atlandı");
            let _ = std::fs::remove_dir_all(&tmp);
            return None;
        }
        let remote = tmp.join("remote.git");
        let repo = tmp.join("repo");
        std::fs::create_dir_all(&repo).unwrap();
        assert!(git(&tmp, &["init", "-q", "--bare", "remote.git"]));
        assert!(git(&remote, &["symbolic-ref", "HEAD", "refs/heads/main"]));
        assert!(git(&repo, &["init", "-q"]));
        assert!(git(&repo, &["symbolic-ref", "HEAD", "refs/heads/main"]));
        configure(&repo);
        std::fs::write(repo.join("a.txt"), "a\n").unwrap();
        std::fs::write(repo.join("b.txt"), "b\n").unwrap();
        assert!(git(&repo, &["add", "-A"]));
        assert!(git(&repo, &["commit", "-q", "-m", "ilk"]));
        assert!(git(&repo, &["remote", "add", "origin", remote.to_str().unwrap()]));
        assert!(git(&repo, &["push", "-q", "-u", "origin", "main"]));
        Some(Fixture { tmp, remote, repo })
    }

    /// A second clone of the remote, to simulate another user.
    fn second_clone(fx: &Fixture) -> PathBuf {
        let other = fx.tmp.join("other");
        assert!(git(&fx.tmp, &["clone", "-q", fx.remote.to_str().unwrap(), "other"]));
        configure(&other);
        other
    }

    #[test]
    fn branch_name_validation() {
        let ok = ["main", "feature/x", "fix-1", "a.b", "v1.0.0", "ünicode", "a@b", "x/y/z"];
        let bad = [
            "", " ", "a b", "a..b", "a~b", "a^b", "a:b", "a?b", "a*b", "a[b", "a\\b", "-x", "/x", "x/", "x.lock", "x.", "a@{b", "@",
            "a//b", ".hidden", "a/.b", "a.lock/b", "tab\tx",
        ];
        for n in ok {
            assert!(is_valid_branch_name(n), "{n:?} geçerli olmalı");
        }
        for n in bad {
            assert!(!is_valid_branch_name(n), "{n:?} geçersiz olmalı");
        }
    }

    #[test]
    fn create_delete_and_list_branches() {
        let Some(fx) = fixture("branches") else { return };
        let repo = &fx.repo;
        assert!(create_branch(repo, "bad name", None, false).is_err());
        create_branch(repo, "zeta", None, false).unwrap();
        create_branch(repo, "alpha", Some("main"), true).unwrap();
        assert!(create_branch(repo, "alpha", None, false).is_err()); // exists

        let list = branches(repo).unwrap();
        let names: Vec<(&str, bool, bool)> = list.iter().map(|b| (b.name.as_str(), b.is_head, b.is_remote)).collect();
        // HEAD (alpha) first, then the rest by name, then remotes.
        assert_eq!(names, [("alpha", true, false), ("main", false, false), ("zeta", false, false), ("origin/main", false, true)]);
        let main = list.iter().find(|b| b.name == "main").unwrap();
        assert_eq!(main.upstream.as_deref(), Some("origin/main"));
        assert!(list.iter().all(|b| !b.name.ends_with("HEAD")));

        // ahead of upstream
        checkout(repo, "main", CheckoutMode::Normal).unwrap();
        std::fs::write(repo.join("a.txt"), "a2\n").unwrap();
        assert!(git(repo, &["commit", "-q", "-am", "iki"]));
        let main = branches(repo).unwrap().into_iter().find(|b| b.name == "main").unwrap();
        assert_eq!((main.ahead, main.behind), (1, 0));

        // delete: unmerged needs force
        checkout(repo, "alpha", CheckoutMode::Normal).unwrap();
        assert!(delete_branch(repo, "alpha", false).is_err()); // current branch
        checkout(repo, "main", CheckoutMode::Normal).unwrap();
        delete_branch(repo, "alpha", false).unwrap(); // merged (same commit as main's parent)
        assert!(git(repo, &["switch", "-q", "-c", "unmerged"]));
        std::fs::write(repo.join("u.txt"), "u\n").unwrap();
        assert!(git(repo, &["add", "u.txt"]));
        assert!(git(repo, &["commit", "-q", "-m", "u"]));
        checkout(repo, "main", CheckoutMode::Normal).unwrap();
        assert!(delete_branch(repo, "unmerged", false).is_err());
        delete_branch(repo, "unmerged", true).unwrap();
        assert!(!branches(repo).unwrap().iter().any(|b| b.name == "unmerged"));
    }

    #[test]
    fn checkout_remote_branch_tracks_it() {
        let Some(fx) = fixture("remote-co") else { return };
        let other = second_clone(&fx);
        assert!(git(&other, &["switch", "-q", "-c", "feat"]));
        std::fs::write(other.join("f.txt"), "f\n").unwrap();
        assert!(git(&other, &["add", "f.txt"]));
        assert!(git(&other, &["commit", "-q", "-m", "feat"]));
        assert!(git(&other, &["push", "-q", "origin", "feat"]));

        let mut lines = Vec::new();
        fetch(&fx.repo, &mut |l| lines.push(l.to_string())).unwrap();
        assert!(branches(&fx.repo).unwrap().iter().any(|b| b.is_remote && b.name == "origin/feat"));

        checkout(&fx.repo, "origin/feat", CheckoutMode::Normal).unwrap();
        assert_eq!(status(&fx.repo).unwrap().branch, "feat");
        assert!(fx.repo.join("f.txt").exists());
        let feat = branches(&fx.repo).unwrap().into_iter().find(|b| b.name == "feat").unwrap();
        assert!(feat.is_head);
        assert_eq!(feat.upstream.as_deref(), Some("origin/feat"));

        // Local `feat` exists now: selecting the remote ref again just switches.
        checkout(&fx.repo, "main", CheckoutMode::Normal).unwrap();
        checkout(&fx.repo, "origin/feat", CheckoutMode::Normal).unwrap();
        assert_eq!(status(&fx.repo).unwrap().branch, "feat");

        assert!(checkout(&fx.repo, "nope", CheckoutMode::Normal).is_err());
        assert!(checkout(&fx.repo, "-x", CheckoutMode::Normal).is_err());
    }

    /// `other` branch changes a.txt; main has a dirty a.txt => conflicting switch.
    fn conflicting_setup(repo: &Path) {
        assert!(git(repo, &["switch", "-q", "-c", "other"]));
        std::fs::write(repo.join("a.txt"), "other\n").unwrap();
        assert!(git(repo, &["commit", "-q", "-am", "other"]));
        assert!(git(repo, &["switch", "-q", "main"]));
        std::fs::write(repo.join("a.txt"), "dirty\n").unwrap();
    }

    #[test]
    fn checkout_normal_conflict_has_turkish_error() {
        let Some(fx) = fixture("co-conflict") else { return };
        conflicting_setup(&fx.repo);
        let err = checkout(&fx.repo, "other", CheckoutMode::Normal).unwrap_err().to_string();
        assert!(err.contains("çakışıyor"), "{err}");
        assert!(err.contains("a.txt"), "git stderr eklenmeli: {err}");
        assert_eq!(status(&fx.repo).unwrap().branch, "main");
    }

    #[test]
    fn checkout_discard_overwrites() {
        let Some(fx) = fixture("co-discard") else { return };
        conflicting_setup(&fx.repo);
        checkout(&fx.repo, "other", CheckoutMode::Discard).unwrap();
        assert_eq!(std::fs::read_to_string(fx.repo.join("a.txt")).unwrap().trim(), "other");
        assert!(status(&fx.repo).unwrap().is_clean());
    }

    #[test]
    fn checkout_stash_carries_changes() {
        let Some(fx) = fixture("co-stash") else { return };
        let repo = &fx.repo;
        assert!(git(repo, &["branch", "other"]));
        std::fs::write(repo.join("b.txt"), "dirty b\n").unwrap();
        std::fs::write(repo.join("new.txt"), "untracked\n").unwrap();
        checkout(repo, "other", CheckoutMode::Stash).unwrap();
        assert_eq!(status(repo).unwrap().branch, "other");
        assert_eq!(std::fs::read_to_string(repo.join("b.txt")).unwrap().trim(), "dirty b");
        assert_eq!(std::fs::read_to_string(repo.join("new.txt")).unwrap().trim(), "untracked");
        assert_eq!(git_out(repo, &["stash", "list"]), ""); // popped

        // Clean tree: no stash needed.
        assert!(git(repo, &["checkout", "-q", "--", "b.txt"]));
        std::fs::remove_file(repo.join("new.txt")).unwrap();
        checkout(repo, "main", CheckoutMode::Stash).unwrap();
        assert_eq!(status(repo).unwrap().branch, "main");
    }

    #[test]
    fn push_pull_sync_with_bare_origin() {
        let Some(fx) = fixture("net") else { return };
        let repo = &fx.repo;

        // push: new branch without upstream gets -u
        create_branch(repo, "topic", None, true).unwrap();
        std::fs::write(repo.join("t.txt"), "t\n").unwrap();
        assert!(git(repo, &["add", "t.txt"]));
        assert!(git(repo, &["commit", "-q", "-m", "topic"]));
        let mut lines = Vec::new();
        push(repo, &mut |l| lines.push(l.to_string())).unwrap();
        assert!(git(&fx.remote, &["rev-parse", "--verify", "-q", "refs/heads/topic"]));
        let topic = branches(repo).unwrap().into_iter().find(|b| b.name == "topic").unwrap();
        assert_eq!(topic.upstream.as_deref(), Some("origin/topic"));
        assert!(!lines.is_empty(), "ilerleme satırları gelmeli");
        assert!(lines.iter().all(|l| !l.is_empty()));
        checkout(repo, "main", CheckoutMode::Normal).unwrap();

        // pull: fast-forward from another clone
        let other = second_clone(&fx);
        std::fs::write(other.join("o1.txt"), "o1\n").unwrap();
        assert!(git(&other, &["add", "o1.txt"]));
        assert!(git(&other, &["commit", "-q", "-m", "o1"]));
        assert!(git(&other, &["push", "-q", "origin", "main"]));
        pull(repo, &mut |_| {}).unwrap();
        assert!(repo.join("o1.txt").exists());

        // sync: diverged histories => merge, then push
        std::fs::write(other.join("o2.txt"), "o2\n").unwrap();
        assert!(git(&other, &["add", "o2.txt"]));
        assert!(git(&other, &["commit", "-q", "-m", "o2"]));
        assert!(git(&other, &["push", "-q", "origin", "main"]));
        std::fs::write(repo.join("l.txt"), "l\n").unwrap();
        assert!(git(repo, &["add", "l.txt"]));
        assert!(git(repo, &["commit", "-q", "-m", "local"]));
        sync(repo, &mut |_| {}).unwrap();
        assert!(repo.join("o2.txt").exists() && repo.join("l.txt").exists());
        assert_eq!(git_out(repo, &["rev-parse", "HEAD"]), git_out(&fx.remote, &["rev-parse", "refs/heads/main"]));
        let main = branches(repo).unwrap().into_iter().find(|b| b.name == "main").unwrap();
        assert_eq!((main.ahead, main.behind), (0, 0));

        assert_eq!(remote_url(repo).as_deref(), fx.remote.to_str());
    }

    #[test]
    fn network_errors_are_clear() {
        let Some(fx) = fixture("net-err") else { return };
        // No remote at all.
        let lonely = fx.tmp.join("lonely");
        init(&lonely).unwrap();
        configure(&lonely);
        assert!(lonely.join(".git").exists());
        assert_eq!(remote_url(&lonely), None);
        for r in [fetch(&lonely, &mut |_| {}), pull(&lonely, &mut |_| {}), push(&lonely, &mut |_| {}), sync(&lonely, &mut |_| {})] {
            assert!(r.unwrap_err().to_string().contains("Uzak depo tanımlı değil"));
        }
        // Unreachable remote: error carries git's message.
        assert!(git(&lonely, &["remote", "add", "origin", fx.tmp.join("yok.git").to_str().unwrap()]));
        let err = fetch(&lonely, &mut |_| {}).unwrap_err().to_string();
        assert!(err.starts_with("git fetch başarısız: "), "{err}");
        assert!(err.len() > "git fetch başarısız: ".len());
    }

    #[test]
    fn init_commit_connect_push() {
        let Some(fx) = fixture("connect") else { return };
        // a new folder with files, never under git: init, commit, connect, push
        let dir = fx.tmp.join("yeni proje");
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("not.txt"), "merhaba\n").unwrap();
        init(&dir).unwrap();
        configure(&dir);
        assert!(has_changes(&dir));
        assert!(commit_all(&dir, "   ").is_err(), "empty message refused");
        commit_all(&dir, "İlk kayıt").unwrap();
        assert!(!has_changes(&dir));
        assert!(commit_all(&dir, "boş").is_err(), "nothing to commit");
        assert!(remote_url(&dir).is_none());
        let bare = fx.tmp.join("yeni.git");
        assert!(git(&fx.tmp, &["init", "-q", "--bare", "yeni.git"]));
        connect_remote(&dir, bare.to_str().unwrap()).unwrap();
        assert_eq!(remote_url(&dir).as_deref(), bare.to_str());
        push(&dir, &mut |_| {}).unwrap();
        let branch = git_out(&dir, &["symbolic-ref", "--short", "HEAD"]);
        assert_eq!(git_out(&bare, &["rev-parse", &branch]), git_out(&dir, &["rev-parse", "HEAD"]));
        // connecting again changes the address instead of failing
        connect_remote(&dir, fx.remote.to_str().unwrap()).unwrap();
        assert_eq!(remote_url(&dir).as_deref(), fx.remote.to_str());
        assert!(connect_remote(&dir, "  ").is_err());
    }

    #[test]
    fn clone_returns_new_folder() {
        let Some(fx) = fixture("clone") else { return };
        let parent = fx.tmp.join("dest");
        let got = clone(fx.remote.to_str().unwrap(), &parent, &mut |_| {}).unwrap();
        assert_eq!(got, parent.join("remote"));
        assert_eq!(repo_root(&got), Some(got.clone()));
        assert!(got.join("a.txt").exists());
        assert_eq!(status(&got).unwrap().branch, "main");
        // Existing non-empty target => clear failure.
        assert!(clone(fx.remote.to_str().unwrap(), &parent, &mut |_| {}).is_err());
    }

    #[test]
    fn last_commit_for_repo_and_path() {
        let Some(fx) = fixture("last") else { return };
        let repo = &fx.repo;
        std::fs::write(repo.join("b.txt"), "b2\n").unwrap();
        assert!(git(repo, &["commit", "-q", "-am", "b değişti"]));
        let head = last_commit(repo, None).unwrap();
        assert_eq!((head.message.as_str(), head.author.as_str()), ("b değişti", "t"));
        assert!(!head.sha.is_empty() && head.sha.len() <= 12);
        let now_ms = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis() as i64;
        assert!((now_ms - head.time_ms).abs() < 5 * 60 * 1000);
        let a = last_commit(repo, Some(Path::new("a.txt"))).unwrap();
        assert_eq!(a.message, "ilk");
        assert_ne!(a.sha, head.sha);
        assert_eq!(last_commit(repo, Some(Path::new("yok.txt"))), None);
        assert_eq!(last_commit(&fx.tmp, None), None); // not a repo
    }

    #[test]
    fn last_commits_of_children_per_item() {
        let Some(fx) = fixture("children") else { return };
        let repo = &fx.repo;
        std::fs::create_dir_all(repo.join("klasör/alt")).unwrap();
        std::fs::write(repo.join("klasör/alt/derin.txt"), "d\n").unwrap();
        std::fs::write(repo.join("klasör/üst.txt"), "u\n").unwrap();
        assert!(git(repo, &["add", "-A"]));
        assert!(git(repo, &["commit", "-q", "-m", "klasör eklendi"]));
        std::fs::write(repo.join("b.txt"), "b2\n").unwrap();
        assert!(git(repo, &["commit", "-q", "-am", "b değişti"]));
        std::fs::write(repo.join("izlenmeyen.txt"), "x").unwrap();
        let names: Vec<String> = ["a.txt", "B.TXT", "klasör", "izlenmeyen.txt"].map(String::from).to_vec();
        let got = last_commits_of_children(repo, repo, &names, 1000);
        assert_eq!(got["a.txt"].message, "ilk");
        assert_eq!(got["B.TXT"].message, "b değişti"); // case-insensitive like Windows
        assert_eq!(got["klasör"].message, "klasör eklendi"); // a folder: its newest file
        assert!(!got.contains_key("izlenmeyen.txt"));
        // inside a subfolder, the path typed in another case
        let sub = PathBuf::from(repo.to_string_lossy().to_uppercase()).join("klasör");
        let got = last_commits_of_children(repo, &sub, &["alt".into(), "üst.txt".into()], 1000);
        assert_eq!(got.len(), 2);
        assert_eq!(rel_inside(Path::new(r"C:\r"), Path::new(r"c:\R\a\b")).as_deref(), Some("a/b"));
        assert_eq!(rel_inside(Path::new(r"C:\r"), Path::new(r"C:\rx")), None);
    }
}