2
2
"""Utility functions for use in tests."""
3
3
4
4
from __future__ import (
5
- absolute_import , division , print_function , unicode_literals )
5
+ absolute_import ,
6
+ division ,
7
+ print_function ,
8
+ unicode_literals ,
9
+ )
6
10
7
11
import logging
8
12
import subprocess
13
+ import textwrap
9
14
from os import chdir , environ , mkdir
10
15
from os .path import abspath
11
16
from os .path import join as pjoin
@@ -55,34 +60,16 @@ def make_test_git():
55
60
# URL are not allowed and using a real URL will require Internet to clone
56
61
# the repo
57
62
check_output (['git' , 'checkout' , '-b' , 'submodule' , 'master' ], env = env )
58
- # https://stackoverflow.com/a/37378302/2187091
59
- mkdir (pjoin (directory , 'foobar' ))
60
- gitmodules_path = pjoin (directory , '.gitmodules' )
61
- with open (gitmodules_path , 'w' ) as fh :
62
- fh .write ('''[submodule "foobar"]\n \t path = foobar\n \t url = https://foobar.com/git\n ''' )
63
- check_output (
64
- [
65
- 'git' , 'update-index' , '--add' , '--cacheinfo' , '160000' ,
66
- '233febf4846d7a0aeb95b6c28962e06e21d13688' , 'foobar' ,
67
- ],
68
- env = env ,
63
+ add_submodule_without_cloning (
64
+ directory , 'foobar' , 'https://foobar.com/git'
69
65
)
70
66
check_output (['git' , 'add' , '.' ], env = env )
71
67
check_output (['git' , 'commit' , '-m"Add submodule"' ], env = env )
72
68
73
- # Add a relative submodule URL in the relativesubmodule branch
74
- check_output (['git' , 'checkout' , '-b' , 'relativesubmodule' , 'master' ], env = env )
75
- check_output (
76
- ['git' , 'submodule' , 'add' , '-b' , 'master' , './' , 'relativesubmodule' ],
77
- env = env
78
- )
79
- check_output (['git' , 'add' , '.' ], env = env )
80
- check_output (['git' , 'commit' , '-m"Add relative submodule"' ], env = env )
81
69
# Add an invalid submodule URL in the invalidsubmodule branch
82
70
check_output (['git' , 'checkout' , '-b' , 'invalidsubmodule' , 'master' ], env = env )
83
- check_output (
84
- ['git' , 'submodule' , 'add' , '-b' , 'master' , './' , 'invalidsubmodule' ],
85
- env = env ,
71
+ add_submodule_without_cloning (
72
+ directory ,
'invalid' ,
'[email protected] :rtfd/readthedocs.org.git'
86
73
)
87
74
check_output (['git' , 'add' , '.' ], env = env )
88
75
check_output (['git' , 'commit' , '-m"Add invalid submodule"' ], env = env )
@@ -92,6 +79,36 @@ def make_test_git():
92
79
return directory
93
80
94
81
82
+ @restoring_chdir
83
+ def add_submodule_without_cloning (directory , submodule , url ):
84
+ """
85
+ Add a submodule without cloning it.
86
+
87
+ We write directly to the git index, more details in:
88
+ https://stackoverflow.com/a/37378302/2187091
89
+ """
90
+ env = environ .copy ()
91
+ env ['GIT_DIR' ] = pjoin (directory , '.git' )
92
+ chdir (directory )
93
+
94
+ mkdir (pjoin (directory , submodule ))
95
+ gitmodules_path = pjoin (directory , '.gitmodules' )
96
+ with open (gitmodules_path , 'w+' ) as fh :
97
+ content = textwrap .dedent ('''
98
+ [submodule "{submodule}"]
99
+ path = {submodule}
100
+ url = {url}
101
+ ''' )
102
+ fh .write (content .format (submodule = submodule , url = url ))
103
+ check_output (
104
+ [
105
+ 'git' , 'update-index' , '--add' , '--cacheinfo' , '160000' ,
106
+ '233febf4846d7a0aeb95b6c28962e06e21d13688' , submodule ,
107
+ ],
108
+ env = env ,
109
+ )
110
+
111
+
95
112
@restoring_chdir
96
113
def make_git_repo (directory , name = 'sample_repo' ):
97
114
path = get_readthedocs_app_path ()
0 commit comments