Skip to content

Commit 678eb35

Browse files
committed
add zsh_home_dir
which return ZDOTDIR if set else HOME
1 parent 322ec1e commit 678eb35

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

libmamba/include/mamba/util/environment.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ namespace mamba::util
6262
*/
6363
[[nodiscard]] auto user_home_dir() -> std::string;
6464

65+
/**
66+
* Return the directory to zsh dotfiles.
67+
*
68+
* This function checks for the `ZDOTDIR` environment variable and falls back to the user's
69+
* home directory if it is not set.
70+
*/
71+
[[nodiscard]] auto zsh_home_dir() -> std::string;
72+
6573
/**
6674
* Return the current user config directory.
6775
*

libmamba/src/core/shell_init.cpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ namespace mamba
12591259
{
12601260
init_root_prefix(context, shell, conda_prefix);
12611261
auto mamba_exe = get_self_exe_path();
1262-
fs::u8path home = util::user_home_dir();
1262+
fs::u8path home = (shell == "zsh") ? util::zsh_home_dir() : util::user_home_dir();
12631263
if (shell == "bash")
12641264
{
12651265
// On Linux, when opening the terminal, .bashrc is sourced (because it is an interactive
@@ -1275,15 +1275,7 @@ namespace mamba
12751275
}
12761276
else if (shell == "zsh")
12771277
{
1278-
fs::u8path zshrc_path
1279-
// use ZDOTDIR if set and fallback to HOME
1280-
const char* zdotdir = std::getenv("ZDOTDIR");
1281-
if (zdotdir != nullptr && zdotdir[0] != '\0')
1282-
{
1283-
zshrc_path = fs::u8path(zdotdir) / ".zshrc";
1284-
} else {
1285-
zshrc_path = home / ".zshrc";
1286-
}
1278+
fs::u8path zshrc_path = home / ".zshrc";
12871279
modify_rc_file(context, zshrc_path, conda_prefix, shell, mamba_exe);
12881280
}
12891281
else if (shell == "csh")
@@ -1348,7 +1340,7 @@ namespace mamba
13481340
void deinit_shell(Context& context, const std::string& shell, const fs::u8path& conda_prefix)
13491341
{
13501342
auto mamba_exe = get_self_exe_path();
1351-
fs::u8path home = util::user_home_dir();
1343+
fs::u8path home = (shell == "zsh") ? util::zsh_home_dir() : util::user_home_dir();
13521344
if (shell == "bash")
13531345
{
13541346
fs::u8path bashrc_path = (util::on_mac || util::on_win) ? home / ".bash_profile"
@@ -1357,15 +1349,7 @@ namespace mamba
13571349
}
13581350
else if (shell == "zsh")
13591351
{
1360-
fs::u8path zshrc_path
1361-
// use ZDOTDIR if set and fallback to HOME
1362-
const char* zdotdir = std::getenv("ZDOTDIR");
1363-
if (zdotdir != nullptr && zdotdir[0] != '\0')
1364-
{
1365-
zshrc_path = fs::u8path(zdotdir) / ".zshrc";
1366-
} else {
1367-
zshrc_path = home / ".zshrc";
1368-
}
1352+
fs::u8path zshrc_path = home / ".zshrc";
13691353
reset_rc_file(context, zshrc_path, shell, mamba_exe);
13701354
}
13711355
else if (shell == "xonsh")
@@ -1423,22 +1407,15 @@ namespace mamba
14231407

14241408
fs::u8path config_path_for_shell(const std::string& shell)
14251409
{
1426-
fs::u8path home = util::user_home_dir();
1410+
fs::u8path home = (shell == "zsh") ? util::zsh_home_dir() : util::user_home_dir();
14271411
fs::u8path config_path;
14281412
if (shell == "bash")
14291413
{
14301414
config_path = (util::on_mac || util::on_win) ? home / ".bash_profile" : home / ".bashrc";
14311415
}
14321416
else if (shell == "zsh")
14331417
{
1434-
// use ZDOTDIR if set and fallback to HOME
1435-
const char* zdotdir = std::getenv("ZDOTDIR");
1436-
if (zdotdir != nullptr && zdotdir[0] != '\0')
1437-
{
1438-
config_path = fs::u8path(zdotdir) / ".zshrc";
1439-
} else {
1440-
config_path = home / ".zshrc";
1441-
}
1418+
config_path = home / ".zshrc";
14421419
}
14431420
else if (shell == "xonsh")
14441421
{

libmamba/src/util/environment.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ namespace mamba::util
290290
throw std::runtime_error("HOME not set.");
291291
}
292292

293+
auto zsh_home_dir() -> std::string
294+
{
295+
if (auto maybe_zdotdir = get_env("ZDOTDIR").value_or(""); !maybe_zdotdir.empty())
296+
{
297+
return maybe_zdotdir;
298+
}
299+
return user_home_dir();
300+
}
301+
293302
auto user_config_dir() -> std::string
294303
{
295304
if (auto maybe_dir = get_env("XDG_CONFIG_HOME").value_or(""); !maybe_dir.empty())

0 commit comments

Comments
 (0)