doubi7306 2018-01-31 08:55
浏览 80
已采纳

通过项目同步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

  • 写回答

2条回答 默认 最新

  • douhui8025 2018-02-02 01:51
    关注

    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/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