浏览代码

Skip git refs without sourcedir or conf.py

Jan Holthuis 4 年之前
父节点
当前提交
0f12d7b9d5
共有 2 个文件被更改,包括 26 次插入9 次删除
  1. 16 1
      sphinx_multiversion/git.py
  2. 10 8
      sphinx_multiversion/main.py

+ 16 - 1
sphinx_multiversion/git.py

@@ -48,7 +48,7 @@ def get_all_refs(gitroot):
         yield GitRef(name, commit, source, is_remote, refname, creatordate)
 
 
-def get_refs(gitroot, tag_whitelist, branch_whitelist, remote_whitelist):
+def get_refs(gitroot, tag_whitelist, branch_whitelist, remote_whitelist, files=()):
     for ref in get_all_refs(gitroot):
         if ref.source == "tags":
             if tag_whitelist is None or not re.match(tag_whitelist, ref.name):
@@ -69,9 +69,24 @@ def get_refs(gitroot, tag_whitelist, branch_whitelist, remote_whitelist):
         else:
             continue
 
+        if not all(file_exists(gitroot, ref.name, filename)
+                   for filename in files):
+            continue
+
         yield ref
 
 
+def file_exists(gitroot, refname, filename):
+    cmd = (
+        "git",
+        "cat-file",
+        "-e",
+        "{}:{}".format(refname, filename),
+    )
+    proc = subprocess.run(cmd, cwd=gitroot, capture_output=True)
+    return proc.returncode == 0
+
+
 def copy_tree(src, dst, reference, sourcepath="."):
     with tempfile.SpooledTemporaryFile() as fp:
         cmd = (

+ 10 - 8
sphinx_multiversion/main.py

@@ -91,13 +91,22 @@ def main(argv=None):
     config.pre_init_values()
     config.init_values()
 
-    # Get git references
+    # Get relative paths to root of git repository
     gitroot = pathlib.Path(".").resolve()
+    sourcedir = os.path.relpath(args.sourcedir, str(gitroot))
+    if args.confdir:
+        confdir = os.path.relpath(args.confdir, str(gitroot))
+    else:
+        confdir = sourcedir
+    conffile = os.path.join(confdir, "conf.py")
+
+    # Get git references
     gitrefs = git.get_refs(
         str(gitroot),
         config.smv_tag_whitelist,
         config.smv_branch_whitelist,
         config.smv_remote_whitelist,
+        files=(sourcedir, conffile),
     )
 
     # Order git refs
@@ -108,13 +117,6 @@ def main(argv=None):
 
     logger = logging.getLogger(__name__)
 
-    # Get Sourcedir
-    sourcedir = os.path.relpath(args.sourcedir, str(gitroot))
-    if args.confdir:
-        confdir = os.path.relpath(args.confdir, str(gitroot))
-    else:
-        confdir = sourcedir
-
     with tempfile.TemporaryDirectory() as tmp:
         # Generate Metadata
         metadata = {}