77from typing import Sequence
88
99import pre_commit .constants as C
10- from pre_commit import git
1110from pre_commit .envcontext import envcontext
1211from pre_commit .envcontext import PatchesT
1312from pre_commit .envcontext import Var
1413from pre_commit .hook import Hook
1514from pre_commit .languages import helpers
1615from pre_commit .prefix import Prefix
1716from pre_commit .util import cmd_output
18- from pre_commit .util import cmd_output_b
1917from pre_commit .util import rmtree
2018
2119ENVIRONMENT_DIR = 'golangenv'
@@ -36,53 +34,28 @@ def in_env(prefix: Prefix) -> Generator[None, None, None]:
3634 yield
3735
3836
39- def guess_go_dir (remote_url : str ) -> str :
40- if remote_url .endswith ('.git' ):
41- remote_url = remote_url [:- 1 * len ('.git' )]
42- looks_like_url = (
43- not remote_url .startswith ('file://' ) and
44- ('//' in remote_url or '@' in remote_url )
45- )
46- remote_url = remote_url .replace (':' , '/' )
47- if looks_like_url :
48- _ , _ , remote_url = remote_url .rpartition ('//' )
49- _ , _ , remote_url = remote_url .rpartition ('@' )
50- return remote_url
51- else :
52- return 'unknown_src_dir'
53-
54-
5537def install_environment (
5638 prefix : Prefix ,
5739 version : str ,
5840 additional_dependencies : Sequence [str ],
5941) -> None :
6042 helpers .assert_version_default ('golang' , version )
61- directory = helpers .environment_dir (prefix , ENVIRONMENT_DIR , C .DEFAULT )
62-
63- remote = git .get_remote_url (prefix .prefix_dir )
64- repo_src_dir = os .path .join (directory , 'src' , guess_go_dir (remote ))
65-
66- # Clone into the goenv we'll create
67- cmd = ('git' , 'clone' , '--recursive' , '.' , repo_src_dir )
68- helpers .run_setup_cmd (prefix , cmd )
43+ env_dir = helpers .environment_dir (prefix , ENVIRONMENT_DIR , version )
6944
7045 if sys .platform == 'cygwin' : # pragma: no cover
71- _ , gopath , _ = cmd_output ('cygpath' , '-w' , directory )
72- gopath = gopath .strip ()
46+ gopath = cmd_output ('cygpath' , '-w' , env_dir )[1 ].strip ()
7347 else :
74- gopath = directory
48+ gopath = env_dir
7549 env = dict (os .environ , GOPATH = gopath )
7650 env .pop ('GOBIN' , None )
77- cmd_output_b ('go' , 'install' , './...' , cwd = repo_src_dir , env = env )
51+
52+ helpers .run_setup_cmd (prefix , ('go' , 'install' , './...' ), env = env )
7853 for dependency in additional_dependencies :
79- cmd_output_b (
80- 'go' , 'install' , dependency , cwd = repo_src_dir , env = env ,
81- )
82- # Same some disk space, we don't need these after installation
83- rmtree (prefix .path (directory , 'src' ))
84- pkgdir = prefix .path (directory , 'pkg' )
85- if os .path .exists (pkgdir ): # pragma: no cover (go<1.10)
54+ helpers .run_setup_cmd (prefix , ('go' , 'install' , dependency ), env = env )
55+
56+ # save some disk space -- we don't need this after installation
57+ pkgdir = os .path .join (env_dir , 'pkg' )
58+ if os .path .exists (pkgdir ): # pragma: no branch (always true on windows?)
8659 rmtree (pkgdir )
8760
8861
0 commit comments