tq/adapterbase: support rewriting href

Use insteadOf/pushInsteadOf aliases to rewrite href to upload/download
LFS objects. This is useful in situations such like you need to access
the LFS server via a reverse proxy.
This commit is contained in:
Hidetoshi Hirokawa 2019-04-02 17:48:21 +09:00
parent 141093398e
commit 7da4901957
3 changed files with 52 additions and 3 deletions

@ -289,3 +289,29 @@ begin_test "pull: outside git repository"
grep "Not in a git repository" pull.log
)
end_test
begin_test "pull with invalid insteadof"
(
set -e
mkdir insteadof
cd insteadof
git init
git lfs install --local --skip-smudge
git remote add origin "$GITSERVER/t-pull"
git pull origin master
# set insteadOf to rewrite the href of downloading LFS object.
git config url."$GITSERVER/storage/invalid".insteadOf "$GITSERVER/storage/"
set +e
git lfs pull > pull.log 2>&1
res=$?
set -e
[ "$res" = "2" ]
# check rewritten href is used to download LFS object.
grep "LFS: Repository or object not found: $GITSERVER/storage/invalid" pull.log
)
end_test

@ -667,3 +667,24 @@ begin_test "push with deprecated _links"
assert_server_object "$reponame" "$contents_oid"
)
end_test
begin_test "push with invalid pushInsteadof"
(
set -e
push_repo_setup "push-invalid-pushinsteadof"
# set pushInsteadOf to rewrite the href of uploading LFS object.
git config url."$GITSERVER/storage/invalid".pushInsteadOf "$GITSERVER/storage/"
set +e
git lfs push origin master > push.log 2>&1
res=$?
set -e
[ "$res" = "2" ]
# check rewritten href is used to upload LFS object.
grep "LFS: Authorization error: $GITSERVER/storage/invalid" push.log
)
end_test

@ -194,12 +194,14 @@ func (a *adapterBase) worker(workerNum int, ctx interface{}) {
var httpRE = regexp.MustCompile(`\Ahttps?://`)
func (a *adapterBase) newHTTPRequest(method string, rel *Action) (*http.Request, error) {
if !httpRE.MatchString(rel.Href) {
urlfragment := strings.SplitN(rel.Href, "?", 2)[0]
href := a.apiClient.Endpoints.NewEndpoint(a.direction.String(), rel.Href).Url
if !httpRE.MatchString(href) {
urlfragment := strings.SplitN(href, "?", 2)[0]
return nil, fmt.Errorf("missing protocol: %q", urlfragment)
}
req, err := http.NewRequest(method, rel.Href, nil)
req, err := http.NewRequest(method, href, nil)
if err != nil {
return nil, err
}