Browse Source

Fix failure to find refs when not called from repo root

Resolves #24.
Jan Holthuis 4 years ago
parent
commit
552f71e166
2 changed files with 23 additions and 4 deletions
  1. 12 2
      sphinx_multiversion/git.py
  2. 11 2
      sphinx_multiversion/main.py

+ 12 - 2
sphinx_multiversion/git.py

@@ -16,6 +16,16 @@ GitRef = collections.namedtuple(
 logger = logging.getLogger(__name__)
 
 
+def get_toplevel_path(cwd=None):
+    cmd = (
+        "git",
+        "rev-parse",
+        "--show-toplevel",
+    )
+    output = subprocess.check_output(cmd, cwd=cwd).decode()
+    return output.rstrip("\n")
+
+
 def get_all_refs(gitroot):
     cmd = (
         "git",
@@ -136,7 +146,7 @@ def file_exists(gitroot, refname, filename):
     return proc.returncode == 0
 
 
-def copy_tree(src, dst, reference, sourcepath="."):
+def copy_tree(gitroot, src, dst, reference, sourcepath="."):
     with tempfile.SpooledTemporaryFile() as fp:
         cmd = (
             "git",
@@ -147,7 +157,7 @@ def copy_tree(src, dst, reference, sourcepath="."):
             "--",
             sourcepath,
         )
-        subprocess.check_call(cmd, stdout=fp)
+        subprocess.check_call(cmd, cwd=gitroot, stdout=fp)
         fp.seek(0)
         with tarfile.TarFile(fileobj=fp) as tarfp:
             tarfp.extractall(dst)

+ 11 - 2
sphinx_multiversion/main.py

@@ -63,6 +63,8 @@ def main(argv=None):
     if args.noconfig:
         return 1
 
+    logger = logging.getLogger(__name__)
+
     sourcedir_absolute = os.path.abspath(args.sourcedir)
     confdir_absolute = (
         os.path.abspath(args.confdir)
@@ -96,12 +98,19 @@ def main(argv=None):
     config.init_values()
 
     # Get relative paths to root of git repository
-    gitroot = pathlib.Path(".").resolve()
+    gitroot = pathlib.Path(
+        git.get_toplevel_path(cwd=sourcedir_absolute)
+    ).resolve()
+    logger.debug("Git toplevel path: %s", str(gitroot))
     sourcedir = os.path.relpath(sourcedir_absolute, str(gitroot))
+    logger.debug(
+        "Source dir (relative to git toplevel path): %s", str(sourcedir)
+    )
     if args.confdir:
         confdir = os.path.relpath(confdir_absolute, str(gitroot))
     else:
         confdir = sourcedir
+    logger.debug("Conf dir (relative to git toplevel path): %s", str(confdir))
     conffile = os.path.join(confdir, "conf.py")
 
     # Get git references
@@ -129,7 +138,7 @@ def main(argv=None):
             # Clone Git repo
             repopath = os.path.join(tmp, gitref.commit)
             try:
-                git.copy_tree(gitroot.as_uri(), repopath, gitref)
+                git.copy_tree(str(gitroot), gitroot.as_uri(), repopath, gitref)
             except (OSError, subprocess.CalledProcessError):
                 logger.error(
                     "Failed to copy git tree for %s to %s",