gabyx / Githooks

🦎 Githooks: per-repo and shared Git hooks with version control and auto update. [✩Star] if you're using it!
MIT License
102 stars 5 forks source link

bash script in shared hooks is working incorrectly #172

Closed ruslan-y closed 3 months ago

ruslan-y commented 4 months ago

Hi there!

I used git hook in local repo for few project. The bash script located in folder .git/hooks/prepare-commit-msg and works fine.

#!/bin/sh

PREFIX="DEV-"
BRANCH_NAME=$(git symbolic-ref --short HEAD)

# Identify the JIRA ticket number from the branch name
TICKET_ID=$(echo "$BRANCH_NAME" | grep -oE '[A-Z]+-[0-9]+')

# Checks if the message already includes the ticket ID.
if cat < "$1" | grep -q "$PREFIX"; then
  echo "πŸ†— The message already has the ticket ID."
  exit 0
fi

# Prepend the JIRA ticket to the commit message if found
if [ -n "$TICKET_ID" ]; then
    COMMIT_MSG=$(cat "$1")
    echo "πŸ†” The current branch name contains the ticket ID. Adding it to the commit message."
    echo "$TICKET_ID $COMMIT_MSG" > "$1"
fi

if [ -z "$TICKET_ID" ]; then
    echo "😐️ The current branch name does not contain the ticket ID."
    exit 1
fi

After I switched to shared githooks, I can no longer use it as before. I am getting some errors related to bash arguments that are not being passed properly.

/tmp/test-hooks (DEV-111*) Β» git commit -m 'test'

🦎 Launching '[1]' global shared hooks [type: 'pre-commit', threads: '8']...
/Users/user/.githooks/shared/5ec0d3fe1b8fde8f8b7e34017c39a2a64aab6456-git-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg: line 10: : No such file or directory
cat: : No such file or directory
πŸ†” The current branch name contains the ticket ID. Adding it to the commit message.
/Users/user/.githooks/shared/5ec0d3fe1b8fde8f8b7e34017c39a2a64aab6456-git-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg: line 19: : No such file or directory

[DEV-111 d99a607] test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tt5

Maybe you've come across something similar? Any answers are appreciated.

gabyx commented 4 months ago

Hm... weird case. I mean you are using #!/bin/sh which is POSIX shell but should also work because what you do in your hook is ( afaik) compliant with sh. Can you check if the file is made chmod +x and maybe change to #! /usr/bin/env bash which is far more robust. Somehow its not finding cat which is weird. Maybe you can print the env variables with env and check what it contains? Please report that here.

Another note: You can make this hook containerized if you like (check the examples) such that it runs with docker or podman, I am however a bit unsure how the $1 will work, because the path in the container might be different. I think the containerized version of prepare-commit-msg might have some troubles I need to fix or provide a decent workaround =).

gabyx commented 4 months ago

Another note: Always use set -u and set -e (every non-zero exit is an error and a direct exit) in shell scripts.

ruslan-y commented 4 months ago

Hi! I've completed everything that you described above but it didn't help.

file execution right is correct -rwxr-xr-x 1 user staff 728B Jun 5 11:31 prepare-commit-msg

Also i've tried used #! /usr/bin/env bash into the script. The same problem.

I'v added env command into the script before main part

SHELL=/bin/zsh
LSCOLORS=Gxfxcxdxbxegedabagacad
ITERM_PROFILE=Default
COLORTERM=truecolor
LESS=-R
XPC_FLAGS=0x0
TERM_PROGRAM_VERSION=3.5.0
TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF
__CFBundleIdentifier=com.googlecode.iterm2
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.4Fka7gWzlc/Listeners
GITHOOKS_ARCH=arm64
GIT_INDEX_FILE=.git/index
PWD=/Users/user/Projects/1/test-hooks123
LOGNAME=user
GIT_AUTHOR_DATE=@1717570624 +0500
COMMAND_MODE=unix2003
GIT_EXEC_PATH=/opt/homebrew/opt/git/libexec/git-core
HOME=/Users/user
LANG=ru_RU.UTF-8
TMPDIR=/var/folders/qx/zlx7pbzj5nsfv7kv7lq1s99c0000gn/T/
LC_TERMINAL=iTerm2
GIT_AUTHOR_EMAIL=email
GIT_PREFIX=
STAGED_FILES=0958

TERM=xterm-256color
ZSH=/Users/user/.oh-my-zsh
USER=user
OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
COLORFGBG=15;0
LC_TERMINAL_VERSION=3.5.0
SHLVL=2
GITHOOKS_OS=darwin
GIT_EDITOR=:
PAGER=less
XPC_SERVICE_NAME=0
TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo
LC_ALL=en_US.UTF-8
GIT_AUTHOR_NAME=User User
PATH=/opt/homebrew/opt/git/libexec/git-core:/opt/homebrew/opt/git/libexec/git-core:/opt/homebrew/bin:/opt/homebrew/opt/node@18/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Applications/iTerm.app/Contents/Resources/utilities:/opt/homebrew/bin/
__CF_USER_TEXT_ENCODING=0x1F5:0x7:0x31
TERM_PROGRAM=iTerm.app
_=/usr/bin/env
gabyx commented 3 months ago

I will investigate this: It seems that indeed something is fishy here. I write a short test case quicky.

BTW: Never ever share the env without deleting some strange TOKEN env variables nobody should see, I update your message above.

gabyx commented 3 months ago

In the meantime: Can you add echo "Args:" "$@" at the first line in your script. To inspect the arguments passed to the script.

gabyx commented 3 months ago

@ruslan-y : I cannot reproduce it in a test case:

See https://github.com/gabyx/githooks/compare/bugfix%2Fissue-172?expand=1

ruslan-y commented 3 months ago

@gabyx Hello! I've added echo "Args:" "$@" at the first line in my script and i'm getting nothing Args: Also I tried using your script above and see the following error

🦎 Launching '[1]' global shared hooks [type: 'pre-commit', threads: '8']...
β›‘  Hook '/Users/user/.githooks/shared/5ec0d3fe1b8fde8f8b7e34017c39a2a64aab6456-git-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg' failed!
   -> errors:
   βœ—  Command failed: '/Users/user/.githooks/shared/5ec0d3fe1b8fde8f8b7e34017c39a2a64aab6456-git-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg []'.
   βœ—  exit status 1
β›” Some hooks failed, check output for details:
   β€’ 'ns:5ec0d3fe1b/pre-commit/prepare-commit-msg'
β›” Fatal error -> Abort.
gabyx commented 3 months ago

what was the git command you executed. I assume you do not run this share hook containerized? We need probably a debug build with debug log:

gabyx commented 3 months ago

I assume you are on macOS:

git hooks --version

Should be 3.0.2:

Would help if you would checkout this repo on main and build it (just needs go, there is a nix/flake.nix if you use Nix...)

./githooks/scripts/build.sh

and then change the runner temporarily in your repository where you have the hooks:

git config --local githooks.runner "<githooks-checkout>/githooks/bin/githooks-runner"

and then run inside the repo:

git commit --allow-empty -m "Test"

There should be a ton of output now.

ruslan-y commented 3 months ago

I use git command git commit -m "my commit"

Yes, I'm on macOS, but I tried it on Ubuntu. The same issue

I ran build.sh script and changed the runner path in local repository, then I make a commit using your test script

πŸ›   Githooks Runner [version: 3.0.2+4.a89d530]
πŸ›   Install dir set to: '/root/.githooks'.
πŸ›   Settings:
    β€’ Args: '[]'
    β€’ Repo Path: '/root/tt'
    β€’ Repo Hooks: '/root/tt/.githooks'
    β€’ Git Dir Worktree: '/root/tt/.git'
    β€’ Install Dir: '/root/.githooks'
    β€’ Hook Path: '/root/.githooks/templates/hooks/pre-commit'
    β€’ Hook Name: 'pre-commit'
    β€’ Trusted: 'false'
    β€’ Containerized: 'false'
πŸ›   Repository already registered.
πŸ›   Checksum store contains '0' checksums
   and directory search path '/root/tt/.git/.githooks.checksums'.
πŸ›   User ignore patterns: '{[] []}'.
πŸ›   Accumuldated repository ignore patterns: '{[] []}'.
πŸ›   Exporting staged files:
   - 1234
   - tt
πŸ›   Old hook 'pre-commit.replaced.githook' does not exist. -> Skip!
πŸ›   Namespace envs: map[]
πŸ›   Getting hooks in '/root/tt/.githooks'
πŸ›   Shared hooks not updated.
πŸ›   Getting hooks in '/root/.githooks/shared/c172ade2e6c1d22e00277c22fdeb49910ddea6ea-https-gitlab-com-project-githooks-git/githooks'
πŸ›   Local Hooks: none
πŸ›   Repo Shared Hooks: none
πŸ›   Local Shared Hooks: none
πŸ›   Global Shared Hooks :
    Batch: 0
     - '/root/.githooks/shared/c172ade2e6c1d22e00277c22fdeb49910ddea6ea-https-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg' []

πŸ›   Hooks priority list written to '/tmp/2812734871-githooks-prio-list-pre-commit.json'.
🦎 Launching '[1]' global shared hooks [type: 'pre-commit', threads: '24']...
/root/.githooks/shared/c172ade2e6c1d22e00277c22fdeb49910ddea6ea-https-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg: line 11: /root/.githooks/shared/c172ade2e6c1d22e00277c22fdeb49910ddea6ea-https-gitlab-com-project-githooks-git/githooks/general.sh: No such file or directory
β›‘  Hook '/root/.githooks/shared/c172ade2e6c1d22e00277c22fdeb49910ddea6ea-https-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg' failed!
   -> errors:
   βœ—  Command failed: '/root/.githooks/shared/c172ade2e6c1d22e00277c22fdeb49910ddea6ea-https-gitlab-com-project-githooks-git/githooks/pre-commit/prepare-commit-msg []'.
   βœ—  exit status 1
β›” Some hooks failed, check output for details:
   β€’ 'ns:c172ade2e6/pre-commit/prepare-commit-msg'
β›” Fatal error -> Abort.
gabyx commented 3 months ago

Ah nice, thanks that helps. I see now that, somehow there is no Args: [] at the beginning. These are the arguments witch which the runner was called. LOL: the error you have is that you run prepare-commit-msg under a hook pre-commit so this is not right. You need to make a folder prepare-commit-msg/check-it.sh then it works (or only the file prepare-commit-msg.sh. Sorry that was a long round trip. Sometimes its better to share complete MWE to quickly help.

Reopen if it still does not work!