All your machines, one app. For you and your AI agents: agent sessions, desktops, files, logs and GitHub across your Macs, Linux and Windows machines, with an MCP server.
Windows: find the GitHub CLI, and know its credential helper
Adib Hannad44796f2f321
diff --git a/agent/src/repos.rs b/agent/src/repos.rsindex 36ae384..4214622 100644--- a/agent/src/repos.rs+++ b/agent/src/repos.rs@@ -250,10 +250,22 @@ fn run_with_timeout(cmd: &mut Command, limit: Duration) -> Result<(bool, String,}}+/// The GitHub CLI in those PATH folders (gh.exe on Windows), else where its+/// Windows installer puts it: a PATH read before it was installed (a+/// long-running agent's) doesn't have it yet.+fn find_gh(path_dirs: &[String]) -> Option<PathBuf> {+ let name = if cfg!(windows) { "gh.exe" } else { "gh" };+ let mut places: Vec<PathBuf> = path_dirs.iter().map(|d| Path::new(d).join(name)).collect();+ if cfg!(windows) {+ let programs = std::env::var_os("ProgramFiles").map(PathBuf::from).unwrap_or_else(|| PathBuf::from(r"C:\Program Files"));+ places.push(programs.join("GitHub CLI").join("gh.exe"));+ }+ places.into_iter().find(|p| p.is_file())+}+/// Whether the GitHub CLI is here and signed in (for agents that use it).pub fn gh_status(path_dirs: &[String]) -> Value {- let gh = path_dirs.iter().map(|d| Path::new(d).join("gh")).find(|p| p.is_file());- let Some(gh) = gh else { return json!({ "installed": false }) };+ let Some(gh) = find_gh(path_dirs) else { return json!({ "installed": false }) };let out = run_with_timeout(Command::new(&gh).args(["auth", "status", "--hostname", "github.com"]), Duration::from_secs(8));let (ok, stdout, stderr) = out.unwrap_or((false, String::new(), String::new()));let text = format!("{stdout}{stderr}");@@ -270,6 +282,19 @@ pub fn gh_status(path_dirs: &[String]) -> Value {mod tests {use super::*;+ #[test]+ fn finds_the_github_cli_on_the_path() {+ let tmp = std::env::temp_dir().join(format!("muxen-gh-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&tmp);+ std::fs::create_dir_all(tmp.join("bin")).unwrap();+ // On Windows the program is gh.exe; a plain "gh" there isn't it.+ let name = if cfg!(windows) { "gh.exe" } else { "gh" };+ std::fs::write(tmp.join("bin").join(name), b"").unwrap();+ let dirs = [tmp.join("empty").display().to_string(), tmp.join("bin").display().to_string()];+ assert_eq!(find_gh(&dirs), Some(tmp.join("bin").join(name)));+ let _ = std::fs::remove_dir_all(&tmp);+ }+#[test]fn reads_shortstat() {assert_eq!(shortstat(" 3 files changed, 120 insertions(+), 40 deletions(-)\n"), (3, 120, 40));diff --git a/src/fleet/code.rs b/src/fleet/code.rsindex bbc484a..d64bcc5 100644--- a/src/fleet/code.rs+++ b/src/fleet/code.rs@@ -177,12 +177,15 @@ fn parse_git_setup(out: &str) -> GitSetup {if !applies || helper.is_empty() {continue;}- let short = if helper.contains("gh auth git-credential") {+ // gh writes `!<its path> auth git-credential`, its path in '…' when+ // it has a space or a backslash (always, on Windows).+ let words = helper_words(helper);+ let short = if matches!(words.as_slice(), [p, a, c, ..] if program_name(p).eq_ignore_ascii_case("gh") && a == "auth" && c == "git-credential") {s.uses_gh = true;"gh".to_string()} else {- let first = helper.split_whitespace().next().unwrap_or(helper);- first.rsplit('/').next().unwrap_or(first).trim_start_matches('!').trim_start_matches("git-credential-").to_string()+ let first = words.first().map(String::as_str).unwrap_or(helper);+ program_name(first).trim_start_matches("git-credential-").to_string()};if !s.helpers.contains(&short) {s.helpers.push(short);@@ -192,6 +195,36 @@ fn parse_git_setup(out: &str) -> GitSetup {s}+/// A credential helper's words as git's shell splits them: a leading `!`+/// dropped, and what's in '…' or "…" kept as one.+fn helper_words(helper: &str) -> Vec<String> {+ let (mut words, mut word, mut quote) = (Vec::new(), None::<String>, None::<char>);+ for c in helper.trim().trim_start_matches('!').chars() {+ match quote {+ Some(q) if c == q => quote = None,+ Some(_) => word.get_or_insert_with(String::new).push(c),+ None if c == '\'' || c == '"' => {+ quote = Some(c);+ word.get_or_insert_with(String::new);+ }+ None if c.is_whitespace() => words.extend(word.take()),+ None => word.get_or_insert_with(String::new).push(c),+ }+ }+ words.extend(word);+ words+}++/// A program's name without its folder (either slash: a Windows machine's+/// paths are read here too) or `.exe`.+fn program_name(path: &str) -> &str {+ let base = path.rsplit(['/', '\\']).next().unwrap_or(path);+ match base.len().checked_sub(4) {+ Some(i) if base.is_char_boundary(i) && base[i..].eq_ignore_ascii_case(".exe") => &base[..i],+ _ => base,+ }+}+#[cfg(test)]mod tests {use super::*;@@ -208,6 +241,21 @@ mod tests {assert_eq!(s.ssh_keys, vec!["id_ed25519"]);assert_eq!(parse_git_setup("git=\n").git, None);}++ #[test]+ fn knows_gh_on_windows_and_quoted_paths() {+ let out = "cred=credential.helper manager\ncred=credential.https://github.com.helper \ncred=credential.https://github.com.helper !'C:\\Program Files\\GitHub CLI\\gh.exe' auth git-credential\n";+ let s = parse_git_setup(out);+ assert!(s.uses_gh);+ assert_eq!(s.helpers, vec!["manager", "gh"]);+ for helper in ["!\"C:\\Program Files\\GitHub CLI\\GH.EXE\" auth git-credential", "!'/home/me/my apps/gh' auth git-credential", "!gh auth git-credential"] {+ assert!(parse_git_setup(&format!("cred=credential.helper {helper}\n")).uses_gh, "{helper}");+ }+ // Other helpers keep their own names, from a quoted path too.+ let s = parse_git_setup("cred=credential.helper !'C:\\Program Files\\Git\\mingw64\\bin\\git-credential-manager.exe'\ncred=credential.helper !gh-other auth git-credential\n");+ assert!(!s.uses_gh);+ assert_eq!(s.helpers, vec!["manager", "gh-other"]);+ }}fn checkout(machine_id: &str, r: &Value) -> Checkout {diff --git a/src/ui/sessions.rs b/src/ui/sessions.rsindex 0f95ca3..0b23d76 100644--- a/src/ui/sessions.rs+++ b/src/ui/sessions.rs@@ -1421,12 +1421,21 @@ impl Root {/// Runs a GitHub CLI command (sign-in, granting a permission) in a terminal/// on a machine, where its browser steps can be followed.pub(super) fn gh_cli_run(&mut self, machine_id: &str, args: &str, title: &str, window: &mut Window, cx: &mut Context<Self>) {+ // Windows runs it in PowerShell, which has no printf or read; and gh+ // by the path the agent found it at, in case the shell's PATH is older.+ let windows = self.snapshot.machines.iter().any(|m| m.id == machine_id && m.platform.starts_with("windows"));+ let command = if windows {+ let gh = self.engine.sessions_of(machine_id).and_then(|s| s.gh).and_then(|g| g.path).map(|p| crate::ops::remote::ps_quote(&p)).unwrap_or_else(|| "gh".into());+ format!("& {gh} {args}; Write-Host ''; [void](Read-Host 'Press Enter to close')")+ } else {+ format!("gh {args}; printf '\\nPress Enter to close.'; read _")+ };let req = StartSession {machine_id: machine_id.to_string(),agent: "shell".into(),cwd: "~".into(),title: Some(title.into()),- command: Some(format!("gh {args}; printf '\\nPress Enter to close.'; read _")),+ command: Some(command),..Default::default()};self.notify_user("Follow the steps in the terminal: copy the code, then open github.com/login/device.", false, cx);