Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Git Commit Hook Alternative #11

Open
AlJohri opened this issue Feb 14, 2022 · 1 comment
Open

Git Commit Hook Alternative #11

AlJohri opened this issue Feb 14, 2022 · 1 comment

Comments

@AlJohri
Copy link

AlJohri commented Feb 14, 2022

I just wanted to post a quick alternative to git-auto using a git post-commit hook and some gotchas I ran into while getting this set up. I like this approach because it ensures every commit is immediately pushed.

Creating post-commit hook

  1. Ensure your logseq database is a git repository. This usually entails running git init in the root of the logseq directory.
  2. Check "Enable Git auto commit" in the Logseq version control settings.
  3. Create the file .git/hooks/post-commit relative to the root of the logseq repository with the following contents:
    #!/bin/bash
    git push

Potential Gotchas

  1. On macOS, I usually use the osxkeychain for authentication using a "personal access token". This, for some reason didn't work out well and kept giving me the following error: fatal: could not read Username for 'https://github.com': Device not configured. I ensured that the script was being run as personal account, not root, but this didn't fix the issue. The easiest solution was to switch to using the git-credential-manager (GCM).

    # check if current credential.helper is osxkeychain
    ❯ git config credential.helper
    osxkeychain
    
    # clear osxkeychain password
    ❯ git credential-osxkeychain erase
    host=github.com
    protocol=https
    > [Press Return]
    
    # install latest git and git credential manager (GCM) from homebrew
    ❯ brew install git
    ❯ brew tap microsoft/git
    ❯ brew install --cask git-credential-manager-core
    
    # verify cask installation has automatically updated the credential.helper
    ❯ git config credential.helper
    /usr/local/share/gcm-core/git-credential-manager-core
    
    # attempt to git push, will bring up a pop-up establishing authentication
    git push
  2. For some reason, I was unable to change the hashbang for the post-commit hook. It seems to always use the system bash installed at /bin/bash (for me, version 3.2.57) even if I explicitly changed the hashbang to homebrew installed bash via #!/opt/homebrew/bin/bash. This means you can't use newer bash features like mycmd &>> /tmp/mylog etc.

    This longer script helped me debug all of these issues:

    #!/bin/bash
    mkdir -p /tmp/logseq/
    timestamp=$(date +"%s")
    filepath="/tmp/logseq/$timestamp"
    touch $filepath
    echo "attepmting push" >> $filepath
    echo "current directory: $(pwd)" >> $filepath
    echo "bash version: $(bash --version)" >> $filepath
    echo "whoami: $(whoami)" >> $filepath
    git remote -v >> $filepath 2>&1
    git push >> $filepath 2>&1

Hope this helps!

@tibtiq
Copy link

tibtiq commented Mar 26, 2022

Nice alternative. I was having issues getting git-auto to work on Windows.
For those on windows the contents of .git/hooks/post-commit should be

#!/bin/sh
git push

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants