Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Upgrade to latest libgit2 #1124

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

Merged
merged 2 commits into from
Jul 4, 2015
Merged
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
163 changes: 99 additions & 64 deletions LibGit2Sharp.Tests/FilterFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ public void InitCallbackNotMadeWhenFilterNeverUsed()
initializeCallback);
var registration = GlobalSettings.RegisterFilter(filter);

Assert.False(called);

GlobalSettings.DeregisterFilter(registration);
try
{
Assert.False(called);
}
finally
{
GlobalSettings.DeregisterFilter(registration);
}
}

[Fact]
Expand All @@ -89,16 +94,22 @@ public void InitCallbackMadeWhenUsingTheFilter()
successCallback,
initializeCallback);
var registration = GlobalSettings.RegisterFilter(filter);
Assert.False(called);

string repoPath = InitNewRepository();
using (var repo = CreateTestRepository(repoPath))
try
{
StageNewFile(repo);
Assert.True(called);
}
Assert.False(called);

GlobalSettings.DeregisterFilter(registration);
string repoPath = InitNewRepository();
using (var repo = CreateTestRepository(repoPath))
{
StageNewFile(repo);
Assert.True(called);
}
}
finally
{
GlobalSettings.DeregisterFilter(registration);
}
}

[Fact]
Expand All @@ -116,13 +127,18 @@ public void WhenStagingFileApplyIsCalledWithCleanForCorrectPath()
var filter = new FakeFilter(FilterName, attributes, clean);
var registration = GlobalSettings.RegisterFilter(filter);

using (var repo = CreateTestRepository(repoPath))
try
{
StageNewFile(repo);
Assert.True(called);
using (var repo = CreateTestRepository(repoPath))
{
StageNewFile(repo);
Assert.True(called);
}
}
finally
{
GlobalSettings.DeregisterFilter(registration);
}

GlobalSettings.DeregisterFilter(registration);
}

[Fact]
Expand All @@ -138,17 +154,22 @@ public void CleanFilterWritesOutputToObjectTree()
var filter = new FakeFilter(FilterName, attributes, cleanCallback);
var registration = GlobalSettings.RegisterFilter(filter);

using (var repo = CreateTestRepository(repoPath))
try
{
FileInfo expectedFile = StageNewFile(repo, decodedInput);
var commit = repo.Commit("Clean that file");
var blob = (Blob)commit.Tree[expectedFile.Name].Target;
using (var repo = CreateTestRepository(repoPath))
{
FileInfo expectedFile = StageNewFile(repo, decodedInput);
var commit = repo.Commit("Clean that file");
var blob = (Blob)commit.Tree[expectedFile.Name].Target;

var textDetected = blob.GetContentText();
Assert.Equal(encodedInput, textDetected);
var textDetected = blob.GetContentText();
Assert.Equal(encodedInput, textDetected);
}
}
finally
{
GlobalSettings.DeregisterFilter(registration);
}

GlobalSettings.DeregisterFilter(registration);
}

[Fact]
Expand All @@ -165,19 +186,24 @@ public void WhenCheckingOutAFileFileSmudgeWritesCorrectFileToWorkingDirectory()
var filter = new FakeFilter(FilterName, attributes, null, smudgeCallback);
var registration = GlobalSettings.RegisterFilter(filter);

FileInfo expectedFile = CheckoutFileForSmudge(repoPath, branchName, encodedInput);

string combine = Path.Combine(repoPath, "..", expectedFile.Name);
string readAllText = File.ReadAllText(combine);
Assert.Equal(decodedInput, readAllText);
try
{
FileInfo expectedFile = CheckoutFileForSmudge(repoPath, branchName, encodedInput);

GlobalSettings.DeregisterFilter(registration);
string combine = Path.Combine(repoPath, "..", expectedFile.Name);
string readAllText = File.ReadAllText(combine);
Assert.Equal(decodedInput, readAllText);
}
finally
{
GlobalSettings.DeregisterFilter(registration);
}
}

[Fact]
public void CanFilterLargeFiles()
{
const int ContentLength = 128 * 1024 * 1024;
const int ContentLength = 128 * 1024 * 1024 - 13;
const char ContentValue = 'x';

char[] content = (new string(ContentValue, 1024)).ToCharArray();
Expand All @@ -187,51 +213,60 @@ public void CanFilterLargeFiles()
var filter = new FileExportFilter(FilterName, attributes);
var registration = GlobalSettings.RegisterFilter(filter);

string filePath = Path.Combine(Directory.GetParent(repoPath).Parent.FullName, Guid.NewGuid().ToString() + ".blob");
FileInfo contentFile = new FileInfo(filePath);
using (var writer = new StreamWriter(contentFile.OpenWrite()) { AutoFlush = true })
try
{
for (int i = 0; i < ContentLength / content.Length; i++)
string filePath = Path.Combine(Directory.GetParent(repoPath).Parent.FullName, Guid.NewGuid().ToString() + ".blob");
FileInfo contentFile = new FileInfo(filePath);
using (var writer = new StreamWriter(contentFile.OpenWrite()) { AutoFlush = true })
{
writer.Write(content);
}
}
int remain = ContentLength;

string attributesPath = Path.Combine(Directory.GetParent(repoPath).Parent.FullName, ".gitattributes");
FileInfo attributesFile = new FileInfo(attributesPath);
while (remain > 0)
{
int chunkSize = remain > content.Length ? content.Length : remain;
writer.Write(content, 0, chunkSize);
remain -= chunkSize;
}
}

