Skip to content

Generate a unique ID at compile time to work around a CLR bug #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ _ReSharper*/
*.userprefs
*.swp
*.DotSettings
#Ignore custom generated files
LibGit2Sharp/Core/UniqueIdentifier.cs

!Lib/NativeBinaries/*/*.pdb
42 changes: 42 additions & 0 deletions CustomBuildTasks/CustomBuildTasks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B6138573-A4B9-44E7-83C2-8964CAF51EDA}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CustomBuildTasks</RootNamespace>
<AssemblyName>CustomBuildTasks</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities" />
</ItemGroup>
<ItemGroup>
<Compile Include="GenerateUniqueIdentifierTask.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
36 changes: 36 additions & 0 deletions CustomBuildTasks/GenerateUniqueIdentifierTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace CustomBuildTasks
{
public class GenerateUniqueIdentifierTask : Task
{
public override bool Execute()
{
using (FileStream fs = new FileStream(this.OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.WriteLine("using System;");
sw.WriteLine();
sw.WriteLine("namespace LibGit2Sharp.Core");
sw.WriteLine("{");
sw.WriteLine(" internal static class UniqueId");
sw.WriteLine(" {");
sw.WriteLine(" public const String UniqueIdentifier = \"" + Guid.NewGuid().ToString() + "\";");
sw.WriteLine(" }");
sw.WriteLine("}");
}

return true;
}

public String OutputFile
{
get;
set;
}
}
}
Binary file added Lib/CustomBuildTasks/CustomBuildTasks.dll
Binary file not shown.
2 changes: 2 additions & 0 deletions LibGit2Sharp/Core/FilePathMarshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace LibGit2Sharp.Core
///
/// Use this marshaler for return values, for example:
/// [return: MarshalAs(UnmanagedType.CustomMarshaler,
/// MarshalCookie = UniqueId.UniqueIdentifier,
/// MarshalTypeRef = typeof(FilePathNoCleanupMarshaler))]
/// </summary>
internal class FilePathNoCleanupMarshaler : FilePathMarshaler
Expand Down Expand Up @@ -42,6 +43,7 @@ public override void CleanUpNativeData(IntPtr pNativeData)
/// [DllImport(libgit2)]
/// internal static extern int git_index_open(out IndexSafeHandle index,
/// [MarshalAs(UnmanagedType.CustomMarshaler,
/// MarshalCookie = UniqueId.UniqueIdentifier,
/// MarshalTypeRef = typeof(FilePathMarshaler))] FilePath indexpath);
/// </summary>
internal class FilePathMarshaler : ICustomMarshaler
Expand Down
182 changes: 91 additions & 91 deletions LibGit2Sharp/Core/NativeMethods.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions LibGit2Sharp/Core/Utf8Marshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace LibGit2Sharp.Core
///
/// Use this marshaler for return values, for example:
/// [return: MarshalAs(UnmanagedType.CustomMarshaler,
/// MarshalCookie = UniqueId.UniqueIdentifier,
/// MarshalTypeRef = typeof(Utf8NoCleanupMarshaler))]
/// </summary>
internal class Utf8NoCleanupMarshaler : Utf8Marshaler
Expand Down Expand Up @@ -42,6 +43,7 @@ public override void CleanUpNativeData(IntPtr pNativeData)
/// [DllImport(libgit2)]
/// internal static extern int git_tag_delete(RepositorySafeHandle repo,
/// [MarshalAs(UnmanagedType.CustomMarshaler,
/// MarshalCookie = UniqueId.UniqueIdentifier,
/// MarshalTypeRef = typeof(Utf8Marshaler))] String tagName);
/// </summary>
internal class Utf8Marshaler : ICustomMarshaler
Expand Down
2 changes: 2 additions & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
<Compile Include="TreeEntryDefinition.cs" />
<Compile Include="VoidReference.cs" />
<Compile Include="Core\RawContentStream.cs" />
<Compile Include="Core\UniqueIdentifier.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
Expand All @@ -261,6 +262,7 @@
<EmbeddedResource Include="libgit2sharp_hash.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="UniqueIdentifier.targets" />
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
Expand Down
26 changes: 26 additions & 0 deletions LibGit2Sharp/UniqueIdentifier.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- Import the version of CustomBuildTasks.dll that is dropped in the repository. -->
<UsingTask TaskName="GenerateUniqueIdentifierTask" AssemblyFile="..\Lib\CustomBuildTasks\CustomBuildTasks.dll" />

<!-- Set the path of the generated UniqueIdentifier.cs file. -->
<PropertyGroup>
<UniqueIdentifierPath>Core\UniqueIdentifier.cs</UniqueIdentifierPath>
<CoreCompileDependsOn>$(CoreCompileDependsOn);GenerateUniqueIdentifierCs</CoreCompileDependsOn>
<CoreCleanDependsOn>$(CoreCleanDependsOn);CleanUniqueIdentifierCs</CoreCleanDependsOn>
</PropertyGroup>

<!-- This target runs if any of the projects or .cs files for the project have changed since the last time
the UniqueIdentifier.cs file was generated. -->
<Target Name="GenerateUniqueIdentifierCs"
Inputs="$(MSBuildThisFileFullPath);$(MSBuildAllProjects);@(Compile)"
Outputs="$(UniqueIdentifierPath)">
<GenerateUniqueIdentifierTask OutputFile="$(UniqueIdentifierPath)" />
</Target>

<Target Name="CleanUniqueIdentifierCs">
<Delete Files="$(UniqueIdentifierPath)" />
</Target>

</Project>