Skip to content

Commit 65e5c6a

Browse files
committed
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 <[email protected]>
1 parent d407c82 commit 65e5c6a

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

pkg/git/libgit2/transport.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ var (
4747
func RemoteCallbacks(ctx context.Context, opts *git.AuthOptions) git2go.RemoteCallbacks {
4848
if opts != nil {
4949
return git2go.RemoteCallbacks{
50-
TransferProgressCallback: transferProgressCallback(ctx),
51-
CredentialsCallback: credentialsCallback(opts),
52-
CertificateCheckCallback: certificateCallback(opts),
50+
SidebandProgressCallback: transportMessageCallback(ctx),
51+
TransferProgressCallback: transferProgressCallback(ctx),
52+
PushTransferProgressCallback: pushTransferProgressCallback(ctx),
53+
CredentialsCallback: credentialsCallback(opts),
54+
CertificateCheckCallback: certificateCallback(opts),
5355
}
5456
}
5557
return git2go.RemoteCallbacks{}
@@ -73,6 +75,38 @@ func transferProgressCallback(ctx context.Context) git2go.TransferProgressCallba
7375
}
7476
}
7577

78+
// transportMessageCallback constructs TransportMessageCallback which signals
79+
// libgit2 it should cancel the network operation when the given context is
80+
// closed.
81+
func transportMessageCallback(ctx context.Context) git2go.TransportMessageCallback {
82+
return func(_ string) git2go.ErrorCode {
83+
select {
84+
case <-ctx.Done():
85+
return git2go.ErrorCodeUser
86+
default:
87+
return git2go.ErrorCodeOK
88+
}
89+
}
90+
}
91+
92+
// pushTransferProgressCallback constructs PushTransferProgressCallback which
93+
// signals libgit2 it should stop the push transfer when the given context is
94+
// closed (due to e.g. a timeout).
95+
func pushTransferProgressCallback(ctx context.Context) git2go.PushTransferProgressCallback {
96+
return func(current, total uint32, _ uint) git2go.ErrorCode {
97+
// Early return if current equals total.
98+
if current == total {
99+
return git2go.ErrorCodeOK
100+
}
101+
select {
102+
case <-ctx.Done():
103+
return git2go.ErrorCodeUser
104+
default:
105+
return git2go.ErrorCodeOK
106+
}
107+
}
108+
}
109+
76110
// credentialsCallback constructs CredentialsCallbacks with the given options
77111
// for git.Transport, and returns the result.
78112
func credentialsCallback(opts *git.AuthOptions) git2go.CredentialsCallback {

0 commit comments

Comments
 (0)