5

Currently, I have a post-receive hook that contains:

git --work-tree=/served/data/location --git-dir=/this/bare/git/repo checkout -f

That worked great, until I wanted to include a submodule, which it just ignores.

After a bit of reading, I thought I could simply add:

git --work-tree=/served/data/location --git-dir=/this/bare/git/repo submodule update --init --recursive

alas:

git-submodule cannot be used without a working tree

Odd, since I've plainly supplied the same --work-tree as for the prior checkout, which worked fine.

I'm using git version 2.7.4 on the server, and pushing with git version 2.11.0.


As far as I can tell, this is the same issue as here, except that talks about something called 'OpenShift' that I've never heard of and am not using, so the answer doesn't really help.

Community
  • 1
  • 1
OJFord
  • 10,522
  • 8
  • 64
  • 98
  • What git version are you using on which OS? – VonC Dec 18 '16 at 21:04
  • Remote: 2.7.4; 'Pusher': 2.11.0 – OJFord Dec 18 '16 at 21:05
  • Could you try the same command after setting GIT_DIR environment variable to the bare repo? – VonC Dec 18 '16 at 21:07
  • What is your setup? Presumably you have a local repo which you use for development. Then do you push to a remote repo? How is that remote repo configured? The error message hints that the remote is a so-called "bare repository". How much access do you have to configuring the remote repo? – Code-Apprentice Dec 18 '16 at 21:07
  • @Code-Apprentice Yes. With `git init --bare` and the above hook. Total. – OJFord Dec 18 '16 at 21:09
  • @VonC Same error. – OJFord Dec 18 '16 at 21:10
  • The error message is clear. You must have a working tree in order to use `git submodule`. This means you cannot use a bare repository. – Code-Apprentice Dec 18 '16 at 21:13
  • @Code-Apprentice `--work-tree=/served/data/location` – OJFord Dec 18 '16 at 21:14
  • hmm...apparently I don't completely understand your question. Can you show the exact sequence of commands you used? The synopsis beginning "I've tried..." seems too incomplete to me. Multiple sets of commands would be good too, to show everything you have tried exactly as you typed it. – Code-Apprentice Dec 18 '16 at 21:17
  • Ah! No error if I `cd served && git --work-tree=... --git-dir=... submodule ...` -but, it doesn't seem to *do* anything... (there's definitely a submodule expected!) – OJFord Dec 18 '16 at 21:27
  • Just to be sure, can you `unset GIT_DIR` before your cd, and git submodule command? – VonC Dec 18 '16 at 21:33
  • @VonC Still nothing. No error, but no submodule either. – OJFord Dec 18 '16 at 21:35
  • @OllieFord what a `git --work-tree=... --git-dir=...status` tells you? – VonC Dec 18 '16 at 21:37
  • @VonC Clean. -- – OJFord Dec 18 '16 at 21:41
  • Ahah! On to something now.. I made an empty commit, pushed it, and checked `status` again - now it's showing unstaged changes including `deleted: {submodule}`. – OJFord Dec 18 '16 at 21:43
  • What happens if you run with `git -c core.bare=false`? (and also the explicit worktree path) – jthill Dec 18 '16 at 21:46
  • I think it's working now, semi-problem now is that it's a private repo, so it fails to clone. I didn't really want to put keys on there - don't suppose there's a way to include the submodule in the push somehow? – OJFord Dec 18 '16 at 21:48
  • you can maintain the submodule commits as branches in the main repo, see [here](https://stackoverflow.com/questions/31099593/how-to-commit-a-git-repository-inside-another-git-repository/31101264#31101264). – jthill Dec 18 '16 at 21:49
  • I did it using gitlab-ci. Please check [this](https://stackoverflow.com/a/68870892/14417051) – Sumanth Singh Aug 21 '21 at 07:20

1 Answers1

4

For some reason, the command needed to be run from inside the work tree, not the bare git directory, even though both arguments are supplied:

/bare-repo/hooks/post-receive:

git --work-tree=/served-data --git-dir=/bare-repo checkout -f
cd /served-data
git --work-tree=/served-data --git-dir=/bare-repo submodule update --init --recursive
OJFord
  • 10,522
  • 8
  • 64
  • 98
  • I am getting "fatal: Not a git repository" and "Unable to find current revision in submodule path" since my work tree is not a git repository. – ababak Apr 10 '18 at 10:46
  • @ababak That's what `--git-dir` is for - it says "I'm not in a git repo (or the wrong one), look here" – OJFord Apr 10 '18 at 20:06
  • Yes, I am getting this when --git-dir is specified and pointing to my bare repo: `a.babak@GRAD76 MINGW64 //bstorage/rep/set/scripts/maya/python/surf $ git --work-tree=//bstorage/rep/set/scripts/maya/python/surf --git-dir=//bstorage/rep/set/scripts/deployment/amg_maya_surf.git submodule update --init --recursive fatal: Not a git repository: ../.git/modules/clip Unable to find current revision in submodule path 'clip'` – ababak Apr 11 '18 at 12:18
  • Sorry, can't make a proper formatting but you can see the paths, the command and the result – ababak Apr 11 '18 at 12:20
  • 1
    @shaneparsons Now marked as answer, thanks. (I think you have to wait 2 days from asking to accept your own answer as answer, and it seems I found the problem more quickly than that, then forgot about it!) – OJFord Oct 29 '19 at 14:57
  • Remember to `cd` to the working directory before running the submodule command, otherwise you will get this error: `fatal: /usr/lib/git-core/git-submodule cannot be used without a working tree.` – Joe Heffer Oct 23 '20 at 08:54
  • Yes, that is the error the question was about, and `cd`ing is the solution in this answer. – OJFord Oct 26 '20 at 14:31