string configPath = CreateConfigurationWithDummyUser(Constants.Signature);
var repositoryOptions = new RepositoryOptions { GlobalConfigurationLocation = configPath };
string attributesPath = Path.Combine(Directory.GetParent(repoPath).Parent.FullName, ".gitattributes");
FileInfo attributesFile = new FileInfo(attributesPath);

using (Repository repo = new Repository(repoPath, repositoryOptions))
{
File.WriteAllText(attributesPath, "*.blob filter=test");
repo.Stage(attributesFile.Name);
repo.Stage(contentFile.Name);
repo.Commit("test");
contentFile.Delete();
repo.Checkout("HEAD", new CheckoutOptions() { CheckoutModifiers = CheckoutModifiers.Force });
}
string configPath = CreateConfigurationWithDummyUser(Constants.Signature);
var repositoryOptions = new RepositoryOptions { GlobalConfigurationLocation = configPath };

contentFile = new FileInfo(filePath);
Assert.True(contentFile.Exists, "Contents not restored correctly by forced checkout.");
using (StreamReader reader = contentFile.OpenText())
{
int totalRead = 0;
char[] block = new char[1024];
int read;
while ((read = reader.Read(block, 0, block.Length)) > 0)
using (Repository repo = new Repository(repoPath, repositoryOptions))
{
Assert.True(CharArrayAreEqual(block, content, read));
totalRead += read;
File.WriteAllText(attributesPath, "*.blob filter=test");
repo.Stage(attributesFile.Name);
repo.Stage(contentFile.Name);
repo.Commit("test");
contentFile.Delete();
repo.Checkout("HEAD", new CheckoutOptions() { CheckoutModifiers = CheckoutModifiers.Force });
}

Assert.Equal(ContentLength, totalRead);
}
contentFile = new FileInfo(filePath);
Assert.True(contentFile.Exists, "Contents not restored correctly by forced checkout.");
using (StreamReader reader = contentFile.OpenText())
{
int totalRead = 0;
char[] block = new char[1024];
int read;
while ((read = reader.Read(block, 0, block.Length)) > 0)
{
Assert.True(CharArrayAreEqual(block, content, read));
totalRead += read;
}

contentFile.Delete();
Assert.Equal(ContentLength, totalRead);
}

GlobalSettings.DeregisterFilter(registration);
contentFile.Delete();
}
finally
{
GlobalSettings.DeregisterFilter(registration);
}
}

[Fact]
Expand Down
8 changes: 4 additions & 4 deletions LibGit2Sharp.Tests/StashFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void CanStashAndApplyWithOptions()
repo.Stashes.Add(stasher, "This stash with default options");
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Apply(0));

Assert.Equal(FileStatus.NewInWorkdir, repo.RetrieveStatus(filename));
Assert.Equal(FileStatus.NewInIndex, repo.RetrieveStatus(filename));
Assert.Equal(1, repo.Stashes.Count());

repo.Stage(filename);
Expand Down Expand Up @@ -258,13 +258,13 @@ public void CanStashAndPop()
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Pop(0));
Assert.Equal(0, repo.Stashes.Count());

Assert.Equal(FileStatus.NewInWorkdir, repo.RetrieveStatus(filename));
Assert.Equal(FileStatus.NewInIndex, repo.RetrieveStatus(filename));
Assert.Equal(contents, File.ReadAllText(Path.Combine(repo.Info.WorkingDirectory, filename)));
}
}

