如何跨文件夹或存储库共享文件

时间:2018-11-15 20:33:15

标签: git version-control workflow

我正在处理一个必须跨多个主题的代码段。

假设我有三个主题:

  • 橙色
  • 绿色
  • 蓝色

每个主题的内部都有一个名为“代码段”的文件夹,我在其中放置了代码段。

  • 橙色/snippets/code.html
  • 绿色/snippets/code.html
  • 蓝色/snippets/code.html

每个主题中的“ code.html”文件都完全相同。我会在自己的GitHub存储库中对其进行跟踪,然后将其复制并粘贴到每个主题存储库中。

如何在一个地方编辑此代码段,并确保它在所有主题中都得到更新?我正在寻找一种可扩展的方法,主题的数量可能会增长到20-30。

在我的情况下,与“ code.html”的示例相比,我有更多的文件-它们位于两个文件夹中,总大小约为10。

我正在阅读有关Git子模块的信息,但我对它们是否适合我的问题没有信心。我不确定这个问题是否与Git有关,对此感到抱歉。

P.S。我正在开发SaaS-确切地说是Shopify。因此,在这种情况下,任何形式的PHP技巧都将无法使用。我需要以本地方式对文件进行处理。

编辑:现实生活中的示例:

我有这4个文件:

{{ theme }}/snippets/file1.liquid
{{ theme }}/snippets/file2.liquid
{{ theme }}/assets/file3.liquid
{{ theme }}/assets/file4.liquid

我无法创建子目录或其他目录。

3 个答案:

答案 0 :(得分:1)

假设您在存储库中拥有通用代码。 您可以在<Theme>/snippets/common中创建一个子模块。每次您要构建/部署主题时,都将其更新为最新的母版。

这意味着您的code.html将在<Theme>/snippets/common/code.html

cd <Theme>/snippets

# To add the submodule 
git submodule add <common code repository> common
# 2 new files will be added to the repository
# .gitmodules and the "common" folder

# When you clone the repo in another machine
# Always do the following command 
# to get the files from the common repository
git submodule update --init

# If you made a change in the common repository
git submodule update --remote

答案 1 :(得分:0)

像您上面列出的那样的Php文件结构将起作用。 snippets文件夹本质上是一个.php扩展文件,其中包括在项目之间或您所讨论的主题中共享的html文件的附件。

答案 2 :(得分:0)

也许您可以使用bash脚本在整个存储库中自动执行此过程。以下脚本将采用您在第一个变量中设置的所有文件路径,并找到最后修改的文件路径。从那里它将文件复制到其余所有文件,将它们添加到存储库中,然后提交。

#!/usr/bin/env bash

declare -a filePaths=("Orange/snippets/code.html" "Green/snippets/code.html" "Blue/snippets/code.html")
declare newestFile

for file in "${filePaths[@]}"; do
echo "current file is $file"
if [[ "$file" -nt ${newestFile} ]];
then
newestFile="${file}"
fi
echo "newest file is ${newestFile}"
done

for file in "${filePaths[@]}"; do
if ! [[ "$file" -ef $newestFile ]];
then
echo "copying ${newestFile} to ${file}"
cp ${newestFile} ${file}
currentDir=`dirname "${file}"`
cd ${currentDir}
git add ${file}
git commit -m 'automatic update commit'
fi
done

修改code.hmtl文件后,您可以运行此脚本或将其作为生产管道的一部分。

相关问题