浏览代码

git: Find additional version metadata such as is_remote and commit id

Jan Holthuis 5 年之前
父节点
当前提交
1f826ea099
共有 1 个文件被更改,包括 19 次插入10 次删除
  1. 19 10
      sphinx_multiversion/git.py

+ 19 - 10
sphinx_multiversion/git.py

@@ -7,7 +7,9 @@ from . import sphinx
 
 
 VersionRef = collections.namedtuple('VersionRef', [
 VersionRef = collections.namedtuple('VersionRef', [
     'name',
     'name',
+    'commit',
     'source',
     'source',
+    'is_remote',
     'refname',
     'refname',
     'version',
     'version',
     'release',
     'release',
@@ -15,18 +17,20 @@ VersionRef = collections.namedtuple('VersionRef', [
 
 
 
 
 def get_refs(gitroot):
 def get_refs(gitroot):
-    cmd = ("git", "for-each-ref", "--format", "%(refname)", "refs")
+    cmd = ("git", "for-each-ref", "--format", "%(objectname) %(refname)", "refs")
     output = subprocess.check_output(cmd, cwd=gitroot).decode()
     output = subprocess.check_output(cmd, cwd=gitroot).decode()
     for line in output.splitlines():
     for line in output.splitlines():
-        refname = line.strip()
-
+        line = line.strip()
         # Parse refname
         # Parse refname
-        matchobj = re.match(r"^refs/(heads|tags|remotes/[^/]+)/(\S+)$", refname)
+        matchobj = re.match(r"^(\w+) refs/(heads|tags|remotes/[^/]+)/(\S+)$", line)
         if not matchobj:
         if not matchobj:
             continue
             continue
-        source = matchobj.group(1)
-        name = matchobj.group(2)
-        yield (name, source, refname)
+        commit = matchobj.group(1)
+        source = matchobj.group(2)
+        name = matchobj.group(3)
+        refname = line.partition(' ')[2]
+
+        yield (name, commit, source, refname)
 
 
 
 
 def get_conf(gitroot, refname, confpath):
 def get_conf(gitroot, refname, confpath):
@@ -36,14 +40,19 @@ def get_conf(gitroot, refname, confpath):
 
 
 
 
 def find_versions(gitroot, confpath, tag_whitelist, branch_whitelist, remote_whitelist):
 def find_versions(gitroot, confpath, tag_whitelist, branch_whitelist, remote_whitelist):
-    for name, source, refname in get_refs(gitroot):
+    for name, commit, source, refname in get_refs(gitroot):
+        is_remote = False
         if source == 'tags':
         if source == 'tags':
             if tag_whitelist is None or not re.match(tag_whitelist, name):
             if tag_whitelist is None or not re.match(tag_whitelist, name):
                 continue
                 continue
         elif source == 'heads':
         elif source == 'heads':
             if branch_whitelist is None or not re.match(branch_whitelist, name):
             if branch_whitelist is None or not re.match(branch_whitelist, name):
                 continue
                 continue
-        elif remote_whitelist is not None and re.match(remote_whitelist, source):
+        elif source.startswith('remotes/') and remote_whitelist is not None:
+            is_remote = True
+            remote_name = source.partition('/')[2]
+            if not re.match(remote_whitelist, remote_name):
+                continue
             if branch_whitelist is None or not re.match(branch_whitelist, name):
             if branch_whitelist is None or not re.match(branch_whitelist, name):
                 continue
                 continue
         else:
         else:
@@ -53,7 +62,7 @@ def find_versions(gitroot, confpath, tag_whitelist, branch_whitelist, remote_whi
         config = sphinx.parse_conf(conf)
         config = sphinx.parse_conf(conf)
         version = config['version']
         version = config['version']
         release = config['release']
         release = config['release']
-        yield VersionRef(name, source, refname, version, release)
+        yield VersionRef(name, commit, source, is_remote, refname, version, release)
 
 
 
 
 def shallow_clone(src, dst, branch):
 def shallow_clone(src, dst, branch):