Search code examples
fish

Fish shell: Alias per directory basis


Currently my team uses Git a.w.a Mercurial for various projects, So I am here to define my own vcs commands to make common things seamless between different projects/directories (git & hg).

How do I achieve this in fish shell? So that alias can trigger corresponding git or hg commands per directory/subdirectory basis.


Solution

  • You can determine the current git branch by running:

    set git_branch (git symbolic-ref HEAD ^/dev/null | sed -e 's|^refs/heads/||')
    

    and the current hg branch with:

    set hg_branch (hg branch)
    

    Then simply check which one is truthy, and proceed from there:

    if test "$git_branch"
        git $argv
    else if test "$hg_branch"
        hg $argv
    else
        echo "[X] Not in a VCS directory"
        return 1
    end
    

    If you actually want to write a whole suite of VCS commands that are git/hg agnostic that requires a big more work to translate all the equivalent commands and map the arguments, etc.

    This is generally not a good idea though, you're obscuring the actual behavior of the underlying system. It's better just to show the developer which type of repo you're in, or use a real compatibility layer like hg-git.