Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when I clone a repository with symlinks on Windows?

There's been a lot of questions about adding support for symlinks on Windows. But, what actually happens when I clone a repository with symlinks on Windows?

like image 950
Andres Riofrio Avatar asked Jul 26 '12 05:07

Andres Riofrio


People also ask

Does git support symlinks on Windows?

Although Git supports symlinks, I would strongly recommend against storing them as links in your repository, especially if you're also working with that code on Windows.

Do Windows symlinks work?

Overview. Symlinks, or symbolic links, are “virtual” files or folders which reference a physical file or folder located elsewhere, and are an important feature built in to many operating systems, including Linux and Windows. The Windows' NTFS file system has supported symlinks since Windows Vista.

What happens if you clone an existing repository?

When you clone a repository, you copy the repository from GitHub.com to your local machine. Cloning a repository pulls down a full copy of all the repository data that GitHub.com has at that point in time, including all versions of every file and folder for the project.

What does git do with symlinks?

Git just stores the contents of the link (i.e. the aforementioned path of the file system object that it links to) in a 'blob' just like it would for any other file. It then stores the name, mode and type (including the fact that it is a symlink) in the tree object that represents its containing directory.


2 Answers

Since version 1.5.3 of the native Git client git clone and git init will probe the target file system for symlink support and set the local repository configuration for core.symlinks accordingly, i.e. to false for FAT or NTFS. This makes symlinks created and committed e.g. under Linux appear as plain text files that contain the link text under Windows (see the git config documentation on core.symlinks for details).

Since Git for Windows version 2.10.2 the installer has an explicit option to enable symbolic link support.

In older versions of Git for Windows you can manually set core.symlinks to true which enabled Git to create symbolic links under the following constraints:

  • Symbolic links are only available on Windows Vista and later.
  • Symbolic links will only work on NTFS, not on FAT.
  • You need to be an admin and / or have the SeCreateSymbolicLinkPrivilege privilege.
  • Symbolic links on remote filesystems are disabled by default.
  • Windows' symbolic links are typed.
  • Many programs do not understand symbolic links (that includes older version of Windows Explorer).

More details are available in the Git for Windows wiki.

In older versions of Git for Windows manually setting core.symlinks manually to true after cloning and reset your working tree, you would get error messages similar to

$ git reset --hard HEAD error: unable to create symlink directory (Function not implemented) error: unable to create symlink linux-links/this_is_a_symbolic_link_to_file (Function not implemented) fatal: Could not reset index file to revision 'HEAD'. 

As a side note, the JGit client did not probe the target file system for symlink support until its version 3.3, so the core.symlinks setting was falling back to whatever the system / global Git configuration was. Starting with version 3.3 JGit probes for symlink support but seems to be too conservative, setting core.symlinks = false in some cases where symlinks would be in fact supported.

You can checkout https://github.com/sschuberth/git-playground which contains a bunch of links created on Linux for testing.

like image 93
sschuberth Avatar answered Oct 11 '22 07:10

sschuberth


One solution would have a filter in order to detect symlinks stored by Git and replace them with Windows symlink.
That is detailed in "Git Symlinks in Windows"

However, true symlink support isn't just for now:
See issue 224 and the very recent (July 2012) discussion on GitHub (which you looked at):

here are three types of file system links on Windows: hardlinks, junctions, and symlinks.

  • Hardlinks and junctions are available since NT. Hardlinks can only point to files, junctions only to directories (on the same volume).
  • The symlinks available since Vista can point to either files or directories, also on different volumes.
  • mklink, which ships since Vista, can create all of the above. But the way it is called in the script makes it only create symlinks (which is good, IMHO, as they most closely resemble Linux symbolic links).

For pre-Vista, we'd need a fallback that either creates hardlinks for files using "fsutil hardlink" (but probably only if "ln" is called without "-s") and creates junctions for directories using "fsutils reparsepoint", or simply calls the original ln.exe.

In addition to breaking Windows XP setups, a change like this will also break standard Windows 7 setups, because mklink requires administrator privileges by default. This can be fixed by checking if it worked or not, and reverting to copying in such cases.

Just for the record: I played around a bit with trying to make symlink support in Git for Windows itself recently, but ended up concluding that the "symlink support" in Windows 7 and up is pretty close to useless for emulating Unix symlinks.

There is a project claiming "Open Source, 100% Compatible ln for Windows (and Junction Point library)", but:

Unfortunately, normal users doesn't have the required permissions to create symlinks by default on Windows. Combine this with the fact that you cannot change what a symlink points to in the same way that POSIX requires, makes them more or less useless for us. This I already wrote above.

Now, the authors can claim all they want that this is "100% compatible" for all I care, but a quick look at the source code reveals that it's not. They do not provide any fallbacks, they don't even load the CreateSymbolicLink function dynamically. So the result on non-symlink capable Windows versions will be a crash with a missing symbol error.

like image 24
VonC Avatar answered Oct 11 '22 07:10

VonC