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

Skip to content

Commit 0ab6702

Browse files
committed
wrote a test that now uses a callback woooooo
1 parent d925831 commit 0ab6702

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using LibGit2Sharp.Handlers;
56
using LibGit2Sharp.Tests.TestHelpers;
67
using Xunit;
78
using Xunit.Extensions;
@@ -503,6 +504,32 @@ public void IndexIsUpdatedAfterAddingFile()
503504
}
504505
}
505506

507+
[Fact]
508+
public void CanSpecifyCallbackWhenIsInvokedByUpdating()
509+
{
510+
const string fileName = "new-file.txt";
511+
512+
var path = SandboxAssumeUnchangedTestRepo();
513+
using (var repo = new Repository(path))
514+
{
515+
repo.Index.Clear();
516+
517+
Touch(repo.Info.WorkingDirectory, fileName, "hello test file\n");
518+
519+
repo.Index.Add(fileName);
520+
521+
var count = 0;
522+
Touch(repo.Info.WorkingDirectory, fileName, "rewrite the file\n");
523+
IndexUpdaterHandler callback = (file, pathspec) =>
524+
{
525+
count++;
526+
return 0;
527+
};
528+
repo.Index.Update(callback);
529+
530+
Assert.Equal(1, count);
531+
}
532+
}
506533

507534
private static void AddSomeCornerCases(Repository repo)
508535
{

LibGit2Sharp/Core/Proxy.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ public static IndexReucEntrySafeHandle git_index_reuc_get_bypath(IndexSafeHandle
11351135
return NativeMethods.git_index_reuc_get_bypath(index, path);
11361136
}
11371137

1138-
public static void git_index_update_all(IndexSafeHandle index, IEnumerable<string> filePaths)
1138+
public static void git_index_update_all(IndexSafeHandle index, IEnumerable<string> filePaths, NativeMethods.git_index_matched_path_cb callback)
11391139
{
11401140
// maybe not?
11411141
using (ThreadAffinity())
@@ -1145,10 +1145,6 @@ public static void git_index_update_all(IndexSafeHandle index, IEnumerable<strin
11451145
try
11461146
{
11471147
array = GitStrArrayManaged.BuildFrom(filePaths.ToArray());
1148-
NativeMethods.git_index_matched_path_cb callback = (path, pathspec, payload) =>
1149-
{
1150-
return 0;
1151-
};
11521148
var res = NativeMethods.git_index_update_all(index, ref array.Array, callback, IntPtr.Zero);
11531149
Ensure.ZeroResult(res);
11541150
}

LibGit2Sharp/Handlers.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ namespace LibGit2Sharp.Handlers
112112
/// <param name="problematicRefspec">The refspec which didn't match the default.</param>
113113
public delegate void RemoteRenameFailureHandler(string problematicRefspec);
114114

115+
/// <summary>
116+
/// Delegate definition for index updater callback
117+
/// </summary>
118+
/// <param name="path">Path of the file to be updated.</param>
119+
/// <param name="pathSpec">Pathspec which caught the path.</param>
120+
/// <returns>
121+
/// 0 to update the file, a positive number to skip the file, or a negative number to signal an error
122+
/// </returns>
123+
public delegate int IndexUpdaterHandler(string path, string pathSpec);
124+
115125
/// <summary>
116126
/// The stages of pack building.
117127
/// </summary>

LibGit2Sharp/Index.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using System.Collections.Generic;
44
using System.Diagnostics;
55
using System.Globalization;
6+
using System.Linq;
67
using LibGit2Sharp.Core;
78
using LibGit2Sharp.Core.Handles;
9+
using LibGit2Sharp.Handlers;
810

911
namespace LibGit2Sharp
1012
{
@@ -231,7 +233,7 @@ public virtual void Add(Blob blob, string indexEntryPath, Mode indexEntryMode)
231233
/// </summary>
232234
public virtual void Update()
233235
{
234-
Proxy.git_index_update_all(Handle, new [] { "*" });
236+
Proxy.git_index_update_all(Handle, new [] { "*" }, null);
235237
}
236238

237239
/// <summary>
@@ -242,20 +244,33 @@ public virtual void Update()
242244
/// </param>
243245
public virtual void Update(string pathSpec)
244246
{
245-
Proxy.git_index_update_all(Handle, new[] { pathSpec });
247+
Proxy.git_index_update_all(Handle, new[] { pathSpec }, null);
246248
}
247249

248250
/// <summary>
249-
/// Update files for given pathspecs
251+
/// Update files for a given set of pathspecs
250252
/// </summary>
251253
/// <param name="pathSpecs">
252254
/// Limit the scope of paths to update to the provided pathspecs
253255
/// </param>
254256
public virtual void Update(IEnumerable<string> pathSpecs)
255257
{
256-
Proxy.git_index_update_all(Handle, pathSpecs);
258+
Proxy.git_index_update_all(Handle, pathSpecs, null);
257259
}
258260

261+
/// <summary>
262+
/// Update files based on a given callback
263+
/// </summary>
264+
/// <param name="indexUpdaterHandler">
265+
/// Callback to process file programatically
266+
/// </param>
267+
public virtual void Update(IndexUpdaterHandler indexUpdaterHandler)
268+
{
269+
var callback = IndexCallbacks.ToCallback(indexUpdaterHandler);
270+
271+
Proxy.git_index_update_all(Handle, new[] { "*" }, callback);
272+
273+
}
259274

260275
private void UpdatePhysicalIndex()
261276
{

LibGit2Sharp/IndexCallbacks.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using LibGit2Sharp.Core;
6+
using LibGit2Sharp.Handlers;
7+
8+
namespace LibGit2Sharp
9+
{
10+
internal class IndexCallbacks
11+
{
12+
public static NativeMethods.git_index_matched_path_cb ToCallback(IndexUpdaterHandler handler)
13+
{
14+
NativeMethods.git_index_matched_path_cb cb = (path, pathSpec, intPtr) =>
15+
{
16+
if (handler != null)
17+
{
18+
return handler(path, pathSpec);
19+
}
20+
return 0;
21+
};
22+
return cb;
23+
}
24+
}
25+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<Compile Include="Core\Handles\IndexReucEntrySafeHandle.cs" />
8282
<Compile Include="EntryExistsException.cs" />
8383
<Compile Include="FetchOptionsBase.cs" />
84+
<Compile Include="IndexCallbacks.cs" />
8485
<Compile Include="LogEntry.cs" />
8586
<Compile Include="FollowFilter.cs" />
8687
<Compile Include="IBelongToARepository.cs" />

0 commit comments

Comments
 (0)