@@ -18,6 +18,7 @@ from doc_builder.build_docs import (
1818 main as build_docs ,
1919)
2020from doc_builder .build_docs_shared_args import main as build_docs_shared_args
21+ from doc_builder .sys_utils import get_git_head_or_branch , check_permanent_file
2122
2223# Change to the parent director of doc-builder and add to Python path
2324os .chdir (os .path .join (os .path .dirname (__file__ ), os .pardir ))
@@ -40,36 +41,6 @@ STATIC_DIR = os.path.join(SOURCE, "_static")
4041TEMPLATES_DIR = os .path .join (SOURCE , "_templates" )
4142
4243
43- def get_git_head_or_branch ():
44- """
45- Get the name of the current branch. If detached HEAD, get current commit SHA.
46- """
47- try :
48- result = subprocess .run (
49- ["git" , "symbolic-ref" , "--short" , "-q" , "HEAD" ],
50- stdout = subprocess .PIPE ,
51- stderr = subprocess .DEVNULL ,
52- text = True ,
53- check = True ,
54- )
55- output = result .stdout .strip ()
56- except subprocess .CalledProcessError :
57- output = ""
58-
59- if not output :
60- # Fallback to commit SHA
61- result = subprocess .run (
62- ["git" , "rev-parse" , "HEAD" ],
63- stdout = subprocess .PIPE ,
64- stderr = subprocess .DEVNULL ,
65- text = True ,
66- check = True ,
67- )
68- output = result .stdout .strip ()
69-
70- return output
71-
72-
7344def checkout_and_build (version , args ):
7445 """
7546 Check out docs for a version and build
@@ -82,21 +53,9 @@ def checkout_and_build(version, args):
8253 # the permanent_files list.
8354 permanent_files = [CONF_PY , VERSIONS_PY , STATIC_DIR , TEMPLATES_DIR , "doc-builder" ]
8455
85- # Check "permanent" files: Each should (a) exist and (b) not have any uncommitted changes.
56+ # Check some things about "permanent" files before checkout
8657 for filename in permanent_files :
87- # Error if doesn't exist
88- if not os .path .exists (filename ):
89- raise FileNotFoundError (filename )
90- # Error if it contains uncommitted changes
91- cmd = f"git add . && git diff --quiet { filename } && git diff --cached --quiet { filename } "
92- try :
93- subprocess .check_output (cmd , shell = True )
94- except subprocess .CalledProcessError as e :
95- subprocess .check_output (
96- "git reset" , shell = True
97- ) # Unstage files staged by `git add`
98- msg = f"Important file/submodule may contain uncommitted changes: '{ filename } '"
99- raise RuntimeError (msg ) from e
58+ check_permanent_file (filename )
10059
10160 # Check out the git reference of this version (branch name, tag, or commit SHA)
10261 subprocess .check_output ("git checkout " + version .ref , shell = True )
0 commit comments