Lukas Oppermann
Lukas Oppermann

Reputation: 2938

git submodule pull and commit automatically on webserver

I have the following setup, I am working on a project project with the submodule submodule. Whenever I push changes to github it sends a post request to update.php on the server.

This php file executes a git command. Without submodules I can just do a git pull and everything is fine but with submodules it is much more difficult.

I have this at the moment, but it does not do what I want. I should git pull the repo and update and pull the latest version of each submodule.

<?php echo `git submodule foreach 'git checkout master; git pull; 
git submodule update --init --recursive; git commit -m "updating"' && git pull && 
git submodule foreach 'git add -A .' 
&& git commit -m "updating to latest version including submodules" 2>&1s`;

EDIT// Okay, I got it half way done.

<?php echo `git submodule foreach 'git checkout master; git pull; 
git submodule update --init --recursive; git commit -am "updating"; echo "updated"' && 
git pull &&  git commit -am "updating to latest version including submodules" && 
echo 'updated'`;

The echo prevents the script to stop because of non-zero returned. It works 100% fine when I run it from the console using php update.php.

When github initialized the file, or I run it from the browser it still does not work.

Any ideas?

Upvotes: 1

Views: 1212

Answers (1)

Michael
Michael

Reputation: 1522

Even if it would work there are things you should know about:

  1. If there are new submodules, you don't init them. So you should start your script with git pull && git submodule update --init.

  2. git commit -am "updating" doesn't create a new commit because you didn't change anything in the submodule.

    • git checkout master only checks out branch master. This makes sense because of the otherwise detached head.

      Note: There might be repos where master is not the branch you want! See note below.

    • git pull fetches from the remote and merges the changes into master. Even if this is not a fast-forward merge¹, there's nothing to commit as the merge commit is made automatically.

      ¹Because this is a checkout on your server there shouldn't be a need for a non fast-forward merge. Otherwise you should think about merge conflicts and git push (see 3.), too!

    • git submodule update --init --recursive only fetches from the submodule's submodule remotes and checks out the already referenced commits. So nothing new to commit here, either.

  3. If git commit -am "updating" would actually create a commit, the commit would be only available in the checked out version (fork), not in the referenced repository (see url = … in .gitmodules). So you commit references to submodule commit hashs in git commit -am "updating to latest version including submodules" that do not exist anywhere else.

    So in this case you would need to add a git push to your git submodule foreach '…'. See also here.

  4. As I mentioned in a comment to your question this workflow has one major disadvantage: Nobody tested the project files with the latest commits in the submodules. This might break the project!

Try (indented for better readability):

<?php echo `git pull &&
  git submodule update --init &&
  git submodule foreach 'git checkout master;
    git pull; 
    git submodule update --init --recursive;
    echo "updated"' && 
  git commit -am "updated submodules" && 
  echo 'updated'`;
?>

Note on permissions:

You said it works (with submodules) when using the php cli. Did you try this on your local machine or on the server? And if you did so on the server did you try it using the webserver's account?

Note on submodule branches:

Using git checkout master (see 2.1) lets you only use the master branch of a submodule. To change this add update = rebase or update = merge to each entry in .gitmodules. (No detached head any more!) Then run git submodule foreach 'git checkout master' (or check out/create any other tracking branch) and you can simply use git submodule foreach 'git pull' from now on.

Upvotes: 2

Related Questions