Commit edc7c1d6 authored by jan.koester's avatar jan.koester
Browse files

sh: keep the inherited $PATH, append config.yaml's directories

config.yaml's PATH list replaced the inherited $PATH on every start, so
"PATH=/opt/x:$PATH sh script" lost /opt/x and git's shell commands
(merge-octopus, filter-branch, ...) could not ". git-sh-setup" from the
libexec/git-core directory git prepends.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent def8bd9f
Loading
Loading
Loading
Loading
+30 −2
Changes for src/envoirment.cpp: 30 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -354,10 +354,38 @@ void fresh::Environment::load(const std::string &configpath){
    // PATH too. Goes through setVariable(), not a direct _variables[]
    // write, so this also exports it into the real process environment and
    // (redundantly but harmlessly) re-derives _path from it.
    std::string joined;
    // An inherited $PATH comes first: "PATH=/opt/x:$PATH sh script" and
    // programs that prepend their own directory before running a script
    // (git puts libexec/git-core there for ". git-sh-setup") must keep
    // it; config.yaml's directories are appended where missing. Without
    // an inherited $PATH (init, a bare environment) config.yaml's list
    // is the whole path, as before.
    std::vector<std::string> merged;
    const char *inherited=getenv("PATH");
    if(inherited && *inherited){
        std::string dirs=inherited;
        size_t pos=0;
        for(;;){
            size_t end=dirs.find(':',pos);
            merged.push_back(dirs.substr(pos,end==std::string::npos ? std::string::npos : end-pos));
            if(end==std::string::npos)
                break;
            pos=end+1;
        }
    }
    for(size_t i=0; i<_path.size(); ++i){
        bool have=false;
        for(size_t k=0; k<merged.size(); ++k)
            if(merged[k]==_path[i])
                have=true;
        if(!have)
            merged.push_back(_path[i]);
    }

    std::string joined;
    for(size_t i=0; i<merged.size(); ++i){
        if(i>0) joined+=":";
        joined+=_path[i];
        joined+=merged[i];
    }
    _exported["PATH"]=true;
    setVariable("PATH",joined);