Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using git submodule update --init on a post hook

Tags:

git

I have this simple hook on /hooks/post-update inside a bare repository:

#!/bin/sh
git-update-server-info

GIT_WORK_TREE=/home/user/apps/application-site/dev git checkout -f develop
GIT_WORK_TREE=/home/user/apps/application-site/dev git submodule update --init

GIT_WORK_TREE=/home/user/apps/application-site/master git checkout -f master
GIT_WORK_TREE=/home/user/apps/application-site/master git submodule update --init

The repository has some submodules, that I am expecting is pushing to production server, and checkout the two branches on two directories, so can I have later a dev.myapp.com for the development branch and www.myapp.com for the master branch, and all this updating also the submodules on the branches.

Checkout is working as expected, but not the submodule update --init, :'(

The remote output raises this errors.

remote: Switched to branch 'develop'
remote: You need to run this command from the toplevel of the working tree.
remote: Switched to branch 'master'
remote: You need to run this command from the toplevel of the working tree.

I am not quite sure what to do.

like image 594
Mario César Avatar asked Sep 29 '11 06:09

Mario César


People also ask

What does git submodule update -- init do?

The git submodule init command creates the local configuration file for the submodules, if this configuration does not exist. If you track branches in your submodules, you can update them via the --remote parameter of the git submodule update command.

Do submodules update automatically?

Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.

How do I run a git submodule init?

Git Submodule init Syntax git submodule init is a straightforward command that performs a single path recording task. Run it by using the syntax below: git submodule init -- [path1​] [path2..] Note: Execute git submodule init in the main repository directory.


2 Answers

The answer is to do exactly what git is telling you, which is:

remote: You need to run this command from the toplevel of the working tree.

So do that. Here's a sample post-update hook:

#!/bin/sh

export GIT_DIR=$(pwd)

cd /home/lars/projects/stackoverflow/gitstuff/worktree

git checkout -f master
git submodule init
git submodule update

This assumes that:

  • core.bare is false
  • receive.denyCurrentBranch is ignore. You need this because otherwise you'll get an error pushing into a repository with core.bare set to false.

...and seems to work in my limited testing.

like image 110
larsks Avatar answered Sep 22 '22 10:09

larsks


Your first GIT_WORK_TREE variable (line 4) is suspect, considering you are refering in the next lines to a $WORK_TREE variable (which isn't defined in this script).


Note that starting git1.8.4 (July 2013), you wouldn't have (for git submodule commands) to go back to the root directory anymore.

See commit 091a6eb0feed820a43663ca63dc2bc0bb247bbae:

submodule: drop the top-level requirement

Use the new rev-parse --prefix option to process all paths given to the submodule command, dropping the requirement that it be run from the top-level of the repository.

Since the interpretation of a relative submodule URL depends on whether or not "remote.origin.url" is configured, explicitly block relative URLs in "git submodule add" when not at the top level of the working tree.

Signed-off-by: John Keeping

Depends on commit 12b9d32790b40bf3ea49134095619700191abf1f

This makes 'git rev-parse' behave as if it were invoked from the specified subdirectory of a repository, with the difference that any file paths which it prints are prefixed with the full path from the top of the working tree.

This is useful for shell scripts where we may want to cd to the top of the working tree but need to handle relative paths given by the user on the command line.

like image 20
VonC Avatar answered Sep 21 '22 10:09

VonC