tq/adapterbase: add lfs.transfer.enablehrefrewrite config

For backward compatibility and some incompatible situations such as
using SSH protocol, href-rewriting is only enabled if
lfs.transfer.enablehrefrewrite is set to true explicitly.
This commit is contained in:
Hidetoshi Hirokawa 2019-04-03 13:01:37 +09:00
parent 7da4901957
commit a935aec8ab
4 changed files with 56 additions and 27 deletions

@ -162,6 +162,13 @@ be scoped inside the configuration for a remote.
not an integer, is less than one, or is not given, a default value of three
will be used instead.
* `lfs.transfer.enablehrefrewrite`
If set to true, this enables rewriting href of LFS objects using
`url.*.insteadof/pushinsteadof` config. `pushinsteadof` is used only for
uploading, and `insteadof` is used for downloading and for uploading when
`pushinsteadof` is not set.
### Push settings
* `lfs.allowincompletepush`

@ -248,6 +248,38 @@ begin_test "pull with multiple remotes"
)
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/"
# Enable href rewriting explicitly.
git config lfs.transfer.enablehrefrewrite true
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
# lfs-pull succeed after unsetting enableHrefRerite config
git config --unset lfs.transfer.enablehrefrewrite
git lfs pull
)
end_test
begin_test "pull: with missing object"
(
set -e
@ -289,29 +321,3 @@ 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

@ -676,6 +676,8 @@ begin_test "push with invalid pushInsteadof"
# set pushInsteadOf to rewrite the href of uploading LFS object.
git config url."$GITSERVER/storage/invalid".pushInsteadOf "$GITSERVER/storage/"
# Enable href rewriting explicitly.
git config lfs.transfer.enablehrefrewrite true
set +e
git lfs push origin master > push.log 2>&1
@ -686,5 +688,9 @@ begin_test "push with invalid pushInsteadof"
# check rewritten href is used to upload LFS object.
grep "LFS: Authorization error: $GITSERVER/storage/invalid" push.log
# lfs-push succeed after unsetting enableHrefRerite config
git config --unset lfs.transfer.enablehrefrewrite
git lfs push origin master
)
end_test

@ -51,6 +51,11 @@ type transferImplementation interface {
DoTransfer(ctx interface{}, t *Transfer, cb ProgressCallback, authOkFunc func()) error
}
const (
enableHrefReriteKey = "lfs.transfer.enablehrefrewrite"
defaultEnableHrefRerite = false
)
func newAdapterBase(f *fs.Filesystem, name string, dir Direction, ti transferImplementation) *adapterBase {
return &adapterBase{
fs: f,
@ -194,7 +199,12 @@ func (a *adapterBase) worker(workerNum int, ctx interface{}) {
var httpRE = regexp.MustCompile(`\Ahttps?://`)
func (a *adapterBase) newHTTPRequest(method string, rel *Action) (*http.Request, error) {
href := a.apiClient.Endpoints.NewEndpoint(a.direction.String(), rel.Href).Url
enableRewrite := a.apiClient.GitEnv().Bool(enableHrefReriteKey, defaultEnableHrefRerite)
href := rel.Href
if enableRewrite {
href = a.apiClient.Endpoints.NewEndpoint(a.direction.String(), rel.Href).Url
}
if !httpRE.MatchString(href) {
urlfragment := strings.SplitN(href, "?", 2)[0]