CharlesChiuGit / Logseq-Git-Sync-101

This repo aims to help Logseq users to sync their data with Git and GitHub.
MIT License
996 stars 82 forks source link

pre-commit hook fails on Linux #4

Closed daniel-church closed 2 years ago

daniel-church commented 2 years ago

Running on Ubuntu 20.04 I get the below error message every time logseq attempts to commit. The script runs fine with bash, but errors out every time it is included in the git process.

10:01:01.880 › [Git] .git/hooks/pre-commit: 11: [[: not found
Already up to date.
CharlesChiuGit commented 2 years ago

Did you use the exact git-hooks files I provided? line 11 shouldn't execute anything at all.

daniel-church commented 2 years ago

I was using the exact sheet, but took those two commented lines out that were at line 11 to see if it was causing the issue. Now I have the below script and get the below message, which lines up with the if statement that seems to be causing the issue.

#!/bin/sh
#
# Pull before committing
# Credential handling options:
#  - hardcode credentials in URL 
#  - use ssh with key auth 
#  - https://git-scm.com/docs/git-credential-store
#  - git credential helper on windows

output=$(git pull --no-rebase)

# Handle non error output as otherwise it gets shown with any exit code by logseq
if [[ "$output" == "Already up to date." ]]; then
   # no ouput
   exit 0
else
   # probably error print it to screen   
   echo $output
fi

git add -A
10:36:03.482 › [Git] .git/hooks/pre-commit: 13: [[: not found
Already up to date.
CharlesChiuGit commented 2 years ago

Maybe the space character or the newline character are having issues on windows and linux. I don't think it's a big problem. But if you very want to get rid of it, maybe you can re-type the pre-commits. Remember to use the .sample files in the .git/hooks as template because the file type is very unique.

daniel-church commented 2 years ago

Once I made the slight modifications below it began working. May be useful to note for any additional Linux users. I didn't dive too much farther into it, but I am assuming the sh interpreter does not include the double brackets as valid if syntax or something along those lines.

Thanks again for putting this all together, very excited to get this all integrated!

#!/bin/sh

output=$(git pull --no-rebase)

# Handle non error output as otherwise it gets shown with any exit code by logseq
if [ "$output" = "Already up to date." ]
then
   # no ouput
   exit 0
else
   # probably error print it to screen
   echo "else"
   # echo $output
fi

git add -A
CharlesChiuGit commented 2 years ago

Interesting! If it's the interpreter that goes wrong, maybe you can try replace #!/bin/bash with #!/bin/sh

romilly commented 2 years ago

It's not the interpreter. It's a bug in your pre-commit script, which has [[ ]] instead of [ ].

I'll do a pr later.

CharlesChiuGit commented 2 years ago

@romilly thanks! So my pre-commit script will act differently on different platform? Is there a more robust way to do it?

romilly commented 2 years ago

I'm no expert, and I have no access to Windows or OS/X - Linux only I'm afraid.

Also I am now getting a (slightly different) error. I'll do a PR when it's working :)

CharlesChiuGit commented 2 years ago

Cool!

romilly commented 2 years ago

Doh! The code is fine, it's the difference between sh and bash on recent Linuxes.

With #!/bin/bash as the shebang, all works.

romilly commented 2 years ago

And thanks for an excellent guide!

CharlesChiuGit commented 2 years ago

Cool! So just the shebang? [[ ]] is actuacally fine? If that's the only problem I'll fix it right now.