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条)

报告相同问题?

悬赏问题

  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 chatgpt网页版侧边栏和模型切换点击没反应怎么办
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas