flowey_lib_hvlite/
install_git_credential_manager.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Globally install the git credential manager
5
6use flowey::node::prelude::*;
7use flowey_lib_common::_util::wslpath;
8
9flowey_request! {
10    pub enum Request {
11        /// Automatically configure the user's global config manager
12        AutoConfigure,
13        /// WSL2 will use linux git credential manager if true, windows version if false.
14        UseNativeLinuxOnWsl2,
15        /// Ensure that git was configured
16        EnsureConfigured(WriteVar<SideEffect>),
17    }
18}
19
20new_flow_node!(struct Node);
21
22impl FlowNode for Node {
23    type Request = Request;
24
25    fn imports(dep: &mut ImportCtx<'_>) {
26        dep.import::<flowey_lib_common::install_git::Node>();
27        dep.import::<flowey_lib_common::check_needs_relaunch::Node>();
28    }
29
30    fn emit(requests: Vec<Self::Request>, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
31        if !matches!(ctx.backend(), FlowBackend::Local) {
32            anyhow::bail!("only supported on the local backend at this time");
33        }
34
35        let mut use_native_linux_on_wsl2 = None;
36        let mut auto_configure = None;
37        let mut ensure_configured = Vec::new();
38
39        for req in requests {
40            match req {
41                Request::AutoConfigure => {
42                    same_across_all_reqs("AutoConfigure", &mut auto_configure, true)?
43                }
44                Request::UseNativeLinuxOnWsl2 => same_across_all_reqs(
45                    "UseNativeLinuxOnWsl2",
46                    &mut use_native_linux_on_wsl2,
47                    true,
48                )?,
49                Request::EnsureConfigured(v) => ensure_configured.push(v),
50            }
51        }
52
53        let use_native_linux_on_wsl2 = use_native_linux_on_wsl2.unwrap_or(false);
54        let auto_configure = auto_configure.unwrap_or(false);
55
56        // -- end of req processing -- //
57
58        let git_ensure_installed =
59            ctx.reqv(flowey_lib_common::install_git::Request::EnsureInstalled);
60
61        let (read_env, write_env) = ctx.new_var();
62        ctx.req(flowey_lib_common::check_needs_relaunch::Params {
63            check: read_env,
64            done: ensure_configured,
65        });
66
67        ctx.emit_rust_step("configure git credential manager", move |ctx| {
68            git_ensure_installed.clone().claim(ctx);
69            let write_env = write_env.claim(ctx);
70
71            move |rt: &mut RustRuntimeServices<'_>| {
72                let mut env_to_write = None;
73                let existing_credman = flowey::shell_cmd!(rt, "git config --global credential.helper").ignore_status().read()?;
74                log::info!("existing credentials helper: {existing_credman}");
75
76                if !existing_credman.is_empty() {
77                    if existing_credman.contains("git-credential-manager")
78                        || existing_credman.contains("credential-manager-core")
79                        || existing_credman.contains("manager")
80                    {
81                        log::info!("existing credentials helper matches a known-good credential helper.");
82                        rt.write(write_env, &None);
83                        return Ok(())
84                    } else {
85                        log::warn!("existing credentials helper isn't any of the known-good credential helpers.");
86                        log::warn!("assume the user knows what they're doing?");
87                        rt.write(write_env, &None);
88                        return Ok(())
89                    }
90                }
91
92                // give the user an option to maunally install the cred manager
93                if existing_credman.is_empty() && !auto_configure {
94                    log::info!("Could not detect an existing Git Credential helper.");
95                    log::info!("Press <y> to automatically configure an appropriate --global configuration helper, or any other key to abort the run.");
96                    let mut input = String::new();
97                    std::io::stdin().read_line(&mut input)?;
98                    if input.trim() != "y" {
99                        anyhow::bail!("aborting...")
100                    }
101                }
102
103                if flowey_lib_common::_util::running_in_wsl(rt) && !use_native_linux_on_wsl2 {
104                    let windows_user_profile_path_windows = flowey::shell_cmd!(rt, "cmd.exe /c echo %UserProfile%").read().map_err(|_| anyhow::anyhow!("Unable to run cmd.exe, please restart WSL by running `wsl --shutdown` in powershell and try again."))?;
105                    let windows_user_profile_path = wslpath::win_to_linux(windows_user_profile_path_windows);
106                    let gcm_path_opt_1 = windows_user_profile_path.join("AppData/Local/Programs/Git Credential Manager/git-credential-manager.exe");
107                    let gcm_path_opt_2 = wslpath::win_to_linux(r#"C:\Program Files\Git\mingw64\bin\git-credential-manager.exe"#);
108                    let gcm_path_opt_3 = wslpath::win_to_linux(r#"C:\Program Files\Git\mingw64\libexec\git-core\git-credential-manager.exe"#);
109                    let gcm_path_opt_4 = wslpath::win_to_linux(r#"C:\Program Files (x86)\Git Credential Manager\git-credential-manager.exe"#);
110
111                    let gcm_path = if rt.sh.path_exists(&gcm_path_opt_1) {
112                        &gcm_path_opt_1
113                    } else if rt.sh.path_exists(&gcm_path_opt_2) {
114                        &gcm_path_opt_2
115                    } else if rt.sh.path_exists(&gcm_path_opt_3) {
116                        &gcm_path_opt_3
117                    } else if rt.sh.path_exists(&gcm_path_opt_4) {
118                        &gcm_path_opt_4
119                    } else {
120                        anyhow::bail!("Git Credential Manager not found, please install it manually.");
121                    };
122
123                    if gcm_path == &gcm_path_opt_1 || gcm_path == &gcm_path_opt_4 {
124                        let mut wslenv = rt.sh.var("WSLENV")?;
125                        if !wslenv.contains("GIT_EXEC_PATH/wp") {
126                            log::info!("Standalone Git Credential Manager has been detected.");
127                            log::info!("Please run the following from an administrator command prompt to configure it:");
128                            log::info!("SETX WSLENV %WSLENV%:GIT_EXEC_PATH/wp");
129                            log::info!("This command shares `GIT_EXEC_PATH`, an environment variable which determines where Git looks for its sub-programs,");
130                            log::info!(" with WSL processes spawned from Win32 and vice versa. `/wp` are flags specifying how `GIT_EXEC_PATH` gets translated.");
131                            log::info!("Please refer to <https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/>");
132                            log::info!("and <https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables> for more details.");
133
134                            let do_config = if !auto_configure {
135                                wslenv.push_str(":GIT_EXEC_PATH/wp");
136
137                                log::info!("Please press <y> to automatically configure it, or any other key to continue after manual configuration.");
138                                let mut input = String::new();
139                                std::io::stdin().read_line(&mut input)?;
140                                input.trim() == "y"
141                            } else {
142                                true
143                            };
144
145                            if do_config {
146                                flowey::shell_cmd!(rt, "setx.exe WSLENV {wslenv}").run()?;
147                            }
148
149                            env_to_write = Some(flowey_lib_common::check_needs_relaunch::BinOrEnv::Env("WSLENV".to_string(), "GIT_EXEC_PATH/wp".to_string()));
150                        }
151                    }
152
153                    // Have to do this weird string business due to requiring the escaped space character in Program\ Files
154                    let gcm_path_str = gcm_path.to_str().expect("Invalid git credential manager path").to_string().replace(' ', "\\ ");
155                    flowey::shell_cmd!(rt, "git config --global credential.helper {gcm_path_str}").run()?;
156                    flowey::shell_cmd!(rt, "git config --global credential.https://dev.azure.com.useHttpPath true").run()?;
157                } else if matches!(rt.platform(), FlowPlatform::Windows) {
158                    flowey::shell_cmd!(rt, "git config --global credential.helper manager").run()?;
159                } else {
160                    anyhow::bail!("git credential manager configuration only supported for windows/wsl2 at this time")
161                }
162
163                rt.write(write_env, &env_to_write);
164                Ok(())
165            }
166        });
167
168        Ok(())
169    }
170}