2

I'm struggling to write a pre-receive hook which should verify push basing on existence of marker file. But newref populated by script is pointing into nowhere.
I can`t get any data to validate getting:

remote: fatal: bad object 3bb1ab6de84c324cd2fb0a7961bd5afccfd77d55

I`m trying to acquire data by:

git show $newrev

I tried also:

list_commits=git rev-list $oldrev..$newrev

but only got:

remote: fatal: Invalid revision range 341cc54a0afa8824e6f61514d6709b5f71f2c444..cc6c92ef2af267bc228219216078e0b1b3b7b2bb

When I ran git log there was not any of pushed commits.
How do I get access to them?

I have repository on Atlassian Bitbucket v5.1.0 maybe it is the case? If yes, do you know any workarounds?

Whole pre-receive script for now:

#!/bin/bash
while read oldrev newrev refname; 
do
    git show $newrev
done

EDIT:

Thanks to info I`ve found here https://git.seveas.net/manpages/gitrepository-layout.html to make object file discoverable you have to edit objects/info/alternates file by adding folder name, which home by default is object folder.

F.e. to add incoming-b2241(pushed changes) which is in .git/objects you need to add only incoming-b2241 into alternates

After this incoming objects are accessible for git cat-file and so.

Don`t forget to re-edit it afterwards, as non-existing directories will corrupt repository, but luckily errors are pretty descriptive and then you know what need to be fixed.

  • [related: Access the changed files path in git pre-receive hook](https://stackoverflow.com/questions/28414640/access-the-changed-files-path-in-git-pre-receive-hook) – Nahuel Fouilleul Jan 03 '18 at 16:23
  • these solutions are not working for me : / Got "bad object" and "reference is not a tree" – Szymon Masłowski Jan 03 '18 at 16:34
  • That looks like it should work. I wonder if they're using alternate objects (modern Git does this temporarily during receive so that they won't persist if they're rejected) and somehow stripping environment variables, in your setup. – torek Jan 03 '18 at 16:36
  • maybe the following is more accurate [Can git pre-receive hooks evaulate the incoming commit?](https://stackoverflow.com/questions/22546393/can-git-pre-receive-hooks-evaulate-the-incoming-commit) but it's about pre-commit not pre-receive – Nahuel Fouilleul Jan 03 '18 at 16:38
  • I have git version 2.13.0.windows.1, so I think it can be considered as modern? When it stared to work this way? Code taken from thread about pre-commit produces same error - bad object 654684... – Szymon Masłowski Jan 03 '18 at 16:51
  • The "quarantine" code first went in to [Git 2.11.0](https://github.com/git/git/commit/722ff7f876c8a2ad99c42434f58af098e61b96e8). Normally you won't see any weirdness with it, but be sure whatever you are doing is not stripping out environment variables `GIT_OBJECT_DIRECTORY` and/or `GIT_ALTERNATE_OBJECT_DIRECTORIES`. – torek Jan 03 '18 at 18:42
  • If you are intrested I`ve solved and edited post : ) – Szymon Masłowski Jan 11 '18 at 15:24

1 Answers1

2

Are you using the external hooks plugin? If so, it sounds like you're running into this issue.

The short answer is: you need to update the plugin.

The long answer is: On newer versions of Git, it needs to be told where to find the revisions that are involved in the push but aren't yet added to the repo. You're accomplishing that by adding it to objects/info/alternates, but it can also be done by setting an environment variable (safer, as it doesn't make any changes to the repo itself).

The External Hooks plugin passes those environment variables through in newer versions, so updating the plugin should make Git hooks work as they do normally, without editing the repo.

ZoFreX
  • 8,812
  • 5
  • 31
  • 51