通过项目同步PHP文件

I have a php server with this file tree :

/common
    - functions.inc.php
    - ...
/website1
    /functions
        - functions.inc.php
/website2
    /functions
        - functions.inc.php

Websites should be shipped separately without the common folder which might contain non-needed files per case (jquery extensions,...)

I would like to synchronize some files in common through all the website* projects, so for example when I change something in common/functions.inc.php the file is replaced in all the website*/functions/ folders

I am thinking of a bash script to do this (manual or through inotify), but I was wondering is there an automatic/manual native or an existing tool through version control (git, svn,...) to synchronize these files this way?

I have in mind something like the versioning of angular dependencies or maven depencies using a version number.

I am currently using an external svn (https://www.assembla.com/home), but if git has more options for this case I could consider migrating. My projects are on a linux machine (Raspberry pi or Lubuntu) but a Windows approach could be handy.

I completely own the functions.inc.php file so I can write anything inside such as a version number

Yes, you can use git pre-commit hook to sync common/functions.inc.php file with website*/functions/functions.inc.php files authmatically.

Assume all the files of the file tree managed in master, then below sheel scripts for per-commit hook will detect if the common/functions.inc.php file changes, if changes, update website*/functions/functions.inc.php files before committing changes:

#!/bin/sh
#

branch=$(git rev-parse --abbrev-ref HEAD)
if [[ $branch == "master" ]]; then
  {
    echo "this is master branch"
    file="common/functions.inc.php"
    if [[ $(git diff --name-only HEAD) =~ $file ]]; then
      echo "changed the file"
      cp -fr common/functions.inc.php website1/functions
      cp -fr common/functions.inc.php website2/functions
    else
      echo "didn't change the file $(git diff --name-only HEAD)"
    fi
  }
else
  echo "it's not on master branch"
fi
git add .

And if you want to ship the code except the commom folder, then you can create a new branch (such as release), and then ignore the common folder:

git checkout -b release
rm -rf common
touch .gitignore
echo common >> '.gitignore'
git add .
git commit -m 'ignore common folder in release branch'

And when your code is changed (in website* folder), you can update the release branch for a new shipment:

git checkout release
git checkout master -- website1/
git checkout master -- website2/

You can use :

Git submodules

  • make common a separate git repo.
  • Then declare it as a submodule in each website.

Composer

  • make common a separate composer package (also a git repo)
  • declare it as a dependency in each website.

This means that websites will also have to be managed by composer.