【问题标题】:How to share source code library between several solutions in different repositories?如何在不同存储库中的多个解决方案之间共享源代码库?
【发布时间】:2017-08-27 02:53:39
【问题描述】:

我有一个公共库,我想在几个解决方案之间共享它,它存储在不同的 github 存储库中。我们将共享库放在单独的 GitHub 存储库中并创建了 nuget 包,可以将其安装在每个所需的项目/解决方案中。缺点是更改库中的代码涉及几个步骤(更改代码、推送到库存储库、创建 nuget 包、安装包),这很烦人。

我想使用git subtree 来创建带有库项目的子目录,并将其用作我的解决方案中的本地项目,并且可以轻松更改代码/测试/调试。 “主要”解决方案的开发人员将有一个选择 - 将库作为二进制 nuget 包或作为子目录中的源项目。

问题: 当我尝试git subtree 时,我发现了一个问题:当包含在独立库解决方案中和包含在一个主要的解决方案。 在 MyLib.sln 中,路径为 "..\Packages" ,但在 Main.sln 中,路径应为 "..\..\Packages" 。
Main GitHub Repository(和 OtherUserOfMyLib GitHub Repository(is))的结构:

Main.sln
LibSubfolder
-----------------| MyLib.sln
-----------------| MyLibSource
--------------------------| myLib.csproj
--------------------------| myLib code
-----------------| (expected Packages from lib solution)
Packages.  (From main solution)

MyLib GitHub Repository 的结构:

MyLib.sln
MyLibSource
--------------| myLib.csproj
--------------| myLib code
Packages.  (From lib solution)

我尝试git subtree 从 Lib 存储库中仅复制 MyLibSource 子目录(这将允许包文件夹的相对位置相同)。不幸的是,我没有找到从另一个 repo 复制子目录的方法,该方法支持稍后将更改推送回原始 repo。答案 Add subdirectory of remote repo with git-subtree 有几个选项,但所有选项似乎都只讨论了单向(拉)同步。 在我看来,git submodule 和 git subrepo 不支持从源存储库复制子文件夹。

我错过了什么吗?任何人都可以建议,如何从库存储库复制到我的存储库文件夹的子文件夹,这将允许以后的双向(拉/推)同步?

我考虑的其他选项是更改包文件夹的位置。但我不确定,选择哪个路径对 main.sln 和 MyLib.sln 保持一致。

第三种方法是不要在 MyLib 存储库中使用 MyLib.sln ;仅将存储库用作库源代码的主存储。而是在主存储库中创建 MyLib.sln 并使用它来构建 NuGet 包。我将能够使用 MyLibSource 项目的 git subtree pull/push 内容。 OtherUserOfMyLib 存储库将仅引用子文件夹中的项目,并在需要时调用 git subtree pull/push

Main.sln
MyLib.sln       ( locate in Main repository instead of MyLib repository)
LibSubfolder
-----------------| MyLibSource
--------------------------| myLib.csproj
--------------------------| myLib code
Packages

我的问题是尝试哪种方法:

  • 花时间试图找到一种方法来推回 git subtree created 子文件夹?
  • 尝试更改包文件夹的位置?
  • 保留在存储库之外构建 nuget 包的解决方案 有包裹(第三种方法)?
  • 还有什么?

我看过类似的问题,例如 Best practice to share common libraries between solutions in .NET 和 How do you share code between projects/solutions in Visual Studio? 但没有找到满意的解决方案。

【问题讨论】:

    标签: git visual-studio nuget-package projects-and-solutions


    【解决方案1】:

    我能够使用 git subtree 将公共存储库(例如源代码库)共享为不同客户端解决方案(存储库)的子文件夹,并允许将更改推送回共享存储库。

    我们对 c# 库使用类似的方法,Powershell psm 模块的文件夹,考虑用于收集 React jsx 组件。对于 c# 库,其中一个客户端存储库构建 NuGet 包,任何喜欢引用 NuGet 包而不是源代码的解决方案都可以使用该包。

    如果我们需要包含一些开源 github 库的源代码(例如,如果我们希望在新版本可用之前进行更改),我们会使用相同的方法。

    在每个应用程序存储库中,我们需要自定义两个批处理文件(针对每个公共存储库):subtreePull 和 subtreePush。

    批处理文件示例(基于Add subdirectory of remote repo with git-subtree 答案的选项 4):
    MyCommonLib-SubtreePull.cmd

    @rem Template Pull and Push  are located in SubTreeCommands folder
    @Rem from https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree
    @set remoteBranch=master
    @set RelativeTarget=src/MyApplicationProject/LibSource/MyCommonLib
    @echo If you have errors "Working tree has modifications.  Cannot add." uncomment stash before command and stash apply after command
    @REM git stash 
    @rem run this command from the toplevel of the working tree 
    @for /f %%i in ('git rev-parse --show-toplevel ') do set GitRoot=%%i
    CD %GitRoot%
    IF EXIST %RelativeTarget% (
    set gitCmd=pull
    ) ELSE (
    set gitCmd=add  
    )
    @rem if you want to have full history, do not include  --squash
    git subtree %gitCmd%  --prefix %RelativeTarget% https://github.com/MyCompany/MyCommonLib.git %remoteBranch% --squash
    @REM git stash apply
    @pause
    

    MyCommonLib-SubtreePush.cmd

    @Rem from https://medium.com/@v/git-subtrees-a-tutorial-6ff568381844#.a2ne9vlve
    set remoteBranch=master
    @rem run this command from the toplevel of the working tree 
    @for /f %%i in ('git rev-parse --show-toplevel ') do set GitRoot=%%i
    CD %GitRoot%
    git subtree push --prefix=src/MyApplicationProject/LibSource/MyCommonLib https://github.com/MyCompany/MyCommonLib.git  %remoteBranch%
    @pause
    

    在工作期间,您正在为您的应用程序存储库中的更改执行正常的 git commit/git push,包括 LibSource 公共库子文件夹中的更改。

    要同步您的常用库更改,请运行 subtreePush/subtreePull 命令。
    请注意,建议在库发生重大更改后(例如,在每个相关项目结束时)运行 subtreePush。
    subtreePull 可以由不同的人在不同的存储库中完成,建议在对存储库进行重大工作之前运行(例如在项目开始时)。
    子树拉取和推送批处理都需要一些时间来运行,但您可以启动它们并执行其他任何操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-22
      • 2010-09-13
      • 2020-12-22
      • 1970-01-01
      • 2017-10-26
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多