Skip to content

Commit 2dac92c

Browse files
authored
Merge pull request pre-commit#1789 from paulhfischer/recursive_golang
added recursive repository support for golang
2 parents e6caddb + 833bbf7 commit 2dac92c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

pre_commit/languages/golang.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def install_environment(
6969
repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))
7070

7171
# Clone into the goenv we'll create
72-
helpers.run_setup_cmd(prefix, ('git', 'clone', '.', repo_src_dir))
72+
cmd = ('git', 'clone', '--recursive', '.', repo_src_dir)
73+
helpers.run_setup_cmd(prefix, cmd)
7374

7475
if sys.platform == 'cygwin': # pragma: no cover
7576
_, gopath, _ = cmd_output('cygpath', '-w', directory)

tests/repository_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re_assert
1111

1212
import pre_commit.constants as C
13+
from pre_commit import git
1314
from pre_commit.clientlib import CONFIG_SCHEMA
1415
from pre_commit.clientlib import load_manifest
1516
from pre_commit.envcontext import envcontext
@@ -346,6 +347,59 @@ def test_golang_hook_still_works_when_gobin_is_set(tempdir_factory, store):
346347
assert os.listdir(gobin_dir) == []
347348

348349

350+
def test_golang_with_recursive_submodule(tmpdir, tempdir_factory, store):
351+
sub_go = '''\
352+
package sub
353+
354+
import "fmt"
355+
356+
func Func() {
357+
fmt.Println("hello hello world")
358+
}
359+
'''
360+
sub = tmpdir.join('sub').ensure_dir()
361+
sub.join('sub.go').write(sub_go)
362+
cmd_output('git', '-C', str(sub), 'init', '.')
363+
cmd_output('git', '-C', str(sub), 'add', '.')
364+
git.commit(str(sub))
365+
366+
pre_commit_hooks = '''\
367+
- id: example
368+
name: example
369+
entry: example
370+
language: golang
371+
verbose: true
372+
'''
373+
go_mod = '''\
374+
module github.com/asottile/example
375+
376+
go 1.14
377+
'''
378+
main_go = '''\
379+
package main
380+
381+
import "github.com/asottile/example/sub"
382+
383+
func main() {
384+
sub.Func()
385+
}
386+
'''
387+
repo = tmpdir.join('repo').ensure_dir()
388+
repo.join('.pre-commit-hooks.yaml').write(pre_commit_hooks)
389+
repo.join('go.mod').write(go_mod)
390+
repo.join('main.go').write(main_go)
391+
cmd_output('git', '-C', str(repo), 'init', '.')
392+
cmd_output('git', '-C', str(repo), 'add', '.')
393+
cmd_output('git', '-C', str(repo), 'submodule', 'add', str(sub), 'sub')
394+
git.commit(str(repo))
395+
396+
config = make_config_from_repo(str(repo))
397+
hook = _get_hook(config, store, 'example')
398+
ret, out = _hook_run(hook, (), color=False)
399+
assert ret == 0
400+
assert _norm_out(out) == b'hello hello world\n'
401+
402+
349403
def test_rust_hook(tempdir_factory, store):
350404
_test_hook_repo(
351405
tempdir_factory, store, 'rust_hooks_repo',

0 commit comments

Comments
 (0)