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_git_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_git_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,43 @@ def make_test_git():
92
79
return directory
93
80
94
81
82
+ @restoring_chdir
83
+ def add_git_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
+ :param directory: The directory where the git repo is
91
+ :type directory: str
92
+ :param submodule: The name of the submodule to be created
93
+ :type submodule: str
94
+ :param url: The url where the submodule points to
95
+ :type url: str
96
+ """
97
+ env = environ .copy ()
98
+ env ['GIT_DIR' ] = pjoin (directory , '.git' )
99
+ chdir (directory )
100
+
101
+ mkdir (pjoin (directory , submodule ))
102
+ gitmodules_path = pjoin (directory , '.gitmodules' )
103
+ with open (gitmodules_path , 'w+' ) as fh :
104
+ content = textwrap .dedent ('''
105
+ [submodule "{submodule}"]
106
+ path = {submodule}
107
+ url = {url}
108
+ ''' )
109
+ fh .write (content .format (submodule = submodule , url = url ))
110
+ check_output (
111
+ [
112
+ 'git' , 'update-index' , '--add' , '--cacheinfo' , '160000' ,
113
+ '233febf4846d7a0aeb95b6c28962e06e21d13688' , submodule ,
114
+ ],
115
+ env = env ,
116
+ )
117
+
118
+
95
119
@restoring_chdir
96
120
def make_git_repo (directory , name = 'sample_repo' ):
97
121
path = get_readthedocs_app_path ()
0 commit comments