Fit Dev
Fit Dev

Reputation: 3689

Visual Studio GIT with Symlinks / Linked Files in Solution

I have a file structure as below:

D:\SharedCodeDir\codefile1.txt
D:\SharedCodeDir\codefile2.txt
D:\SharedCodeDir\...
D:\Solution\Project\projectfile1.txt
D:\Solution\Project\projectfile2.txt
D:\Solution\Project\...

I need to include D:\SharedCodeDir\codefile1.txt and other "shared" files in my VS project. I have several options to accomplish this:

  1. I can create a symlink to D:\SharedCodeDir\codefile1.txt in D:\Solution\Project\ so that D:\Solution\Project\codefile1.txt will really be a symbolic link to real file at D:\SharedCodeDir\codefile1.txt

  2. I can simply add D:\SharedCodeDir\codefile1.txt to my VS project as a link in the Add File dialog.

  3. I can create a hard link, so that both D:\SharedCodeDir\codefile1.txt and D:\Solution\Project\codefile1.txt will point to the same data on disk. This is least desired.

  4. I could create a folder symlink / junction in D:\Solution\Project to D:\SharedCodeDir, so that D:\Solution\Project\SharedCodeDir is really a symlink / junction to the real folder in D:\SharedCodeDir

Question: In Visual Studio 2013 / 2015, using Microsoft Git provider, how do I properly add my solution / project to Git source control, and which git attributes in the config gile if any I have to set, so that my "shared" files in D:\SharedCodeDir are properly accounted for (i.e. being tracked)?

Behavior I want:

Sample Case: 1) Suppose codefile1.txt is first being edited from VS as part of Solution 1. Then it is committed. 2) Then that same codefile1.txt is edited in some other program or from VS but from Solution 2 (in which case, it too is committed to Solution 2's repo at the end). 3) Now, when I get back to Solution 1, VS / git should alert me that there were changes to codefile1.txt since the last commit and ask me what I want to do.

Now, if codefile1.txt is not committed in step 1), that is it is checked out but not checked back in, I should be able to edit it outside VS or from another solution and it should behave as a normal linked file (VS alerting me of the fact that the file has changed since it was opened, if the file is opened in VS and has been changed outside). Then, I should just commit it as part of a solution that uses the file, so that it will be committed with all the latest changes to the solution's repo.

Finally, under no circumstances, should any Git-bound solution / repo attempt to replace the physical file at D:\SharedCodeDir\codefile1.txt with the latest version that they have committed (in cases they are different). It should only alert me of the fact that the files are different and what I want to do (replace the D:\SharedCodeDir\codefile1.txtwith the one from repo, or the other way around).

Also, I do not want D:\SharedCodeDir\codefile1.txt to be copied locally by Git to D:\SharedCodeDir\Solution\Project\codefile1.txt as another physical file. That is D:\SharedCodeDir\Solution\Project\codefile1.txt should always be a link of some sort (as described in alternatives 1 - 4 above) to the real file in D:\SharedCodeDir\codefile1.txt .

So how can I achieve this setup?

If it is really not possible with Git, how about SVN or alternatives?

Upvotes: 2

Views: 2256

Answers (1)

MelOS
MelOS

Reputation: 653

In my case, very similar to yours, I did the following:

  1. Added files outside my solution folder using Add As Link in Visual Studio. These file links will be placed in a certain folder inside my solution.

In my case, these files will be shared with other projects and so each project will do the next step:

  1. To physically copy these files from lets say D:\SharedCode\*.cs to D:\MyProject\Shared*.cs I edited .csproj file and added the following just before the </Project> tag
<Target Name="CopyLinkedFiles" BeforeTargets="Build">
<Copy SourceFiles="%(Compile.Identity)" DestinationFiles="%(Compile.Link)" 
SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Condition="'%(Compile.Link)'
!= ''" />
</Target>

So in this case all folders will work on newest copy of the shared files but still git will treat them as they were part of the specific project.

Note: If you need to link Content files(Resources,.txt..) instead of Compile files(.cs), replace Compile. with Content. in the code above

Upvotes: 3

Related Questions