-
Notifications
You must be signed in to change notification settings - Fork 899
Register custom filters #854
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file renamed
BIN
+5.44 MB
Lib/NativeBinaries/amd64/git2-e0902fb.pdb → Lib/NativeBinaries/amd64/git2-fb2f3a7.pdb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file renamed
BIN
+5.43 MB
Lib/NativeBinaries/x86/git2-e0902fb.pdb → Lib/NativeBinaries/x86/git2-fb2f3a7.pdb
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using LibGit2Sharp.Tests.TestHelpers; | ||
using Xunit; | ||
|
||
namespace LibGit2Sharp.Tests | ||
{ | ||
public class FilterFixture : BaseFixture | ||
{ | ||
private const int GitPassThrough = -30; | ||
|
||
readonly Func<Stream, Stream, int> successCallback = (reader, writer) => 0; | ||
|
||
private const string FilterName = "the-filter"; | ||
readonly List<FilterAttributeEntry> attributes = new List<FilterAttributeEntry> { new FilterAttributeEntry("test") }; | ||
|
||
[Fact] | ||
public void CanRegisterFilterWithSingleAttribute() | ||
{ | ||
var filter = new EmptyFilter(FilterName, attributes); | ||
Assert.Equal( attributes , filter.Attributes); | ||
} | ||
|
||
[Fact] | ||
public void CanRegisterAndUnregisterTheSameFilter() | ||
{ | ||
var filter = new EmptyFilter(FilterName + 1, attributes); | ||
|
||
var registration = GlobalSettings.RegisterFilter(filter); | ||
GlobalSettings.DeregisterFilter(registration); | ||
|
||
var secondRegistration = GlobalSettings.RegisterFilter(filter); | ||
GlobalSettings.DeregisterFilter(secondRegistration); | ||
} | ||
|
||
[Fact] | ||
public void CanRegisterAndDeregisterAfterGarbageCollection() | ||
{ | ||
var filter = new EmptyFilter(FilterName + 2, attributes); | ||
var filterRegistration = GlobalSettings.RegisterFilter(filter); | ||
|
||
GC.Collect(); | ||
|
||
GlobalSettings.DeregisterFilter(filterRegistration); | ||
} | ||
|
||
[Fact] | ||
public void SameFilterIsEqual() | ||
{ | ||
var filter = new EmptyFilter(FilterName + 3, attributes); | ||
Assert.Equal(filter, filter); | ||
} | ||
|
||
[Fact] | ||
public void InitCallbackNotMadeWhenFilterNeverUsed() | ||
{ | ||
bool called = false; | ||
Func<int> initializeCallback = () => | ||
{ | ||
called = true; | ||
return 0; | ||
}; | ||
|
||
var filter = new FakeFilter(FilterName + 11, attributes, | ||
successCallback, | ||
successCallback, | ||
initializeCallback); | ||
|
||
var filterRegistration = GlobalSettings.RegisterFilter(filter); | ||
|
||
Assert.False(called); | ||
|
||
GlobalSettings.DeregisterFilter(filterRegistration); | ||
} | ||
|
||
[Fact] | ||
public void InitCallbackMadeWhenUsingTheFilter() | ||
{ | ||
bool called = false; | ||
Func<int> initializeCallback = () => | ||
{ | ||
called = true; | ||
return 0; | ||
}; | ||
|
||
var filter = new FakeFilter(FilterName + 12, attributes, | ||
successCallback, | ||
successCallback, | ||
initializeCallback); | ||
|
||
var filterRegistration = GlobalSettings.RegisterFilter(filter); | ||
Assert.False(called); | ||
|
||
string repoPath = InitNewRepository(); | ||
using (var repo = CreateTestRepository(repoPath)) | ||
{ | ||
StageNewFile(repo); | ||
|
||
} | ||
|
||
GlobalSettings.DeregisterFilter(filterRegistration); | ||
} | ||
|
||
[Fact] | ||
public void WhenStagingFileApplyIsCalledWithCleanForCorrectPath() | ||
{ | ||
string repoPath = InitNewRepository(); | ||
bool called = false; | ||
|
||
Func<Stream, Stream, int> clean = (reader, writer) => | ||
{ | ||
called = true; | ||
return GitPassThrough; | ||
}; | ||
var filter = new FakeFilter(FilterName + 15, attributes, clean); | ||
|
||
var filterRegistration = GlobalSettings.RegisterFilter(filter); | ||
|
||
using (var repo = CreateTestRepository(repoPath)) | ||
{ | ||
StageNewFile(repo); | ||
Assert.True(called); | ||
} | ||
|
||
GlobalSettings.DeregisterFilter(filterRegistration); | ||
} | ||
|
||
[Fact] | ||
public void CleanFilterWritesOutputToObjectTree() | ||
{ | ||
const string decodedInput = "This is a substitution cipher"; | ||
const string encodedInput = "Guvf vf n fhofgvghgvba pvcure"; | ||
|
||
string repoPath = InitNewRepository(); | ||
|
||
Func<Stream, Stream, int> cleanCallback = SubstitutionCipherFilter.RotateByThirteenPlaces; | ||
|
||
var filter = new FakeFilter(FilterName + 16, attributes, cleanCallback); | ||
|
||
var filterRegistration = GlobalSettings.RegisterFilter(filter); | ||
|
||
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); | ||
} | ||
|
||
GlobalSettings.DeregisterFilter(filterRegistration); | ||
} | ||
|
||
|
||
[Fact] | ||
public void WhenCheckingOutAFileFileSmudgeWritesCorrectFileToWorkingDirectory() | ||
{ | ||
const string decodedInput = "This is a substitution cipher"; | ||
const string encodedInput = "Guvf vf n fhofgvghgvba pvcure"; | ||
|
||
const string branchName = "branch"; | ||
string repoPath = InitNewRepository(); | ||
|
||
Func<Stream, Stream, int> smudgeCallback = SubstitutionCipherFilter.RotateByThirteenPlaces; | ||
|
||
var filter = new FakeFilter(FilterName + 17, attributes, null, smudgeCallback); | ||
var filterRegistration = 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); | ||
|
||
GlobalSettings.DeregisterFilter(filterRegistration); | ||
} | ||
|
||
[Fact] | ||
public void FilterStreamsAreCoherent() | ||
{ | ||
string repoPath = InitNewRepository(); | ||
|
||
bool? inputCanWrite = null, inputCanRead = null, inputCanSeek = null; | ||
bool? outputCanWrite = null, outputCanRead = null, outputCanSeek = null; | ||
|
||
Func<Stream, Stream, int> assertor = (input, output) => | ||
{ | ||
inputCanRead = input.CanRead; | ||
inputCanWrite = input.CanWrite; | ||
inputCanSeek = input.CanSeek; | ||
|
||
outputCanRead = output.CanRead; | ||
outputCanWrite = output.CanWrite; | ||
outputCanSeek = output.CanSeek; | ||
|
||
return GitPassThrough; | ||
}; | ||
|
||
var filter = new FakeFilter(FilterName + 18, attributes, assertor, assertor); | ||
|
||
var filterRegistration = GlobalSettings.RegisterFilter(filter); | ||
|
||
using (var repo = CreateTestRepository(repoPath)) | ||
{ | ||
StageNewFile(repo); | ||
} | ||
|
||
GlobalSettings.DeregisterFilter(filterRegistration); | ||
|
||
Assert.True(inputCanRead.HasValue); | ||
Assert.True(inputCanWrite.HasValue); | ||
Assert.True(inputCanSeek.HasValue); | ||
Assert.True(outputCanRead.HasValue); | ||
Assert.True(outputCanWrite.HasValue); | ||
Assert.True(outputCanSeek.HasValue); | ||
|
||
Assert.True(inputCanRead.Value); | ||
Assert.False(inputCanWrite.Value); | ||
Assert.False(inputCanSeek.Value); | ||
|
||
Assert.False(outputCanRead.Value); | ||
Assert.True(outputCanWrite.Value); | ||
Assert.False(outputCanSeek.Value); | ||
} | ||
|
||
private FileInfo CheckoutFileForSmudge(string repoPath, string branchName, string content) | ||
{ | ||
FileInfo expectedPath; | ||
using (var repo = CreateTestRepository(repoPath)) | ||
{ | ||
StageNewFile(repo, content); | ||
|
||
repo.Commit("Initial commit"); | ||
|
||
expectedPath = CommitFileOnBranch(repo, branchName, content); | ||
|
||
repo.Checkout("master"); | ||
|
||
repo.Checkout(branchName); | ||
} | ||
return expectedPath; | ||
} | ||
|
||
private static FileInfo CommitFileOnBranch(Repository repo, string branchName, String content) | ||
{ | ||
var branch = repo.CreateBranch(branchName); | ||
repo.Checkout(branch.Name); | ||
|
||
FileInfo expectedPath = StageNewFile(repo, content); | ||
repo.Commit("Commit"); | ||
return expectedPath; | ||
} | ||
|
||
private static FileInfo StageNewFile(IRepository repo, string contents = "null") | ||
{ | ||
string newFilePath = Touch(repo.Info.WorkingDirectory, Guid.NewGuid() + ".txt", contents); | ||
var stageNewFile = new FileInfo(newFilePath); | ||
repo.Stage(newFilePath); | ||
return stageNewFile; | ||
} | ||
|
||
private Repository CreateTestRepository(string path) | ||
{ | ||
string configPath = CreateConfigurationWithDummyUser(Constants.Signature); | ||
var repositoryOptions = new RepositoryOptions { GlobalConfigurationLocation = configPath }; | ||
var repository = new Repository(path, repositoryOptions); | ||
CreateAttributesFile(repository, "* filter=test"); | ||
return repository; | ||
} | ||
|
||
private static void CreateAttributesFile(IRepository repo, string attributeEntry) | ||
{ | ||
Touch(repo.Info.WorkingDirectory, ".gitattributes", attributeEntry); | ||
} | ||
|
||
class EmptyFilter : Filter | ||
{ | ||
public EmptyFilter(string name, IEnumerable<FilterAttributeEntry> attributes) | ||
: base(name, attributes) | ||
{ } | ||
} | ||
|
||
class FakeFilter : Filter | ||
{ | ||
private readonly Func<Stream, Stream, int> cleanCallback; | ||
private readonly Func<Stream, Stream, int> smudgeCallback; | ||
private readonly Func<int> initCallback; | ||
|
||
public FakeFilter(string name, IEnumerable<FilterAttributeEntry> attributes, | ||
Func<Stream, Stream, int> cleanCallback = null, | ||
Func<Stream, Stream, int> smudgeCallback = null, | ||
Func<int> initCallback = null) | ||
: base(name, attributes) | ||
{ | ||
this.cleanCallback = cleanCallback; | ||
this.smudgeCallback = smudgeCallback; | ||
this.initCallback = initCallback; | ||
} | ||
|
||
protected override int Clean(string path, Stream input, Stream output) | ||
{ | ||
return cleanCallback != null ? cleanCallback(input, output) : base.Clean(path, input, output); | ||
} | ||
|
||
protected override int Smudge(string path, Stream input, Stream output) | ||
{ | ||
return smudgeCallback != null ? smudgeCallback(input, output) : base.Smudge(path, input, output); | ||
} | ||
|
||
protected override int Initialize() | ||
{ | ||
return initCallback != null ? initCallback() : base.Initialize(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is the consumer going to know about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could use the
GitErrorCode.PassThrough
in the test fixtures. This might help people looking for code samples to not hard code-30