From 65e5c6a6863ab20963c83da4797c6ac8494615f3 Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 2 Nov 2021 02:51:18 +0530 Subject: [PATCH] libgit2: Add more RemoteCallbacks Add SidebandProgressCallback to be able to cancel the network operation before any transfer operation. Add PushTransferProgressCallback to be able to cancel the push transfer operation. Signed-off-by: Sunny --- pkg/git/libgit2/transport.go | 40 +++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/git/libgit2/transport.go b/pkg/git/libgit2/transport.go index 6da358cb..e609fcb3 100644 --- a/pkg/git/libgit2/transport.go +++ b/pkg/git/libgit2/transport.go @@ -47,9 +47,11 @@ var ( func RemoteCallbacks(ctx context.Context, opts *git.AuthOptions) git2go.RemoteCallbacks { if opts != nil { return git2go.RemoteCallbacks{ - TransferProgressCallback: transferProgressCallback(ctx), - CredentialsCallback: credentialsCallback(opts), - CertificateCheckCallback: certificateCallback(opts), + SidebandProgressCallback: transportMessageCallback(ctx), + TransferProgressCallback: transferProgressCallback(ctx), + PushTransferProgressCallback: pushTransferProgressCallback(ctx), + CredentialsCallback: credentialsCallback(opts), + CertificateCheckCallback: certificateCallback(opts), } } return git2go.RemoteCallbacks{} @@ -73,6 +75,38 @@ func transferProgressCallback(ctx context.Context) git2go.TransferProgressCallba } } +// transportMessageCallback constructs TransportMessageCallback which signals +// libgit2 it should cancel the network operation when the given context is +// closed. +func transportMessageCallback(ctx context.Context) git2go.TransportMessageCallback { + return func(_ string) git2go.ErrorCode { + select { + case <-ctx.Done(): + return git2go.ErrorCodeUser + default: + return git2go.ErrorCodeOK + } + } +} + +// pushTransferProgressCallback constructs PushTransferProgressCallback which +// signals libgit2 it should stop the push transfer when the given context is +// closed (due to e.g. a timeout). +func pushTransferProgressCallback(ctx context.Context) git2go.PushTransferProgressCallback { + return func(current, total uint32, _ uint) git2go.ErrorCode { + // Early return if current equals total. + if current == total { + return git2go.ErrorCodeOK + } + select { + case <-ctx.Done(): + return git2go.ErrorCodeUser + default: + return git2go.ErrorCodeOK + } + } +} + // credentialsCallback constructs CredentialsCallbacks with the given options // for git.Transport, and returns the result. func credentialsCallback(opts *git.AuthOptions) git2go.CredentialsCallback {