Useful snippets

    git hooks

    2026-02-25

    To activate pre-push, rename .git/hooks/pre-push.sample to .git/hooks/pre-push


    Example script:

    branch_name=$(git symbolic-ref HEAD 2>/dev/null)
    

    allowed_branch_name="refs/heads/"

    allowed_branch_name+="min-branch"

    if [ "$branch_name" != "$allowed_branch_name" ]; then echo "Current branch: ${branch_name}" echo "Allowed branch: ${allowed_branch_name}" echo echo "You can't push directly to this branch." # also prevents --force-with-lease pushes exit 1 fi

    should work given that this command:

    git symbolic-ref HEAD 2>/dev/null

    gives: refs/heads/min-branch

    and:

    git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3

    gives min-branch

    Check if a file exists (pre-push example)

    2026-03-03

    Bash

    Check existence

    lpo() {
      if [[ -f ".git/hooks/pre-push" ]]; then
        echo ".git/hooks/pre-push was found:"
        echo "pre-push is activated"
      else
        if [[ -f ".git/hooks/pre-push.sample" ]]; then
          echo ".git/hooks/pre-push was not found:"
          echo "but .git/hooks/pre-push.sample was found:"
          echo "pre-push is deactivated"
        else
          echo "Hmmmmm"
        fi
      fi
    }

    Nvim edit file

    po() {
      if [[ -f ".git/hooks/pre-push" ]]; then
        echo ".git/hooks/pre-push was found:"
        nvim .git/hooks/pre-push
      else
        if [[ -f ".git/hooks/pre-push.sample" ]]; then
          echo ".git/hooks/pre-push was not found:"
          echo "but .git/hooks/pre-push.sample was found:"
          nvim .git/hooks/pre-push.sample
        else
          echo "Hmmmmm"
        fi
      fi
    }

    Lua nvim edit file

    function file_exists(name)
       local f=io.open(name,"r")
       if f~=nil then io.close(f) return true else return false end
    end
    

    function open_prepush() if file_exists(".git/hooks/pre-push") then vim.cmd("vsplit .git/hooks/pre-push") elseif file_exists(".git/hooks/pre-push.sample") then vim.cmd("vsplit .git/hooks/pre-push.sample") else vim.cmd("echo 'Hmmmmm'") end end

    vim.keymap.set("n", "<leader><leader>po", open_prepush, { desc = "Open pre-push a new split" })