Skip to content

Commit

Permalink
Merge pull request #255 from klauer/enh_template_git
Browse files Browse the repository at this point in the history
ENH: add git information to the template tool, if available
  • Loading branch information
klauer authored Mar 22, 2021
2 parents bdd9d58 + 5b51174 commit f81f0a9
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions pytmc/bin/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
projects - {project_filename: {...}, ...}
others - {filename: {...}, ...}
If installed and available, ``git_info`` will be available on each project.
The following helpers are available in the environment::
config_to_pragma
Expand Down Expand Up @@ -55,6 +57,12 @@
from . import pragmalint, stcmd, summary, util
from .db import process as db_process

try:
import git
except ImportError:
git = None


DESCRIPTION = __doc__

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -90,6 +98,77 @@ def build_arg_parser(argparser=None):
return argparser


def find_git_root(fn: pathlib.Path) -> Optional[pathlib.Path]:
"""Given a file, find the git repository root (if it exists)."""
while fn:
if (fn / ".git").exists():
return fn
fn = fn.parent


def _to_http_url(url: str) -> str:
"""Git over SSH -> GitHub https URL."""
if url.startswith("[email protected]:"):
_, repo_slug = url.split(':')
return f"https://github.com/{repo_slug}"
return url


def _to_doc_url(url: str) -> str:
"""Git over SSH -> GitHub https URL."""
try:
org, repo = _to_repo_slug(url).split('/')
return f"https://{org}.github.io/{repo}"
except Exception:
return ""


def _to_tree_url(url: str, hash: str) -> str:
"""Get a github.com/org/repo/tree/master-style URL."""
url = _to_http_url(url)
if url.startswith("https://github.com"):
return f"{url}/tree/{hash}"
return url


def _to_repo_slug(url: str) -> str:
"""Get a org/repo from a full URL."""
url = _to_http_url(url)
github = "https://github.com/"
if url.startswith(github):
return url.split(github)[1]
return url


def get_git_info(fn: pathlib.Path) -> str:
"""Get the git hash and other info for the repository that ``fn`` is in."""
if git is None:
raise RuntimeError("gitpython not installed")
repo = git.Repo(find_git_root(fn))
urls = [
url
for remote in repo.remotes
for url in remote.urls
]
repo_slugs = [_to_repo_slug(url) for url in urls]
head_sha = repo.head.commit.hexsha
try:
desc = repo.git.describe("--contains", head_sha)
except git.GitCommandError:
desc = repo.git.describe("--always", "--tags")

return {
"describe": desc,
"sha": head_sha,
"repo_slug": repo_slugs[0] if repo_slugs else None,
"repo_slugs": repo_slugs,
"doc_urls": [_to_doc_url(url) for url in urls],
"repo_urls": [_to_http_url(url) for url in urls],
"tree_urls": [_to_tree_url(url, head_sha) for url in urls],
"repo": repo,
}


def project_to_dict(path: str) -> dict:
"""
Create a user/template-facing dictionary of project information.
Expand All @@ -114,6 +193,23 @@ def project_to_dict(path: str) -> dict:
for fn in project_files
}

for fn, project in projects.items():
try:
project.git_info = get_git_info(fn)
except Exception:
project.git_info = {
"describe": "unknown",
"sha": "unknown",
"urls": [],
"links": [],
"repo_slug": "unknown",
"repo_slugs": [],
"doc_urls": [],
"repo_urls": [],
"tree_urls": [],
"repo": None,
}

solutions = {solution: projects} if solution is not None else {}

return {
Expand Down

0 comments on commit f81f0a9

Please sign in to comment.