Anders Abel
Anders Abel

Reputation: 69270

Same file in two locations using git

I've got a library project that has an associated JavaScript file, that any web sites using the library will have to include.

In the source control, the js file belongs to the library (which is right). However, when running/debugging/changing the application it is really convenient to treat the file as belonging to the web project using the library.

In TFS we handled this by remapping the scripts directory of the library onto the scripts directory of the web project, meaning that when working the project, the js file effectively looks as part of the web project, while it follows the library when branching/merging.

How do I do something similar in git?

Using a build script to copy the file is not a viable solution. It would make live changes while debugging impossible since it would only update the copy, or require a rebuild of the entire app for just a small change to force the file to be copied.

Using "add as link" is not an option either, since the web server isn't reading the project file and won't know of the link and will just return a 404 when the script is requested.

The project is developed in Visual Studio 2012, so any solution must work well together with the project structure in VS.

Upvotes: 1

Views: 450

Answers (3)

Anders Abel
Anders Abel

Reputation: 69270

A workaround is to use ntfs hard links.

mklink /h aliasdir\script.cs realdir\script.cs

Then add aliasdir to .gitignore to prevent git from adding the file twice. The file will now be in one place only (realdir) but can be added from both places.

The con is that the hard links have to be created each time the repository is cloned, it is not a configuration that is possible to check in.

I also tried with symlinks (mklink wihtout /h) first, but Cassini (the ASP.NET development server) doesn't follow them.

Upvotes: 1

sschuberth
sschuberth

Reputation: 29867

How about just making the library project's Git repository a submodule (or a subtree, if you prefer) of the application's Git repository, and then using a relative path to refer to the library's JavaScript file from the application?

Upvotes: 2

Pondidum
Pondidum

Reputation: 11637

Depending on your number of projects to use this with, you can add the file to another project, but in the add dialog, select to add the file as a link:

Add as link

This will keep the file in its original location, but act as if it was part of the solution (so you can do Content|Copy-Always etc to it), and has the advantage of being source-control agnostic.

Upvotes: 0

Related Questions