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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口