8

I am running XAMPP server on my Windows Server 2008 R2. I am running Git 64bit version 2.16.1.windows.1 I would like to use post-receive hook to update my website after i do pa push from my client.

I created a bare project MyProject.git on server and cloned it to MyProject.

My hook is:

#!C:/Program\ Files/Git/usr/bin/sh.exe 
echo "Hook got triggered.." > hooks.txt
exec powershell.exe -NoProfile -ExecutionPolicy Bypass -File ".\hooks\post-receive.ps1"

Powershell script is:

cd "C:/xampp/htdocs/webapp/myProject/"
git pull origin master
exec git-update-server-info

I searched similar questions without any luck for solution of to problem.

If i run the script manually in powershell it runs ok. Based on my log the hook inst even firing.

The Git on Apache looks like it's configured correctly, because i can push to remote without problems and if i do a pull on the server in MyProject folder, the files that i get from repo are correct.

What am i missing?

EDIT:

I fixed my hook to:

#!C:/Program\ Files/Git/usr/bin/sh.exe
echo "Hook got triggered.."
exec powershell.exe -NoProfile -ExecutionPolicy Bypass -File ".\hooks\post-receive.ps1"

And my powershell script to:

cd "C:/xampp/htdocs/webapp/myProject/"
unset GIT_DIR
git pull origin master
exec git-update-server-info

Response when i push to remote is:

Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 296 bytes | 296.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To http://git.mysite.com/myProject.git
  999aeb1..cf0df6f  master -> master

There is no "Hook got triggered.." response.

EDIT2: I chaneged my hook to:

#!C:/Program\ Files/Git/usr/bin/sh.exe
echo "Hook got triggered.."
cd "C:/xampp/htdocs/webapp/drajv-tecaj/"
unset GIT_DIR
git pull origin master

The script runs correctly if executed manually usin bash. But the script is still not running after i do a push on client.

EDIT3 Pasting my config for virtual host, that I'm pushing to:

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/webapp"
    ServerName git.mysite.com
    <Directory "C:/xampp/htdocs/webapp">
        Options +ExecCGI +Indexes +FollowSymLinks
        Require all granted
    </Directory>
    <Directory "C:/Program Files/Git/mingw64/libexec/git-core/">
        Require all granted 
    </Directory>
    SetEnv GIT_PROJECT_ROOT C:/xampp/htdocs/webapp
    SetEnv GIT_HTTP_EXPORT_ALL
    ScriptAlias /git "C:/Program Files/Git/mingw64/libexec/git-core/git-http-backend.exe/"
    ScriptAliasMatch \
        "(?x)^/(.*/(HEAD | \
                    info/refs | \
                    objects/(info/[Apache Git server on Windows^/]+ | \
                             [0-9a-f]{2}/[0-9a-f]{38} | \
                             pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
                    git-(upload|receive)-pack))$" \
                    "C:/Program Files/Git/mingw64/libexec/git-core/git-http-backend.exe/$1"
</VirtualHost>
pixelistik
  • 7,541
  • 3
  • 32
  • 42
Nomeaning25
  • 263
  • 1
  • 11
  • What is the exact name of your post-receive script? it should have *no* file extension. – Ferrybig Feb 02 '18 at 09:00
  • The exact file name copied from hooks folder is `post-receive`. The whole path is `C:/xampp/htdocs/webapp/myProject.git/hooks/post-receive` as described in comments of bottom answer – Nomeaning25 Feb 02 '18 at 09:06
  • The issue is still not resolved – Nomeaning25 Feb 09 '18 at 09:49
  • Can you try changing the first line to `#!/bin/sh` (the shebang line), if you haven't tried that combination yet – Ferrybig Feb 09 '18 at 09:51
  • Tried that, didnt work. Now my shebang line is `#!/bin/sh;C:/Program\ Files/Git/usr/bin/sh.exe;/usr/bin/env sh;/usr/bin/sh` – Nomeaning25 Feb 09 '18 at 13:06
  • Don't end the shebang line with a semicolon, make it like so: https://paste.ubuntu.com/26546432/ – Ferrybig Feb 09 '18 at 13:08
  • It's not, jsut the line got splited there in the comment. I file it's all one line. I Included multiple locations. Basicaly trial and error form this point because i don't know where the problem is. The script works if i execute it manauly, the hook just doesn't fire on it's own whene i do a push. – Nomeaning25 Feb 09 '18 at 13:13

1 Answers1

2

As seen in "git post-receive not working correctly", you would need to unset GIT_DIR for your pull to work correctly from within a hook.

cd "C:/xampp/htdocs/webapp/myProject/"
unset GIT_DIR
git pull origin master
exec git-update-server-info
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Ok, I added that line for the future. But i don't think there is more of a problem that `post-receive` is not firing. Nothing gets added to `hooks.txt` when i do the push from my client machine. I removed the last part of the line so that i schold get the response "Hook got triggered.." in my comand window. I edited my question with response that i get when i do a push – Nomeaning25 Feb 02 '18 at 07:54
  • @Nomeaning25 what is the full path and name of your hook on the server side? did you had an extension? (post-receive.txt) – VonC Feb 02 '18 at 08:00
  • No, no extensions. The full path to my hooks is `C:/xampp/htdocs/webapp/myProject.git/hooks/`. This is my bare project that I use for remote repo. When i push to remote I push to this project. – Nomeaning25 Feb 02 '18 at 08:03
  • 1
    @Nomeaning25 I understand https://tygertec.com/git-hooks-practical-uses-windows/, but just in case, can you try other shebangs, like `#!/usr/bin/env sh` or `#!/usr/bin/sh`? – VonC Feb 02 '18 at 08:07
  • @Nomeaning25 by the way, what git version do you have on your server? 2.16.x I suppose? – VonC Feb 02 '18 at 08:13
  • Tried both of shebangs but same result. I still dont get anythig from hoot to response. Based on `git --version` i am using 2.16.1.windows.1 – Nomeaning25 Feb 02 '18 at 08:23
  • @Nomeaning25 And I presume, in your last two tries, that no files were pulled? – VonC Feb 02 '18 at 08:26
  • @Nomeaning25 Just to be sure, your hook is named `post-receive`? As in `C:/xampp/htdocs/webapp/myProject.git/hooks/post-receive`? It should be executable. – VonC Feb 02 '18 at 08:27
  • Correct, no files were pulled. – Nomeaning25 Feb 02 '18 at 08:27
  • And you have two different folders, one for the bare repo `C:/xampp/htdocs/webapp/myProject.git` and one for the non-bare repo `C:/xampp/htdocs/webapp/myProject`, with a `.git/` subfolder in it. – VonC Feb 02 '18 at 08:29
  • I presume i have to execute it throug GitBash? In command promt it does not work – Nomeaning25 Feb 02 '18 at 08:33
  • The bare repo and non-bare repo are configured as you described – Nomeaning25 Feb 02 '18 at 08:33
  • @Nomeaning25 Yes, bash is needed to run those hook scripts (mainly because of the shebang) – VonC Feb 02 '18 at 08:49
  • Yes, I can execute the script using bash. The script executes normaly and pulls my repo. I will edit my question since i edited my script – Nomeaning25 Feb 02 '18 at 08:52
  • Any other ideas? Is it possible that i have something woring in my apache configuration? – Nomeaning25 Feb 02 '18 at 10:21
  • @Nomeaning25 Yes, I suspect the Apache config for calling git might be incorrect. See for instance https://gist.github.com/karmi/484199#file-git-hosting-apache-conf-L55-L68. – VonC Feb 02 '18 at 10:35