[Fact]
public void StashReportsConflictsWhenReinstated()
public void StashFailsWithUncommittedChangesIntheIndex()
{
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
Expand All @@ -286,7 +286,7 @@ public void StashReportsConflictsWhenReinstated()
repo.Stage(filename);
Touch(repo.Info.WorkingDirectory, filename2, newContents);

Assert.Equal(StashApplyStatus.Conflicts, repo.Stashes.Pop(0, new StashApplyOptions
Assert.Equal(StashApplyStatus.UncommittedChanges, repo.Stashes.Pop(0, new StashApplyOptions
{
ApplyModifiers = StashApplyModifiers.ReinstateIndex,
}));
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void SetUpTestEnvironment()
{
IsFileSystemCaseSensitive = IsFileSystemCaseSensitiveInternal();

string initialAssemblyParentFolder = Directory.GetParent(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath).FullName;
string initialAssemblyParentFolder = Directory.GetParent(new Uri(typeof(BaseFixture).Assembly.EscapedCodeBase).LocalPath).FullName;

const string sourceRelativePath = @"../../Resources";
ResourcesDirectory = new DirectoryInfo(Path.Combine(initialAssemblyParentFolder, sourceRelativePath));
Expand Down
14 changes: 14 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/FileExportFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public FileExportFilter(string name, IEnumerable<FilterAttributeEntry> attribute
FilesFiltered = new HashSet<string>();
}

protected override void Create(string path, string root, FilterMode mode)
{
if (mode == FilterMode.Clean)
{
string filename = Path.GetFileName(path);
string cachePath = Path.Combine(root, ".git", filename);

if (File.Exists(cachePath))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethomson Is this implementation specific to this test or should something similar should always happen because of racy-git?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unique to this test filter. When going to the ODB, it writes a temp file with the data and returns a unique ID. Then it tests that when going to the workdir, it can replace the data with what was written in the temp file. (This test is basically a poor man's git-media or git-lfs.)

However if your filter was invoked twice (by git re-examining racily clean files, for example) then we should clean up that temp file before trying to append to it. Otherwise you would end up with it duplicated.

{
File.Delete(cachePath);
}
}
}

protected override void Clean(string path, string root, Stream input, Stream output)
{
CleanCalledCount++;
Expand Down
15 changes: 15 additions & 0 deletions LibGit2Sharp/Core/GitErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ internal enum GitErrorCode
/// </summary>
Peel = -19,

/// <summary>
/// Unexpected EOF.
/// </summary>
EndOfFile = -20,

/// <summary>
/// Invalid operation or input.
/// </summary>
Invalid = -21,

/// <summary>
/// Uncommitted changes in index prevented operation.
/// </summary>
Uncommitted = -22,

/// <summary>
/// Skip and passthrough the given ODB backend.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions LibGit2Sharp/Core/GitSubmoduleIgnore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace LibGit2Sharp.Core
{
internal enum GitSubmoduleIgnore
{
Unspecified = -1,
None = 1,
Untracked = 2,
Dirty = 3,
All = 4,
}
}
8 changes: 3 additions & 5 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,10 +1526,6 @@ internal static extern int git_submodule_add_to_index(
SubmoduleSafeHandle submodule,
[MarshalAs(UnmanagedType.Bool)] bool write_index);

[DllImport(libgit2)]
internal static extern int git_submodule_save(
SubmoduleSafeHandle submodule);

[DllImport(libgit2)]
internal static extern void git_submodule_free(
IntPtr submodule);
Expand Down Expand Up @@ -1576,7 +1572,9 @@ internal static extern int git_submodule_reload(
[DllImport(libgit2)]
internal static extern int git_submodule_status(
out SubmoduleStatus status,
SubmoduleSafeHandle submodule);
RepositorySafeHandle repo,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath name,
GitSubmoduleIgnore ignore);

[DllImport(libgit2)]
internal static extern int git_submodule_init(
Expand Down
15 changes: 7 additions & 8 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2679,6 +2679,11 @@ private static StashApplyStatus get_stash_status(int res)
return StashApplyStatus.Conflicts;
}

if (res == (int)GitErrorCode.Uncommitted)
{
return StashApplyStatus.UncommittedChanges;
}

if (res == (int)GitErrorCode.NotFound)
{
return StashApplyStatus.NotFound;
Expand Down Expand Up @@ -2805,12 +2810,6 @@ public static void git_submodule_add_to_index(SubmoduleSafeHandle submodule, boo
Ensure.ZeroResult(res);
}

public static void git_submodule_save(SubmoduleSafeHandle submodule)
{
var res = NativeMethods.git_submodule_save(submodule);
Ensure.ZeroResult(res);
}

public static void git_submodule_update(SubmoduleSafeHandle submodule, bool init, ref GitSubmoduleOptions options)
{
var res = NativeMethods.git_submodule_update(submodule, init, ref options);
Expand Down Expand Up @@ -2868,10 +2867,10 @@ public static void git_submodule_reload(SubmoduleSafeHandle submodule)
Ensure.ZeroResult(res);
}

public static SubmoduleStatus git_submodule_status(SubmoduleSafeHandle submodule)
public static SubmoduleStatus git_submodule_status(RepositorySafeHandle repo, string name)
{
SubmoduleStatus status;
var res = NativeMethods.git_submodule_status(out status, submodule);
var res = NativeMethods.git_submodule_status(out status, repo, name, GitSubmoduleIgnore.Unspecified);
Ensure.ZeroResult(res);
return status;
}
Expand Down
Loading