diff options
author | Paul Betts <paul@paulbetts.org> | 2013-01-22 03:06:51 +0400 |
---|---|---|
committer | Paul Betts <paul@paulbetts.org> | 2013-01-22 03:08:47 +0400 |
commit | dc1848cf8169ccb85f088983115fd8506eae367c (patch) | |
tree | 1a28f4598bbb000d1265d16f87d850450edeb1cf | |
parent | 4e73ab988de6f6442eb5906416d6a6526530cb8a (diff) |
Add Ignore support
This PR adds support for the new .gitignore methods in libgit2
-rw-r--r-- | LibGit2Sharp.Tests/IgnoreFixture.cs | 53 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 14 | ||||
-rw-r--r-- | LibGit2Sharp/Core/Proxy.cs | 35 | ||||
-rw-r--r-- | LibGit2Sharp/Ignore.cs | 61 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 13 |
7 files changed, 178 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/IgnoreFixture.cs b/LibGit2Sharp.Tests/IgnoreFixture.cs new file mode 100644 index 00000000..5ae91f15 --- /dev/null +++ b/LibGit2Sharp.Tests/IgnoreFixture.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using LibGit2Sharp.Tests.TestHelpers; +using Xunit; + +namespace LibGit2Sharp.Tests +{ + public class IgnoreFixture : BaseFixture + { + [Fact] + public void TemporaryRulesShouldApplyUntilCleared() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); + using (var repo = new Repository(path.RepositoryPath)) + { + File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, "Foo.cs"), "Bar"); + + Assert.True(repo.Index.RetrieveStatus().Untracked.Contains("Foo.cs")); + + repo.Ignore.AddTemporaryRules(new[] { "*.cs" }); + + Assert.False(repo.Index.RetrieveStatus().Untracked.Contains("Foo.cs")); + + repo.Ignore.ResetAllTemporaryRules(); + + Assert.True(repo.Index.RetrieveStatus().Untracked.Contains("Foo.cs")); + } + } + + [Fact] + public void IsPathIgnoredShouldVerifyWhetherPathIsIgnored() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); + using (var repo = new Repository(path.RepositoryPath)) + { + File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, "Foo.cs"), "Bar"); + + Assert.False(repo.Ignore.IsPathIgnored("Foo.cs")); + + repo.Ignore.AddTemporaryRules(new[] { "*.cs" }); + + Assert.True(repo.Ignore.IsPathIgnored("Foo.cs")); + + repo.Ignore.ResetAllTemporaryRules(); + + Assert.False(repo.Ignore.IsPathIgnored("Foo.cs")); + } + } + } +} diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj index 2639e2a4..feb184c3 100644 --- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj +++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj @@ -59,6 +59,7 @@ <ItemGroup> <Compile Include="CheckoutFixture.cs" /> <Compile Include="CloneFixture.cs" /> + <Compile Include="IgnoreFixture.cs" /> <Compile Include="MergeFixture.cs" /> <Compile Include="CleanFixture.cs" /> <Compile Include="CurrentOperationFixture.cs" /> diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 31b2a763..ecfd1e6a 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -395,6 +395,20 @@ namespace LibGit2Sharp.Core internal static extern int git_graph_ahead_behind(out UIntPtr ahead, out UIntPtr behind, RepositorySafeHandle repo, ref GitOid one, ref GitOid two); [DllImport(libgit2)] + internal static extern int git_ignore_add_rule( + RepositorySafeHandle repo, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Utf8Marshaler))] string rules); + + [DllImport(libgit2)] + internal static extern int git_ignore_clear_internal_rules(RepositorySafeHandle repo); + + [DllImport(libgit2)] + internal static extern int git_ignore_path_is_ignored( + out int ignored, + RepositorySafeHandle repo, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof (Utf8Marshaler))] string path); + + [DllImport(libgit2)] internal static extern int git_index_add_from_workdir( IndexSafeHandle index, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath path); diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index e704a856..c8d7b790 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -634,6 +634,41 @@ namespace LibGit2Sharp.Core return new Tuple<int, int>((int)ahead, (int)behind); } } + + #endregion + + #region git_ignore_ + + public static void git_ignore_add_rule(RepositorySafeHandle repo, string rules) + { + using (ThreadAffinity()) + { + int res = NativeMethods.git_ignore_add_rule(repo, rules); + Ensure.Success(res); + } + } + + public static void git_ignore_clear_internal_rules(RepositorySafeHandle repo) + { + using (ThreadAffinity()) + { + int res = NativeMethods.git_ignore_clear_internal_rules(repo); + Ensure.Success(res); + } + } + + public static bool git_ignore_path_is_ignored(RepositorySafeHandle repo, string path) + { + using (ThreadAffinity()) + { + int ignored; + int res = NativeMethods.git_ignore_path_is_ignored(out ignored, repo, path); + Ensure.Success(res); + + return (ignored != 0); + } + } + #endregion #region git_index_ diff --git a/LibGit2Sharp/Ignore.cs b/LibGit2Sharp/Ignore.cs new file mode 100644 index 00000000..a3367bd6 --- /dev/null +++ b/LibGit2Sharp/Ignore.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using LibGit2Sharp.Core; + +namespace LibGit2Sharp +{ + public class Ignore + { + private readonly Repository repo; + + /// <summary> + /// Needed for mocking purposes. + /// </summary> + protected Ignore() + { } + + internal Ignore(Repository repo) + { + this.repo = repo; + } + + /// <summary> + /// Adds a custom .gitignore rule that will be applied to futher operations to the Index. This is in addition + /// to the standard .gitignore rules that would apply as a result of the system/user/repo .gitignore + /// </summary> + /// <param name="rules">The content of a .gitignore file that will be applied.</param> + public virtual void AddTemporaryRules(IEnumerable<string> rules) + { + var allRules = rules.Aggregate(new StringBuilder(), (acc, x) => + { + acc.Append(x); + acc.Append("\n"); + return acc; + }); + + Proxy.git_ignore_add_rule(repo.Handle, allRules.ToString()); + } + + /// <summary> + /// Resets all custom rules that were applied via calls to <see cref="Repository.AddCustomIgnoreRules"/> - note that + /// this will not affect the application of the user/repo .gitignores. + /// </summary> + public virtual void ResetAllTemporaryRules() + { + Proxy.git_ignore_clear_internal_rules(repo.Handle); + } + + /// <summary> + /// Given a relative path, this method determines whether a path should be ignored, checking + /// both the custom ignore rules as well as the "normal" .gitignores. + /// </summary> + /// <param name="relativePath">A path relative to the repository</param> + /// <returns>true if the path should be ignored.</returns> + public virtual bool IsPathIgnored(string relativePath) + { + return Proxy.git_ignore_path_is_ignored(repo.Handle, relativePath); + } + } +} diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index fe693252..4e8f1ce1 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -89,6 +89,7 @@ <Compile Include="DiffOptions.cs" /> <Compile Include="DiffTargets.cs" /> <Compile Include="Handlers.cs" /> + <Compile Include="Ignore.cs" /> <Compile Include="MergeConflictException.cs" /> <Compile Include="NameConflictException.cs" /> <Compile Include="ReferenceCollectionExtensions.cs" /> diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 3b9fa492..379c7448 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -33,6 +33,7 @@ namespace LibGit2Sharp private readonly NoteCollection notes; private readonly Lazy<ObjectDatabase> odb; private readonly Stack<IDisposable> toCleanup = new Stack<IDisposable>(); + private readonly Ignore ignore; private static readonly Lazy<string> versionRetriever = new Lazy<string>(RetrieveVersion); /// <summary> @@ -103,6 +104,7 @@ namespace LibGit2Sharp odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this)); diff = new Diff(this); notes = new NoteCollection(this); + ignore = new Ignore(this); EagerlyLoadTheConfigIfAnyPathHaveBeenPassed(options); } @@ -187,6 +189,17 @@ namespace LibGit2Sharp } /// <summary> + /// Manipulate the currently ignored files. + /// </summary> + public Ignore Ignore + { + get + { + return ignore; + } + } + + /// <summary> /// Gets the database. /// </summary> public ObjectDatabase ObjectDatabase |