using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Handlers;
namespace LibGit2Sharp
{
///
/// Class to translate libgit2 callbacks into delegates exposed by LibGit2Sharp.
/// Handles generating libgit2 git_remote_callbacks datastructure given a set
/// of LibGit2Sharp delegates and handles propagating libgit2 callbacks into
/// corresponding LibGit2Sharp exposed delegates.
///
internal class RemoteCallbacks
{
internal RemoteCallbacks(ProgressHandler onProgress = null, CompletionHandler onCompletion = null, UpdateTipsHandler onUpdateTips = null)
{
Progress = onProgress;
Completion = onCompletion;
UpdateTips = onUpdateTips;
}
#region Delegates
///
/// Progress callback. Corresponds to libgit2 progress callback.
///
private readonly ProgressHandler Progress;
///
/// UpdateTips callback. Corresponds to libgit2 update_tips callback.
///
private readonly UpdateTipsHandler UpdateTips;
///
/// Completion callback. Corresponds to libgit2 Completion callback.
///
private readonly CompletionHandler Completion;
#endregion
internal GitRemoteCallbacks GenerateCallbacks()
{
GitRemoteCallbacks callbacks = new GitRemoteCallbacks {version = 1};
if (Progress != null)
{
callbacks.progress = GitProgressHandler;
}
if (UpdateTips != null)
{
callbacks.update_tips = GitUpdateTipsHandler;
}
if (Completion != null)
{
callbacks.completion = GitCompletionHandler;
}
return callbacks;
}
#region Handlers to respond to callbacks raised by libgit2
///
/// Handler for libgit2 Progress callback. Converts values
/// received from libgit2 callback to more suitable types
/// and calls delegate provided by LibGit2Sharp consumer.
///
/// IntPtr to string from libgit2
/// length of string
///
private void GitProgressHandler(IntPtr str, int len, IntPtr data)
{
ProgressHandler onProgress = Progress;
if (onProgress != null)
{
string message = Utf8Marshaler.FromNative(str, len);
onProgress(message);
}
}
///
/// Handler for libgit2 update_tips callback. Converts values
/// received from libgit2 callback to more suitable types
/// and calls delegate provided by LibGit2Sharp consumer.
///
/// IntPtr to string
/// Old reference ID
/// New referene ID
///
///
private int GitUpdateTipsHandler(IntPtr str, ref GitOid oldId, ref GitOid newId, IntPtr data)
{
UpdateTipsHandler onUpdateTips = UpdateTips;
int result = 0;
if (onUpdateTips != null)
{
string refName = Utf8Marshaler.FromNative(str);
result = onUpdateTips(refName, oldId, newId);
}
return result;
}
///
/// Handler for libgit2 completion callback. Converts values
/// received from libgit2 callback to more suitable types
/// and calls delegate provided by LibGit2Sharp consumer.
///
///
///
///
private int GitCompletionHandler(RemoteCompletionType remoteCompletionType, IntPtr data)
{
CompletionHandler completion = Completion;
int result = 0;
if (completion != null)
{
result = completion(remoteCompletionType);
}
return result;
}
#endregion
}
}