Fix msysGit Portable $HOME location

后端 未结 2 835
轮回少年
轮回少年 2020-11-27 13:30

I have successfully installed and configured msysGit Portable on my flash drive, and have used it to pull and push GitHub repos. However, I seem to always have to kludge the

2条回答
  •  我在风中等你
    2020-11-27 14:12

    Setting the home dir

    The solution with a git-bash-portable.bat wrapper opens another Windows CMD window for me that stays in the background.

    Another, more native solution is to adjust /etc/profile and set the HOME var there. Just add the following lines to the end of /etc/profile, myuser beeing your virtual username:

    # end of /etc/profile
    export HOME="/home/myuser"
    cd
    

    This sets the proper HOME directory and cds into it. Then the startup mechanism, like loading all files from /etc/profile.d works correctly and you just start git-bash.exe with a doubleclick.

    Of course you have to create your home directory for this to work. Start git-bash and create it:

    mkdir -p /home/myuser
    

    Starting or reconnecting to the agent

    Regarding the agent, it usually has to be reloaded with every git-bash shell opened. A solution to get an independent agent spanning all git-bash windows is to include the following little script ~/.mgssh in the startup. It stores the agent env vars in a file agent.env in the .ssh directory. Any new shell reads the file, checks if the agent is still running and connects to it. If it is not running it starts the agent and rewrites the agent.env file. Make sure your .ssh dir exists.

    # cat ~/.mgssh
    
    agentfile=~/.ssh/agent.env
    
    agent_load_env()
    {
      test -f "$agentfile" && . "$agentfile" >| /dev/null;
    }
    
    agent_start()
    {
      (umask 077; ssh-agent >| "$agentfile")
      . "$agentfile" >| /dev/null;
    }
    
    agent_load_env
    
    # agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
    agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
    
    if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
        agent_start
    fi
    
    # uncomment this, if you want to add a key on agent startup
    #if [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    #    ssh-add
    #fi
    
    unset agentfile
    

    Now source the .mgssh script in your .bashrc:

    # cat .bashrc
    . ~/.mgssh
    
    # ... more .bashrc content
    

    Found this on GitHub:

    https://help.github.com/articles/working-with-ssh-key-passphrases/#platform-windows

    Killing the agent before stick removal

    Usually, before you remove your usbstick you ask Windows to eject the stick, by right clicking it in the explorer or using the little systray icon. This will not work, if your agent is still up and running. Make sure to kill the agent before closing the last shell upon stick removal:

    $ ssh-agent -k
    
    unset SSH_AUTH_SOCK;
    unset SSH_AGENT_PID;
    echo Agent pid 8472 killed;
    

    Remark: Usually you would use eval $(ssh-agent -k) to unset the env vars as well, but as you do this just before closing the shell it's irrelevant. The above startup script .mgssh takes care of cleaning up the ~/.ssh/agent.env file so that does not have to be done either.

提交回复
热议问题