From a935aec8abea990c94f6e209ac710eb885dec5b3 Mon Sep 17 00:00:00 2001 From: Hidetoshi Hirokawa Date: Wed, 3 Apr 2019 13:01:37 +0900 Subject: [PATCH] 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. --- docs/man/git-lfs-config.5.ronn | 7 ++++ t/t-pull.sh | 58 +++++++++++++++++++--------------- t/t-push.sh | 6 ++++ tq/adapterbase.go | 12 ++++++- 4 files changed, 56 insertions(+), 27 deletions(-) diff --git a/docs/man/git-lfs-config.5.ronn b/docs/man/git-lfs-config.5.ronn index 2dafc876..b97f4dd3 100644 --- a/docs/man/git-lfs-config.5.ronn +++ b/docs/man/git-lfs-config.5.ronn @@ -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` diff --git a/t/t-pull.sh b/t/t-pull.sh index 1ee024e1..a9d433c5 100755 --- a/t/t-pull.sh +++ b/t/t-pull.sh @@ -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 diff --git a/t/t-push.sh b/t/t-push.sh index 8ea1f107..991c5dbb 100755 --- a/t/t-push.sh +++ b/t/t-push.sh @@ -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 diff --git a/tq/adapterbase.go b/tq/adapterbase.go index d29243d7..f01e6a64 100644 --- a/tq/adapterbase.go +++ b/tq/adapterbase.go @@ -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]