Skip to content

Commit

Permalink
libgit2: Configured libgit2 clone ProxyOptions
Browse files Browse the repository at this point in the history
This configures ProxyOptions for all libgit2 Checkout functions when
cloning and configures the options based on current environment
settings using the http.ProxyFromEnvironment function.

Refs: fluxcd#131
Signed-off-by: Robert Clarke <rob@robertandrewclarke.com>
  • Loading branch information
racdev committed Dec 15, 2021
1 parent 3f5da11 commit 0c98a05
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/git/libgit2/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package libgit2
import (
"context"
"fmt"
"net/http"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -64,6 +65,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
FetchOptions: &git2go.FetchOptions{
DownloadTags: git2go.DownloadTagsNone,
RemoteCallbacks: RemoteCallbacks(ctx, opts),
ProxyOptions: getProxyOptions(url),
},
CheckoutBranch: c.Branch,
})
Expand Down Expand Up @@ -93,6 +95,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git.
FetchOptions: &git2go.FetchOptions{
DownloadTags: git2go.DownloadTagsAll,
RemoteCallbacks: RemoteCallbacks(ctx, opts),
ProxyOptions: getProxyOptions(url),
},
})
if err != nil {
Expand All @@ -116,6 +119,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, opts *g
FetchOptions: &git2go.FetchOptions{
DownloadTags: git2go.DownloadTagsNone,
RemoteCallbacks: RemoteCallbacks(ctx, opts),
ProxyOptions: getProxyOptions(url),
},
})
if err != nil {
Expand Down Expand Up @@ -147,6 +151,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, opts *g
FetchOptions: &git2go.FetchOptions{
DownloadTags: git2go.DownloadTagsAll,
RemoteCallbacks: RemoteCallbacks(ctx, opts),
ProxyOptions: getProxyOptions(url),
},
})
if err != nil {
Expand Down Expand Up @@ -305,3 +310,14 @@ func buildSignature(s *git2go.Signature) git.Signature {
When: s.When,
}
}

func getProxyOptions(url string) git2go.ProxyOptions {
proxyOptions := git2go.ProxyOptions{}
req, _ := http.NewRequest("GET", url, nil)
proxy, _ := http.ProxyFromEnvironment(req)
if proxy != nil {
proxyOptions.Type = git2go.ProxyTypeSpecified
proxyOptions.Url = proxy.String()
}
return proxyOptions
}

0 comments on commit 0c98a05

Please sign in to comment.